blob: 7ef1ad937d4240a4ccaa62462c32ab1a24a46e73 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Hall Liud892bec2018-11-30 14:51:45 -080019import static android.content.pm.PackageManager.PERMISSION_GRANTED;
20
Tyler Gunn7bcdc742019-10-04 15:56:59 -070021import static com.android.internal.telephony.PhoneConstants.PHONE_TYPE_IMS;
Ta-wei Yen87c49842016-05-13 21:19:52 -070022import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
23
Ta-wei Yen30a69c82016-12-27 14:52:32 -080024import android.Manifest.permission;
Hall Liua1548bd2019-12-24 14:14:12 -080025import android.annotation.NonNull;
Tyler Gunnf70ed162019-04-03 15:28:53 -070026import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080028import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070029import android.content.ComponentName;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070030import android.content.ContentResolver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.content.Context;
32import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070033import android.content.SharedPreferences;
Nathan Harold31d7ff32018-10-15 20:20:30 -070034import android.content.pm.ApplicationInfo;
Derek Tan740e1672017-06-27 14:56:27 -070035import android.content.pm.ComponentInfo;
Amith Yamasani6e118872016-02-19 12:53:51 -080036import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070037import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.net.Uri;
39import android.os.AsyncResult;
40import android.os.Binder;
Hall Liuf19c44f2018-11-27 14:38:17 -080041import android.os.Build;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.os.Bundle;
43import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070044import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070045import android.os.Looper;
46import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070047import android.os.Messenger;
Hall Liua1548bd2019-12-24 14:14:12 -080048import android.os.ParcelFileDescriptor;
Malcolm Chen6ca97372019-07-01 16:28:21 -070049import android.os.ParcelUuid;
Tyler Gunn65d45c22017-06-05 11:22:26 -070050import android.os.PersistableBundle;
Shuo Qiancd19c462020-01-16 20:51:11 -080051import android.os.Process;
Brad Ebinger5f64b052017-12-14 14:26:15 -080052import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070053import android.os.ResultReceiver;
Brad Ebinger1ce9c432019-07-16 13:19:44 -070054import android.os.ServiceSpecificException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070056import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070057import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070058import android.preference.PreferenceManager;
Naina Nallurid63128d2019-09-17 14:10:30 -070059import android.provider.DeviceConfig;
Ihab Awadf2177b72013-11-25 13:33:23 -080060import android.provider.Settings;
Amit Mahajan7dbbd822019-03-13 17:33:47 -070061import android.provider.Telephony;
Inseob Kim14bb3d02018-12-13 17:11:34 +090062import android.sysprop.TelephonyProperties;
Santos Cordon7a1885b2015-02-03 11:15:19 -080063import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080064import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070065import android.telecom.TelecomManager;
Chen Xu227e06f2019-09-26 22:48:11 -070066import android.telephony.Annotation.ApnType;
Shuo Qian4a594052020-01-23 11:59:30 -080067import android.telephony.CallForwardingInfo;
Junda Liu12f7d802015-05-01 12:06:44 -070068import android.telephony.CarrierConfigManager;
Michele Berionne482f8202018-11-27 18:57:59 -080069import android.telephony.CarrierRestrictionRules;
yincheng zhao2737e882019-09-06 17:06:54 -070070import android.telephony.CellIdentity;
Meng Wanga10e89e2019-12-09 13:13:01 -080071import android.telephony.CellIdentityCdma;
72import android.telephony.CellIdentityGsm;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import android.telephony.CellInfo;
Nathan Haroldf180aac2018-06-01 18:43:55 -070074import android.telephony.CellInfoGsm;
75import android.telephony.CellInfoWcdma;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070076import android.telephony.ClientRequestStats;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -070077import android.telephony.ICellInfoCallback;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070078import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080079import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070080import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080081import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070082import android.telephony.NetworkScanRequest;
Michele4245e952019-02-04 11:36:23 -080083import android.telephony.PhoneCapability;
Hall Liud892bec2018-11-30 14:51:45 -080084import android.telephony.PhoneNumberRange;
Wink Saville5d475dd2014-10-17 15:00:58 -070085import android.telephony.RadioAccessFamily;
Hall Liub2ac8ef2019-02-28 15:56:23 -080086import android.telephony.RadioAccessSpecifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080088import android.telephony.SignalStrength;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080089import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080090import android.telephony.SubscriptionManager;
Peter Wangc035ce42020-01-08 21:00:22 -080091import android.telephony.TelephonyFrameworkInitializer;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070092import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070093import android.telephony.TelephonyManager;
Hall Liub2ac8ef2019-02-28 15:56:23 -080094import android.telephony.TelephonyScanManager;
Jordan Liu5aa07002018-12-18 15:44:48 -080095import android.telephony.UiccCardInfo;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +000096import android.telephony.UiccSlotInfo;
Tyler Gunn65d45c22017-06-05 11:22:26 -070097import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -070098import android.telephony.VisualVoicemailSmsFilterSettings;
Jack Yub5d8f642018-11-26 11:20:48 -080099import android.telephony.data.ApnSetting;
100import android.telephony.emergency.EmergencyNumber;
Brad Ebinger1ce9c432019-07-16 13:19:44 -0700101import android.telephony.ims.ImsException;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800102import android.telephony.ims.ProvisioningManager;
Brad Ebingera34a6c22019-10-22 17:36:18 -0700103import android.telephony.ims.RegistrationManager;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700104import android.telephony.ims.aidl.IImsCapabilityCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800105import android.telephony.ims.aidl.IImsConfig;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -0700106import android.telephony.ims.aidl.IImsConfigCallback;
Brad Ebinger22bc3e42018-01-16 09:39:35 -0800107import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger35c841c2018-10-01 10:40:55 -0700108import android.telephony.ims.aidl.IImsRegistrationCallback;
Brad Ebingerbc7dd582019-10-17 17:03:22 -0700109import android.telephony.ims.feature.ImsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800110import android.telephony.ims.feature.MmTelFeature;
allenwtsu99c623b2020-01-03 18:24:23 +0800111import android.telephony.ims.feature.RcsFeature;
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800112import android.telephony.ims.stub.ImsConfigImplBase;
Brad Ebinger1f2b5082018-02-08 16:11:32 -0800113import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800115import android.util.ArraySet;
Hall Liud60acc92020-05-21 17:09:35 -0700116import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700117import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -0800118import android.util.Pair;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800119
Andrew Lee312e8172014-10-23 17:01:36 -0700120import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -0800121import com.android.ims.internal.IImsServiceFeatureCallback;
Shuo Qian4a594052020-01-23 11:59:30 -0800122import com.android.internal.telephony.CallForwardInfo;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700123import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700124import com.android.internal.telephony.CallStateException;
pkanwar79ec0542017-07-31 14:10:01 -0700125import com.android.internal.telephony.CarrierInfoManager;
chen xu651eec72018-11-11 19:03:44 -0800126import com.android.internal.telephony.CarrierResolver;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700127import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700128import com.android.internal.telephony.CommandException;
Shuo Qian4a594052020-01-23 11:59:30 -0800129import com.android.internal.telephony.CommandsInterface;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700130import com.android.internal.telephony.DefaultPhoneNotifier;
Nathan Harold48d6fd52019-02-06 19:01:40 -0800131import com.android.internal.telephony.HalVersion;
Hall Liu73f5d362020-01-20 13:42:00 -0800132import com.android.internal.telephony.IBooleanConsumer;
Hall Liu27d24262020-09-18 19:04:59 -0700133import com.android.internal.telephony.ICallForwardingInfoCallback;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700134import com.android.internal.telephony.IIntegerConsumer;
Hall Liud892bec2018-11-30 14:51:45 -0800135import com.android.internal.telephony.INumberVerificationCallback;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700136import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -0800137import com.android.internal.telephony.IccCard;
Jack Yu5f7092c2018-04-13 14:05:37 -0700138import com.android.internal.telephony.LocaleTracker;
yinxub1bed742017-04-17 11:45:04 -0700139import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700140import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700142import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800143import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700144import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700145import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700146import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700147import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -0700148import com.android.internal.telephony.RILConstants;
Jack Yu5f7092c2018-04-13 14:05:37 -0700149import com.android.internal.telephony.ServiceStateTracker;
Amit Mahajandccb3f12019-05-13 13:48:32 -0700150import com.android.internal.telephony.SmsController;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700151import com.android.internal.telephony.SmsPermissions;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800152import com.android.internal.telephony.SubscriptionController;
Peter Wang59571be2020-01-27 12:35:15 +0800153import com.android.internal.telephony.TelephonyIntents;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800154import com.android.internal.telephony.TelephonyPermissions;
Malcolm Chendc8c10e2019-04-10 18:25:07 -0700155import com.android.internal.telephony.dataconnection.ApnSettingUtils;
sqianf4ca7ed2019-01-15 18:32:07 -0800156import com.android.internal.telephony.emergency.EmergencyNumberTracker;
Derek Tan740e1672017-06-27 14:56:27 -0700157import com.android.internal.telephony.euicc.EuiccConnector;
Brad Ebinger9c0eb502019-01-23 15:06:19 -0800158import com.android.internal.telephony.ims.ImsResolver;
Tyler Gunn7bcdc742019-10-04 15:56:59 -0700159import com.android.internal.telephony.imsphone.ImsPhone;
160import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
Pengquan Meng6c2dc9f2019-02-06 11:12:53 -0800161import com.android.internal.telephony.metrics.TelephonyMetrics;
Meng Wangafbc5852019-09-19 17:37:13 -0700162import com.android.internal.telephony.uicc.IccCardApplicationStatus.AppType;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700163import com.android.internal.telephony.uicc.IccIoResult;
changbetty7157e9e2019-12-06 18:16:37 +0800164import com.android.internal.telephony.uicc.IccRecords;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700165import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800166import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700167import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800168import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700169import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800170import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000171import com.android.internal.telephony.uicc.UiccSlot;
zoey chenc730df82019-12-18 17:07:20 +0800172import com.android.internal.telephony.util.LocaleUtils;
fionaxu7ed723d2017-05-30 18:58:54 -0700173import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Hall Liu27d24262020-09-18 19:04:59 -0700174import com.android.internal.util.FunctionalUtils;
Jake Hambye994d462014-02-03 13:10:13 -0800175import com.android.internal.util.HexDump;
Brad Ebingera63db5f2019-04-23 16:31:13 -0700176import com.android.phone.settings.PickSmsSubscriptionActivity;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700177import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800178import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700179import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700180import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Peter Wang44b186e2020-01-13 23:33:09 -0800181import com.android.telephony.Rlog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800182
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700183import java.io.FileDescriptor;
184import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800186import java.util.Arrays;
sqian11b7a0e2018-12-05 18:48:28 -0800187import java.util.HashMap;
sqianf4ca7ed2019-01-15 18:32:07 -0800188import java.util.HashSet;
Jake Hambye994d462014-02-03 13:10:13 -0800189import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100190import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800191import java.util.Map;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700192import java.util.NoSuchElementException;
sqianf4ca7ed2019-01-15 18:32:07 -0800193import java.util.Set;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800194import java.util.concurrent.atomic.AtomicBoolean;
Hall Liu73f5d362020-01-20 13:42:00 -0800195import java.util.function.Consumer;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700196
197/**
198 * Implementation of the ITelephony interface.
199 */
Santos Cordon117fee72014-05-16 17:56:12 -0700200public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 private static final String LOG_TAG = "PhoneInterfaceManager";
202 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
203 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800204 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700205
206 // Message codes used with mMainThreadHandler
207 private static final int CMD_HANDLE_PIN_MMI = 1;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700208 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
209 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700210 private static final int CMD_OPEN_CHANNEL = 9;
211 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
212 private static final int CMD_CLOSE_CHANNEL = 11;
213 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800214 private static final int CMD_NV_READ_ITEM = 13;
215 private static final int EVENT_NV_READ_ITEM_DONE = 14;
216 private static final int CMD_NV_WRITE_ITEM = 15;
217 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
218 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
219 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
chen xu6dac5ab2018-10-26 17:39:23 -0700220 private static final int CMD_RESET_MODEM_CONFIG = 19;
221 private static final int EVENT_RESET_MODEM_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800222 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
223 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
224 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
225 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800226 private static final int CMD_SEND_ENVELOPE = 25;
227 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000228 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
229 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700230 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
231 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
232 private static final int CMD_EXCHANGE_SIM_IO = 31;
233 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800234 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
235 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700236 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
237 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700238 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
239 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700240 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
241 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
242 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
243 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700244 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
245 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
246 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
247 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700248 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800249 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
250 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000251 private static final int CMD_SWITCH_SLOTS = 50;
252 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700253 private static final int CMD_GET_NETWORK_SELECTION_MODE = 52;
254 private static final int EVENT_GET_NETWORK_SELECTION_MODE_DONE = 53;
255 private static final int CMD_GET_CDMA_ROAMING_MODE = 54;
256 private static final int EVENT_GET_CDMA_ROAMING_MODE_DONE = 55;
257 private static final int CMD_SET_CDMA_ROAMING_MODE = 56;
258 private static final int EVENT_SET_CDMA_ROAMING_MODE_DONE = 57;
259 private static final int CMD_SET_CDMA_SUBSCRIPTION_MODE = 58;
260 private static final int EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE = 59;
Nathan Harold3ff88932018-08-14 10:19:49 -0700261 private static final int CMD_GET_ALL_CELL_INFO = 60;
262 private static final int EVENT_GET_ALL_CELL_INFO_DONE = 61;
263 private static final int CMD_GET_CELL_LOCATION = 62;
264 private static final int EVENT_GET_CELL_LOCATION_DONE = 63;
chen xu6dac5ab2018-10-26 17:39:23 -0700265 private static final int CMD_MODEM_REBOOT = 64;
266 private static final int EVENT_CMD_MODEM_REBOOT_DONE = 65;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -0700267 private static final int CMD_REQUEST_CELL_INFO_UPDATE = 66;
268 private static final int EVENT_REQUEST_CELL_INFO_UPDATE_DONE = 67;
Malcolm Chen8e4ed912019-01-15 20:22:16 -0800269 private static final int CMD_REQUEST_ENABLE_MODEM = 68;
270 private static final int EVENT_ENABLE_MODEM_DONE = 69;
Nazanin Bakhshif71371d2019-04-29 17:29:44 -0700271 private static final int CMD_GET_MODEM_STATUS = 70;
272 private static final int EVENT_GET_MODEM_STATUS_DONE = 71;
yincheng zhao2737e882019-09-06 17:06:54 -0700273 private static final int CMD_SET_FORBIDDEN_PLMNS = 72;
274 private static final int EVENT_SET_FORBIDDEN_PLMNS_DONE = 73;
Naina Nallurid63128d2019-09-17 14:10:30 -0700275 private static final int CMD_ERASE_MODEM_CONFIG = 74;
276 private static final int EVENT_ERASE_MODEM_CONFIG_DONE = 75;
zoey chene02881a2019-12-30 16:11:23 +0800277 private static final int CMD_CHANGE_ICC_LOCK_PASSWORD = 76;
278 private static final int EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE = 77;
279 private static final int CMD_SET_ICC_LOCK_ENABLED = 78;
280 private static final int EVENT_SET_ICC_LOCK_ENABLED_DONE = 79;
Hall Liu73f5d362020-01-20 13:42:00 -0800281 private static final int CMD_SET_SYSTEM_SELECTION_CHANNELS = 80;
282 private static final int EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE = 81;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800283 private static final int MSG_NOTIFY_USER_ACTIVITY = 82;
Shuo Qian4a594052020-01-23 11:59:30 -0800284 private static final int CMD_GET_CALL_FORWARDING = 83;
285 private static final int EVENT_GET_CALL_FORWARDING_DONE = 84;
286 private static final int CMD_SET_CALL_FORWARDING = 85;
287 private static final int EVENT_SET_CALL_FORWARDING_DONE = 86;
288 private static final int CMD_GET_CALL_WAITING = 87;
289 private static final int EVENT_GET_CALL_WAITING_DONE = 88;
290 private static final int CMD_SET_CALL_WAITING = 89;
291 private static final int EVENT_SET_CALL_WAITING_DONE = 90;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700292
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800293 // Parameters of select command.
294 private static final int SELECT_COMMAND = 0xA4;
295 private static final int SELECT_P1 = 0x04;
296 private static final int SELECT_P2 = 0;
297 private static final int SELECT_P3 = 0x10;
298
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 /** The singleton instance. */
300 private static PhoneInterfaceManager sInstance;
301
Wink Saville3ab207e2014-11-20 13:07:20 -0800302 private PhoneGlobals mApp;
Wink Saville3ab207e2014-11-20 13:07:20 -0800303 private CallManager mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -0800304 private ImsResolver mImsResolver;
Stuart Scott981d8582015-04-21 14:09:50 -0700305 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800306 private AppOpsManager mAppOps;
307 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800308 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800309 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700310 private PhoneConfigurationManager mPhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311
Peter Wangdafb9ac2020-01-15 14:13:38 -0800312 /** User Activity */
313 private AtomicBoolean mNotifyUserActivity;
Peter Wangdafb9ac2020-01-15 14:13:38 -0800314 private static final int USER_ACTIVITY_NOTIFICATION_DELAY = 200;
315
Jeff Davidson8ab02b22020-03-28 12:24:40 -0700316 private Set<Integer> mCarrierPrivilegeTestOverrideSubIds = new ArraySet<>();
317
Derek Tan97ebb422014-09-05 16:55:38 -0700318 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
319 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800320 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Brad Ebinger1c8542e2019-01-14 13:43:14 -0800321 private static final String PREF_PROVISION_IMS_MMTEL_PREFIX = "provision_ims_mmtel_";
Derek Tan89e89d42014-07-08 17:00:10 -0700322
Michelecea4cf22018-12-21 15:00:11 -0800323 // String to store multi SIM allowed
324 private static final String PREF_MULTI_SIM_RESTRICTED = "multisim_restricted";
325
Derek Tan740e1672017-06-27 14:56:27 -0700326 // The AID of ISD-R.
327 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
328
yinxub1bed742017-04-17 11:45:04 -0700329 private NetworkScanRequestTracker mNetworkScanRequestTracker;
330
David Kelly5e06a7f2018-03-12 14:10:59 +0000331 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
332 private static final int MANUFACTURER_CODE_LENGTH = 8;
333
Derek Tan89e89d42014-07-08 17:00:10 -0700334 /**
Naina Nallurid63128d2019-09-17 14:10:30 -0700335 * Experiment flag to enable erase modem config on reset network, default value is false
336 */
337 public static final String RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED =
338 "reset_network_erase_modem_config_enabled";
339
340 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700341 * A request object to use for transmitting data to an ICC.
342 */
343 private static final class IccAPDUArgument {
344 public int channel, cla, command, p1, p2, p3;
345 public String data;
346
347 public IccAPDUArgument(int channel, int cla, int command,
348 int p1, int p2, int p3, String data) {
349 this.channel = channel;
350 this.cla = cla;
351 this.command = command;
352 this.p1 = p1;
353 this.p2 = p2;
354 this.p3 = p3;
355 this.data = data;
356 }
357 }
358
359 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700360 * A request object to use for transmitting data to an ICC.
361 */
362 private static final class ManualNetworkSelectionArgument {
363 public OperatorInfo operatorInfo;
364 public boolean persistSelection;
365
366 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
367 this.operatorInfo = operatorInfo;
368 this.persistSelection = persistSelection;
369 }
370 }
371
372 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700373 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
374 * request after sending. The main thread will notify the request when it is complete.
375 */
376 private static final class MainThreadRequest {
377 /** The argument to use for the request */
378 public Object argument;
379 /** The result of the request that is run on the main thread */
380 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800381 // The subscriber id that this request applies to. Defaults to
382 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
383 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700384
Nathan Harold92bed182018-10-12 18:16:49 -0700385 // In cases where subId is unavailable, the caller needs to specify the phone.
386 public Phone phone;
387
vagdeviaf9a5b92018-08-15 16:01:53 -0700388 public WorkSource workSource;
389
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700390 public MainThreadRequest(Object argument) {
391 this.argument = argument;
392 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800393
Nathan Harold92bed182018-10-12 18:16:49 -0700394 MainThreadRequest(Object argument, Phone phone, WorkSource workSource) {
395 this.argument = argument;
396 if (phone != null) {
397 this.phone = phone;
398 }
399 this.workSource = workSource;
400 }
401
vagdeviaf9a5b92018-08-15 16:01:53 -0700402 MainThreadRequest(Object argument, Integer subId, WorkSource workSource) {
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800403 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800404 if (subId != null) {
405 this.subId = subId;
406 }
vagdeviaf9a5b92018-08-15 16:01:53 -0700407 this.workSource = workSource;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800408 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700409 }
410
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800411 private static final class IncomingThirdPartyCallArgs {
412 public final ComponentName component;
413 public final String callId;
414 public final String callerDisplayName;
415
416 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
417 String callerDisplayName) {
418 this.component = component;
419 this.callId = callId;
420 this.callerDisplayName = callerDisplayName;
421 }
422 }
423
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700424 /**
425 * A handler that processes messages on the main thread in the phone process. Since many
426 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
427 * inbound binder threads to the main thread in the phone process. The Binder thread
428 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
429 * on, which will be notified when the operation completes and will contain the result of the
430 * request.
431 *
432 * <p>If a MainThreadRequest object is provided in the msg.obj field,
433 * note that request.result must be set to something non-null for the calling thread to
434 * unblock.
435 */
436 private final class MainThreadHandler extends Handler {
437 @Override
438 public void handleMessage(Message msg) {
439 MainThreadRequest request;
440 Message onCompleted;
441 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800442 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700443 IccAPDUArgument iccArgument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800444 final Phone defaultPhone = getDefaultPhone();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700445
446 switch (msg.what) {
Pengquan Menga1bb6272018-09-06 09:59:22 -0700447 case CMD_HANDLE_USSD_REQUEST: {
448 request = (MainThreadRequest) msg.obj;
449 final Phone phone = getPhoneFromRequest(request);
450 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
451 String ussdRequest = ussdObject.first;
452 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700453
Pengquan Menga1bb6272018-09-06 09:59:22 -0700454 if (!isUssdApiAllowed(request.subId)) {
455 // Carrier does not support use of this API, return failure.
456 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
457 UssdResponse response = new UssdResponse(ussdRequest, null);
458 Bundle returnData = new Bundle();
459 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
460 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
Tyler Gunn65d45c22017-06-05 11:22:26 -0700461
Pengquan Menga1bb6272018-09-06 09:59:22 -0700462 request.result = true;
463 notifyRequester(request);
464 return;
465 }
Tyler Gunn65d45c22017-06-05 11:22:26 -0700466
Pengquan Menga1bb6272018-09-06 09:59:22 -0700467 try {
468 request.result = phone != null
469 ? phone.handleUssdRequest(ussdRequest, wrappedCallback) : false;
470 } catch (CallStateException cse) {
471 request.result = false;
472 }
473 // Wake up the requesting thread
474 notifyRequester(request);
475 break;
pkanwar32d516d2016-10-14 19:37:38 -0700476 }
477
Yorke Lee716f67e2015-06-17 15:39:16 -0700478 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700479 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700480 final Phone phone = getPhoneFromRequest(request);
481 request.result = phone != null ?
482 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
483 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700484 // Wake up the requesting thread
Pengquan Menga1bb6272018-09-06 09:59:22 -0700485 notifyRequester(request);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700486 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700487 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700488
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700489 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700490 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700491 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800492 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700493 if (uiccCard == null) {
494 loge("iccTransmitApduLogicalChannel: No UICC");
495 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700496 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700497 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700498 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
499 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700500 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700501 iccArgument.channel, iccArgument.cla, iccArgument.command,
502 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700503 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700504 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700505 break;
506
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700507 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700508 ar = (AsyncResult) msg.obj;
509 request = (MainThreadRequest) ar.userObj;
510 if (ar.exception == null && ar.result != null) {
511 request.result = ar.result;
512 } else {
513 request.result = new IccIoResult(0x6F, 0, (byte[])null);
514 if (ar.result == null) {
515 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800516 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700517 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800518 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700519 } else {
520 loge("iccTransmitApduLogicalChannel: Unknown exception");
521 }
522 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700523 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700524 break;
525
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700526 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
527 request = (MainThreadRequest) msg.obj;
528 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800529 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700530 if (uiccCard == null) {
531 loge("iccTransmitApduBasicChannel: No UICC");
532 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700533 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700534 } else {
535 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
536 request);
537 uiccCard.iccTransmitApduBasicChannel(
538 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
539 iccArgument.p3, iccArgument.data, onCompleted);
540 }
541 break;
542
543 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
544 ar = (AsyncResult) msg.obj;
545 request = (MainThreadRequest) ar.userObj;
546 if (ar.exception == null && ar.result != null) {
547 request.result = ar.result;
548 } else {
549 request.result = new IccIoResult(0x6F, 0, (byte[])null);
550 if (ar.result == null) {
551 loge("iccTransmitApduBasicChannel: Empty response");
552 } else if (ar.exception instanceof CommandException) {
553 loge("iccTransmitApduBasicChannel: CommandException: " +
554 ar.exception);
555 } else {
556 loge("iccTransmitApduBasicChannel: Unknown exception");
557 }
558 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700559 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700560 break;
561
562 case CMD_EXCHANGE_SIM_IO:
563 request = (MainThreadRequest) msg.obj;
564 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800565 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700566 if (uiccCard == null) {
567 loge("iccExchangeSimIO: No UICC");
568 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700569 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700570 } else {
571 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
572 request);
573 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
574 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
575 iccArgument.data, onCompleted);
576 }
577 break;
578
579 case EVENT_EXCHANGE_SIM_IO_DONE:
580 ar = (AsyncResult) msg.obj;
581 request = (MainThreadRequest) ar.userObj;
582 if (ar.exception == null && ar.result != null) {
583 request.result = ar.result;
584 } else {
585 request.result = new IccIoResult(0x6f, 0, (byte[])null);
586 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700587 notifyRequester(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700588 break;
589
Derek Tan4d5e5c12014-02-04 11:54:58 -0800590 case CMD_SEND_ENVELOPE:
591 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800592 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700593 if (uiccCard == null) {
594 loge("sendEnvelopeWithStatus: No UICC");
595 request.result = new IccIoResult(0x6F, 0, (byte[])null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700596 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700597 } else {
598 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
599 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
600 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800601 break;
602
603 case EVENT_SEND_ENVELOPE_DONE:
604 ar = (AsyncResult) msg.obj;
605 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700606 if (ar.exception == null && ar.result != null) {
607 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800608 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700609 request.result = new IccIoResult(0x6F, 0, (byte[])null);
610 if (ar.result == null) {
611 loge("sendEnvelopeWithStatus: Empty response");
612 } else if (ar.exception instanceof CommandException) {
613 loge("sendEnvelopeWithStatus: CommandException: " +
614 ar.exception);
615 } else {
616 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
617 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800618 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700619 notifyRequester(request);
Derek Tan4d5e5c12014-02-04 11:54:58 -0800620 break;
621
Shishir Agrawal566b7612013-10-28 14:41:00 -0700622 case CMD_OPEN_CHANNEL:
623 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800624 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800625 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700626 if (uiccCard == null) {
627 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800628 request.result = new IccOpenLogicalChannelResponse(-1,
629 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Pengquan Menga1bb6272018-09-06 09:59:22 -0700630 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700631 } else {
632 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800633 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
634 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700635 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700636 break;
637
638 case EVENT_OPEN_CHANNEL_DONE:
639 ar = (AsyncResult) msg.obj;
640 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700641 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700642 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700643 int[] result = (int[]) ar.result;
644 int channelId = result[0];
645 byte[] selectResponse = null;
646 if (result.length > 1) {
647 selectResponse = new byte[result.length - 1];
648 for (int i = 1; i < result.length; ++i) {
649 selectResponse[i - 1] = (byte) result[i];
650 }
651 }
652 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700653 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700654 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700655 if (ar.result == null) {
656 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700657 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700658 if (ar.exception != null) {
659 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
660 }
661
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700662 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700663 if (ar.exception instanceof CommandException) {
664 CommandException.Error error =
665 ((CommandException) (ar.exception)).getCommandError();
666 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700667 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700668 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700669 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700670 }
671 }
672 openChannelResp = new IccOpenLogicalChannelResponse(
673 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700674 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700675 request.result = openChannelResp;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700676 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700677 break;
678
679 case CMD_CLOSE_CHANNEL:
680 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800681 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700682 if (uiccCard == null) {
683 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900684 request.result = false;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700685 notifyRequester(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700686 } else {
687 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
688 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
689 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700690 break;
691
692 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800693 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
694 break;
695
696 case CMD_NV_READ_ITEM:
697 request = (MainThreadRequest) msg.obj;
698 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800699 defaultPhone.nvReadItem((Integer) request.argument, onCompleted,
700 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800701 break;
702
703 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700704 ar = (AsyncResult) msg.obj;
705 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800706 if (ar.exception == null && ar.result != null) {
707 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700708 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800709 request.result = "";
710 if (ar.result == null) {
711 loge("nvReadItem: Empty response");
712 } else if (ar.exception instanceof CommandException) {
713 loge("nvReadItem: CommandException: " +
714 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700715 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800716 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700717 }
718 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700719 notifyRequester(request);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700720 break;
721
Jake Hambye994d462014-02-03 13:10:13 -0800722 case CMD_NV_WRITE_ITEM:
723 request = (MainThreadRequest) msg.obj;
724 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
725 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800726 defaultPhone.nvWriteItem(idValue.first, idValue.second, onCompleted,
vagdeviaf9a5b92018-08-15 16:01:53 -0700727 request.workSource);
Jake Hambye994d462014-02-03 13:10:13 -0800728 break;
729
730 case EVENT_NV_WRITE_ITEM_DONE:
731 handleNullReturnEvent(msg, "nvWriteItem");
732 break;
733
734 case CMD_NV_WRITE_CDMA_PRL:
735 request = (MainThreadRequest) msg.obj;
736 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800737 defaultPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800738 break;
739
740 case EVENT_NV_WRITE_CDMA_PRL_DONE:
741 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
742 break;
743
chen xu6dac5ab2018-10-26 17:39:23 -0700744 case CMD_RESET_MODEM_CONFIG:
Jake Hambye994d462014-02-03 13:10:13 -0800745 request = (MainThreadRequest) msg.obj;
chen xu6dac5ab2018-10-26 17:39:23 -0700746 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800747 defaultPhone.resetModemConfig(onCompleted);
Jake Hambye994d462014-02-03 13:10:13 -0800748 break;
749
chen xu6dac5ab2018-10-26 17:39:23 -0700750 case EVENT_RESET_MODEM_CONFIG_DONE:
751 handleNullReturnEvent(msg, "resetModemConfig");
Jake Hambye994d462014-02-03 13:10:13 -0800752 break;
753
Jake Hamby7c27be32014-03-03 13:25:59 -0800754 case CMD_GET_PREFERRED_NETWORK_TYPE:
755 request = (MainThreadRequest) msg.obj;
756 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700757 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800758 break;
759
760 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
761 ar = (AsyncResult) msg.obj;
762 request = (MainThreadRequest) ar.userObj;
763 if (ar.exception == null && ar.result != null) {
764 request.result = ar.result; // Integer
765 } else {
Nazish Tabassume8ba43a2020-07-28 14:49:25 +0530766 // request.result must be set to something non-null
767 // for the calling thread to unblock
768 request.result = new int[]{-1};
Jake Hamby7c27be32014-03-03 13:25:59 -0800769 if (ar.result == null) {
770 loge("getPreferredNetworkType: Empty response");
771 } else if (ar.exception instanceof CommandException) {
772 loge("getPreferredNetworkType: CommandException: " +
773 ar.exception);
774 } else {
775 loge("getPreferredNetworkType: Unknown exception");
776 }
777 }
Pengquan Menga1bb6272018-09-06 09:59:22 -0700778 notifyRequester(request);
Jake Hamby7c27be32014-03-03 13:25:59 -0800779 break;
780
781 case CMD_SET_PREFERRED_NETWORK_TYPE:
782 request = (MainThreadRequest) msg.obj;
783 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
784 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700785 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800786 break;
787
788 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
789 handleNullReturnEvent(msg, "setPreferredNetworkType");
790 break;
791
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000792 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
793 request = (MainThreadRequest)msg.obj;
794 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -0800795 defaultPhone.invokeOemRilRequestRaw((byte[]) request.argument, onCompleted);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000796 break;
797
798 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
799 ar = (AsyncResult)msg.obj;
800 request = (MainThreadRequest)ar.userObj;
801 request.result = ar;
Pengquan Menga1bb6272018-09-06 09:59:22 -0700802 notifyRequester(request);
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000803 break;
804
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800805 case CMD_SET_VOICEMAIL_NUMBER:
806 request = (MainThreadRequest) msg.obj;
807 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
808 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800809 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
810 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800811 break;
812
813 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
814 handleNullReturnEvent(msg, "setVoicemailNumber");
815 break;
816
Stuart Scott54788802015-03-30 13:18:01 -0700817 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
818 request = (MainThreadRequest) msg.obj;
819 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
820 request);
821 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
822 break;
823
824 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
825 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
826 break;
827
Shishir Agrawal302c8692015-06-19 13:49:39 -0700828 case CMD_PERFORM_NETWORK_SCAN:
829 request = (MainThreadRequest) msg.obj;
830 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
831 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
832 break;
833
Hall Liu27d24262020-09-18 19:04:59 -0700834 case CMD_GET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -0800835 request = (MainThreadRequest) msg.obj;
836 onCompleted = obtainMessage(EVENT_GET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -0700837 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> args =
838 (Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
839 request.argument;
840 int callForwardingReason = args.first;
841 request.phone.getCallForwardingOption(callForwardingReason, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -0800842 break;
Hall Liu27d24262020-09-18 19:04:59 -0700843 }
844 case EVENT_GET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -0800845 ar = (AsyncResult) msg.obj;
846 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -0700847 TelephonyManager.CallForwardingInfoCallback callback =
848 ((Pair<Integer, TelephonyManager.CallForwardingInfoCallback>)
849 request.argument).second;
Shuo Qian4a594052020-01-23 11:59:30 -0800850 if (ar.exception == null && ar.result != null) {
Hall Liu27d24262020-09-18 19:04:59 -0700851 CallForwardingInfo callForwardingInfo = null;
Shuo Qian4a594052020-01-23 11:59:30 -0800852 CallForwardInfo[] callForwardInfos = (CallForwardInfo[]) ar.result;
853 for (CallForwardInfo callForwardInfo : callForwardInfos) {
854 // Service Class is a bit mask per 3gpp 27.007. Search for
855 // any service for voice call.
856 if ((callForwardInfo.serviceClass
857 & CommandsInterface.SERVICE_CLASS_VOICE) > 0) {
Hall Liu27d24262020-09-18 19:04:59 -0700858 callForwardingInfo = new CallForwardingInfo(true,
859 callForwardInfo.reason,
860 callForwardInfo.number,
861 callForwardInfo.timeSeconds);
Shuo Qian4a594052020-01-23 11:59:30 -0800862 break;
863 }
864 }
865 // Didn't find a call forward info for voice call.
866 if (callForwardingInfo == null) {
Hall Liu27d24262020-09-18 19:04:59 -0700867 callForwardingInfo = new CallForwardingInfo(false /* enabled */,
868 0 /* reason */, null /* number */, 0 /* timeout */);
Shuo Qian4a594052020-01-23 11:59:30 -0800869 }
Hall Liu27d24262020-09-18 19:04:59 -0700870 callback.onCallForwardingInfoAvailable(callForwardingInfo);
Shuo Qian4a594052020-01-23 11:59:30 -0800871 } else {
872 if (ar.result == null) {
873 loge("EVENT_GET_CALL_FORWARDING_DONE: Empty response");
874 }
875 if (ar.exception != null) {
876 loge("EVENT_GET_CALL_FORWARDING_DONE: Exception: " + ar.exception);
877 }
Hall Liu27d24262020-09-18 19:04:59 -0700878 int errorCode = CallForwardingInfo.ERROR_UNKNOWN;
Shuo Qian4a594052020-01-23 11:59:30 -0800879 if (ar.exception instanceof CommandException) {
880 CommandException.Error error =
881 ((CommandException) (ar.exception)).getCommandError();
882 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
Hall Liu27d24262020-09-18 19:04:59 -0700883 errorCode = CallForwardingInfo.ERROR_FDN_CHECK_FAILURE;
Shuo Qian4a594052020-01-23 11:59:30 -0800884 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
Hall Liu27d24262020-09-18 19:04:59 -0700885 errorCode = CallForwardingInfo.ERROR_NOT_SUPPORTED;
Shuo Qian4a594052020-01-23 11:59:30 -0800886 }
887 }
Hall Liu27d24262020-09-18 19:04:59 -0700888 callback.onError(errorCode);
Shuo Qian4a594052020-01-23 11:59:30 -0800889 }
Shuo Qian4a594052020-01-23 11:59:30 -0800890 break;
Hall Liu27d24262020-09-18 19:04:59 -0700891 }
Shuo Qian4a594052020-01-23 11:59:30 -0800892
Hall Liu27d24262020-09-18 19:04:59 -0700893 case CMD_SET_CALL_FORWARDING: {
Shuo Qian4a594052020-01-23 11:59:30 -0800894 request = (MainThreadRequest) msg.obj;
895 onCompleted = obtainMessage(EVENT_SET_CALL_FORWARDING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -0700896 request = (MainThreadRequest) msg.obj;
Shuo Qian4a594052020-01-23 11:59:30 -0800897 CallForwardingInfo callForwardingInfoToSet =
Hall Liu27d24262020-09-18 19:04:59 -0700898 ((Pair<CallForwardingInfo, Consumer<Integer>>)
899 request.argument).first;
900 request.phone.setCallForwardingOption(
901 callForwardingInfoToSet.isEnabled()
902 ? CommandsInterface.CF_ACTION_ENABLE
903 : CommandsInterface.CF_ACTION_DISABLE,
Shuo Qian4a594052020-01-23 11:59:30 -0800904 callForwardingInfoToSet.getReason(),
905 callForwardingInfoToSet.getNumber(),
906 callForwardingInfoToSet.getTimeoutSeconds(), onCompleted);
907 break;
Hall Liu27d24262020-09-18 19:04:59 -0700908 }
Shuo Qian4a594052020-01-23 11:59:30 -0800909
Hall Liu27d24262020-09-18 19:04:59 -0700910 case EVENT_SET_CALL_FORWARDING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -0800911 ar = (AsyncResult) msg.obj;
912 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -0700913 Consumer<Integer> callback =
914 ((Pair<CallForwardingInfo, Consumer<Integer>>)
915 request.argument).second;
916 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -0800917 loge("setCallForwarding exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -0700918 int errorCode = CallForwardingInfo.ERROR_UNKNOWN;
919 if (ar.exception instanceof CommandException) {
920 CommandException.Error error =
921 ((CommandException) (ar.exception)).getCommandError();
922 if (error == CommandException.Error.FDN_CHECK_FAILURE) {
923 errorCode = CallForwardingInfo.ERROR_FDN_CHECK_FAILURE;
924 } else if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
925 errorCode = CallForwardingInfo.ERROR_NOT_SUPPORTED;
926 }
927 }
928 callback.accept(errorCode);
929 } else {
930 callback.accept(CallForwardingInfo.SUCCESS);
Shuo Qian4a594052020-01-23 11:59:30 -0800931 }
Shuo Qian4a594052020-01-23 11:59:30 -0800932 break;
Hall Liu27d24262020-09-18 19:04:59 -0700933 }
Shuo Qian4a594052020-01-23 11:59:30 -0800934
Hall Liu27d24262020-09-18 19:04:59 -0700935 case CMD_GET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -0800936 request = (MainThreadRequest) msg.obj;
937 onCompleted = obtainMessage(EVENT_GET_CALL_WAITING_DONE, request);
938 getPhoneFromRequest(request).getCallWaiting(onCompleted);
939 break;
Hall Liu27d24262020-09-18 19:04:59 -0700940 }
Shuo Qian4a594052020-01-23 11:59:30 -0800941
Hall Liu27d24262020-09-18 19:04:59 -0700942 case EVENT_GET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -0800943 ar = (AsyncResult) msg.obj;
944 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -0700945 Consumer<Integer> callback = (Consumer<Integer>) request.argument;
Shuo Qian4a594052020-01-23 11:59:30 -0800946 int callForwardingStatus = TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR;
947 if (ar.exception == null && ar.result != null) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -0800948 int[] callForwardResults = (int[]) ar.result;
Shuo Qian4a594052020-01-23 11:59:30 -0800949 // Service Class is a bit mask per 3gpp 27.007.
950 // Search for any service for voice call.
Shuo Qiand6a0dba2020-02-18 18:13:49 -0800951 if (callForwardResults.length > 1
952 && ((callForwardResults[1]
Hall Liu27d24262020-09-18 19:04:59 -0700953 & CommandsInterface.SERVICE_CLASS_VOICE) > 0)) {
Shuo Qiand6a0dba2020-02-18 18:13:49 -0800954 callForwardingStatus = callForwardResults[0] == 0
Hall Liu27d24262020-09-18 19:04:59 -0700955 ? TelephonyManager.CALL_WAITING_STATUS_DISABLED
956 : TelephonyManager.CALL_WAITING_STATUS_ENABLED;
Shuo Qian4a594052020-01-23 11:59:30 -0800957 } else {
Hall Liu27d24262020-09-18 19:04:59 -0700958 callForwardingStatus = TelephonyManager.CALL_WAITING_STATUS_DISABLED;
Shuo Qian4a594052020-01-23 11:59:30 -0800959 }
960 } else {
961 if (ar.result == null) {
962 loge("EVENT_GET_CALL_WAITING_DONE: Empty response");
963 }
964 if (ar.exception != null) {
965 loge("EVENT_GET_CALL_WAITING_DONE: Exception: " + ar.exception);
966 }
967 if (ar.exception instanceof CommandException) {
968 CommandException.Error error =
969 ((CommandException) (ar.exception)).getCommandError();
970 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
971 callForwardingStatus =
972 TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED;
973 }
974 }
975 }
Hall Liu27d24262020-09-18 19:04:59 -0700976 callback.accept(callForwardingStatus);
Shuo Qian4a594052020-01-23 11:59:30 -0800977 break;
Hall Liu27d24262020-09-18 19:04:59 -0700978 }
Shuo Qian4a594052020-01-23 11:59:30 -0800979
Hall Liu27d24262020-09-18 19:04:59 -0700980 case CMD_SET_CALL_WAITING: {
Shuo Qian4a594052020-01-23 11:59:30 -0800981 request = (MainThreadRequest) msg.obj;
982 onCompleted = obtainMessage(EVENT_SET_CALL_WAITING_DONE, request);
Hall Liu27d24262020-09-18 19:04:59 -0700983 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
984 getPhoneFromRequest(request).setCallWaiting(enable, onCompleted);
Shuo Qian4a594052020-01-23 11:59:30 -0800985 break;
Hall Liu27d24262020-09-18 19:04:59 -0700986 }
Shuo Qian4a594052020-01-23 11:59:30 -0800987
Hall Liu27d24262020-09-18 19:04:59 -0700988 case EVENT_SET_CALL_WAITING_DONE: {
Shuo Qian4a594052020-01-23 11:59:30 -0800989 ar = (AsyncResult) msg.obj;
990 request = (MainThreadRequest) ar.userObj;
Hall Liu27d24262020-09-18 19:04:59 -0700991 boolean enable = ((Pair<Boolean, Consumer<Integer>>) request.argument).first;
992 Consumer<Integer> callback =
993 ((Pair<Boolean, Consumer<Integer>>) request.argument).second;
994 if (ar.exception != null) {
Shuo Qian4a594052020-01-23 11:59:30 -0800995 loge("setCallWaiting exception: " + ar.exception);
Hall Liu27d24262020-09-18 19:04:59 -0700996 if (ar.exception instanceof CommandException) {
997 CommandException.Error error =
998 ((CommandException) (ar.exception)).getCommandError();
999 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1000 callback.accept(TelephonyManager.CALL_WAITING_STATUS_NOT_SUPPORTED);
1001 } else {
1002 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1003 }
1004 } else {
1005 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
1006 }
1007 } else {
1008 callback.accept(enable ? TelephonyManager.CALL_WAITING_STATUS_ENABLED
1009 : TelephonyManager.CALL_WAITING_STATUS_DISABLED);
Shuo Qian4a594052020-01-23 11:59:30 -08001010 }
Shuo Qian4a594052020-01-23 11:59:30 -08001011 break;
Hall Liu27d24262020-09-18 19:04:59 -07001012 }
Shuo Qian4a594052020-01-23 11:59:30 -08001013
Shishir Agrawal302c8692015-06-19 13:49:39 -07001014 case EVENT_PERFORM_NETWORK_SCAN_DONE:
1015 ar = (AsyncResult) msg.obj;
1016 request = (MainThreadRequest) ar.userObj;
1017 CellNetworkScanResult cellScanResult;
1018 if (ar.exception == null && ar.result != null) {
1019 cellScanResult = new CellNetworkScanResult(
1020 CellNetworkScanResult.STATUS_SUCCESS,
1021 (List<OperatorInfo>) ar.result);
1022 } else {
1023 if (ar.result == null) {
1024 loge("getCellNetworkScanResults: Empty response");
1025 }
1026 if (ar.exception != null) {
1027 loge("getCellNetworkScanResults: Exception: " + ar.exception);
1028 }
1029 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
1030 if (ar.exception instanceof CommandException) {
1031 CommandException.Error error =
1032 ((CommandException) (ar.exception)).getCommandError();
1033 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
1034 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
1035 } else if (error == CommandException.Error.GENERIC_FAILURE) {
1036 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
1037 }
1038 }
1039 cellScanResult = new CellNetworkScanResult(errorCode, null);
1040 }
1041 request.result = cellScanResult;
Pengquan Menga1bb6272018-09-06 09:59:22 -07001042 notifyRequester(request);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001043 break;
1044
1045 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
1046 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001047 ManualNetworkSelectionArgument selArg =
1048 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -07001049 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
1050 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07001051 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
1052 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001053 break;
1054
1055 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
Pengquan Menge3d01e22018-09-20 15:25:35 -07001056 ar = (AsyncResult) msg.obj;
1057 request = (MainThreadRequest) ar.userObj;
1058 if (ar.exception == null) {
1059 request.result = true;
1060 } else {
1061 request.result = false;
1062 loge("setNetworkSelectionModeManual " + ar.exception);
1063 }
1064 notifyRequester(request);
1065 mApp.onNetworkSelectionChanged(request.subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07001066 break;
1067
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001068 case CMD_GET_MODEM_ACTIVITY_INFO:
1069 request = (MainThreadRequest) msg.obj;
1070 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
James Mattisab947702019-04-03 14:18:34 -07001071 if (defaultPhone != null) {
1072 defaultPhone.getModemActivityInfo(onCompleted, request.workSource);
Shuo Qian8f4750a2020-02-20 17:12:10 -08001073 } else {
1074 ResultReceiver result = (ResultReceiver) request.argument;
1075 Bundle bundle = new Bundle();
1076 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY,
1077 new ModemActivityInfo(0, 0, 0, new int[0], 0));
1078 result.send(0, bundle);
James Mattisab947702019-04-03 14:18:34 -07001079 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001080 break;
1081
1082 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
1083 ar = (AsyncResult) msg.obj;
1084 request = (MainThreadRequest) ar.userObj;
Shuo Qian8f4750a2020-02-20 17:12:10 -08001085 ResultReceiver result = (ResultReceiver) request.argument;
1086
1087 ModemActivityInfo ret = new ModemActivityInfo(0, 0, 0, new int[0], 0);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001088 if (ar.exception == null && ar.result != null) {
Shuo Qian8f4750a2020-02-20 17:12:10 -08001089 // Update the last modem activity info and the result of the request.
1090 ModemActivityInfo info = (ModemActivityInfo) ar.result;
1091 if (isModemActivityInfoValid(info)) {
1092 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
1093 int[] txTimeMs = info.getTransmitTimeMillis();
1094 int[] lastModemTxTimeMs = mLastModemActivityInfo
1095 .getTransmitTimeMillis();
1096 for (int i = 0; i < mergedTxTimeMs.length; i++) {
1097 mergedTxTimeMs[i] = txTimeMs[i] + lastModemTxTimeMs[i];
1098 }
1099 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
1100 mLastModemActivityInfo.setSleepTimeMillis(info.getSleepTimeMillis()
1101 + mLastModemActivityInfo.getSleepTimeMillis());
1102 mLastModemActivityInfo.setIdleTimeMillis(info.getIdleTimeMillis()
1103 + mLastModemActivityInfo.getIdleTimeMillis());
1104 mLastModemActivityInfo.setTransmitTimeMillis(mergedTxTimeMs);
1105 mLastModemActivityInfo.setReceiveTimeMillis(
1106 info.getReceiveTimeMillis()
1107 + mLastModemActivityInfo.getReceiveTimeMillis());
1108 }
1109 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
1110 mLastModemActivityInfo.getSleepTimeMillis(),
1111 mLastModemActivityInfo.getIdleTimeMillis(),
1112 mLastModemActivityInfo.getTransmitTimeMillis(),
1113 mLastModemActivityInfo.getReceiveTimeMillis());
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001114 } else {
1115 if (ar.result == null) {
1116 loge("queryModemActivityInfo: Empty response");
1117 } else if (ar.exception instanceof CommandException) {
1118 loge("queryModemActivityInfo: CommandException: " +
1119 ar.exception);
1120 } else {
1121 loge("queryModemActivityInfo: Unknown exception");
1122 }
1123 }
Shuo Qian8f4750a2020-02-20 17:12:10 -08001124 Bundle bundle = new Bundle();
1125 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
1126 result.send(0, bundle);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001127 notifyRequester(request);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07001128 break;
1129
Meng Wang1a7c35a2016-05-05 20:56:15 -07001130 case CMD_SET_ALLOWED_CARRIERS:
1131 request = (MainThreadRequest) msg.obj;
Michele Berionne482f8202018-11-27 18:57:59 -08001132 CarrierRestrictionRules argument =
1133 (CarrierRestrictionRules) request.argument;
Meng Wang1a7c35a2016-05-05 20:56:15 -07001134 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
Michele Berionne482f8202018-11-27 18:57:59 -08001135 defaultPhone.setAllowedCarriers(argument, onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001136 break;
1137
1138 case EVENT_SET_ALLOWED_CARRIERS_DONE:
1139 ar = (AsyncResult) msg.obj;
1140 request = (MainThreadRequest) ar.userObj;
1141 if (ar.exception == null && ar.result != null) {
1142 request.result = ar.result;
1143 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001144 request.result = TelephonyManager.SET_CARRIER_RESTRICTION_ERROR;
1145 if (ar.exception instanceof CommandException) {
1146 loge("setAllowedCarriers: CommandException: " + ar.exception);
1147 CommandException.Error error =
1148 ((CommandException) (ar.exception)).getCommandError();
1149 if (error == CommandException.Error.REQUEST_NOT_SUPPORTED) {
1150 request.result =
1151 TelephonyManager.SET_CARRIER_RESTRICTION_NOT_SUPPORTED;
1152 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07001153 } else {
1154 loge("setAllowedCarriers: Unknown exception");
1155 }
1156 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001157 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001158 break;
1159
1160 case CMD_GET_ALLOWED_CARRIERS:
1161 request = (MainThreadRequest) msg.obj;
1162 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001163 defaultPhone.getAllowedCarriers(onCompleted, request.workSource);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001164 break;
1165
1166 case EVENT_GET_ALLOWED_CARRIERS_DONE:
1167 ar = (AsyncResult) msg.obj;
1168 request = (MainThreadRequest) ar.userObj;
1169 if (ar.exception == null && ar.result != null) {
1170 request.result = ar.result;
1171 } else {
Michele Berionne482f8202018-11-27 18:57:59 -08001172 request.result = new IllegalStateException(
1173 "Failed to get carrier restrictions");
Meng Wang1a7c35a2016-05-05 20:56:15 -07001174 if (ar.result == null) {
1175 loge("getAllowedCarriers: Empty response");
1176 } else if (ar.exception instanceof CommandException) {
1177 loge("getAllowedCarriers: CommandException: " +
1178 ar.exception);
1179 } else {
1180 loge("getAllowedCarriers: Unknown exception");
1181 }
1182 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001183 notifyRequester(request);
Meng Wang1a7c35a2016-05-05 20:56:15 -07001184 break;
1185
Nathan Haroldb3014052017-01-25 15:57:32 -08001186 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
1187 ar = (AsyncResult) msg.obj;
1188 request = (MainThreadRequest) ar.userObj;
1189 if (ar.exception == null && ar.result != null) {
1190 request.result = ar.result;
1191 } else {
1192 request.result = new IllegalArgumentException(
1193 "Failed to retrieve Forbidden Plmns");
1194 if (ar.result == null) {
1195 loge("getForbiddenPlmns: Empty response");
1196 } else {
1197 loge("getForbiddenPlmns: Unknown exception");
1198 }
1199 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001200 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001201 break;
1202
1203 case CMD_GET_FORBIDDEN_PLMNS:
1204 request = (MainThreadRequest) msg.obj;
1205 uiccCard = getUiccCardFromRequest(request);
1206 if (uiccCard == null) {
1207 loge("getForbiddenPlmns() UiccCard is null");
1208 request.result = new IllegalArgumentException(
1209 "getForbiddenPlmns() UiccCard is null");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001210 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001211 break;
1212 }
1213 Integer appType = (Integer) request.argument;
1214 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
1215 if (uiccApp == null) {
1216 loge("getForbiddenPlmns() no app with specified type -- "
1217 + appType);
1218 request.result = new IllegalArgumentException("Failed to get UICC App");
Pengquan Menga1bb6272018-09-06 09:59:22 -07001219 notifyRequester(request);
Nathan Haroldb3014052017-01-25 15:57:32 -08001220 break;
1221 } else {
1222 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
1223 + " specified type -- " + appType);
1224 }
1225 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
1226 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
1227 onCompleted);
1228 break;
1229
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001230 case CMD_SWITCH_SLOTS:
1231 request = (MainThreadRequest) msg.obj;
1232 int[] physicalSlots = (int[]) request.argument;
1233 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
1234 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
1235 break;
1236
1237 case EVENT_SWITCH_SLOTS_DONE:
1238 ar = (AsyncResult) msg.obj;
1239 request = (MainThreadRequest) ar.userObj;
1240 request.result = (ar.exception == null);
Pengquan Menga1bb6272018-09-06 09:59:22 -07001241 notifyRequester(request);
1242 break;
1243 case CMD_GET_NETWORK_SELECTION_MODE:
1244 request = (MainThreadRequest) msg.obj;
1245 onCompleted = obtainMessage(EVENT_GET_NETWORK_SELECTION_MODE_DONE, request);
1246 getPhoneFromRequest(request).getNetworkSelectionMode(onCompleted);
1247 break;
1248
1249 case EVENT_GET_NETWORK_SELECTION_MODE_DONE:
1250 ar = (AsyncResult) msg.obj;
1251 request = (MainThreadRequest) ar.userObj;
1252 if (ar.exception != null) {
1253 request.result = TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
1254 } else {
1255 int mode = ((int[]) ar.result)[0];
1256 if (mode == 0) {
1257 request.result = TelephonyManager.NETWORK_SELECTION_MODE_AUTO;
1258 } else {
1259 request.result = TelephonyManager.NETWORK_SELECTION_MODE_MANUAL;
1260 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001261 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001262 notifyRequester(request);
1263 break;
1264 case CMD_GET_CDMA_ROAMING_MODE:
1265 request = (MainThreadRequest) msg.obj;
1266 onCompleted = obtainMessage(EVENT_GET_CDMA_ROAMING_MODE_DONE, request);
1267 getPhoneFromRequest(request).queryCdmaRoamingPreference(onCompleted);
1268 break;
1269 case EVENT_GET_CDMA_ROAMING_MODE_DONE:
1270 ar = (AsyncResult) msg.obj;
1271 request = (MainThreadRequest) ar.userObj;
1272 if (ar.exception != null) {
1273 request.result = TelephonyManager.CDMA_ROAMING_MODE_RADIO_DEFAULT;
1274 } else {
1275 request.result = ((int[]) ar.result)[0];
1276 }
1277 notifyRequester(request);
1278 break;
1279 case CMD_SET_CDMA_ROAMING_MODE:
1280 request = (MainThreadRequest) msg.obj;
1281 onCompleted = obtainMessage(EVENT_SET_CDMA_ROAMING_MODE_DONE, request);
1282 int mode = (int) request.argument;
1283 getPhoneFromRequest(request).setCdmaRoamingPreference(mode, onCompleted);
1284 break;
1285 case EVENT_SET_CDMA_ROAMING_MODE_DONE:
1286 ar = (AsyncResult) msg.obj;
1287 request = (MainThreadRequest) ar.userObj;
1288 request.result = ar.exception == null;
1289 notifyRequester(request);
1290 break;
1291 case CMD_SET_CDMA_SUBSCRIPTION_MODE:
1292 request = (MainThreadRequest) msg.obj;
1293 onCompleted = obtainMessage(EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE, request);
1294 int subscriptionMode = (int) request.argument;
1295 getPhoneFromRequest(request).setCdmaSubscription(subscriptionMode, onCompleted);
1296 break;
1297 case EVENT_SET_CDMA_SUBSCRIPTION_MODE_DONE:
1298 ar = (AsyncResult) msg.obj;
1299 request = (MainThreadRequest) ar.userObj;
1300 request.result = ar.exception == null;
1301 notifyRequester(request);
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00001302 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001303 case CMD_GET_ALL_CELL_INFO:
1304 request = (MainThreadRequest) msg.obj;
Nathan Harold3ff88932018-08-14 10:19:49 -07001305 onCompleted = obtainMessage(EVENT_GET_ALL_CELL_INFO_DONE, request);
Nathan Harold92bed182018-10-12 18:16:49 -07001306 request.phone.requestCellInfoUpdate(request.workSource, onCompleted);
Nathan Harold3ff88932018-08-14 10:19:49 -07001307 break;
Nathan Harold3ff88932018-08-14 10:19:49 -07001308 case EVENT_GET_ALL_CELL_INFO_DONE:
1309 ar = (AsyncResult) msg.obj;
1310 request = (MainThreadRequest) ar.userObj;
Nathan Harold8d0f1742018-10-02 12:14:47 -07001311 // If a timeout occurs, the response will be null
1312 request.result = (ar.exception == null && ar.result != null)
1313 ? ar.result : new ArrayList<CellInfo>();
Nathan Harold3ff88932018-08-14 10:19:49 -07001314 synchronized (request) {
1315 request.notifyAll();
1316 }
1317 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001318 case CMD_REQUEST_CELL_INFO_UPDATE:
1319 request = (MainThreadRequest) msg.obj;
1320 request.phone.requestCellInfoUpdate(request.workSource,
1321 obtainMessage(EVENT_REQUEST_CELL_INFO_UPDATE_DONE, request));
1322 break;
1323 case EVENT_REQUEST_CELL_INFO_UPDATE_DONE:
1324 ar = (AsyncResult) msg.obj;
1325 request = (MainThreadRequest) ar.userObj;
1326 ICellInfoCallback cb = (ICellInfoCallback) request.argument;
1327 try {
1328 if (ar.exception != null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001329 Log.e(LOG_TAG, "Exception retrieving CellInfo=" + ar.exception);
Meng Wangd8921f42019-09-30 17:13:54 -07001330 cb.onError(
1331 TelephonyManager.CellInfoCallback.ERROR_MODEM_ERROR,
1332 ar.exception.getClass().getName(),
1333 ar.exception.toString());
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001334 } else if (ar.result == null) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001335 Log.w(LOG_TAG, "Timeout Waiting for CellInfo!");
Meng Wangd8921f42019-09-30 17:13:54 -07001336 cb.onError(TelephonyManager.CellInfoCallback.ERROR_TIMEOUT, null, null);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001337 } else {
1338 // use the result as returned
1339 cb.onCellInfo((List<CellInfo>) ar.result);
1340 }
1341 } catch (RemoteException re) {
1342 Log.w(LOG_TAG, "Discarded CellInfo due to Callback RemoteException");
1343 }
1344 break;
1345 case CMD_GET_CELL_LOCATION:
Nathan Harold3ff88932018-08-14 10:19:49 -07001346 request = (MainThreadRequest) msg.obj;
1347 WorkSource ws = (WorkSource) request.argument;
1348 Phone phone = getPhoneFromRequest(request);
Meng Wanga10e89e2019-12-09 13:13:01 -08001349 phone.getCellIdentity(ws, obtainMessage(EVENT_GET_CELL_LOCATION_DONE, request));
Nathan Harold3ff88932018-08-14 10:19:49 -07001350 break;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001351 case EVENT_GET_CELL_LOCATION_DONE:
Nathan Harold3ff88932018-08-14 10:19:49 -07001352 ar = (AsyncResult) msg.obj;
1353 request = (MainThreadRequest) ar.userObj;
1354 if (ar.exception == null) {
1355 request.result = ar.result;
1356 } else {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001357 phone = getPhoneFromRequest(request);
Nathan Harold3ff88932018-08-14 10:19:49 -07001358 request.result = (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
Meng Wanga10e89e2019-12-09 13:13:01 -08001359 ? new CellIdentityCdma() : new CellIdentityGsm();
Nathan Harold3ff88932018-08-14 10:19:49 -07001360 }
1361
1362 synchronized (request) {
1363 request.notifyAll();
1364 }
1365 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001366 case CMD_MODEM_REBOOT:
1367 request = (MainThreadRequest) msg.obj;
1368 onCompleted = obtainMessage(EVENT_RESET_MODEM_CONFIG_DONE, request);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001369 defaultPhone.rebootModem(onCompleted);
chen xu6dac5ab2018-10-26 17:39:23 -07001370 break;
chen xu6dac5ab2018-10-26 17:39:23 -07001371 case EVENT_CMD_MODEM_REBOOT_DONE:
1372 handleNullReturnEvent(msg, "rebootModem");
1373 break;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001374 case CMD_REQUEST_ENABLE_MODEM:
1375 request = (MainThreadRequest) msg.obj;
1376 boolean enable = (boolean) request.argument;
1377 onCompleted = obtainMessage(EVENT_ENABLE_MODEM_DONE, request);
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001378 onCompleted.arg1 = enable ? 1 : 0;
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001379 PhoneConfigurationManager.getInstance()
1380 .enablePhone(request.phone, enable, onCompleted);
1381 break;
1382 case EVENT_ENABLE_MODEM_DONE:
1383 ar = (AsyncResult) msg.obj;
1384 request = (MainThreadRequest) ar.userObj;
1385 request.result = (ar.exception == null);
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001386 int phoneId = request.phone.getPhoneId();
Nazanin Bakhshi33d584b2019-02-27 10:44:32 -08001387 //update the cache as modem status has changed
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07001388 if ((boolean) request.result) {
1389 mPhoneConfigurationManager.addToPhoneStatusCache(phoneId, msg.arg1 == 1);
1390 updateModemStateMetrics();
1391 } else {
1392 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1393 + ar.exception);
1394 }
1395 notifyRequester(request);
1396 break;
1397 case CMD_GET_MODEM_STATUS:
1398 request = (MainThreadRequest) msg.obj;
1399 onCompleted = obtainMessage(EVENT_GET_MODEM_STATUS_DONE, request);
1400 PhoneConfigurationManager.getInstance()
1401 .getPhoneStatusFromModem(request.phone, onCompleted);
1402 break;
1403 case EVENT_GET_MODEM_STATUS_DONE:
1404 ar = (AsyncResult) msg.obj;
1405 request = (MainThreadRequest) ar.userObj;
1406 int id = request.phone.getPhoneId();
1407 if (ar.exception == null && ar.result != null) {
1408 request.result = ar.result;
1409 //update the cache as modem status has changed
1410 mPhoneConfigurationManager.addToPhoneStatusCache(id,
1411 (boolean) request.result);
1412 } else {
1413 // Return true if modem status cannot be retrieved. For most cases,
1414 // modem status is on. And for older version modems, GET_MODEM_STATUS
1415 // and disable modem are not supported. Modem is always on.
1416 // TODO: this should be fixed in R to support a third
1417 // status UNKNOWN b/131631629
1418 request.result = true;
1419 Log.e(LOG_TAG, msg.what + " failure. Not updating modem status."
1420 + ar.exception);
1421 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08001422 notifyRequester(request);
1423 break;
Hall Liu73f5d362020-01-20 13:42:00 -08001424 case CMD_SET_SYSTEM_SELECTION_CHANNELS: {
1425 request = (MainThreadRequest) msg.obj;
1426 onCompleted = obtainMessage(EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE, request);
1427 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1428 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1429 request.phone.setSystemSelectionChannels(args.first, onCompleted);
1430 break;
1431 }
1432 case EVENT_SET_SYSTEM_SELECTION_CHANNELS_DONE: {
1433 ar = (AsyncResult) msg.obj;
1434 request = (MainThreadRequest) ar.userObj;
1435 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> args =
1436 (Pair<List<RadioAccessSpecifier>, Consumer<Boolean>>) request.argument;
1437 args.second.accept(ar.exception == null);
1438 notifyRequester(request);
1439 break;
1440 }
yincheng zhao2737e882019-09-06 17:06:54 -07001441 case EVENT_SET_FORBIDDEN_PLMNS_DONE:
1442 ar = (AsyncResult) msg.obj;
1443 request = (MainThreadRequest) ar.userObj;
1444 if (ar.exception == null && ar.result != null) {
1445 request.result = ar.result;
1446 } else {
1447 request.result = -1;
1448 loge("Failed to set Forbidden Plmns");
1449 if (ar.result == null) {
1450 loge("setForbidenPlmns: Empty response");
1451 } else if (ar.exception != null) {
1452 loge("setForbiddenPlmns: Exception: " + ar.exception);
1453 request.result = -1;
1454 } else {
1455 loge("setForbiddenPlmns: Unknown exception");
1456 }
1457 }
1458 notifyRequester(request);
1459 break;
1460 case CMD_SET_FORBIDDEN_PLMNS:
1461 request = (MainThreadRequest) msg.obj;
1462 uiccCard = getUiccCardFromRequest(request);
1463 if (uiccCard == null) {
1464 loge("setForbiddenPlmns: UiccCard is null");
1465 request.result = -1;
1466 notifyRequester(request);
1467 break;
1468 }
1469 Pair<Integer, List<String>> setFplmnsArgs =
1470 (Pair<Integer, List<String>>) request.argument;
1471 appType = setFplmnsArgs.first;
1472 List<String> fplmns = setFplmnsArgs.second;
1473 uiccApp = uiccCard.getApplicationByType(appType);
1474 if (uiccApp == null) {
1475 loge("setForbiddenPlmns: no app with specified type -- " + appType);
1476 request.result = -1;
1477 loge("Failed to get UICC App");
1478 notifyRequester(request);
1479 } else {
1480 onCompleted = obtainMessage(EVENT_SET_FORBIDDEN_PLMNS_DONE, request);
1481 ((SIMRecords) uiccApp.getIccRecords())
1482 .setForbiddenPlmns(onCompleted, fplmns);
1483 }
yinchengzhao4d163c02019-12-12 15:21:47 -08001484 break;
Naina Nallurid63128d2019-09-17 14:10:30 -07001485 case CMD_ERASE_MODEM_CONFIG:
1486 request = (MainThreadRequest) msg.obj;
1487 onCompleted = obtainMessage(EVENT_ERASE_MODEM_CONFIG_DONE, request);
1488 defaultPhone.eraseModemConfig(onCompleted);
1489 break;
1490 case EVENT_ERASE_MODEM_CONFIG_DONE:
1491 handleNullReturnEvent(msg, "eraseModemConfig");
yincheng zhao2737e882019-09-06 17:06:54 -07001492 break;
zoey chene02881a2019-12-30 16:11:23 +08001493
1494 case CMD_CHANGE_ICC_LOCK_PASSWORD:
1495 request = (MainThreadRequest) msg.obj;
1496 onCompleted = obtainMessage(EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE, request);
1497 Pair<String, String> changed = (Pair<String, String>) request.argument;
1498 getPhoneFromRequest(request).getIccCard().changeIccLockPassword(
1499 changed.first, changed.second, onCompleted);
1500 break;
1501 case EVENT_CHANGE_ICC_LOCK_PASSWORD_DONE:
1502 ar = (AsyncResult) msg.obj;
1503 request = (MainThreadRequest) ar.userObj;
1504 if (ar.exception == null) {
1505 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
1506 } else {
1507 request.result = msg.arg1;
1508 }
1509 notifyRequester(request);
1510 break;
1511
1512 case CMD_SET_ICC_LOCK_ENABLED:
1513 request = (MainThreadRequest) msg.obj;
1514 onCompleted = obtainMessage(EVENT_SET_ICC_LOCK_ENABLED_DONE, request);
1515 Pair<Boolean, String> enabled = (Pair<Boolean, String>) request.argument;
1516 getPhoneFromRequest(request).getIccCard().setIccLockEnabled(
1517 enabled.first, enabled.second, onCompleted);
1518 break;
1519 case EVENT_SET_ICC_LOCK_ENABLED_DONE:
1520 ar = (AsyncResult) msg.obj;
1521 request = (MainThreadRequest) ar.userObj;
1522 if (ar.exception == null) {
1523 request.result = TelephonyManager.CHANGE_ICC_LOCK_SUCCESS;
1524 } else {
1525 request.result = msg.arg1;
1526 }
1527 notifyRequester(request);
1528 break;
1529
Peter Wangdafb9ac2020-01-15 14:13:38 -08001530 case MSG_NOTIFY_USER_ACTIVITY:
1531 removeMessages(MSG_NOTIFY_USER_ACTIVITY);
Peter Wang59571be2020-01-27 12:35:15 +08001532 Intent intent = new Intent(TelephonyIntents.ACTION_USER_ACTIVITY_NOTIFICATION);
Peter Wangdafb9ac2020-01-15 14:13:38 -08001533 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
1534 getDefaultPhone().getContext().sendBroadcastAsUser(
1535 intent, UserHandle.ALL, permission.USER_ACTIVITY);
1536 break;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001537 default:
1538 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1539 break;
1540 }
1541 }
Jake Hambye994d462014-02-03 13:10:13 -08001542
Pengquan Menga1bb6272018-09-06 09:59:22 -07001543 private void notifyRequester(MainThreadRequest request) {
1544 synchronized (request) {
1545 request.notifyAll();
1546 }
1547 }
1548
Jake Hambye994d462014-02-03 13:10:13 -08001549 private void handleNullReturnEvent(Message msg, String command) {
1550 AsyncResult ar = (AsyncResult) msg.obj;
1551 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1552 if (ar.exception == null) {
1553 request.result = true;
1554 } else {
1555 request.result = false;
1556 if (ar.exception instanceof CommandException) {
1557 loge(command + ": CommandException: " + ar.exception);
1558 } else {
1559 loge(command + ": Unknown exception");
1560 }
1561 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07001562 notifyRequester(request);
Jake Hambye994d462014-02-03 13:10:13 -08001563 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001564 }
1565
1566 /**
1567 * Posts the specified command to be executed on the main thread,
1568 * waits for the request to complete, and returns the result.
1569 * @see #sendRequestAsync
1570 */
1571 private Object sendRequest(int command, Object argument) {
Nathan Harold92bed182018-10-12 18:16:49 -07001572 return sendRequest(
1573 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001574 }
1575
1576 /**
1577 * Posts the specified command to be executed on the main thread,
1578 * waits for the request to complete, and returns the result.
1579 * @see #sendRequestAsync
1580 */
1581 private Object sendRequest(int command, Object argument, WorkSource workSource) {
1582 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID,
Nathan Harold92bed182018-10-12 18:16:49 -07001583 null, workSource);
Wink Saville36469e72014-06-11 15:17:00 -07001584 }
1585
1586 /**
1587 * Posts the specified command to be executed on the main thread,
1588 * waits for the request to complete, and returns the result.
1589 * @see #sendRequestAsync
1590 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001591 private Object sendRequest(int command, Object argument, Integer subId) {
Nathan Harold92bed182018-10-12 18:16:49 -07001592 return sendRequest(command, argument, subId, null, null);
vagdeviaf9a5b92018-08-15 16:01:53 -07001593 }
1594
1595 /**
1596 * Posts the specified command to be executed on the main thread,
1597 * waits for the request to complete, and returns the result.
1598 * @see #sendRequestAsync
1599 */
Nathan Harold92bed182018-10-12 18:16:49 -07001600 private Object sendRequest(int command, Object argument, int subId, WorkSource workSource) {
1601 return sendRequest(command, argument, subId, null, workSource);
1602 }
1603
1604 /**
1605 * Posts the specified command to be executed on the main thread,
1606 * waits for the request to complete, and returns the result.
1607 * @see #sendRequestAsync
1608 */
1609 private Object sendRequest(int command, Object argument, Phone phone, WorkSource workSource) {
1610 return sendRequest(
1611 command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID, phone, workSource);
1612 }
1613
1614 /**
1615 * Posts the specified command to be executed on the main thread,
1616 * waits for the request to complete, and returns the result.
1617 * @see #sendRequestAsync
1618 */
1619 private Object sendRequest(
1620 int command, Object argument, Integer subId, Phone phone, WorkSource workSource) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001621 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
1622 throw new RuntimeException("This method will deadlock if called from the main thread.");
1623 }
1624
Nathan Harold92bed182018-10-12 18:16:49 -07001625 MainThreadRequest request = null;
1626 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID && phone != null) {
1627 throw new IllegalArgumentException("subId and phone cannot both be specified!");
1628 } else if (phone != null) {
1629 request = new MainThreadRequest(argument, phone, workSource);
1630 } else {
1631 request = new MainThreadRequest(argument, subId, workSource);
1632 }
1633
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001634 Message msg = mMainThreadHandler.obtainMessage(command, request);
1635 msg.sendToTarget();
1636
1637 // Wait for the request to complete
1638 synchronized (request) {
1639 while (request.result == null) {
1640 try {
1641 request.wait();
1642 } catch (InterruptedException e) {
1643 // Do nothing, go back and wait until the request is complete
1644 }
1645 }
1646 }
1647 return request.result;
1648 }
1649
1650 /**
1651 * Asynchronous ("fire and forget") version of sendRequest():
1652 * Posts the specified command to be executed on the main thread, and
1653 * returns immediately.
1654 * @see #sendRequest
1655 */
1656 private void sendRequestAsync(int command) {
1657 mMainThreadHandler.sendEmptyMessage(command);
1658 }
1659
1660 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001661 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001662 * @see {@link #sendRequest(int)}
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001663 */
1664 private void sendRequestAsync(int command, Object argument) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07001665 sendRequestAsync(command, argument, null, null);
1666 }
1667
1668 /**
1669 * Same as {@link #sendRequestAsync(int,Object)} except it takes a Phone and WorkSource.
1670 * @see {@link #sendRequest(int,Object)}
1671 */
1672 private void sendRequestAsync(
1673 int command, Object argument, Phone phone, WorkSource workSource) {
1674 MainThreadRequest request = new MainThreadRequest(argument, phone, workSource);
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001675 Message msg = mMainThreadHandler.obtainMessage(command, request);
1676 msg.sendToTarget();
1677 }
1678
1679 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001680 * Initialize the singleton PhoneInterfaceManager instance.
1681 * This is only done once, at startup, from PhoneApp.onCreate().
1682 */
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001683 /* package */ static PhoneInterfaceManager init(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001684 synchronized (PhoneInterfaceManager.class) {
1685 if (sInstance == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001686 sInstance = new PhoneInterfaceManager(app);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001687 } else {
1688 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1689 }
1690 return sInstance;
1691 }
1692 }
1693
1694 /** Private constructor; @see init() */
Jordan Liu1979a042020-03-20 21:39:35 +00001695 private PhoneInterfaceManager(PhoneGlobals app) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001696 mApp = app;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001697 mCM = PhoneGlobals.getInstance().mCM;
Brad Ebinger24c29992019-12-05 13:03:21 -08001698 mImsResolver = PhoneGlobals.getInstance().getImsResolver();
Stuart Scott981d8582015-04-21 14:09:50 -07001699 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001700 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1701 mMainThreadHandler = new MainThreadHandler();
Tobias Thiererb19e1f12018-12-11 17:54:03 +00001702 mSubscriptionController = SubscriptionController.getInstance();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001703 mTelephonySharedPreferences =
1704 PreferenceManager.getDefaultSharedPreferences(mApp);
yinxub1bed742017-04-17 11:45:04 -07001705 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07001706 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Peter Wanga3cf4ac2020-01-27 09:39:46 +08001707 mNotifyUserActivity = new AtomicBoolean(false);
Wink Saville3ab207e2014-11-20 13:07:20 -08001708
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001709 publish();
1710 }
1711
Nazanin Bakhshif782e562018-12-11 15:15:39 -08001712 private Phone getDefaultPhone() {
1713 Phone thePhone = getPhone(getDefaultSubscription());
1714 return (thePhone != null) ? thePhone : PhoneFactory.getDefaultPhone();
1715 }
1716
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001717 private void publish() {
1718 if (DBG) log("publish: " + this);
1719
Peter Wangc035ce42020-01-08 21:00:22 -08001720 TelephonyFrameworkInitializer
1721 .getTelephonyServiceManager()
1722 .getTelephonyServiceRegisterer()
1723 .register(this);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001724 }
1725
Stuart Scott584921c2015-01-15 17:10:34 -08001726 private Phone getPhoneFromRequest(MainThreadRequest request) {
Jordan Liu4c733742019-02-28 12:03:40 -08001727 if (request.phone != null) {
1728 return request.phone;
1729 } else {
1730 return getPhoneFromSubId(request.subId);
1731 }
1732 }
1733
1734 private Phone getPhoneFromSubId(int subId) {
1735 return (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1736 ? getDefaultPhone() : getPhone(subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001737 }
1738
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001739 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1740 Phone phone = getPhoneFromRequest(request);
1741 return phone == null ? null :
1742 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1743 }
1744
Wink Saville36469e72014-06-11 15:17:00 -07001745 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001746 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001747 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001748 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001749
Naina Nallurid63128d2019-09-17 14:10:30 -07001750 private void sendEraseModemConfig(Phone phone) {
1751 if (phone != null) {
1752 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
1753 mApp, phone.getSubId(), "eraseModemConfig");
1754 final long identity = Binder.clearCallingIdentity();
1755 try {
1756 Boolean success = (Boolean) sendRequest(CMD_ERASE_MODEM_CONFIG, null);
1757 if (DBG) log("eraseModemConfig:" + ' ' + (success ? "ok" : "fail"));
1758 } finally {
1759 Binder.restoreCallingIdentity(identity);
1760 }
1761 }
1762 }
1763
Peter Wang44b186e2020-01-13 23:33:09 -08001764 private boolean isImsAvailableOnDevice() {
1765 PackageManager pm = getDefaultPhone().getContext().getPackageManager();
1766 if (pm == null) {
1767 // For some reason package manger is not available.. This will fail internally anyway,
1768 // so do not throw error and allow.
1769 return true;
1770 }
1771 return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS, 0);
1772 }
1773
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001774 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001775 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001776 }
1777
Wink Savilleb564aae2014-10-23 10:18:09 -07001778 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001779 if (DBG) log("dial: " + number);
1780 // No permission check needed here: This is just a wrapper around the
1781 // ACTION_DIAL intent, which is available to any app since it puts up
1782 // the UI before it does anything.
1783
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001784 final long identity = Binder.clearCallingIdentity();
1785 try {
1786 String url = createTelUrl(number);
1787 if (url == null) {
1788 return;
1789 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001790
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001791 // PENDING: should we just silently fail if phone is offhook or ringing?
1792 PhoneConstants.State state = mCM.getState(subId);
1793 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1794 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1795 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1796 mApp.startActivity(intent);
1797 }
1798 } finally {
1799 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001800 }
1801 }
1802
1803 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001804 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001805 }
1806
Wink Savilleb564aae2014-10-23 10:18:09 -07001807 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001808 if (DBG) log("call: " + number);
1809
1810 // This is just a wrapper around the ACTION_CALL intent, but we still
1811 // need to do a permission check since we're calling startActivity()
1812 // from the context of the phone app.
1813 enforceCallPermission();
1814
Jordan Liu1617b712019-07-10 15:06:26 -07001815 if (mAppOps.noteOp(AppOpsManager.OPSTR_CALL_PHONE, Binder.getCallingUid(), callingPackage)
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001816 != AppOpsManager.MODE_ALLOWED) {
1817 return;
1818 }
1819
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001820 final long identity = Binder.clearCallingIdentity();
1821 try {
1822 String url = createTelUrl(number);
1823 if (url == null) {
1824 return;
1825 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001826
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001827 boolean isValid = false;
1828 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
1829 if (slist != null) {
1830 for (SubscriptionInfo subInfoRecord : slist) {
1831 if (subInfoRecord.getSubscriptionId() == subId) {
1832 isValid = true;
1833 break;
1834 }
Wink Saville3ab207e2014-11-20 13:07:20 -08001835 }
Wink Saville08874612014-08-31 19:19:58 -07001836 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001837 if (!isValid) {
1838 return;
1839 }
Wink Saville08874612014-08-31 19:19:58 -07001840
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001841 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
1842 intent.putExtra(SUBSCRIPTION_KEY, subId);
1843 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1844 mApp.startActivity(intent);
1845 } finally {
1846 Binder.restoreCallingIdentity(identity);
1847 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001848 }
1849
Wink Savilleb564aae2014-10-23 10:18:09 -07001850 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001851 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001852 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1853 }
1854
Wink Savilleb564aae2014-10-23 10:18:09 -07001855 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001856 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001857 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1858 }
1859
Wink Savilleb564aae2014-10-23 10:18:09 -07001860 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001861 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001862
1863 final long identity = Binder.clearCallingIdentity();
1864 try {
1865 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
1866 checkSimPin.start();
1867 return checkSimPin.unlockSim(null, pin);
1868 } finally {
1869 Binder.restoreCallingIdentity(identity);
1870 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001871 }
1872
Wink Savilleb564aae2014-10-23 10:18:09 -07001873 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001874 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001875
1876 final long identity = Binder.clearCallingIdentity();
1877 try {
1878 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
1879 checkSimPuk.start();
1880 return checkSimPuk.unlockSim(puk, pin);
1881 } finally {
1882 Binder.restoreCallingIdentity(identity);
1883 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001884 }
1885
1886 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001887 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001888 * a synchronous one.
1889 */
1890 private static class UnlockSim extends Thread {
1891
1892 private final IccCard mSimCard;
1893
1894 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001895 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1896 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001897
1898 // For replies from SimCard interface
1899 private Handler mHandler;
1900
1901 // For async handler to identify request type
1902 private static final int SUPPLY_PIN_COMPLETE = 100;
1903
1904 public UnlockSim(IccCard simCard) {
1905 mSimCard = simCard;
1906 }
1907
1908 @Override
1909 public void run() {
1910 Looper.prepare();
1911 synchronized (UnlockSim.this) {
1912 mHandler = new Handler() {
1913 @Override
1914 public void handleMessage(Message msg) {
1915 AsyncResult ar = (AsyncResult) msg.obj;
1916 switch (msg.what) {
1917 case SUPPLY_PIN_COMPLETE:
1918 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1919 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001920 mRetryCount = msg.arg1;
1921 if (ar.exception != null) {
1922 if (ar.exception instanceof CommandException &&
1923 ((CommandException)(ar.exception)).getCommandError()
1924 == CommandException.Error.PASSWORD_INCORRECT) {
1925 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
vivi.lib5e9ada2019-09-12 16:04:24 +08001926 } //When UiccCardApp dispose,handle message and return exception
1927 else if (ar.exception instanceof CommandException &&
1928 ((CommandException) (ar.exception)).getCommandError()
1929 == CommandException.Error.ABORTED) {
1930 mResult = PhoneConstants.PIN_OPERATION_ABORTED;
Wink Saville9de0f752013-10-22 19:04:03 -07001931 } else {
1932 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1933 }
1934 } else {
1935 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1936 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001937 mDone = true;
1938 UnlockSim.this.notifyAll();
1939 }
1940 break;
1941 }
1942 }
1943 };
1944 UnlockSim.this.notifyAll();
1945 }
1946 Looper.loop();
1947 }
1948
1949 /*
1950 * Use PIN or PUK to unlock SIM card
1951 *
1952 * If PUK is null, unlock SIM card with PIN
1953 *
1954 * If PUK is not null, unlock SIM card with PUK and set PIN code
1955 */
Wink Saville9de0f752013-10-22 19:04:03 -07001956 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001957
1958 while (mHandler == null) {
1959 try {
1960 wait();
1961 } catch (InterruptedException e) {
1962 Thread.currentThread().interrupt();
1963 }
1964 }
1965 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1966
1967 if (puk == null) {
1968 mSimCard.supplyPin(pin, callback);
1969 } else {
1970 mSimCard.supplyPuk(puk, pin, callback);
1971 }
1972
1973 while (!mDone) {
1974 try {
1975 Log.d(LOG_TAG, "wait for done");
1976 wait();
1977 } catch (InterruptedException e) {
1978 // Restore the interrupted status
1979 Thread.currentThread().interrupt();
1980 }
1981 }
1982 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001983 int[] resultArray = new int[2];
1984 resultArray[0] = mResult;
1985 resultArray[1] = mRetryCount;
1986 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001987 }
1988 }
1989
Nathan Harold7c8d0f12020-05-28 20:40:31 -07001990 /**
1991 * This method has been removed due to privacy and stability concerns.
1992 */
1993 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001994 public void updateServiceLocation() {
Nathan Harold7c8d0f12020-05-28 20:40:31 -07001995 Log.e(LOG_TAG, "Call to unsupported method updateServiceLocation()");
1996 return;
Wink Saville36469e72014-06-11 15:17:00 -07001997 }
1998
Nathan Harold1f889d82020-06-04 17:05:26 -07001999 @Override
2000 public void updateServiceLocationWithPackageName(String callingPackage) {
2001 mApp.getSystemService(AppOpsManager.class)
2002 .checkPackage(Binder.getCallingUid(), callingPackage);
2003
2004 final int targetSdk = getTargetSdk(callingPackage);
2005 if (targetSdk > android.os.Build.VERSION_CODES.R) {
2006 // Callers targeting S have no business invoking this method.
2007 return;
2008 }
2009
2010 LocationAccessPolicy.LocationPermissionResult locationResult =
2011 LocationAccessPolicy.checkLocationPermission(mApp,
2012 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2013 .setCallingPackage(callingPackage)
2014 .setCallingFeatureId(null)
2015 .setCallingPid(Binder.getCallingPid())
2016 .setCallingUid(Binder.getCallingUid())
2017 .setMethod("updateServiceLocation")
2018 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2019 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2020 .build());
2021 // Apps that lack location permission have no business calling this method;
2022 // however, because no permission was declared in the public API, denials must
2023 // all be "soft".
2024 switch (locationResult) {
2025 case DENIED_HARD: /* fall through */
2026 case DENIED_SOFT:
2027 return;
2028 }
2029
2030 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002031 final long identity = Binder.clearCallingIdentity();
2032 try {
Nathan Harold1f889d82020-06-04 17:05:26 -07002033 final Phone phone = getPhone(getDefaultSubscription());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002034 if (phone != null) {
Nathan Harold1f889d82020-06-04 17:05:26 -07002035 phone.updateServiceLocation(workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002036 }
2037 } finally {
2038 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002039 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002040 }
2041
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002042 @Deprecated
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002043 @Override
2044 public boolean isRadioOn(String callingPackage) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002045 return isRadioOnWithFeature(callingPackage, null);
2046 }
2047
2048
2049 @Override
2050 public boolean isRadioOnWithFeature(String callingPackage, String callingFeatureId) {
2051 return isRadioOnForSubscriberWithFeature(getDefaultSubscription(), callingPackage,
2052 callingFeatureId);
2053 }
2054
2055 @Deprecated
2056 @Override
2057 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
2058 return isRadioOnForSubscriberWithFeature(subId, callingPackage, null);
Wink Saville36469e72014-06-11 15:17:00 -07002059 }
2060
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002061 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002062 public boolean isRadioOnForSubscriberWithFeature(int subId, String callingPackage,
2063 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002064 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002065 mApp, subId, callingPackage, callingFeatureId, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002066 return false;
2067 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002068
2069 final long identity = Binder.clearCallingIdentity();
2070 try {
2071 return isRadioOnForSubscriber(subId);
2072 } finally {
2073 Binder.restoreCallingIdentity(identity);
2074 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002075 }
2076
2077 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002078 final long identity = Binder.clearCallingIdentity();
2079 try {
2080 final Phone phone = getPhone(subId);
2081 if (phone != null) {
2082 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
2083 } else {
2084 return false;
2085 }
2086 } finally {
2087 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002088 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002089 }
2090
2091 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002092 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002093 }
Wink Saville36469e72014-06-11 15:17:00 -07002094
Wink Savilleb564aae2014-10-23 10:18:09 -07002095 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002096 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002097
2098 final long identity = Binder.clearCallingIdentity();
2099 try {
2100 final Phone phone = getPhone(subId);
2101 if (phone != null) {
2102 phone.setRadioPower(!isRadioOnForSubscriber(subId));
2103 }
2104 } finally {
2105 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002106 }
Wink Saville36469e72014-06-11 15:17:00 -07002107 }
2108
2109 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002110 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07002111 }
2112
Wink Savilleb564aae2014-10-23 10:18:09 -07002113 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07002114 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002115
2116 final long identity = Binder.clearCallingIdentity();
2117 try {
2118 final Phone phone = getPhone(subId);
2119 if (phone == null) {
2120 return false;
2121 }
2122 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
2123 toggleRadioOnOffForSubscriber(subId);
2124 }
2125 return true;
2126 } finally {
2127 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002128 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002129 }
Wink Saville36469e72014-06-11 15:17:00 -07002130
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002131 public boolean needMobileRadioShutdown() {
Shuo Qianfa7b6b32019-12-10 10:40:38 -08002132 enforceReadPrivilegedPermission("needMobileRadioShutdown");
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002133 /*
2134 * If any of the Radios are available, it will need to be
2135 * shutdown. So return true if any Radio is available.
2136 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002137 final long identity = Binder.clearCallingIdentity();
2138 try {
2139 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2140 Phone phone = PhoneFactory.getPhone(i);
2141 if (phone != null && phone.isRadioAvailable()) return true;
2142 }
2143 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
2144 return false;
2145 } finally {
2146 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002147 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002148 }
2149
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002150 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002151 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002152 enforceModifyPermission();
2153
2154 final long identity = Binder.clearCallingIdentity();
2155 try {
2156 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2157 logv("Shutting down Phone " + i);
2158 shutdownRadioUsingPhoneId(i);
2159 }
2160 } finally {
2161 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002162 }
2163 }
2164
2165 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002166 Phone phone = PhoneFactory.getPhone(phoneId);
2167 if (phone != null && phone.isRadioAvailable()) {
2168 phone.shutdownRadio();
2169 }
2170 }
2171
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002172 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07002173 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002174
2175 final long identity = Binder.clearCallingIdentity();
2176 try {
2177 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
2178 if (defaultPhone != null) {
2179 defaultPhone.setRadioPower(turnOn);
2180 return true;
2181 } else {
2182 loge("There's no default phone.");
2183 return false;
2184 }
2185 } finally {
2186 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07002187 }
Wink Saville36469e72014-06-11 15:17:00 -07002188 }
2189
Wink Savilleb564aae2014-10-23 10:18:09 -07002190 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002191 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002192
2193 final long identity = Binder.clearCallingIdentity();
2194 try {
2195 final Phone phone = getPhone(subId);
2196 if (phone != null) {
2197 phone.setRadioPower(turnOn);
2198 return true;
2199 } else {
2200 return false;
2201 }
2202 } finally {
2203 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002204 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002205 }
2206
Wink Saville36469e72014-06-11 15:17:00 -07002207 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002208 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002209 public boolean enableDataConnectivity() {
2210 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002211
2212 final long identity = Binder.clearCallingIdentity();
2213 try {
2214 int subId = mSubscriptionController.getDefaultDataSubId();
2215 final Phone phone = getPhone(subId);
2216 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00002217 phone.getDataEnabledSettings().setDataEnabled(
2218 TelephonyManager.DATA_ENABLED_REASON_USER, true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002219 return true;
2220 } else {
2221 return false;
2222 }
2223 } finally {
2224 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002225 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002226 }
2227
Wink Saville36469e72014-06-11 15:17:00 -07002228 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07002229 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002230 public boolean disableDataConnectivity() {
2231 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002232
2233 final long identity = Binder.clearCallingIdentity();
2234 try {
2235 int subId = mSubscriptionController.getDefaultDataSubId();
2236 final Phone phone = getPhone(subId);
2237 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00002238 phone.getDataEnabledSettings().setDataEnabled(
2239 TelephonyManager.DATA_ENABLED_REASON_USER, false);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002240 return true;
2241 } else {
2242 return false;
2243 }
2244 } finally {
2245 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002246 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002247 }
2248
Sanket Padawe356d7632015-06-22 14:03:32 -07002249 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07002250 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002251 final long identity = Binder.clearCallingIdentity();
2252 try {
2253 final Phone phone = getPhone(subId);
2254 if (phone != null) {
Jack Yub5d8f642018-11-26 11:20:48 -08002255 return phone.isDataAllowed(ApnSetting.TYPE_DEFAULT);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002256 } else {
2257 return false;
2258 }
2259 } finally {
2260 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002261 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002262 }
2263
2264 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002265 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07002266 }
2267
pkanwarae03a6b2016-11-06 20:37:09 -08002268 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002269 enforceCallPermission();
2270
2271 final long identity = Binder.clearCallingIdentity();
2272 try {
2273 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2274 return;
2275 }
2276 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
2277 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
2278 } finally {
2279 Binder.restoreCallingIdentity(identity);
2280 }
pkanwar32d516d2016-10-14 19:37:38 -07002281 };
2282
Wink Savilleb564aae2014-10-23 10:18:09 -07002283 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002284 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002285
2286 final long identity = Binder.clearCallingIdentity();
2287 try {
2288 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
2289 return false;
2290 }
2291 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
2292 } finally {
2293 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002294 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002295 }
2296
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002297 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002298 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002299 }
2300
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002301 public int getCallStateForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002302 final long identity = Binder.clearCallingIdentity();
2303 try {
2304 Phone phone = PhoneFactory.getPhone(slotIndex);
2305 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
2306 PhoneConstantConversions.convertCallState(phone.getState());
2307 } finally {
2308 Binder.restoreCallingIdentity(identity);
2309 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002310 }
2311
Sanket Padawe356d7632015-06-22 14:03:32 -07002312 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00002313 public int getDataState() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002314 return getDataStateForSubId(mSubscriptionController.getDefaultDataSubId());
2315 }
2316
2317 @Override
2318 public int getDataStateForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002319 final long identity = Binder.clearCallingIdentity();
2320 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002321 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002322 if (phone != null) {
2323 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
2324 } else {
2325 return PhoneConstantConversions.convertDataState(
2326 PhoneConstants.DataState.DISCONNECTED);
2327 }
2328 } finally {
2329 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002330 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002331 }
2332
Sanket Padawe356d7632015-06-22 14:03:32 -07002333 @Override
Nathan Harolde037c472019-06-26 00:41:07 +00002334 public int getDataActivity() {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002335 return getDataActivityForSubId(mSubscriptionController.getDefaultDataSubId());
2336 }
2337
2338 @Override
2339 public int getDataActivityForSubId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002340 final long identity = Binder.clearCallingIdentity();
2341 try {
Nathan Haroldc4689b12019-06-14 16:58:30 -07002342 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002343 if (phone != null) {
2344 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
2345 } else {
2346 return TelephonyManager.DATA_ACTIVITY_NONE;
2347 }
2348 } finally {
2349 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002350 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002351 }
2352
2353 @Override
Meng Wanga10e89e2019-12-09 13:13:01 -08002354 public CellIdentity getCellLocation(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002355 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002356 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002357
2358 LocationAccessPolicy.LocationPermissionResult locationResult =
2359 LocationAccessPolicy.checkLocationPermission(mApp,
2360 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2361 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002362 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002363 .setCallingPid(Binder.getCallingPid())
2364 .setCallingUid(Binder.getCallingUid())
2365 .setMethod("getCellLocation")
Hall Liu773ba022020-01-24 18:07:12 -08002366 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002367 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2368 .build());
2369 switch (locationResult) {
2370 case DENIED_HARD:
2371 throw new SecurityException("Not allowed to access cell location");
2372 case DENIED_SOFT:
Meng Wanga10e89e2019-12-09 13:13:01 -08002373 return (getDefaultPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA)
2374 ? new CellIdentityCdma() : new CellIdentityGsm();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002375 }
2376
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002377 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002378 final long identity = Binder.clearCallingIdentity();
2379 try {
2380 if (DBG_LOC) log("getCellLocation: is active user");
Nathan Harold3ff88932018-08-14 10:19:49 -07002381 int subId = mSubscriptionController.getDefaultDataSubId();
Meng Wanga10e89e2019-12-09 13:13:01 -08002382 return (CellIdentity) sendRequest(CMD_GET_CELL_LOCATION, workSource, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002383 } finally {
2384 Binder.restoreCallingIdentity(identity);
2385 }
Svetoslav64fad262015-04-14 14:35:21 -07002386 }
2387
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002388 @Override
Jack Yueb1e7fe2020-02-22 19:38:58 -08002389 public String getNetworkCountryIsoForPhone(int phoneId) {
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002390 // Reporting the correct network country is ambiguous when IWLAN could conflict with
2391 // registered cell info, so return a NULL country instead.
2392 final long identity = Binder.clearCallingIdentity();
2393 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07002394 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
2395 // Get default phone in this case.
2396 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
2397 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002398 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002399 Phone phone = PhoneFactory.getPhone(phoneId);
Nathan Harold532f51c2020-04-21 19:31:10 -07002400 if (phone == null) return "";
2401 ServiceStateTracker sst = phone.getServiceStateTracker();
2402 if (sst == null) return "";
2403 LocaleTracker lt = sst.getLocaleTracker();
2404 if (lt == null) return "";
2405 if (!TextUtils.isEmpty(lt.getCurrentCountry())) return lt.getCurrentCountry();
2406 EmergencyNumberTracker ent = phone.getEmergencyNumberTracker();
2407 return (ent == null) ? "" : ent.getEmergencyCountryIso();
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002408 } finally {
2409 Binder.restoreCallingIdentity(identity);
2410 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002411 }
2412
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002413 /**
2414 * This method was removed due to potential issues caused by performing partial
2415 * updates of service state, and lack of a credible use case.
2416 *
2417 * This has the ability to break the telephony implementation by disabling notification of
2418 * changes in device connectivity. DO NOT USE THIS!
2419 */
Jonathan Basseribf5362b2017-07-19 12:22:35 -07002420 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002421 public void enableLocationUpdates() {
2422 mApp.enforceCallingOrSelfPermission(
2423 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002424 }
2425
Nathan Harold7c8d0f12020-05-28 20:40:31 -07002426 /**
2427 * This method was removed due to potential issues caused by performing partial
2428 * updates of service state, and lack of a credible use case.
2429 *
2430 * This has the ability to break the telephony implementation by disabling notification of
2431 * changes in device connectivity. DO NOT USE THIS!
2432 */
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002433 @Override
2434 public void disableLocationUpdates() {
2435 mApp.enforceCallingOrSelfPermission(
2436 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002437 }
2438
Nathan Harold31d7ff32018-10-15 20:20:30 -07002439 /**
2440 * Returns the target SDK version number for a given package name.
2441 *
Nathan Haroldec184742019-07-10 17:04:16 -07002442 * This call MUST be invoked before clearing the calling UID.
2443 *
Nathan Harold31d7ff32018-10-15 20:20:30 -07002444 * @return target SDK if the package is found or INT_MAX.
2445 */
2446 private int getTargetSdk(String packageName) {
2447 try {
Nathan Haroldec184742019-07-10 17:04:16 -07002448 final ApplicationInfo ai = mApp.getPackageManager().getApplicationInfoAsUser(
Chen Xu0150f0e2019-07-30 15:12:06 -07002449 packageName, 0, UserHandle.getUserHandleForUid(Binder.getCallingUid()));
Nathan Harold31d7ff32018-10-15 20:20:30 -07002450 if (ai != null) return ai.targetSdkVersion;
2451 } catch (PackageManager.NameNotFoundException unexpected) {
Nathan Haroldec184742019-07-10 17:04:16 -07002452 loge("Failed to get package info for pkg="
2453 + packageName + ", uid=" + Binder.getCallingUid());
Nathan Harold31d7ff32018-10-15 20:20:30 -07002454 }
2455 return Integer.MAX_VALUE;
2456 }
2457
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002458 @Override
2459 @SuppressWarnings("unchecked")
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002460 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage,
2461 String callingFeatureId) {
Nathan Harold31d7ff32018-10-15 20:20:30 -07002462 final int targetSdk = getTargetSdk(callingPackage);
Nathan Harolddbea45a2018-08-30 14:35:07 -07002463 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2464 throw new SecurityException(
2465 "getNeighboringCellInfo() is unavailable to callers targeting Q+ SDK levels.");
2466 }
Nathan Haroldb4d55612018-07-20 13:13:08 -07002467
Jordan Liu1617b712019-07-10 15:06:26 -07002468 if (mAppOps.noteOp(AppOpsManager.OPSTR_NEIGHBORING_CELLS, Binder.getCallingUid(),
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002469 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2470 return null;
2471 }
Svetoslav64fad262015-04-14 14:35:21 -07002472
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002473 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002474
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002475 List<CellInfo> info = getAllCellInfo(callingPackage, callingFeatureId);
Nathan Haroldf180aac2018-06-01 18:43:55 -07002476 if (info == null) return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002477
Nathan Haroldf180aac2018-06-01 18:43:55 -07002478 List<NeighboringCellInfo> neighbors = new ArrayList<NeighboringCellInfo>();
2479 for (CellInfo ci : info) {
2480 if (ci instanceof CellInfoGsm) {
2481 neighbors.add(new NeighboringCellInfo((CellInfoGsm) ci));
2482 } else if (ci instanceof CellInfoWcdma) {
2483 neighbors.add(new NeighboringCellInfo((CellInfoWcdma) ci));
2484 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002485 }
Nathan Haroldf180aac2018-06-01 18:43:55 -07002486 return (neighbors.size()) > 0 ? neighbors : null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002487 }
2488
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002489 private List<CellInfo> getCachedCellInfo() {
2490 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2491 for (Phone phone : PhoneFactory.getPhones()) {
2492 List<CellInfo> info = phone.getAllCellInfo();
2493 if (info != null) cellInfos.addAll(info);
2494 }
2495 return cellInfos;
2496 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002497
2498 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002499 public List<CellInfo> getAllCellInfo(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002500 mApp.getSystemService(AppOpsManager.class)
Hall Liu1aa510f2017-11-22 17:40:08 -08002501 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002502
2503 LocationAccessPolicy.LocationPermissionResult locationResult =
2504 LocationAccessPolicy.checkLocationPermission(mApp,
2505 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2506 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002507 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002508 .setCallingPid(Binder.getCallingPid())
2509 .setCallingUid(Binder.getCallingUid())
2510 .setMethod("getAllCellInfo")
Nathan Harold5ae50b52019-02-20 15:46:36 -08002511 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002512 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
2513 .build());
2514 switch (locationResult) {
2515 case DENIED_HARD:
2516 throw new SecurityException("Not allowed to access cell info");
2517 case DENIED_SOFT:
2518 return new ArrayList<>();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002519 }
2520
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002521 final int targetSdk = getTargetSdk(callingPackage);
2522 if (targetSdk >= android.os.Build.VERSION_CODES.Q) {
2523 return getCachedCellInfo();
2524 }
2525
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07002526 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002527 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002528 final long identity = Binder.clearCallingIdentity();
2529 try {
2530 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
2531 for (Phone phone : PhoneFactory.getPhones()) {
Nathan Harold3ff88932018-08-14 10:19:49 -07002532 final List<CellInfo> info = (List<CellInfo>) sendRequest(
Nathan Harold92bed182018-10-12 18:16:49 -07002533 CMD_GET_ALL_CELL_INFO, null, phone, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002534 if (info != null) cellInfos.addAll(info);
2535 }
2536 return cellInfos;
2537 } finally {
2538 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002539 }
2540 }
2541
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002542 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002543 public void requestCellInfoUpdate(int subId, ICellInfoCallback cb, String callingPackage,
2544 String callingFeatureId) {
2545 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId,
2546 getWorkSource(Binder.getCallingUid()));
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002547 }
2548
2549 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002550 public void requestCellInfoUpdateWithWorkSource(int subId, ICellInfoCallback cb,
2551 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002552 enforceModifyPermission();
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002553 requestCellInfoUpdateInternal(subId, cb, callingPackage, callingFeatureId, workSource);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002554 }
2555
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002556 private void requestCellInfoUpdateInternal(int subId, ICellInfoCallback cb,
2557 String callingPackage, String callingFeatureId, WorkSource workSource) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002558 mApp.getSystemService(AppOpsManager.class)
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002559 .checkPackage(Binder.getCallingUid(), callingPackage);
Hall Liuf19c44f2018-11-27 14:38:17 -08002560
2561 LocationAccessPolicy.LocationPermissionResult locationResult =
2562 LocationAccessPolicy.checkLocationPermission(mApp,
2563 new LocationAccessPolicy.LocationPermissionQuery.Builder()
2564 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07002565 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08002566 .setCallingPid(Binder.getCallingPid())
2567 .setCallingUid(Binder.getCallingUid())
2568 .setMethod("requestCellInfoUpdate")
Hall Liud60acc92020-05-21 17:09:35 -07002569 .setMinSdkVersionForCoarse(Build.VERSION_CODES.BASE)
2570 .setMinSdkVersionForFine(Build.VERSION_CODES.BASE)
Hall Liuf19c44f2018-11-27 14:38:17 -08002571 .build());
2572 switch (locationResult) {
2573 case DENIED_HARD:
Hall Liud60acc92020-05-21 17:09:35 -07002574 if (getTargetSdk(callingPackage) < Build.VERSION_CODES.Q) {
2575 // Safetynet logging for b/154934934
2576 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
2577 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002578 throw new SecurityException("Not allowed to access cell info");
2579 case DENIED_SOFT:
Hall Liud60acc92020-05-21 17:09:35 -07002580 if (getTargetSdk(callingPackage) < Build.VERSION_CODES.Q) {
2581 // Safetynet logging for b/154934934
2582 EventLog.writeEvent(0x534e4554, "154934934", Binder.getCallingUid());
2583 }
Nathan Harold5320c422019-05-09 10:26:08 -07002584 try {
2585 cb.onCellInfo(new ArrayList<CellInfo>());
2586 } catch (RemoteException re) {
2587 // Drop without consequences
2588 }
Hall Liuf19c44f2018-11-27 14:38:17 -08002589 return;
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002590 }
2591
Nathan Harolda939a962019-05-09 10:13:47 -07002592
2593 final Phone phone = getPhoneFromSubId(subId);
Nathan Haroldfa8da0f2018-09-27 18:51:29 -07002594 if (phone == null) throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
2595
2596 sendRequestAsync(CMD_REQUEST_CELL_INFO_UPDATE, cb, phone, workSource);
2597 }
2598
2599 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002600 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002601 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002602 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002603
2604 final long identity = Binder.clearCallingIdentity();
2605 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002606 getDefaultPhone().setCellInfoListRate(rateInMillis, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002607 } finally {
2608 Binder.restoreCallingIdentity(identity);
2609 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002610 }
2611
Shishir Agrawala9f32182016-04-12 12:00:16 -07002612 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002613 public String getImeiForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002614 Phone phone = PhoneFactory.getPhone(slotIndex);
2615 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002616 return null;
2617 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002618 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002619 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002620 callingPackage, callingFeatureId, "getImeiForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002621 return null;
2622 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002623
2624 final long identity = Binder.clearCallingIdentity();
2625 try {
2626 return phone.getImei();
2627 } finally {
2628 Binder.restoreCallingIdentity(identity);
2629 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002630 }
2631
2632 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002633 public String getTypeAllocationCodeForSlot(int slotIndex) {
2634 Phone phone = PhoneFactory.getPhone(slotIndex);
2635 String tac = null;
2636 if (phone != null) {
2637 String imei = phone.getImei();
2638 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2639 }
2640 return tac;
2641 }
2642
2643 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002644 public String getMeidForSlot(int slotIndex, String callingPackage, String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002645 Phone phone = PhoneFactory.getPhone(slotIndex);
2646 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002647 return null;
2648 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002649
Jeff Davidson913390f2018-02-23 17:11:49 -08002650 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07002651 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002652 callingPackage, callingFeatureId, "getMeidForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002653 return null;
2654 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002655
2656 final long identity = Binder.clearCallingIdentity();
2657 try {
2658 return phone.getMeid();
2659 } finally {
2660 Binder.restoreCallingIdentity(identity);
2661 }
Jack Yu2af8d712017-03-15 17:14:14 -07002662 }
2663
2664 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002665 public String getManufacturerCodeForSlot(int slotIndex) {
2666 Phone phone = PhoneFactory.getPhone(slotIndex);
2667 String manufacturerCode = null;
2668 if (phone != null) {
2669 String meid = phone.getMeid();
2670 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2671 }
2672 return manufacturerCode;
2673 }
2674
2675 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002676 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage,
2677 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002678 Phone phone = PhoneFactory.getPhone(slotIndex);
2679 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002680 return null;
2681 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002682 int subId = phone.getSubId();
2683 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002684 mApp, subId, callingPackage, callingFeatureId,
2685 "getDeviceSoftwareVersionForSlot")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002686 return null;
2687 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002688
2689 final long identity = Binder.clearCallingIdentity();
2690 try {
2691 return phone.getDeviceSvn();
2692 } finally {
2693 Binder.restoreCallingIdentity(identity);
2694 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002695 }
2696
fionaxu43304da2017-11-27 22:51:16 -08002697 @Override
2698 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002699 final long identity = Binder.clearCallingIdentity();
2700 try {
2701 final Phone phone = getPhone(subId);
2702 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2703 } finally {
2704 Binder.restoreCallingIdentity(identity);
2705 }
fionaxu43304da2017-11-27 22:51:16 -08002706 }
2707
2708 @Override
2709 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002710 final long identity = Binder.clearCallingIdentity();
2711 try {
2712 final Phone phone = getPhone(subId);
2713 return phone == null ? null : phone.getCarrierName();
2714 } finally {
2715 Binder.restoreCallingIdentity(identity);
2716 }
fionaxu43304da2017-11-27 22:51:16 -08002717 }
2718
calvinpanffe225e2018-11-01 19:43:06 +08002719 @Override
chen xu0026ca62019-03-06 15:28:50 -08002720 public int getSubscriptionSpecificCarrierId(int subId) {
chen xu25637222018-11-04 17:17:00 -08002721 final long identity = Binder.clearCallingIdentity();
2722 try {
2723 final Phone phone = getPhone(subId);
2724 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID
chen xu0026ca62019-03-06 15:28:50 -08002725 : phone.getSpecificCarrierId();
chen xu25637222018-11-04 17:17:00 -08002726 } finally {
2727 Binder.restoreCallingIdentity(identity);
2728 }
2729 }
2730
2731 @Override
chen xu0026ca62019-03-06 15:28:50 -08002732 public String getSubscriptionSpecificCarrierName(int subId) {
chen xu25637222018-11-04 17:17:00 -08002733 final long identity = Binder.clearCallingIdentity();
2734 try {
2735 final Phone phone = getPhone(subId);
chen xu0026ca62019-03-06 15:28:50 -08002736 return phone == null ? null : phone.getSpecificCarrierName();
chen xu25637222018-11-04 17:17:00 -08002737 } finally {
2738 Binder.restoreCallingIdentity(identity);
2739 }
2740 }
2741
chen xu651eec72018-11-11 19:03:44 -08002742 @Override
chen xu864e11c2018-12-06 22:10:03 -08002743 public int getCarrierIdFromMccMnc(int slotIndex, String mccmnc, boolean isSubscriptionMccMnc) {
2744 if (!isSubscriptionMccMnc) {
2745 enforceReadPrivilegedPermission("getCarrierIdFromMccMnc");
2746 }
chen xu651eec72018-11-11 19:03:44 -08002747 final Phone phone = PhoneFactory.getPhone(slotIndex);
2748 if (phone == null) {
2749 return TelephonyManager.UNKNOWN_CARRIER_ID;
2750 }
2751 final long identity = Binder.clearCallingIdentity();
2752 try {
2753 return CarrierResolver.getCarrierIdFromMccMnc(phone.getContext(), mccmnc);
2754 } finally {
2755 Binder.restoreCallingIdentity(identity);
2756 }
2757 }
2758
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002759 //
2760 // Internal helper methods.
2761 //
2762
Sanket Padaweee13a9b2016-03-08 17:30:28 -08002763 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002764 * Make sure the caller has the MODIFY_PHONE_STATE permission.
2765 *
2766 * @throws SecurityException if the caller does not have the required permission
2767 */
2768 private void enforceModifyPermission() {
2769 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
2770 }
2771
Shuo Qiancd19c462020-01-16 20:51:11 -08002772 /**
2773 * Make sure the caller is system.
2774 *
2775 * @throws SecurityException if the caller is not system.
2776 */
2777 private void enforceSystemCaller() {
2778 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
2779 throw new SecurityException("Caller must be system");
2780 }
2781 }
2782
Shuo Qian3b6ee772019-11-13 17:43:31 -08002783 private void enforceActiveEmergencySessionPermission() {
2784 mApp.enforceCallingOrSelfPermission(
2785 android.Manifest.permission.READ_ACTIVE_EMERGENCY_SESSION, null);
2786 }
2787
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002788 /**
2789 * Make sure the caller has the CALL_PHONE permission.
2790 *
2791 * @throws SecurityException if the caller does not have the required permission
2792 */
2793 private void enforceCallPermission() {
2794 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
2795 }
2796
paulhu5a773602019-08-23 19:17:33 +08002797 private void enforceSettingsPermission() {
2798 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_SETTINGS, null);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002799 }
2800
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002801 private String createTelUrl(String number) {
2802 if (TextUtils.isEmpty(number)) {
2803 return null;
2804 }
2805
Jake Hambye994d462014-02-03 13:10:13 -08002806 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002807 }
2808
Ihab Awadf9e92732013-12-05 18:02:52 -08002809 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002810 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
2811 }
2812
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002813 private static void logv(String msg) {
2814 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
2815 }
2816
Ihab Awadf9e92732013-12-05 18:02:52 -08002817 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002818 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
2819 }
2820
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002821 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002822 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002823 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002824 }
2825
Sanket Padawe356d7632015-06-22 14:03:32 -07002826 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002827 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002828 final long identity = Binder.clearCallingIdentity();
2829 try {
2830 final Phone phone = PhoneFactory.getPhone(slotIndex);
2831 if (phone == null) {
2832 return PhoneConstants.PHONE_TYPE_NONE;
2833 } else {
2834 return phone.getPhoneType();
2835 }
2836 } finally {
2837 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002838 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002839 }
2840
2841 /**
2842 * Returns the CDMA ERI icon index to display
2843 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002844 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002845 public int getCdmaEriIconIndex(String callingPackage, String callingFeatureId) {
2846 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage,
2847 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07002848 }
2849
Sanket Padawe356d7632015-06-22 14:03:32 -07002850 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002851 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage,
2852 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002853 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002854 mApp, subId, callingPackage, callingFeatureId,
2855 "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002856 return -1;
2857 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002858
2859 final long identity = Binder.clearCallingIdentity();
2860 try {
2861 final Phone phone = getPhone(subId);
2862 if (phone != null) {
2863 return phone.getCdmaEriIconIndex();
2864 } else {
2865 return -1;
2866 }
2867 } finally {
2868 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002869 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002870 }
2871
2872 /**
2873 * Returns the CDMA ERI icon mode,
2874 * 0 - ON
2875 * 1 - FLASHING
2876 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002877 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002878 public int getCdmaEriIconMode(String callingPackage, String callingFeatureId) {
2879 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage,
2880 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07002881 }
2882
Sanket Padawe356d7632015-06-22 14:03:32 -07002883 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002884 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage,
2885 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002886 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002887 mApp, subId, callingPackage, callingFeatureId,
2888 "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002889 return -1;
2890 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002891
2892 final long identity = Binder.clearCallingIdentity();
2893 try {
2894 final Phone phone = getPhone(subId);
2895 if (phone != null) {
2896 return phone.getCdmaEriIconMode();
2897 } else {
2898 return -1;
2899 }
2900 } finally {
2901 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002902 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002903 }
2904
2905 /**
2906 * Returns the CDMA ERI text,
2907 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002908 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002909 public String getCdmaEriText(String callingPackage, String callingFeatureId) {
2910 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage,
2911 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07002912 }
2913
Sanket Padawe356d7632015-06-22 14:03:32 -07002914 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002915 public String getCdmaEriTextForSubscriber(int subId, String callingPackage,
2916 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002917 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07002918 mApp, subId, callingPackage, callingFeatureId,
2919 "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002920 return null;
2921 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002922
2923 final long identity = Binder.clearCallingIdentity();
2924 try {
2925 final Phone phone = getPhone(subId);
2926 if (phone != null) {
2927 return phone.getCdmaEriText();
2928 } else {
2929 return null;
2930 }
2931 } finally {
2932 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002933 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002934 }
2935
2936 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002937 * Returns the CDMA MDN.
2938 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002939 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002940 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002941 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2942 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002943
2944 final long identity = Binder.clearCallingIdentity();
2945 try {
2946 final Phone phone = getPhone(subId);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002947 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002948 return phone.getLine1Number();
2949 } else {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08002950 loge("getCdmaMdn: no phone found. Invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002951 return null;
2952 }
2953 } finally {
2954 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002955 }
2956 }
2957
2958 /**
2959 * Returns the CDMA MIN.
2960 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002961 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002962 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002963 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2964 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002965
2966 final long identity = Binder.clearCallingIdentity();
2967 try {
2968 final Phone phone = getPhone(subId);
2969 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2970 return phone.getCdmaMin();
2971 } else {
2972 return null;
2973 }
2974 } finally {
2975 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002976 }
2977 }
2978
Hall Liud892bec2018-11-30 14:51:45 -08002979 @Override
2980 public void requestNumberVerification(PhoneNumberRange range, long timeoutMillis,
2981 INumberVerificationCallback callback, String callingPackage) {
2982 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE)
2983 != PERMISSION_GRANTED) {
2984 throw new SecurityException("Caller must hold the MODIFY_PHONE_STATE permission");
2985 }
2986 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2987
2988 String authorizedPackage = NumberVerificationManager.getAuthorizedPackage(mApp);
2989 if (!TextUtils.equals(callingPackage, authorizedPackage)) {
2990 throw new SecurityException("Calling package must be configured in the device config");
2991 }
2992
2993 if (range == null) {
2994 throw new NullPointerException("Range must be non-null");
2995 }
2996
2997 timeoutMillis = Math.min(timeoutMillis,
Hall Liubd069e32019-02-28 18:56:30 -08002998 TelephonyManager.getMaxNumberVerificationTimeoutMillis());
Hall Liud892bec2018-11-30 14:51:45 -08002999
3000 NumberVerificationManager.getInstance().requestVerification(range, callback, timeoutMillis);
3001 }
3002
Junda Liuca05d5d2014-08-14 22:36:34 -07003003 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003004 * Returns true if CDMA provisioning needs to run.
3005 */
3006 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003007 final long identity = Binder.clearCallingIdentity();
3008 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003009 return getDefaultPhone().needsOtaServiceProvisioning();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003010 } finally {
3011 Binder.restoreCallingIdentity(identity);
3012 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003013 }
3014
3015 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003016 * Sets the voice mail number of a given subId.
3017 */
3018 @Override
3019 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003020 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3021 mApp, subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003022
3023 final long identity = Binder.clearCallingIdentity();
3024 try {
3025 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
3026 new Pair<String, String>(alphaTag, number), new Integer(subId));
3027 return success;
3028 } finally {
3029 Binder.restoreCallingIdentity(identity);
3030 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003031 }
3032
Ta-wei Yen87c49842016-05-13 21:19:52 -07003033 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003034 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
3035 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003036 TelecomManager tm = mApp.getSystemService(TelecomManager.class);
3037 String systemDialer = tm.getSystemDialerPackage();
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003038 if (!TextUtils.equals(callingPackage, systemDialer)) {
3039 throw new SecurityException("caller must be system dialer");
3040 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003041
3042 final long identity = Binder.clearCallingIdentity();
3043 try {
3044 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
3045 if (phoneAccountHandle == null) {
3046 return null;
3047 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003048 return VisualVoicemailSettingsUtil.dump(mApp, phoneAccountHandle);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003049 } finally {
3050 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003051 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07003052 }
3053
3054 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003055 public String getVisualVoicemailPackageName(String callingPackage, String callingFeatureId,
3056 int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003057 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08003058 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003059 mApp, subId, callingPackage, callingFeatureId,
3060 "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08003061 return null;
3062 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003063
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003064 final long identity = Binder.clearCallingIdentity();
3065 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003066 return RemoteVvmTaskManager.getRemotePackage(mApp, subId).getPackageName();
Jeff Davidsona8e4e242018-03-15 17:16:18 -07003067 } finally {
3068 Binder.restoreCallingIdentity(identity);
3069 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08003070 }
3071
3072 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003073 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
3074 VisualVoicemailSmsFilterSettings settings) {
3075 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003076
3077 final long identity = Binder.clearCallingIdentity();
3078 try {
3079 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003080 mApp, callingPackage, subId, settings);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003081 } finally {
3082 Binder.restoreCallingIdentity(identity);
3083 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003084 }
3085
3086 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003087 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
3088 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003089
3090 final long identity = Binder.clearCallingIdentity();
3091 try {
3092 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003093 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003094 } finally {
3095 Binder.restoreCallingIdentity(identity);
3096 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003097 }
3098
3099 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07003100 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
3101 String callingPackage, int subId) {
3102 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003103
3104 final long identity = Binder.clearCallingIdentity();
3105 try {
3106 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003107 mApp, callingPackage, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003108 } finally {
3109 Binder.restoreCallingIdentity(identity);
3110 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07003111 }
3112
3113 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003114 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003115 enforceReadPrivilegedPermission("getActiveVisualVoicemailSmsFilterSettings");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003116
3117 final long identity = Binder.clearCallingIdentity();
3118 try {
3119 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003120 mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003121 } finally {
3122 Binder.restoreCallingIdentity(identity);
3123 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003124 }
3125
3126 @Override
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003127 public void sendVisualVoicemailSmsForSubscriber(String callingPackage,
3128 String callingAttributionTag, int subId, String number, int port, String text,
3129 PendingIntent sentIntent) {
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003130 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003131 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003132 enforceSendSmsPermission();
Amit Mahajandccb3f12019-05-13 13:48:32 -07003133 SmsController smsController = PhoneFactory.getSmsController();
Philip P. Moltmann2f6f8ce2020-03-18 18:17:02 -07003134 smsController.sendVisualVoicemailSmsForSubscriber(callingPackage, callingAttributionTag,
3135 subId, number, port, text, sentIntent);
Ta-wei Yen87c49842016-05-13 21:19:52 -07003136 }
Amit Mahajandccb3f12019-05-13 13:48:32 -07003137
Shishir Agrawal76d5da92014-11-09 16:17:25 -08003138 /**
fionaxu0152e512016-11-14 13:36:14 -08003139 * Sets the voice activation state of a given subId.
3140 */
3141 @Override
3142 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003143 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3144 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003145
3146 final long identity = Binder.clearCallingIdentity();
3147 try {
3148 final Phone phone = getPhone(subId);
3149 if (phone != null) {
3150 phone.setVoiceActivationState(activationState);
3151 } else {
3152 loge("setVoiceActivationState fails with invalid subId: " + subId);
3153 }
3154 } finally {
3155 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003156 }
3157 }
3158
3159 /**
3160 * Sets the data activation state of a given subId.
3161 */
3162 @Override
3163 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003164 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3165 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003166
3167 final long identity = Binder.clearCallingIdentity();
3168 try {
3169 final Phone phone = getPhone(subId);
3170 if (phone != null) {
3171 phone.setDataActivationState(activationState);
3172 } else {
Taesu Leef8fbed92019-10-07 18:47:02 +09003173 loge("setDataActivationState fails with invalid subId: " + subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003174 }
3175 } finally {
3176 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003177 }
3178 }
3179
3180 /**
3181 * Returns the voice activation state of a given subId.
3182 */
3183 @Override
3184 public int getVoiceActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003185 enforceReadPrivilegedPermission("getVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003186
fionaxu0152e512016-11-14 13:36:14 -08003187 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003188 final long identity = Binder.clearCallingIdentity();
3189 try {
3190 if (phone != null) {
3191 return phone.getVoiceActivationState();
3192 } else {
3193 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3194 }
3195 } finally {
3196 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003197 }
3198 }
3199
3200 /**
3201 * Returns the data activation state of a given subId.
3202 */
3203 @Override
3204 public int getDataActivationState(int subId, String callingPackage) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003205 enforceReadPrivilegedPermission("getDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003206
fionaxu0152e512016-11-14 13:36:14 -08003207 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003208 final long identity = Binder.clearCallingIdentity();
3209 try {
3210 if (phone != null) {
3211 return phone.getDataActivationState();
3212 } else {
3213 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
3214 }
3215 } finally {
3216 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08003217 }
3218 }
3219
3220 /**
Wink Saville36469e72014-06-11 15:17:00 -07003221 * Returns the unread count of voicemails for a subId
3222 */
Sanket Padawe356d7632015-06-22 14:03:32 -07003223 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003224 public int getVoiceMessageCountForSubscriber(int subId, String callingPackage,
3225 String callingFeatureId) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003226 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07003227 mApp, subId, callingPackage, callingFeatureId,
3228 "getVoiceMessageCountForSubscriber")) {
Brad Ebingerf7664ba2018-11-29 12:43:38 -08003229 return 0;
3230 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003231 final long identity = Binder.clearCallingIdentity();
3232 try {
3233 final Phone phone = getPhone(subId);
3234 if (phone != null) {
3235 return phone.getVoiceMessageCount();
3236 } else {
3237 return 0;
3238 }
3239 } finally {
3240 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003241 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003242 }
3243
3244 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08003245 * returns true, if the device is in a state where both voice and data
3246 * are supported simultaneously. This can change based on location or network condition.
3247 */
3248 @Override
3249 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003250 final long identity = Binder.clearCallingIdentity();
3251 try {
3252 final Phone phone = getPhone(subId);
3253 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
3254 } finally {
3255 Binder.restoreCallingIdentity(identity);
3256 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08003257 }
3258
3259 /**
fionaxu235cc5e2017-03-06 22:25:57 -08003260 * Send the dialer code if called from the current default dialer or the caller has
3261 * carrier privilege.
3262 * @param inputCode The dialer code to send
3263 */
3264 @Override
3265 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003266 final Phone defaultPhone = getDefaultPhone();
fionaxu235cc5e2017-03-06 22:25:57 -08003267 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07003268 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
3269 String defaultDialer = tm.getDefaultDialerPackage();
fionaxu235cc5e2017-03-06 22:25:57 -08003270 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08003271 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08003272 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08003273 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003274
3275 final long identity = Binder.clearCallingIdentity();
3276 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003277 defaultPhone.sendDialerSpecialCode(inputCode);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003278 } finally {
3279 Binder.restoreCallingIdentity(identity);
3280 }
fionaxu235cc5e2017-03-06 22:25:57 -08003281 }
3282
Pengquan Menga1bb6272018-09-06 09:59:22 -07003283 @Override
3284 public int getNetworkSelectionMode(int subId) {
shilufc958392020-01-20 11:36:01 -08003285 TelephonyPermissions
3286 .enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3287 mApp, subId, "getNetworkSelectionMode");
3288 final long identity = Binder.clearCallingIdentity();
3289 try {
3290 if (!isActiveSubscription(subId)) {
3291 return TelephonyManager.NETWORK_SELECTION_MODE_UNKNOWN;
3292 }
3293 return (int) sendRequest(CMD_GET_NETWORK_SELECTION_MODE, null /* argument */, subId);
3294 } finally {
3295 Binder.restoreCallingIdentity(identity);
Pengquan Menge92a50d2018-09-21 15:54:48 -07003296 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07003297 }
3298
Brad Ebinger35c841c2018-10-01 10:40:55 -07003299 @Override
Brad Ebingerb2b65522019-03-15 13:48:47 -07003300 public boolean isInEmergencySmsMode() {
3301 enforceReadPrivilegedPermission("isInEmergencySmsMode");
3302 final long identity = Binder.clearCallingIdentity();
3303 try {
3304 for (Phone phone : PhoneFactory.getPhones()) {
3305 if (phone.isInEmergencySmsMode()) {
3306 return true;
3307 }
3308 }
3309 } finally {
3310 Binder.restoreCallingIdentity(identity);
3311 }
3312 return false;
3313 }
3314
shilu366312e2019-12-17 09:28:10 -08003315 /**
3316 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3317 * @param subId The subscription to use to check the configuration.
3318 * @param c The callback that will be used to send the result.
3319 */
Brad Ebingerb2b65522019-03-15 13:48:47 -07003320 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003321 public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback c)
3322 throws RemoteException {
Rambo Wang37f9c242020-02-10 14:45:28 -08003323 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3324 mApp, subId, "registerImsRegistrationCallback");
shilu366312e2019-12-17 09:28:10 -08003325
Brad Ebingerbc7dd582019-10-17 17:03:22 -07003326 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3327 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3328 "IMS not available on device.");
3329 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003330 final long token = Binder.clearCallingIdentity();
3331 try {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003332 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003333 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003334 .addRegistrationCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003335 } catch (ImsException e) {
3336 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003337 } finally {
3338 Binder.restoreCallingIdentity(token);
3339 }
3340 }
3341
shilu366312e2019-12-17 09:28:10 -08003342 /**
3343 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3344 * @param subId The subscription to use to check the configuration.
3345 * @param c The callback that will be used to send the result.
3346 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003347 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003348 public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback c) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003349 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3350 mApp, subId, "unregisterImsRegistrationCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003351 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3352 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3353 }
Meng Wangafbc5852019-09-19 17:37:13 -07003354 final long token = Binder.clearCallingIdentity();
3355 try {
Meng Wang8ea57e32020-06-25 11:03:56 -07003356 // TODO(b/159910732): Remove ImsManager dependence and query through ImsPhone directly.
Meng Wangafbc5852019-09-19 17:37:13 -07003357 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
3358 .removeRegistrationCallbackForSubscription(c, subId);
3359 } catch (ImsException e) {
3360 Log.i(LOG_TAG, "unregisterImsRegistrationCallback: " + subId
3361 + "is inactive, ignoring unregister.");
3362 // If the subscription is no longer active, just return, since the callback
3363 // will already have been removed internally.
3364 } finally {
3365 Binder.restoreCallingIdentity(token);
3366 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003367 }
3368
Brad Ebingera34a6c22019-10-22 17:36:18 -07003369 /**
3370 * Get the IMS service registration state for the MmTelFeature associated with this sub id.
3371 */
3372 @Override
3373 public void getImsMmTelRegistrationState(int subId, IIntegerConsumer consumer) {
3374 enforceReadPrivilegedPermission("getImsMmTelRegistrationState");
3375 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3376 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3377 "IMS not available on device.");
3378 }
3379 final long token = Binder.clearCallingIdentity();
3380 try {
3381 Phone phone = getPhone(subId);
3382 if (phone == null) {
3383 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3384 + subId + "'");
3385 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3386 }
3387 phone.getImsRegistrationState(regState -> {
3388 try {
3389 consumer.accept((regState == null)
3390 ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState);
3391 } catch (RemoteException e) {
3392 // Ignore if the remote process is no longer available to call back.
3393 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3394 }
3395 });
3396 } finally {
3397 Binder.restoreCallingIdentity(token);
3398 }
3399 }
3400
3401 /**
3402 * Get the transport type for the IMS service registration state.
3403 */
3404 @Override
3405 public void getImsMmTelRegistrationTransportType(int subId, IIntegerConsumer consumer) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003406 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3407 mApp, subId, "getImsMmTelRegistrationTransportType");
Brad Ebingera34a6c22019-10-22 17:36:18 -07003408 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3409 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3410 "IMS not available on device.");
3411 }
3412 final long token = Binder.clearCallingIdentity();
3413 try {
3414 Phone phone = getPhone(subId);
3415 if (phone == null) {
3416 Log.w(LOG_TAG, "getImsMmTelRegistrationState: called with an invalid subscription '"
3417 + subId + "'");
3418 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3419 }
3420 phone.getImsRegistrationTech(regTech -> {
3421 // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager
3422 int regTechConverted = (regTech == null)
3423 ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech;
3424 regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get(
3425 regTechConverted);
3426 try {
3427 consumer.accept(regTechConverted);
3428 } catch (RemoteException e) {
3429 // Ignore if the remote process is no longer available to call back.
3430 Log.w(LOG_TAG, "getImsMmTelRegistrationState: callback not available.");
3431 }
3432 });
3433 } finally {
3434 Binder.restoreCallingIdentity(token);
3435 }
3436 }
3437
shilu366312e2019-12-17 09:28:10 -08003438 /**
3439 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3440 * @param subId The subscription to use to check the configuration.
3441 * @param c The callback that will be used to send the result.
3442 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003443 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003444 public void registerMmTelCapabilityCallback(int subId, IImsCapabilityCallback c)
3445 throws RemoteException {
Rambo Wang37f9c242020-02-10 14:45:28 -08003446 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3447 mApp, subId, "registerMmTelCapabilityCallback");
Brad Ebingerbc7dd582019-10-17 17:03:22 -07003448 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3449 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3450 "IMS not available on device.");
3451 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003452 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3453 final long token = Binder.clearCallingIdentity();
3454 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003455 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger35c841c2018-10-01 10:40:55 -07003456 .addCapabilitiesCallbackForSubscription(c, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003457 } catch (ImsException e) {
3458 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003459 } finally {
3460 Binder.restoreCallingIdentity(token);
3461 }
3462 }
3463
shilu366312e2019-12-17 09:28:10 -08003464 /**
3465 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3466 * @param subId The subscription to use to check the configuration.
3467 * @param c The callback that will be used to send the result.
3468 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003469 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003470 public void unregisterMmTelCapabilityCallback(int subId, IImsCapabilityCallback c) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003471 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3472 mApp, subId, "unregisterMmTelCapabilityCallback");
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003473 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3474 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3475 }
Meng Wangafbc5852019-09-19 17:37:13 -07003476
3477 final long token = Binder.clearCallingIdentity();
3478 try {
Meng Wang8ea57e32020-06-25 11:03:56 -07003479 // TODO(b/159910732): Remove ImsManager dependence and query through ImsPhone directly.
Meng Wangafbc5852019-09-19 17:37:13 -07003480 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003481 .removeCapabilitiesCallbackForSubscription(c, subId);
Meng Wangafbc5852019-09-19 17:37:13 -07003482 } catch (ImsException e) {
3483 Log.i(LOG_TAG, "unregisterMmTelCapabilityCallback: " + subId
3484 + "is inactive, ignoring unregister.");
3485 // If the subscription is no longer active, just return, since the callback
3486 // will already have been removed internally.
3487 } finally {
3488 Binder.restoreCallingIdentity(token);
3489 }
Brad Ebinger35c841c2018-10-01 10:40:55 -07003490 }
3491
3492 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003493 public boolean isCapable(int subId, int capability, int regTech) {
3494 enforceReadPrivilegedPermission("isCapable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003495 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3496 final long token = Binder.clearCallingIdentity();
3497 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003498 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003499 getSlotIndexOrException(subId)).queryMmTelCapability(capability, regTech);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003500 } catch (com.android.ims.ImsException e) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003501 Log.w(LOG_TAG, "IMS isCapable - service unavailable: " + e.getMessage());
3502 return false;
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003503 } catch (ImsException e) {
Brad Ebinger6b5ac222019-02-04 14:36:52 -08003504 Log.i(LOG_TAG, "isCapable: " + subId + " is inactive, returning false.");
3505 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07003506 } finally {
3507 Binder.restoreCallingIdentity(token);
3508 }
3509 }
3510
3511 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003512 public boolean isAvailable(int subId, int capability, int regTech) {
3513 enforceReadPrivilegedPermission("isAvailable");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003514 final long token = Binder.clearCallingIdentity();
3515 try {
3516 Phone phone = getPhone(subId);
3517 if (phone == null) return false;
3518 return phone.isImsCapabilityAvailable(capability, regTech);
Daniel Bright5e40e4e2020-03-11 16:35:39 -07003519 } catch (com.android.ims.ImsException e) {
3520 Log.w(LOG_TAG, "IMS isAvailable - service unavailable: " + e.getMessage());
3521 return false;
Brad Ebinger35c841c2018-10-01 10:40:55 -07003522 } finally {
3523 Binder.restoreCallingIdentity(token);
3524 }
3525 }
3526
Brad Ebingerbc7dd582019-10-17 17:03:22 -07003527 /**
3528 * Determines if the MmTel feature capability is supported by the carrier configuration for this
3529 * subscription.
3530 * @param subId The subscription to use to check the configuration.
3531 * @param callback The callback that will be used to send the result.
3532 * @param capability The MmTelFeature capability that will be used to send the result.
3533 * @param transportType The transport type of the MmTelFeature capability.
3534 */
3535 @Override
3536 public void isMmTelCapabilitySupported(int subId, IIntegerConsumer callback, int capability,
3537 int transportType) {
3538 enforceReadPrivilegedPermission("isMmTelCapabilitySupported");
3539 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
3540 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3541 "IMS not available on device.");
3542 }
3543 final long token = Binder.clearCallingIdentity();
3544 try {
3545 int slotId = getSlotIndex(subId);
3546 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
3547 Log.w(LOG_TAG, "isMmTelCapabilitySupported: called with an inactive subscription '"
3548 + subId + "'");
3549 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
3550 }
3551 ImsManager.getInstance(mApp, slotId).isSupported(capability,
3552 transportType, aBoolean -> {
3553 try {
3554 callback.accept((aBoolean == null) ? 0 : (aBoolean ? 1 : 0));
3555 } catch (RemoteException e) {
3556 Log.w(LOG_TAG, "isMmTelCapabilitySupported: remote caller is not "
3557 + "running. Ignore");
3558 }
3559 });
3560 } finally {
3561 Binder.restoreCallingIdentity(token);
3562 }
3563 }
3564
shilu366312e2019-12-17 09:28:10 -08003565 /**
3566 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3567 * @param subId The subscription to use to check the configuration.
3568 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003569 @Override
3570 public boolean isAdvancedCallingSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003571 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3572 mApp, subId, "isAdvancedCallingSettingEnabled");
shilu366312e2019-12-17 09:28:10 -08003573
Brad Ebinger35c841c2018-10-01 10:40:55 -07003574 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3575 final long token = Binder.clearCallingIdentity();
3576 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003577 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003578 getSlotIndexOrException(subId)).isEnhanced4gLteModeSettingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003579 } catch (ImsException e) {
3580 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003581 } finally {
3582 Binder.restoreCallingIdentity(token);
3583 }
3584 }
3585
3586 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003587 public void setAdvancedCallingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003588 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003589 "setAdvancedCallingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003590 final long identity = Binder.clearCallingIdentity();
3591 try {
3592 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003593 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003594 getSlotIndexOrException(subId)).setEnhanced4gLteModeSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003595 } catch (ImsException e) {
3596 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003597 } finally {
3598 Binder.restoreCallingIdentity(identity);
3599 }
3600 }
3601
shilu366312e2019-12-17 09:28:10 -08003602 /**
3603 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3604 * @param subId The subscription to use to check the configuration.
3605 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003606 @Override
Brad Ebinger9878b0b2018-11-08 17:43:22 -08003607 public boolean isVtSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003608 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3609 mApp, subId, "isVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003610 final long identity = Binder.clearCallingIdentity();
3611 try {
3612 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003613 return ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).isVtEnabledByUser();
3614 } catch (ImsException e) {
3615 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003616 } finally {
3617 Binder.restoreCallingIdentity(identity);
3618 }
3619 }
3620
3621 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003622 public void setVtSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003623 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003624 "setVtSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003625 final long identity = Binder.clearCallingIdentity();
3626 try {
3627 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003628 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setVtSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003629 } catch (ImsException e) {
3630 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003631 } finally {
3632 Binder.restoreCallingIdentity(identity);
3633 }
3634 }
3635
shilu366312e2019-12-17 09:28:10 -08003636 /**
3637 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3638 * @param subId The subscription to use to check the configuration.
3639 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003640 @Override
3641 public boolean isVoWiFiSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003642 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3643 mApp, subId, "isVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003644 final long identity = Binder.clearCallingIdentity();
3645 try {
3646 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003647 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003648 getSlotIndexOrException(subId)).isWfcEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003649 } catch (ImsException e) {
3650 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003651 } finally {
3652 Binder.restoreCallingIdentity(identity);
3653 }
3654 }
3655
3656 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003657 public void setVoWiFiSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003658 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003659 "setVoWiFiSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003660 final long identity = Binder.clearCallingIdentity();
3661 try {
3662 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003663 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setWfcSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003664 } catch (ImsException e) {
3665 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003666 } finally {
3667 Binder.restoreCallingIdentity(identity);
3668 }
3669 }
3670
shilu366312e2019-12-17 09:28:10 -08003671 /**
3672 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3673 * @param subId The subscription to use to check the configuration.
3674 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003675 @Override
3676 public boolean isVoWiFiRoamingSettingEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003677 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3678 mApp, subId, "isVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003679 final long identity = Binder.clearCallingIdentity();
3680 try {
3681 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003682 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003683 getSlotIndexOrException(subId)).isWfcRoamingEnabledByUser();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003684 } catch (ImsException e) {
3685 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003686 } finally {
3687 Binder.restoreCallingIdentity(identity);
3688 }
3689 }
3690
3691 @Override
Brad Ebinger1c162042019-02-21 14:49:10 -08003692 public void setVoWiFiRoamingSettingEnabled(int subId, boolean isEnabled) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07003693 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
Brad Ebinger1c162042019-02-21 14:49:10 -08003694 "setVoWiFiRoamingSettingEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003695 final long identity = Binder.clearCallingIdentity();
3696 try {
3697 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003698 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003699 getSlotIndexOrException(subId)).setWfcRoamingSetting(isEnabled);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003700 } catch (ImsException e) {
3701 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003702 } finally {
3703 Binder.restoreCallingIdentity(identity);
3704 }
3705 }
3706
3707 @Override
3708 public void setVoWiFiNonPersistent(int subId, boolean isCapable, int mode) {
3709 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3710 "setVoWiFiNonPersistent");
3711 final long identity = Binder.clearCallingIdentity();
3712 try {
3713 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003714 ImsManager.getInstance(mApp,
Brad Ebinger2d29c012019-05-07 18:33:46 -07003715 getSlotIndexOrException(subId)).setWfcNonPersistent(isCapable, mode);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003716 } catch (ImsException e) {
3717 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003718 } finally {
3719 Binder.restoreCallingIdentity(identity);
3720 }
3721 }
3722
shilu366312e2019-12-17 09:28:10 -08003723 /**
3724 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3725 * @param subId The subscription to use to check the configuration.
3726 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003727 @Override
3728 public int getVoWiFiModeSetting(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003729 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3730 mApp, subId, "getVoWiFiModeSetting");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003731 final long identity = Binder.clearCallingIdentity();
3732 try {
3733 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003734 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003735 getSlotIndexOrException(subId)).getWfcMode(false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003736 } catch (ImsException e) {
3737 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003738 } finally {
3739 Binder.restoreCallingIdentity(identity);
3740 }
3741 }
3742
3743 @Override
3744 public void setVoWiFiModeSetting(int subId, int mode) {
3745 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3746 "setVoWiFiModeSetting");
3747 final long identity = Binder.clearCallingIdentity();
3748 try {
3749 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003750 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003751 getSlotIndexOrException(subId)).setWfcMode(mode, false /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003752 } catch (ImsException e) {
3753 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003754 } finally {
3755 Binder.restoreCallingIdentity(identity);
3756 }
3757 }
3758
3759 @Override
3760 public int getVoWiFiRoamingModeSetting(int subId) {
3761 enforceReadPrivilegedPermission("getVoWiFiRoamingModeSetting");
3762 final long identity = Binder.clearCallingIdentity();
3763 try {
3764 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003765 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003766 getSlotIndexOrException(subId)).getWfcMode(true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003767 } catch (ImsException e) {
3768 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003769 } finally {
3770 Binder.restoreCallingIdentity(identity);
3771 }
3772 }
3773
3774 @Override
3775 public void setVoWiFiRoamingModeSetting(int subId, int mode) {
3776 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3777 "setVoWiFiRoamingModeSetting");
3778 final long identity = Binder.clearCallingIdentity();
3779 try {
3780 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003781 ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003782 getSlotIndexOrException(subId)).setWfcMode(mode, true /*isRoaming*/);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003783 } catch (ImsException e) {
3784 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003785 } finally {
3786 Binder.restoreCallingIdentity(identity);
3787 }
3788 }
3789
3790 @Override
3791 public void setRttCapabilitySetting(int subId, boolean isEnabled) {
3792 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3793 "setRttCapabilityEnabled");
3794 final long identity = Binder.clearCallingIdentity();
3795 try {
3796 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003797 ImsManager.getInstance(mApp, getSlotIndexOrException(subId)).setRttEnabled(isEnabled);
3798 } catch (ImsException e) {
3799 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003800 } finally {
3801 Binder.restoreCallingIdentity(identity);
3802 }
3803 }
3804
shilu366312e2019-12-17 09:28:10 -08003805 /**
3806 * Requires carrier privileges or READ_PRECISE_PHONE_STATE permission.
3807 * @param subId The subscription to use to check the configuration.
3808 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07003809 @Override
3810 public boolean isTtyOverVolteEnabled(int subId) {
Rambo Wang37f9c242020-02-10 14:45:28 -08003811 TelephonyPermissions.enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
3812 mApp, subId, "isTtyOverVolteEnabled");
Brad Ebinger35c841c2018-10-01 10:40:55 -07003813 final long identity = Binder.clearCallingIdentity();
3814 try {
3815 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003816 return ImsManager.getInstance(mApp,
Brad Ebinger35c841c2018-10-01 10:40:55 -07003817 getSlotIndexOrException(subId)).isTtyOnVoLteCapable();
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003818 } catch (ImsException e) {
3819 throw new ServiceSpecificException(e.getCode());
Brad Ebinger35c841c2018-10-01 10:40:55 -07003820 } finally {
3821 Binder.restoreCallingIdentity(identity);
3822 }
3823 }
3824
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003825 @Override
3826 public void registerImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3827 enforceReadPrivilegedPermission("registerImsProvisioningChangedCallback");
3828 final long identity = Binder.clearCallingIdentity();
3829 try {
Brad Ebingerd0331732020-01-16 11:21:18 -08003830 if (!isImsAvailableOnDevice()) {
3831 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
3832 "IMS not available on device.");
Peter Wang44b186e2020-01-13 23:33:09 -08003833 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003834 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003835 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003836 .addProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003837 } catch (ImsException e) {
3838 throw new ServiceSpecificException(e.getCode());
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003839 } finally {
3840 Binder.restoreCallingIdentity(identity);
3841 }
3842 }
3843
3844 @Override
3845 public void unregisterImsProvisioningChangedCallback(int subId, IImsConfigCallback callback) {
3846 enforceReadPrivilegedPermission("unregisterImsProvisioningChangedCallback");
3847 final long identity = Binder.clearCallingIdentity();
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003848 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
3849 throw new IllegalArgumentException("Invalid Subscription ID: " + subId);
3850 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003851 try {
3852 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08003853 ImsManager.getInstance(mApp, getSlotIndexOrException(subId))
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003854 .removeProvisioningCallbackForSubscription(callback, subId);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003855 } catch (ImsException e) {
Brad Ebinger4ae57f92019-01-09 16:51:30 -08003856 Log.i(LOG_TAG, "unregisterImsProvisioningChangedCallback: " + subId
3857 + "is inactive, ignoring unregister.");
3858 // If the subscription is no longer active, just return, since the callback will already
3859 // have been removed internally.
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003860 } finally {
3861 Binder.restoreCallingIdentity(identity);
3862 }
3863 }
3864
allenwtsu99c623b2020-01-03 18:24:23 +08003865
3866 private void checkModifyPhoneStatePermission(int subId, String message) {
3867 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
3868 message);
3869 }
3870
3871 private boolean isImsProvisioningRequired(int subId, int capability,
3872 boolean isMmtelCapability) {
3873 Phone phone = getPhone(subId);
3874 if (phone == null) {
3875 loge("phone instance null for subid " + subId);
3876 return false;
3877 }
3878 if (isMmtelCapability) {
3879 if (!doesImsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3880 return false;
3881 }
3882 } else {
3883 if (!doesRcsCapabilityRequireProvisioning(phone.getContext(), subId, capability)) {
3884 return false;
3885 }
3886 }
3887 return true;
3888 }
3889
3890 @Override
3891 public void setRcsProvisioningStatusForCapability(int subId, int capability,
3892 boolean isProvisioned) {
3893 checkModifyPhoneStatePermission(subId, "setRcsProvisioningStatusForCapability");
3894
3895 final long identity = Binder.clearCallingIdentity();
3896 try {
3897 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3898 if (!isImsProvisioningRequired(subId, capability, false)) {
3899 return;
3900 }
3901
3902 // this capability requires provisioning, route to the correct API.
3903 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3904 switch (capability) {
3905 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
3906 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
3907 ims.setEabProvisioned(isProvisioned);
3908 break;
3909 default: {
3910 throw new IllegalArgumentException("Tried to set provisioning for "
3911 + "rcs capability '" + capability + "', which does not require "
3912 + "provisioning.");
3913 }
3914 }
3915 } finally {
3916 Binder.restoreCallingIdentity(identity);
3917 }
3918
3919 }
3920
3921
3922 @Override
3923 public boolean getRcsProvisioningStatusForCapability(int subId, int capability) {
3924 enforceReadPrivilegedPermission("getRcsProvisioningStatusForCapability");
3925 final long identity = Binder.clearCallingIdentity();
3926 try {
3927 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
3928 if (!isImsProvisioningRequired(subId, capability, false)) {
3929 return true;
3930 }
3931
3932 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3933 switch (capability) {
3934 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE:
3935 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
3936 return ims.isEabProvisionedOnDevice();
3937
3938 default: {
3939 throw new IllegalArgumentException("Tried to get rcs provisioning for "
3940 + "capability '" + capability + "', which does not require "
3941 + "provisioning.");
3942 }
3943 }
3944
3945 } finally {
3946 Binder.restoreCallingIdentity(identity);
3947 }
3948 }
3949
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07003950 @Override
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003951 public void setImsProvisioningStatusForCapability(int subId, int capability, int tech,
3952 boolean isProvisioned) {
3953 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
3954 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3955 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
3956 }
allenwtsu99c623b2020-01-03 18:24:23 +08003957 checkModifyPhoneStatePermission(subId, "setImsProvisioningStatusForCapability");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003958 final long identity = Binder.clearCallingIdentity();
3959 try {
3960 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08003961 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003962 return;
3963 }
3964
3965 // this capability requires provisioning, route to the correct API.
3966 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
3967 switch (capability) {
3968 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
3969 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
3970 ims.setVolteProvisioned(isProvisioned);
3971 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
3972 ims.setWfcProvisioned(isProvisioned);
3973 }
3974 break;
3975 }
3976 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
3977 // There is currently no difference in VT provisioning type.
3978 ims.setVtProvisioned(isProvisioned);
3979 break;
3980 }
3981 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
3982 // There is no "deprecated" UT provisioning mechanism through ImsConfig, so
3983 // change the capability of the feature instead if needed.
3984 if (isMmTelCapabilityProvisionedInCache(subId, capability, tech)
3985 == isProvisioned) {
3986 // No change in provisioning.
3987 return;
3988 }
3989 cacheMmTelCapabilityProvisioning(subId, capability, tech, isProvisioned);
3990 try {
3991 ims.changeMmTelCapability(capability, tech, isProvisioned);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07003992 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08003993 loge("setImsProvisioningStatusForCapability: couldn't change UT capability"
3994 + ", Exception" + e.getMessage());
3995 }
3996 break;
3997 }
3998 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08003999 throw new IllegalArgumentException("Tried to set provisioning for "
4000 + "MmTel capability '" + capability + "', which does not require "
4001 + "provisioning. ");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004002 }
4003 }
4004
4005 } finally {
4006 Binder.restoreCallingIdentity(identity);
4007 }
4008 }
4009
4010 @Override
4011 public boolean getImsProvisioningStatusForCapability(int subId, int capability, int tech) {
4012 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4013 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4014 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4015 }
4016 enforceReadPrivilegedPermission("getProvisioningStatusForCapability");
4017 final long identity = Binder.clearCallingIdentity();
4018 try {
4019 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
allenwtsu99c623b2020-01-03 18:24:23 +08004020 if (!isImsProvisioningRequired(subId, capability, true)) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004021 return true;
4022 }
4023
4024 ImsManager ims = ImsManager.getInstance(mApp, getSlotIndex(subId));
4025 switch (capability) {
4026 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE: {
4027 if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4028 return ims.isVolteProvisionedOnDevice();
4029 } else if (tech == ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN) {
4030 return ims.isWfcProvisionedOnDevice();
4031 }
4032 // This should never happen, since we are checking tech above to make sure it
4033 // is either LTE or IWLAN.
4034 throw new IllegalArgumentException("Invalid radio technology for voice "
4035 + "capability.");
4036 }
4037 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4038 // There is currently no difference in VT provisioning type.
4039 return ims.isVtProvisionedOnDevice();
4040 }
4041 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4042 // There is no "deprecated" UT provisioning mechanism, so get from shared prefs.
4043 return isMmTelCapabilityProvisionedInCache(subId, capability, tech);
4044 }
4045 default: {
allenwtsu99c623b2020-01-03 18:24:23 +08004046 throw new IllegalArgumentException(
4047 "Tried to get provisioning for MmTel capability '" + capability
4048 + "', which does not require provisioning.");
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004049 }
4050 }
4051
4052 } finally {
4053 Binder.restoreCallingIdentity(identity);
4054 }
4055 }
4056
4057 @Override
4058 public boolean isMmTelCapabilityProvisionedInCache(int subId, int capability, int tech) {
4059 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4060 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4061 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4062 }
4063 enforceReadPrivilegedPermission("isMmTelCapabilityProvisionedInCache");
4064 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
4065 return (provisionedBits & capability) > 0;
4066 }
4067
4068 @Override
4069 public void cacheMmTelCapabilityProvisioning(int subId, int capability, int tech,
4070 boolean isProvisioned) {
4071 if (tech != ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN
4072 && tech != ImsRegistrationImplBase.REGISTRATION_TECH_LTE) {
4073 throw new IllegalArgumentException("Registration technology '" + tech + "' is invalid");
4074 }
4075 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4076 "setProvisioningStatusForCapability");
4077 int provisionedBits = getMmTelCapabilityProvisioningBitfield(subId, tech);
4078 // If the current provisioning status for capability already matches isProvisioned,
4079 // do nothing.
4080 if (((provisionedBits & capability) > 0) == isProvisioned) {
4081 return;
4082 }
4083 if (isProvisioned) {
4084 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits | capability));
4085 } else {
4086 setMmTelCapabilityProvisioningBitfield(subId, tech, (provisionedBits & ~capability));
4087 }
4088 }
4089
4090 /**
4091 * @return the bitfield containing the MmTel provisioning for the provided subscription and
4092 * technology. The bitfield should mirror the bitfield defined by
4093 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}.
4094 */
4095 private int getMmTelCapabilityProvisioningBitfield(int subId, int tech) {
4096 String key = getMmTelProvisioningKey(subId, tech);
4097 // Default is no capabilities are provisioned.
4098 return mTelephonySharedPreferences.getInt(key, 0 /*default*/);
4099 }
4100
4101 /**
4102 * Sets the MmTel capability provisioning bitfield (defined by
4103 * {@link MmTelFeature.MmTelCapabilities.MmTelCapability}) for the subscription and
4104 * technology specified.
4105 *
4106 * Note: This is a synchronous command and should not be called on UI thread.
4107 */
4108 private void setMmTelCapabilityProvisioningBitfield(int subId, int tech, int newField) {
4109 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
4110 String key = getMmTelProvisioningKey(subId, tech);
4111 editor.putInt(key, newField);
4112 editor.commit();
4113 }
4114
4115 private static String getMmTelProvisioningKey(int subId, int tech) {
4116 // resulting key is provision_ims_mmtel_{subId}_{tech}
4117 return PREF_PROVISION_IMS_MMTEL_PREFIX + subId + "_" + tech;
4118 }
4119
4120 /**
4121 * Query CarrierConfig to see if the specified capability requires provisioning for the
4122 * carrier associated with the subscription id.
4123 */
4124 private boolean doesImsCapabilityRequireProvisioning(Context context, int subId,
4125 int capability) {
4126 CarrierConfigManager configManager = new CarrierConfigManager(context);
4127 PersistableBundle c = configManager.getConfigForSubId(subId);
4128 boolean requireUtProvisioning = c.getBoolean(
Brad Ebinger076903f2019-05-13 10:00:22 -07004129 CarrierConfigManager.KEY_CARRIER_SUPPORTS_SS_OVER_UT_BOOL, false)
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004130 && c.getBoolean(CarrierConfigManager.KEY_CARRIER_UT_PROVISIONING_REQUIRED_BOOL,
4131 false);
4132 boolean requireVoiceVtProvisioning = c.getBoolean(
4133 CarrierConfigManager.KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
4134
4135 // First check to make sure that the capability requires provisioning.
4136 switch (capability) {
4137 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VOICE:
4138 // intentional fallthrough
4139 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_VIDEO: {
4140 if (requireVoiceVtProvisioning) {
4141 // Voice and Video requires provisioning
4142 return true;
4143 }
4144 break;
4145 }
4146 case MmTelFeature.MmTelCapabilities.CAPABILITY_TYPE_UT: {
4147 if (requireUtProvisioning) {
4148 // UT requires provisioning
4149 return true;
4150 }
4151 break;
4152 }
4153 }
4154 return false;
4155 }
4156
allenwtsu99c623b2020-01-03 18:24:23 +08004157 private boolean doesRcsCapabilityRequireProvisioning(Context context, int subId,
4158 int capability) {
4159 CarrierConfigManager configManager = new CarrierConfigManager(context);
4160 PersistableBundle c = configManager.getConfigForSubId(subId);
4161
4162 boolean requireRcsProvisioning = c.getBoolean(
4163 CarrierConfigManager.KEY_CARRIER_RCS_PROVISIONING_REQUIRED_BOOL, false);
4164
4165 // First check to make sure that the capability requires provisioning.
4166 switch (capability) {
4167 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_PRESENCE_UCE:
4168 // intentional fallthrough
4169 case RcsFeature.RcsImsCapabilities.CAPABILITY_TYPE_OPTIONS_UCE: {
4170 if (requireRcsProvisioning) {
4171 // OPTION or PRESENCE requires provisioning
4172 return true;
4173 }
4174 break;
4175 }
4176 }
4177 return false;
4178 }
4179
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004180 @Override
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004181 public int getImsProvisioningInt(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004182 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4183 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4184 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004185 enforceReadPrivilegedPermission("getImsProvisioningInt");
4186 final long identity = Binder.clearCallingIdentity();
4187 try {
4188 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004189 int slotId = getSlotIndex(subId);
4190 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4191 Log.w(LOG_TAG, "getImsProvisioningInt: called with an inactive subscription '"
4192 + subId + "' for key:" + key);
4193 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
4194 }
4195 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigInt(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004196 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004197 Log.w(LOG_TAG, "getImsProvisioningInt: ImsService is not available for subscription '"
4198 + subId + "' for key:" + key);
4199 return ImsConfigImplBase.CONFIG_RESULT_UNKNOWN;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004200 } finally {
4201 Binder.restoreCallingIdentity(identity);
4202 }
4203 }
4204
4205 @Override
4206 public String getImsProvisioningString(int subId, int key) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004207 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4208 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4209 }
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004210 enforceReadPrivilegedPermission("getImsProvisioningString");
4211 final long identity = Binder.clearCallingIdentity();
4212 try {
4213 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004214 int slotId = getSlotIndex(subId);
4215 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4216 Log.w(LOG_TAG, "getImsProvisioningString: called for an inactive subscription id '"
4217 + subId + "' for key:" + key);
4218 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_GENERIC;
4219 }
4220 return ImsManager.getInstance(mApp, slotId).getConfigInterface().getConfigString(key);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004221 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004222 Log.w(LOG_TAG, "getImsProvisioningString: ImsService is not available for sub '"
4223 + subId + "' for key:" + key);
4224 return ProvisioningManager.STRING_QUERY_RESULT_ERROR_NOT_READY;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004225 } finally {
4226 Binder.restoreCallingIdentity(identity);
4227 }
4228 }
4229
4230 @Override
4231 public int setImsProvisioningInt(int subId, int key, int value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004232 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4233 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4234 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08004235 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4236 "setImsProvisioningInt");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004237 final long identity = Binder.clearCallingIdentity();
4238 try {
4239 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004240 int slotId = getSlotIndex(subId);
4241 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4242 Log.w(LOG_TAG, "setImsProvisioningInt: called with an inactive subscription id '"
4243 + subId + "' for key:" + key);
4244 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4245 }
4246 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004247 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004248 Log.w(LOG_TAG, "setImsProvisioningInt: ImsService unavailable for sub '" + subId
4249 + "' for key:" + key);
4250 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004251 } finally {
4252 Binder.restoreCallingIdentity(identity);
4253 }
4254 }
4255
4256 @Override
4257 public int setImsProvisioningString(int subId, int key, String value) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004258 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
4259 throw new IllegalArgumentException("Invalid Subscription id '" + subId + "'");
4260 }
Brad Ebinger3d0b34e2018-11-15 14:13:12 -08004261 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp, subId,
4262 "setImsProvisioningString");
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004263 final long identity = Binder.clearCallingIdentity();
4264 try {
4265 // TODO: Refactor to remove ImsManager dependence and query through ImsPhone directly.
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004266 int slotId = getSlotIndex(subId);
4267 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
4268 Log.w(LOG_TAG, "setImsProvisioningString: called with an inactive subscription id '"
4269 + subId + "' for key:" + key);
4270 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
4271 }
4272 return ImsManager.getInstance(mApp, slotId).getConfigInterface().setConfig(key, value);
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004273 } catch (com.android.ims.ImsException e) {
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004274 Log.w(LOG_TAG, "setImsProvisioningString: ImsService unavailable for sub '" + subId
4275 + "' for key:" + key);
4276 return ImsConfigImplBase.CONFIG_RESULT_FAILED;
Brad Ebingerdf5b4f02018-10-31 11:24:17 -07004277 } finally {
4278 Binder.restoreCallingIdentity(identity);
4279 }
4280 }
4281
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004282 private int getSlotIndexOrException(int subId) throws ImsException {
Brad Ebinger35c841c2018-10-01 10:40:55 -07004283 int slotId = SubscriptionManager.getSlotIndex(subId);
4284 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
Brad Ebinger1ce9c432019-07-16 13:19:44 -07004285 throw new ImsException("Invalid Subscription Id, subId=" + subId,
4286 ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
Brad Ebinger35c841c2018-10-01 10:40:55 -07004287 }
4288 return slotId;
4289 }
4290
Brad Ebinger1c8542e2019-01-14 13:43:14 -08004291 private int getSlotIndex(int subId) {
4292 int slotId = SubscriptionManager.getSlotIndex(subId);
4293 if (!SubscriptionManager.isValidSlotIndex(slotId)) {
4294 return SubscriptionManager.INVALID_SIM_SLOT_INDEX;
4295 }
4296 return slotId;
4297 }
4298
Wink Saville36469e72014-06-11 15:17:00 -07004299 /**
Nathan Harold9042f0b2019-05-21 15:51:27 -07004300 * Returns the data network type for a subId; does not throw SecurityException.
Wink Saville36469e72014-06-11 15:17:00 -07004301 */
4302 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004303 public int getNetworkTypeForSubscriber(int subId, String callingPackage,
4304 String callingFeatureId) {
Nathan Haroldef60dba2019-05-22 13:55:14 -07004305 final int targetSdk = getTargetSdk(callingPackage);
4306 if (targetSdk > android.os.Build.VERSION_CODES.Q) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004307 return getDataNetworkTypeForSubscriber(subId, callingPackage, callingFeatureId);
Nathan Haroldef60dba2019-05-22 13:55:14 -07004308 } else if (targetSdk == android.os.Build.VERSION_CODES.Q
Nathan Harold9042f0b2019-05-21 15:51:27 -07004309 && !TelephonyPermissions.checkCallingOrSelfReadPhoneStateNoThrow(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004310 mApp, subId, callingPackage, callingFeatureId,
4311 "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004312 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4313 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004314
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004315 final long identity = Binder.clearCallingIdentity();
4316 try {
4317 final Phone phone = getPhone(subId);
4318 if (phone != null) {
4319 return phone.getServiceState().getDataNetworkType();
4320 } else {
4321 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4322 }
4323 } finally {
4324 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004325 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004326 }
4327
4328 /**
4329 * Returns the data network type
4330 */
4331 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004332 public int getDataNetworkType(String callingPackage, String callingFeatureId) {
4333 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage,
4334 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004335 }
4336
4337 /**
4338 * Returns the data network type for a subId
4339 */
4340 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004341 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage,
4342 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004343 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004344 mApp, subId, callingPackage, callingFeatureId,
4345 "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004346 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4347 }
4348
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004349 final long identity = Binder.clearCallingIdentity();
4350 try {
4351 final Phone phone = getPhone(subId);
4352 if (phone != null) {
4353 return phone.getServiceState().getDataNetworkType();
4354 } else {
4355 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4356 }
4357 } finally {
4358 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004359 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004360 }
4361
4362 /**
Wink Saville36469e72014-06-11 15:17:00 -07004363 * Returns the Voice network type for a subId
4364 */
4365 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004366 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage,
4367 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004368 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004369 mApp, subId, callingPackage, callingFeatureId,
4370 "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07004371 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4372 }
4373
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004374 final long identity = Binder.clearCallingIdentity();
4375 try {
4376 final Phone phone = getPhone(subId);
4377 if (phone != null) {
4378 return phone.getServiceState().getVoiceNetworkType();
4379 } else {
4380 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
4381 }
4382 } finally {
4383 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004384 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004385 }
4386
4387 /**
4388 * @return true if a ICC card is present
4389 */
4390 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07004391 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004392 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
4393 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07004394 }
4395
4396 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004397 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07004398 */
Sanket Padawe356d7632015-06-22 14:03:32 -07004399 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004400 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004401 final long identity = Binder.clearCallingIdentity();
4402 try {
4403 final Phone phone = PhoneFactory.getPhone(slotIndex);
4404 if (phone != null) {
4405 return phone.getIccCard().hasIccCard();
4406 } else {
4407 return false;
4408 }
4409 } finally {
4410 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08004411 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004412 }
4413
4414 /**
4415 * Return if the current radio is LTE on CDMA. This
4416 * is a tri-state return value as for a period of time
4417 * the mode may be unknown.
4418 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004419 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004420 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08004421 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004422 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004423 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004424 public int getLteOnCdmaMode(String callingPackage, String callingFeatureId) {
4425 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage,
4426 callingFeatureId);
Wink Saville36469e72014-06-11 15:17:00 -07004427 }
4428
Sanket Padawe356d7632015-06-22 14:03:32 -07004429 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004430 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage,
4431 String callingFeatureId) {
Sarah Chin790d2922020-01-16 12:17:23 -08004432 try {
4433 enforceReadPrivilegedPermission("getLteOnCdmaModeForSubscriber");
4434 } catch (SecurityException e) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004435 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4436 }
4437
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004438 final long identity = Binder.clearCallingIdentity();
4439 try {
4440 final Phone phone = getPhone(subId);
4441 if (phone == null) {
4442 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
4443 } else {
Nathan Harold05ad6332020-07-10 11:54:36 -07004444 return TelephonyProperties.lte_on_cdma_device()
4445 .orElse(PhoneConstants.LTE_ON_CDMA_FALSE);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004446 }
4447 } finally {
4448 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07004449 }
Wink Saville36469e72014-06-11 15:17:00 -07004450 }
4451
Wink Saville36469e72014-06-11 15:17:00 -07004452 /**
4453 * {@hide}
4454 * Returns Default subId, 0 in the case of single standby.
4455 */
Wink Savilleb564aae2014-10-23 10:18:09 -07004456 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004457 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07004458 }
4459
Shishir Agrawala9f32182016-04-12 12:00:16 -07004460 private int getSlotForDefaultSubscription() {
4461 return mSubscriptionController.getPhoneId(getDefaultSubscription());
4462 }
4463
Wink Savilleb564aae2014-10-23 10:18:09 -07004464 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08004465 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07004466 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004467
Pengquan Menge92a50d2018-09-21 15:54:48 -07004468 private boolean isActiveSubscription(int subId) {
4469 return mSubscriptionController.isActiveSubId(subId);
4470 }
4471
Ihab Awadf2177b72013-11-25 13:33:23 -08004472 /**
4473 * @see android.telephony.TelephonyManager.WifiCallingChoices
4474 */
4475 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004476 final long identity = Binder.clearCallingIdentity();
4477 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004478 return Settings.System.getInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004479 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
4480 getWhenToMakeWifiCallsDefaultPreference());
4481 } finally {
4482 Binder.restoreCallingIdentity(identity);
4483 }
Ihab Awadf2177b72013-11-25 13:33:23 -08004484 }
4485
4486 /**
4487 * @see android.telephony.TelephonyManager.WifiCallingChoices
4488 */
4489 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004490 final long identity = Binder.clearCallingIdentity();
4491 try {
4492 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004493 Settings.System.putInt(mApp.getContentResolver(),
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004494 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
4495 } finally {
4496 Binder.restoreCallingIdentity(identity);
4497 }
Ihab Awadf9e92732013-12-05 18:02:52 -08004498 }
4499
Sailesh Nepald1e68152013-12-12 19:08:02 -08004500 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07004501 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08004502 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08004503 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08004504
Jordan Liu4c733742019-02-28 12:03:40 -08004505 private Phone getPhoneFromSlotIdOrThrowException(int slotIndex) {
4506 int phoneId = UiccController.getInstance().getPhoneIdFromSlotId(slotIndex);
4507 if (phoneId == -1) {
4508 throw new IllegalArgumentException("Given slot index: " + slotIndex
4509 + " does not correspond to an active phone");
4510 }
4511 return PhoneFactory.getPhone(phoneId);
4512 }
4513
Shishir Agrawal566b7612013-10-28 14:41:00 -07004514 @Override
Derek Tan740e1672017-06-27 14:56:27 -07004515 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
4516 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004517 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4518 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004519 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08004520 if (DBG) {
4521 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
4522 }
4523 return iccOpenLogicalChannelWithPermission(getPhoneFromSubId(subId), callingPackage, aid,
4524 p2);
4525 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004526
Jordan Liu4c733742019-02-28 12:03:40 -08004527
4528 @Override
4529 public IccOpenLogicalChannelResponse iccOpenLogicalChannelBySlot(
4530 int slotIndex, String callingPackage, String aid, int p2) {
4531 enforceModifyPermission();
4532 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4533 if (DBG) {
4534 log("iccOpenLogicalChannelBySlot: slot=" + slotIndex + " aid=" + aid + " p2=" + p2);
4535 }
4536 return iccOpenLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
4537 callingPackage, aid, p2);
4538 }
4539
4540 private IccOpenLogicalChannelResponse iccOpenLogicalChannelWithPermission(Phone phone,
4541 String callingPackage, String aid, int p2) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004542 final long identity = Binder.clearCallingIdentity();
4543 try {
4544 if (TextUtils.equals(ISDR_AID, aid)) {
4545 // Only allows LPA to open logical channel to ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004546 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
4547 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004548 if (bestComponent == null
4549 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
4550 loge("The calling package is not allowed to access ISD-R.");
4551 throw new SecurityException(
4552 "The calling package is not allowed to access ISD-R.");
4553 }
Derek Tan740e1672017-06-27 14:56:27 -07004554 }
Derek Tan740e1672017-06-27 14:56:27 -07004555
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004556 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
Jordan Liu4c733742019-02-28 12:03:40 -08004557 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), phone,
4558 null /* workSource */);
4559 if (DBG) log("iccOpenLogicalChannelWithPermission: " + response);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004560 return response;
4561 } finally {
4562 Binder.restoreCallingIdentity(identity);
4563 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004564 }
4565
4566 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004567 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004568 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4569 mApp, subId, "iccCloseLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08004570 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
4571 return iccCloseLogicalChannelWithPermission(getPhoneFromSubId(subId), channel);
4572 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004573
Jordan Liu4c733742019-02-28 12:03:40 -08004574 @Override
4575 public boolean iccCloseLogicalChannelBySlot(int slotIndex, int channel) {
4576 enforceModifyPermission();
4577 if (DBG) log("iccCloseLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel);
4578 return iccCloseLogicalChannelWithPermission(getPhoneFromSlotIdOrThrowException(slotIndex),
4579 channel);
4580 }
4581
4582 private boolean iccCloseLogicalChannelWithPermission(Phone phone, int channel) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004583 final long identity = Binder.clearCallingIdentity();
4584 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004585 if (channel < 0) {
4586 return false;
4587 }
Jordan Liu4c733742019-02-28 12:03:40 -08004588 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, phone,
4589 null /* workSource */);
4590 if (DBG) log("iccCloseLogicalChannelWithPermission: " + success);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004591 return success;
4592 } finally {
4593 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07004594 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004595 }
4596
4597 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004598 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07004599 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004600 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4601 mApp, subId, "iccTransmitApduLogicalChannel");
Jordan Liu4c733742019-02-28 12:03:40 -08004602 if (DBG) {
4603 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
4604 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
4605 + p3 + " data=" + data);
4606 }
4607 return iccTransmitApduLogicalChannelWithPermission(getPhoneFromSubId(subId), channel, cla,
4608 command, p1, p2, p3, data);
4609 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004610
Jordan Liu4c733742019-02-28 12:03:40 -08004611 @Override
4612 public String iccTransmitApduLogicalChannelBySlot(int slotIndex, int channel, int cla,
4613 int command, int p1, int p2, int p3, String data) {
4614 enforceModifyPermission();
4615 if (DBG) {
4616 log("iccTransmitApduLogicalChannelBySlot: slotIndex=" + slotIndex + " chnl=" + channel
4617 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
4618 + p3 + " data=" + data);
4619 }
4620 return iccTransmitApduLogicalChannelWithPermission(
4621 getPhoneFromSlotIdOrThrowException(slotIndex), channel, cla, command, p1, p2, p3,
4622 data);
4623 }
4624
4625 private String iccTransmitApduLogicalChannelWithPermission(Phone phone, int channel, int cla,
4626 int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004627 final long identity = Binder.clearCallingIdentity();
4628 try {
Hall Liu4fd771b2019-05-02 09:16:29 -07004629 if (channel <= 0) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004630 return "";
4631 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004632
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004633 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08004634 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), phone,
4635 null /* workSource */);
4636 if (DBG) log("iccTransmitApduLogicalChannelWithPermission: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07004637
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004638 // Append the returned status code to the end of the response payload.
4639 String s = Integer.toHexString(
4640 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4641 if (response.payload != null) {
4642 s = IccUtils.bytesToHexString(response.payload) + s;
4643 }
4644 return s;
4645 } finally {
4646 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07004647 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07004648 }
Jake Hambye994d462014-02-03 13:10:13 -08004649
Evan Charltonc66da362014-05-16 14:06:40 -07004650 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004651 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
4652 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004653 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4654 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004655 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jordan Liu4c733742019-02-28 12:03:40 -08004656 if (DBG) {
4657 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
4658 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
4659 }
4660 return iccTransmitApduBasicChannelWithPermission(getPhoneFromSubId(subId), callingPackage,
4661 cla, command, p1, p2, p3, data);
4662 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004663
Jordan Liu4c733742019-02-28 12:03:40 -08004664 @Override
4665 public String iccTransmitApduBasicChannelBySlot(int slotIndex, String callingPackage, int cla,
4666 int command, int p1, int p2, int p3, String data) {
4667 enforceModifyPermission();
4668 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4669 if (DBG) {
4670 log("iccTransmitApduBasicChannelBySlot: slotIndex=" + slotIndex + " cla=" + cla
4671 + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3
4672 + " data=" + data);
4673 }
4674
4675 return iccTransmitApduBasicChannelWithPermission(
4676 getPhoneFromSlotIdOrThrowException(slotIndex), callingPackage, cla, command, p1,
4677 p2, p3, data);
4678 }
4679
4680 // open APDU basic channel assuming the caller has sufficient permissions
4681 private String iccTransmitApduBasicChannelWithPermission(Phone phone, String callingPackage,
4682 int cla, int command, int p1, int p2, int p3, String data) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004683 final long identity = Binder.clearCallingIdentity();
4684 try {
4685 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
4686 && TextUtils.equals(ISDR_AID, data)) {
4687 // Only allows LPA to select ISD-R.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004688 ComponentInfo bestComponent = EuiccConnector.findBestComponent(getDefaultPhone()
4689 .getContext().getPackageManager());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004690 if (bestComponent == null
4691 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
4692 loge("The calling package is not allowed to select ISD-R.");
4693 throw new SecurityException(
4694 "The calling package is not allowed to select ISD-R.");
4695 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004696 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08004697
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004698 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Jordan Liu4c733742019-02-28 12:03:40 -08004699 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), phone,
4700 null /* workSource */);
4701 if (DBG) log("iccTransmitApduBasicChannelWithPermission: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004702
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004703 // Append the returned status code to the end of the response payload.
4704 String s = Integer.toHexString(
4705 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4706 if (response.payload != null) {
4707 s = IccUtils.bytesToHexString(response.payload) + s;
4708 }
4709 return s;
4710 } finally {
4711 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07004712 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004713 }
4714
4715 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004716 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004717 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004718 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4719 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004720
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004721 final long identity = Binder.clearCallingIdentity();
4722 try {
4723 if (DBG) {
4724 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
4725 + p1 + " " + p2 + " " + p3 + ":" + filePath);
4726 }
4727
4728 IccIoResult response =
4729 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
4730 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
4731 subId);
4732
4733 if (DBG) {
4734 log("Exchange SIM_IO [R]" + response);
4735 }
4736
4737 byte[] result = null;
4738 int length = 2;
4739 if (response.payload != null) {
4740 length = 2 + response.payload.length;
4741 result = new byte[length];
4742 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
4743 } else {
4744 result = new byte[length];
4745 }
4746
4747 result[length - 1] = (byte) response.sw2;
4748 result[length - 2] = (byte) response.sw1;
4749 return result;
4750 } finally {
4751 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004752 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004753 }
4754
Nathan Haroldb3014052017-01-25 15:57:32 -08004755 /**
4756 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
4757 * on a particular subscription
4758 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004759 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage,
4760 String callingFeatureId) {
sqianb6e41952018-03-12 14:54:01 -07004761 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004762 mApp, subId, callingPackage, callingFeatureId, "getForbiddenPlmns")) {
sqianb6e41952018-03-12 14:54:01 -07004763 return null;
4764 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004765
4766 final long identity = Binder.clearCallingIdentity();
4767 try {
4768 if (appType != TelephonyManager.APPTYPE_USIM
4769 && appType != TelephonyManager.APPTYPE_SIM) {
4770 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
4771 return null;
4772 }
4773 Object response = sendRequest(
4774 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
4775 if (response instanceof String[]) {
4776 return (String[]) response;
4777 }
yincheng zhao2737e882019-09-06 17:06:54 -07004778 // Response is an Exception of some kind
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004779 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08004780 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004781 } finally {
4782 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08004783 }
Nathan Haroldb3014052017-01-25 15:57:32 -08004784 }
4785
yincheng zhao2737e882019-09-06 17:06:54 -07004786 /**
4787 * Set the forbidden PLMN list from the given app type (ex APPTYPE_USIM) on a particular
4788 * subscription.
4789 *
4790 * @param subId the id of the subscription.
4791 * @param appType the uicc app type, must be USIM or SIM.
4792 * @param fplmns the Forbiden plmns list that needed to be written to the SIM.
4793 * @param callingPackage the op Package name.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004794 * @param callingFeatureId the feature in the package.
yincheng zhao2737e882019-09-06 17:06:54 -07004795 * @return number of fplmns that is successfully written to the SIM.
4796 */
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004797 public int setForbiddenPlmns(int subId, int appType, List<String> fplmns, String callingPackage,
4798 String callingFeatureId) {
4799 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, subId, callingPackage,
4800 callingFeatureId, "setForbiddenPlmns")) {
yincheng zhao2737e882019-09-06 17:06:54 -07004801 if (DBG) logv("no permissions for setForbiddenplmns");
4802 throw new IllegalStateException("No Permissions for setForbiddenPlmns");
4803 }
4804 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
4805 loge("setForbiddenPlmnList(): App Type must be USIM or SIM");
4806 throw new IllegalArgumentException("Invalid appType: App Type must be USIM or SIM");
4807 }
4808 if (fplmns == null) {
4809 throw new IllegalArgumentException("Fplmn List provided is null");
4810 }
4811 for (String fplmn : fplmns) {
4812 if (!CellIdentity.isValidPlmn(fplmn)) {
4813 throw new IllegalArgumentException("Invalid fplmn provided: " + fplmn);
4814 }
4815 }
4816 final long identity = Binder.clearCallingIdentity();
4817 try {
4818 Object response = sendRequest(
4819 CMD_SET_FORBIDDEN_PLMNS,
4820 new Pair<Integer, List<String>>(new Integer(appType), fplmns),
4821 subId);
4822 return (int) response;
4823 } finally {
4824 Binder.restoreCallingIdentity(identity);
4825 }
4826 }
4827
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07004828 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08004829 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004830 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4831 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07004832
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004833 final long identity = Binder.clearCallingIdentity();
4834 try {
4835 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
4836 if (response.payload == null) {
4837 return "";
4838 }
Evan Charltonc66da362014-05-16 14:06:40 -07004839
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004840 // Append the returned status code to the end of the response payload.
4841 String s = Integer.toHexString(
4842 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
4843 s = IccUtils.bytesToHexString(response.payload) + s;
4844 return s;
4845 } finally {
4846 Binder.restoreCallingIdentity(identity);
4847 }
Evan Charltonc66da362014-05-16 14:06:40 -07004848 }
4849
Jake Hambye994d462014-02-03 13:10:13 -08004850 /**
4851 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4852 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4853 *
4854 * @param itemID the ID of the item to read
4855 * @return the NV item as a String, or null on error.
4856 */
4857 @Override
4858 public String nvReadItem(int itemID) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004859 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004860 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4861 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004862
4863 final long identity = Binder.clearCallingIdentity();
4864 try {
4865 if (DBG) log("nvReadItem: item " + itemID);
vagdeviaf9a5b92018-08-15 16:01:53 -07004866 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004867 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
4868 return value;
4869 } finally {
4870 Binder.restoreCallingIdentity(identity);
4871 }
Jake Hambye994d462014-02-03 13:10:13 -08004872 }
4873
4874 /**
4875 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
4876 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
4877 *
4878 * @param itemID the ID of the item to read
4879 * @param itemValue the value to write, as a String
4880 * @return true on success; false on any failure
4881 */
4882 @Override
4883 public boolean nvWriteItem(int itemID, String itemValue) {
vagdeviaf9a5b92018-08-15 16:01:53 -07004884 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Jeff Davidson7e17e312018-02-13 18:17:36 -08004885 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4886 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004887
4888 final long identity = Binder.clearCallingIdentity();
4889 try {
4890 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
4891 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
vagdeviaf9a5b92018-08-15 16:01:53 -07004892 new Pair<Integer, String>(itemID, itemValue), workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004893 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
4894 return success;
4895 } finally {
4896 Binder.restoreCallingIdentity(identity);
4897 }
Jake Hambye994d462014-02-03 13:10:13 -08004898 }
4899
4900 /**
4901 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
4902 * Used for device configuration by some CDMA operators.
4903 *
4904 * @param preferredRoamingList byte array containing the new PRL
4905 * @return true on success; false on any failure
4906 */
4907 @Override
4908 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004909 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4910 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004911
4912 final long identity = Binder.clearCallingIdentity();
4913 try {
4914 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
4915 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
4916 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
4917 return success;
4918 } finally {
4919 Binder.restoreCallingIdentity(identity);
4920 }
Jake Hambye994d462014-02-03 13:10:13 -08004921 }
4922
4923 /**
chen xu6dac5ab2018-10-26 17:39:23 -07004924 * Rollback modem configurations to factory default except some config which are in whitelist.
Jake Hambye994d462014-02-03 13:10:13 -08004925 * Used for device configuration by some CDMA operators.
4926 *
chen xu6dac5ab2018-10-26 17:39:23 -07004927 * @param slotIndex - device slot.
4928 *
Jake Hambye994d462014-02-03 13:10:13 -08004929 * @return true on success; false on any failure
4930 */
4931 @Override
chen xu6dac5ab2018-10-26 17:39:23 -07004932 public boolean resetModemConfig(int slotIndex) {
4933 Phone phone = PhoneFactory.getPhone(slotIndex);
4934 if (phone != null) {
4935 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4936 mApp, phone.getSubId(), "resetModemConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004937
chen xu6dac5ab2018-10-26 17:39:23 -07004938 final long identity = Binder.clearCallingIdentity();
4939 try {
4940 Boolean success = (Boolean) sendRequest(CMD_RESET_MODEM_CONFIG, null);
4941 if (DBG) log("resetModemConfig:" + ' ' + (success ? "ok" : "fail"));
4942 return success;
4943 } finally {
4944 Binder.restoreCallingIdentity(identity);
4945 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004946 }
chen xu6dac5ab2018-10-26 17:39:23 -07004947 return false;
4948 }
4949
4950 /**
4951 * Generate a radio modem reset. Used for device configuration by some CDMA operators.
4952 *
4953 * @param slotIndex - device slot.
4954 *
4955 * @return true on success; false on any failure
4956 */
4957 @Override
4958 public boolean rebootModem(int slotIndex) {
4959 Phone phone = PhoneFactory.getPhone(slotIndex);
4960 if (phone != null) {
4961 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4962 mApp, phone.getSubId(), "rebootModem");
4963
4964 final long identity = Binder.clearCallingIdentity();
4965 try {
4966 Boolean success = (Boolean) sendRequest(CMD_MODEM_REBOOT, null);
4967 if (DBG) log("rebootModem:" + ' ' + (success ? "ok" : "fail"));
4968 return success;
4969 } finally {
4970 Binder.restoreCallingIdentity(identity);
4971 }
4972 }
4973 return false;
Jake Hambye994d462014-02-03 13:10:13 -08004974 }
Jake Hamby7c27be32014-03-03 13:25:59 -08004975
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004976 public String[] getPcscfAddress(String apnType, String callingPackage,
4977 String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004978 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07004979 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
4980 callingPackage, callingFeatureId, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07004981 return new String[0];
4982 }
4983
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004984 final long identity = Binder.clearCallingIdentity();
4985 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08004986 return defaultPhone.getPcscfAddress(apnType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004987 } finally {
4988 Binder.restoreCallingIdentity(identity);
4989 }
Wink Saville36469e72014-06-11 15:17:00 -07004990 }
4991
Brad Ebinger51f743a2017-01-23 13:50:20 -08004992 /**
Grace Jiaaa2eb6b2020-01-09 16:26:08 -08004993 * Toggle IMS disable and enable for the framework to reset it. See {@link #enableIms(int)} and
4994 * {@link #disableIms(int)}.
4995 * @param slotIndex device slot.
4996 */
4997 public void resetIms(int slotIndex) {
4998 enforceModifyPermission();
4999
5000 final long identity = Binder.clearCallingIdentity();
5001 try {
5002 if (mImsResolver == null) {
5003 // may happen if the does not support IMS.
5004 return;
5005 }
5006 mImsResolver.disableIms(slotIndex);
5007 mImsResolver.enableIms(slotIndex);
5008 } finally {
5009 Binder.restoreCallingIdentity(identity);
5010 }
5011 }
5012
5013 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005014 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
5015 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08005016 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005017 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08005018 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005019
5020 final long identity = Binder.clearCallingIdentity();
5021 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005022 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005023 // may happen if the device does not support IMS.
5024 return;
5025 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005026 mImsResolver.enableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005027 } finally {
5028 Binder.restoreCallingIdentity(identity);
5029 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005030 }
5031
5032 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005033 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
5034 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08005035 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005036 public void disableIms(int slotId) {
5037 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005038
5039 final long identity = Binder.clearCallingIdentity();
5040 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005041 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005042 // may happen if the device does not support IMS.
5043 return;
5044 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005045 mImsResolver.disableIms(slotId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005046 } finally {
5047 Binder.restoreCallingIdentity(identity);
5048 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005049 }
5050
5051 /**
Brad Ebinger67b3e042020-09-11 12:45:11 -07005052 * Registers for updates to the MmTelFeature connection through the IImsServiceFeatureCallback
5053 * callback.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005054 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005055 @Override
Brad Ebingerf6aca002020-10-01 13:51:05 -07005056 public void registerMmTelFeatureCallback(int slotId, IImsServiceFeatureCallback callback) {
Brad Ebinger34bef922017-11-09 10:27:08 -08005057 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005058
5059 final long identity = Binder.clearCallingIdentity();
5060 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005061 if (mImsResolver == null) {
Brad Ebinger67b3e042020-09-11 12:45:11 -07005062 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5063 "Device does not support IMS");
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005064 }
Brad Ebingerf6aca002020-10-01 13:51:05 -07005065 mImsResolver.listenForFeature(slotId, ImsFeature.FEATURE_MMTEL, callback);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005066 } finally {
5067 Binder.restoreCallingIdentity(identity);
5068 }
Brad Ebinger34bef922017-11-09 10:27:08 -08005069 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005070 /**
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005071 * Unregister a previously registered IImsServiceFeatureCallback associated with an ImsFeature.
5072 */
Brad Ebinger67b3e042020-09-11 12:45:11 -07005073 @Override
5074 public void unregisterImsFeatureCallback(IImsServiceFeatureCallback callback) {
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005075 enforceModifyPermission();
5076
5077 final long identity = Binder.clearCallingIdentity();
5078 try {
5079 if (mImsResolver == null) return;
Brad Ebinger67b3e042020-09-11 12:45:11 -07005080 mImsResolver.unregisterImsFeatureCallback(callback);
Brad Ebinger075ff3a2020-05-18 17:52:58 -07005081 } finally {
5082 Binder.restoreCallingIdentity(identity);
5083 }
5084 }
5085
5086 /**
Brad Ebinger5f64b052017-12-14 14:26:15 -08005087 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005088 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger5f64b052017-12-14 14:26:15 -08005089 */
5090 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
5091 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005092
5093 final long identity = Binder.clearCallingIdentity();
5094 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005095 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005096 // may happen if the device does not support IMS.
5097 return null;
5098 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005099 return mImsResolver.getImsRegistration(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005100 } finally {
5101 Binder.restoreCallingIdentity(identity);
5102 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08005103 }
5104
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005105 /**
5106 * Returns the {@link IImsConfig} structure associated with the slotId and feature
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005107 * specified or null if IMS is not supported on the slot specified.
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005108 */
5109 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
5110 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005111
5112 final long identity = Binder.clearCallingIdentity();
5113 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005114 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005115 // may happen if the device does not support IMS.
5116 return null;
5117 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005118 return mImsResolver.getImsConfig(slotId, feature);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005119 } finally {
5120 Binder.restoreCallingIdentity(identity);
5121 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08005122 }
5123
Brad Ebinger884c07b2018-02-15 16:17:40 -08005124 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07005125 * Sets the ImsService Package Name that Telephony will bind to.
5126 *
Brad Ebinger24c29992019-12-05 13:03:21 -08005127 * @param slotIndex the slot ID that the ImsService should bind for.
5128 * @param isCarrierService true if the ImsService is the carrier override, false if the
Brad Ebingerdac2f002018-04-03 15:17:52 -07005129 * ImsService is the device default ImsService.
Brad Ebinger24c29992019-12-05 13:03:21 -08005130 * @param featureTypes An integer array of feature types associated with a packageName.
5131 * @param packageName The name of the package that the current configuration will be replaced
5132 * with.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005133 * @return true if setting the ImsService to bind to succeeded, false if it did not.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005134 */
Brad Ebinger24c29992019-12-05 13:03:21 -08005135 public boolean setBoundImsServiceOverride(int slotIndex, boolean isCarrierService,
5136 int[] featureTypes, String packageName) {
5137 int[] subIds = SubscriptionManager.getSubId(slotIndex);
5138 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005139 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
5140 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
Brad Ebinger24c29992019-12-05 13:03:21 -08005141 "setBoundImsServiceOverride");
Brad Ebingerde696de2018-04-06 09:56:40 -07005142
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005143 final long identity = Binder.clearCallingIdentity();
5144 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005145 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005146 // may happen if the device does not support IMS.
5147 return false;
5148 }
Brad Ebinger24c29992019-12-05 13:03:21 -08005149 Map<Integer, String> featureConfig = new HashMap<>();
5150 for (int featureType : featureTypes) {
5151 featureConfig.put(featureType, packageName);
5152 }
5153 return mImsResolver.overrideImsServiceConfiguration(slotIndex, isCarrierService,
5154 featureConfig);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005155 } finally {
5156 Binder.restoreCallingIdentity(identity);
5157 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005158 }
5159
5160 /**
Brad Ebinger24c29992019-12-05 13:03:21 -08005161 * Return the package name of the currently bound ImsService.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005162 *
5163 * @param slotId The slot that the ImsService is associated with.
5164 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
5165 * the device default.
Brad Ebinger24c29992019-12-05 13:03:21 -08005166 * @param featureType The feature associated with the queried configuration.
Brad Ebingerdac2f002018-04-03 15:17:52 -07005167 * @return the package name of the ImsService configuration.
5168 */
Brad Ebinger24c29992019-12-05 13:03:21 -08005169 public String getBoundImsServicePackage(int slotId, boolean isCarrierImsService,
5170 @ImsFeature.FeatureType int featureType) {
Brad Ebingerde696de2018-04-06 09:56:40 -07005171 int[] subIds = SubscriptionManager.getSubId(slotId);
Brad Ebinger24c29992019-12-05 13:03:21 -08005172 TelephonyPermissions
5173 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5174 mApp, (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
5175 "getBoundImsServicePackage");
Brad Ebingerde696de2018-04-06 09:56:40 -07005176
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005177 final long identity = Binder.clearCallingIdentity();
5178 try {
Brad Ebinger24c29992019-12-05 13:03:21 -08005179 if (mImsResolver == null) {
Brad Ebinger9c0eb502019-01-23 15:06:19 -08005180 // may happen if the device does not support IMS.
5181 return "";
5182 }
Brad Ebingera80c3312019-12-02 10:59:39 -08005183 // TODO: change API to query RCS separately.
Brad Ebinger24c29992019-12-05 13:03:21 -08005184 return mImsResolver.getImsServiceConfiguration(slotId, isCarrierImsService,
5185 featureType);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005186 } finally {
5187 Binder.restoreCallingIdentity(identity);
5188 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07005189 }
5190
Brad Ebingerbc7dd582019-10-17 17:03:22 -07005191 /**
5192 * Get the MmTelFeature state associated with the requested subscription id.
5193 * @param subId The subscription that the MmTelFeature is associated with.
5194 * @param callback A callback with an integer containing the
5195 * {@link android.telephony.ims.feature.ImsFeature.ImsState} associated with the MmTelFeature.
5196 */
5197 @Override
5198 public void getImsMmTelFeatureState(int subId, IIntegerConsumer callback) {
5199 enforceReadPrivilegedPermission("getImsMmTelFeatureState");
5200 if (!ImsManager.isImsSupportedOnDevice(mApp)) {
5201 throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION,
5202 "IMS not available on device.");
5203 }
5204 final long token = Binder.clearCallingIdentity();
5205 try {
5206 int slotId = getSlotIndex(subId);
5207 if (slotId <= SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
5208 Log.w(LOG_TAG, "getImsMmTelFeatureState: called with an inactive subscription '"
5209 + subId + "'");
5210 throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION);
5211 }
5212 ImsManager.getInstance(mApp, slotId).getImsServiceState(anInteger -> {
5213 try {
5214 callback.accept(anInteger == null ? ImsFeature.STATE_UNAVAILABLE : anInteger);
5215 } catch (RemoteException e) {
5216 Log.w(LOG_TAG, "getImsMmTelFeatureState: remote caller is no longer running. "
5217 + "Ignore");
5218 }
5219 });
5220 } finally {
5221 Binder.restoreCallingIdentity(token);
5222 }
5223 }
5224
Wink Saville36469e72014-06-11 15:17:00 -07005225 public void setImsRegistrationState(boolean registered) {
5226 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005227
5228 final long identity = Binder.clearCallingIdentity();
5229 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005230 getDefaultPhone().setImsRegistrationState(registered);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005231 } finally {
5232 Binder.restoreCallingIdentity(identity);
5233 }
Wink Saville36469e72014-06-11 15:17:00 -07005234 }
5235
5236 /**
Stuart Scott54788802015-03-30 13:18:01 -07005237 * Set the network selection mode to automatic.
5238 *
5239 */
5240 @Override
5241 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005242 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5243 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005244
5245 final long identity = Binder.clearCallingIdentity();
5246 try {
shilufc958392020-01-20 11:36:01 -08005247 if (!isActiveSubscription(subId)) {
5248 return;
5249 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005250 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
5251 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
5252 } finally {
5253 Binder.restoreCallingIdentity(identity);
5254 }
Stuart Scott54788802015-03-30 13:18:01 -07005255 }
5256
Pengquan Mengea84e042018-09-20 14:57:26 -07005257 /**
5258 * Ask the radio to connect to the input network and change selection mode to manual.
5259 *
5260 * @param subId the id of the subscription.
5261 * @param operatorInfo the operator information, included the PLMN, long name and short name of
5262 * the operator to attach to.
5263 * @param persistSelection whether the selection will persist until reboot. If true, only allows
5264 * attaching to the selected PLMN until reboot; otherwise, attach to the chosen PLMN and resume
5265 * normal network selection next time.
5266 * @return {@code true} on success; {@code true} on any failure.
Shishir Agrawal302c8692015-06-19 13:49:39 -07005267 */
5268 @Override
Pengquan Mengea84e042018-09-20 14:57:26 -07005269 public boolean setNetworkSelectionModeManual(
5270 int subId, OperatorInfo operatorInfo, boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005271 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5272 mApp, subId, "setNetworkSelectionModeManual");
Pengquan Menge92a50d2018-09-21 15:54:48 -07005273
5274 if (!isActiveSubscription(subId)) {
5275 return false;
5276 }
5277
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005278 final long identity = Binder.clearCallingIdentity();
5279 try {
Pengquan Mengea84e042018-09-20 14:57:26 -07005280 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operatorInfo,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005281 persistSelection);
Pengquan Mengea84e042018-09-20 14:57:26 -07005282 if (DBG) {
5283 log("setNetworkSelectionModeManual: subId: " + subId
5284 + " operator: " + operatorInfo);
5285 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005286 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
5287 } finally {
5288 Binder.restoreCallingIdentity(identity);
5289 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005290 }
shilu84f6e8b2019-12-19 13:58:01 -08005291 /**
5292 * Get the manual network selection
5293 *
5294 * @param subId the id of the subscription.
5295 *
5296 * @return the previously saved user selected PLMN
5297 */
5298 @Override
5299 public String getManualNetworkSelectionPlmn(int subId) {
5300 TelephonyPermissions
5301 .enforeceCallingOrSelfReadPrecisePhoneStatePermissionOrCarrierPrivilege(
5302 mApp, subId, "getManualNetworkSelectionPlmn");
5303
5304 final long identity = Binder.clearCallingIdentity();
5305 try {
5306 if (!isActiveSubscription(subId)) {
shilufa1c2592020-03-10 10:59:43 -07005307 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08005308 }
5309
5310 final Phone phone = getPhone(subId);
5311 if (phone == null) {
shilufa1c2592020-03-10 10:59:43 -07005312 throw new IllegalArgumentException("Invalid Subscription Id: " + subId);
shilu84f6e8b2019-12-19 13:58:01 -08005313 }
5314 OperatorInfo networkSelection = phone.getSavedNetworkSelection();
5315 return TextUtils.isEmpty(networkSelection.getOperatorNumeric())
5316 ? phone.getManualNetworkSelectionPlmn() : networkSelection.getOperatorNumeric();
5317 } finally {
5318 Binder.restoreCallingIdentity(identity);
5319 }
5320 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005321
5322 /**
5323 * Scans for available networks.
5324 */
5325 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07005326 public CellNetworkScanResult getCellNetworkScanResults(int subId, String callingPackage,
5327 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005328 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5329 mApp, subId, "getCellNetworkScanResults");
Hall Liuf19c44f2018-11-27 14:38:17 -08005330 LocationAccessPolicy.LocationPermissionResult locationResult =
5331 LocationAccessPolicy.checkLocationPermission(mApp,
5332 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5333 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07005334 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08005335 .setCallingPid(Binder.getCallingPid())
5336 .setCallingUid(Binder.getCallingUid())
5337 .setMethod("getCellNetworkScanResults")
5338 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07005339 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
5340 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08005341 .build());
5342 switch (locationResult) {
5343 case DENIED_HARD:
5344 throw new SecurityException("Not allowed to access scan results -- location");
5345 case DENIED_SOFT:
5346 return null;
5347 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005348
Pengquan Menga1bb6272018-09-06 09:59:22 -07005349 long identity = Binder.clearCallingIdentity();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005350 try {
5351 if (DBG) log("getCellNetworkScanResults: subId " + subId);
Pengquan Menga1bb6272018-09-06 09:59:22 -07005352 return (CellNetworkScanResult) sendRequest(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005353 CMD_PERFORM_NETWORK_SCAN, null, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005354 } finally {
5355 Binder.restoreCallingIdentity(identity);
5356 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07005357 }
5358
5359 /**
Shuo Qian4a594052020-01-23 11:59:30 -08005360 * Get the call forwarding info, given the call forwarding reason.
5361 */
5362 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005363 public void getCallForwarding(int subId, int callForwardingReason,
5364 ICallForwardingInfoCallback callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08005365 enforceReadPrivilegedPermission("getCallForwarding");
5366 long identity = Binder.clearCallingIdentity();
5367 try {
5368 if (DBG) {
5369 log("getCallForwarding: subId " + subId
5370 + " callForwardingReason" + callForwardingReason);
5371 }
Hall Liu27d24262020-09-18 19:04:59 -07005372
5373 Phone phone = getPhone(subId);
5374 if (phone == null) {
5375 try {
5376 callback.onError(CallForwardingInfo.ERROR_UNKNOWN);
5377 } catch (RemoteException e) {
5378 // ignore
5379 }
5380 return;
5381 }
5382
5383 Pair<Integer, TelephonyManager.CallForwardingInfoCallback> argument = Pair.create(
5384 callForwardingReason, new TelephonyManager.CallForwardingInfoCallback() {
5385 @Override
5386 public void onCallForwardingInfoAvailable(CallForwardingInfo info) {
5387 try {
5388 callback.onCallForwardingInfoAvailable(info);
5389 } catch (RemoteException e) {
5390 // ignore
5391 }
5392 }
5393
5394 @Override
5395 public void onError(int error) {
5396 try {
5397 callback.onError(error);
5398 } catch (RemoteException e) {
5399 // ignore
5400 }
5401 }
5402 });
5403 sendRequestAsync(CMD_GET_CALL_FORWARDING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08005404 } finally {
5405 Binder.restoreCallingIdentity(identity);
5406 }
5407 }
5408
5409 /**
5410 * Sets the voice call forwarding info including status (enable/disable), call forwarding
5411 * reason, the number to forward, and the timeout before the forwarding is attempted.
5412 */
5413 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005414 public void setCallForwarding(int subId, CallForwardingInfo callForwardingInfo,
5415 IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08005416 enforceModifyPermission();
5417 long identity = Binder.clearCallingIdentity();
5418 try {
5419 if (DBG) {
5420 log("setCallForwarding: subId " + subId
5421 + " callForwardingInfo" + callForwardingInfo);
5422 }
Hall Liu27d24262020-09-18 19:04:59 -07005423
5424 Phone phone = getPhone(subId);
5425 if (phone == null) {
5426 try {
5427 callback.accept(CallForwardingInfo.ERROR_UNKNOWN);
5428 } catch (RemoteException e) {
5429 // ignore
5430 }
5431 return;
5432 }
5433
5434 Pair<CallForwardingInfo, Consumer<Integer>> arguments = Pair.create(callForwardingInfo,
5435 FunctionalUtils.ignoreRemoteException(callback::accept));
5436
5437 sendRequestAsync(CMD_SET_CALL_FORWARDING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08005438 } finally {
5439 Binder.restoreCallingIdentity(identity);
5440 }
5441 }
5442
5443 /**
Hall Liu27d24262020-09-18 19:04:59 -07005444 * Get the call waiting status for a subId.
Shuo Qian4a594052020-01-23 11:59:30 -08005445 */
5446 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005447 public void getCallWaitingStatus(int subId, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08005448 enforceReadPrivilegedPermission("getCallForwarding");
5449 long identity = Binder.clearCallingIdentity();
5450 try {
Hall Liu27d24262020-09-18 19:04:59 -07005451
5452 Phone phone = getPhone(subId);
5453 if (phone == null) {
5454 try {
5455 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
5456 } catch (RemoteException e) {
5457 // ignore
5458 }
5459 return;
5460 }
5461
5462 Consumer<Integer> argument = FunctionalUtils.ignoreRemoteException(callback::accept);
5463
Shuo Qian4a594052020-01-23 11:59:30 -08005464 if (DBG) log("getCallWaitingStatus: subId " + subId);
Hall Liu27d24262020-09-18 19:04:59 -07005465 sendRequestAsync(CMD_GET_CALL_WAITING, argument, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08005466 } finally {
5467 Binder.restoreCallingIdentity(identity);
5468 }
5469 }
5470
5471 /**
Hall Liu27d24262020-09-18 19:04:59 -07005472 * Sets whether call waiting is enabled for a given subId.
Shuo Qian4a594052020-01-23 11:59:30 -08005473 */
5474 @Override
Hall Liu27d24262020-09-18 19:04:59 -07005475 public void setCallWaitingStatus(int subId, boolean enable, IIntegerConsumer callback) {
Shuo Qian4a594052020-01-23 11:59:30 -08005476 enforceModifyPermission();
5477 long identity = Binder.clearCallingIdentity();
5478 try {
Hall Liu27d24262020-09-18 19:04:59 -07005479 if (DBG) log("setCallWaitingStatus: subId " + subId + " enable: " + enable);
5480
5481 Phone phone = getPhone(subId);
5482 if (phone == null) {
5483 try {
5484 callback.accept(TelephonyManager.CALL_WAITING_STATUS_UNKNOWN_ERROR);
5485 } catch (RemoteException e) {
5486 // ignore
5487 }
5488 return;
5489 }
5490
5491 Pair<Boolean, Consumer<Integer>> arguments = Pair.create(enable,
5492 FunctionalUtils.ignoreRemoteException(callback::accept));
5493
5494 sendRequestAsync(CMD_SET_CALL_WAITING, arguments, phone, null);
Shuo Qian4a594052020-01-23 11:59:30 -08005495 } finally {
5496 Binder.restoreCallingIdentity(identity);
5497 }
5498 }
5499
5500 /**
yinxub1bed742017-04-17 11:45:04 -07005501 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07005502 *
yinxub1bed742017-04-17 11:45:04 -07005503 * @param subId id of the subscription
5504 * @param request contains the radio access networks with bands/channels to scan
5505 * @param messenger callback messenger for scan results or errors
5506 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07005507 * @return the id of the requested scan which can be used to stop the scan.
5508 */
5509 @Override
5510 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07005511 IBinder binder, String callingPackage, String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005512 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5513 mApp, subId, "requestNetworkScan");
Hall Liuf19c44f2018-11-27 14:38:17 -08005514 LocationAccessPolicy.LocationPermissionResult locationResult =
5515 LocationAccessPolicy.checkLocationPermission(mApp,
5516 new LocationAccessPolicy.LocationPermissionQuery.Builder()
5517 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07005518 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08005519 .setCallingPid(Binder.getCallingPid())
5520 .setCallingUid(Binder.getCallingUid())
5521 .setMethod("requestNetworkScan")
5522 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
James.cf Lin1d4d7392020-07-03 18:22:53 +08005523 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
5524 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08005525 .build());
Hall Liub2ac8ef2019-02-28 15:56:23 -08005526 if (locationResult != LocationAccessPolicy.LocationPermissionResult.ALLOWED) {
Nathan Harold1c11dba2020-09-22 17:54:53 -07005527 SecurityException e = checkNetworkRequestForSanitizedLocationAccess(
5528 request, subId, callingPackage);
Hall Liub2ac8ef2019-02-28 15:56:23 -08005529 if (e != null) {
5530 if (locationResult == LocationAccessPolicy.LocationPermissionResult.DENIED_HARD) {
5531 throw e;
5532 } else {
Hall Liu0e5abaf2019-04-04 01:25:30 -07005533 loge(e.getMessage());
Hall Liub2ac8ef2019-02-28 15:56:23 -08005534 return TelephonyScanManager.INVALID_SCAN_ID;
5535 }
5536 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005537 }
Hall Liu912dfd32019-04-25 14:02:26 -07005538 int callingUid = Binder.getCallingUid();
5539 int callingPid = Binder.getCallingPid();
Ying Xu94a46582019-04-18 17:14:56 -07005540 final long identity = Binder.clearCallingIdentity();
5541 try {
5542 return mNetworkScanRequestTracker.startNetworkScan(
5543 request, messenger, binder, getPhone(subId),
Hall Liu912dfd32019-04-25 14:02:26 -07005544 callingUid, callingPid, callingPackage);
Ying Xu94a46582019-04-18 17:14:56 -07005545 } finally {
5546 Binder.restoreCallingIdentity(identity);
5547 }
yinxu504e1392017-04-12 16:03:22 -07005548 }
5549
Hall Liub2ac8ef2019-02-28 15:56:23 -08005550 private SecurityException checkNetworkRequestForSanitizedLocationAccess(
Nathan Harold1c11dba2020-09-22 17:54:53 -07005551 NetworkScanRequest request, int subId, String callingPackage) {
5552 boolean hasCarrierPriv = checkCarrierPrivilegesForPackage(subId, callingPackage)
Hall Liu558027f2019-05-15 19:14:05 -07005553 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
5554 boolean hasNetworkScanPermission =
5555 mApp.checkCallingOrSelfPermission(android.Manifest.permission.NETWORK_SCAN)
5556 == PERMISSION_GRANTED;
5557
5558 if (!hasCarrierPriv && !hasNetworkScanPermission) {
5559 return new SecurityException("permission.NETWORK_SCAN or carrier privileges is needed"
5560 + " for network scans without location access.");
Hall Liub2ac8ef2019-02-28 15:56:23 -08005561 }
5562
5563 if (request.getSpecifiers() != null && request.getSpecifiers().length > 0) {
5564 for (RadioAccessSpecifier ras : request.getSpecifiers()) {
Hall Liub2ac8ef2019-02-28 15:56:23 -08005565 if (ras.getChannels() != null && ras.getChannels().length > 0) {
5566 return new SecurityException("Specific channels must not be"
5567 + " scanned without location access.");
5568 }
5569 }
5570 }
5571
Hall Liub2ac8ef2019-02-28 15:56:23 -08005572 return null;
5573 }
5574
yinxu504e1392017-04-12 16:03:22 -07005575 /**
5576 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07005577 *
5578 * @param subId id of the subscription
5579 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07005580 */
5581 @Override
5582 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005583 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5584 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005585
Hall Liu912dfd32019-04-25 14:02:26 -07005586 int callingUid = Binder.getCallingUid();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005587 final long identity = Binder.clearCallingIdentity();
5588 try {
Hall Liu912dfd32019-04-25 14:02:26 -07005589 mNetworkScanRequestTracker.stopNetworkScan(scanId, callingUid);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005590 } finally {
5591 Binder.restoreCallingIdentity(identity);
5592 }
yinxu504e1392017-04-12 16:03:22 -07005593 }
5594
5595 /**
Junda Liu84d15a22014-07-02 11:21:04 -07005596 * Get the calculated preferred network type.
5597 * Used for debugging incorrect network type.
5598 *
5599 * @return the preferred network type, defined in RILConstants.java.
5600 */
5601 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005602 public int getCalculatedPreferredNetworkType(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005603 final Phone defaultPhone = getDefaultPhone();
5604 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07005605 callingPackage, callingFeatureId, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07005606 return RILConstants.PREFERRED_NETWORK_MODE;
5607 }
5608
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005609 final long identity = Binder.clearCallingIdentity();
5610 try {
5611 // FIXME: need to get SubId from somewhere.
Nazanin Bakhshif782e562018-12-11 15:15:39 -08005612 return PhoneFactory.calculatePreferredNetworkType(defaultPhone.getContext(), 0);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005613 } finally {
5614 Binder.restoreCallingIdentity(identity);
5615 }
Junda Liu84d15a22014-07-02 11:21:04 -07005616 }
5617
5618 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08005619 * Get the preferred network type.
5620 * Used for device configuration by some CDMA operators.
5621 *
5622 * @return the preferred network type, defined in RILConstants.java.
5623 */
5624 @Override
Stuart Scott54788802015-03-30 13:18:01 -07005625 public int getPreferredNetworkType(int subId) {
Pengquan Menga4009cb2018-12-20 11:00:24 -08005626 TelephonyPermissions
5627 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5628 mApp, subId, "getPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005629
5630 final long identity = Binder.clearCallingIdentity();
5631 try {
5632 if (DBG) log("getPreferredNetworkType");
5633 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
5634 int networkType = (result != null ? result[0] : -1);
5635 if (DBG) log("getPreferredNetworkType: " + networkType);
5636 return networkType;
5637 } finally {
5638 Binder.restoreCallingIdentity(identity);
5639 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005640 }
5641
5642 /**
5643 * Set the preferred network type.
Jake Hamby7c27be32014-03-03 13:25:59 -08005644 *
5645 * @param networkType the preferred network type, defined in RILConstants.java.
5646 * @return true on success; false on any failure.
5647 */
5648 @Override
Stuart Scott54788802015-03-30 13:18:01 -07005649 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005650 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5651 mApp, subId, "setPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005652
5653 final long identity = Binder.clearCallingIdentity();
5654 try {
calvinpan8ed33732020-03-12 14:17:55 +08005655 Boolean success = (Boolean) sendRequest(
5656 CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
calvinpan03641a72020-08-18 16:53:59 +08005657
5658 if (success) {
5659 Settings.Global.putInt(mApp.getContentResolver(),
5660 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
5661 }
calvinpan8ed33732020-03-12 14:17:55 +08005662 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
5663 return success;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005664 } finally {
5665 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07005666 }
Jake Hamby7c27be32014-03-03 13:25:59 -08005667 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005668
5669 /**
calvinpan0ac9c1a2020-01-14 20:42:55 +08005670 * Get the allowed network types that store in the telephony provider.
5671 *
5672 * @param subId the id of the subscription.
5673 * @return allowedNetworkTypes the allowed network types.
5674 */
5675 @Override
5676 public long getAllowedNetworkTypes(int subId) {
5677 TelephonyPermissions
5678 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5679 mApp, subId, "getAllowedNetworkTypes");
5680
5681 final long identity = Binder.clearCallingIdentity();
5682 try {
5683 return SubscriptionManager.getLongSubscriptionProperty(
5684 subId, SubscriptionManager.ALLOWED_NETWORK_TYPES, -1, mApp);
5685 } finally {
5686 Binder.restoreCallingIdentity(identity);
5687 }
5688 }
5689
5690 /**
5691 * Set the allowed network types.
5692 *
5693 * @param subId the id of the subscription.
5694 * @param allowedNetworkTypes the allowed network types.
5695 * @return true on success; false on any failure.
5696 */
5697 @Override
5698 public boolean setAllowedNetworkTypes(int subId, long allowedNetworkTypes) {
5699 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5700 mApp, subId, "setAllowedNetworkTypes");
calvinpan0ac9c1a2020-01-14 20:42:55 +08005701
calvinpan8ed33732020-03-12 14:17:55 +08005702 SubscriptionManager.setSubscriptionProperty(subId,
5703 SubscriptionManager.ALLOWED_NETWORK_TYPES,
5704 String.valueOf(allowedNetworkTypes));
calvinpan0ac9c1a2020-01-14 20:42:55 +08005705
calvinpan8ed33732020-03-12 14:17:55 +08005706 int preferredNetworkMode = Settings.Global.getInt(mApp.getContentResolver(),
5707 Settings.Global.PREFERRED_NETWORK_MODE + subId,
5708 RILConstants.PREFERRED_NETWORK_MODE);
5709 return setPreferredNetworkType(subId, preferredNetworkMode);
calvinpan0ac9c1a2020-01-14 20:42:55 +08005710 }
5711
5712 /**
Sooraj Sasindranc46dfbd2020-06-03 01:06:00 -07005713 * Get the allowed network types for certain reason.
5714 *
5715 * @param subId the id of the subscription.
5716 * @param reason the reason the allowed network type change is taking place
5717 * @return the allowed network types.
5718 */
5719 @Override
5720 public long getAllowedNetworkTypesForReason(int subId,
5721 @TelephonyManager.AllowedNetworkTypesReason int reason) {
5722 TelephonyPermissions
5723 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5724 mApp, subId, "getAllowedNetworkTypesForReason");
5725 final long identity = Binder.clearCallingIdentity();
5726 try {
5727 return getPhoneFromSubId(subId).getAllowedNetworkTypes(reason);
5728 } finally {
5729 Binder.restoreCallingIdentity(identity);
5730 }
5731 }
5732
5733 /**
5734 * Get the effective allowed network types on the device.
5735 * This API will return an intersection of allowed network types for all reasons,
5736 * including the configuration done through setAllowedNetworkTypes
5737 *
5738 * @param subId the id of the subscription.
5739 * @return the allowed network types
5740 */
5741 @Override
5742 public long getEffectiveAllowedNetworkTypes(int subId) {
5743 TelephonyPermissions
5744 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
5745 mApp, subId, "getEffectiveAllowedNetworkTypes");
5746 final long identity = Binder.clearCallingIdentity();
5747 try {
5748 return getPhoneFromSubId(subId).getEffectiveAllowedNetworkTypes();
5749 } finally {
5750 Binder.restoreCallingIdentity(identity);
5751 }
5752 }
5753
5754 /**
5755 * Set the allowed network types of the device and
5756 * provide the reason triggering the allowed network change.
5757 *
5758 * @param subId the id of the subscription.
5759 * @param reason the reason the allowed network type change is taking place
5760 * @param allowedNetworkTypes the allowed network types.
5761 * @return true on success; false on any failure.
5762 */
5763 @Override
5764 public boolean setAllowedNetworkTypesForReason(int subId,
5765 @TelephonyManager.AllowedNetworkTypesReason int reason, long allowedNetworkTypes) {
5766 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5767 mApp, subId, "setAllowedNetworkTypesForReason");
5768 final long identity = Binder.clearCallingIdentity();
5769 try {
5770 getPhoneFromSubId(subId).setAllowedNetworkTypes(reason, allowedNetworkTypes);
5771 int preferredNetworkMode = Settings.Global.getInt(mApp.getContentResolver(),
5772 Settings.Global.PREFERRED_NETWORK_MODE + subId,
5773 RILConstants.PREFERRED_NETWORK_MODE);
5774 return setPreferredNetworkType(subId, preferredNetworkMode);
5775 } finally {
5776 Binder.restoreCallingIdentity(identity);
5777 }
5778 }
5779
5780 /**
Miaoa84611c2019-03-15 09:21:10 +08005781 * Check whether DUN APN is required for tethering with subId.
Junda Liu475951f2014-11-07 16:45:03 -08005782 *
Miaoa84611c2019-03-15 09:21:10 +08005783 * @param subId the id of the subscription to require tethering.
Amit Mahajanfe58cdf2017-07-11 12:01:53 -07005784 * @return {@code true} if DUN APN is required for tethering.
Junda Liu475951f2014-11-07 16:45:03 -08005785 * @hide
5786 */
5787 @Override
SongFerngWangf08d8122019-11-15 14:58:44 +08005788 public boolean isTetheringApnRequiredForSubscriber(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08005789 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005790 final long identity = Binder.clearCallingIdentity();
Miaoa84611c2019-03-15 09:21:10 +08005791 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005792 try {
Miaoa84611c2019-03-15 09:21:10 +08005793 if (phone != null) {
5794 return phone.hasMatchedTetherApnSetting();
5795 } else {
5796 return false;
5797 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005798 } finally {
5799 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08005800 }
Junda Liu475951f2014-11-07 16:45:03 -08005801 }
5802
5803 /**
Shuo Qiancd19c462020-01-16 20:51:11 -08005804 * Enable or disable always reporting signal strength changes from radio.
5805 *
5806 * @param isEnable {@code true} for enabling; {@code false} for disabling.
5807 */
5808 @Override
5809 public void setAlwaysReportSignalStrength(int subId, boolean isEnable) {
5810 enforceModifyPermission();
5811 enforceSystemCaller();
5812
5813 final long identity = Binder.clearCallingIdentity();
5814 final Phone phone = getPhone(subId);
5815 try {
5816 if (phone != null) {
5817 if (DBG) {
5818 log("setAlwaysReportSignalStrength: subId=" + subId
5819 + " isEnable=" + isEnable);
5820 }
5821 phone.setAlwaysReportSignalStrength(isEnable);
5822 } else {
5823 loge("setAlwaysReportSignalStrength: no phone found for subId="
5824 + subId);
5825 }
5826 } finally {
5827 Binder.restoreCallingIdentity(identity);
5828 }
5829 }
5830
5831 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08005832 * Get the user enabled state of Mobile Data.
5833 *
5834 * TODO: remove and use isUserDataEnabled.
5835 * This can't be removed now because some vendor codes
5836 * calls through ITelephony directly while they should
5837 * use TelephonyManager.
5838 *
5839 * @return true on enabled
5840 */
5841 @Override
5842 public boolean getDataEnabled(int subId) {
5843 return isUserDataEnabled(subId);
5844 }
5845
5846 /**
5847 * Get whether mobile data is enabled per user setting.
5848 *
5849 * There are other factors deciding whether mobile data is actually enabled, but they are
5850 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07005851 *
Jeff Davidsona1920712016-11-18 17:05:56 -08005852 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07005853 *
5854 * @return {@code true} if data is enabled else {@code false}
5855 */
5856 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08005857 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07005858 try {
5859 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
5860 null);
5861 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08005862 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
5863 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07005864 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005865
5866 final long identity = Binder.clearCallingIdentity();
5867 try {
5868 int phoneId = mSubscriptionController.getPhoneId(subId);
5869 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
5870 Phone phone = PhoneFactory.getPhone(phoneId);
5871 if (phone != null) {
5872 boolean retVal = phone.isUserDataEnabled();
5873 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
5874 return retVal;
5875 } else {
5876 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
5877 return false;
5878 }
5879 } finally {
5880 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08005881 }
5882 }
5883
5884 /**
Shuo Qian8ee4e882020-01-08 14:30:06 -08005885 * Checks if the device is capable of mobile data by considering whether whether the
5886 * user has enabled mobile data, whether the carrier has enabled mobile data, and
5887 * whether the network policy allows data connections.
Malcolm Chen964682d2017-11-28 16:20:07 -08005888 *
Shuo Qian8ee4e882020-01-08 14:30:06 -08005889 * @return {@code true} if the overall data connection is capable; {@code false} if not.
Malcolm Chen964682d2017-11-28 16:20:07 -08005890 */
5891 @Override
5892 public boolean isDataEnabled(int subId) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005893 try {
5894 try {
5895 mApp.enforceCallingOrSelfPermission(
5896 android.Manifest.permission.ACCESS_NETWORK_STATE,
5897 null);
5898 } catch (Exception e) {
5899 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
5900 "isDataEnabled");
5901 }
5902 } catch (Exception e) {
5903 enforceReadPrivilegedPermission("isDataEnabled");
5904 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005905
5906 final long identity = Binder.clearCallingIdentity();
5907 try {
5908 int phoneId = mSubscriptionController.getPhoneId(subId);
5909 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
5910 Phone phone = PhoneFactory.getPhone(phoneId);
5911 if (phone != null) {
Jack Yud79fba22018-12-13 11:51:28 -08005912 boolean retVal = phone.getDataEnabledSettings().isDataEnabled();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005913 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
5914 return retVal;
5915 } else {
5916 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
5917 return false;
5918 }
5919 } finally {
5920 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08005921 }
Robert Greenwalted86e582014-05-21 20:03:20 -07005922 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07005923
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005924 /**
5925 * Check if data is enabled for a specific reason
5926 * @param subId Subscription index
5927 * @param reason the reason the data enable change is taking place
5928 * @return {@code true} if the overall data is enabled; {@code false} if not.
5929 */
5930 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005931 public boolean isDataEnabledForReason(int subId,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005932 @TelephonyManager.DataEnabledReason int reason) {
5933 try {
5934 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
5935 null);
5936 } catch (Exception e) {
5937 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005938 "isDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005939 }
5940
5941
5942 final long identity = Binder.clearCallingIdentity();
5943 try {
5944 int phoneId = mSubscriptionController.getPhoneId(subId);
5945 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005946 log("isDataEnabledForReason: subId=" + subId + " phoneId=" + phoneId
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005947 + " reason=" + reason);
5948 }
5949 Phone phone = PhoneFactory.getPhone(phoneId);
5950 if (phone != null) {
5951 boolean retVal;
5952 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER) {
5953 retVal = phone.isUserDataEnabled();
5954 } else {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005955 retVal = phone.getDataEnabledSettings().isDataEnabledForReason(reason);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005956 }
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005957 if (DBG) log("isDataEnabledForReason: retVal=" + retVal);
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005958 return retVal;
5959 } else {
5960 if (DBG) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07005961 loge("isDataEnabledForReason: no phone subId="
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00005962 + subId + " retVal=false");
5963 }
5964 return false;
5965 }
5966 } finally {
5967 Binder.restoreCallingIdentity(identity);
5968 }
5969 }
5970
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08005971 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, int uid,
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005972 Phone phone) {
Hall Liu54a2a0c2020-07-13 12:13:03 -07005973 if (uid == Process.SYSTEM_UID || uid == Process.PHONE_UID) {
5974 // Skip the check if it's one of these special uids
5975 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
5976 }
5977
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005978 //load access rules from carrier configs, and check those as well: b/139133814
5979 SubscriptionController subController = SubscriptionController.getInstance();
5980 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
5981 || subController == null) return privilegeFromSim;
5982
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005983 PackageManager pkgMgr = phone.getContext().getPackageManager();
5984 String[] packages = pkgMgr.getPackagesForUid(uid);
5985
5986 final long identity = Binder.clearCallingIdentity();
5987 try {
Jeff Davidson8ab02b22020-03-28 12:24:40 -07005988 int subId = phone.getSubId();
5989 if (mCarrierPrivilegeTestOverrideSubIds.contains(subId)) {
5990 // A test override is in place for the privileges for this subId, so don't try to
5991 // read the subscription privileges.
5992 return privilegeFromSim;
5993 }
5994 SubscriptionInfo subInfo = subController.getSubscriptionInfo(subId);
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07005995 SubscriptionManager subManager = (SubscriptionManager)
5996 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
5997 for (String pkg : packages) {
5998 if (subManager.canManageSubscription(subInfo, pkg)) {
5999 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
6000 }
6001 }
6002 return privilegeFromSim;
6003 } finally {
6004 Binder.restoreCallingIdentity(identity);
6005 }
6006 }
6007
6008 private int getCarrierPrivilegeStatusFromCarrierConfigRules(int privilegeFromSim, Phone phone,
6009 String pkgName) {
6010 //load access rules from carrier configs, and check those as well: b/139133814
6011 SubscriptionController subController = SubscriptionController.getInstance();
6012 if (privilegeFromSim == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS
6013 || subController == null) return privilegeFromSim;
6014
6015 final long identity = Binder.clearCallingIdentity();
6016 try {
Jeff Davidson8ab02b22020-03-28 12:24:40 -07006017 int subId = phone.getSubId();
6018 if (mCarrierPrivilegeTestOverrideSubIds.contains(subId)) {
6019 // A test override is in place for the privileges for this subId, so don't try to
6020 // read the subscription privileges.
6021 return privilegeFromSim;
6022 }
6023 SubscriptionInfo subInfo = subController.getSubscriptionInfo(subId);
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006024 SubscriptionManager subManager = (SubscriptionManager)
6025 phone.getContext().getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
6026 return subManager.canManageSubscription(subInfo, pkgName)
6027 ? TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS : privilegeFromSim;
6028 } finally {
6029 Binder.restoreCallingIdentity(identity);
6030 }
6031 }
6032
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006033 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006034 public int getCarrierPrivilegeStatus(int subId) {
6035 final Phone phone = getPhone(subId);
6036 if (phone == null) {
6037 loge("getCarrierPrivilegeStatus: Invalid subId");
6038 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6039 }
6040 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006041 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08006042 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006043 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6044 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006045
6046 return getCarrierPrivilegeStatusFromCarrierConfigRules(
6047 card.getCarrierPrivilegeStatusForCurrentTransaction(
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006048 phone.getContext().getPackageManager()), Binder.getCallingUid(), phone);
Shishir Agrawal60f9c952014-06-23 12:00:43 -07006049 }
Junda Liu29340342014-07-10 15:23:27 -07006050
6051 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08006052 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006053 enforceReadPrivilegedPermission("getCarrierPrivilegeStatusForUid");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006054 final Phone phone = getPhone(subId);
6055 if (phone == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006056 loge("getCarrierPrivilegeStatusForUid: Invalid subId");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006057 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
6058 }
6059 UiccProfile profile =
6060 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
6061 if (profile == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006062 loge("getCarrierPrivilegeStatusForUid: No UICC");
Jeff Davidson7e17e312018-02-13 18:17:36 -08006063 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6064 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006065 return getCarrierPrivilegeStatusFromCarrierConfigRules(
Shuo Qian2c0ae432019-12-05 11:40:37 -08006066 profile.getCarrierPrivilegeStatusForUid(
Malcolm Chendf0b4cc2020-02-24 15:12:43 -08006067 phone.getContext().getPackageManager(), uid), uid, phone);
Jeff Davidson7e17e312018-02-13 18:17:36 -08006068 }
6069
6070 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006071 public int checkCarrierPrivilegesForPackage(int subId, String pkgName) {
6072 if (TextUtils.isEmpty(pkgName)) {
Junda Liu317d70b2016-03-08 09:33:53 -08006073 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
chen xuf7e9fe82019-05-09 19:31:02 -07006074 }
6075
6076 int phoneId = SubscriptionManager.getPhoneId(subId);
6077 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006078 if (card == null) {
chen xuf7e9fe82019-05-09 19:31:02 -07006079 loge("checkCarrierPrivilegesForPackage: No UICC on subId " + subId);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07006080 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6081 }
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006082 return getCarrierPrivilegeStatusFromCarrierConfigRules(
6083 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
6084 getPhone(phoneId), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006085 }
6086
6087 @Override
6088 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08006089 if (TextUtils.isEmpty(pkgName))
6090 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07006091 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
6092 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
6093 UiccCard card = UiccController.getInstance().getUiccCard(i);
6094 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07006095 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07006096 continue;
6097 }
6098
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006099 result = getCarrierPrivilegeStatusFromCarrierConfigRules(
6100 card.getCarrierPrivilegeStatus(mApp.getPackageManager(), pkgName),
6101 getPhone(i), pkgName);
Zach Johnson50ecba32015-05-19 00:24:21 -07006102 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
6103 break;
6104 }
6105 }
6106
6107 return result;
Junda Liu29340342014-07-10 15:23:27 -07006108 }
Derek Tan89e89d42014-07-08 17:00:10 -07006109
6110 @Override
Junda Liue64de782015-04-16 17:19:16 -07006111 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
6112 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
6113 loge("phoneId " + phoneId + " is not valid.");
6114 return null;
6115 }
6116 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006117 if (card == null) {
Taesu Leef8fbed92019-10-07 18:47:02 +09006118 loge("getCarrierPackageNamesForIntentAndPhone: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006119 return null ;
6120 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006121 return card.getCarrierPackageNamesForIntent(mApp.getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006122 }
6123
Amith Yamasani6e118872016-02-19 12:53:51 -08006124 @Override
chen xuf7e9fe82019-05-09 19:31:02 -07006125 public List<String> getPackagesWithCarrierPrivileges(int phoneId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006126 PackageManager pm = mApp.getPackageManager();
Amith Yamasani6e118872016-02-19 12:53:51 -08006127 List<String> privilegedPackages = new ArrayList<>();
6128 List<PackageInfo> packages = null;
chen xuf7e9fe82019-05-09 19:31:02 -07006129 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
6130 // has UICC in that slot.
6131 if (card != null) {
Amith Yamasani6e118872016-02-19 12:53:51 -08006132 if (card.hasCarrierPrivilegeRules()) {
6133 if (packages == null) {
6134 // Only check packages in user 0 for now
6135 packages = pm.getInstalledPackagesAsUser(
Nazanin Bakhshi5d0636e2019-08-19 16:29:37 -07006136 PackageManager.MATCH_DISABLED_COMPONENTS
6137 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
Cheonho Park17089c62019-08-01 15:23:12 +09006138 | PackageManager.GET_SIGNING_CERTIFICATES,
Amit Mahajanb8f13202020-01-27 18:16:07 -08006139 UserHandle.SYSTEM.getIdentifier());
Amith Yamasani6e118872016-02-19 12:53:51 -08006140 }
6141 for (int p = packages.size() - 1; p >= 0; p--) {
6142 PackageInfo pkgInfo = packages.get(p);
6143 if (pkgInfo != null && pkgInfo.packageName != null
6144 && card.getCarrierPrivilegeStatus(pkgInfo)
chen xuf7e9fe82019-05-09 19:31:02 -07006145 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Amith Yamasani6e118872016-02-19 12:53:51 -08006146 privilegedPackages.add(pkgInfo.packageName);
6147 }
6148 }
6149 }
6150 }
6151 return privilegedPackages;
6152 }
6153
chen xuf7e9fe82019-05-09 19:31:02 -07006154 @Override
6155 public List<String> getPackagesWithCarrierPrivilegesForAllPhones() {
Shuo Qian067a06d2019-12-03 23:40:18 +00006156 enforceReadPrivilegedPermission("getPackagesWithCarrierPrivilegesForAllPhones");
6157
6158 final long identity = Binder.clearCallingIdentity();
6159
chen xuf7e9fe82019-05-09 19:31:02 -07006160 List<String> privilegedPackages = new ArrayList<>();
Shuo Qian067a06d2019-12-03 23:40:18 +00006161 try {
6162 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
6163 privilegedPackages.addAll(getPackagesWithCarrierPrivileges(i));
6164 }
6165 } finally {
6166 Binder.restoreCallingIdentity(identity);
chen xuf7e9fe82019-05-09 19:31:02 -07006167 }
6168 return privilegedPackages;
6169 }
6170
Wink Savilleb564aae2014-10-23 10:18:09 -07006171 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07006172 final Phone phone = getPhone(subId);
6173 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07006174 if (card == null) {
6175 loge("getIccId: No UICC");
6176 return null;
6177 }
6178 String iccId = card.getIccId();
6179 if (TextUtils.isEmpty(iccId)) {
6180 loge("getIccId: ICC ID is null or empty.");
6181 return null;
6182 }
6183 return iccId;
6184 }
6185
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07006186 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08006187 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
6188 String number) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006189 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08006190 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07006191
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006192 final long identity = Binder.clearCallingIdentity();
6193 try {
6194 final String iccId = getIccId(subId);
6195 final Phone phone = getPhone(subId);
6196 if (phone == null) {
6197 return false;
6198 }
6199 final String subscriberId = phone.getSubscriberId();
6200
6201 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08006202 Rlog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006203 + subscriberId + " to " + number);
6204 }
6205
6206 if (TextUtils.isEmpty(iccId)) {
6207 return false;
6208 }
6209
6210 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
6211
6212 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
6213 if (alphaTag == null) {
6214 editor.remove(alphaTagPrefKey);
6215 } else {
6216 editor.putString(alphaTagPrefKey, alphaTag);
6217 }
6218
6219 // Record both the line number and IMSI for this ICCID, since we need to
6220 // track all merged IMSIs based on line number
6221 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6222 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
6223 if (number == null) {
6224 editor.remove(numberPrefKey);
6225 editor.remove(subscriberPrefKey);
6226 } else {
6227 editor.putString(numberPrefKey, number);
6228 editor.putString(subscriberPrefKey, subscriberId);
6229 }
6230
6231 editor.commit();
6232 return true;
6233 } finally {
6234 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07006235 }
Derek Tan7226c842014-07-02 17:42:23 -07006236 }
6237
6238 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006239 public String getLine1NumberForDisplay(int subId, String callingPackage,
6240 String callingFeatureId) {
Makoto Onukifee69342015-06-29 14:44:50 -07006241 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08006242 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006243 mApp, subId, callingPackage, callingFeatureId, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08006244 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07006245 return null;
6246 }
Derek Tan97ebb422014-09-05 16:55:38 -07006247
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006248 final long identity = Binder.clearCallingIdentity();
6249 try {
6250 String iccId = getIccId(subId);
6251 if (iccId != null) {
6252 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6253 if (DBG_MERGE) {
6254 log("getLine1NumberForDisplay returning "
6255 + mTelephonySharedPreferences.getString(numberPrefKey, null));
6256 }
6257 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08006258 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006259 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
6260 return null;
6261 } finally {
6262 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07006263 }
Derek Tan7226c842014-07-02 17:42:23 -07006264 }
6265
6266 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006267 public String getLine1AlphaTagForDisplay(int subId, String callingPackage,
6268 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006269 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006270 mApp, subId, callingPackage, callingFeatureId, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07006271 return null;
6272 }
Derek Tan97ebb422014-09-05 16:55:38 -07006273
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006274 final long identity = Binder.clearCallingIdentity();
6275 try {
6276 String iccId = getIccId(subId);
6277 if (iccId != null) {
6278 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
6279 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
6280 }
6281 return null;
6282 } finally {
6283 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07006284 }
Derek Tan7226c842014-07-02 17:42:23 -07006285 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07006286
6287 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006288 public String[] getMergedSubscriberIds(int subId, String callingPackage,
6289 String callingFeatureId) {
Jeff Davidson913390f2018-02-23 17:11:49 -08006290 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
6291 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08006292 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08006293 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006294 callingFeatureId, "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006295 return null;
6296 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08006297
Jordan Liub49b04b2019-05-06 14:45:15 -07006298 // Clear calling identity, when calling TelephonyManager, because callerUid must be
6299 // the process, where TelephonyManager was instantiated.
6300 // Otherwise AppOps check will fail.
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006301 final long identity = Binder.clearCallingIdentity();
6302 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006303 final Context context = mApp;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006304 final TelephonyManager tele = TelephonyManager.from(context);
6305 final SubscriptionManager sub = SubscriptionManager.from(context);
6306
6307 // Figure out what subscribers are currently active
6308 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006309
Jordan Liub49b04b2019-05-06 14:45:15 -07006310 // Only consider subs which match the current subId
6311 // This logic can be simplified. See b/131189269 for progress.
6312 if (isActiveSubscription(subId)) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006313 activeSubscriberIds.add(tele.getSubscriberId(subId));
6314 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006315
6316 // First pass, find a number override for an active subscriber
6317 String mergeNumber = null;
6318 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
6319 for (String key : prefs.keySet()) {
6320 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
6321 final String subscriberId = (String) prefs.get(key);
6322 if (activeSubscriberIds.contains(subscriberId)) {
6323 final String iccId = key.substring(
6324 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
6325 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
6326 mergeNumber = (String) prefs.get(numberKey);
6327 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08006328 Rlog.d(LOG_TAG, "Found line number " + mergeNumber
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006329 + " for active subscriber " + subscriberId);
6330 }
6331 if (!TextUtils.isEmpty(mergeNumber)) {
6332 break;
6333 }
6334 }
6335 }
6336 }
6337
6338 // Shortcut when no active merged subscribers
6339 if (TextUtils.isEmpty(mergeNumber)) {
6340 return null;
6341 }
6342
6343 // Second pass, find all subscribers under that line override
6344 final ArraySet<String> result = new ArraySet<>();
6345 for (String key : prefs.keySet()) {
6346 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
6347 final String number = (String) prefs.get(key);
6348 if (mergeNumber.equals(number)) {
6349 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
6350 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
6351 final String subscriberId = (String) prefs.get(subscriberKey);
6352 if (!TextUtils.isEmpty(subscriberId)) {
6353 result.add(subscriberId);
6354 }
6355 }
6356 }
6357 }
6358
6359 final String[] resultArray = result.toArray(new String[result.size()]);
6360 Arrays.sort(resultArray);
6361 if (DBG_MERGE) {
Amit Mahajanb8f13202020-01-27 18:16:07 -08006362 Rlog.d(LOG_TAG,
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006363 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
6364 }
6365 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07006366 } finally {
6367 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08006368 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08006369 }
6370
6371 @Override
zoey chen38003472019-12-13 17:16:31 +08006372 public String[] getMergedImsisFromGroup(int subId, String callingPackage) {
6373 enforceReadPrivilegedPermission("getMergedImsisFromGroup");
Malcolm Chen6ca97372019-07-01 16:28:21 -07006374
6375 final long identity = Binder.clearCallingIdentity();
6376 try {
6377 final TelephonyManager telephonyManager = mApp.getSystemService(
6378 TelephonyManager.class);
6379 String subscriberId = telephonyManager.getSubscriberId(subId);
6380 if (subscriberId == null) {
6381 if (DBG) {
zoey chen38003472019-12-13 17:16:31 +08006382 log("getMergedImsisFromGroup can't find subscriberId for subId "
Malcolm Chen6ca97372019-07-01 16:28:21 -07006383 + subId);
6384 }
6385 return null;
6386 }
6387
6388 final SubscriptionInfo info = SubscriptionController.getInstance()
6389 .getSubscriptionInfo(subId);
6390 final ParcelUuid groupUuid = info.getGroupUuid();
6391 // If it doesn't belong to any group, return just subscriberId of itself.
6392 if (groupUuid == null) {
6393 return new String[]{subscriberId};
6394 }
6395
6396 // Get all subscriberIds from the group.
6397 final List<String> mergedSubscriberIds = new ArrayList<>();
6398 final List<SubscriptionInfo> groupInfos = SubscriptionController.getInstance()
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006399 .getSubscriptionsInGroup(groupUuid, mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08006400 mApp.getAttributionTag());
Malcolm Chen6ca97372019-07-01 16:28:21 -07006401 for (SubscriptionInfo subInfo : groupInfos) {
6402 subscriberId = telephonyManager.getSubscriberId(subInfo.getSubscriptionId());
6403 if (subscriberId != null) {
6404 mergedSubscriberIds.add(subscriberId);
6405 }
6406 }
6407
6408 return mergedSubscriberIds.toArray(new String[mergedSubscriberIds.size()]);
6409 } finally {
6410 Binder.restoreCallingIdentity(identity);
6411
6412 }
6413 }
6414
6415 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006416 public boolean setOperatorBrandOverride(int subId, String brand) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006417 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mApp,
Jeff Davidson7e17e312018-02-13 18:17:36 -08006418 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006419
6420 final long identity = Binder.clearCallingIdentity();
6421 try {
6422 final Phone phone = getPhone(subId);
6423 return phone == null ? false : phone.setOperatorBrandOverride(brand);
6424 } finally {
6425 Binder.restoreCallingIdentity(identity);
6426 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07006427 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05006428
6429 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006430 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08006431 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
6432 List<String> cdmaNonRoamingList) {
Shuo Qian2c0ae432019-12-05 11:40:37 -08006433 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
6434 mApp, subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006435
6436 final long identity = Binder.clearCallingIdentity();
6437 try {
6438 final Phone phone = getPhone(subId);
6439 if (phone == null) {
6440 return false;
6441 }
6442 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
6443 cdmaNonRoamingList);
6444 } finally {
6445 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08006446 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08006447 }
6448
6449 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00006450 @Deprecated
6451 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
6452 enforceModifyPermission();
6453
6454 int returnValue = 0;
6455 try {
vagdeviaf9a5b92018-08-15 16:01:53 -07006456 AsyncResult result = (AsyncResult) sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
Shuo Qian850e4d6a2018-04-25 21:02:08 +00006457 if(result.exception == null) {
6458 if (result.result != null) {
6459 byte[] responseData = (byte[])(result.result);
6460 if(responseData.length > oemResp.length) {
6461 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
6462 responseData.length + "bytes. Buffer Size is " +
6463 oemResp.length + "bytes.");
6464 }
6465 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
6466 returnValue = responseData.length;
6467 }
6468 } else {
6469 CommandException ex = (CommandException) result.exception;
6470 returnValue = ex.getCommandError().ordinal();
6471 if(returnValue > 0) returnValue *= -1;
6472 }
6473 } catch (RuntimeException e) {
6474 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
6475 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
6476 if(returnValue > 0) returnValue *= -1;
6477 }
6478
6479 return returnValue;
6480 }
6481
6482 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07006483 public void setRadioCapability(RadioAccessFamily[] rafs) {
6484 try {
6485 ProxyController.getInstance().setRadioCapability(rafs);
6486 } catch (RuntimeException e) {
6487 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
6488 }
6489 }
6490
6491 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07006492 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08006493 Phone phone = PhoneFactory.getPhone(phoneId);
Shuo Qiandee53402020-05-29 14:08:15 -07006494 try {
6495 TelephonyPermissions
6496 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
6497 mApp, phone.getSubId(), "getRadioAccessFamily");
6498 } catch (SecurityException e) {
6499 EventLog.writeEvent(0x534e4554, "150857259", -1, "Missing Permission");
6500 throw e;
6501 }
chen xub97461a2018-10-26 14:17:57 -07006502 int raf = RadioAccessFamily.RAF_UNKNOWN;
Jeff Davidson913390f2018-02-23 17:11:49 -08006503 if (phone == null) {
chen xub97461a2018-10-26 14:17:57 -07006504 return raf;
Jeff Davidson913390f2018-02-23 17:11:49 -08006505 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006506 final long identity = Binder.clearCallingIdentity();
6507 try {
chen xub97461a2018-10-26 14:17:57 -07006508 TelephonyPermissions
6509 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
6510 mApp, phone.getSubId(), "getRadioAccessFamily");
6511 raf = ProxyController.getInstance().getRadioAccessFamily(phoneId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006512 } finally {
6513 Binder.restoreCallingIdentity(identity);
6514 }
chen xub97461a2018-10-26 14:17:57 -07006515 return raf;
Wink Saville5d475dd2014-10-17 15:00:58 -07006516 }
Andrew Leedf14ead2014-10-17 14:22:52 -07006517
6518 @Override
6519 public void enableVideoCalling(boolean enable) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006520 final Phone defaultPhone = getDefaultPhone();
Andrew Leedf14ead2014-10-17 14:22:52 -07006521 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006522
6523 final long identity = Binder.clearCallingIdentity();
6524 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006525 ImsManager.getInstance(defaultPhone.getContext(),
6526 defaultPhone.getPhoneId()).setVtSetting(enable);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006527 } finally {
6528 Binder.restoreCallingIdentity(identity);
6529 }
Andrew Leedf14ead2014-10-17 14:22:52 -07006530 }
6531
6532 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006533 public boolean isVideoCallingEnabled(String callingPackage, String callingFeatureId) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006534 final Phone defaultPhone = getDefaultPhone();
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006535 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, defaultPhone.getSubId(),
6536 callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00006537 return false;
6538 }
Svet Ganovb320e182015-04-16 12:30:10 -07006539
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006540 final long identity = Binder.clearCallingIdentity();
6541 try {
6542 // Check the user preference and the system-level IMS setting. Even if the user has
6543 // enabled video calling, if IMS is disabled we aren't able to support video calling.
6544 // In the long run, we may instead need to check if there exists a connection service
6545 // which can support video calling.
6546 ImsManager imsManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006547 ImsManager.getInstance(defaultPhone.getContext(), defaultPhone.getPhoneId());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006548 return imsManager.isVtEnabledByPlatform()
6549 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
6550 && imsManager.isVtEnabledByUser();
6551 } finally {
6552 Binder.restoreCallingIdentity(identity);
6553 }
Andrew Leedf14ead2014-10-17 14:22:52 -07006554 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06006555
Andrew Leea1239f22015-03-02 17:44:07 -08006556 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006557 public boolean canChangeDtmfToneLength(int subId, String callingPackage,
6558 String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006559 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006560 mApp, subId, callingPackage, callingFeatureId,
6561 "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006562 return false;
6563 }
6564
6565 final long identity = Binder.clearCallingIdentity();
6566 try {
6567 CarrierConfigManager configManager =
6568 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006569 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006570 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
6571 } finally {
6572 Binder.restoreCallingIdentity(identity);
6573 }
Andrew Leea1239f22015-03-02 17:44:07 -08006574 }
6575
6576 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006577 public boolean isWorldPhone(int subId, String callingPackage, String callingFeatureId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006578 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006579 mApp, subId, callingPackage, callingFeatureId, "isVideoCallingEnabled")) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006580 return false;
6581 }
6582
6583 final long identity = Binder.clearCallingIdentity();
6584 try {
6585 CarrierConfigManager configManager =
6586 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006587 return configManager.getConfigForSubId(subId)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006588 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
6589 } finally {
6590 Binder.restoreCallingIdentity(identity);
6591 }
Andrew Leea1239f22015-03-02 17:44:07 -08006592 }
6593
Andrew Lee9431b832015-03-09 18:46:45 -07006594 @Override
6595 public boolean isTtyModeSupported() {
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07006596 TelecomManager telecomManager = mApp.getSystemService(TelecomManager.class);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08006597 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07006598 }
6599
6600 @Override
6601 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006602 final long identity = Binder.clearCallingIdentity();
6603 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006604 return mApp.getResources().getBoolean(R.bool.hac_enabled);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006605 } finally {
6606 Binder.restoreCallingIdentity(identity);
6607 }
Andrew Lee9431b832015-03-09 18:46:45 -07006608 }
6609
Hall Liuf6668912018-10-31 17:05:23 -07006610 /**
6611 * Determines whether the device currently supports RTT (Real-time text). Based both on carrier
6612 * support for the feature and device firmware support.
6613 *
6614 * @return {@code true} if the device and carrier both support RTT, {@code false} otherwise.
6615 */
6616 @Override
6617 public boolean isRttSupported(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006618 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006619 final Phone phone = getPhone(subscriptionId);
6620 if (phone == null) {
6621 loge("isRttSupported: no Phone found. Invalid subId:" + subscriptionId);
6622 return false;
6623 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006624 try {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006625 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(subscriptionId).getBoolean(
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006626 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
6627 boolean isDeviceSupported =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006628 phone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006629 return isCarrierSupported && isDeviceSupported;
6630 } finally {
6631 Binder.restoreCallingIdentity(identity);
6632 }
Hall Liu98187582018-01-22 19:15:32 -08006633 }
6634
Hall Liuf6668912018-10-31 17:05:23 -07006635 /**
Hall Liuf2daa022019-07-23 18:39:00 -07006636 * Determines whether the user has turned on RTT. If the carrier wants to ignore the user-set
6637 * RTT setting, will return true if the device and carrier both support RTT.
6638 * Otherwise. only returns true if the device and carrier both also support RTT.
Hall Liuf6668912018-10-31 17:05:23 -07006639 */
6640 public boolean isRttEnabled(int subscriptionId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006641 final long identity = Binder.clearCallingIdentity();
6642 try {
Hall Liu5bab75c2019-12-11 23:58:20 +00006643 boolean isRttSupported = isRttSupported(subscriptionId);
6644 boolean isUserRttSettingOn = Settings.Secure.getInt(
6645 mApp.getContentResolver(), Settings.Secure.RTT_CALLING_MODE, 0) != 0;
6646 boolean shouldIgnoreUserRttSetting = mApp.getCarrierConfigForSubId(subscriptionId)
6647 .getBoolean(CarrierConfigManager.KEY_IGNORE_RTT_MODE_SETTING_BOOL);
6648 return isRttSupported && (isUserRttSettingOn || shouldIgnoreUserRttSetting);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006649 } finally {
6650 Binder.restoreCallingIdentity(identity);
6651 }
Hall Liu3ad5f012018-04-06 16:23:39 -07006652 }
6653
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006654 @Deprecated
6655 @Override
6656 public String getDeviceId(String callingPackage) {
6657 return getDeviceIdWithFeature(callingPackage, null);
6658 }
6659
Sanket Padawe7310cc72015-01-14 09:53:20 -08006660 /**
6661 * Returns the unique device ID of phone, for example, the IMEI for
6662 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
6663 *
6664 * <p>Requires Permission:
6665 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
6666 */
6667 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006668 public String getDeviceIdWithFeature(String callingPackage, String callingFeatureId) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08006669 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08006670 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08006671 return null;
6672 }
Jeff Davidson913390f2018-02-23 17:11:49 -08006673 int subId = phone.getSubId();
Michael Groover70af6dc2018-10-01 16:23:15 -07006674 if (!TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mApp, subId,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006675 callingPackage, callingFeatureId, "getDeviceId")) {
Jeff Davidson913390f2018-02-23 17:11:49 -08006676 return null;
6677 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006678
6679 final long identity = Binder.clearCallingIdentity();
6680 try {
6681 return phone.getDeviceId();
6682 } finally {
6683 Binder.restoreCallingIdentity(identity);
6684 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08006685 }
6686
Ping Sunc67b7c22016-03-02 19:16:45 +08006687 /**
6688 * {@hide}
6689 * Returns the IMS Registration Status on a particular subid
6690 *
6691 * @param subId
6692 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006693 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08006694 Phone phone = getPhone(subId);
6695 if (phone != null) {
6696 return phone.isImsRegistered();
6697 } else {
6698 return false;
6699 }
6700 }
6701
Santos Cordon7a1885b2015-02-03 11:15:19 -08006702 @Override
6703 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006704 final long identity = Binder.clearCallingIdentity();
6705 try {
6706 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
6707 } finally {
6708 Binder.restoreCallingIdentity(identity);
6709 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08006710 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07006711
Tyler Gunnf70ed162019-04-03 15:28:53 -07006712 @Override
Shuo Qian6e6137d2019-10-30 16:33:31 -07006713 public int getSubIdForPhoneAccountHandle(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006714 PhoneAccountHandle phoneAccountHandle, String callingPackage, String callingFeatureId) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07006715 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, getDefaultSubscription(),
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006716 callingPackage, callingFeatureId, "getSubIdForPhoneAccountHandle")) {
Shuo Qian6e6137d2019-10-30 16:33:31 -07006717 throw new SecurityException("Requires READ_PHONE_STATE permission.");
6718 }
6719 final long identity = Binder.clearCallingIdentity();
6720 try {
6721 return PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
6722 } finally {
6723 Binder.restoreCallingIdentity(identity);
6724 }
6725 }
6726
6727 @Override
Tyler Gunnf70ed162019-04-03 15:28:53 -07006728 public @Nullable PhoneAccountHandle getPhoneAccountHandleForSubscriptionId(int subscriptionId) {
duki.hongfd96bde2020-07-22 17:32:19 +09006729 enforceReadPrivilegedPermission("getPhoneAccountHandleForSubscriptionId, "
6730 + "subscriptionId: " + subscriptionId);
Tyler Gunnf70ed162019-04-03 15:28:53 -07006731 final long identity = Binder.clearCallingIdentity();
6732 try {
6733 Phone phone = getPhone(subscriptionId);
6734 if (phone == null) {
6735 return null;
6736 }
6737 return PhoneUtils.makePstnPhoneAccountHandle(phone);
6738 } finally {
6739 Binder.restoreCallingIdentity(identity);
6740 }
6741 }
6742
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006743 /**
6744 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07006745 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006746 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006747 final long identity = Binder.clearCallingIdentity();
6748 try {
6749 Phone phone = getPhone(subId);
6750 if (phone != null) {
6751 return phone.isWifiCallingEnabled();
6752 } else {
6753 return false;
6754 }
6755 } finally {
6756 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006757 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07006758 }
6759
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006760 /**
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006761 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07006762 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006763 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006764 final long identity = Binder.clearCallingIdentity();
6765 try {
6766 Phone phone = getPhone(subId);
6767 if (phone != null) {
6768 return phone.isVideoEnabled();
6769 } else {
6770 return false;
6771 }
6772 } finally {
6773 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006774 }
6775 }
6776
6777 /**
6778 * @return the IMS registration technology for the MMTEL feature. Valid return values are
6779 * defined in {@link ImsRegistrationImplBase}.
6780 */
6781 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006782 final long identity = Binder.clearCallingIdentity();
6783 try {
6784 Phone phone = getPhone(subId);
6785 if (phone != null) {
6786 return phone.getImsRegistrationTech();
6787 } else {
6788 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
6789 }
6790 } finally {
6791 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08006792 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07006793 }
6794
Stuart Scott8eef64f2015-04-08 15:13:54 -07006795 @Override
6796 public void factoryReset(int subId) {
paulhu5a773602019-08-23 19:17:33 +08006797 enforceSettingsPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07006798 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
6799 return;
6800 }
6801
Svet Ganovcc087f82015-05-12 20:35:54 -07006802 final long identity = Binder.clearCallingIdentity();
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006803
Svet Ganovcc087f82015-05-12 20:35:54 -07006804 try {
Stuart Scott981d8582015-04-21 14:09:50 -07006805 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
6806 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07006807 setDataEnabledForReason(subId, TelephonyManager.DATA_ENABLED_REASON_USER,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00006808 getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07006809 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07006810 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
Nazanin Bakhshif782e562018-12-11 15:15:39 -08006811 setDataRoamingEnabled(subId, getDefaultDataRoamingEnabled(subId));
6812 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mApp);
Svet Ganovcc087f82015-05-12 20:35:54 -07006813 }
Amit Mahajan7dbbd822019-03-13 17:33:47 -07006814 // There has been issues when Sms raw table somehow stores orphan
6815 // fragments. They lead to garbled message when new fragments come
6816 // in and combined with those stale ones. In case this happens again,
6817 // user can reset all network settings which will clean up this table.
6818 cleanUpSmsRawTable(getDefaultPhone().getContext());
Brad Ebingerbc7dd582019-10-17 17:03:22 -07006819 // Clean up IMS settings as well here.
6820 int slotId = getSlotIndex(subId);
6821 if (slotId > SubscriptionManager.INVALID_SIM_SLOT_INDEX) {
6822 ImsManager.getInstance(mApp, slotId).factoryReset();
6823 }
Naina Nallurid63128d2019-09-17 14:10:30 -07006824
6825 // Erase modem config if erase modem on network setting is enabled.
6826 String configValue = DeviceConfig.getProperty(DeviceConfig.NAMESPACE_TELEPHONY,
6827 RESET_NETWORK_ERASE_MODEM_CONFIG_ENABLED);
6828 if (configValue != null && Boolean.parseBoolean(configValue)) {
6829 sendEraseModemConfig(getDefaultPhone());
6830 }
Svet Ganovcc087f82015-05-12 20:35:54 -07006831 } finally {
6832 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07006833 }
6834 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006835
Amit Mahajan7dbbd822019-03-13 17:33:47 -07006836 private void cleanUpSmsRawTable(Context context) {
6837 ContentResolver resolver = context.getContentResolver();
6838 Uri uri = Uri.withAppendedPath(Telephony.Sms.CONTENT_URI, "raw/permanentDelete");
6839 resolver.delete(uri, null, null);
6840 }
6841
Narayan Kamath1c496c22015-04-16 14:40:19 +01006842 @Override
chen xu5d3637b2019-01-21 23:31:38 -08006843 public String getSimLocaleForSubscriber(int subId) {
6844 enforceReadPrivilegedPermission("getSimLocaleForSubscriber, subId: " + subId);
6845 final Phone phone = getPhone(subId);
6846 if (phone == null) {
6847 log("getSimLocaleForSubscriber, invalid subId");
chen xu2bb91e42019-01-24 14:35:54 -08006848 return null;
chen xu5d3637b2019-01-21 23:31:38 -08006849 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006850 final long identity = Binder.clearCallingIdentity();
6851 try {
chen xu5d3637b2019-01-21 23:31:38 -08006852 final SubscriptionInfo info = mSubscriptionController.getActiveSubscriptionInfo(subId,
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08006853 phone.getContext().getOpPackageName(), phone.getContext().getAttributionTag());
chen xu6291c472019-02-04 12:55:53 -08006854 if (info == null) {
6855 log("getSimLocaleForSubscriber, inactive subId: " + subId);
6856 return null;
6857 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006858 // Try and fetch the locale from the carrier properties or from the SIM language
6859 // preferences (EF-PL and EF-LI)...
6860 final int mcc = info.getMcc();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006861 String simLanguage = null;
chen xu5d3637b2019-01-21 23:31:38 -08006862 final Locale localeFromDefaultSim = phone.getLocaleFromSimAndCarrierPrefs();
6863 if (localeFromDefaultSim != null) {
6864 if (!localeFromDefaultSim.getCountry().isEmpty()) {
6865 if (DBG) log("Using locale from subId: " + subId + " locale: "
6866 + localeFromDefaultSim);
6867 return localeFromDefaultSim.toLanguageTag();
6868 } else {
6869 simLanguage = localeFromDefaultSim.getLanguage();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006870 }
6871 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006872
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006873 // The SIM language preferences only store a language (e.g. fr = French), not an
6874 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
6875 // the SIM and carrier preferences does not include a country we add the country
6876 // determined from the SIM MCC to provide an exact locale.
zoey chenc730df82019-12-18 17:07:20 +08006877 final Locale mccLocale = LocaleUtils.getLocaleFromMcc(mApp, mcc, simLanguage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006878 if (mccLocale != null) {
chen xu5d3637b2019-01-21 23:31:38 -08006879 if (DBG) log("No locale from SIM, using mcc locale:" + mccLocale);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006880 return mccLocale.toLanguageTag();
6881 }
6882
6883 if (DBG) log("No locale found - returning null");
6884 return null;
6885 } finally {
6886 Binder.restoreCallingIdentity(identity);
6887 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01006888 }
6889
6890 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006891 return mSubscriptionController.getAllSubInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08006892 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01006893 }
6894
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006895 /**
6896 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
6897 */
6898 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006899 return mSubscriptionController.getActiveSubscriptionInfoList(mApp.getOpPackageName(),
Philip P. Moltmann8d34f0c2020-03-05 16:24:02 -08006900 mApp.getAttributionTag());
Narayan Kamath1c496c22015-04-16 14:40:19 +01006901 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006902
Chenjie Yu1ba97252018-01-11 18:16:20 -08006903 private final ModemActivityInfo mLastModemActivityInfo =
Chen Xud78231e2019-09-10 18:49:52 -07006904 new ModemActivityInfo(0, 0, 0, new int[0], 0);
Chenjie Yu1ba97252018-01-11 18:16:20 -08006905
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006906 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07006907 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
6908 * representing the state of the modem.
6909 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08006910 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
6911 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07006912 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006913 */
6914 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07006915 public void requestModemActivityInfo(ResultReceiver result) {
6916 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07006917 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006918
6919 final long identity = Binder.clearCallingIdentity();
6920 try {
Shuo Qian8f4750a2020-02-20 17:12:10 -08006921 sendRequestAsync(CMD_GET_MODEM_ACTIVITY_INFO, result, null, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006922 } finally {
6923 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08006924 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07006925 }
Jack Yu85bd38a2015-11-09 11:34:32 -08006926
Siddharth Rayb8114062018-06-17 15:02:38 -07006927 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
6928 // less than total activity duration.
6929 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
6930 if (info == null) {
6931 return false;
6932 }
6933 int activityDurationMs =
6934 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
6935 int totalTxTimeMs = 0;
Chen Xud78231e2019-09-10 18:49:52 -07006936 int[] txTimeMs = info.getTransmitTimeMillis();
6937 for (int i = 0; i < info.getTransmitPowerInfo().size(); i++) {
6938 totalTxTimeMs += txTimeMs[i];
Siddharth Rayb8114062018-06-17 15:02:38 -07006939 }
6940 return (info.isValid()
6941 && (info.getSleepTimeMillis() <= activityDurationMs)
6942 && (info.getIdleTimeMillis() <= activityDurationMs)
Chen Xud78231e2019-09-10 18:49:52 -07006943 && (info.getReceiveTimeMillis() <= activityDurationMs)
Siddharth Rayb8114062018-06-17 15:02:38 -07006944 && (totalTxTimeMs <= activityDurationMs));
6945 }
6946
Jack Yu85bd38a2015-11-09 11:34:32 -08006947 /**
6948 * {@hide}
6949 * Returns the service state information on specified subscription.
6950 */
6951 @Override
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006952 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage,
6953 String callingFeatureId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08006954 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07006955 mApp, subId, callingPackage, callingFeatureId, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08006956 return null;
6957 }
6958
Hall Liuf19c44f2018-11-27 14:38:17 -08006959 LocationAccessPolicy.LocationPermissionResult fineLocationResult =
6960 LocationAccessPolicy.checkLocationPermission(mApp,
6961 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6962 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006963 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006964 .setCallingPid(Binder.getCallingPid())
6965 .setCallingUid(Binder.getCallingUid())
6966 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07006967 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08006968 .setMinSdkVersionForFine(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006969 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
6970 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006971 .build());
6972
6973 LocationAccessPolicy.LocationPermissionResult coarseLocationResult =
6974 LocationAccessPolicy.checkLocationPermission(mApp,
6975 new LocationAccessPolicy.LocationPermissionQuery.Builder()
6976 .setCallingPackage(callingPackage)
Philip P. Moltmann3a2772a2019-10-04 08:15:00 -07006977 .setCallingFeatureId(callingFeatureId)
Hall Liuf19c44f2018-11-27 14:38:17 -08006978 .setCallingPid(Binder.getCallingPid())
6979 .setCallingUid(Binder.getCallingUid())
6980 .setMethod("getServiceStateForSubscriber")
Hall Liuf18a0cf2019-04-17 13:37:11 -07006981 .setLogAsInfo(true)
Hall Liuf19c44f2018-11-27 14:38:17 -08006982 .setMinSdkVersionForCoarse(Build.VERSION_CODES.Q)
Hall Liuc4a3e422020-05-26 17:18:03 -07006983 .setMinSdkVersionForFine(Integer.MAX_VALUE)
6984 .setMinSdkVersionForEnforcement(Build.VERSION_CODES.Q)
Hall Liuf19c44f2018-11-27 14:38:17 -08006985 .build());
6986 // We don't care about hard or soft here -- all we need to know is how much info to scrub.
6987 boolean hasFinePermission =
6988 fineLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
6989 boolean hasCoarsePermission =
6990 coarseLocationResult == LocationAccessPolicy.LocationPermissionResult.ALLOWED;
6991
Malcolm Chenaa4a8532018-02-28 15:00:40 -08006992 final long identity = Binder.clearCallingIdentity();
6993 try {
Jordan Liuc437b192020-08-17 10:59:12 -07006994 // isActiveSubId requires READ_PHONE_STATE, which we already check for above
6995 if (!mSubscriptionController.isActiveSubId(subId, callingPackage, callingFeatureId)) {
6996 Rlog.d(LOG_TAG,
6997 "getServiceStateForSubscriber returning null for inactive subId=" + subId);
6998 return null;
6999 }
7000
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007001 final Phone phone = getPhone(subId);
7002 if (phone == null) {
7003 return null;
7004 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007005
Hall Liuf19c44f2018-11-27 14:38:17 -08007006 ServiceState ss = phone.getServiceState();
7007
7008 // Scrub out the location info in ServiceState depending on what level of access
7009 // the caller has.
7010 if (hasFinePermission) return ss;
Malcolm Chen5052de62019-12-30 13:56:38 -08007011 if (hasCoarsePermission) return ss.createLocationInfoSanitizedCopy(false);
7012 return ss.createLocationInfoSanitizedCopy(true);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007013 } finally {
7014 Binder.restoreCallingIdentity(identity);
7015 }
Jack Yu85bd38a2015-11-09 11:34:32 -08007016 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007017
7018 /**
7019 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
7020 *
7021 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
7022 * voicemail ringtone.
7023 * @return The URI for the ringtone to play when receiving a voicemail from a specific
7024 * PhoneAccount.
7025 */
7026 @Override
7027 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007028 final long identity = Binder.clearCallingIdentity();
7029 try {
7030 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
7031 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007032 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007033 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007034
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007035 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
7036 } finally {
7037 Binder.restoreCallingIdentity(identity);
7038 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007039 }
7040
7041 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007042 * Sets the per-account voicemail ringtone.
7043 *
7044 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
7045 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7046 *
7047 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
7048 * voicemail ringtone.
7049 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
7050 * PhoneAccount.
7051 */
7052 @Override
7053 public void setVoicemailRingtoneUri(String callingPackage,
7054 PhoneAccountHandle phoneAccountHandle, Uri uri) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007055 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007056 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007057 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
7058 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007059 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7060 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
7061 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007062 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007063
7064 final long identity = Binder.clearCallingIdentity();
7065 try {
7066 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
7067 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007068 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007069 }
7070 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
7071 } finally {
7072 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007073 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007074 }
7075
7076 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08007077 * Returns whether vibration is set for voicemail notification in Phone settings.
7078 *
7079 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
7080 * voicemail vibration setting.
7081 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
7082 */
7083 @Override
7084 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007085 final long identity = Binder.clearCallingIdentity();
7086 try {
7087 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
7088 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007089 phone = getDefaultPhone();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007090 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007091
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007092 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
7093 } finally {
7094 Binder.restoreCallingIdentity(identity);
7095 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08007096 }
7097
Youhan Wange64578a2016-05-02 15:32:42 -07007098 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007099 * Sets the per-account voicemail vibration.
7100 *
7101 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
7102 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7103 *
7104 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
7105 * voicemail vibration setting.
7106 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
7107 * specific PhoneAccount.
7108 */
7109 @Override
7110 public void setVoicemailVibrationEnabled(String callingPackage,
7111 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007112 final Phone defaultPhone = getDefaultPhone();
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007113 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Tyler Gunn5ddfdc92019-10-31 13:08:23 -07007114 TelecomManager tm = defaultPhone.getContext().getSystemService(TelecomManager.class);
7115 if (!TextUtils.equals(callingPackage, tm.getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007116 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7117 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
7118 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007119 }
7120
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007121 final long identity = Binder.clearCallingIdentity();
7122 try {
7123 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
7124 if (phone == null) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007125 phone = defaultPhone;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007126 }
7127 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
7128 } finally {
7129 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007130 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08007131 }
7132
7133 /**
Youhan Wange64578a2016-05-02 15:32:42 -07007134 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
7135 *
7136 * @throws SecurityException if the caller does not have the required permission
7137 */
Brad Ebinger35c841c2018-10-01 10:40:55 -07007138 private void enforceReadPrivilegedPermission(String message) {
Youhan Wange64578a2016-05-02 15:32:42 -07007139 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Brad Ebinger35c841c2018-10-01 10:40:55 -07007140 message);
Youhan Wange64578a2016-05-02 15:32:42 -07007141 }
7142
7143 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007144 * Make sure either called from same process as self (phone) or IPC caller has send SMS
7145 * permission.
7146 *
7147 * @throws SecurityException if the caller does not have the required permission
7148 */
7149 private void enforceSendSmsPermission() {
7150 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
7151 }
7152
7153 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007154 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007155 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007156 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007157 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08007158 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007159 final long identity = Binder.clearCallingIdentity();
7160 try {
7161 ComponentName componentName =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007162 RemoteVvmTaskManager.getRemotePackage(mApp, subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007163 if (componentName == null) {
7164 throw new SecurityException(
7165 "Caller not current active visual voicemail package[null]");
7166 }
7167 String vvmPackage = componentName.getPackageName();
7168 if (!callingPackage.equals(vvmPackage)) {
7169 throw new SecurityException("Caller not current active visual voicemail package["
7170 + vvmPackage + "]");
7171 }
7172 } finally {
7173 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08007174 }
7175 }
7176
7177 /**
Youhan Wange64578a2016-05-02 15:32:42 -07007178 * Return the application ID for the app type.
7179 *
7180 * @param subId the subscription ID that this request applies to.
7181 * @param appType the uicc app type.
7182 * @return Application ID for specificied app type, or null if no uicc.
7183 */
7184 @Override
7185 public String getAidForAppType(int subId, int appType) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007186 enforceReadPrivilegedPermission("getAidForAppType");
Youhan Wange64578a2016-05-02 15:32:42 -07007187 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007188
7189 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07007190 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007191 if (phone == null) {
7192 return null;
7193 }
7194 String aid = null;
7195 try {
7196 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
7197 .getApplicationByType(appType).getAid();
7198 } catch (Exception e) {
7199 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
7200 }
7201 return aid;
7202 } finally {
7203 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07007204 }
Youhan Wange64578a2016-05-02 15:32:42 -07007205 }
7206
Youhan Wang4001d252016-05-11 10:29:41 -07007207 /**
7208 * Return the Electronic Serial Number.
7209 *
7210 * @param subId the subscription ID that this request applies to.
7211 * @return ESN or null if error.
7212 */
7213 @Override
7214 public String getEsn(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007215 enforceReadPrivilegedPermission("getEsn");
Youhan Wang4001d252016-05-11 10:29:41 -07007216 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007217
7218 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07007219 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007220 if (phone == null) {
7221 return null;
7222 }
7223 String esn = null;
7224 try {
7225 esn = phone.getEsn();
7226 } catch (Exception e) {
7227 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
7228 }
7229 return esn;
7230 } finally {
7231 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07007232 }
Youhan Wang4001d252016-05-11 10:29:41 -07007233 }
7234
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007235 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07007236 * Return the Preferred Roaming List Version.
7237 *
7238 * @param subId the subscription ID that this request applies to.
7239 * @return PRLVersion or null if error.
7240 */
7241 @Override
7242 public String getCdmaPrlVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007243 enforceReadPrivilegedPermission("getCdmaPrlVersion");
Youhan Wang66ad5d72016-07-18 17:56:58 -07007244 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007245
7246 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07007247 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007248 if (phone == null) {
7249 return null;
7250 }
7251 String cdmaPrlVersion = null;
7252 try {
7253 cdmaPrlVersion = phone.getCdmaPrlVersion();
7254 } catch (Exception e) {
7255 Log.e(LOG_TAG, "Not getting PRLVersion", e);
7256 }
7257 return cdmaPrlVersion;
7258 } finally {
7259 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07007260 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07007261 }
7262
7263 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007264 * Get snapshot of Telephony histograms
7265 * @return List of Telephony histograms
7266 * @hide
7267 */
7268 @Override
7269 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007270 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7271 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007272
7273 final long identity = Binder.clearCallingIdentity();
7274 try {
7275 return RIL.getTelephonyRILTimingHistograms();
7276 } finally {
7277 Binder.restoreCallingIdentity(identity);
7278 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07007279 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07007280
7281 /**
7282 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08007283 * Set the allowed carrier list and the excluded carrier list, indicating the priority between
7284 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007285 * Require system privileges. In the future we may add this to carrier APIs.
7286 *
Michele Berionne482f8202018-11-27 18:57:59 -08007287 * @return Integer with the result of the operation, as defined in {@link TelephonyManager}.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007288 */
7289 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08007290 @TelephonyManager.SetCarrierRestrictionResult
7291 public int setAllowedCarriers(CarrierRestrictionRules carrierRestrictionRules) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07007292 enforceModifyPermission();
vagdeviaf9a5b92018-08-15 16:01:53 -07007293 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007294
Michele Berionne482f8202018-11-27 18:57:59 -08007295 if (carrierRestrictionRules == null) {
7296 throw new NullPointerException("carrier restriction cannot be null");
Meng Wang9b7c4e92017-02-17 11:41:27 -08007297 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007298
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007299 final long identity = Binder.clearCallingIdentity();
7300 try {
Michele Berionne482f8202018-11-27 18:57:59 -08007301 return (int) sendRequest(CMD_SET_ALLOWED_CARRIERS, carrierRestrictionRules,
vagdeviaf9a5b92018-08-15 16:01:53 -07007302 workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007303 } finally {
7304 Binder.restoreCallingIdentity(identity);
7305 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07007306 }
7307
7308 /**
7309 * {@hide}
Michele Berionne482f8202018-11-27 18:57:59 -08007310 * Get the allowed carrier list and the excluded carrier list, including the priority between
7311 * the two lists.
Meng Wang1a7c35a2016-05-05 20:56:15 -07007312 * Require system privileges. In the future we may add this to carrier APIs.
7313 *
Michele Berionne482f8202018-11-27 18:57:59 -08007314 * @return {@link android.telephony.CarrierRestrictionRules}
Meng Wang1a7c35a2016-05-05 20:56:15 -07007315 */
7316 @Override
Michele Berionne482f8202018-11-27 18:57:59 -08007317 public CarrierRestrictionRules getAllowedCarriers() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007318 enforceReadPrivilegedPermission("getAllowedCarriers");
vagdeviaf9a5b92018-08-15 16:01:53 -07007319 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007320
7321 final long identity = Binder.clearCallingIdentity();
7322 try {
Michele Berionne482f8202018-11-27 18:57:59 -08007323 Object response = sendRequest(CMD_GET_ALLOWED_CARRIERS, null, workSource);
7324 if (response instanceof CarrierRestrictionRules) {
7325 return (CarrierRestrictionRules) response;
7326 }
7327 // Response is an Exception of some kind,
7328 // which is signalled to the user as a NULL retval
7329 return null;
7330 } catch (Exception e) {
7331 Log.e(LOG_TAG, "getAllowedCarriers. Exception ex=" + e);
7332 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007333 } finally {
7334 Binder.restoreCallingIdentity(identity);
7335 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07007336 }
7337
fionaxu59545b42016-05-25 15:53:37 -07007338 /**
fionaxu59545b42016-05-25 15:53:37 -07007339 * Action set from carrier signalling broadcast receivers to enable/disable radio
7340 * @param subId the subscription ID that this action applies to.
7341 * @param enabled control enable or disable radio.
7342 * {@hide}
7343 */
7344 @Override
7345 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
7346 enforceModifyPermission();
7347 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007348
7349 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07007350 if (phone == null) {
7351 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
7352 return;
7353 }
7354 try {
7355 phone.carrierActionSetRadioEnabled(enabled);
7356 } catch (Exception e) {
7357 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007358 } finally {
7359 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07007360 }
7361 }
7362
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07007363 /**
fionaxu8da9cb12017-05-23 15:02:46 -07007364 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
7365 * network status based on which carrier apps could apply actions accordingly,
7366 * enable/disable default url handler for example.
7367 *
7368 * @param subId the subscription ID that this action applies to.
7369 * @param report control start/stop reporting the default network status.
7370 * {@hide}
7371 */
7372 @Override
7373 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
7374 enforceModifyPermission();
7375 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007376
7377 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07007378 if (phone == null) {
7379 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
7380 return;
7381 }
7382 try {
7383 phone.carrierActionReportDefaultNetworkStatus(report);
7384 } catch (Exception e) {
7385 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007386 } finally {
7387 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07007388 }
7389 }
7390
7391 /**
fionaxud9622282017-07-17 17:51:30 -07007392 * Action set from carrier signalling broadcast receivers to reset all carrier actions
7393 * @param subId the subscription ID that this action applies to.
7394 * {@hide}
7395 */
7396 @Override
7397 public void carrierActionResetAll(int subId) {
7398 enforceModifyPermission();
7399 final Phone phone = getPhone(subId);
7400 if (phone == null) {
7401 loge("carrierAction: ResetAll fails with invalid sibId: " + subId);
7402 return;
7403 }
7404 try {
7405 phone.carrierActionResetAll();
7406 } catch (Exception e) {
7407 Log.e(LOG_TAG, "carrierAction: ResetAll fails. Exception ex=" + e);
7408 }
7409 }
7410
7411 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07007412 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
7413 * bug report is being generated.
7414 */
7415 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07007416 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007417 if (mApp.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
7418 != PackageManager.PERMISSION_GRANTED) {
dcashman22b950d2016-06-27 11:39:02 -07007419 writer.println("Permission Denial: can't dump Phone from pid="
7420 + Binder.getCallingPid()
7421 + ", uid=" + Binder.getCallingUid()
7422 + "without permission "
7423 + android.Manifest.permission.DUMP);
7424 return;
7425 }
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007426 DumpsysHandler.dump(mApp, fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07007427 }
Jack Yueb89b242016-06-22 13:27:47 -07007428
Brad Ebingerdac2f002018-04-03 15:17:52 -07007429 @Override
Hall Liua1548bd2019-12-24 14:14:12 -08007430 public int handleShellCommand(@NonNull ParcelFileDescriptor in,
7431 @NonNull ParcelFileDescriptor out, @NonNull ParcelFileDescriptor err,
7432 @NonNull String[] args) {
7433 return new TelephonyShellCommand(this, getDefaultPhone().getContext()).exec(
7434 this, in.getFileDescriptor(), out.getFileDescriptor(),
7435 err.getFileDescriptor(), args);
Brad Ebingerdac2f002018-04-03 15:17:52 -07007436 }
7437
Jack Yueb89b242016-06-22 13:27:47 -07007438 /**
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007439 * Policy control of data connection with reason {@@TelephonyManager.DataEnabledReason}
Greg Kaiser17f41752020-05-05 16:47:47 +00007440 * @param subId Subscription index
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007441 * @param reason the reason the data enable change is taking place
7442 * @param enabled True if enabling the data, otherwise disabling.
7443 * @hide
Jack Yu75ab2952016-07-08 14:29:33 -07007444 */
7445 @Override
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007446 public void setDataEnabledForReason(int subId, @TelephonyManager.DataEnabledReason int reason,
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007447 boolean enabled) {
7448 if (reason == TelephonyManager.DATA_ENABLED_REASON_USER
7449 || reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
7450 try {
7451 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
Sooraj Sasindran95c07a92020-07-15 01:35:24 -07007452 mApp, subId, "setDataEnabledForReason");
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007453 } catch (SecurityException se) {
7454 enforceModifyPermission();
7455 }
7456 } else {
7457 enforceModifyPermission();
7458 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007459
7460 final long identity = Binder.clearCallingIdentity();
7461 try {
7462 Phone phone = getPhone(subId);
7463 if (phone != null) {
Sooraj Sasindranaf1b5132020-05-05 21:06:20 +00007464 if (reason == TelephonyManager.DATA_ENABLED_REASON_CARRIER) {
7465 phone.carrierActionSetMeteredApnsEnabled(enabled);
7466 } else {
7467 phone.getDataEnabledSettings().setDataEnabled(reason, enabled);
7468 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007469 }
7470 } finally {
7471 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07007472 }
7473 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007474
7475 /**
7476 * Get Client request stats
7477 * @return List of Client Request Stats
7478 * @hide
7479 */
7480 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007481 public List<ClientRequestStats> getClientRequestStats(String callingPackage,
7482 String callingFeatureId, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08007483 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007484 mApp, subId, callingPackage, callingFeatureId, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007485 return null;
7486 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007487 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007488
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007489 final long identity = Binder.clearCallingIdentity();
7490 try {
7491 if (phone != null) {
7492 return phone.getClientRequestStats();
7493 }
7494
7495 return null;
7496 } finally {
7497 Binder.restoreCallingIdentity(identity);
7498 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007499 }
7500
Narayan Kamathf04b5a12018-01-09 11:47:15 +00007501 private WorkSource getWorkSource(int uid) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007502 String packageName = mApp.getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00007503 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07007504 }
Jack Yueb4124c2017-02-16 15:32:43 -08007505
7506 /**
Grace Chen70990072017-03-24 17:21:30 -07007507 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08007508 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007509 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07007510 * @param state State of SIM (power down, power up, pass through)
7511 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
7512 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
7513 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08007514 *
7515 **/
7516 @Override
Grace Chen70990072017-03-24 17:21:30 -07007517 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08007518 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07007519 Phone phone = PhoneFactory.getPhone(slotIndex);
7520
vagdeviaf9a5b92018-08-15 16:01:53 -07007521 WorkSource workSource = getWorkSource(Binder.getCallingUid());
7522
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007523 final long identity = Binder.clearCallingIdentity();
7524 try {
7525 if (phone != null) {
vagdeviaf9a5b92018-08-15 16:01:53 -07007526 phone.setSimPowerState(state, workSource);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007527 }
7528 } finally {
7529 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08007530 }
7531 }
Shuo Qiandd210312017-04-12 22:11:33 +00007532
Tyler Gunn65d45c22017-06-05 11:22:26 -07007533 private boolean isUssdApiAllowed(int subId) {
7534 CarrierConfigManager configManager =
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007535 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Tyler Gunn65d45c22017-06-05 11:22:26 -07007536 if (configManager == null) {
7537 return false;
7538 }
7539 PersistableBundle pb = configManager.getConfigForSubId(subId);
7540 if (pb == null) {
7541 return false;
7542 }
7543 return pb.getBoolean(
7544 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
7545 }
7546
Shuo Qiandd210312017-04-12 22:11:33 +00007547 /**
7548 * Check if phone is in emergency callback mode
7549 * @return true if phone is in emergency callback mode
7550 * @param subId sub id
7551 */
goneil9c5f4872017-12-05 14:07:56 -08007552 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00007553 public boolean getEmergencyCallbackMode(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007554 enforceReadPrivilegedPermission("getEmergencyCallbackMode");
Shuo Qiandd210312017-04-12 22:11:33 +00007555 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007556
7557 final long identity = Binder.clearCallingIdentity();
7558 try {
7559 if (phone != null) {
7560 return phone.isInEcm();
7561 } else {
7562 return false;
7563 }
7564 } finally {
7565 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00007566 }
7567 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007568
7569 /**
7570 * Get the current signal strength information for the given subscription.
7571 * Because this information is not updated when the device is in a low power state
7572 * it should not be relied-upon to be current.
7573 * @param subId Subscription index
7574 * @return the most recent cached signal strength info from the modem
7575 */
7576 @Override
7577 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007578 final long identity = Binder.clearCallingIdentity();
7579 try {
7580 Phone p = getPhone(subId);
7581 if (p == null) {
7582 return null;
7583 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007584
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007585 return p.getSignalStrength();
7586 } finally {
7587 Binder.restoreCallingIdentity(identity);
7588 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08007589 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007590
Pengquan Meng77b7f132018-08-22 14:49:57 -07007591 /**
Chen Xuf792fd62018-10-17 17:54:36 +00007592 * Get the current modem radio state for the given slot.
7593 * @param slotIndex slot index.
7594 * @param callingPackage the name of the package making the call.
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007595 * @param callingFeatureId The feature in the package.
Chen Xuf792fd62018-10-17 17:54:36 +00007596 * @return the current radio power state from the modem
7597 */
7598 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007599 public int getRadioPowerState(int slotIndex, String callingPackage, String callingFeatureId) {
Chen Xuf792fd62018-10-17 17:54:36 +00007600 Phone phone = PhoneFactory.getPhone(slotIndex);
7601 if (phone != null) {
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007602 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp, phone.getSubId(),
7603 callingPackage, callingFeatureId, "getRadioPowerState")) {
Chen Xuf792fd62018-10-17 17:54:36 +00007604 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
7605 }
7606
7607 final long identity = Binder.clearCallingIdentity();
7608 try {
7609 return phone.getRadioPowerState();
7610 } finally {
7611 Binder.restoreCallingIdentity(identity);
7612 }
7613 }
7614 return TelephonyManager.RADIO_POWER_UNAVAILABLE;
7615 }
7616
7617 /**
Pengquan Meng77b7f132018-08-22 14:49:57 -07007618 * Checks if data roaming is enabled on the subscription with id {@code subId}.
7619 *
7620 * <p>Requires one of the following permissions:
7621 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE},
7622 * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling app has carrier
7623 * privileges.
7624 *
7625 * @param subId subscription id
7626 * @return {@code true} if data roaming is enabled on this subscription, otherwise return
7627 * {@code false}.
7628 */
7629 @Override
7630 public boolean isDataRoamingEnabled(int subId) {
Shuo Qian093013d2020-08-13 15:42:55 -07007631 try {
7632 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
7633 null);
7634 } catch (Exception e) {
7635 TelephonyPermissions.enforeceCallingOrSelfReadPhoneStatePermissionOrCarrierPrivilege(
7636 mApp, subId, "isDataRoamingEnabled");
7637 }
Pengquan Meng44e66f12019-04-01 10:48:20 -07007638
Pengquan Menga1bb6272018-09-06 09:59:22 -07007639 boolean isEnabled = false;
7640 final long identity = Binder.clearCallingIdentity();
Pengquan Meng77b7f132018-08-22 14:49:57 -07007641 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07007642 Phone phone = getPhone(subId);
7643 isEnabled = phone != null ? phone.getDataRoamingEnabled() : false;
Pengquan Menga1bb6272018-09-06 09:59:22 -07007644 } finally {
7645 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07007646 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07007647 return isEnabled;
Pengquan Meng77b7f132018-08-22 14:49:57 -07007648 }
7649
7650
7651 /**
7652 * Enables/Disables the data roaming on the subscription with id {@code subId}.
7653 *
7654 * <p> Requires permission:
7655 * {@link android.Manifest.permission#MODIFY_PHONE_STATE} or that the calling app has carrier
7656 * privileges.
7657 *
7658 * @param subId subscription id
7659 * @param isEnabled {@code true} means enable, {@code false} means disable.
7660 */
7661 @Override
7662 public void setDataRoamingEnabled(int subId, boolean isEnabled) {
Pengquan Meng44e66f12019-04-01 10:48:20 -07007663 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7664 mApp, subId, "setDataRoamingEnabled");
7665
Pengquan Menga1bb6272018-09-06 09:59:22 -07007666 final long identity = Binder.clearCallingIdentity();
7667 try {
Pengquan Menga1bb6272018-09-06 09:59:22 -07007668 Phone phone = getPhone(subId);
7669 if (phone != null) {
7670 phone.setDataRoamingEnabled(isEnabled);
7671 }
7672 } finally {
7673 Binder.restoreCallingIdentity(identity);
Pengquan Meng77b7f132018-08-22 14:49:57 -07007674 }
7675 }
7676
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007677 @Override
Pengquan Meng6884a2c2018-10-03 12:19:13 -07007678 public boolean isManualNetworkSelectionAllowed(int subId) {
tom hsuc91afc72020-01-06 23:46:07 +08007679 TelephonyPermissions
7680 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Meng44e66f12019-04-01 10:48:20 -07007681 mApp, subId, "isManualNetworkSelectionAllowed");
7682
Pengquan Meng6884a2c2018-10-03 12:19:13 -07007683 boolean isAllowed = true;
7684 final long identity = Binder.clearCallingIdentity();
7685 try {
Pengquan Meng6884a2c2018-10-03 12:19:13 -07007686 Phone phone = getPhone(subId);
7687 if (phone != null) {
7688 isAllowed = phone.isCspPlmnEnabled();
7689 }
7690 } finally {
7691 Binder.restoreCallingIdentity(identity);
7692 }
7693 return isAllowed;
7694 }
7695
7696 @Override
Jordan Liu75f43ea2019-01-17 16:56:37 -08007697 public List<UiccCardInfo> getUiccCardsInfo(String callingPackage) {
Jordan Liu4cda4552020-03-23 11:55:07 -07007698 // Verify that tha callingPackage belongs to the calling UID
7699 mApp.getSystemService(AppOpsManager.class)
7700 .checkPackage(Binder.getCallingUid(), callingPackage);
7701
Jordan Liu1e142fc2019-04-22 15:10:43 -07007702 boolean hasReadPermission = false;
Jordan Liuc65bc952019-02-12 17:54:02 -08007703 try {
7704 enforceReadPrivilegedPermission("getUiccCardsInfo");
Jordan Liu1e142fc2019-04-22 15:10:43 -07007705 hasReadPermission = true;
Jordan Liuc65bc952019-02-12 17:54:02 -08007706 } catch (SecurityException e) {
7707 // even without READ_PRIVILEGED_PHONE_STATE, we allow the call to continue if the caller
7708 // has carrier privileges on an active UICC
7709 if (checkCarrierPrivilegesForPackageAnyPhone(callingPackage)
7710 != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Jordan Liu1e142fc2019-04-22 15:10:43 -07007711 throw new SecurityException("Caller does not have permission.");
Jordan Liuc65bc952019-02-12 17:54:02 -08007712 }
Jordan Liu75f43ea2019-01-17 16:56:37 -08007713 }
Jordan Liu5aa07002018-12-18 15:44:48 -08007714
7715 final long identity = Binder.clearCallingIdentity();
7716 try {
Jordan Liu75f43ea2019-01-17 16:56:37 -08007717 UiccController uiccController = UiccController.getInstance();
7718 ArrayList<UiccCardInfo> cardInfos = uiccController.getAllUiccCardInfos();
Jordan Liu1e142fc2019-04-22 15:10:43 -07007719 if (hasReadPermission) {
7720 return cardInfos;
Jordan Liu75f43ea2019-01-17 16:56:37 -08007721 }
Jordan Liu1e142fc2019-04-22 15:10:43 -07007722
7723 // Remove private info if the caller doesn't have access
7724 ArrayList<UiccCardInfo> filteredInfos = new ArrayList<>();
7725 for (UiccCardInfo cardInfo : cardInfos) {
7726 // For an inactive eUICC, the UiccCard will be null even though the UiccCardInfo
7727 // is available
7728 UiccCard card = uiccController.getUiccCardForSlot(cardInfo.getSlotIndex());
7729 if (card == null || card.getUiccProfile() == null) {
7730 // assume no access if the card or profile is unavailable
7731 filteredInfos.add(cardInfo.getUnprivileged());
7732 continue;
7733 }
7734 UiccProfile profile = card.getUiccProfile();
7735 if (profile.getCarrierPrivilegeStatus(mApp.getPackageManager(), callingPackage)
7736 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
7737 filteredInfos.add(cardInfo);
7738 } else {
7739 filteredInfos.add(cardInfo.getUnprivileged());
7740 }
7741 }
7742 return filteredInfos;
Jordan Liu5aa07002018-12-18 15:44:48 -08007743 } finally {
7744 Binder.restoreCallingIdentity(identity);
7745 }
7746 }
7747
7748 @Override
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007749 public UiccSlotInfo[] getUiccSlotsInfo() {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007750 enforceReadPrivilegedPermission("getUiccSlotsInfo");
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007751
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007752 final long identity = Binder.clearCallingIdentity();
7753 try {
7754 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
7755 if (slots == null) {
7756 Rlog.i(LOG_TAG, "slots is null.");
7757 return null;
7758 }
7759
7760 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
7761 for (int i = 0; i < slots.length; i++) {
7762 UiccSlot slot = slots[i];
7763 if (slot == null) {
7764 continue;
7765 }
7766
Jordan Liu7be7e652019-05-06 18:55:02 +00007767 String cardId;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007768 UiccCard card = slot.getUiccCard();
7769 if (card != null) {
7770 cardId = card.getCardId();
Jordan Liu7be7e652019-05-06 18:55:02 +00007771 } else {
Jordan Liu01bd00d2019-09-12 16:19:43 -07007772 cardId = slot.getEid();
7773 if (TextUtils.isEmpty(cardId)) {
7774 cardId = slot.getIccId();
7775 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007776 }
7777
Jordan Liu857451f2019-05-09 16:35:35 -07007778 if (cardId != null) {
7779 // if cardId is an ICCID, strip off trailing Fs before exposing to user
7780 // if cardId is an EID, it's all digits so this is fine
7781 cardId = IccUtils.stripTrailingFs(cardId);
7782 }
7783
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007784 int cardState = 0;
7785 switch (slot.getCardState()) {
7786 case CARDSTATE_ABSENT:
7787 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
7788 break;
7789 case CARDSTATE_PRESENT:
7790 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
7791 break;
7792 case CARDSTATE_ERROR:
7793 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
7794 break;
7795 case CARDSTATE_RESTRICTED:
7796 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
7797 break;
7798 default:
7799 break;
7800
7801 }
7802
7803 infos[i] = new UiccSlotInfo(
7804 slot.isActive(),
7805 slot.isEuicc(),
7806 cardId,
7807 cardState,
7808 slot.getPhoneId(),
Jordan Liua2619582019-02-14 12:56:40 -08007809 slot.isExtendedApduSupported(),
7810 slot.isRemovable());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007811 }
7812 return infos;
7813 } finally {
7814 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07007815 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007816 }
7817
7818 @Override
7819 public boolean switchSlots(int[] physicalSlots) {
7820 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007821
7822 final long identity = Binder.clearCallingIdentity();
7823 try {
7824 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
7825 } finally {
7826 Binder.restoreCallingIdentity(identity);
7827 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00007828 }
Jack Yu4c988042018-02-27 15:30:01 -08007829
7830 @Override
Jordan Liu7de49fa2018-12-06 14:48:49 -08007831 public int getCardIdForDefaultEuicc(int subId, String callingPackage) {
Jordan Liu7de49fa2018-12-06 14:48:49 -08007832 final long identity = Binder.clearCallingIdentity();
7833 try {
7834 return UiccController.getInstance().getCardIdForDefaultEuicc();
7835 } finally {
7836 Binder.restoreCallingIdentity(identity);
7837 }
7838 }
7839
Pengquan Meng85728fb2018-03-12 16:31:21 -07007840 /**
goneil47ffb6e2018-04-06 15:40:58 -07007841 * A test API to reload the UICC profile.
7842 *
7843 * <p>Requires that the calling app has permission
7844 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
7845 * @hide
7846 */
7847 @Override
7848 public void refreshUiccProfile(int subId) {
7849 enforceModifyPermission();
7850
7851 final long identity = Binder.clearCallingIdentity();
7852 try {
7853 Phone phone = getPhone(subId);
7854 if (phone == null) {
7855 return;
7856 }
7857 UiccCard uiccCard = phone.getUiccCard();
7858 if (uiccCard == null) {
7859 return;
7860 }
7861 UiccProfile uiccProfile = uiccCard.getUiccProfile();
7862 if (uiccProfile == null) {
7863 return;
7864 }
7865 uiccProfile.refresh();
7866 } finally {
7867 Binder.restoreCallingIdentity(identity);
7868 }
7869 }
7870
7871 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07007872 * Returns false if the mobile data is disabled by default, otherwise return true.
7873 */
7874 private boolean getDefaultDataEnabled() {
Inseob Kim14bb3d02018-12-13 17:11:34 +09007875 return TelephonyProperties.mobile_data().orElse(true);
Pengquan Meng85728fb2018-03-12 16:31:21 -07007876 }
7877
7878 /**
7879 * Returns true if the data roaming is enabled by default, i.e the system property
7880 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
7881 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
7882 */
7883 private boolean getDefaultDataRoamingEnabled(int subId) {
7884 final CarrierConfigManager configMgr = (CarrierConfigManager)
Nazanin Bakhshif782e562018-12-11 15:15:39 -08007885 mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
Shuo Qian1d84a0e2020-07-15 12:36:44 -07007886 boolean isDataRoamingEnabled = TelephonyProperties.data_roaming().orElse(false);
Pengquan Meng85728fb2018-03-12 16:31:21 -07007887 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
7888 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
7889 return isDataRoamingEnabled;
7890 }
7891
7892 /**
7893 * Returns the default network type for the given {@code subId}, if the default network type is
7894 * not set, return {@link Phone#PREFERRED_NT_MODE}.
7895 */
7896 private int getDefaultNetworkType(int subId) {
Inseob Kim14bb3d02018-12-13 17:11:34 +09007897 List<Integer> list = TelephonyProperties.default_network();
7898 int phoneId = mSubscriptionController.getPhoneId(subId);
7899 if (phoneId >= 0 && phoneId < list.size() && list.get(phoneId) != null) {
7900 return list.get(phoneId);
7901 }
7902 return Phone.PREFERRED_NT_MODE;
Pengquan Meng85728fb2018-03-12 16:31:21 -07007903 }
fionaxua13278b2018-03-21 00:08:13 -07007904
7905 @Override
7906 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
chen xueaba88a2019-03-15 13:15:10 -07007907 gid1, String gid2, String plmn, String spn, String carrierPrivilegeRules, String apn) {
fionaxua13278b2018-03-21 00:08:13 -07007908 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007909
7910 final long identity = Binder.clearCallingIdentity();
7911 try {
7912 final Phone phone = getPhone(subId);
7913 if (phone == null) {
7914 loge("setCarrierTestOverride fails with invalid subId: " + subId);
7915 return;
7916 }
chen xueaba88a2019-03-15 13:15:10 -07007917 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn,
7918 carrierPrivilegeRules, apn);
Jeff Davidson8ab02b22020-03-28 12:24:40 -07007919 if (carrierPrivilegeRules == null) {
7920 mCarrierPrivilegeTestOverrideSubIds.remove(subId);
7921 } else {
7922 mCarrierPrivilegeTestOverrideSubIds.add(subId);
7923 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007924 } finally {
7925 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07007926 }
fionaxua13278b2018-03-21 00:08:13 -07007927 }
7928
7929 @Override
7930 public int getCarrierIdListVersion(int subId) {
Brad Ebinger35c841c2018-10-01 10:40:55 -07007931 enforceReadPrivilegedPermission("getCarrierIdListVersion");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08007932
7933 final long identity = Binder.clearCallingIdentity();
7934 try {
7935 final Phone phone = getPhone(subId);
7936 if (phone == null) {
7937 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
7938 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
7939 }
7940 return phone.getCarrierIdListVersion();
7941 } finally {
7942 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07007943 }
fionaxua13278b2018-03-21 00:08:13 -07007944 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07007945
7946 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007947 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage,
7948 String callingFeatureId) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07007949 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07007950 mApp, subId, callingPackage, callingFeatureId,
7951 "getNumberOfModemsWithSimultaneousDataConnections")) {
Malcolm Chen2c63d402018-08-14 16:00:53 -07007952 return -1;
7953 }
7954
7955 final long identity = Binder.clearCallingIdentity();
7956 try {
7957 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
7958 } finally {
7959 Binder.restoreCallingIdentity(identity);
7960 }
7961 }
Pengquan Menga1bb6272018-09-06 09:59:22 -07007962
7963 @Override
7964 public int getCdmaRoamingMode(int subId) {
zoey chen7e6d4e52019-12-17 18:18:59 +08007965 TelephonyPermissions
7966 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
Pengquan Menga1bb6272018-09-06 09:59:22 -07007967 mApp, subId, "getCdmaRoamingMode");
7968
7969 final long identity = Binder.clearCallingIdentity();
7970 try {
7971 return (int) sendRequest(CMD_GET_CDMA_ROAMING_MODE, null /* argument */, subId);
7972 } finally {
7973 Binder.restoreCallingIdentity(identity);
7974 }
7975 }
7976
7977 @Override
7978 public boolean setCdmaRoamingMode(int subId, int mode) {
7979 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7980 mApp, subId, "setCdmaRoamingMode");
7981
7982 final long identity = Binder.clearCallingIdentity();
7983 try {
7984 return (boolean) sendRequest(CMD_SET_CDMA_ROAMING_MODE, mode, subId);
7985 } finally {
7986 Binder.restoreCallingIdentity(identity);
7987 }
7988 }
7989
7990 @Override
7991 public boolean setCdmaSubscriptionMode(int subId, int mode) {
7992 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
7993 mApp, subId, "setCdmaSubscriptionMode");
7994
7995 final long identity = Binder.clearCallingIdentity();
7996 try {
7997 return (boolean) sendRequest(CMD_SET_CDMA_SUBSCRIPTION_MODE, mode, subId);
7998 } finally {
7999 Binder.restoreCallingIdentity(identity);
8000 }
8001 }
Makoto Onukida3bf792018-09-18 16:06:29 -07008002
sqianc5eccab2018-10-19 18:46:41 -07008003 @Override
sqian8c685422019-02-22 15:55:18 -08008004 public Map<Integer, List<EmergencyNumber>> getEmergencyNumberList(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008005 String callingPackage, String callingFeatureId) {
sqian11b7a0e2018-12-05 18:48:28 -08008006 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008007 mApp, getDefaultSubscription(), callingPackage, callingFeatureId,
8008 "getEmergencyNumberList")) {
sqian11b7a0e2018-12-05 18:48:28 -08008009 throw new SecurityException("Requires READ_PHONE_STATE permission.");
8010 }
8011 final long identity = Binder.clearCallingIdentity();
8012 try {
sqian854d44b2018-12-12 16:48:18 -08008013 Map<Integer, List<EmergencyNumber>> emergencyNumberListInternal = new HashMap<>();
8014 for (Phone phone: PhoneFactory.getPhones()) {
8015 if (phone.getEmergencyNumberTracker() != null
8016 && phone.getEmergencyNumberTracker().getEmergencyNumberList() != null) {
8017 emergencyNumberListInternal.put(
8018 phone.getSubId(),
8019 phone.getEmergencyNumberTracker().getEmergencyNumberList());
8020 }
sqian11b7a0e2018-12-05 18:48:28 -08008021 }
sqian854d44b2018-12-12 16:48:18 -08008022 return emergencyNumberListInternal;
sqian11b7a0e2018-12-05 18:48:28 -08008023 } finally {
8024 Binder.restoreCallingIdentity(identity);
8025 }
sqianc5eccab2018-10-19 18:46:41 -07008026 }
8027
8028 @Override
sqian8c685422019-02-22 15:55:18 -08008029 public boolean isEmergencyNumber(String number, boolean exactMatch) {
Nazanin Bakhshif782e562018-12-11 15:15:39 -08008030 final Phone defaultPhone = getDefaultPhone();
sqian11b7a0e2018-12-05 18:48:28 -08008031 if (!exactMatch) {
8032 TelephonyPermissions
8033 .enforeceCallingOrSelfReadPrivilegedPhoneStatePermissionOrCarrierPrivilege(
sqian8c685422019-02-22 15:55:18 -08008034 mApp, defaultPhone.getSubId(), "isEmergencyNumber(Potential)");
sqian11b7a0e2018-12-05 18:48:28 -08008035 }
8036 final long identity = Binder.clearCallingIdentity();
8037 try {
sqian854d44b2018-12-12 16:48:18 -08008038 for (Phone phone: PhoneFactory.getPhones()) {
8039 if (phone.getEmergencyNumberTracker() != null
8040 && phone.getEmergencyNumberTracker() != null) {
8041 if (phone.getEmergencyNumberTracker().isEmergencyNumber(
8042 number, exactMatch)) {
8043 return true;
sqian11b7a0e2018-12-05 18:48:28 -08008044 }
8045 }
sqian11b7a0e2018-12-05 18:48:28 -08008046 }
8047 return false;
8048 } finally {
8049 Binder.restoreCallingIdentity(identity);
8050 }
8051 }
8052
sqianf4ca7ed2019-01-15 18:32:07 -08008053 /**
8054 * Update emergency number list for test mode.
8055 */
8056 @Override
8057 public void updateEmergencyNumberListTestMode(int action, EmergencyNumber num) {
8058 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
8059 "updateEmergencyNumberListTestMode");
8060
8061 final long identity = Binder.clearCallingIdentity();
8062 try {
8063 for (Phone phone: PhoneFactory.getPhones()) {
8064 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8065 if (tracker != null) {
8066 tracker.executeEmergencyNumberTestModeCommand(action, num);
8067 }
8068 }
8069 } finally {
8070 Binder.restoreCallingIdentity(identity);
8071 }
8072 }
8073
8074 /**
8075 * Get the full emergency number list for test mode.
8076 */
8077 @Override
8078 public List<String> getEmergencyNumberListTestMode() {
8079 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(),
8080 "getEmergencyNumberListTestMode");
8081
8082 final long identity = Binder.clearCallingIdentity();
8083 try {
8084 Set<String> emergencyNumbers = new HashSet<>();
8085 for (Phone phone: PhoneFactory.getPhones()) {
8086 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8087 if (tracker != null) {
8088 for (EmergencyNumber num : tracker.getEmergencyNumberList()) {
8089 emergencyNumbers.add(num.getNumber());
8090 }
8091 }
8092 }
8093 return new ArrayList<>(emergencyNumbers);
8094 } finally {
8095 Binder.restoreCallingIdentity(identity);
8096 }
8097 }
8098
chen xud6b45bd2018-10-30 22:27:10 -07008099 @Override
Shuo Qian3b6ee772019-11-13 17:43:31 -08008100 public int getEmergencyNumberDbVersion(int subId) {
8101 enforceReadPrivilegedPermission("getEmergencyNumberDbVersion");
8102
8103 final long identity = Binder.clearCallingIdentity();
8104 try {
8105 final Phone phone = getPhone(subId);
8106 if (phone == null) {
8107 loge("getEmergencyNumberDbVersion fails with invalid subId: " + subId);
8108 return TelephonyManager.INVALID_EMERGENCY_NUMBER_DB_VERSION;
8109 }
8110 return phone.getEmergencyNumberDbVersion();
8111 } finally {
8112 Binder.restoreCallingIdentity(identity);
8113 }
8114 }
8115
8116 @Override
8117 public void notifyOtaEmergencyNumberDbInstalled() {
8118 enforceModifyPermission();
8119
8120 final long identity = Binder.clearCallingIdentity();
8121 try {
8122 for (Phone phone: PhoneFactory.getPhones()) {
8123 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8124 if (tracker != null) {
8125 tracker.updateOtaEmergencyNumberDatabase();
8126 }
8127 }
8128 } finally {
8129 Binder.restoreCallingIdentity(identity);
8130 }
8131 }
8132
8133 @Override
Shuo Qianc373f112020-03-05 17:55:34 -08008134 public void updateOtaEmergencyNumberDbFilePath(ParcelFileDescriptor otaParcelFileDescriptor) {
Shuo Qian3b6ee772019-11-13 17:43:31 -08008135 enforceActiveEmergencySessionPermission();
8136
8137 final long identity = Binder.clearCallingIdentity();
8138 try {
8139 for (Phone phone: PhoneFactory.getPhones()) {
8140 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8141 if (tracker != null) {
Shuo Qianc373f112020-03-05 17:55:34 -08008142 tracker.updateOtaEmergencyNumberDbFilePath(otaParcelFileDescriptor);
8143 }
8144 }
8145 } finally {
8146 Binder.restoreCallingIdentity(identity);
8147 }
8148 }
8149
8150 @Override
8151 public void resetOtaEmergencyNumberDbFilePath() {
8152 enforceActiveEmergencySessionPermission();
8153
8154 final long identity = Binder.clearCallingIdentity();
8155 try {
8156 for (Phone phone: PhoneFactory.getPhones()) {
8157 EmergencyNumberTracker tracker = phone.getEmergencyNumberTracker();
8158 if (tracker != null) {
8159 tracker.resetOtaEmergencyNumberDbFilePath();
Shuo Qian3b6ee772019-11-13 17:43:31 -08008160 }
8161 }
8162 } finally {
8163 Binder.restoreCallingIdentity(identity);
8164 }
8165 }
8166
8167 @Override
chen xud6b45bd2018-10-30 22:27:10 -07008168 public List<String> getCertsFromCarrierPrivilegeAccessRules(int subId) {
8169 enforceReadPrivilegedPermission("getCertsFromCarrierPrivilegeAccessRules");
8170 Phone phone = getPhone(subId);
8171 if (phone == null) {
8172 return null;
8173 }
8174 final long identity = Binder.clearCallingIdentity();
8175 try {
8176 UiccProfile profile = UiccController.getInstance()
8177 .getUiccProfileForPhone(phone.getPhoneId());
8178 if (profile != null) {
8179 return profile.getCertsFromCarrierPrivilegeAccessRules();
8180 }
8181 } finally {
8182 Binder.restoreCallingIdentity(identity);
8183 }
8184 return null;
8185 }
Malcolm Chen8e4ed912019-01-15 20:22:16 -08008186
8187 /**
8188 * Enable or disable a modem stack.
8189 */
8190 @Override
8191 public boolean enableModemForSlot(int slotIndex, boolean enable) {
8192 enforceModifyPermission();
8193
8194 final long identity = Binder.clearCallingIdentity();
8195 try {
8196 Phone phone = PhoneFactory.getPhone(slotIndex);
8197 if (phone == null) {
8198 return false;
8199 } else {
8200 return (Boolean) sendRequest(CMD_REQUEST_ENABLE_MODEM, enable, phone, null);
8201 }
8202 } finally {
8203 Binder.restoreCallingIdentity(identity);
8204 }
8205 }
Michelecea4cf22018-12-21 15:00:11 -08008206
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008207 /**
8208 * Whether a modem stack is enabled or not.
8209 */
8210 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008211 public boolean isModemEnabledForSlot(int slotIndex, String callingPackage,
8212 String callingFeatureId) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008213 Phone phone = PhoneFactory.getPhone(slotIndex);
8214 if (phone == null) return false;
8215
8216 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008217 mApp, phone.getSubId(), callingPackage, callingFeatureId,
8218 "isModemEnabledForSlot")) {
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008219 throw new SecurityException("Requires READ_PHONE_STATE permission.");
8220 }
8221
8222 final long identity = Binder.clearCallingIdentity();
8223 try {
Nazanin Bakhshif71371d2019-04-29 17:29:44 -07008224 try {
8225 return mPhoneConfigurationManager.getPhoneStatusFromCache(phone.getPhoneId());
8226 } catch (NoSuchElementException ex) {
8227 return (Boolean) sendRequest(CMD_GET_MODEM_STATUS, null, phone, null);
8228 }
Malcolm Chen4bcd9822019-03-27 18:34:05 -07008229 } finally {
8230 Binder.restoreCallingIdentity(identity);
8231 }
8232 }
8233
Michelecea4cf22018-12-21 15:00:11 -08008234 @Override
Michele0ea7d782019-03-19 14:58:42 -07008235 public void setMultiSimCarrierRestriction(boolean isMultiSimCarrierRestricted) {
Michelecea4cf22018-12-21 15:00:11 -08008236 enforceModifyPermission();
8237
8238 final long identity = Binder.clearCallingIdentity();
8239 try {
8240 mTelephonySharedPreferences.edit()
Michele0ea7d782019-03-19 14:58:42 -07008241 .putBoolean(PREF_MULTI_SIM_RESTRICTED, isMultiSimCarrierRestricted)
Michelecea4cf22018-12-21 15:00:11 -08008242 .commit();
8243 } finally {
8244 Binder.restoreCallingIdentity(identity);
8245 }
8246 }
8247
8248 @Override
Michele0ea7d782019-03-19 14:58:42 -07008249 @TelephonyManager.IsMultiSimSupportedResult
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008250 public int isMultiSimSupported(String callingPackage, String callingFeatureId) {
Michele4245e952019-02-04 11:36:23 -08008251 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(mApp,
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008252 getDefaultPhone().getSubId(), callingPackage, callingFeatureId,
8253 "isMultiSimSupported")) {
Michele0ea7d782019-03-19 14:58:42 -07008254 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele4245e952019-02-04 11:36:23 -08008255 }
Michelecea4cf22018-12-21 15:00:11 -08008256
8257 final long identity = Binder.clearCallingIdentity();
8258 try {
Michele0ea7d782019-03-19 14:58:42 -07008259 return isMultiSimSupportedInternal();
Michelecea4cf22018-12-21 15:00:11 -08008260 } finally {
8261 Binder.restoreCallingIdentity(identity);
8262 }
8263 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008264
Michele0ea7d782019-03-19 14:58:42 -07008265 @TelephonyManager.IsMultiSimSupportedResult
8266 private int isMultiSimSupportedInternal() {
Michele30b57b22019-03-01 12:01:14 -08008267 // If the device has less than 2 SIM cards, indicate that multisim is restricted.
8268 int numPhysicalSlots = UiccController.getInstance().getUiccSlots().length;
8269 if (numPhysicalSlots < 2) {
Michele0ea7d782019-03-19 14:58:42 -07008270 loge("isMultiSimSupportedInternal: requires at least 2 cards");
8271 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08008272 }
8273 // Check if the hardware supports multisim functionality. If usage of multisim is not
8274 // supported by the modem, indicate that it is restricted.
8275 PhoneCapability staticCapability =
8276 mPhoneConfigurationManager.getStaticPhoneCapability();
8277 if (staticCapability == null) {
Michele0ea7d782019-03-19 14:58:42 -07008278 loge("isMultiSimSupportedInternal: no static configuration available");
8279 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08008280 }
Sarah Chin7caee492020-02-18 20:49:02 +00008281 if (staticCapability.logicalModemList.size() < 2) {
Michele0ea7d782019-03-19 14:58:42 -07008282 loge("isMultiSimSupportedInternal: maximum number of modem is < 2");
8283 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_HARDWARE;
Michele30b57b22019-03-01 12:01:14 -08008284 }
8285 // Check if support of multiple SIMs is restricted by carrier
8286 if (mTelephonySharedPreferences.getBoolean(PREF_MULTI_SIM_RESTRICTED, false)) {
Michele0ea7d782019-03-19 14:58:42 -07008287 return TelephonyManager.MULTISIM_NOT_SUPPORTED_BY_CARRIER;
Michele30b57b22019-03-01 12:01:14 -08008288 }
8289
Michele0ea7d782019-03-19 14:58:42 -07008290 return TelephonyManager.MULTISIM_ALLOWED;
Michele30b57b22019-03-01 12:01:14 -08008291 }
8292
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008293 /**
8294 * Switch configs to enable multi-sim or switch back to single-sim
Nazanin Bakhshi17318782019-03-01 11:56:08 -08008295 * Note: Switch from multi-sim to single-sim is only possible with MODIFY_PHONE_STATE
8296 * permission, but the other way around is possible with either MODIFY_PHONE_STATE
8297 * or carrier privileges
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008298 * @param numOfSims number of active sims we want to switch to
8299 */
8300 @Override
8301 public void switchMultiSimConfig(int numOfSims) {
Nazanin Bakhshi17318782019-03-01 11:56:08 -08008302 if (numOfSims == 1) {
8303 enforceModifyPermission();
8304 } else {
8305 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8306 mApp, SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, "switchMultiSimConfig");
8307 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008308 final long identity = Binder.clearCallingIdentity();
Michele30b57b22019-03-01 12:01:14 -08008309
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008310 try {
Michele30b57b22019-03-01 12:01:14 -08008311 //only proceed if multi-sim is not restricted
Michele0ea7d782019-03-19 14:58:42 -07008312 if (isMultiSimSupportedInternal() != TelephonyManager.MULTISIM_ALLOWED) {
Michele30b57b22019-03-01 12:01:14 -08008313 loge("switchMultiSimConfig not possible. It is restricted or not supported.");
8314 return;
8315 }
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008316 mPhoneConfigurationManager.switchMultiSimConfig(numOfSims);
8317 } finally {
8318 Binder.restoreCallingIdentity(identity);
8319 }
8320 }
8321
Hyungjun Parkbb07fde2019-01-10 15:28:51 +09008322 @Override
8323 public boolean isApplicationOnUicc(int subId, int appType) {
8324 enforceReadPrivilegedPermission("isApplicationOnUicc");
8325 Phone phone = getPhone(subId);
8326 if (phone == null) {
8327 return false;
8328 }
8329 final long identity = Binder.clearCallingIdentity();
8330 try {
8331 UiccCard uiccCard = phone.getUiccCard();
8332 if (uiccCard == null) {
8333 return false;
8334 }
8335 UiccProfile uiccProfile = uiccCard.getUiccProfile();
8336 if (uiccProfile == null) {
8337 return false;
8338 }
8339 if (TelephonyManager.APPTYPE_SIM <= appType
8340 && appType <= TelephonyManager.APPTYPE_ISIM) {
8341 return uiccProfile.isApplicationOnIcc(AppType.values()[appType]);
8342 }
8343 return false;
8344 } finally {
8345 Binder.restoreCallingIdentity(identity);
8346 }
8347 }
8348
Nazanin Bakhshi628473f2019-01-29 17:37:52 -08008349 /**
chen xub4baa772019-04-03 10:23:41 -07008350 * Get whether making changes to modem configurations will trigger reboot.
8351 * Return value defaults to true.
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08008352 */
8353 @Override
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008354 public boolean doesSwitchMultiSimConfigTriggerReboot(int subId, String callingPackage,
8355 String callingFeatureId) {
chen xub4baa772019-04-03 10:23:41 -07008356 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Philip P. Moltmann700a9592019-10-03 11:53:50 -07008357 mApp, subId, callingPackage, callingFeatureId,
8358 "doesSwitchMultiSimConfigTriggerReboot")) {
chen xub4baa772019-04-03 10:23:41 -07008359 return false;
8360 }
Nazanin Bakhshi5fe5ef22019-01-30 10:52:09 -08008361 final long identity = Binder.clearCallingIdentity();
8362 try {
8363 return mPhoneConfigurationManager.isRebootRequiredForModemConfigChange();
8364 } finally {
8365 Binder.restoreCallingIdentity(identity);
8366 }
8367 }
8368
Nathan Harold29f5f052019-02-15 13:41:57 -08008369 private void updateModemStateMetrics() {
8370 TelephonyMetrics metrics = TelephonyMetrics.getInstance();
8371 // TODO: check the state for each modem if the api is ready.
8372 metrics.updateEnabledModemBitmap((1 << TelephonyManager.from(mApp).getPhoneCount()) - 1);
8373 }
8374
Pengquan Meng3889a572019-01-23 11:16:29 -08008375 @Override
8376 public int[] getSlotsMapping() {
8377 enforceReadPrivilegedPermission("getSlotsMapping");
8378
8379 final long identity = Binder.clearCallingIdentity();
8380 try {
8381 int phoneCount = TelephonyManager.getDefault().getPhoneCount();
8382 // All logical slots should have a mapping to a physical slot.
8383 int[] logicalSlotsMapping = new int[phoneCount];
8384 UiccSlotInfo[] slotInfos = getUiccSlotsInfo();
8385 for (int i = 0; i < slotInfos.length; i++) {
8386 if (SubscriptionManager.isValidPhoneId(slotInfos[i].getLogicalSlotIdx())) {
8387 logicalSlotsMapping[slotInfos[i].getLogicalSlotIdx()] = i;
8388 }
8389 }
8390 return logicalSlotsMapping;
8391 } finally {
8392 Binder.restoreCallingIdentity(identity);
8393 }
8394 }
Nathan Harold48d6fd52019-02-06 19:01:40 -08008395
8396 /**
8397 * Get the IRadio HAL Version
8398 */
8399 @Override
8400 public int getRadioHalVersion() {
8401 Phone phone = getDefaultPhone();
8402 if (phone == null) return -1;
8403 HalVersion hv = phone.getHalVersion();
8404 if (hv.equals(HalVersion.UNKNOWN)) return -1;
8405 return hv.major * 100 + hv.minor;
8406 }
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008407
8408 /**
Shuo Qianda2d6ec2020-01-14 15:18:28 -08008409 * Get the current calling package name.
8410 * @return the current calling package name
8411 */
8412 @Override
8413 public String getCurrentPackageName() {
8414 return mApp.getPackageManager().getPackagesForUid(Binder.getCallingUid())[0];
8415 }
8416
8417 /**
Malcolm Chene5ad5792019-04-18 13:51:02 -07008418 * Return whether data is enabled for certain APN type. This will tell if framework will accept
8419 * corresponding network requests on a subId.
8420 *
8421 * Data is enabled if:
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008422 * 1) user data is turned on, or
Malcolm Chene5ad5792019-04-18 13:51:02 -07008423 * 2) APN is un-metered for this subscription, or
8424 * 3) APN type is whitelisted. E.g. MMS is whitelisted if
changbettyd5c246e2019-12-24 15:40:37 +08008425 * {@link TelephonyManager#setAlwaysAllowMmsData} is turned on.
Malcolm Chene5ad5792019-04-18 13:51:02 -07008426 *
8427 * @return whether data is allowed for a apn type.
8428 *
8429 * @hide
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008430 */
8431 @Override
Malcolm Chene5ad5792019-04-18 13:51:02 -07008432 public boolean isDataEnabledForApn(int apnType, int subId, String callingPackage) {
Amit Mahajan5d4e1922019-10-07 16:20:43 -07008433 enforceReadPrivilegedPermission("Needs READ_PRIVILEGED_PHONE_STATE for "
8434 + "isDataEnabledForApn");
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008435
8436 // Now that all security checks passes, perform the operation as ourselves.
8437 final long identity = Binder.clearCallingIdentity();
8438 try {
8439 Phone phone = getPhone(subId);
8440 if (phone == null) return false;
8441
Jack Yu41407ee2019-05-13 16:54:09 -07008442 boolean isMetered = ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chene5ad5792019-04-18 13:51:02 -07008443 return !isMetered || phone.getDataEnabledSettings().isDataEnabled(apnType);
8444 } finally {
8445 Binder.restoreCallingIdentity(identity);
8446 }
8447 }
8448
8449 @Override
Jack Yu41407ee2019-05-13 16:54:09 -07008450 public boolean isApnMetered(@ApnType int apnType, int subId) {
Malcolm Chene5ad5792019-04-18 13:51:02 -07008451 enforceReadPrivilegedPermission("isApnMetered");
8452
8453 // Now that all security checks passes, perform the operation as ourselves.
8454 final long identity = Binder.clearCallingIdentity();
8455 try {
8456 Phone phone = getPhone(subId);
8457 if (phone == null) return true; // By default return true.
8458
Jack Yu41407ee2019-05-13 16:54:09 -07008459 return ApnSettingUtils.isMeteredApnType(apnType, phone);
Malcolm Chendc8c10e2019-04-10 18:25:07 -07008460 } finally {
8461 Binder.restoreCallingIdentity(identity);
8462 }
8463 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07008464
8465 @Override
Hall Liu73f5d362020-01-20 13:42:00 -08008466 public void setSystemSelectionChannels(List<RadioAccessSpecifier> specifiers,
8467 int subscriptionId, IBooleanConsumer resultCallback) {
8468 enforceModifyPermission();
8469 long token = Binder.clearCallingIdentity();
8470 try {
8471 Phone phone = getPhone(subscriptionId);
8472 if (phone == null) {
8473 try {
8474 if (resultCallback != null) {
8475 resultCallback.accept(false);
8476 }
8477 } catch (RemoteException e) {
8478 // ignore
8479 }
8480 return;
8481 }
8482 Pair<List<RadioAccessSpecifier>, Consumer<Boolean>> argument =
8483 Pair.create(specifiers, (x) -> {
8484 try {
8485 if (resultCallback != null) {
8486 resultCallback.accept(x);
8487 }
8488 } catch (RemoteException e) {
8489 // ignore
8490 }
8491 });
8492 sendRequestAsync(CMD_SET_SYSTEM_SELECTION_CHANNELS, argument, phone, null);
8493 } finally {
8494 Binder.restoreCallingIdentity(token);
8495 }
8496 }
8497
8498 @Override
changbetty7157e9e2019-12-06 18:16:37 +08008499 public boolean isMvnoMatched(int subId, int mvnoType, @NonNull String mvnoMatchData) {
changbettyca3d40d2020-03-03 16:27:31 +08008500 enforceReadPrivilegedPermission("isMvnoMatched");
changbetty7157e9e2019-12-06 18:16:37 +08008501 IccRecords iccRecords = UiccController.getInstance().getIccRecords(
8502 SubscriptionManager.getPhoneId(subId), UiccController.APP_FAM_3GPP);
8503 if (iccRecords == null) {
8504 Log.d(LOG_TAG, "isMvnoMatched# IccRecords is null");
8505 return false;
8506 }
8507 return ApnSettingUtils.mvnoMatches(iccRecords, mvnoType, mvnoMatchData);
8508 }
8509
8510 @Override
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07008511 public void enqueueSmsPickResult(String callingPackage, String callingAttributionTag,
8512 IIntegerConsumer pendingSubIdResult) {
Shuo Qianda2d6ec2020-01-14 15:18:28 -08008513 if (callingPackage == null) {
8514 callingPackage = getCurrentPackageName();
8515 }
Brad Ebingera63db5f2019-04-23 16:31:13 -07008516 SmsPermissions permissions = new SmsPermissions(getDefaultPhone(), mApp,
8517 (AppOpsManager) mApp.getSystemService(Context.APP_OPS_SERVICE));
Philip P. Moltmannd02b7372020-03-18 17:06:12 -07008518 if (!permissions.checkCallingCanSendSms(callingPackage, callingAttributionTag,
8519 "Sending message")) {
Brad Ebingera63db5f2019-04-23 16:31:13 -07008520 throw new SecurityException("Requires SEND_SMS permission to perform this operation");
8521 }
8522 PickSmsSubscriptionActivity.addPendingResult(pendingSubIdResult);
8523 Intent intent = new Intent();
8524 intent.setClass(mApp, PickSmsSubscriptionActivity.class);
8525 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
8526 // Bring up choose default SMS subscription dialog right now
8527 intent.putExtra(PickSmsSubscriptionActivity.DIALOG_TYPE_KEY,
8528 PickSmsSubscriptionActivity.SMS_PICK_FOR_MESSAGE);
8529 mApp.startActivity(intent);
8530 }
chen xud5ca2d52019-05-28 15:20:57 -07008531
8532 @Override
8533 public String getMmsUAProfUrl(int subId) {
8534 //TODO investigate if this API should require proper permission check in R b/133791609
8535 final long identity = Binder.clearCallingIdentity();
8536 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08008537 String carrierUAProfUrl = mApp.getCarrierConfigForSubId(subId).getString(
8538 CarrierConfigManager.KEY_MMS_UA_PROF_URL_STRING);
8539 if (!TextUtils.isEmpty(carrierUAProfUrl)) {
8540 return carrierUAProfUrl;
8541 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08008542 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
8543 .getString(com.android.internal.R.string.config_mms_user_agent_profile_url);
chen xud5ca2d52019-05-28 15:20:57 -07008544 } finally {
8545 Binder.restoreCallingIdentity(identity);
8546 }
8547 }
8548
8549 @Override
8550 public String getMmsUserAgent(int subId) {
8551 //TODO investigate if this API should require proper permission check in R b/133791609
8552 final long identity = Binder.clearCallingIdentity();
8553 try {
Guoqiang.Qiu171383d2020-07-13 09:38:32 +08008554 String carrierUserAgent = mApp.getCarrierConfigForSubId(subId).getString(
8555 CarrierConfigManager.KEY_MMS_USER_AGENT_STRING);
8556 if (!TextUtils.isEmpty(carrierUserAgent)) {
8557 return carrierUserAgent;
8558 }
Daniel Brightebb4eb72020-02-18 15:16:33 -08008559 return SubscriptionManager.getResourcesForSubId(getDefaultPhone().getContext(), subId)
8560 .getString(com.android.internal.R.string.config_mms_user_agent);
chen xud5ca2d52019-05-28 15:20:57 -07008561 } finally {
8562 Binder.restoreCallingIdentity(identity);
8563 }
8564 }
Jack Yub07d4972019-05-28 16:12:25 -07008565
8566 @Override
8567 public boolean setDataAllowedDuringVoiceCall(int subId, boolean allow) {
8568 enforceModifyPermission();
8569
8570 // Now that all security checks passes, perform the operation as ourselves.
8571 final long identity = Binder.clearCallingIdentity();
8572 try {
8573 Phone phone = getPhone(subId);
8574 if (phone == null) return false;
8575
8576 return phone.getDataEnabledSettings().setAllowDataDuringVoiceCall(allow);
8577 } finally {
8578 Binder.restoreCallingIdentity(identity);
8579 }
8580 }
8581
8582 @Override
8583 public boolean isDataAllowedInVoiceCall(int subId) {
8584 enforceReadPrivilegedPermission("isDataAllowedInVoiceCall");
8585
8586 // Now that all security checks passes, perform the operation as ourselves.
8587 final long identity = Binder.clearCallingIdentity();
8588 try {
8589 Phone phone = getPhone(subId);
8590 if (phone == null) return false;
8591
8592 return phone.getDataEnabledSettings().isDataAllowedInVoiceCall();
8593 } finally {
8594 Binder.restoreCallingIdentity(identity);
8595 }
8596 }
Tyler Gunn7bcdc742019-10-04 15:56:59 -07008597
changbettyd5c246e2019-12-24 15:40:37 +08008598 @Override
8599 public boolean setAlwaysAllowMmsData(int subId, boolean alwaysAllow) {
8600 enforceModifyPermission();
8601
8602 // Now that all security checks passes, perform the operation as ourselves.
8603 final long identity = Binder.clearCallingIdentity();
8604 try {
8605 Phone phone = getPhone(subId);
8606 if (phone == null) return false;
8607
8608 return phone.getDataEnabledSettings().setAlwaysAllowMmsData(alwaysAllow);
8609 } finally {
8610 Binder.restoreCallingIdentity(identity);
8611 }
8612 }
8613
Tyler Gunn7bcdc742019-10-04 15:56:59 -07008614 /**
8615 * Updates whether conference event pacakge handling is enabled.
8616 * @param isCepEnabled {@code true} if CEP handling is enabled (default), or {@code false}
8617 * otherwise.
8618 */
8619 @Override
8620 public void setCepEnabled(boolean isCepEnabled) {
8621 TelephonyPermissions.enforceShellOnly(Binder.getCallingUid(), "setCepEnabled");
8622
8623 final long identity = Binder.clearCallingIdentity();
8624 try {
8625 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled);
8626 for (Phone phone : PhoneFactory.getPhones()) {
8627 Phone defaultPhone = phone.getImsPhone();
8628 if (defaultPhone != null && defaultPhone.getPhoneType() == PHONE_TYPE_IMS) {
8629 ImsPhone imsPhone = (ImsPhone) defaultPhone;
8630 ImsPhoneCallTracker imsPhoneCallTracker =
8631 (ImsPhoneCallTracker) imsPhone.getCallTracker();
8632 imsPhoneCallTracker.setConferenceEventPackageEnabled(isCepEnabled);
8633 Rlog.i(LOG_TAG, "setCepEnabled isCepEnabled=" + isCepEnabled + ", for imsPhone "
8634 + imsPhone.getMsisdn());
8635 }
8636 }
8637 } finally {
8638 Binder.restoreCallingIdentity(identity);
8639 }
8640 }
allenwtsu46dcc572020-01-08 18:24:03 +08008641
8642 /**
8643 * Notify that an RCS autoconfiguration XML file has been received for provisioning.
8644 *
8645 * @param config The XML file to be read. ASCII/UTF8 encoded text if not compressed.
8646 * @param isCompressed The XML file is compressed in gzip format and must be decompressed
8647 * before being read.
8648 */
8649 @Override
8650 public void notifyRcsAutoConfigurationReceived(int subId, @NonNull byte[] config, boolean
8651 isCompressed) {
8652 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
8653 mApp, subId, "notifyRcsAutoConfigurationReceived");
8654 try {
8655 IImsConfig configBinder = getImsConfig(getSlotIndex(subId), ImsFeature.FEATURE_RCS);
8656 if (configBinder == null) {
8657 Rlog.e(LOG_TAG, "null result for getImsConfig");
8658 } else {
8659 configBinder.notifyRcsAutoConfigurationReceived(config, isCompressed);
8660 }
8661 } catch (RemoteException e) {
8662 Rlog.e(LOG_TAG, "fail to getImsConfig " + e.getMessage());
8663 }
8664 }
zoey chene02881a2019-12-30 16:11:23 +08008665
8666 @Override
8667 public boolean isIccLockEnabled(int subId) {
8668 enforceReadPrivilegedPermission("isIccLockEnabled");
8669
8670 // Now that all security checks passes, perform the operation as ourselves.
8671 final long identity = Binder.clearCallingIdentity();
8672 try {
8673 Phone phone = getPhone(subId);
8674 if (phone != null && phone.getIccCard() != null) {
8675 return phone.getIccCard().getIccLockEnabled();
8676 } else {
8677 return false;
8678 }
8679 } finally {
8680 Binder.restoreCallingIdentity(identity);
8681 }
8682 }
8683
8684 /**
8685 * Set the ICC pin lock enabled or disabled.
8686 *
8687 * @return an integer representing the status of IccLock enabled or disabled in the following
8688 * three cases:
8689 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if enabled or disabled IccLock
8690 * successfully.
8691 * - Positive number and zero for remaining password attempts.
8692 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
8693 *
8694 */
8695 @Override
8696 public int setIccLockEnabled(int subId, boolean enabled, String password) {
8697 enforceModifyPermission();
8698
8699 Phone phone = getPhone(subId);
8700 if (phone == null) {
8701 return 0;
8702 }
8703 // Now that all security checks passes, perform the operation as ourselves.
8704 final long identity = Binder.clearCallingIdentity();
8705 try {
8706 int attemptsRemaining = (int) sendRequest(CMD_SET_ICC_LOCK_ENABLED,
8707 new Pair<Boolean, String>(enabled, password), phone, null);
8708 return attemptsRemaining;
8709
8710 } catch (Exception e) {
8711 Log.e(LOG_TAG, "setIccLockEnabled. Exception e =" + e);
8712 } finally {
8713 Binder.restoreCallingIdentity(identity);
8714 }
8715 return 0;
8716 }
8717
8718 /**
8719 * Change the ICC password used in ICC pin lock.
8720 *
8721 * @return an integer representing the status of IccLock changed in the following three cases:
8722 * - {@link TelephonyManager#CHANGE_ICC_LOCK_SUCCESS} if changed IccLock successfully.
8723 * - Positive number and zero for remaining password attempts.
8724 * - Negative number for other failure cases (such like enabling/disabling PIN failed).
8725 *
8726 */
8727 @Override
8728 public int changeIccLockPassword(int subId, String oldPassword, String newPassword) {
8729 enforceModifyPermission();
8730
8731 Phone phone = getPhone(subId);
8732 if (phone == null) {
8733 return 0;
8734 }
8735 // Now that all security checks passes, perform the operation as ourselves.
8736 final long identity = Binder.clearCallingIdentity();
8737 try {
8738 int attemptsRemaining = (int) sendRequest(CMD_CHANGE_ICC_LOCK_PASSWORD,
8739 new Pair<String, String>(oldPassword, newPassword), phone, null);
8740 return attemptsRemaining;
8741
8742 } catch (Exception e) {
8743 Log.e(LOG_TAG, "changeIccLockPassword. Exception e =" + e);
8744 } finally {
8745 Binder.restoreCallingIdentity(identity);
8746 }
8747 return 0;
8748 }
Peter Wangdafb9ac2020-01-15 14:13:38 -08008749
8750 /**
8751 * Request for receiving user activity notification
8752 */
8753 @Override
8754 public void requestUserActivityNotification() {
8755 if (!mNotifyUserActivity.get()
8756 && !mMainThreadHandler.hasMessages(MSG_NOTIFY_USER_ACTIVITY)) {
8757 mNotifyUserActivity.set(true);
8758 }
8759 }
8760
8761 /**
8762 * Called when userActivity is signalled in the power manager.
8763 * This is safe to call from any thread, with any window manager locks held or not.
8764 */
8765 @Override
8766 public void userActivity() {
8767 // ***************************************
8768 // * Inherited from PhoneWindowManager *
8769 // ***************************************
8770 // THIS IS CALLED FROM DEEP IN THE POWER MANAGER
8771 // WITH ITS LOCKS HELD.
8772 //
8773 // This code must be VERY careful about the locks
8774 // it acquires.
8775 // In fact, the current code acquires way too many,
8776 // and probably has lurking deadlocks.
8777
8778 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
8779 throw new SecurityException("Only the OS may call notifyUserActivity()");
8780 }
8781
8782 if (mNotifyUserActivity.getAndSet(false)) {
8783 mMainThreadHandler.sendEmptyMessageDelayed(MSG_NOTIFY_USER_ACTIVITY,
8784 USER_ACTIVITY_NOTIFICATION_DELAY);
8785 }
8786 }
Malcolm Chen4639c562020-04-13 11:59:40 -07008787
8788 @Override
8789 public boolean canConnectTo5GInDsdsMode() {
8790 return mApp.getResources().getBoolean(R.bool.config_5g_connection_in_dsds_mode);
8791 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07008792}