blob: a1d837a6c18158839cf36debb7711f9585ca1e24 [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
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Ta-wei Yen30a69c82016-12-27 14:52:32 -080021import android.Manifest.permission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080023import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070024import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070025import android.content.Context;
26import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070027import android.content.SharedPreferences;
Derek Tan740e1672017-06-27 14:56:27 -070028import android.content.pm.ComponentInfo;
Amith Yamasani6e118872016-02-19 12:53:51 -080029import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070030import android.content.pm.PackageManager;
Jack Yu84291ec2017-05-26 16:07:50 -070031import android.net.NetworkStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.net.Uri;
33import android.os.AsyncResult;
34import android.os.Binder;
35import android.os.Bundle;
36import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070037import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Looper;
39import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070040import android.os.Messenger;
Tyler Gunn65d45c22017-06-05 11:22:26 -070041import android.os.PersistableBundle;
Brad Ebinger5f64b052017-12-14 14:26:15 -080042import android.os.RemoteException;
Adam Lesinski903a54c2016-04-11 14:49:52 -070043import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070044import android.os.ServiceManager;
Brad Ebingerdac2f002018-04-03 15:17:52 -070045import android.os.ShellCallback;
Pengquan Meng85728fb2018-03-12 16:31:21 -070046import android.os.SystemProperties;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070048import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070049import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070050import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080051import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070052import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080053import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080054import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070055import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070056import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.telephony.CellInfo;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070058import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070059import android.telephony.IccOpenLogicalChannelResponse;
Hall Liu1aa510f2017-11-22 17:40:08 -080060import android.telephony.LocationAccessPolicy;
Ta-wei Yen87c49842016-05-13 21:19:52 -070061import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080062import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070063import android.telephony.NetworkScanRequest;
Wink Saville5d475dd2014-10-17 15:00:58 -070064import android.telephony.RadioAccessFamily;
Tyler Gunn65d45c22017-06-05 11:22:26 -070065import android.telephony.Rlog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080067import android.telephony.SignalStrength;
Jack Yu84291ec2017-05-26 16:07:50 -070068import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080069import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080070import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070071import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070072import android.telephony.TelephonyManager;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +000073import android.telephony.UiccSlotInfo;
Tyler Gunn65d45c22017-06-05 11:22:26 -070074import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -070075import android.telephony.VisualVoicemailSmsFilterSettings;
Brad Ebinger22bc3e42018-01-16 09:39:35 -080076import android.telephony.ims.aidl.IImsConfig;
77import android.telephony.ims.aidl.IImsMmTelFeature;
78import android.telephony.ims.aidl.IImsRcsFeature;
79import android.telephony.ims.aidl.IImsRegistration;
Brad Ebinger1f2b5082018-02-08 16:11:32 -080080import android.telephony.ims.stub.ImsRegistrationImplBase;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080082import android.util.ArraySet;
Tyler Gunn67073572018-02-14 14:19:42 -080083import android.util.EventLog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070084import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080085import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080086import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080087
Andrew Lee312e8172014-10-23 17:01:36 -070088import com.android.ims.ImsManager;
Brad Ebinger34bef922017-11-09 10:27:08 -080089import com.android.ims.internal.IImsServiceFeatureCallback;
Shishir Agrawal566b7612013-10-28 14:41:00 -070090import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -070091import com.android.internal.telephony.CallStateException;
pkanwar79ec0542017-07-31 14:10:01 -070092import com.android.internal.telephony.CarrierInfoManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070093import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070094import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070096import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080097import com.android.internal.telephony.IccCard;
Jack Yu5f7092c2018-04-13 14:05:37 -070098import com.android.internal.telephony.LocaleTracker;
Narayan Kamath1c496c22015-04-16 14:40:19 +010099import com.android.internal.telephony.MccTable;
yinxub1bed742017-04-17 11:45:04 -0700100import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700101import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102import com.android.internal.telephony.Phone;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700103import com.android.internal.telephony.PhoneConfigurationManager;
Nathan Harolda667c152016-12-14 11:27:20 -0800104import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -0700105import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -0700106import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -0700107import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -0700108import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -0700109import com.android.internal.telephony.RILConstants;
Jack Yu5f7092c2018-04-13 14:05:37 -0700110import com.android.internal.telephony.ServiceStateTracker;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800111import com.android.internal.telephony.SubscriptionController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800112import com.android.internal.telephony.TelephonyPermissions;
Derek Tan740e1672017-06-27 14:56:27 -0700113import com.android.internal.telephony.euicc.EuiccConnector;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700114import com.android.internal.telephony.uicc.IccIoResult;
115import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -0800116import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700117import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -0800118import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700119import com.android.internal.telephony.uicc.UiccController;
Jeff Davidson7e17e312018-02-13 18:17:36 -0800120import com.android.internal.telephony.uicc.UiccProfile;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000121import com.android.internal.telephony.uicc.UiccSlot;
fionaxu7ed723d2017-05-30 18:58:54 -0700122import com.android.internal.telephony.util.VoicemailNotificationSettingsUtil;
Jake Hambye994d462014-02-03 13:10:13 -0800123import com.android.internal.util.HexDump;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700124import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800125import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700126import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700127import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800128
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700129import java.io.FileDescriptor;
130import java.io.PrintWriter;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800131import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700132import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800133import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800134import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100135import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800136import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700137
138/**
139 * Implementation of the ITelephony interface.
140 */
Santos Cordon117fee72014-05-16 17:56:12 -0700141public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700142 private static final String LOG_TAG = "PhoneInterfaceManager";
143 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
144 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800145 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700146
147 // Message codes used with mMainThreadHandler
148 private static final int CMD_HANDLE_PIN_MMI = 1;
149 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
150 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
151 private static final int CMD_ANSWER_RINGING_CALL = 4;
152 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700153 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
154 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700155 private static final int CMD_OPEN_CHANNEL = 9;
156 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
157 private static final int CMD_CLOSE_CHANNEL = 11;
158 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800159 private static final int CMD_NV_READ_ITEM = 13;
160 private static final int EVENT_NV_READ_ITEM_DONE = 14;
161 private static final int CMD_NV_WRITE_ITEM = 15;
162 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
163 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
164 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
165 private static final int CMD_NV_RESET_CONFIG = 19;
166 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800167 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
168 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
169 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
170 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800171 private static final int CMD_SEND_ENVELOPE = 25;
172 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000173 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
174 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
Derek Tan6b088ee2014-09-05 14:15:18 -0700175 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
176 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
177 private static final int CMD_EXCHANGE_SIM_IO = 31;
178 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800179 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
180 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700181 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
182 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700183 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
184 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700185 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
186 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
187 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
188 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700189 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
190 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
191 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
192 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700193 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800194 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
195 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000196 private static final int CMD_SWITCH_SLOTS = 50;
197 private static final int EVENT_SWITCH_SLOTS_DONE = 51;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700198
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -0800199 // Parameters of select command.
200 private static final int SELECT_COMMAND = 0xA4;
201 private static final int SELECT_P1 = 0x04;
202 private static final int SELECT_P2 = 0;
203 private static final int SELECT_P3 = 0x10;
204
Pengquan Meng85728fb2018-03-12 16:31:21 -0700205 private static final String DEFAULT_NETWORK_MODE_PROPERTY_NAME = "ro.telephony.default_network";
206 private static final String DEFAULT_DATA_ROAMING_PROPERTY_NAME = "ro.com.android.dataroaming";
207 private static final String DEFAULT_MOBILE_DATA_PROPERTY_NAME = "ro.com.android.mobiledata";
208
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209 /** The singleton instance. */
210 private static PhoneInterfaceManager sInstance;
211
Wink Saville3ab207e2014-11-20 13:07:20 -0800212 private PhoneGlobals mApp;
213 private Phone mPhone;
214 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700215 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800216 private AppOpsManager mAppOps;
217 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800218 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800219 private SharedPreferences mTelephonySharedPreferences;
Malcolm Chen2c63d402018-08-14 16:00:53 -0700220 private PhoneConfigurationManager mPhoneConfigurationManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221
Derek Tan97ebb422014-09-05 16:55:38 -0700222 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
223 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800224 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700225
Derek Tan740e1672017-06-27 14:56:27 -0700226 // The AID of ISD-R.
227 private static final String ISDR_AID = "A0000005591010FFFFFFFF8900000100";
228
yinxub1bed742017-04-17 11:45:04 -0700229 private NetworkScanRequestTracker mNetworkScanRequestTracker;
230
David Kelly5e06a7f2018-03-12 14:10:59 +0000231 private static final int TYPE_ALLOCATION_CODE_LENGTH = 8;
232 private static final int MANUFACTURER_CODE_LENGTH = 8;
233
Derek Tan89e89d42014-07-08 17:00:10 -0700234 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700235 * A request object to use for transmitting data to an ICC.
236 */
237 private static final class IccAPDUArgument {
238 public int channel, cla, command, p1, p2, p3;
239 public String data;
240
241 public IccAPDUArgument(int channel, int cla, int command,
242 int p1, int p2, int p3, String data) {
243 this.channel = channel;
244 this.cla = cla;
245 this.command = command;
246 this.p1 = p1;
247 this.p2 = p2;
248 this.p3 = p3;
249 this.data = data;
250 }
251 }
252
253 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700254 * A request object to use for transmitting data to an ICC.
255 */
256 private static final class ManualNetworkSelectionArgument {
257 public OperatorInfo operatorInfo;
258 public boolean persistSelection;
259
260 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
261 this.operatorInfo = operatorInfo;
262 this.persistSelection = persistSelection;
263 }
264 }
265
266 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700267 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
268 * request after sending. The main thread will notify the request when it is complete.
269 */
270 private static final class MainThreadRequest {
271 /** The argument to use for the request */
272 public Object argument;
273 /** The result of the request that is run on the main thread */
274 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800275 // The subscriber id that this request applies to. Defaults to
276 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
277 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700278
279 public MainThreadRequest(Object argument) {
280 this.argument = argument;
281 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800282
283 public MainThreadRequest(Object argument, Integer subId) {
284 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800285 if (subId != null) {
286 this.subId = subId;
287 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800288 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 }
290
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800291 private static final class IncomingThirdPartyCallArgs {
292 public final ComponentName component;
293 public final String callId;
294 public final String callerDisplayName;
295
296 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
297 String callerDisplayName) {
298 this.component = component;
299 this.callId = callId;
300 this.callerDisplayName = callerDisplayName;
301 }
302 }
303
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 /**
305 * A handler that processes messages on the main thread in the phone process. Since many
306 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
307 * inbound binder threads to the main thread in the phone process. The Binder thread
308 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
309 * on, which will be notified when the operation completes and will contain the result of the
310 * request.
311 *
312 * <p>If a MainThreadRequest object is provided in the msg.obj field,
313 * note that request.result must be set to something non-null for the calling thread to
314 * unblock.
315 */
316 private final class MainThreadHandler extends Handler {
317 @Override
318 public void handleMessage(Message msg) {
319 MainThreadRequest request;
320 Message onCompleted;
321 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800322 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700323 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700324
325 switch (msg.what) {
pkanwar32d516d2016-10-14 19:37:38 -0700326 case CMD_HANDLE_USSD_REQUEST: {
327 request = (MainThreadRequest) msg.obj;
328 final Phone phone = getPhoneFromRequest(request);
329 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
330 String ussdRequest = ussdObject.first;
331 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700332
333 if (!isUssdApiAllowed(request.subId)) {
334 // Carrier does not support use of this API, return failure.
335 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
336 UssdResponse response = new UssdResponse(ussdRequest, null);
337 Bundle returnData = new Bundle();
338 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
339 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
340
341 request.result = true;
342 synchronized (request) {
343 request.notifyAll();
344 }
345 return;
346 }
347
Tyler Gunn52dcf772017-04-26 11:30:31 -0700348 try {
349 request.result = phone != null ?
350 phone.handleUssdRequest(ussdRequest, wrappedCallback)
351 : false;
352 } catch (CallStateException cse) {
353 request.result = false;
354 }
pkanwar32d516d2016-10-14 19:37:38 -0700355 // Wake up the requesting thread
356 synchronized (request) {
357 request.notifyAll();
358 }
359 break;
360 }
361
Yorke Lee716f67e2015-06-17 15:39:16 -0700362 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700363 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700364 final Phone phone = getPhoneFromRequest(request);
365 request.result = phone != null ?
366 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
367 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700368 // Wake up the requesting thread
369 synchronized (request) {
370 request.notifyAll();
371 }
372 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700373 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700374
375 case CMD_HANDLE_NEIGHBORING_CELL:
376 request = (MainThreadRequest) msg.obj;
377 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
378 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700379 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700380 break;
381
382 case EVENT_NEIGHBORING_CELL_DONE:
383 ar = (AsyncResult) msg.obj;
384 request = (MainThreadRequest) ar.userObj;
385 if (ar.exception == null && ar.result != null) {
386 request.result = ar.result;
387 } else {
388 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800389 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700390 }
391 // Wake up the requesting thread
392 synchronized (request) {
393 request.notifyAll();
394 }
395 break;
396
397 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700398 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800399 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700400 answerRingingCallInternal(answer_subId);
Mengjun Lengb3369682017-10-19 18:39:20 +0800401 request.result = ""; // dummy result for notifying the waiting thread
402 // Wake up the requesting thread
403 synchronized (request) {
404 request.notifyAll();
405 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700406 break;
407
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700408 case CMD_END_CALL:
409 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800410 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700411 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700412 Phone phone = getPhone(end_subId);
413 if (phone == null) {
414 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
415 break;
416 }
417 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700418 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
419 // CDMA: If the user presses the Power button we treat it as
420 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700421 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700422 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
423 // GSM: End the call as per the Phone state
424 hungUp = PhoneUtils.hangup(mCM);
425 } else {
426 throw new IllegalStateException("Unexpected phone type: " + phoneType);
427 }
428 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
429 request.result = hungUp;
430 // Wake up the requesting thread
431 synchronized (request) {
432 request.notifyAll();
433 }
434 break;
435
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700436 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700437 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700438 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800439 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700440 if (uiccCard == null) {
441 loge("iccTransmitApduLogicalChannel: No UICC");
442 request.result = new IccIoResult(0x6F, 0, (byte[])null);
443 synchronized (request) {
444 request.notifyAll();
445 }
446 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700447 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
448 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700449 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700450 iccArgument.channel, iccArgument.cla, iccArgument.command,
451 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700452 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700453 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700454 break;
455
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700456 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700457 ar = (AsyncResult) msg.obj;
458 request = (MainThreadRequest) ar.userObj;
459 if (ar.exception == null && ar.result != null) {
460 request.result = ar.result;
461 } else {
462 request.result = new IccIoResult(0x6F, 0, (byte[])null);
463 if (ar.result == null) {
464 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800465 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700466 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800467 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700468 } else {
469 loge("iccTransmitApduLogicalChannel: Unknown exception");
470 }
471 }
472 synchronized (request) {
473 request.notifyAll();
474 }
475 break;
476
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700477 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
478 request = (MainThreadRequest) msg.obj;
479 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800480 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700481 if (uiccCard == null) {
482 loge("iccTransmitApduBasicChannel: No UICC");
483 request.result = new IccIoResult(0x6F, 0, (byte[])null);
484 synchronized (request) {
485 request.notifyAll();
486 }
487 } else {
488 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
489 request);
490 uiccCard.iccTransmitApduBasicChannel(
491 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
492 iccArgument.p3, iccArgument.data, onCompleted);
493 }
494 break;
495
496 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
497 ar = (AsyncResult) msg.obj;
498 request = (MainThreadRequest) ar.userObj;
499 if (ar.exception == null && ar.result != null) {
500 request.result = ar.result;
501 } else {
502 request.result = new IccIoResult(0x6F, 0, (byte[])null);
503 if (ar.result == null) {
504 loge("iccTransmitApduBasicChannel: Empty response");
505 } else if (ar.exception instanceof CommandException) {
506 loge("iccTransmitApduBasicChannel: CommandException: " +
507 ar.exception);
508 } else {
509 loge("iccTransmitApduBasicChannel: Unknown exception");
510 }
511 }
512 synchronized (request) {
513 request.notifyAll();
514 }
515 break;
516
517 case CMD_EXCHANGE_SIM_IO:
518 request = (MainThreadRequest) msg.obj;
519 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800520 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700521 if (uiccCard == null) {
522 loge("iccExchangeSimIO: No UICC");
523 request.result = new IccIoResult(0x6F, 0, (byte[])null);
524 synchronized (request) {
525 request.notifyAll();
526 }
527 } else {
528 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
529 request);
530 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
531 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
532 iccArgument.data, onCompleted);
533 }
534 break;
535
536 case EVENT_EXCHANGE_SIM_IO_DONE:
537 ar = (AsyncResult) msg.obj;
538 request = (MainThreadRequest) ar.userObj;
539 if (ar.exception == null && ar.result != null) {
540 request.result = ar.result;
541 } else {
542 request.result = new IccIoResult(0x6f, 0, (byte[])null);
543 }
544 synchronized (request) {
545 request.notifyAll();
546 }
547 break;
548
Derek Tan4d5e5c12014-02-04 11:54:58 -0800549 case CMD_SEND_ENVELOPE:
550 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800551 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700552 if (uiccCard == null) {
553 loge("sendEnvelopeWithStatus: No UICC");
554 request.result = new IccIoResult(0x6F, 0, (byte[])null);
555 synchronized (request) {
556 request.notifyAll();
557 }
558 } else {
559 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
560 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
561 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800562 break;
563
564 case EVENT_SEND_ENVELOPE_DONE:
565 ar = (AsyncResult) msg.obj;
566 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700567 if (ar.exception == null && ar.result != null) {
568 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800569 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700570 request.result = new IccIoResult(0x6F, 0, (byte[])null);
571 if (ar.result == null) {
572 loge("sendEnvelopeWithStatus: Empty response");
573 } else if (ar.exception instanceof CommandException) {
574 loge("sendEnvelopeWithStatus: CommandException: " +
575 ar.exception);
576 } else {
577 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
578 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800579 }
580 synchronized (request) {
581 request.notifyAll();
582 }
583 break;
584
Shishir Agrawal566b7612013-10-28 14:41:00 -0700585 case CMD_OPEN_CHANNEL:
586 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800587 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800588 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700589 if (uiccCard == null) {
590 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800591 request.result = new IccOpenLogicalChannelResponse(-1,
592 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700593 synchronized (request) {
594 request.notifyAll();
595 }
596 } else {
597 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800598 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
599 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700600 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700601 break;
602
603 case EVENT_OPEN_CHANNEL_DONE:
604 ar = (AsyncResult) msg.obj;
605 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700606 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700607 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700608 int[] result = (int[]) ar.result;
609 int channelId = result[0];
610 byte[] selectResponse = null;
611 if (result.length > 1) {
612 selectResponse = new byte[result.length - 1];
613 for (int i = 1; i < result.length; ++i) {
614 selectResponse[i - 1] = (byte) result[i];
615 }
616 }
617 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700618 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700619 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700620 if (ar.result == null) {
621 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700622 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700623 if (ar.exception != null) {
624 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
625 }
626
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700627 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700628 if (ar.exception instanceof CommandException) {
629 CommandException.Error error =
630 ((CommandException) (ar.exception)).getCommandError();
631 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700632 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700633 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700634 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700635 }
636 }
637 openChannelResp = new IccOpenLogicalChannelResponse(
638 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700639 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700640 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700641 synchronized (request) {
642 request.notifyAll();
643 }
644 break;
645
646 case CMD_CLOSE_CHANNEL:
647 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800648 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700649 if (uiccCard == null) {
650 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900651 request.result = false;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700652 synchronized (request) {
653 request.notifyAll();
654 }
655 } else {
656 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
657 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
658 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700659 break;
660
661 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800662 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
663 break;
664
665 case CMD_NV_READ_ITEM:
666 request = (MainThreadRequest) msg.obj;
667 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
668 mPhone.nvReadItem((Integer) request.argument, onCompleted);
669 break;
670
671 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700672 ar = (AsyncResult) msg.obj;
673 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800674 if (ar.exception == null && ar.result != null) {
675 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700676 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800677 request.result = "";
678 if (ar.result == null) {
679 loge("nvReadItem: Empty response");
680 } else if (ar.exception instanceof CommandException) {
681 loge("nvReadItem: CommandException: " +
682 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700683 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800684 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700685 }
686 }
687 synchronized (request) {
688 request.notifyAll();
689 }
690 break;
691
Jake Hambye994d462014-02-03 13:10:13 -0800692 case CMD_NV_WRITE_ITEM:
693 request = (MainThreadRequest) msg.obj;
694 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
695 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
696 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
697 break;
698
699 case EVENT_NV_WRITE_ITEM_DONE:
700 handleNullReturnEvent(msg, "nvWriteItem");
701 break;
702
703 case CMD_NV_WRITE_CDMA_PRL:
704 request = (MainThreadRequest) msg.obj;
705 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
706 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
707 break;
708
709 case EVENT_NV_WRITE_CDMA_PRL_DONE:
710 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
711 break;
712
713 case CMD_NV_RESET_CONFIG:
714 request = (MainThreadRequest) msg.obj;
715 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
716 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
717 break;
718
719 case EVENT_NV_RESET_CONFIG_DONE:
720 handleNullReturnEvent(msg, "nvResetConfig");
721 break;
722
Jake Hamby7c27be32014-03-03 13:25:59 -0800723 case CMD_GET_PREFERRED_NETWORK_TYPE:
724 request = (MainThreadRequest) msg.obj;
725 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700726 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800727 break;
728
729 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
730 ar = (AsyncResult) msg.obj;
731 request = (MainThreadRequest) ar.userObj;
732 if (ar.exception == null && ar.result != null) {
733 request.result = ar.result; // Integer
734 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800735 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800736 if (ar.result == null) {
737 loge("getPreferredNetworkType: Empty response");
738 } else if (ar.exception instanceof CommandException) {
739 loge("getPreferredNetworkType: CommandException: " +
740 ar.exception);
741 } else {
742 loge("getPreferredNetworkType: Unknown exception");
743 }
744 }
745 synchronized (request) {
746 request.notifyAll();
747 }
748 break;
749
750 case CMD_SET_PREFERRED_NETWORK_TYPE:
751 request = (MainThreadRequest) msg.obj;
752 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
753 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700754 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800755 break;
756
757 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
758 handleNullReturnEvent(msg, "setPreferredNetworkType");
759 break;
760
Shuo Qian850e4d6a2018-04-25 21:02:08 +0000761 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
762 request = (MainThreadRequest)msg.obj;
763 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
764 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
765 break;
766
767 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
768 ar = (AsyncResult)msg.obj;
769 request = (MainThreadRequest)ar.userObj;
770 request.result = ar;
771 synchronized (request) {
772 request.notifyAll();
773 }
774 break;
775
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800776 case CMD_SET_VOICEMAIL_NUMBER:
777 request = (MainThreadRequest) msg.obj;
778 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
779 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800780 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
781 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800782 break;
783
784 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
785 handleNullReturnEvent(msg, "setVoicemailNumber");
786 break;
787
Stuart Scott54788802015-03-30 13:18:01 -0700788 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
789 request = (MainThreadRequest) msg.obj;
790 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
791 request);
792 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
793 break;
794
795 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
796 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
797 break;
798
Shishir Agrawal302c8692015-06-19 13:49:39 -0700799 case CMD_PERFORM_NETWORK_SCAN:
800 request = (MainThreadRequest) msg.obj;
801 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
802 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
803 break;
804
805 case EVENT_PERFORM_NETWORK_SCAN_DONE:
806 ar = (AsyncResult) msg.obj;
807 request = (MainThreadRequest) ar.userObj;
808 CellNetworkScanResult cellScanResult;
809 if (ar.exception == null && ar.result != null) {
810 cellScanResult = new CellNetworkScanResult(
811 CellNetworkScanResult.STATUS_SUCCESS,
812 (List<OperatorInfo>) ar.result);
813 } else {
814 if (ar.result == null) {
815 loge("getCellNetworkScanResults: Empty response");
816 }
817 if (ar.exception != null) {
818 loge("getCellNetworkScanResults: Exception: " + ar.exception);
819 }
820 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
821 if (ar.exception instanceof CommandException) {
822 CommandException.Error error =
823 ((CommandException) (ar.exception)).getCommandError();
824 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
825 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
826 } else if (error == CommandException.Error.GENERIC_FAILURE) {
827 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
828 }
829 }
830 cellScanResult = new CellNetworkScanResult(errorCode, null);
831 }
832 request.result = cellScanResult;
833 synchronized (request) {
834 request.notifyAll();
835 }
836 break;
837
838 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
839 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700840 ManualNetworkSelectionArgument selArg =
841 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700842 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
843 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700844 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
845 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700846 break;
847
848 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
849 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
850 break;
851
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700852 case CMD_GET_MODEM_ACTIVITY_INFO:
853 request = (MainThreadRequest) msg.obj;
854 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700855 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700856 break;
857
858 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
859 ar = (AsyncResult) msg.obj;
860 request = (MainThreadRequest) ar.userObj;
861 if (ar.exception == null && ar.result != null) {
862 request.result = ar.result;
863 } else {
864 if (ar.result == null) {
865 loge("queryModemActivityInfo: Empty response");
866 } else if (ar.exception instanceof CommandException) {
867 loge("queryModemActivityInfo: CommandException: " +
868 ar.exception);
869 } else {
870 loge("queryModemActivityInfo: Unknown exception");
871 }
872 }
Amit Mahajand4766222016-01-28 15:28:28 -0800873 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
874 if (request.result == null) {
875 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
876 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700877 synchronized (request) {
878 request.notifyAll();
879 }
880 break;
881
Meng Wang1a7c35a2016-05-05 20:56:15 -0700882 case CMD_SET_ALLOWED_CARRIERS:
883 request = (MainThreadRequest) msg.obj;
884 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
885 mPhone.setAllowedCarriers(
886 (List<CarrierIdentifier>) request.argument,
887 onCompleted);
888 break;
889
890 case EVENT_SET_ALLOWED_CARRIERS_DONE:
891 ar = (AsyncResult) msg.obj;
892 request = (MainThreadRequest) ar.userObj;
893 if (ar.exception == null && ar.result != null) {
894 request.result = ar.result;
895 } else {
896 if (ar.result == null) {
897 loge("setAllowedCarriers: Empty response");
898 } else if (ar.exception instanceof CommandException) {
899 loge("setAllowedCarriers: CommandException: " +
900 ar.exception);
901 } else {
902 loge("setAllowedCarriers: Unknown exception");
903 }
904 }
905 // Result cannot be null. Return -1 on error.
906 if (request.result == null) {
907 request.result = new int[]{-1};
908 }
909 synchronized (request) {
910 request.notifyAll();
911 }
912 break;
913
914 case CMD_GET_ALLOWED_CARRIERS:
915 request = (MainThreadRequest) msg.obj;
916 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
917 mPhone.getAllowedCarriers(onCompleted);
918 break;
919
920 case EVENT_GET_ALLOWED_CARRIERS_DONE:
921 ar = (AsyncResult) msg.obj;
922 request = (MainThreadRequest) ar.userObj;
923 if (ar.exception == null && ar.result != null) {
924 request.result = ar.result;
925 } else {
926 if (ar.result == null) {
927 loge("getAllowedCarriers: Empty response");
928 } else if (ar.exception instanceof CommandException) {
929 loge("getAllowedCarriers: CommandException: " +
930 ar.exception);
931 } else {
932 loge("getAllowedCarriers: Unknown exception");
933 }
934 }
935 // Result cannot be null. Return empty list of CarrierIdentifier.
936 if (request.result == null) {
937 request.result = new ArrayList<CarrierIdentifier>(0);
938 }
939 synchronized (request) {
940 request.notifyAll();
941 }
942 break;
943
Nathan Haroldb3014052017-01-25 15:57:32 -0800944 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
945 ar = (AsyncResult) msg.obj;
946 request = (MainThreadRequest) ar.userObj;
947 if (ar.exception == null && ar.result != null) {
948 request.result = ar.result;
949 } else {
950 request.result = new IllegalArgumentException(
951 "Failed to retrieve Forbidden Plmns");
952 if (ar.result == null) {
953 loge("getForbiddenPlmns: Empty response");
954 } else {
955 loge("getForbiddenPlmns: Unknown exception");
956 }
957 }
958 synchronized (request) {
959 request.notifyAll();
960 }
961 break;
962
963 case CMD_GET_FORBIDDEN_PLMNS:
964 request = (MainThreadRequest) msg.obj;
965 uiccCard = getUiccCardFromRequest(request);
966 if (uiccCard == null) {
967 loge("getForbiddenPlmns() UiccCard is null");
968 request.result = new IllegalArgumentException(
969 "getForbiddenPlmns() UiccCard is null");
970 synchronized (request) {
971 request.notifyAll();
972 }
973 break;
974 }
975 Integer appType = (Integer) request.argument;
976 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
977 if (uiccApp == null) {
978 loge("getForbiddenPlmns() no app with specified type -- "
979 + appType);
980 request.result = new IllegalArgumentException("Failed to get UICC App");
981 synchronized (request) {
982 request.notifyAll();
983 }
984 break;
985 } else {
986 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
987 + " specified type -- " + appType);
988 }
989 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
990 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
991 onCompleted);
992 break;
993
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +0000994 case CMD_SWITCH_SLOTS:
995 request = (MainThreadRequest) msg.obj;
996 int[] physicalSlots = (int[]) request.argument;
997 onCompleted = obtainMessage(EVENT_SWITCH_SLOTS_DONE, request);
998 UiccController.getInstance().switchSlots(physicalSlots, onCompleted);
999 break;
1000
1001 case EVENT_SWITCH_SLOTS_DONE:
1002 ar = (AsyncResult) msg.obj;
1003 request = (MainThreadRequest) ar.userObj;
1004 request.result = (ar.exception == null);
1005 synchronized (request) {
1006 request.notifyAll();
1007 }
1008 break;
1009
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001010 default:
1011 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
1012 break;
1013 }
1014 }
Jake Hambye994d462014-02-03 13:10:13 -08001015
1016 private void handleNullReturnEvent(Message msg, String command) {
1017 AsyncResult ar = (AsyncResult) msg.obj;
1018 MainThreadRequest request = (MainThreadRequest) ar.userObj;
1019 if (ar.exception == null) {
1020 request.result = true;
1021 } else {
1022 request.result = false;
1023 if (ar.exception instanceof CommandException) {
1024 loge(command + ": CommandException: " + ar.exception);
1025 } else {
1026 loge(command + ": Unknown exception");
1027 }
1028 }
1029 synchronized (request) {
1030 request.notifyAll();
1031 }
1032 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 }
1034
1035 /**
1036 * Posts the specified command to be executed on the main thread,
1037 * waits for the request to complete, and returns the result.
1038 * @see #sendRequestAsync
1039 */
1040 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001041 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -07001042 }
1043
1044 /**
1045 * Posts the specified command to be executed on the main thread,
1046 * waits for the request to complete, and returns the result.
1047 * @see #sendRequestAsync
1048 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001049 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001050 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
1051 throw new RuntimeException("This method will deadlock if called from the main thread.");
1052 }
1053
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001054 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055 Message msg = mMainThreadHandler.obtainMessage(command, request);
1056 msg.sendToTarget();
1057
1058 // Wait for the request to complete
1059 synchronized (request) {
1060 while (request.result == null) {
1061 try {
1062 request.wait();
1063 } catch (InterruptedException e) {
1064 // Do nothing, go back and wait until the request is complete
1065 }
1066 }
1067 }
1068 return request.result;
1069 }
1070
1071 /**
1072 * Asynchronous ("fire and forget") version of sendRequest():
1073 * Posts the specified command to be executed on the main thread, and
1074 * returns immediately.
1075 * @see #sendRequest
1076 */
1077 private void sendRequestAsync(int command) {
1078 mMainThreadHandler.sendEmptyMessage(command);
1079 }
1080
1081 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001082 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
1083 * @see {@link #sendRequest(int,Object)}
1084 */
1085 private void sendRequestAsync(int command, Object argument) {
1086 MainThreadRequest request = new MainThreadRequest(argument);
1087 Message msg = mMainThreadHandler.obtainMessage(command, request);
1088 msg.sendToTarget();
1089 }
1090
1091 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001092 * Initialize the singleton PhoneInterfaceManager instance.
1093 * This is only done once, at startup, from PhoneApp.onCreate().
1094 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001095 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001096 synchronized (PhoneInterfaceManager.class) {
1097 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001098 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099 } else {
1100 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1101 }
1102 return sInstance;
1103 }
1104 }
1105
1106 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001107 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001108 mApp = app;
1109 mPhone = phone;
1110 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001111 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001112 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1113 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001114 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001115 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001116 mSubscriptionController = SubscriptionController.getInstance();
yinxub1bed742017-04-17 11:45:04 -07001117 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Malcolm Chen2c63d402018-08-14 16:00:53 -07001118 mPhoneConfigurationManager = PhoneConfigurationManager.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001119
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001120 publish();
1121 }
1122
1123 private void publish() {
1124 if (DBG) log("publish: " + this);
1125
1126 ServiceManager.addService("phone", this);
1127 }
1128
Stuart Scott584921c2015-01-15 17:10:34 -08001129 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001130 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1131 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001132 }
1133
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001134 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1135 Phone phone = getPhoneFromRequest(request);
1136 return phone == null ? null :
1137 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1138 }
1139
Wink Saville36469e72014-06-11 15:17:00 -07001140 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001141 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001142 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001143 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001144 //
1145 // Implementation of the ITelephony interface.
1146 //
1147
1148 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001149 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001150 }
1151
Wink Savilleb564aae2014-10-23 10:18:09 -07001152 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001153 if (DBG) log("dial: " + number);
1154 // No permission check needed here: This is just a wrapper around the
1155 // ACTION_DIAL intent, which is available to any app since it puts up
1156 // the UI before it does anything.
1157
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001158 final long identity = Binder.clearCallingIdentity();
1159 try {
1160 String url = createTelUrl(number);
1161 if (url == null) {
1162 return;
1163 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001164
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001165 // PENDING: should we just silently fail if phone is offhook or ringing?
1166 PhoneConstants.State state = mCM.getState(subId);
1167 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1168 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1169 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1170 mApp.startActivity(intent);
1171 }
1172 } finally {
1173 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001174 }
1175 }
1176
1177 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001178 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001179 }
1180
Wink Savilleb564aae2014-10-23 10:18:09 -07001181 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001182 if (DBG) log("call: " + number);
1183
1184 // This is just a wrapper around the ACTION_CALL intent, but we still
1185 // need to do a permission check since we're calling startActivity()
1186 // from the context of the phone app.
1187 enforceCallPermission();
1188
1189 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1190 != AppOpsManager.MODE_ALLOWED) {
1191 return;
1192 }
1193
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001194 final long identity = Binder.clearCallingIdentity();
1195 try {
1196 String url = createTelUrl(number);
1197 if (url == null) {
1198 return;
1199 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001200
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001201 boolean isValid = false;
1202 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoListPrivileged();
1203 if (slist != null) {
1204 for (SubscriptionInfo subInfoRecord : slist) {
1205 if (subInfoRecord.getSubscriptionId() == subId) {
1206 isValid = true;
1207 break;
1208 }
Wink Saville3ab207e2014-11-20 13:07:20 -08001209 }
Wink Saville08874612014-08-31 19:19:58 -07001210 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001211 if (!isValid) {
1212 return;
1213 }
Wink Saville08874612014-08-31 19:19:58 -07001214
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001215 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
1216 intent.putExtra(SUBSCRIPTION_KEY, subId);
1217 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1218 mApp.startActivity(intent);
1219 } finally {
1220 Binder.restoreCallingIdentity(identity);
1221 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001222 }
1223
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001224 /**
1225 * End a call based on call state
1226 * @return true is a call was ended
1227 */
1228 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001229 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001230 }
1231
1232 /**
1233 * End a call based on the call state of the subId
1234 * @return true is a call was ended
1235 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001236 public boolean endCallForSubscriber(int subId) {
Tyler Gunn67073572018-02-14 14:19:42 -08001237 if (mApp.checkCallingOrSelfPermission(permission.MODIFY_PHONE_STATE)
1238 != PackageManager.PERMISSION_GRANTED) {
1239 Log.i(LOG_TAG, "endCall: called without modify phone state.");
1240 EventLog.writeEvent(0x534e4554, "67862398", -1, "");
1241 throw new SecurityException("MODIFY_PHONE_STATE permission required.");
1242 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001243
1244 final long identity = Binder.clearCallingIdentity();
1245 try {
1246 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
1247 } finally {
1248 Binder.restoreCallingIdentity(identity);
1249 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001250 }
1251
1252 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001253 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001254 }
1255
Wink Savilleb564aae2014-10-23 10:18:09 -07001256 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001257 if (DBG) log("answerRingingCall...");
1258 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1259 // but that can probably wait till the big TelephonyManager API overhaul.
1260 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1261 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001262
1263 final long identity = Binder.clearCallingIdentity();
1264 try {
1265 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
1266 } finally {
1267 Binder.restoreCallingIdentity(identity);
1268 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001269 }
1270
1271 /**
1272 * Make the actual telephony calls to implement answerRingingCall().
1273 * This should only be called from the main thread of the Phone app.
1274 * @see #answerRingingCall
1275 *
1276 * TODO: it would be nice to return true if we answered the call, or
1277 * false if there wasn't actually a ringing incoming call, or some
1278 * other error occurred. (In other words, pass back the return value
1279 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1280 * But that would require calling this method via sendRequest() rather
1281 * than sendRequestAsync(), and right now we don't actually *need* that
1282 * return value, so let's just return void for now.
1283 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001284 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001285 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001287 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1288 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001289 if (hasActiveCall && hasHoldingCall) {
1290 // Both lines are in use!
1291 // TODO: provide a flag to let the caller specify what
1292 // policy to use if both lines are in use. (The current
1293 // behavior is hardwired to "answer incoming, end ongoing",
1294 // which is how the CALL button is specced to behave.)
1295 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1296 return;
1297 } else {
1298 // answerCall() will automatically hold the current active
1299 // call, if there is one.
1300 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1301 return;
1302 }
1303 } else {
1304 // No call was ringing.
1305 return;
1306 }
1307 }
1308
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001309 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001310 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001311 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001312 public void silenceRinger() {
1313 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001314 }
1315
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001316 @Override
1317 public boolean isOffhook(String callingPackage) {
1318 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001319 }
1320
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001321 @Override
1322 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08001323 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08001324 mApp, subId, callingPackage, "isOffhookForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001325 return false;
1326 }
1327
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001328 final long identity = Binder.clearCallingIdentity();
1329 try {
1330 final Phone phone = getPhone(subId);
1331 if (phone != null) {
1332 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1333 } else {
1334 return false;
1335 }
1336 } finally {
1337 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001338 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 }
1340
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001341 @Override
1342 public boolean isRinging(String callingPackage) {
1343 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001344 }
1345
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001346 @Override
1347 public boolean isRingingForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08001348 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08001349 mApp, subId, callingPackage, "isRingingForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001350 return false;
1351 }
1352
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001353 final long identity = Binder.clearCallingIdentity();
1354 try {
1355 final Phone phone = getPhone(subId);
1356 if (phone != null) {
1357 return (phone.getState() == PhoneConstants.State.RINGING);
1358 } else {
1359 return false;
1360 }
1361 } finally {
1362 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001363 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001364 }
1365
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001366 @Override
1367 public boolean isIdle(String callingPackage) {
1368 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001369 }
1370
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001371 @Override
1372 public boolean isIdleForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08001373 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08001374 mApp, subId, callingPackage, "isIdleForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001375 return false;
1376 }
1377
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001378 final long identity = Binder.clearCallingIdentity();
1379 try {
1380 final Phone phone = getPhone(subId);
1381 if (phone != null) {
1382 return (phone.getState() == PhoneConstants.State.IDLE);
1383 } else {
1384 return false;
1385 }
1386 } finally {
1387 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001388 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001389 }
1390
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001391 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001392 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001393 }
1394
Wink Savilleb564aae2014-10-23 10:18:09 -07001395 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001396 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001397 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1398 }
1399
1400 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001401 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001402 }
1403
Wink Savilleb564aae2014-10-23 10:18:09 -07001404 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001405 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001406 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1407 }
1408
1409 /** {@hide} */
1410 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001411 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001412 }
1413
Wink Savilleb564aae2014-10-23 10:18:09 -07001414 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001415 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001416
1417 final long identity = Binder.clearCallingIdentity();
1418 try {
1419 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
1420 checkSimPin.start();
1421 return checkSimPin.unlockSim(null, pin);
1422 } finally {
1423 Binder.restoreCallingIdentity(identity);
1424 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 }
1426
Wink Saville9de0f752013-10-22 19:04:03 -07001427 /** {@hide} */
1428 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001429 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001430 }
1431
Wink Savilleb564aae2014-10-23 10:18:09 -07001432 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001434
1435 final long identity = Binder.clearCallingIdentity();
1436 try {
1437 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
1438 checkSimPuk.start();
1439 return checkSimPuk.unlockSim(puk, pin);
1440 } finally {
1441 Binder.restoreCallingIdentity(identity);
1442 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 }
1444
1445 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001446 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001447 * a synchronous one.
1448 */
1449 private static class UnlockSim extends Thread {
1450
1451 private final IccCard mSimCard;
1452
1453 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001454 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1455 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456
1457 // For replies from SimCard interface
1458 private Handler mHandler;
1459
1460 // For async handler to identify request type
1461 private static final int SUPPLY_PIN_COMPLETE = 100;
1462
1463 public UnlockSim(IccCard simCard) {
1464 mSimCard = simCard;
1465 }
1466
1467 @Override
1468 public void run() {
1469 Looper.prepare();
1470 synchronized (UnlockSim.this) {
1471 mHandler = new Handler() {
1472 @Override
1473 public void handleMessage(Message msg) {
1474 AsyncResult ar = (AsyncResult) msg.obj;
1475 switch (msg.what) {
1476 case SUPPLY_PIN_COMPLETE:
1477 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1478 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001479 mRetryCount = msg.arg1;
1480 if (ar.exception != null) {
1481 if (ar.exception instanceof CommandException &&
1482 ((CommandException)(ar.exception)).getCommandError()
1483 == CommandException.Error.PASSWORD_INCORRECT) {
1484 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1485 } else {
1486 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1487 }
1488 } else {
1489 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1490 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001491 mDone = true;
1492 UnlockSim.this.notifyAll();
1493 }
1494 break;
1495 }
1496 }
1497 };
1498 UnlockSim.this.notifyAll();
1499 }
1500 Looper.loop();
1501 }
1502
1503 /*
1504 * Use PIN or PUK to unlock SIM card
1505 *
1506 * If PUK is null, unlock SIM card with PIN
1507 *
1508 * If PUK is not null, unlock SIM card with PUK and set PIN code
1509 */
Wink Saville9de0f752013-10-22 19:04:03 -07001510 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001511
1512 while (mHandler == null) {
1513 try {
1514 wait();
1515 } catch (InterruptedException e) {
1516 Thread.currentThread().interrupt();
1517 }
1518 }
1519 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1520
1521 if (puk == null) {
1522 mSimCard.supplyPin(pin, callback);
1523 } else {
1524 mSimCard.supplyPuk(puk, pin, callback);
1525 }
1526
1527 while (!mDone) {
1528 try {
1529 Log.d(LOG_TAG, "wait for done");
1530 wait();
1531 } catch (InterruptedException e) {
1532 // Restore the interrupted status
1533 Thread.currentThread().interrupt();
1534 }
1535 }
1536 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001537 int[] resultArray = new int[2];
1538 resultArray[0] = mResult;
1539 resultArray[1] = mRetryCount;
1540 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001541 }
1542 }
1543
1544 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001545 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001546
1547 }
1548
Wink Savilleb564aae2014-10-23 10:18:09 -07001549 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 // No permission check needed here: this call is harmless, and it's
1551 // needed for the ServiceState.requestStateUpdate() call (which is
1552 // already intentionally exposed to 3rd parties.)
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001553 final long identity = Binder.clearCallingIdentity();
1554 try {
1555 final Phone phone = getPhone(subId);
1556 if (phone != null) {
1557 phone.updateServiceLocation();
1558 }
1559 } finally {
1560 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001561 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001562 }
1563
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001564 @Override
1565 public boolean isRadioOn(String callingPackage) {
1566 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001567 }
1568
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001569 @Override
1570 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08001571 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08001572 mApp, subId, callingPackage, "isRadioOnForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001573 return false;
1574 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001575
1576 final long identity = Binder.clearCallingIdentity();
1577 try {
1578 return isRadioOnForSubscriber(subId);
1579 } finally {
1580 Binder.restoreCallingIdentity(identity);
1581 }
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001582 }
1583
1584 private boolean isRadioOnForSubscriber(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001585 final long identity = Binder.clearCallingIdentity();
1586 try {
1587 final Phone phone = getPhone(subId);
1588 if (phone != null) {
1589 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1590 } else {
1591 return false;
1592 }
1593 } finally {
1594 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001595 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001596 }
1597
1598 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001599 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001600 }
Wink Saville36469e72014-06-11 15:17:00 -07001601
Wink Savilleb564aae2014-10-23 10:18:09 -07001602 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001604
1605 final long identity = Binder.clearCallingIdentity();
1606 try {
1607 final Phone phone = getPhone(subId);
1608 if (phone != null) {
1609 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1610 }
1611 } finally {
1612 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001613 }
Wink Saville36469e72014-06-11 15:17:00 -07001614 }
1615
1616 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001617 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001618 }
1619
Wink Savilleb564aae2014-10-23 10:18:09 -07001620 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001621 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001622
1623 final long identity = Binder.clearCallingIdentity();
1624 try {
1625 final Phone phone = getPhone(subId);
1626 if (phone == null) {
1627 return false;
1628 }
1629 if ((phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF) != turnOn) {
1630 toggleRadioOnOffForSubscriber(subId);
1631 }
1632 return true;
1633 } finally {
1634 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001635 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001636 }
Wink Saville36469e72014-06-11 15:17:00 -07001637
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001638 public boolean needMobileRadioShutdown() {
1639 /*
1640 * If any of the Radios are available, it will need to be
1641 * shutdown. So return true if any Radio is available.
1642 */
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001643 final long identity = Binder.clearCallingIdentity();
1644 try {
1645 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1646 Phone phone = PhoneFactory.getPhone(i);
1647 if (phone != null && phone.isRadioAvailable()) return true;
1648 }
1649 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1650 return false;
1651 } finally {
1652 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001653 }
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001654 }
1655
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001656 @Override
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001657 public void shutdownMobileRadios() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001658 enforceModifyPermission();
1659
1660 final long identity = Binder.clearCallingIdentity();
1661 try {
1662 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1663 logv("Shutting down Phone " + i);
1664 shutdownRadioUsingPhoneId(i);
1665 }
1666 } finally {
1667 Binder.restoreCallingIdentity(identity);
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001668 }
1669 }
1670
1671 private void shutdownRadioUsingPhoneId(int phoneId) {
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001672 Phone phone = PhoneFactory.getPhone(phoneId);
1673 if (phone != null && phone.isRadioAvailable()) {
1674 phone.shutdownRadio();
1675 }
1676 }
1677
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001678 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07001679 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001680
1681 final long identity = Binder.clearCallingIdentity();
1682 try {
1683 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1684 if (defaultPhone != null) {
1685 defaultPhone.setRadioPower(turnOn);
1686 return true;
1687 } else {
1688 loge("There's no default phone.");
1689 return false;
1690 }
1691 } finally {
1692 Binder.restoreCallingIdentity(identity);
Wei Liu9ae2a062016-08-08 11:09:34 -07001693 }
Wink Saville36469e72014-06-11 15:17:00 -07001694 }
1695
Wink Savilleb564aae2014-10-23 10:18:09 -07001696 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001697 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001698
1699 final long identity = Binder.clearCallingIdentity();
1700 try {
1701 final Phone phone = getPhone(subId);
1702 if (phone != null) {
1703 phone.setRadioPower(turnOn);
1704 return true;
1705 } else {
1706 return false;
1707 }
1708 } finally {
1709 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001710 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001711 }
1712
Wink Saville36469e72014-06-11 15:17:00 -07001713 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001714 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001715 public boolean enableDataConnectivity() {
1716 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001717
1718 final long identity = Binder.clearCallingIdentity();
1719 try {
1720 int subId = mSubscriptionController.getDefaultDataSubId();
1721 final Phone phone = getPhone(subId);
1722 if (phone != null) {
1723 phone.setUserDataEnabled(true);
1724 return true;
1725 } else {
1726 return false;
1727 }
1728 } finally {
1729 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001730 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001731 }
1732
Wink Saville36469e72014-06-11 15:17:00 -07001733 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001734 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001735 public boolean disableDataConnectivity() {
1736 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001737
1738 final long identity = Binder.clearCallingIdentity();
1739 try {
1740 int subId = mSubscriptionController.getDefaultDataSubId();
1741 final Phone phone = getPhone(subId);
1742 if (phone != null) {
1743 phone.setUserDataEnabled(false);
1744 return true;
1745 } else {
1746 return false;
1747 }
1748 } finally {
1749 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001750 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001751 }
1752
Sanket Padawe356d7632015-06-22 14:03:32 -07001753 @Override
Jack Yuacf8a132017-05-01 17:00:48 -07001754 public boolean isDataConnectivityPossible(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001755 final long identity = Binder.clearCallingIdentity();
1756 try {
1757 final Phone phone = getPhone(subId);
1758 if (phone != null) {
1759 return phone.isDataAllowed();
1760 } else {
1761 return false;
1762 }
1763 } finally {
1764 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001765 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001766 }
1767
1768 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001769 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001770 }
1771
pkanwarae03a6b2016-11-06 20:37:09 -08001772 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001773 enforceCallPermission();
1774
1775 final long identity = Binder.clearCallingIdentity();
1776 try {
1777 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1778 return;
1779 }
1780 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1781 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1782 } finally {
1783 Binder.restoreCallingIdentity(identity);
1784 }
pkanwar32d516d2016-10-14 19:37:38 -07001785 };
1786
Wink Savilleb564aae2014-10-23 10:18:09 -07001787 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001788 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001789
1790 final long identity = Binder.clearCallingIdentity();
1791 try {
1792 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1793 return false;
1794 }
1795 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
1796 } finally {
1797 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001798 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001799 }
1800
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001801 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001802 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001803 }
1804
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001805 public int getCallStateForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001806 final long identity = Binder.clearCallingIdentity();
1807 try {
1808 Phone phone = PhoneFactory.getPhone(slotIndex);
1809 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1810 PhoneConstantConversions.convertCallState(phone.getState());
1811 } finally {
1812 Binder.restoreCallingIdentity(identity);
1813 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001814 }
1815
Sanket Padawe356d7632015-06-22 14:03:32 -07001816 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001817 public int getDataState() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001818 final long identity = Binder.clearCallingIdentity();
1819 try {
1820 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1821 if (phone != null) {
1822 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
1823 } else {
1824 return PhoneConstantConversions.convertDataState(
1825 PhoneConstants.DataState.DISCONNECTED);
1826 }
1827 } finally {
1828 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001829 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001830 }
1831
Sanket Padawe356d7632015-06-22 14:03:32 -07001832 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001833 public int getDataActivity() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001834 final long identity = Binder.clearCallingIdentity();
1835 try {
1836 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1837 if (phone != null) {
1838 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1839 } else {
1840 return TelephonyManager.DATA_ACTIVITY_NONE;
1841 }
1842 } finally {
1843 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001844 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001845 }
1846
1847 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001848 public Bundle getCellLocation(String callingPackage) {
Hall Liu1aa510f2017-11-22 17:40:08 -08001849 mPhone.getContext().getSystemService(AppOpsManager.class)
1850 .checkPackage(Binder.getCallingUid(), callingPackage);
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001851 if (!LocationAccessPolicy.canAccessCellLocation(mPhone.getContext(),
Svet Ganov4af66282018-03-07 19:57:05 -08001852 callingPackage, Binder.getCallingUid(), Binder.getCallingPid(), true)) {
Svetoslav64fad262015-04-14 14:35:21 -07001853 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001854 }
1855
Narayan Kamathf04b5a12018-01-09 11:47:15 +00001856 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001857 final long identity = Binder.clearCallingIdentity();
1858 try {
1859 if (DBG_LOC) log("getCellLocation: is active user");
1860 Bundle data = new Bundle();
1861 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
1862 if (phone == null) {
1863 return null;
1864 }
1865
1866 phone.getCellLocation(workSource).fillInNotifierBundle(data);
1867 return data;
1868 } finally {
1869 Binder.restoreCallingIdentity(identity);
1870 }
Svetoslav64fad262015-04-14 14:35:21 -07001871 }
1872
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001873 @Override
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001874 public String getNetworkCountryIsoForPhone(int phoneId) {
1875 // Reporting the correct network country is ambiguous when IWLAN could conflict with
1876 // registered cell info, so return a NULL country instead.
1877 final long identity = Binder.clearCallingIdentity();
1878 try {
Malcolm Chen3732c2b2018-07-18 20:15:24 -07001879 if (phoneId == SubscriptionManager.INVALID_PHONE_INDEX) {
1880 // Get default phone in this case.
1881 phoneId = SubscriptionManager.DEFAULT_PHONE_INDEX;
1882 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001883 final int subId = mSubscriptionController.getSubIdUsingPhoneId(phoneId);
Jack Yu5f7092c2018-04-13 14:05:37 -07001884 // Todo: fix this when we can get the actual cellular network info when the device
1885 // is on IWLAN.
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001886 if (TelephonyManager.NETWORK_TYPE_IWLAN
1887 == getVoiceNetworkTypeForSubscriber(subId, mApp.getPackageName())) {
1888 return "";
1889 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001890 Phone phone = PhoneFactory.getPhone(phoneId);
1891 if (phone != null) {
1892 ServiceStateTracker sst = phone.getServiceStateTracker();
1893 if (sst != null) {
1894 LocaleTracker lt = sst.getLocaleTracker();
1895 if (lt != null) {
1896 return lt.getCurrentCountry();
1897 }
1898 }
1899 }
1900 return "";
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001901 } finally {
1902 Binder.restoreCallingIdentity(identity);
1903 }
Jonathan Basseribf5362b2017-07-19 12:22:35 -07001904 }
1905
1906 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001907 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001908 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001909 }
1910
Sanket Padawe356d7632015-06-22 14:03:32 -07001911 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001912 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001913 mApp.enforceCallingOrSelfPermission(
1914 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001915
1916 final long identity = Binder.clearCallingIdentity();
1917 try {
1918 final Phone phone = getPhone(subId);
1919 if (phone != null) {
1920 phone.enableLocationUpdates();
1921 }
1922 } finally {
1923 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001924 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001925 }
1926
1927 @Override
1928 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001929 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001930 }
1931
Sanket Padawe356d7632015-06-22 14:03:32 -07001932 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001933 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001934 mApp.enforceCallingOrSelfPermission(
1935 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001936
1937 final long identity = Binder.clearCallingIdentity();
1938 try {
1939 final Phone phone = getPhone(subId);
1940 if (phone != null) {
1941 phone.disableLocationUpdates();
1942 }
1943 } finally {
1944 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07001945 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001946 }
1947
1948 @Override
1949 @SuppressWarnings("unchecked")
Nathan Haroldb4d55612018-07-20 13:13:08 -07001950 public List<NeighboringCellInfo>
1951 getNeighboringCellInfo(String callingPackage, int targetSdk) {
1952 // FIXME: use the P constant when available
1953 if (targetSdk > android.os.Build.VERSION_CODES.O_MR1 + 1) return null;
1954
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001955 if (!LocationAccessPolicy.canAccessCellLocation(mPhone.getContext(),
Svet Ganov4af66282018-03-07 19:57:05 -08001956 callingPackage, Binder.getCallingUid(), Binder.getCallingPid(), true)) {
Svetoslav64fad262015-04-14 14:35:21 -07001957 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001958 }
1959
1960 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1961 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1962 return null;
1963 }
Svetoslav64fad262015-04-14 14:35:21 -07001964
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001965 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001966
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001967 ArrayList<NeighboringCellInfo> cells = null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001968
Narayan Kamathf04b5a12018-01-09 11:47:15 +00001969 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001970
1971 final long identity = Binder.clearCallingIdentity();
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001972 try {
1973 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
1974 CMD_HANDLE_NEIGHBORING_CELL, workSource,
1975 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
1976 } catch (RuntimeException e) {
1977 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001978 } finally {
1979 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001980 }
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001981 return cells;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001982 }
1983
1984
1985 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001986 public List<CellInfo> getAllCellInfo(String callingPackage) {
Hall Liu1aa510f2017-11-22 17:40:08 -08001987 mPhone.getContext().getSystemService(AppOpsManager.class)
1988 .checkPackage(Binder.getCallingUid(), callingPackage);
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001989 if (!LocationAccessPolicy.canAccessCellLocation(mPhone.getContext(),
Svet Ganov4af66282018-03-07 19:57:05 -08001990 callingPackage, Binder.getCallingUid(), Binder.getCallingPid(), true)) {
Svetoslav64fad262015-04-14 14:35:21 -07001991 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001992 }
1993
Svetoslav Ganov4a9d4482017-06-20 19:53:35 -07001994 if (DBG_LOC) log("getAllCellInfo: is active user");
Narayan Kamathf04b5a12018-01-09 11:47:15 +00001995 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08001996 final long identity = Binder.clearCallingIdentity();
1997 try {
1998 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1999 for (Phone phone : PhoneFactory.getPhones()) {
2000 final List<CellInfo> info = phone.getAllCellInfo(workSource);
2001 if (info != null) cellInfos.addAll(info);
2002 }
2003 return cellInfos;
2004 } finally {
2005 Binder.restoreCallingIdentity(identity);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002006 }
2007 }
2008
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07002009 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002010 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08002011 enforceModifyPermission();
Narayan Kamathf04b5a12018-01-09 11:47:15 +00002012 WorkSource workSource = getWorkSource(Binder.getCallingUid());
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002013
2014 final long identity = Binder.clearCallingIdentity();
2015 try {
2016 mPhone.setCellInfoListRate(rateInMillis, workSource);
2017 } finally {
2018 Binder.restoreCallingIdentity(identity);
2019 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002020 }
2021
Shishir Agrawala9f32182016-04-12 12:00:16 -07002022 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002023 public String getImeiForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002024 Phone phone = PhoneFactory.getPhone(slotIndex);
2025 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002026 return null;
2027 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002028 int subId = phone.getSubId();
2029 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2030 mApp, subId, callingPackage, "getImeiForSlot")) {
2031 return null;
2032 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002033
2034 final long identity = Binder.clearCallingIdentity();
2035 try {
2036 return phone.getImei();
2037 } finally {
2038 Binder.restoreCallingIdentity(identity);
2039 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002040 }
2041
2042 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002043 public String getTypeAllocationCodeForSlot(int slotIndex) {
2044 Phone phone = PhoneFactory.getPhone(slotIndex);
2045 String tac = null;
2046 if (phone != null) {
2047 String imei = phone.getImei();
2048 tac = imei == null ? null : imei.substring(0, TYPE_ALLOCATION_CODE_LENGTH);
2049 }
2050 return tac;
2051 }
2052
2053 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07002054 public String getMeidForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002055 Phone phone = PhoneFactory.getPhone(slotIndex);
2056 if (phone == null) {
Jack Yu2af8d712017-03-15 17:14:14 -07002057 return null;
2058 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002059 int subId = phone.getSubId();
2060 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2061 mApp, subId, callingPackage, "getMeidForSlot")) {
2062 return null;
2063 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002064
2065 final long identity = Binder.clearCallingIdentity();
2066 try {
2067 return phone.getMeid();
2068 } finally {
2069 Binder.restoreCallingIdentity(identity);
2070 }
Jack Yu2af8d712017-03-15 17:14:14 -07002071 }
2072
2073 @Override
David Kelly5e06a7f2018-03-12 14:10:59 +00002074 public String getManufacturerCodeForSlot(int slotIndex) {
2075 Phone phone = PhoneFactory.getPhone(slotIndex);
2076 String manufacturerCode = null;
2077 if (phone != null) {
2078 String meid = phone.getMeid();
2079 manufacturerCode = meid == null ? null : meid.substring(0, MANUFACTURER_CODE_LENGTH);
2080 }
2081 return manufacturerCode;
2082 }
2083
2084 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002085 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08002086 Phone phone = PhoneFactory.getPhone(slotIndex);
2087 if (phone == null) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002088 return null;
2089 }
Jeff Davidson913390f2018-02-23 17:11:49 -08002090 int subId = phone.getSubId();
2091 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2092 mApp, subId, callingPackage, "getDeviceSoftwareVersionForSlot")) {
2093 return null;
2094 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002095
2096 final long identity = Binder.clearCallingIdentity();
2097 try {
2098 return phone.getDeviceSvn();
2099 } finally {
2100 Binder.restoreCallingIdentity(identity);
2101 }
Shishir Agrawala9f32182016-04-12 12:00:16 -07002102 }
2103
fionaxu43304da2017-11-27 22:51:16 -08002104 @Override
2105 public int getSubscriptionCarrierId(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002106 final long identity = Binder.clearCallingIdentity();
2107 try {
2108 final Phone phone = getPhone(subId);
2109 return phone == null ? TelephonyManager.UNKNOWN_CARRIER_ID : phone.getCarrierId();
2110 } finally {
2111 Binder.restoreCallingIdentity(identity);
2112 }
fionaxu43304da2017-11-27 22:51:16 -08002113 }
2114
2115 @Override
2116 public String getSubscriptionCarrierName(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002117 final long identity = Binder.clearCallingIdentity();
2118 try {
2119 final Phone phone = getPhone(subId);
2120 return phone == null ? null : phone.getCarrierName();
2121 } finally {
2122 Binder.restoreCallingIdentity(identity);
2123 }
fionaxu43304da2017-11-27 22:51:16 -08002124 }
2125
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002126 //
2127 // Internal helper methods.
2128 //
2129
Sanket Padaweee13a9b2016-03-08 17:30:28 -08002130 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002131 * Make sure the caller has the MODIFY_PHONE_STATE permission.
2132 *
2133 * @throws SecurityException if the caller does not have the required permission
2134 */
2135 private void enforceModifyPermission() {
2136 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
2137 }
2138
2139 /**
2140 * Make sure the caller has the CALL_PHONE permission.
2141 *
2142 * @throws SecurityException if the caller does not have the required permission
2143 */
2144 private void enforceCallPermission() {
2145 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
2146 }
2147
Stuart Scott8eef64f2015-04-08 15:13:54 -07002148 private void enforceConnectivityInternalPermission() {
2149 mApp.enforceCallingOrSelfPermission(
2150 android.Manifest.permission.CONNECTIVITY_INTERNAL,
2151 "ConnectivityService");
2152 }
2153
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002154 private String createTelUrl(String number) {
2155 if (TextUtils.isEmpty(number)) {
2156 return null;
2157 }
2158
Jake Hambye994d462014-02-03 13:10:13 -08002159 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002160 }
2161
Ihab Awadf9e92732013-12-05 18:02:52 -08002162 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002163 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
2164 }
2165
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07002166 private static void logv(String msg) {
2167 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
2168 }
2169
Ihab Awadf9e92732013-12-05 18:02:52 -08002170 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002171 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
2172 }
2173
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002174 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002175 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07002176 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002177 }
2178
Sanket Padawe356d7632015-06-22 14:03:32 -07002179 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002180 public int getActivePhoneTypeForSlot(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002181 final long identity = Binder.clearCallingIdentity();
2182 try {
2183 final Phone phone = PhoneFactory.getPhone(slotIndex);
2184 if (phone == null) {
2185 return PhoneConstants.PHONE_TYPE_NONE;
2186 } else {
2187 return phone.getPhoneType();
2188 }
2189 } finally {
2190 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002191 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002192 }
2193
2194 /**
2195 * Returns the CDMA ERI icon index to display
2196 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002197 @Override
2198 public int getCdmaEriIconIndex(String callingPackage) {
2199 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002200 }
2201
Sanket Padawe356d7632015-06-22 14:03:32 -07002202 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002203 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002204 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002205 mApp, subId, callingPackage, "getCdmaEriIconIndexForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002206 return -1;
2207 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002208
2209 final long identity = Binder.clearCallingIdentity();
2210 try {
2211 final Phone phone = getPhone(subId);
2212 if (phone != null) {
2213 return phone.getCdmaEriIconIndex();
2214 } else {
2215 return -1;
2216 }
2217 } finally {
2218 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002219 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002220 }
2221
2222 /**
2223 * Returns the CDMA ERI icon mode,
2224 * 0 - ON
2225 * 1 - FLASHING
2226 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002227 @Override
2228 public int getCdmaEriIconMode(String callingPackage) {
2229 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002230 }
2231
Sanket Padawe356d7632015-06-22 14:03:32 -07002232 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002233 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002234 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002235 mApp, subId, callingPackage, "getCdmaEriIconModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002236 return -1;
2237 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002238
2239 final long identity = Binder.clearCallingIdentity();
2240 try {
2241 final Phone phone = getPhone(subId);
2242 if (phone != null) {
2243 return phone.getCdmaEriIconMode();
2244 } else {
2245 return -1;
2246 }
2247 } finally {
2248 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002249 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002250 }
2251
2252 /**
2253 * Returns the CDMA ERI text,
2254 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002255 @Override
2256 public String getCdmaEriText(String callingPackage) {
2257 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002258 }
2259
Sanket Padawe356d7632015-06-22 14:03:32 -07002260 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002261 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002262 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002263 mApp, subId, callingPackage, "getCdmaEriIconTextForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002264 return null;
2265 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002266
2267 final long identity = Binder.clearCallingIdentity();
2268 try {
2269 final Phone phone = getPhone(subId);
2270 if (phone != null) {
2271 return phone.getCdmaEriText();
2272 } else {
2273 return null;
2274 }
2275 } finally {
2276 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002277 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002278 }
2279
2280 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002281 * Returns the CDMA MDN.
2282 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002283 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002284 public String getCdmaMdn(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002285 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2286 mApp, subId, "getCdmaMdn");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002287
2288 final long identity = Binder.clearCallingIdentity();
2289 try {
2290 final Phone phone = getPhone(subId);
2291 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
2292 return phone.getLine1Number();
2293 } else {
2294 return null;
2295 }
2296 } finally {
2297 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002298 }
2299 }
2300
2301 /**
2302 * Returns the CDMA MIN.
2303 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002304 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002305 public String getCdmaMin(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002306 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2307 mApp, subId, "getCdmaMin");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002308
2309 final long identity = Binder.clearCallingIdentity();
2310 try {
2311 final Phone phone = getPhone(subId);
2312 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2313 return phone.getCdmaMin();
2314 } else {
2315 return null;
2316 }
2317 } finally {
2318 Binder.restoreCallingIdentity(identity);
Junda Liuca05d5d2014-08-14 22:36:34 -07002319 }
2320 }
2321
2322 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002323 * Returns true if CDMA provisioning needs to run.
2324 */
2325 public boolean needsOtaServiceProvisioning() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002326 final long identity = Binder.clearCallingIdentity();
2327 try {
2328 return mPhone.needsOtaServiceProvisioning();
2329 } finally {
2330 Binder.restoreCallingIdentity(identity);
2331 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002332 }
2333
2334 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002335 * Sets the voice mail number of a given subId.
2336 */
2337 @Override
2338 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002339 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setVoiceMailNumber");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002340
2341 final long identity = Binder.clearCallingIdentity();
2342 try {
2343 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2344 new Pair<String, String>(alphaTag, number), new Integer(subId));
2345 return success;
2346 } finally {
2347 Binder.restoreCallingIdentity(identity);
2348 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002349 }
2350
Ta-wei Yen87c49842016-05-13 21:19:52 -07002351 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002352 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2353 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2354 String systemDialer = TelecomManager.from(mPhone.getContext()).getSystemDialerPackage();
2355 if (!TextUtils.equals(callingPackage, systemDialer)) {
2356 throw new SecurityException("caller must be system dialer");
2357 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002358
2359 final long identity = Binder.clearCallingIdentity();
2360 try {
2361 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2362 if (phoneAccountHandle == null) {
2363 return null;
2364 }
2365 return VisualVoicemailSettingsUtil.dump(mPhone.getContext(), phoneAccountHandle);
2366 } finally {
2367 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002368 }
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002369 }
2370
2371 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002372 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002373 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Jeff Davidson7e17e312018-02-13 18:17:36 -08002374 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002375 mApp, subId, callingPackage, "getVisualVoicemailPackageName")) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002376 return null;
2377 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002378
Jeff Davidsona8e4e242018-03-15 17:16:18 -07002379 final long identity = Binder.clearCallingIdentity();
2380 try {
2381 return RemoteVvmTaskManager
2382 .getRemotePackage(mPhone.getContext(), subId).getPackageName();
2383 } finally {
2384 Binder.restoreCallingIdentity(identity);
2385 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002386 }
2387
2388 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002389 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2390 VisualVoicemailSmsFilterSettings settings) {
2391 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002392
2393 final long identity = Binder.clearCallingIdentity();
2394 try {
2395 VisualVoicemailSmsFilterConfig.enableVisualVoicemailSmsFilter(
2396 mPhone.getContext(), callingPackage, subId, settings);
2397 } finally {
2398 Binder.restoreCallingIdentity(identity);
2399 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002400 }
2401
2402 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002403 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2404 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002405
2406 final long identity = Binder.clearCallingIdentity();
2407 try {
2408 VisualVoicemailSmsFilterConfig.disableVisualVoicemailSmsFilter(
2409 mPhone.getContext(), callingPackage, subId);
2410 } finally {
2411 Binder.restoreCallingIdentity(identity);
2412 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002413 }
2414
2415 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002416 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2417 String callingPackage, int subId) {
2418 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002419
2420 final long identity = Binder.clearCallingIdentity();
2421 try {
2422 return VisualVoicemailSmsFilterConfig.getVisualVoicemailSmsFilterSettings(
2423 mPhone.getContext(), callingPackage, subId);
2424 } finally {
2425 Binder.restoreCallingIdentity(identity);
2426 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002427 }
2428
2429 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002430 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002431 enforceReadPrivilegedPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002432
2433 final long identity = Binder.clearCallingIdentity();
2434 try {
2435 return VisualVoicemailSmsFilterConfig.getActiveVisualVoicemailSmsFilterSettings(
2436 mPhone.getContext(), subId);
2437 } finally {
2438 Binder.restoreCallingIdentity(identity);
2439 }
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002440 }
2441
2442 @Override
2443 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2444 String number, int port, String text, PendingIntent sentIntent) {
2445 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002446 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002447 enforceSendSmsPermission();
2448 // Make the calls as the phone process.
2449 final long identity = Binder.clearCallingIdentity();
2450 try {
2451 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2452 if (port == 0) {
2453 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2454 sentIntent, null, false);
2455 } else {
2456 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2457 smsManager.sendDataMessageWithSelfPermissions(number, null,
2458 (short) port, data, sentIntent, null);
2459 }
2460 } finally {
2461 Binder.restoreCallingIdentity(identity);
2462 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002463 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002464 /**
fionaxu0152e512016-11-14 13:36:14 -08002465 * Sets the voice activation state of a given subId.
2466 */
2467 @Override
2468 public void setVoiceActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002469 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2470 mApp, subId, "setVoiceActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002471
2472 final long identity = Binder.clearCallingIdentity();
2473 try {
2474 final Phone phone = getPhone(subId);
2475 if (phone != null) {
2476 phone.setVoiceActivationState(activationState);
2477 } else {
2478 loge("setVoiceActivationState fails with invalid subId: " + subId);
2479 }
2480 } finally {
2481 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002482 }
2483 }
2484
2485 /**
2486 * Sets the data activation state of a given subId.
2487 */
2488 @Override
2489 public void setDataActivationState(int subId, int activationState) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002490 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2491 mApp, subId, "setDataActivationState");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002492
2493 final long identity = Binder.clearCallingIdentity();
2494 try {
2495 final Phone phone = getPhone(subId);
2496 if (phone != null) {
2497 phone.setDataActivationState(activationState);
2498 } else {
2499 loge("setVoiceActivationState fails with invalid subId: " + subId);
2500 }
2501 } finally {
2502 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002503 }
2504 }
2505
2506 /**
2507 * Returns the voice activation state of a given subId.
2508 */
2509 @Override
2510 public int getVoiceActivationState(int subId, String callingPackage) {
goneil799f6e92017-12-13 12:57:23 -08002511 enforceReadPrivilegedPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002512
fionaxu0152e512016-11-14 13:36:14 -08002513 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002514 final long identity = Binder.clearCallingIdentity();
2515 try {
2516 if (phone != null) {
2517 return phone.getVoiceActivationState();
2518 } else {
2519 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2520 }
2521 } finally {
2522 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002523 }
2524 }
2525
2526 /**
2527 * Returns the data activation state of a given subId.
2528 */
2529 @Override
2530 public int getDataActivationState(int subId, String callingPackage) {
goneil799f6e92017-12-13 12:57:23 -08002531 enforceReadPrivilegedPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002532
fionaxu0152e512016-11-14 13:36:14 -08002533 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002534 final long identity = Binder.clearCallingIdentity();
2535 try {
2536 if (phone != null) {
2537 return phone.getDataActivationState();
2538 } else {
2539 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2540 }
2541 } finally {
2542 Binder.restoreCallingIdentity(identity);
fionaxu0152e512016-11-14 13:36:14 -08002543 }
2544 }
2545
2546 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002547 * Returns the unread count of voicemails
2548 */
2549 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002550 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002551 }
2552
2553 /**
2554 * Returns the unread count of voicemails for a subId
2555 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002556 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002557 public int getVoiceMessageCountForSubscriber( int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002558 final long identity = Binder.clearCallingIdentity();
2559 try {
2560 final Phone phone = getPhone(subId);
2561 if (phone != null) {
2562 return phone.getVoiceMessageCount();
2563 } else {
2564 return 0;
2565 }
2566 } finally {
2567 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002568 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002569 }
2570
2571 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002572 * returns true, if the device is in a state where both voice and data
2573 * are supported simultaneously. This can change based on location or network condition.
2574 */
2575 @Override
2576 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002577 final long identity = Binder.clearCallingIdentity();
2578 try {
2579 final Phone phone = getPhone(subId);
2580 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2581 } finally {
2582 Binder.restoreCallingIdentity(identity);
2583 }
pkanwar8a4dcfb2017-01-19 13:43:16 -08002584 }
2585
2586 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002587 * Send the dialer code if called from the current default dialer or the caller has
2588 * carrier privilege.
2589 * @param inputCode The dialer code to send
2590 */
2591 @Override
2592 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
2593 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2594 String defaultDialer = TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage();
2595 if (!TextUtils.equals(callingPackage, defaultDialer)) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002596 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
2597 getDefaultSubscription(), "sendDialerSpecialCode");
fionaxu235cc5e2017-03-06 22:25:57 -08002598 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002599
2600 final long identity = Binder.clearCallingIdentity();
2601 try {
2602 mPhone.sendDialerSpecialCode(inputCode);
2603 } finally {
2604 Binder.restoreCallingIdentity(identity);
2605 }
fionaxu235cc5e2017-03-06 22:25:57 -08002606 }
2607
2608 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002609 * Returns the data network type.
2610 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002611 *
2612 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2613 */
2614 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002615 public int getNetworkType() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002616 final long identity = Binder.clearCallingIdentity();
2617 try {
2618 final Phone phone = getPhone(getDefaultSubscription());
2619 if (phone != null) {
2620 return phone.getServiceState().getDataNetworkType();
2621 } else {
2622 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2623 }
2624 } finally {
2625 Binder.restoreCallingIdentity(identity);
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002626 }
Wink Saville36469e72014-06-11 15:17:00 -07002627 }
2628
2629 /**
2630 * Returns the network type for a subId
2631 */
2632 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002633 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002634 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002635 mApp, subId, callingPackage, "getNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002636 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2637 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002638
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002639 final long identity = Binder.clearCallingIdentity();
2640 try {
2641 final Phone phone = getPhone(subId);
2642 if (phone != null) {
2643 return phone.getServiceState().getDataNetworkType();
2644 } else {
2645 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2646 }
2647 } finally {
2648 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002649 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002650 }
2651
2652 /**
2653 * Returns the data network type
2654 */
2655 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002656 public int getDataNetworkType(String callingPackage) {
2657 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002658 }
2659
2660 /**
2661 * Returns the data network type for a subId
2662 */
2663 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002664 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002665 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002666 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002667 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2668 }
2669
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002670 final long identity = Binder.clearCallingIdentity();
2671 try {
2672 final Phone phone = getPhone(subId);
2673 if (phone != null) {
2674 return phone.getServiceState().getDataNetworkType();
2675 } else {
2676 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2677 }
2678 } finally {
2679 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002680 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002681 }
2682
2683 /**
Wink Saville36469e72014-06-11 15:17:00 -07002684 * Returns the Voice network type for a subId
2685 */
2686 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002687 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002688 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002689 mApp, subId, callingPackage, "getDataNetworkTypeForSubscriber")) {
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002690 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2691 }
2692
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002693 final long identity = Binder.clearCallingIdentity();
2694 try {
2695 final Phone phone = getPhone(subId);
2696 if (phone != null) {
2697 return phone.getServiceState().getVoiceNetworkType();
2698 } else {
2699 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2700 }
2701 } finally {
2702 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002703 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002704 }
2705
2706 /**
2707 * @return true if a ICC card is present
2708 */
2709 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002710 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002711 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2712 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002713 }
2714
2715 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002716 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002717 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002718 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002719 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002720 final long identity = Binder.clearCallingIdentity();
2721 try {
2722 final Phone phone = PhoneFactory.getPhone(slotIndex);
2723 if (phone != null) {
2724 return phone.getIccCard().hasIccCard();
2725 } else {
2726 return false;
2727 }
2728 } finally {
2729 Binder.restoreCallingIdentity(identity);
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002730 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002731 }
2732
2733 /**
2734 * Return if the current radio is LTE on CDMA. This
2735 * is a tri-state return value as for a period of time
2736 * the mode may be unknown.
2737 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002738 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002739 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002740 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002741 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002742 @Override
2743 public int getLteOnCdmaMode(String callingPackage) {
2744 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002745 }
2746
Sanket Padawe356d7632015-06-22 14:03:32 -07002747 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002748 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002749 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08002750 mApp, subId, callingPackage, "getLteOnCdmaModeForSubscriber")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002751 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2752 }
2753
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002754 final long identity = Binder.clearCallingIdentity();
2755 try {
2756 final Phone phone = getPhone(subId);
2757 if (phone == null) {
2758 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2759 } else {
2760 return phone.getLteOnCdmaMode();
2761 }
2762 } finally {
2763 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07002764 }
Wink Saville36469e72014-06-11 15:17:00 -07002765 }
2766
2767 public void setPhone(Phone phone) {
2768 mPhone = phone;
2769 }
2770
2771 /**
2772 * {@hide}
2773 * Returns Default subId, 0 in the case of single standby.
2774 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002775 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002776 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002777 }
2778
Shishir Agrawala9f32182016-04-12 12:00:16 -07002779 private int getSlotForDefaultSubscription() {
2780 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2781 }
2782
Wink Savilleb564aae2014-10-23 10:18:09 -07002783 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002784 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002785 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002786
2787 /**
2788 * @see android.telephony.TelephonyManager.WifiCallingChoices
2789 */
2790 public int getWhenToMakeWifiCalls() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002791 final long identity = Binder.clearCallingIdentity();
2792 try {
2793 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2794 Settings.System.WHEN_TO_MAKE_WIFI_CALLS,
2795 getWhenToMakeWifiCallsDefaultPreference());
2796 } finally {
2797 Binder.restoreCallingIdentity(identity);
2798 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002799 }
2800
2801 /**
2802 * @see android.telephony.TelephonyManager.WifiCallingChoices
2803 */
2804 public void setWhenToMakeWifiCalls(int preference) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002805 final long identity = Binder.clearCallingIdentity();
2806 try {
2807 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2808 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2809 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
2810 } finally {
2811 Binder.restoreCallingIdentity(identity);
2812 }
Ihab Awadf9e92732013-12-05 18:02:52 -08002813 }
2814
Sailesh Nepald1e68152013-12-12 19:08:02 -08002815 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002816 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002817 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002818 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002819
Shishir Agrawal566b7612013-10-28 14:41:00 -07002820 @Override
Derek Tan740e1672017-06-27 14:56:27 -07002821 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(
2822 int subId, String callingPackage, String aid, int p2) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002823 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2824 mApp, subId, "iccOpenLogicalChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002825 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002826
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002827 final long identity = Binder.clearCallingIdentity();
2828 try {
2829 if (TextUtils.equals(ISDR_AID, aid)) {
2830 // Only allows LPA to open logical channel to ISD-R.
2831 ComponentInfo bestComponent =
2832 EuiccConnector.findBestComponent(mPhone.getContext().getPackageManager());
2833 if (bestComponent == null
2834 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
2835 loge("The calling package is not allowed to access ISD-R.");
2836 throw new SecurityException(
2837 "The calling package is not allowed to access ISD-R.");
2838 }
Derek Tan740e1672017-06-27 14:56:27 -07002839 }
Derek Tan740e1672017-06-27 14:56:27 -07002840
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002841 if (DBG) {
2842 log("iccOpenLogicalChannel: subId=" + subId + " aid=" + aid + " p2=" + p2);
2843 }
2844 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse) sendRequest(
2845 CMD_OPEN_CHANNEL, new Pair<String, Integer>(aid, p2), subId);
2846 if (DBG) log("iccOpenLogicalChannel: " + response);
2847 return response;
2848 } finally {
2849 Binder.restoreCallingIdentity(identity);
2850 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002851 }
2852
2853 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002854 public boolean iccCloseLogicalChannel(int subId, int channel) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002855 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2856 mApp, subId, "iccCloseLogicalChannel");
Shishir Agrawal566b7612013-10-28 14:41:00 -07002857
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002858 final long identity = Binder.clearCallingIdentity();
2859 try {
2860 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
2861 if (channel < 0) {
2862 return false;
2863 }
2864 Boolean success = (Boolean) sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
2865 if (DBG) log("iccCloseLogicalChannel: " + success);
2866 return success;
2867 } finally {
2868 Binder.restoreCallingIdentity(identity);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002869 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002870 }
2871
2872 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002873 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002874 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002875 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2876 mApp, subId, "iccTransmitApduLogicalChannel");
Shishir Agrawal566b7612013-10-28 14:41:00 -07002877
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002878 final long identity = Binder.clearCallingIdentity();
2879 try {
2880 if (DBG) {
2881 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel
2882 + " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3="
2883 + p3 + " data=" + data);
2884 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002885
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002886 if (channel < 0) {
2887 return "";
2888 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002889
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002890 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
2891 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
2892 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002893
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002894 // Append the returned status code to the end of the response payload.
2895 String s = Integer.toHexString(
2896 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2897 if (response.payload != null) {
2898 s = IccUtils.bytesToHexString(response.payload) + s;
2899 }
2900 return s;
2901 } finally {
2902 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002903 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002904 }
Jake Hambye994d462014-02-03 13:10:13 -08002905
Evan Charltonc66da362014-05-16 14:06:40 -07002906 @Override
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08002907 public String iccTransmitApduBasicChannel(int subId, String callingPackage, int cla,
2908 int command, int p1, int p2, int p3, String data) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002909 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2910 mApp, subId, "iccTransmitApduBasicChannel");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002911 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002912
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002913 final long identity = Binder.clearCallingIdentity();
2914 try {
2915 if (command == SELECT_COMMAND && p1 == SELECT_P1 && p2 == SELECT_P2 && p3 == SELECT_P3
2916 && TextUtils.equals(ISDR_AID, data)) {
2917 // Only allows LPA to select ISD-R.
2918 ComponentInfo bestComponent =
2919 EuiccConnector.findBestComponent(mPhone.getContext().getPackageManager());
2920 if (bestComponent == null
2921 || !TextUtils.equals(callingPackage, bestComponent.packageName)) {
2922 loge("The calling package is not allowed to select ISD-R.");
2923 throw new SecurityException(
2924 "The calling package is not allowed to select ISD-R.");
2925 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08002926 }
Holly Jiuyu Sun1cc2d552018-01-26 15:51:16 -08002927
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002928 if (DBG) {
2929 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd="
2930 + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
2931 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002932
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002933 IccIoResult response = (IccIoResult) sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
2934 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
2935 if (DBG) log("iccTransmitApduBasicChannel: " + response);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002936
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002937 // Append the returned status code to the end of the response payload.
2938 String s = Integer.toHexString(
2939 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2940 if (response.payload != null) {
2941 s = IccUtils.bytesToHexString(response.payload) + s;
2942 }
2943 return s;
2944 } finally {
2945 Binder.restoreCallingIdentity(identity);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002946 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002947 }
2948
2949 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002950 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002951 String filePath) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08002952 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
2953 mApp, subId, "iccExchangeSimIO");
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002954
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002955 final long identity = Binder.clearCallingIdentity();
2956 try {
2957 if (DBG) {
2958 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " "
2959 + p1 + " " + p2 + " " + p3 + ":" + filePath);
2960 }
2961
2962 IccIoResult response =
2963 (IccIoResult) sendRequest(CMD_EXCHANGE_SIM_IO,
2964 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2965 subId);
2966
2967 if (DBG) {
2968 log("Exchange SIM_IO [R]" + response);
2969 }
2970
2971 byte[] result = null;
2972 int length = 2;
2973 if (response.payload != null) {
2974 length = 2 + response.payload.length;
2975 result = new byte[length];
2976 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2977 } else {
2978 result = new byte[length];
2979 }
2980
2981 result[length - 1] = (byte) response.sw2;
2982 result[length - 2] = (byte) response.sw1;
2983 return result;
2984 } finally {
2985 Binder.restoreCallingIdentity(identity);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002986 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002987 }
2988
Nathan Haroldb3014052017-01-25 15:57:32 -08002989 /**
2990 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2991 * on a particular subscription
2992 */
sqianb6e41952018-03-12 14:54:01 -07002993 public String[] getForbiddenPlmns(int subId, int appType, String callingPackage) {
2994 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
2995 mApp, subId, callingPackage, "getForbiddenPlmns")) {
2996 return null;
2997 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08002998
2999 final long identity = Binder.clearCallingIdentity();
3000 try {
3001 if (appType != TelephonyManager.APPTYPE_USIM
3002 && appType != TelephonyManager.APPTYPE_SIM) {
3003 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
3004 return null;
3005 }
3006 Object response = sendRequest(
3007 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
3008 if (response instanceof String[]) {
3009 return (String[]) response;
3010 }
3011 // Response is an Exception of some kind,
3012 // which is signalled to the user as a NULL retval
Nathan Haroldb3014052017-01-25 15:57:32 -08003013 return null;
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003014 } finally {
3015 Binder.restoreCallingIdentity(identity);
Nathan Haroldb3014052017-01-25 15:57:32 -08003016 }
Nathan Haroldb3014052017-01-25 15:57:32 -08003017 }
3018
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07003019 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003020 public String sendEnvelopeWithStatus(int subId, String content) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003021 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3022 mApp, subId, "sendEnvelopeWithStatus");
Evan Charltonc66da362014-05-16 14:06:40 -07003023
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003024 final long identity = Binder.clearCallingIdentity();
3025 try {
3026 IccIoResult response = (IccIoResult) sendRequest(CMD_SEND_ENVELOPE, content, subId);
3027 if (response.payload == null) {
3028 return "";
3029 }
Evan Charltonc66da362014-05-16 14:06:40 -07003030
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003031 // Append the returned status code to the end of the response payload.
3032 String s = Integer.toHexString(
3033 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
3034 s = IccUtils.bytesToHexString(response.payload) + s;
3035 return s;
3036 } finally {
3037 Binder.restoreCallingIdentity(identity);
3038 }
Evan Charltonc66da362014-05-16 14:06:40 -07003039 }
3040
Jake Hambye994d462014-02-03 13:10:13 -08003041 /**
3042 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
3043 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
3044 *
3045 * @param itemID the ID of the item to read
3046 * @return the NV item as a String, or null on error.
3047 */
3048 @Override
3049 public String nvReadItem(int itemID) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003050 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3051 mApp, getDefaultSubscription(), "nvReadItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003052
3053 final long identity = Binder.clearCallingIdentity();
3054 try {
3055 if (DBG) log("nvReadItem: item " + itemID);
3056 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
3057 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
3058 return value;
3059 } finally {
3060 Binder.restoreCallingIdentity(identity);
3061 }
Jake Hambye994d462014-02-03 13:10:13 -08003062 }
3063
3064 /**
3065 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
3066 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
3067 *
3068 * @param itemID the ID of the item to read
3069 * @param itemValue the value to write, as a String
3070 * @return true on success; false on any failure
3071 */
3072 @Override
3073 public boolean nvWriteItem(int itemID, String itemValue) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003074 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3075 mApp, getDefaultSubscription(), "nvWriteItem");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003076
3077 final long identity = Binder.clearCallingIdentity();
3078 try {
3079 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
3080 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
3081 new Pair<Integer, String>(itemID, itemValue));
3082 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
3083 return success;
3084 } finally {
3085 Binder.restoreCallingIdentity(identity);
3086 }
Jake Hambye994d462014-02-03 13:10:13 -08003087 }
3088
3089 /**
3090 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
3091 * Used for device configuration by some CDMA operators.
3092 *
3093 * @param preferredRoamingList byte array containing the new PRL
3094 * @return true on success; false on any failure
3095 */
3096 @Override
3097 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003098 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3099 mApp, getDefaultSubscription(), "nvWriteCdmaPrl");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003100
3101 final long identity = Binder.clearCallingIdentity();
3102 try {
3103 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
3104 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
3105 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
3106 return success;
3107 } finally {
3108 Binder.restoreCallingIdentity(identity);
3109 }
Jake Hambye994d462014-02-03 13:10:13 -08003110 }
3111
3112 /**
3113 * Perform the specified type of NV config reset.
3114 * Used for device configuration by some CDMA operators.
3115 *
3116 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
3117 * @return true on success; false on any failure
3118 */
3119 @Override
3120 public boolean nvResetConfig(int resetType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003121 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3122 mApp, getDefaultSubscription(), "nvResetConfig");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003123
3124 final long identity = Binder.clearCallingIdentity();
3125 try {
3126 if (DBG) log("nvResetConfig: type " + resetType);
3127 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
3128 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
3129 return success;
3130 } finally {
3131 Binder.restoreCallingIdentity(identity);
3132 }
Jake Hambye994d462014-02-03 13:10:13 -08003133 }
Jake Hamby7c27be32014-03-03 13:25:59 -08003134
Svet Ganovb320e182015-04-16 12:30:10 -07003135 public String[] getPcscfAddress(String apnType, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003136 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003137 mApp, mPhone.getSubId(), callingPackage, "getPcscfAddress")) {
Svet Ganovb320e182015-04-16 12:30:10 -07003138 return new String[0];
3139 }
3140
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003141 final long identity = Binder.clearCallingIdentity();
3142 try {
3143 return mPhone.getPcscfAddress(apnType);
3144 } finally {
3145 Binder.restoreCallingIdentity(identity);
3146 }
Wink Saville36469e72014-06-11 15:17:00 -07003147 }
3148
Brad Ebinger51f743a2017-01-23 13:50:20 -08003149 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08003150 * Enables IMS for the framework. This will trigger IMS registration and ImsFeature capability
3151 * status updates, if not already enabled.
Brad Ebinger51f743a2017-01-23 13:50:20 -08003152 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08003153 public void enableIms(int slotId) {
Brad Ebinger51f743a2017-01-23 13:50:20 -08003154 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003155
3156 final long identity = Binder.clearCallingIdentity();
3157 try {
3158 PhoneFactory.getImsResolver().enableIms(slotId);
3159 } finally {
3160 Binder.restoreCallingIdentity(identity);
3161 }
Brad Ebinger34bef922017-11-09 10:27:08 -08003162 }
3163
3164 /**
Brad Ebinger22bc3e42018-01-16 09:39:35 -08003165 * Disables IMS for the framework. This will trigger IMS de-registration and trigger ImsFeature
3166 * status updates to disabled.
Brad Ebinger34bef922017-11-09 10:27:08 -08003167 */
Brad Ebinger22bc3e42018-01-16 09:39:35 -08003168 public void disableIms(int slotId) {
3169 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003170
3171 final long identity = Binder.clearCallingIdentity();
3172 try {
3173 PhoneFactory.getImsResolver().disableIms(slotId);
3174 } finally {
3175 Binder.restoreCallingIdentity(identity);
3176 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08003177 }
3178
3179 /**
3180 * Returns the {@link IImsMmTelFeature} that corresponds to the given slot Id for the MMTel
3181 * feature or {@link null} if the service is not available. If the feature is available, the
3182 * {@link IImsServiceFeatureCallback} callback is registered as a listener for feature updates.
3183 */
3184 public IImsMmTelFeature getMmTelFeatureAndListen(int slotId,
Brad Ebinger34bef922017-11-09 10:27:08 -08003185 IImsServiceFeatureCallback callback) {
3186 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003187
3188 final long identity = Binder.clearCallingIdentity();
3189 try {
3190 return PhoneFactory.getImsResolver().getMmTelFeatureAndListen(slotId, callback);
3191 } finally {
3192 Binder.restoreCallingIdentity(identity);
3193 }
Brad Ebinger34bef922017-11-09 10:27:08 -08003194 }
3195
3196 /**
3197 * Returns the {@link IImsRcsFeature} that corresponds to the given slot Id for the RCS
3198 * feature during emergency calling or {@link null} if the service is not available. If the
3199 * feature is available, the {@link IImsServiceFeatureCallback} callback is registered as a
3200 * listener for feature updates.
3201 */
3202 public IImsRcsFeature getRcsFeatureAndListen(int slotId, IImsServiceFeatureCallback callback) {
3203 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003204
3205 final long identity = Binder.clearCallingIdentity();
3206 try {
3207 return PhoneFactory.getImsResolver().getRcsFeatureAndListen(slotId, callback);
3208 } finally {
3209 Binder.restoreCallingIdentity(identity);
3210 }
Brad Ebinger51f743a2017-01-23 13:50:20 -08003211 }
3212
Brad Ebinger5f64b052017-12-14 14:26:15 -08003213 /**
3214 * Returns the {@link IImsRegistration} structure associated with the slotId and feature
3215 * specified.
3216 */
3217 public IImsRegistration getImsRegistration(int slotId, int feature) throws RemoteException {
3218 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003219
3220 final long identity = Binder.clearCallingIdentity();
3221 try {
3222 return PhoneFactory.getImsResolver().getImsRegistration(slotId, feature);
3223 } finally {
3224 Binder.restoreCallingIdentity(identity);
3225 }
Brad Ebinger5f64b052017-12-14 14:26:15 -08003226 }
3227
Brad Ebinger22bc3e42018-01-16 09:39:35 -08003228 /**
3229 * Returns the {@link IImsConfig} structure associated with the slotId and feature
3230 * specified.
3231 */
3232 public IImsConfig getImsConfig(int slotId, int feature) throws RemoteException {
3233 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003234
3235 final long identity = Binder.clearCallingIdentity();
3236 try {
3237 return PhoneFactory.getImsResolver().getImsConfig(slotId, feature);
3238 } finally {
3239 Binder.restoreCallingIdentity(identity);
3240 }
Brad Ebinger22bc3e42018-01-16 09:39:35 -08003241 }
3242
Brad Ebinger884c07b2018-02-15 16:17:40 -08003243 /**
Brad Ebingerdac2f002018-04-03 15:17:52 -07003244 * Sets the ImsService Package Name that Telephony will bind to.
3245 *
3246 * @param slotId the slot ID that the ImsService should bind for.
3247 * @param isCarrierImsService true if the ImsService is the carrier override, false if the
3248 * ImsService is the device default ImsService.
3249 * @param packageName The package name of the application that contains the ImsService to bind
3250 * to.
3251 * @return true if setting the ImsService to bind to succeeded, false if it did not.
3252 * @hide
3253 */
3254 public boolean setImsService(int slotId, boolean isCarrierImsService, String packageName) {
Brad Ebingerde696de2018-04-06 09:56:40 -07003255 int[] subIds = SubscriptionManager.getSubId(slotId);
3256 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
3257 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
3258 "setImsService");
3259
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003260 final long identity = Binder.clearCallingIdentity();
3261 try {
3262 return PhoneFactory.getImsResolver().overrideImsServiceConfiguration(slotId,
3263 isCarrierImsService, packageName);
3264 } finally {
3265 Binder.restoreCallingIdentity(identity);
3266 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07003267 }
3268
3269 /**
3270 * Return the ImsService configuration.
3271 *
3272 * @param slotId The slot that the ImsService is associated with.
3273 * @param isCarrierImsService true, if the ImsService is a carrier override, false if it is
3274 * the device default.
3275 * @return the package name of the ImsService configuration.
3276 */
3277 public String getImsService(int slotId, boolean isCarrierImsService) {
Brad Ebingerde696de2018-04-06 09:56:40 -07003278 int[] subIds = SubscriptionManager.getSubId(slotId);
3279 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(mApp,
3280 (subIds != null ? subIds[0] : SubscriptionManager.INVALID_SUBSCRIPTION_ID),
3281 "getImsService");
3282
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003283 final long identity = Binder.clearCallingIdentity();
3284 try {
3285 return PhoneFactory.getImsResolver().getImsServiceConfiguration(slotId,
3286 isCarrierImsService);
3287 } finally {
3288 Binder.restoreCallingIdentity(identity);
3289 }
Brad Ebingerdac2f002018-04-03 15:17:52 -07003290 }
3291
Wink Saville36469e72014-06-11 15:17:00 -07003292 public void setImsRegistrationState(boolean registered) {
3293 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003294
3295 final long identity = Binder.clearCallingIdentity();
3296 try {
3297 mPhone.setImsRegistrationState(registered);
3298 } finally {
3299 Binder.restoreCallingIdentity(identity);
3300 }
Wink Saville36469e72014-06-11 15:17:00 -07003301 }
3302
3303 /**
Stuart Scott54788802015-03-30 13:18:01 -07003304 * Set the network selection mode to automatic.
3305 *
3306 */
3307 @Override
3308 public void setNetworkSelectionModeAutomatic(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003309 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3310 mApp, subId, "setNetworkSelectionModeAutomatic");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003311
3312 final long identity = Binder.clearCallingIdentity();
3313 try {
3314 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
3315 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
3316 } finally {
3317 Binder.restoreCallingIdentity(identity);
3318 }
Stuart Scott54788802015-03-30 13:18:01 -07003319 }
3320
3321 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07003322 * Set the network selection mode to manual with the selected carrier.
3323 */
3324 @Override
yinxu6e5abd72017-12-01 11:35:19 -08003325 public boolean setNetworkSelectionModeManual(int subId, String operatorNumeric,
Shishir Agrawal77ba3172015-09-10 14:50:19 -07003326 boolean persistSelection) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003327 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3328 mApp, subId, "setNetworkSelectionModeManual");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003329
3330 final long identity = Binder.clearCallingIdentity();
3331 try {
3332 OperatorInfo operator = new OperatorInfo(
yinxu6e5abd72017-12-01 11:35:19 -08003333 /* operatorAlphaLong */ "",
3334 /* operatorAlphaShort */ "",
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003335 operatorNumeric);
3336 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
3337 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
3338 persistSelection);
3339 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
3340 } finally {
3341 Binder.restoreCallingIdentity(identity);
3342 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07003343 }
3344
3345 /**
3346 * Scans for available networks.
3347 */
3348 @Override
3349 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003350 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3351 mApp, subId, "getCellNetworkScanResults");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003352
3353 final long identity = Binder.clearCallingIdentity();
3354 try {
3355 if (DBG) log("getCellNetworkScanResults: subId " + subId);
3356 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
3357 CMD_PERFORM_NETWORK_SCAN, null, subId);
3358 return result;
3359 } finally {
3360 Binder.restoreCallingIdentity(identity);
3361 }
Shishir Agrawal302c8692015-06-19 13:49:39 -07003362 }
3363
3364 /**
yinxub1bed742017-04-17 11:45:04 -07003365 * Starts a new network scan and returns the id of this scan.
yinxu504e1392017-04-12 16:03:22 -07003366 *
yinxub1bed742017-04-17 11:45:04 -07003367 * @param subId id of the subscription
3368 * @param request contains the radio access networks with bands/channels to scan
3369 * @param messenger callback messenger for scan results or errors
3370 * @param binder for the purpose of auto clean when the user thread crashes
yinxu504e1392017-04-12 16:03:22 -07003371 * @return the id of the requested scan which can be used to stop the scan.
3372 */
3373 @Override
3374 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
3375 IBinder binder) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003376 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3377 mApp, subId, "requestNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003378
3379 final long identity = Binder.clearCallingIdentity();
3380 try {
3381 return mNetworkScanRequestTracker.startNetworkScan(
3382 request, messenger, binder, getPhone(subId));
3383 } finally {
3384 Binder.restoreCallingIdentity(identity);
3385 }
yinxu504e1392017-04-12 16:03:22 -07003386 }
3387
3388 /**
3389 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07003390 *
3391 * @param subId id of the subscription
3392 * @param scanId id of the scan that needs to be stopped
yinxu504e1392017-04-12 16:03:22 -07003393 */
3394 @Override
3395 public void stopNetworkScan(int subId, int scanId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003396 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3397 mApp, subId, "stopNetworkScan");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003398
3399 final long identity = Binder.clearCallingIdentity();
3400 try {
3401 mNetworkScanRequestTracker.stopNetworkScan(scanId);
3402 } finally {
3403 Binder.restoreCallingIdentity(identity);
3404 }
yinxu504e1392017-04-12 16:03:22 -07003405 }
3406
3407 /**
Junda Liu84d15a22014-07-02 11:21:04 -07003408 * Get the calculated preferred network type.
3409 * Used for debugging incorrect network type.
3410 *
3411 * @return the preferred network type, defined in RILConstants.java.
3412 */
3413 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003414 public int getCalculatedPreferredNetworkType(String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003415 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003416 mApp, mPhone.getSubId(), callingPackage, "getCalculatedPreferredNetworkType")) {
Svet Ganovb320e182015-04-16 12:30:10 -07003417 return RILConstants.PREFERRED_NETWORK_MODE;
3418 }
3419
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003420 final long identity = Binder.clearCallingIdentity();
3421 try {
3422 // FIXME: need to get SubId from somewhere.
3423 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0);
3424 } finally {
3425 Binder.restoreCallingIdentity(identity);
3426 }
Junda Liu84d15a22014-07-02 11:21:04 -07003427 }
3428
3429 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08003430 * Get the preferred network type.
3431 * Used for device configuration by some CDMA operators.
3432 *
3433 * @return the preferred network type, defined in RILConstants.java.
3434 */
3435 @Override
Stuart Scott54788802015-03-30 13:18:01 -07003436 public int getPreferredNetworkType(int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003437 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3438 mApp, subId, "getPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003439
3440 final long identity = Binder.clearCallingIdentity();
3441 try {
3442 if (DBG) log("getPreferredNetworkType");
3443 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
3444 int networkType = (result != null ? result[0] : -1);
3445 if (DBG) log("getPreferredNetworkType: " + networkType);
3446 return networkType;
3447 } finally {
3448 Binder.restoreCallingIdentity(identity);
3449 }
Jake Hamby7c27be32014-03-03 13:25:59 -08003450 }
3451
3452 /**
3453 * Set the preferred network type.
3454 * Used for device configuration by some CDMA operators.
3455 *
3456 * @param networkType the preferred network type, defined in RILConstants.java.
3457 * @return true on success; false on any failure.
3458 */
3459 @Override
Stuart Scott54788802015-03-30 13:18:01 -07003460 public boolean setPreferredNetworkType(int subId, int networkType) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003461 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3462 mApp, subId, "setPreferredNetworkType");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003463
3464 final long identity = Binder.clearCallingIdentity();
3465 try {
3466 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
3467 Boolean success = (Boolean) sendRequest(
3468 CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
3469 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
3470 if (success) {
3471 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
3472 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
3473 }
3474 return success;
3475 } finally {
3476 Binder.restoreCallingIdentity(identity);
Junda Liu80bc0d12014-07-14 16:36:44 -07003477 }
Jake Hamby7c27be32014-03-03 13:25:59 -08003478 }
Robert Greenwalted86e582014-05-21 20:03:20 -07003479
3480 /**
Junda Liu475951f2014-11-07 16:45:03 -08003481 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
3482 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
3483 * tethering.
3484 *
3485 * @return 0: Not required. 1: required. 2: Not set.
3486 * @hide
3487 */
3488 @Override
3489 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003490 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003491
3492 final long identity = Binder.clearCallingIdentity();
3493 try {
3494 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
3495 Settings.Global.TETHER_DUN_REQUIRED, 2);
3496 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
3497 // config_tether_apndata.
3498 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
3499 dunRequired = 1;
3500 }
3501 return dunRequired;
3502 } finally {
3503 Binder.restoreCallingIdentity(identity);
Junda Liu475951f2014-11-07 16:45:03 -08003504 }
Junda Liu475951f2014-11-07 16:45:03 -08003505 }
3506
3507 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07003508 * Set mobile data enabled
3509 * Used by the user through settings etc to turn on/off mobile data
3510 *
3511 * @param enable {@code true} turn turn data on, else {@code false}
3512 */
3513 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08003514 public void setUserDataEnabled(int subId, boolean enable) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003515 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3516 mApp, subId, "setUserDataEnabled");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003517
3518 final long identity = Binder.clearCallingIdentity();
3519 try {
3520 int phoneId = mSubscriptionController.getPhoneId(subId);
3521 if (DBG) log("setUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
3522 Phone phone = PhoneFactory.getPhone(phoneId);
3523 if (phone != null) {
3524 if (DBG) log("setUserDataEnabled: subId=" + subId + " enable=" + enable);
3525 phone.setUserDataEnabled(enable);
3526 } else {
3527 loge("setUserDataEnabled: no phone for subId=" + subId);
3528 }
3529 } finally {
3530 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08003531 }
Robert Greenwalted86e582014-05-21 20:03:20 -07003532 }
3533
3534 /**
Malcolm Chen964682d2017-11-28 16:20:07 -08003535 * Get the user enabled state of Mobile Data.
3536 *
3537 * TODO: remove and use isUserDataEnabled.
3538 * This can't be removed now because some vendor codes
3539 * calls through ITelephony directly while they should
3540 * use TelephonyManager.
3541 *
3542 * @return true on enabled
3543 */
3544 @Override
3545 public boolean getDataEnabled(int subId) {
3546 return isUserDataEnabled(subId);
3547 }
3548
3549 /**
3550 * Get whether mobile data is enabled per user setting.
3551 *
3552 * There are other factors deciding whether mobile data is actually enabled, but they are
3553 * not considered here. See {@link #isDataEnabled(int)} for more details.
Robert Greenwalt646120a2014-05-23 11:54:03 -07003554 *
Jeff Davidsona1920712016-11-18 17:05:56 -08003555 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07003556 *
3557 * @return {@code true} if data is enabled else {@code false}
3558 */
3559 @Override
Malcolm Chen964682d2017-11-28 16:20:07 -08003560 public boolean isUserDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07003561 try {
3562 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
3563 null);
3564 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003565 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3566 mApp, subId, "isUserDataEnabled");
Robert Greenwalt646120a2014-05-23 11:54:03 -07003567 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003568
3569 final long identity = Binder.clearCallingIdentity();
3570 try {
3571 int phoneId = mSubscriptionController.getPhoneId(subId);
3572 if (DBG) log("isUserDataEnabled: subId=" + subId + " phoneId=" + phoneId);
3573 Phone phone = PhoneFactory.getPhone(phoneId);
3574 if (phone != null) {
3575 boolean retVal = phone.isUserDataEnabled();
3576 if (DBG) log("isUserDataEnabled: subId=" + subId + " retVal=" + retVal);
3577 return retVal;
3578 } else {
3579 if (DBG) loge("isUserDataEnabled: no phone subId=" + subId + " retVal=false");
3580 return false;
3581 }
3582 } finally {
3583 Binder.restoreCallingIdentity(identity);
Malcolm Chen964682d2017-11-28 16:20:07 -08003584 }
3585 }
3586
3587 /**
3588 * Get whether mobile data is enabled.
3589 *
3590 * Comparable to {@link #isUserDataEnabled(int)}, this considers all factors deciding
3591 * whether mobile data is actually enabled.
3592 *
3593 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
3594 *
3595 * @return {@code true} if data is enabled else {@code false}
3596 */
3597 @Override
3598 public boolean isDataEnabled(int subId) {
3599 try {
3600 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
3601 null);
3602 } catch (Exception e) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003603 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
3604 mApp, subId, "isDataEnabled");
Malcolm Chen964682d2017-11-28 16:20:07 -08003605 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003606
3607 final long identity = Binder.clearCallingIdentity();
3608 try {
3609 int phoneId = mSubscriptionController.getPhoneId(subId);
3610 if (DBG) log("isDataEnabled: subId=" + subId + " phoneId=" + phoneId);
3611 Phone phone = PhoneFactory.getPhone(phoneId);
3612 if (phone != null) {
3613 boolean retVal = phone.isDataEnabled();
3614 if (DBG) log("isDataEnabled: subId=" + subId + " retVal=" + retVal);
3615 return retVal;
3616 } else {
3617 if (DBG) loge("isDataEnabled: no phone subId=" + subId + " retVal=false");
3618 return false;
3619 }
3620 } finally {
3621 Binder.restoreCallingIdentity(identity);
Wink Savillee7353bb2014-12-05 14:21:41 -08003622 }
Robert Greenwalted86e582014-05-21 20:03:20 -07003623 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07003624
3625 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003626 public int getCarrierPrivilegeStatus(int subId) {
3627 final Phone phone = getPhone(subId);
3628 if (phone == null) {
3629 loge("getCarrierPrivilegeStatus: Invalid subId");
3630 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
3631 }
3632 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07003633 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08003634 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07003635 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
3636 }
3637 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003638 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07003639 }
Junda Liu29340342014-07-10 15:23:27 -07003640
3641 @Override
Jeff Davidson7e17e312018-02-13 18:17:36 -08003642 public int getCarrierPrivilegeStatusForUid(int subId, int uid) {
3643 final Phone phone = getPhone(subId);
3644 if (phone == null) {
3645 loge("getCarrierPrivilegeStatus: Invalid subId");
3646 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
3647 }
3648 UiccProfile profile =
3649 UiccController.getInstance().getUiccProfileForPhone(phone.getPhoneId());
3650 if (profile == null) {
3651 loge("getCarrierPrivilegeStatus: No UICC");
3652 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
3653 }
3654 return profile.getCarrierPrivilegeStatusForUid(phone.getContext().getPackageManager(), uid);
3655 }
3656
3657 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07003658 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08003659 if (TextUtils.isEmpty(pkgName))
3660 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08003661 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07003662 if (card == null) {
3663 loge("checkCarrierPrivilegesForPackage: No UICC");
3664 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
3665 }
Zach Johnson50ecba32015-05-19 00:24:21 -07003666 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
3667 }
3668
3669 @Override
3670 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08003671 if (TextUtils.isEmpty(pkgName))
3672 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07003673 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
3674 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
3675 UiccCard card = UiccController.getInstance().getUiccCard(i);
3676 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07003677 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07003678 continue;
3679 }
3680
3681 result = card.getCarrierPrivilegeStatus(
3682 mPhone.getContext().getPackageManager(), pkgName);
3683 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
3684 break;
3685 }
3686 }
3687
3688 return result;
Junda Liu29340342014-07-10 15:23:27 -07003689 }
Derek Tan89e89d42014-07-08 17:00:10 -07003690
3691 @Override
Junda Liue64de782015-04-16 17:19:16 -07003692 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
3693 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
3694 loge("phoneId " + phoneId + " is not valid.");
3695 return null;
3696 }
3697 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07003698 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07003699 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07003700 return null ;
3701 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07003702 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07003703 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07003704 }
3705
Amith Yamasani6e118872016-02-19 12:53:51 -08003706 @Override
3707 public List<String> getPackagesWithCarrierPrivileges() {
3708 PackageManager pm = mPhone.getContext().getPackageManager();
3709 List<String> privilegedPackages = new ArrayList<>();
3710 List<PackageInfo> packages = null;
3711 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
3712 UiccCard card = UiccController.getInstance().getUiccCard(i);
3713 if (card == null) {
3714 // No UICC in that slot.
3715 continue;
3716 }
3717 if (card.hasCarrierPrivilegeRules()) {
3718 if (packages == null) {
3719 // Only check packages in user 0 for now
3720 packages = pm.getInstalledPackagesAsUser(
3721 PackageManager.MATCH_DISABLED_COMPONENTS
3722 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
3723 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
3724 }
3725 for (int p = packages.size() - 1; p >= 0; p--) {
3726 PackageInfo pkgInfo = packages.get(p);
3727 if (pkgInfo != null && pkgInfo.packageName != null
3728 && card.getCarrierPrivilegeStatus(pkgInfo)
3729 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
3730 privilegedPackages.add(pkgInfo.packageName);
3731 }
3732 }
3733 }
3734 }
3735 return privilegedPackages;
3736 }
3737
Wink Savilleb564aae2014-10-23 10:18:09 -07003738 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07003739 final Phone phone = getPhone(subId);
3740 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07003741 if (card == null) {
3742 loge("getIccId: No UICC");
3743 return null;
3744 }
3745 String iccId = card.getIccId();
3746 if (TextUtils.isEmpty(iccId)) {
3747 loge("getIccId: ICC ID is null or empty.");
3748 return null;
3749 }
3750 return iccId;
3751 }
3752
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07003753 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08003754 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
3755 String number) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003756 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3757 subId, "setLine1NumberForDisplayForSubscriber");
Derek Tan97ebb422014-09-05 16:55:38 -07003758
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003759 final long identity = Binder.clearCallingIdentity();
3760 try {
3761 final String iccId = getIccId(subId);
3762 final Phone phone = getPhone(subId);
3763 if (phone == null) {
3764 return false;
3765 }
3766 final String subscriberId = phone.getSubscriberId();
3767
3768 if (DBG_MERGE) {
3769 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
3770 + subscriberId + " to " + number);
3771 }
3772
3773 if (TextUtils.isEmpty(iccId)) {
3774 return false;
3775 }
3776
3777 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
3778
3779 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
3780 if (alphaTag == null) {
3781 editor.remove(alphaTagPrefKey);
3782 } else {
3783 editor.putString(alphaTagPrefKey, alphaTag);
3784 }
3785
3786 // Record both the line number and IMSI for this ICCID, since we need to
3787 // track all merged IMSIs based on line number
3788 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3789 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3790 if (number == null) {
3791 editor.remove(numberPrefKey);
3792 editor.remove(subscriberPrefKey);
3793 } else {
3794 editor.putString(numberPrefKey, number);
3795 editor.putString(subscriberPrefKey, subscriberId);
3796 }
3797
3798 editor.commit();
3799 return true;
3800 } finally {
3801 Binder.restoreCallingIdentity(identity);
Sanket Padawe356d7632015-06-22 14:03:32 -07003802 }
Derek Tan7226c842014-07-02 17:42:23 -07003803 }
3804
3805 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003806 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07003807 // This is open to apps with WRITE_SMS.
Jeff Davidson7e17e312018-02-13 18:17:36 -08003808 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneNumber(
Jeff Davidson913390f2018-02-23 17:11:49 -08003809 mApp, subId, callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08003810 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07003811 return null;
3812 }
Derek Tan97ebb422014-09-05 16:55:38 -07003813
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003814 final long identity = Binder.clearCallingIdentity();
3815 try {
3816 String iccId = getIccId(subId);
3817 if (iccId != null) {
3818 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3819 if (DBG_MERGE) {
3820 log("getLine1NumberForDisplay returning "
3821 + mTelephonySharedPreferences.getString(numberPrefKey, null));
3822 }
3823 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Amit Mahajan9cf11512015-11-09 11:40:48 -08003824 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003825 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
3826 return null;
3827 } finally {
3828 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07003829 }
Derek Tan7226c842014-07-02 17:42:23 -07003830 }
3831
3832 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003833 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003834 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003835 mApp, subId, callingPackage, "getLine1AlphaTagForDisplay")) {
Svet Ganovb320e182015-04-16 12:30:10 -07003836 return null;
3837 }
Derek Tan97ebb422014-09-05 16:55:38 -07003838
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003839 final long identity = Binder.clearCallingIdentity();
3840 try {
3841 String iccId = getIccId(subId);
3842 if (iccId != null) {
3843 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
3844 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
3845 }
3846 return null;
3847 } finally {
3848 Binder.restoreCallingIdentity(identity);
Derek Tan7226c842014-07-02 17:42:23 -07003849 }
Derek Tan7226c842014-07-02 17:42:23 -07003850 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003851
3852 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07003853 public String[] getMergedSubscriberIds(String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08003854 // This API isn't public, so no need to provide a valid subscription ID - we're not worried
3855 // about carrier-privileged callers not having access.
Jeff Davidson7e17e312018-02-13 18:17:36 -08003856 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08003857 mApp, SubscriptionManager.INVALID_SUBSCRIPTION_ID, callingPackage,
3858 "getMergedSubscriberIds")) {
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07003859 return null;
3860 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08003861
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07003862 final long identity = Binder.clearCallingIdentity();
3863 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003864 final Context context = mPhone.getContext();
3865 final TelephonyManager tele = TelephonyManager.from(context);
3866 final SubscriptionManager sub = SubscriptionManager.from(context);
3867
3868 // Figure out what subscribers are currently active
3869 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
3870 // Clear calling identity, when calling TelephonyManager, because callerUid must be
3871 // the process, where TelephonyManager was instantiated.
3872 // Otherwise AppOps check will fail.
3873
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07003874 final int[] subIds = sub.getActiveSubscriptionIdList();
3875 for (int subId : subIds) {
3876 activeSubscriberIds.add(tele.getSubscriberId(subId));
3877 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003878
3879 // First pass, find a number override for an active subscriber
3880 String mergeNumber = null;
3881 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
3882 for (String key : prefs.keySet()) {
3883 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
3884 final String subscriberId = (String) prefs.get(key);
3885 if (activeSubscriberIds.contains(subscriberId)) {
3886 final String iccId = key.substring(
3887 PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
3888 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3889 mergeNumber = (String) prefs.get(numberKey);
3890 if (DBG_MERGE) {
3891 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3892 + " for active subscriber " + subscriberId);
3893 }
3894 if (!TextUtils.isEmpty(mergeNumber)) {
3895 break;
3896 }
3897 }
3898 }
3899 }
3900
3901 // Shortcut when no active merged subscribers
3902 if (TextUtils.isEmpty(mergeNumber)) {
3903 return null;
3904 }
3905
3906 // Second pass, find all subscribers under that line override
3907 final ArraySet<String> result = new ArraySet<>();
3908 for (String key : prefs.keySet()) {
3909 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3910 final String number = (String) prefs.get(key);
3911 if (mergeNumber.equals(number)) {
3912 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3913 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3914 final String subscriberId = (String) prefs.get(subscriberKey);
3915 if (!TextUtils.isEmpty(subscriberId)) {
3916 result.add(subscriberId);
3917 }
3918 }
3919 }
3920 }
3921
3922 final String[] resultArray = result.toArray(new String[result.size()]);
3923 Arrays.sort(resultArray);
3924 if (DBG_MERGE) {
3925 Slog.d(LOG_TAG,
3926 "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3927 }
3928 return resultArray;
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07003929 } finally {
3930 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08003931 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08003932 }
3933
3934 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003935 public boolean setOperatorBrandOverride(int subId, String brand) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003936 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
3937 subId, "setOperatorBrandOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003938
3939 final long identity = Binder.clearCallingIdentity();
3940 try {
3941 final Phone phone = getPhone(subId);
3942 return phone == null ? false : phone.setOperatorBrandOverride(brand);
3943 } finally {
3944 Binder.restoreCallingIdentity(identity);
3945 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003946 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003947
3948 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003949 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003950 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3951 List<String> cdmaNonRoamingList) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08003952 TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, "setRoamingOverride");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08003953
3954 final long identity = Binder.clearCallingIdentity();
3955 try {
3956 final Phone phone = getPhone(subId);
3957 if (phone == null) {
3958 return false;
3959 }
3960 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
3961 cdmaNonRoamingList);
3962 } finally {
3963 Binder.restoreCallingIdentity(identity);
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003964 }
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003965 }
3966
3967 @Override
Shuo Qian850e4d6a2018-04-25 21:02:08 +00003968 @Deprecated
3969 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3970 enforceModifyPermission();
3971
3972 int returnValue = 0;
3973 try {
3974 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3975 if(result.exception == null) {
3976 if (result.result != null) {
3977 byte[] responseData = (byte[])(result.result);
3978 if(responseData.length > oemResp.length) {
3979 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3980 responseData.length + "bytes. Buffer Size is " +
3981 oemResp.length + "bytes.");
3982 }
3983 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3984 returnValue = responseData.length;
3985 }
3986 } else {
3987 CommandException ex = (CommandException) result.exception;
3988 returnValue = ex.getCommandError().ordinal();
3989 if(returnValue > 0) returnValue *= -1;
3990 }
3991 } catch (RuntimeException e) {
3992 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3993 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3994 if(returnValue > 0) returnValue *= -1;
3995 }
3996
3997 return returnValue;
3998 }
3999
4000 @Override
Wink Saville5d475dd2014-10-17 15:00:58 -07004001 public void setRadioCapability(RadioAccessFamily[] rafs) {
4002 try {
4003 ProxyController.getInstance().setRadioCapability(rafs);
4004 } catch (RuntimeException e) {
4005 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
4006 }
4007 }
4008
4009 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004010 public int getRadioAccessFamily(int phoneId, String callingPackage) {
Jeff Davidson913390f2018-02-23 17:11:49 -08004011 Phone phone = PhoneFactory.getPhone(phoneId);
4012 if (phone == null) {
4013 return RadioAccessFamily.RAF_UNKNOWN;
4014 }
4015 int subId = phone.getSubId();
Jeff Davidson7e17e312018-02-13 18:17:36 -08004016 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08004017 mApp, subId, callingPackage, "getRadioAccessFamily")) {
Robert Greenwalt36b23af2015-07-06 17:59:14 -07004018 return RadioAccessFamily.RAF_UNKNOWN;
4019 }
4020
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004021 final long identity = Binder.clearCallingIdentity();
4022 try {
4023 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
4024 } finally {
4025 Binder.restoreCallingIdentity(identity);
4026 }
Wink Saville5d475dd2014-10-17 15:00:58 -07004027 }
Andrew Leedf14ead2014-10-17 14:22:52 -07004028
4029 @Override
4030 public void enableVideoCalling(boolean enable) {
4031 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004032
4033 final long identity = Binder.clearCallingIdentity();
4034 try {
4035 ImsManager.getInstance(mPhone.getContext(), mPhone.getPhoneId()).setVtSetting(enable);
4036 } finally {
4037 Binder.restoreCallingIdentity(identity);
4038 }
Andrew Leedf14ead2014-10-17 14:22:52 -07004039 }
4040
4041 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07004042 public boolean isVideoCallingEnabled(String callingPackage) {
Amit Mahajan578e53d2018-03-20 16:18:38 +00004043 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
4044 mApp, mPhone.getSubId(), callingPackage, "isVideoCallingEnabled")) {
4045 return false;
4046 }
Svet Ganovb320e182015-04-16 12:30:10 -07004047
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004048 final long identity = Binder.clearCallingIdentity();
4049 try {
4050 // Check the user preference and the system-level IMS setting. Even if the user has
4051 // enabled video calling, if IMS is disabled we aren't able to support video calling.
4052 // In the long run, we may instead need to check if there exists a connection service
4053 // which can support video calling.
4054 ImsManager imsManager =
4055 ImsManager.getInstance(mPhone.getContext(), mPhone.getPhoneId());
4056 return imsManager.isVtEnabledByPlatform()
4057 && imsManager.isEnhanced4gLteModeSettingEnabledByUser()
4058 && imsManager.isVtEnabledByUser();
4059 } finally {
4060 Binder.restoreCallingIdentity(identity);
4061 }
Andrew Leedf14ead2014-10-17 14:22:52 -07004062 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06004063
Andrew Leea1239f22015-03-02 17:44:07 -08004064 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004065 public boolean canChangeDtmfToneLength(int subId, String callingPackage) {
4066 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
4067 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
4068 return false;
4069 }
4070
4071 final long identity = Binder.clearCallingIdentity();
4072 try {
4073 CarrierConfigManager configManager =
4074 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
4075 return configManager.getConfigForSubId(mPhone.getSubId())
4076 .getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
4077 } finally {
4078 Binder.restoreCallingIdentity(identity);
4079 }
Andrew Leea1239f22015-03-02 17:44:07 -08004080 }
4081
4082 @Override
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004083 public boolean isWorldPhone(int subId, String callingPackage) {
4084 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
4085 mApp, subId, callingPackage, "isVideoCallingEnabled")) {
4086 return false;
4087 }
4088
4089 final long identity = Binder.clearCallingIdentity();
4090 try {
4091 CarrierConfigManager configManager =
4092 (CarrierConfigManager) mApp.getSystemService(Context.CARRIER_CONFIG_SERVICE);
4093 return configManager.getConfigForSubId(mPhone.getSubId())
4094 .getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
4095 } finally {
4096 Binder.restoreCallingIdentity(identity);
4097 }
Andrew Leea1239f22015-03-02 17:44:07 -08004098 }
4099
Andrew Lee9431b832015-03-09 18:46:45 -07004100 @Override
4101 public boolean isTtyModeSupported() {
4102 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
Wooki Wu1f82f7a2016-02-15 15:59:58 +08004103 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07004104 }
4105
4106 @Override
4107 public boolean isHearingAidCompatibilitySupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004108 final long identity = Binder.clearCallingIdentity();
4109 try {
4110 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
4111 } finally {
4112 Binder.restoreCallingIdentity(identity);
4113 }
Andrew Lee9431b832015-03-09 18:46:45 -07004114 }
4115
Hall Liu98187582018-01-22 19:15:32 -08004116 public boolean isRttSupported() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004117 final long identity = Binder.clearCallingIdentity();
4118 try {
4119 boolean isCarrierSupported = mApp.getCarrierConfigForSubId(
4120 mPhone.getSubId()).getBoolean(
4121 CarrierConfigManager.KEY_RTT_SUPPORTED_BOOL);
4122 boolean isDeviceSupported =
4123 mPhone.getContext().getResources().getBoolean(R.bool.config_support_rtt);
4124 return isCarrierSupported && isDeviceSupported;
4125 } finally {
4126 Binder.restoreCallingIdentity(identity);
4127 }
Hall Liu98187582018-01-22 19:15:32 -08004128 }
4129
Hall Liu3ad5f012018-04-06 16:23:39 -07004130 public boolean isRttEnabled() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004131 final long identity = Binder.clearCallingIdentity();
4132 try {
4133 return isRttSupported() && Settings.Secure.getInt(
4134 mPhone.getContext().getContentResolver(),
4135 Settings.Secure.RTT_CALLING_MODE, 0) != 0;
4136 } finally {
4137 Binder.restoreCallingIdentity(identity);
4138 }
Hall Liu3ad5f012018-04-06 16:23:39 -07004139 }
4140
Sanket Padawe7310cc72015-01-14 09:53:20 -08004141 /**
4142 * Returns the unique device ID of phone, for example, the IMEI for
4143 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
4144 *
4145 * <p>Requires Permission:
4146 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
4147 */
4148 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07004149 public String getDeviceId(String callingPackage) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08004150 final Phone phone = PhoneFactory.getPhone(0);
Jeff Davidson913390f2018-02-23 17:11:49 -08004151 if (phone == null) {
Sanket Padawe7310cc72015-01-14 09:53:20 -08004152 return null;
4153 }
Jeff Davidson913390f2018-02-23 17:11:49 -08004154 int subId = phone.getSubId();
4155 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
4156 mApp, subId, callingPackage, "getDeviceId")) {
4157 return null;
4158 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004159
4160 final long identity = Binder.clearCallingIdentity();
4161 try {
4162 return phone.getDeviceId();
4163 } finally {
4164 Binder.restoreCallingIdentity(identity);
4165 }
Sanket Padawe7310cc72015-01-14 09:53:20 -08004166 }
4167
Ping Sunc67b7c22016-03-02 19:16:45 +08004168 /**
4169 * {@hide}
4170 * Returns the IMS Registration Status on a particular subid
4171 *
4172 * @param subId
4173 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004174 public boolean isImsRegistered(int subId) {
Ping Sunc67b7c22016-03-02 19:16:45 +08004175 Phone phone = getPhone(subId);
4176 if (phone != null) {
4177 return phone.isImsRegistered();
4178 } else {
4179 return false;
4180 }
4181 }
4182
Santos Cordon7a1885b2015-02-03 11:15:19 -08004183 @Override
4184 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004185 final long identity = Binder.clearCallingIdentity();
4186 try {
4187 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
4188 } finally {
4189 Binder.restoreCallingIdentity(identity);
4190 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08004191 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07004192
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004193 /**
4194 * @return the VoWiFi calling availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07004195 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004196 public boolean isWifiCallingAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004197 final long identity = Binder.clearCallingIdentity();
4198 try {
4199 Phone phone = getPhone(subId);
4200 if (phone != null) {
4201 return phone.isWifiCallingEnabled();
4202 } else {
4203 return false;
4204 }
4205 } finally {
4206 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004207 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07004208 }
4209
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004210 /**
4211 * @return the VoLTE availability.
Nathan Haroldc55097a2015-03-11 18:14:50 -07004212 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004213 public boolean isVolteAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004214 final long identity = Binder.clearCallingIdentity();
4215 try {
4216 Phone phone = getPhone(subId);
4217 if (phone != null) {
4218 return phone.isVolteEnabled();
4219 } else {
4220 return false;
4221 }
4222 } finally {
4223 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004224 }
Nathan Haroldc55097a2015-03-11 18:14:50 -07004225 }
Svet Ganovb320e182015-04-16 12:30:10 -07004226
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004227 /**
4228 * @return the VT calling availability.
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07004229 */
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004230 public boolean isVideoTelephonyAvailable(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004231 final long identity = Binder.clearCallingIdentity();
4232 try {
4233 Phone phone = getPhone(subId);
4234 if (phone != null) {
4235 return phone.isVideoEnabled();
4236 } else {
4237 return false;
4238 }
4239 } finally {
4240 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004241 }
4242 }
4243
4244 /**
4245 * @return the IMS registration technology for the MMTEL feature. Valid return values are
4246 * defined in {@link ImsRegistrationImplBase}.
4247 */
4248 public @ImsRegistrationImplBase.ImsRegistrationTech int getImsRegTechnologyForMmTel(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004249 final long identity = Binder.clearCallingIdentity();
4250 try {
4251 Phone phone = getPhone(subId);
4252 if (phone != null) {
4253 return phone.getImsRegistrationTech();
4254 } else {
4255 return ImsRegistrationImplBase.REGISTRATION_TECH_NONE;
4256 }
4257 } finally {
4258 Binder.restoreCallingIdentity(identity);
Brad Ebinger1f2b5082018-02-08 16:11:32 -08004259 }
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07004260 }
4261
Stuart Scott8eef64f2015-04-08 15:13:54 -07004262 @Override
4263 public void factoryReset(int subId) {
4264 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07004265 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
4266 return;
4267 }
4268
Svet Ganovcc087f82015-05-12 20:35:54 -07004269 final long identity = Binder.clearCallingIdentity();
4270 try {
Stuart Scott981d8582015-04-21 14:09:50 -07004271 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
4272 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Pengquan Meng85728fb2018-03-12 16:31:21 -07004273 setUserDataEnabled(subId, getDefaultDataEnabled());
Svet Ganovcc087f82015-05-12 20:35:54 -07004274 setNetworkSelectionModeAutomatic(subId);
Pengquan Meng85728fb2018-03-12 16:31:21 -07004275 setPreferredNetworkType(subId, getDefaultNetworkType(subId));
4276 mPhone.setDataRoamingEnabled(getDefaultDataRoamingEnabled(subId));
pkanwar79ec0542017-07-31 14:10:01 -07004277 CarrierInfoManager.deleteAllCarrierKeysForImsiEncryption(mPhone.getContext());
Svet Ganovcc087f82015-05-12 20:35:54 -07004278 }
4279 } finally {
4280 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07004281 }
4282 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01004283
4284 @Override
4285 public String getLocaleFromDefaultSim() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004286 final long identity = Binder.clearCallingIdentity();
4287 try {
4288 // We query all subscriptions instead of just the active ones, because
4289 // this might be called early on in the provisioning flow when the
4290 // subscriptions potentially aren't active yet.
4291 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
4292 if (slist == null || slist.isEmpty()) {
Narayan Kamath1c496c22015-04-16 14:40:19 +01004293 return null;
4294 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01004295
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004296 // This function may be called very early, say, from the setup wizard, at
4297 // which point we won't have a default subscription set. If that's the case
4298 // we just choose the first, which will be valid in "most cases".
4299 final int defaultSubId = getDefaultSubscription();
4300 SubscriptionInfo info = null;
4301 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
4302 info = slist.get(0);
4303 } else {
4304 for (SubscriptionInfo item : slist) {
4305 if (item.getSubscriptionId() == defaultSubId) {
4306 info = item;
4307 break;
4308 }
4309 }
4310
4311 if (info == null) {
4312 return null;
Tony Hill183b2de2015-06-24 14:53:58 +01004313 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01004314 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01004315
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004316 // Try and fetch the locale from the carrier properties or from the SIM language
4317 // preferences (EF-PL and EF-LI)...
4318 final int mcc = info.getMcc();
4319 final Phone defaultPhone = getPhone(info.getSubscriptionId());
4320 String simLanguage = null;
4321 if (defaultPhone != null) {
4322 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
4323 if (localeFromDefaultSim != null) {
4324 if (!localeFromDefaultSim.getCountry().isEmpty()) {
4325 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
4326 return localeFromDefaultSim.toLanguageTag();
4327 } else {
4328 simLanguage = localeFromDefaultSim.getLanguage();
4329 }
4330 }
4331 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01004332
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004333 // The SIM language preferences only store a language (e.g. fr = French), not an
4334 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
4335 // the SIM and carrier preferences does not include a country we add the country
4336 // determined from the SIM MCC to provide an exact locale.
4337 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc,
4338 simLanguage);
4339 if (mccLocale != null) {
4340 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
4341 return mccLocale.toLanguageTag();
4342 }
4343
4344 if (DBG) log("No locale found - returning null");
4345 return null;
4346 } finally {
4347 Binder.restoreCallingIdentity(identity);
4348 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01004349 }
4350
4351 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004352 return mSubscriptionController.getAllSubInfoList(
4353 mPhone.getContext().getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01004354 }
4355
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004356 /**
4357 * NOTE: this method assumes permission checks are done and caller identity has been cleared.
4358 */
4359 private List<SubscriptionInfo> getActiveSubscriptionInfoListPrivileged() {
4360 return mSubscriptionController.getActiveSubscriptionInfoList(
4361 mPhone.getContext().getOpPackageName());
Narayan Kamath1c496c22015-04-16 14:40:19 +01004362 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07004363
Chenjie Yu1ba97252018-01-11 18:16:20 -08004364 private final ModemActivityInfo mLastModemActivityInfo =
4365 new ModemActivityInfo(0, 0, 0, new int[0], 0, 0);
4366
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07004367 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07004368 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
4369 * representing the state of the modem.
4370 *
Chenjie Yu1ba97252018-01-11 18:16:20 -08004371 * NOTE: The underlying implementation clears the modem state, so there should only ever be one
4372 * caller to it. Everyone should call this class to get cumulative data.
Adam Lesinski903a54c2016-04-11 14:49:52 -07004373 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07004374 */
4375 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07004376 public void requestModemActivityInfo(ResultReceiver result) {
4377 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004378
4379 final long identity = Binder.clearCallingIdentity();
4380 try {
4381 ModemActivityInfo ret = null;
4382 synchronized (mLastModemActivityInfo) {
4383 ModemActivityInfo info = (ModemActivityInfo) sendRequest(
4384 CMD_GET_MODEM_ACTIVITY_INFO,
4385 null);
Siddharth Rayb8114062018-06-17 15:02:38 -07004386 if (isModemActivityInfoValid(info)) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004387 int[] mergedTxTimeMs = new int[ModemActivityInfo.TX_POWER_LEVELS];
4388 for (int i = 0; i < mergedTxTimeMs.length; i++) {
4389 mergedTxTimeMs[i] =
4390 info.getTxTimeMillis()[i]
4391 + mLastModemActivityInfo.getTxTimeMillis()[i];
4392 }
4393 mLastModemActivityInfo.setTimestamp(info.getTimestamp());
4394 mLastModemActivityInfo.setSleepTimeMillis(
4395 info.getSleepTimeMillis()
4396 + mLastModemActivityInfo.getSleepTimeMillis());
4397 mLastModemActivityInfo.setIdleTimeMillis(
4398 info.getIdleTimeMillis() + mLastModemActivityInfo.getIdleTimeMillis());
4399 mLastModemActivityInfo.setTxTimeMillis(mergedTxTimeMs);
4400 mLastModemActivityInfo.setRxTimeMillis(
4401 info.getRxTimeMillis() + mLastModemActivityInfo.getRxTimeMillis());
4402 mLastModemActivityInfo.setEnergyUsed(
4403 info.getEnergyUsed() + mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08004404 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004405 ret = new ModemActivityInfo(mLastModemActivityInfo.getTimestamp(),
4406 mLastModemActivityInfo.getSleepTimeMillis(),
4407 mLastModemActivityInfo.getIdleTimeMillis(),
4408 mLastModemActivityInfo.getTxTimeMillis(),
4409 mLastModemActivityInfo.getRxTimeMillis(),
4410 mLastModemActivityInfo.getEnergyUsed());
Chenjie Yu1ba97252018-01-11 18:16:20 -08004411 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004412 Bundle bundle = new Bundle();
4413 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, ret);
4414 result.send(0, bundle);
4415 } finally {
4416 Binder.restoreCallingIdentity(identity);
Chenjie Yu1ba97252018-01-11 18:16:20 -08004417 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07004418 }
Jack Yu85bd38a2015-11-09 11:34:32 -08004419
Siddharth Rayb8114062018-06-17 15:02:38 -07004420 // Checks that ModemActivityInfo is valid. Sleep time, Idle time, Rx time and Tx time should be
4421 // less than total activity duration.
4422 private boolean isModemActivityInfoValid(ModemActivityInfo info) {
4423 if (info == null) {
4424 return false;
4425 }
4426 int activityDurationMs =
4427 (int) (info.getTimestamp() - mLastModemActivityInfo.getTimestamp());
4428 int totalTxTimeMs = 0;
4429 for (int i = 0; i < info.getTxTimeMillis().length; i++) {
4430 totalTxTimeMs += info.getTxTimeMillis()[i];
4431 }
4432 return (info.isValid()
4433 && (info.getSleepTimeMillis() <= activityDurationMs)
4434 && (info.getIdleTimeMillis() <= activityDurationMs)
4435 && (info.getRxTimeMillis() <= activityDurationMs)
4436 && (totalTxTimeMs <= activityDurationMs));
4437 }
4438
Jack Yu85bd38a2015-11-09 11:34:32 -08004439 /**
4440 * {@hide}
4441 * Returns the service state information on specified subscription.
4442 */
4443 @Override
4444 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004445 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08004446 mApp, subId, callingPackage, "getServiceStateForSubscriber")) {
Jack Yu85bd38a2015-11-09 11:34:32 -08004447 return null;
4448 }
4449
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004450 final long identity = Binder.clearCallingIdentity();
4451 try {
4452 final Phone phone = getPhone(subId);
4453 if (phone == null) {
4454 return null;
4455 }
Jack Yu85bd38a2015-11-09 11:34:32 -08004456
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004457 return phone.getServiceState();
4458 } finally {
4459 Binder.restoreCallingIdentity(identity);
4460 }
Jack Yu85bd38a2015-11-09 11:34:32 -08004461 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08004462
4463 /**
4464 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
4465 *
4466 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
4467 * voicemail ringtone.
4468 * @return The URI for the ringtone to play when receiving a voicemail from a specific
4469 * PhoneAccount.
4470 */
4471 @Override
4472 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004473 final long identity = Binder.clearCallingIdentity();
4474 try {
4475 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
4476 if (phone == null) {
4477 phone = mPhone;
4478 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08004479
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004480 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone.getContext());
4481 } finally {
4482 Binder.restoreCallingIdentity(identity);
4483 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08004484 }
4485
4486 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08004487 * Sets the per-account voicemail ringtone.
4488 *
4489 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
4490 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
4491 *
4492 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
4493 * voicemail ringtone.
4494 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
4495 * PhoneAccount.
4496 */
4497 @Override
4498 public void setVoicemailRingtoneUri(String callingPackage,
4499 PhoneAccountHandle phoneAccountHandle, Uri uri) {
4500 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4501 if (!TextUtils.equals(callingPackage,
4502 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004503 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4504 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
4505 "setVoicemailRingtoneUri");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08004506 }
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004507
4508 final long identity = Binder.clearCallingIdentity();
4509 try {
4510 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
4511 if (phone == null) {
4512 phone = mPhone;
4513 }
4514 VoicemailNotificationSettingsUtil.setRingtoneUri(phone.getContext(), uri);
4515 } finally {
4516 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08004517 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08004518 }
4519
4520 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08004521 * Returns whether vibration is set for voicemail notification in Phone settings.
4522 *
4523 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
4524 * voicemail vibration setting.
4525 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
4526 */
4527 @Override
4528 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004529 final long identity = Binder.clearCallingIdentity();
4530 try {
4531 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
4532 if (phone == null) {
4533 phone = mPhone;
4534 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08004535
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004536 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone.getContext());
4537 } finally {
4538 Binder.restoreCallingIdentity(identity);
4539 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08004540 }
4541
Youhan Wange64578a2016-05-02 15:32:42 -07004542 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08004543 * Sets the per-account voicemail vibration.
4544 *
4545 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
4546 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
4547 *
4548 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
4549 * voicemail vibration setting.
4550 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
4551 * specific PhoneAccount.
4552 */
4553 @Override
4554 public void setVoicemailVibrationEnabled(String callingPackage,
4555 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
4556 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
4557 if (!TextUtils.equals(callingPackage,
4558 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004559 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4560 mApp, PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle),
4561 "setVoicemailVibrationEnabled");
Ta-wei Yenc33877d2017-01-23 18:11:21 -08004562 }
4563
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004564 final long identity = Binder.clearCallingIdentity();
4565 try {
4566 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
4567 if (phone == null) {
4568 phone = mPhone;
4569 }
4570 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone.getContext(), enabled);
4571 } finally {
4572 Binder.restoreCallingIdentity(identity);
Ta-wei Yenc33877d2017-01-23 18:11:21 -08004573 }
Ta-wei Yenc33877d2017-01-23 18:11:21 -08004574 }
4575
4576 /**
Youhan Wange64578a2016-05-02 15:32:42 -07004577 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
4578 *
4579 * @throws SecurityException if the caller does not have the required permission
4580 */
4581 private void enforceReadPrivilegedPermission() {
4582 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
4583 null);
4584 }
4585
4586 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004587 * Make sure either called from same process as self (phone) or IPC caller has send SMS
4588 * permission.
4589 *
4590 * @throws SecurityException if the caller does not have the required permission
4591 */
4592 private void enforceSendSmsPermission() {
4593 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
4594 }
4595
4596 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004597 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004598 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004599 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004600 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08004601 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004602 final long identity = Binder.clearCallingIdentity();
4603 try {
4604 ComponentName componentName =
4605 RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
4606 if (componentName == null) {
4607 throw new SecurityException(
4608 "Caller not current active visual voicemail package[null]");
4609 }
4610 String vvmPackage = componentName.getPackageName();
4611 if (!callingPackage.equals(vvmPackage)) {
4612 throw new SecurityException("Caller not current active visual voicemail package["
4613 + vvmPackage + "]");
4614 }
4615 } finally {
4616 Binder.restoreCallingIdentity(identity);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08004617 }
4618 }
4619
4620 /**
Youhan Wange64578a2016-05-02 15:32:42 -07004621 * Return the application ID for the app type.
4622 *
4623 * @param subId the subscription ID that this request applies to.
4624 * @param appType the uicc app type.
4625 * @return Application ID for specificied app type, or null if no uicc.
4626 */
4627 @Override
4628 public String getAidForAppType(int subId, int appType) {
4629 enforceReadPrivilegedPermission();
4630 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004631
4632 final long identity = Binder.clearCallingIdentity();
Youhan Wange64578a2016-05-02 15:32:42 -07004633 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004634 if (phone == null) {
4635 return null;
4636 }
4637 String aid = null;
4638 try {
4639 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
4640 .getApplicationByType(appType).getAid();
4641 } catch (Exception e) {
4642 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
4643 }
4644 return aid;
4645 } finally {
4646 Binder.restoreCallingIdentity(identity);
Youhan Wange64578a2016-05-02 15:32:42 -07004647 }
Youhan Wange64578a2016-05-02 15:32:42 -07004648 }
4649
Youhan Wang4001d252016-05-11 10:29:41 -07004650 /**
4651 * Return the Electronic Serial Number.
4652 *
4653 * @param subId the subscription ID that this request applies to.
4654 * @return ESN or null if error.
4655 */
4656 @Override
4657 public String getEsn(int subId) {
4658 enforceReadPrivilegedPermission();
4659 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004660
4661 final long identity = Binder.clearCallingIdentity();
Youhan Wang4001d252016-05-11 10:29:41 -07004662 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004663 if (phone == null) {
4664 return null;
4665 }
4666 String esn = null;
4667 try {
4668 esn = phone.getEsn();
4669 } catch (Exception e) {
4670 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
4671 }
4672 return esn;
4673 } finally {
4674 Binder.restoreCallingIdentity(identity);
Youhan Wang4001d252016-05-11 10:29:41 -07004675 }
Youhan Wang4001d252016-05-11 10:29:41 -07004676 }
4677
Sanket Padawe99ef1e32016-05-18 16:12:33 -07004678 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07004679 * Return the Preferred Roaming List Version.
4680 *
4681 * @param subId the subscription ID that this request applies to.
4682 * @return PRLVersion or null if error.
4683 */
4684 @Override
4685 public String getCdmaPrlVersion(int subId) {
4686 enforceReadPrivilegedPermission();
4687 Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004688
4689 final long identity = Binder.clearCallingIdentity();
Youhan Wang66ad5d72016-07-18 17:56:58 -07004690 try {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004691 if (phone == null) {
4692 return null;
4693 }
4694 String cdmaPrlVersion = null;
4695 try {
4696 cdmaPrlVersion = phone.getCdmaPrlVersion();
4697 } catch (Exception e) {
4698 Log.e(LOG_TAG, "Not getting PRLVersion", e);
4699 }
4700 return cdmaPrlVersion;
4701 } finally {
4702 Binder.restoreCallingIdentity(identity);
Youhan Wang66ad5d72016-07-18 17:56:58 -07004703 }
Youhan Wang66ad5d72016-07-18 17:56:58 -07004704 }
4705
4706 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07004707 * Get snapshot of Telephony histograms
4708 * @return List of Telephony histograms
4709 * @hide
4710 */
4711 @Override
4712 public List<TelephonyHistogram> getTelephonyHistograms() {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004713 TelephonyPermissions.enforceCallingOrSelfModifyPermissionOrCarrierPrivilege(
4714 mApp, getDefaultSubscription(), "getTelephonyHistograms");
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004715
4716 final long identity = Binder.clearCallingIdentity();
4717 try {
4718 return RIL.getTelephonyRILTimingHistograms();
4719 } finally {
4720 Binder.restoreCallingIdentity(identity);
4721 }
Sanket Padawe99ef1e32016-05-18 16:12:33 -07004722 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07004723
4724 /**
4725 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004726 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07004727 * Require system privileges. In the future we may add this to carrier APIs.
4728 *
4729 * @return The number of carriers set successfully, should match length of carriers
4730 */
4731 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004732 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07004733 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004734
Meng Wang9b7c4e92017-02-17 11:41:27 -08004735 if (carriers == null) {
4736 throw new NullPointerException("carriers cannot be null");
4737 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004738
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004739 final long identity = Binder.clearCallingIdentity();
4740 try {
4741 int subId = SubscriptionManager.getSubId(slotIndex)[0];
4742 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
4743 return retVal[0];
4744 } finally {
4745 Binder.restoreCallingIdentity(identity);
4746 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07004747 }
4748
4749 /**
4750 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004751 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07004752 * Require system privileges. In the future we may add this to carrier APIs.
4753 *
4754 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
4755 * means all carriers are allowed.
4756 */
4757 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004758 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07004759 enforceReadPrivilegedPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004760
4761 final long identity = Binder.clearCallingIdentity();
4762 try {
4763 int subId = SubscriptionManager.getSubId(slotIndex)[0];
4764 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
4765 } finally {
4766 Binder.restoreCallingIdentity(identity);
4767 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07004768 }
4769
fionaxu59545b42016-05-25 15:53:37 -07004770 /**
4771 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
4772 * @param subId the subscription ID that this action applies to.
4773 * @param enabled control enable or disable metered apns.
4774 * {@hide}
4775 */
4776 @Override
4777 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
4778 enforceModifyPermission();
4779 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004780
4781 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07004782 if (phone == null) {
4783 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
4784 return;
4785 }
4786 try {
4787 phone.carrierActionSetMeteredApnsEnabled(enabled);
4788 } catch (Exception e) {
4789 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004790 } finally {
4791 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07004792 }
4793 }
4794
4795 /**
4796 * Action set from carrier signalling broadcast receivers to enable/disable radio
4797 * @param subId the subscription ID that this action applies to.
4798 * @param enabled control enable or disable radio.
4799 * {@hide}
4800 */
4801 @Override
4802 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
4803 enforceModifyPermission();
4804 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004805
4806 final long identity = Binder.clearCallingIdentity();
fionaxu59545b42016-05-25 15:53:37 -07004807 if (phone == null) {
4808 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
4809 return;
4810 }
4811 try {
4812 phone.carrierActionSetRadioEnabled(enabled);
4813 } catch (Exception e) {
4814 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004815 } finally {
4816 Binder.restoreCallingIdentity(identity);
fionaxu59545b42016-05-25 15:53:37 -07004817 }
4818 }
4819
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07004820 /**
fionaxu8da9cb12017-05-23 15:02:46 -07004821 * Action set from carrier signalling broadcast receivers to start/stop reporting the default
4822 * network status based on which carrier apps could apply actions accordingly,
4823 * enable/disable default url handler for example.
4824 *
4825 * @param subId the subscription ID that this action applies to.
4826 * @param report control start/stop reporting the default network status.
4827 * {@hide}
4828 */
4829 @Override
4830 public void carrierActionReportDefaultNetworkStatus(int subId, boolean report) {
4831 enforceModifyPermission();
4832 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004833
4834 final long identity = Binder.clearCallingIdentity();
fionaxu8da9cb12017-05-23 15:02:46 -07004835 if (phone == null) {
4836 loge("carrierAction: ReportDefaultNetworkStatus fails with invalid sibId: " + subId);
4837 return;
4838 }
4839 try {
4840 phone.carrierActionReportDefaultNetworkStatus(report);
4841 } catch (Exception e) {
4842 Log.e(LOG_TAG, "carrierAction: ReportDefaultNetworkStatus fails. Exception ex=" + e);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004843 } finally {
4844 Binder.restoreCallingIdentity(identity);
fionaxu8da9cb12017-05-23 15:02:46 -07004845 }
4846 }
4847
4848 /**
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07004849 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
4850 * bug report is being generated.
4851 */
4852 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07004853 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07004854 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
4855 != PackageManager.PERMISSION_GRANTED) {
4856 writer.println("Permission Denial: can't dump Phone from pid="
4857 + Binder.getCallingPid()
4858 + ", uid=" + Binder.getCallingUid()
4859 + "without permission "
4860 + android.Manifest.permission.DUMP);
4861 return;
4862 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07004863 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07004864 }
Jack Yueb89b242016-06-22 13:27:47 -07004865
Brad Ebingerdac2f002018-04-03 15:17:52 -07004866 @Override
4867 public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err,
4868 String[] args, ShellCallback callback, ResultReceiver resultReceiver)
4869 throws RemoteException {
4870 (new TelephonyShellCommand(this)).exec(this, in, out, err, args, callback, resultReceiver);
4871 }
4872
Jack Yueb89b242016-06-22 13:27:47 -07004873 /**
Jack Yu84291ec2017-05-26 16:07:50 -07004874 * Get aggregated video call data usage since boot.
4875 *
4876 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
4877 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07004878 * {@hide}
4879 */
4880 @Override
Jack Yu84291ec2017-05-26 16:07:50 -07004881 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07004882 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
4883 null);
4884
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004885 final long identity = Binder.clearCallingIdentity();
4886 try {
4887 // NetworkStatsService keeps tracking the active network interface and identity. It
4888 // records the delta with the corresponding network identity.
4889 // We just return the total video call data usage snapshot since boot.
4890 Phone phone = getPhone(subId);
4891 if (phone != null) {
4892 return phone.getVtDataUsage(perUidStats);
4893 }
4894 return null;
4895 } finally {
4896 Binder.restoreCallingIdentity(identity);
Jack Yueb89b242016-06-22 13:27:47 -07004897 }
Jack Yueb89b242016-06-22 13:27:47 -07004898 }
Jack Yu75ab2952016-07-08 14:29:33 -07004899
4900 /**
4901 * Policy control of data connection. Usually used when data limit is passed.
4902 * @param enabled True if enabling the data, otherwise disabling.
4903 * @param subId Subscription index
4904 * {@hide}
4905 */
4906 @Override
4907 public void setPolicyDataEnabled(boolean enabled, int subId) {
4908 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004909
4910 final long identity = Binder.clearCallingIdentity();
4911 try {
4912 Phone phone = getPhone(subId);
4913 if (phone != null) {
4914 phone.setPolicyDataEnabled(enabled);
4915 }
4916 } finally {
4917 Binder.restoreCallingIdentity(identity);
Jack Yu75ab2952016-07-08 14:29:33 -07004918 }
4919 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07004920
4921 /**
4922 * Get Client request stats
4923 * @return List of Client Request Stats
4924 * @hide
4925 */
4926 @Override
4927 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
Jeff Davidson7e17e312018-02-13 18:17:36 -08004928 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
Jeff Davidson913390f2018-02-23 17:11:49 -08004929 mApp, subId, callingPackage, "getClientRequestStats")) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07004930 return null;
4931 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07004932 Phone phone = getPhone(subId);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07004933
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004934 final long identity = Binder.clearCallingIdentity();
4935 try {
4936 if (phone != null) {
4937 return phone.getClientRequestStats();
4938 }
4939
4940 return null;
4941 } finally {
4942 Binder.restoreCallingIdentity(identity);
4943 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07004944 }
4945
Narayan Kamathf04b5a12018-01-09 11:47:15 +00004946 private WorkSource getWorkSource(int uid) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07004947 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
Narayan Kamathf04b5a12018-01-09 11:47:15 +00004948 return new WorkSource(uid, packageName);
Sooraj Sasindran9a909312016-07-18 11:57:25 -07004949 }
Jack Yueb4124c2017-02-16 15:32:43 -08004950
4951 /**
Grace Chen70990072017-03-24 17:21:30 -07004952 * Set SIM card power state.
Jack Yueb4124c2017-02-16 15:32:43 -08004953 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004954 * @param slotIndex SIM slot id.
Grace Chen70990072017-03-24 17:21:30 -07004955 * @param state State of SIM (power down, power up, pass through)
4956 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
4957 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
4958 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yueb4124c2017-02-16 15:32:43 -08004959 *
4960 **/
4961 @Override
Grace Chen70990072017-03-24 17:21:30 -07004962 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yueb4124c2017-02-16 15:32:43 -08004963 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07004964 Phone phone = PhoneFactory.getPhone(slotIndex);
4965
Malcolm Chenaa4a8532018-02-28 15:00:40 -08004966 final long identity = Binder.clearCallingIdentity();
4967 try {
4968 if (phone != null) {
4969 phone.setSimPowerState(state);
4970 }
4971 } finally {
4972 Binder.restoreCallingIdentity(identity);
Jack Yueb4124c2017-02-16 15:32:43 -08004973 }
4974 }
Shuo Qiandd210312017-04-12 22:11:33 +00004975
Tyler Gunn65d45c22017-06-05 11:22:26 -07004976 private boolean isUssdApiAllowed(int subId) {
4977 CarrierConfigManager configManager =
4978 (CarrierConfigManager) mPhone.getContext().getSystemService(
4979 Context.CARRIER_CONFIG_SERVICE);
4980 if (configManager == null) {
4981 return false;
4982 }
4983 PersistableBundle pb = configManager.getConfigForSubId(subId);
4984 if (pb == null) {
4985 return false;
4986 }
4987 return pb.getBoolean(
4988 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
4989 }
4990
Shuo Qiandd210312017-04-12 22:11:33 +00004991 /**
4992 * Check if phone is in emergency callback mode
4993 * @return true if phone is in emergency callback mode
4994 * @param subId sub id
4995 */
goneil9c5f4872017-12-05 14:07:56 -08004996 @Override
Shuo Qiandd210312017-04-12 22:11:33 +00004997 public boolean getEmergencyCallbackMode(int subId) {
goneil9c5f4872017-12-05 14:07:56 -08004998 enforceReadPrivilegedPermission();
Shuo Qiandd210312017-04-12 22:11:33 +00004999 final Phone phone = getPhone(subId);
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005000
5001 final long identity = Binder.clearCallingIdentity();
5002 try {
5003 if (phone != null) {
5004 return phone.isInEcm();
5005 } else {
5006 return false;
5007 }
5008 } finally {
5009 Binder.restoreCallingIdentity(identity);
Shuo Qiandd210312017-04-12 22:11:33 +00005010 }
5011 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08005012
5013 /**
5014 * Get the current signal strength information for the given subscription.
5015 * Because this information is not updated when the device is in a low power state
5016 * it should not be relied-upon to be current.
5017 * @param subId Subscription index
5018 * @return the most recent cached signal strength info from the modem
5019 */
5020 @Override
5021 public SignalStrength getSignalStrength(int subId) {
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005022 final long identity = Binder.clearCallingIdentity();
5023 try {
5024 Phone p = getPhone(subId);
5025 if (p == null) {
5026 return null;
5027 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08005028
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005029 return p.getSignalStrength();
5030 } finally {
5031 Binder.restoreCallingIdentity(identity);
5032 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08005033 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00005034
5035 @Override
5036 public UiccSlotInfo[] getUiccSlotsInfo() {
5037 enforceReadPrivilegedPermission();
5038
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005039 final long identity = Binder.clearCallingIdentity();
5040 try {
5041 UiccSlot[] slots = UiccController.getInstance().getUiccSlots();
5042 if (slots == null) {
5043 Rlog.i(LOG_TAG, "slots is null.");
5044 return null;
5045 }
5046
5047 UiccSlotInfo[] infos = new UiccSlotInfo[slots.length];
5048 for (int i = 0; i < slots.length; i++) {
5049 UiccSlot slot = slots[i];
5050 if (slot == null) {
5051 continue;
5052 }
5053
5054 String cardId;
5055 UiccCard card = slot.getUiccCard();
5056 if (card != null) {
5057 cardId = card.getCardId();
5058 } else {
5059 cardId = slot.getIccId();
5060 }
5061
5062 int cardState = 0;
5063 switch (slot.getCardState()) {
5064 case CARDSTATE_ABSENT:
5065 cardState = UiccSlotInfo.CARD_STATE_INFO_ABSENT;
5066 break;
5067 case CARDSTATE_PRESENT:
5068 cardState = UiccSlotInfo.CARD_STATE_INFO_PRESENT;
5069 break;
5070 case CARDSTATE_ERROR:
5071 cardState = UiccSlotInfo.CARD_STATE_INFO_ERROR;
5072 break;
5073 case CARDSTATE_RESTRICTED:
5074 cardState = UiccSlotInfo.CARD_STATE_INFO_RESTRICTED;
5075 break;
5076 default:
5077 break;
5078
5079 }
5080
5081 infos[i] = new UiccSlotInfo(
5082 slot.isActive(),
5083 slot.isEuicc(),
5084 cardId,
5085 cardState,
5086 slot.getPhoneId(),
5087 slot.isExtendedApduSupported());
5088 }
5089 return infos;
5090 } finally {
5091 Binder.restoreCallingIdentity(identity);
Holly Jiuyu Sun1d957c52018-04-04 13:52:42 -07005092 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00005093 }
5094
5095 @Override
5096 public boolean switchSlots(int[] physicalSlots) {
5097 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005098
5099 final long identity = Binder.clearCallingIdentity();
5100 try {
5101 return (Boolean) sendRequest(CMD_SWITCH_SLOTS, physicalSlots);
5102 } finally {
5103 Binder.restoreCallingIdentity(identity);
5104 }
Holly Jiuyu Sun01c47ad2018-01-24 17:56:33 +00005105 }
Jack Yu4c988042018-02-27 15:30:01 -08005106
5107 @Override
5108 public void setRadioIndicationUpdateMode(int subId, int filters, int mode) {
5109 enforceModifyPermission();
5110 final Phone phone = getPhone(subId);
5111 if (phone == null) {
5112 loge("setRadioIndicationUpdateMode fails with invalid subId: " + subId);
5113 return;
5114 }
5115
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005116 final long identity = Binder.clearCallingIdentity();
5117 try {
5118 phone.setRadioIndicationUpdateMode(filters, mode);
5119 } finally {
5120 Binder.restoreCallingIdentity(identity);
5121 }
Jack Yu4c988042018-02-27 15:30:01 -08005122 }
Pengquan Meng85728fb2018-03-12 16:31:21 -07005123
5124 /**
goneil47ffb6e2018-04-06 15:40:58 -07005125 * A test API to reload the UICC profile.
5126 *
5127 * <p>Requires that the calling app has permission
5128 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
5129 * @hide
5130 */
5131 @Override
5132 public void refreshUiccProfile(int subId) {
5133 enforceModifyPermission();
5134
5135 final long identity = Binder.clearCallingIdentity();
5136 try {
5137 Phone phone = getPhone(subId);
5138 if (phone == null) {
5139 return;
5140 }
5141 UiccCard uiccCard = phone.getUiccCard();
5142 if (uiccCard == null) {
5143 return;
5144 }
5145 UiccProfile uiccProfile = uiccCard.getUiccProfile();
5146 if (uiccProfile == null) {
5147 return;
5148 }
5149 uiccProfile.refresh();
5150 } finally {
5151 Binder.restoreCallingIdentity(identity);
5152 }
5153 }
5154
5155 /**
Pengquan Meng85728fb2018-03-12 16:31:21 -07005156 * Returns false if the mobile data is disabled by default, otherwise return true.
5157 */
5158 private boolean getDefaultDataEnabled() {
5159 return "true".equalsIgnoreCase(
5160 SystemProperties.get(DEFAULT_MOBILE_DATA_PROPERTY_NAME, "true"));
5161 }
5162
5163 /**
5164 * Returns true if the data roaming is enabled by default, i.e the system property
5165 * of {@link #DEFAULT_DATA_ROAMING_PROPERTY_NAME} is true or the config of
5166 * {@link CarrierConfigManager#KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL} is true.
5167 */
5168 private boolean getDefaultDataRoamingEnabled(int subId) {
5169 final CarrierConfigManager configMgr = (CarrierConfigManager)
5170 mPhone.getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
5171 boolean isDataRoamingEnabled = "true".equalsIgnoreCase(
5172 SystemProperties.get(DEFAULT_DATA_ROAMING_PROPERTY_NAME, "false"));
5173 isDataRoamingEnabled |= configMgr.getConfigForSubId(subId).getBoolean(
5174 CarrierConfigManager.KEY_CARRIER_DEFAULT_DATA_ROAMING_ENABLED_BOOL);
5175 return isDataRoamingEnabled;
5176 }
5177
5178 /**
5179 * Returns the default network type for the given {@code subId}, if the default network type is
5180 * not set, return {@link Phone#PREFERRED_NT_MODE}.
5181 */
5182 private int getDefaultNetworkType(int subId) {
5183 return Integer.parseInt(
5184 TelephonyManager.getTelephonyProperty(
5185 mSubscriptionController.getPhoneId(subId),
5186 DEFAULT_NETWORK_MODE_PROPERTY_NAME,
5187 String.valueOf(Phone.PREFERRED_NT_MODE)));
5188 }
fionaxua13278b2018-03-21 00:08:13 -07005189
5190 @Override
5191 public void setCarrierTestOverride(int subId, String mccmnc, String imsi, String iccid, String
5192 gid1, String gid2, String plmn, String spn) {
5193 enforceModifyPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005194
5195 final long identity = Binder.clearCallingIdentity();
5196 try {
5197 final Phone phone = getPhone(subId);
5198 if (phone == null) {
5199 loge("setCarrierTestOverride fails with invalid subId: " + subId);
5200 return;
5201 }
5202 phone.setCarrierTestOverride(mccmnc, imsi, iccid, gid1, gid2, plmn, spn);
5203 } finally {
5204 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07005205 }
fionaxua13278b2018-03-21 00:08:13 -07005206 }
5207
5208 @Override
5209 public int getCarrierIdListVersion(int subId) {
5210 enforceReadPrivilegedPermission();
Malcolm Chenaa4a8532018-02-28 15:00:40 -08005211
5212 final long identity = Binder.clearCallingIdentity();
5213 try {
5214 final Phone phone = getPhone(subId);
5215 if (phone == null) {
5216 loge("getCarrierIdListVersion fails with invalid subId: " + subId);
5217 return TelephonyManager.UNKNOWN_CARRIER_ID_LIST_VERSION;
5218 }
5219 return phone.getCarrierIdListVersion();
5220 } finally {
5221 Binder.restoreCallingIdentity(identity);
fionaxua13278b2018-03-21 00:08:13 -07005222 }
fionaxua13278b2018-03-21 00:08:13 -07005223 }
Malcolm Chen2c63d402018-08-14 16:00:53 -07005224
5225 @Override
5226 public int getNumberOfModemsWithSimultaneousDataConnections(int subId, String callingPackage) {
5227 if (!TelephonyPermissions.checkCallingOrSelfReadPhoneState(
5228 mApp, subId, callingPackage, "getNumberOfModemsWithSimultaneousDataConnections")) {
5229 return -1;
5230 }
5231
5232 final long identity = Binder.clearCallingIdentity();
5233 try {
5234 return mPhoneConfigurationManager.getNumberOfModemsWithSimultaneousDataConnections();
5235 } finally {
5236 Binder.restoreCallingIdentity(identity);
5237 }
5238 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07005239}