blob: b7c913c5e7ddf7fb1c110610dcd19e8e280152c2 [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 Yen1b455992016-12-27 14:52:32 -080021import android.Manifest.permission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.ActivityManager;
23import android.app.AppOpsManager;
Ta-wei Yen1b455992016-12-27 14:52:32 -080024import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070025import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.content.Context;
27import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070028import android.content.SharedPreferences;
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 Yuc83dc1c2017-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;
yinxu34eed212017-04-12 16:03:22 -070037import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Looper;
39import android.os.Message;
yinxu34eed212017-04-12 16:03:22 -070040import android.os.Messenger;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070042import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.os.ServiceManager;
44import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070045import android.os.UserManager;
Sooraj Sasindran22882212016-07-18 11:57:25 -070046import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070047import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080048import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070049import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080050import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080051import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070052import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070053import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.telephony.CellInfo;
Sooraj Sasindran22882212016-07-18 11:57:25 -070055import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070056import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070057import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080058import android.telephony.NeighboringCellInfo;
yinxu34eed212017-04-12 16:03:22 -070059import android.telephony.NetworkScanRequest;
Wink Saville5d475dd2014-10-17 15:00:58 -070060import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080062import android.telephony.SignalStrength;
Jack Yuc83dc1c2017-05-26 16:07:50 -070063import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080064import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080065import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070066import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070067import android.telephony.TelephonyManager;
68import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080070import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080072import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080073import android.util.Slog;
Ta-wei Yen1b455992016-12-27 14:52:32 -080074
Andrew Lee312e8172014-10-23 17:01:36 -070075import com.android.ims.ImsManager;
Brad Ebinger76681002017-01-23 13:50:20 -080076import com.android.ims.internal.IImsServiceController;
77import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070078import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070079import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070080import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080083import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010084import com.android.internal.telephony.MccTable;
yinxub1bed742017-04-17 11:45:04 -070085import com.android.internal.telephony.NetworkScanRequestTracker;
Shishir Agrawal302c8692015-06-19 13:49:39 -070086import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087import com.android.internal.telephony.Phone;
Nathan Harold8f68faa2016-12-14 11:27:20 -080088import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070089import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070090import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070091import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070092import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070093import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080094import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070095import com.android.internal.telephony.uicc.IccIoResult;
96import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080097import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070098import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080099import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700100import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -0800101import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -0700102import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -0800103import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yen463efd12017-01-06 15:29:25 -0800104import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yen1b455992016-12-27 14:52:32 -0800105
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700106import java.io.FileDescriptor;
107import java.io.PrintWriter;
Ta-wei Yen1b455992016-12-27 14:52:32 -0800108import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800110import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800111import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100112import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800113import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114
115/**
116 * Implementation of the ITelephony interface.
117 */
Santos Cordon117fee72014-05-16 17:56:12 -0700118public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700119 private static final String LOG_TAG = "PhoneInterfaceManager";
120 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
121 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800122 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700123
124 // Message codes used with mMainThreadHandler
125 private static final int CMD_HANDLE_PIN_MMI = 1;
126 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
127 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
128 private static final int CMD_ANSWER_RINGING_CALL = 4;
129 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700130 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
131 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700132 private static final int CMD_OPEN_CHANNEL = 9;
133 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
134 private static final int CMD_CLOSE_CHANNEL = 11;
135 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800136 private static final int CMD_NV_READ_ITEM = 13;
137 private static final int EVENT_NV_READ_ITEM_DONE = 14;
138 private static final int CMD_NV_WRITE_ITEM = 15;
139 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
140 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
141 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
142 private static final int CMD_NV_RESET_CONFIG = 19;
143 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800144 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
145 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
146 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
147 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800148 private static final int CMD_SEND_ENVELOPE = 25;
149 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700150 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
151 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
152 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
153 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
154 private static final int CMD_EXCHANGE_SIM_IO = 31;
155 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800156 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
157 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700158 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
159 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700160 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
161 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700162 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
163 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
164 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
165 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700166 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
167 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
168 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
169 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar7e856482016-10-14 19:37:38 -0700170 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800171 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
172 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700173
174 /** The singleton instance. */
175 private static PhoneInterfaceManager sInstance;
176
Wink Saville3ab207e2014-11-20 13:07:20 -0800177 private PhoneGlobals mApp;
178 private Phone mPhone;
179 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700180 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800181 private AppOpsManager mAppOps;
182 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800183 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800184 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700185
Derek Tan97ebb422014-09-05 16:55:38 -0700186 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
187 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800188 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700189
yinxub1bed742017-04-17 11:45:04 -0700190 private NetworkScanRequestTracker mNetworkScanRequestTracker;
191
Derek Tan89e89d42014-07-08 17:00:10 -0700192 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700193 * A request object to use for transmitting data to an ICC.
194 */
195 private static final class IccAPDUArgument {
196 public int channel, cla, command, p1, p2, p3;
197 public String data;
198
199 public IccAPDUArgument(int channel, int cla, int command,
200 int p1, int p2, int p3, String data) {
201 this.channel = channel;
202 this.cla = cla;
203 this.command = command;
204 this.p1 = p1;
205 this.p2 = p2;
206 this.p3 = p3;
207 this.data = data;
208 }
209 }
210
211 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700212 * A request object to use for transmitting data to an ICC.
213 */
214 private static final class ManualNetworkSelectionArgument {
215 public OperatorInfo operatorInfo;
216 public boolean persistSelection;
217
218 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
219 this.operatorInfo = operatorInfo;
220 this.persistSelection = persistSelection;
221 }
222 }
223
224 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700225 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
226 * request after sending. The main thread will notify the request when it is complete.
227 */
228 private static final class MainThreadRequest {
229 /** The argument to use for the request */
230 public Object argument;
231 /** The result of the request that is run on the main thread */
232 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800233 // The subscriber id that this request applies to. Defaults to
234 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
235 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700236
237 public MainThreadRequest(Object argument) {
238 this.argument = argument;
239 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800240
241 public MainThreadRequest(Object argument, Integer subId) {
242 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800243 if (subId != null) {
244 this.subId = subId;
245 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800246 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 }
248
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800249 private static final class IncomingThirdPartyCallArgs {
250 public final ComponentName component;
251 public final String callId;
252 public final String callerDisplayName;
253
254 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
255 String callerDisplayName) {
256 this.component = component;
257 this.callId = callId;
258 this.callerDisplayName = callerDisplayName;
259 }
260 }
261
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700262 /**
263 * A handler that processes messages on the main thread in the phone process. Since many
264 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
265 * inbound binder threads to the main thread in the phone process. The Binder thread
266 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
267 * on, which will be notified when the operation completes and will contain the result of the
268 * request.
269 *
270 * <p>If a MainThreadRequest object is provided in the msg.obj field,
271 * note that request.result must be set to something non-null for the calling thread to
272 * unblock.
273 */
274 private final class MainThreadHandler extends Handler {
275 @Override
276 public void handleMessage(Message msg) {
277 MainThreadRequest request;
278 Message onCompleted;
279 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800280 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700281 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700282
283 switch (msg.what) {
pkanwar7e856482016-10-14 19:37:38 -0700284 case CMD_HANDLE_USSD_REQUEST: {
285 request = (MainThreadRequest) msg.obj;
286 final Phone phone = getPhoneFromRequest(request);
287 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
288 String ussdRequest = ussdObject.first;
289 ResultReceiver wrappedCallback = ussdObject.second;
290 request.result = phone != null ?
291 phone.handleUssdRequest(ussdRequest, wrappedCallback)
292 :false;
293 // Wake up the requesting thread
294 synchronized (request) {
295 request.notifyAll();
296 }
297 break;
298 }
299
Yorke Lee716f67e2015-06-17 15:39:16 -0700300 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700302 final Phone phone = getPhoneFromRequest(request);
303 request.result = phone != null ?
304 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
305 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 // Wake up the requesting thread
307 synchronized (request) {
308 request.notifyAll();
309 }
310 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700311 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700312
313 case CMD_HANDLE_NEIGHBORING_CELL:
314 request = (MainThreadRequest) msg.obj;
315 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
316 request);
Sooraj Sasindran22882212016-07-18 11:57:25 -0700317 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700318 break;
319
320 case EVENT_NEIGHBORING_CELL_DONE:
321 ar = (AsyncResult) msg.obj;
322 request = (MainThreadRequest) ar.userObj;
323 if (ar.exception == null && ar.result != null) {
324 request.result = ar.result;
325 } else {
326 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800327 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 }
329 // Wake up the requesting thread
330 synchronized (request) {
331 request.notifyAll();
332 }
333 break;
334
335 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700336 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800337 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700338 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 break;
340
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700341 case CMD_END_CALL:
342 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800343 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700344 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700345 Phone phone = getPhone(end_subId);
346 if (phone == null) {
347 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
348 break;
349 }
350 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
352 // CDMA: If the user presses the Power button we treat it as
353 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700354 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
356 // GSM: End the call as per the Phone state
357 hungUp = PhoneUtils.hangup(mCM);
358 } else {
359 throw new IllegalStateException("Unexpected phone type: " + phoneType);
360 }
361 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
362 request.result = hungUp;
363 // Wake up the requesting thread
364 synchronized (request) {
365 request.notifyAll();
366 }
367 break;
368
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700369 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700370 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700371 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800372 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700373 if (uiccCard == null) {
374 loge("iccTransmitApduLogicalChannel: No UICC");
375 request.result = new IccIoResult(0x6F, 0, (byte[])null);
376 synchronized (request) {
377 request.notifyAll();
378 }
379 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700380 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
381 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700382 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700383 iccArgument.channel, iccArgument.cla, iccArgument.command,
384 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700385 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700386 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700387 break;
388
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700389 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700390 ar = (AsyncResult) msg.obj;
391 request = (MainThreadRequest) ar.userObj;
392 if (ar.exception == null && ar.result != null) {
393 request.result = ar.result;
394 } else {
395 request.result = new IccIoResult(0x6F, 0, (byte[])null);
396 if (ar.result == null) {
397 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800398 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700399 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800400 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700401 } else {
402 loge("iccTransmitApduLogicalChannel: Unknown exception");
403 }
404 }
405 synchronized (request) {
406 request.notifyAll();
407 }
408 break;
409
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700410 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
411 request = (MainThreadRequest) msg.obj;
412 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800413 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700414 if (uiccCard == null) {
415 loge("iccTransmitApduBasicChannel: No UICC");
416 request.result = new IccIoResult(0x6F, 0, (byte[])null);
417 synchronized (request) {
418 request.notifyAll();
419 }
420 } else {
421 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
422 request);
423 uiccCard.iccTransmitApduBasicChannel(
424 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
425 iccArgument.p3, iccArgument.data, onCompleted);
426 }
427 break;
428
429 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
430 ar = (AsyncResult) msg.obj;
431 request = (MainThreadRequest) ar.userObj;
432 if (ar.exception == null && ar.result != null) {
433 request.result = ar.result;
434 } else {
435 request.result = new IccIoResult(0x6F, 0, (byte[])null);
436 if (ar.result == null) {
437 loge("iccTransmitApduBasicChannel: Empty response");
438 } else if (ar.exception instanceof CommandException) {
439 loge("iccTransmitApduBasicChannel: CommandException: " +
440 ar.exception);
441 } else {
442 loge("iccTransmitApduBasicChannel: Unknown exception");
443 }
444 }
445 synchronized (request) {
446 request.notifyAll();
447 }
448 break;
449
450 case CMD_EXCHANGE_SIM_IO:
451 request = (MainThreadRequest) msg.obj;
452 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800453 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700454 if (uiccCard == null) {
455 loge("iccExchangeSimIO: No UICC");
456 request.result = new IccIoResult(0x6F, 0, (byte[])null);
457 synchronized (request) {
458 request.notifyAll();
459 }
460 } else {
461 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
462 request);
463 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
464 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
465 iccArgument.data, onCompleted);
466 }
467 break;
468
469 case EVENT_EXCHANGE_SIM_IO_DONE:
470 ar = (AsyncResult) msg.obj;
471 request = (MainThreadRequest) ar.userObj;
472 if (ar.exception == null && ar.result != null) {
473 request.result = ar.result;
474 } else {
475 request.result = new IccIoResult(0x6f, 0, (byte[])null);
476 }
477 synchronized (request) {
478 request.notifyAll();
479 }
480 break;
481
Derek Tan4d5e5c12014-02-04 11:54:58 -0800482 case CMD_SEND_ENVELOPE:
483 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800484 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700485 if (uiccCard == null) {
486 loge("sendEnvelopeWithStatus: No UICC");
487 request.result = new IccIoResult(0x6F, 0, (byte[])null);
488 synchronized (request) {
489 request.notifyAll();
490 }
491 } else {
492 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
493 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
494 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800495 break;
496
497 case EVENT_SEND_ENVELOPE_DONE:
498 ar = (AsyncResult) msg.obj;
499 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700500 if (ar.exception == null && ar.result != null) {
501 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800502 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700503 request.result = new IccIoResult(0x6F, 0, (byte[])null);
504 if (ar.result == null) {
505 loge("sendEnvelopeWithStatus: Empty response");
506 } else if (ar.exception instanceof CommandException) {
507 loge("sendEnvelopeWithStatus: CommandException: " +
508 ar.exception);
509 } else {
510 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
511 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800512 }
513 synchronized (request) {
514 request.notifyAll();
515 }
516 break;
517
Shishir Agrawal566b7612013-10-28 14:41:00 -0700518 case CMD_OPEN_CHANNEL:
519 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800520 uiccCard = getUiccCardFromRequest(request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800521 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700522 if (uiccCard == null) {
523 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800524 request.result = new IccOpenLogicalChannelResponse(-1,
525 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700526 synchronized (request) {
527 request.notifyAll();
528 }
529 } else {
530 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800531 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
532 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700533 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700534 break;
535
536 case EVENT_OPEN_CHANNEL_DONE:
537 ar = (AsyncResult) msg.obj;
538 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700539 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700540 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700541 int[] result = (int[]) ar.result;
542 int channelId = result[0];
543 byte[] selectResponse = null;
544 if (result.length > 1) {
545 selectResponse = new byte[result.length - 1];
546 for (int i = 1; i < result.length; ++i) {
547 selectResponse[i - 1] = (byte) result[i];
548 }
549 }
550 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700551 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700552 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700553 if (ar.result == null) {
554 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700555 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700556 if (ar.exception != null) {
557 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
558 }
559
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700560 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700561 if (ar.exception instanceof CommandException) {
562 CommandException.Error error =
563 ((CommandException) (ar.exception)).getCommandError();
564 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700565 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700566 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700567 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700568 }
569 }
570 openChannelResp = new IccOpenLogicalChannelResponse(
571 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700572 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700573 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700574 synchronized (request) {
575 request.notifyAll();
576 }
577 break;
578
579 case CMD_CLOSE_CHANNEL:
580 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800581 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700582 if (uiccCard == null) {
583 loge("iccCloseLogicalChannel: No UICC");
584 request.result = new IccIoResult(0x6F, 0, (byte[])null);
585 synchronized (request) {
586 request.notifyAll();
587 }
588 } else {
589 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
590 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
591 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700592 break;
593
594 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800595 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
596 break;
597
598 case CMD_NV_READ_ITEM:
599 request = (MainThreadRequest) msg.obj;
600 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
601 mPhone.nvReadItem((Integer) request.argument, onCompleted);
602 break;
603
604 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700605 ar = (AsyncResult) msg.obj;
606 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800607 if (ar.exception == null && ar.result != null) {
608 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700609 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800610 request.result = "";
611 if (ar.result == null) {
612 loge("nvReadItem: Empty response");
613 } else if (ar.exception instanceof CommandException) {
614 loge("nvReadItem: CommandException: " +
615 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700616 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800617 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700618 }
619 }
620 synchronized (request) {
621 request.notifyAll();
622 }
623 break;
624
Jake Hambye994d462014-02-03 13:10:13 -0800625 case CMD_NV_WRITE_ITEM:
626 request = (MainThreadRequest) msg.obj;
627 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
628 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
629 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
630 break;
631
632 case EVENT_NV_WRITE_ITEM_DONE:
633 handleNullReturnEvent(msg, "nvWriteItem");
634 break;
635
636 case CMD_NV_WRITE_CDMA_PRL:
637 request = (MainThreadRequest) msg.obj;
638 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
639 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
640 break;
641
642 case EVENT_NV_WRITE_CDMA_PRL_DONE:
643 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
644 break;
645
646 case CMD_NV_RESET_CONFIG:
647 request = (MainThreadRequest) msg.obj;
648 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
649 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
650 break;
651
652 case EVENT_NV_RESET_CONFIG_DONE:
653 handleNullReturnEvent(msg, "nvResetConfig");
654 break;
655
Jake Hamby7c27be32014-03-03 13:25:59 -0800656 case CMD_GET_PREFERRED_NETWORK_TYPE:
657 request = (MainThreadRequest) msg.obj;
658 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700659 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800660 break;
661
662 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
663 ar = (AsyncResult) msg.obj;
664 request = (MainThreadRequest) ar.userObj;
665 if (ar.exception == null && ar.result != null) {
666 request.result = ar.result; // Integer
667 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800668 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800669 if (ar.result == null) {
670 loge("getPreferredNetworkType: Empty response");
671 } else if (ar.exception instanceof CommandException) {
672 loge("getPreferredNetworkType: CommandException: " +
673 ar.exception);
674 } else {
675 loge("getPreferredNetworkType: Unknown exception");
676 }
677 }
678 synchronized (request) {
679 request.notifyAll();
680 }
681 break;
682
683 case CMD_SET_PREFERRED_NETWORK_TYPE:
684 request = (MainThreadRequest) msg.obj;
685 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
686 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700687 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800688 break;
689
690 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
691 handleNullReturnEvent(msg, "setPreferredNetworkType");
692 break;
693
Steven Liu4bf01bc2014-07-17 11:05:29 -0500694 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
695 request = (MainThreadRequest)msg.obj;
696 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
697 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
698 break;
699
700 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
701 ar = (AsyncResult)msg.obj;
702 request = (MainThreadRequest)ar.userObj;
703 request.result = ar;
704 synchronized (request) {
705 request.notifyAll();
706 }
707 break;
708
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800709 case CMD_SET_VOICEMAIL_NUMBER:
710 request = (MainThreadRequest) msg.obj;
711 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
712 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800713 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
714 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800715 break;
716
717 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
718 handleNullReturnEvent(msg, "setVoicemailNumber");
719 break;
720
Stuart Scott54788802015-03-30 13:18:01 -0700721 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
722 request = (MainThreadRequest) msg.obj;
723 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
724 request);
725 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
726 break;
727
728 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
729 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
730 break;
731
Shishir Agrawal302c8692015-06-19 13:49:39 -0700732 case CMD_PERFORM_NETWORK_SCAN:
733 request = (MainThreadRequest) msg.obj;
734 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
735 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
736 break;
737
738 case EVENT_PERFORM_NETWORK_SCAN_DONE:
739 ar = (AsyncResult) msg.obj;
740 request = (MainThreadRequest) ar.userObj;
741 CellNetworkScanResult cellScanResult;
742 if (ar.exception == null && ar.result != null) {
743 cellScanResult = new CellNetworkScanResult(
744 CellNetworkScanResult.STATUS_SUCCESS,
745 (List<OperatorInfo>) ar.result);
746 } else {
747 if (ar.result == null) {
748 loge("getCellNetworkScanResults: Empty response");
749 }
750 if (ar.exception != null) {
751 loge("getCellNetworkScanResults: Exception: " + ar.exception);
752 }
753 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
754 if (ar.exception instanceof CommandException) {
755 CommandException.Error error =
756 ((CommandException) (ar.exception)).getCommandError();
757 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
758 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
759 } else if (error == CommandException.Error.GENERIC_FAILURE) {
760 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
761 }
762 }
763 cellScanResult = new CellNetworkScanResult(errorCode, null);
764 }
765 request.result = cellScanResult;
766 synchronized (request) {
767 request.notifyAll();
768 }
769 break;
770
771 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
772 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700773 ManualNetworkSelectionArgument selArg =
774 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700775 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
776 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700777 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
778 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700779 break;
780
781 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
782 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
783 break;
784
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700785 case CMD_GET_MODEM_ACTIVITY_INFO:
786 request = (MainThreadRequest) msg.obj;
787 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700788 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700789 break;
790
791 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
792 ar = (AsyncResult) msg.obj;
793 request = (MainThreadRequest) ar.userObj;
794 if (ar.exception == null && ar.result != null) {
795 request.result = ar.result;
796 } else {
797 if (ar.result == null) {
798 loge("queryModemActivityInfo: Empty response");
799 } else if (ar.exception instanceof CommandException) {
800 loge("queryModemActivityInfo: CommandException: " +
801 ar.exception);
802 } else {
803 loge("queryModemActivityInfo: Unknown exception");
804 }
805 }
Amit Mahajand4766222016-01-28 15:28:28 -0800806 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
807 if (request.result == null) {
808 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
809 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700810 synchronized (request) {
811 request.notifyAll();
812 }
813 break;
814
Meng Wang1a7c35a2016-05-05 20:56:15 -0700815 case CMD_SET_ALLOWED_CARRIERS:
816 request = (MainThreadRequest) msg.obj;
817 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
818 mPhone.setAllowedCarriers(
819 (List<CarrierIdentifier>) request.argument,
820 onCompleted);
821 break;
822
823 case EVENT_SET_ALLOWED_CARRIERS_DONE:
824 ar = (AsyncResult) msg.obj;
825 request = (MainThreadRequest) ar.userObj;
826 if (ar.exception == null && ar.result != null) {
827 request.result = ar.result;
828 } else {
829 if (ar.result == null) {
830 loge("setAllowedCarriers: Empty response");
831 } else if (ar.exception instanceof CommandException) {
832 loge("setAllowedCarriers: CommandException: " +
833 ar.exception);
834 } else {
835 loge("setAllowedCarriers: Unknown exception");
836 }
837 }
838 // Result cannot be null. Return -1 on error.
839 if (request.result == null) {
840 request.result = new int[]{-1};
841 }
842 synchronized (request) {
843 request.notifyAll();
844 }
845 break;
846
847 case CMD_GET_ALLOWED_CARRIERS:
848 request = (MainThreadRequest) msg.obj;
849 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
850 mPhone.getAllowedCarriers(onCompleted);
851 break;
852
853 case EVENT_GET_ALLOWED_CARRIERS_DONE:
854 ar = (AsyncResult) msg.obj;
855 request = (MainThreadRequest) ar.userObj;
856 if (ar.exception == null && ar.result != null) {
857 request.result = ar.result;
858 } else {
859 if (ar.result == null) {
860 loge("getAllowedCarriers: Empty response");
861 } else if (ar.exception instanceof CommandException) {
862 loge("getAllowedCarriers: CommandException: " +
863 ar.exception);
864 } else {
865 loge("getAllowedCarriers: Unknown exception");
866 }
867 }
868 // Result cannot be null. Return empty list of CarrierIdentifier.
869 if (request.result == null) {
870 request.result = new ArrayList<CarrierIdentifier>(0);
871 }
872 synchronized (request) {
873 request.notifyAll();
874 }
875 break;
876
Nathan Haroldb3014052017-01-25 15:57:32 -0800877 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
878 ar = (AsyncResult) msg.obj;
879 request = (MainThreadRequest) ar.userObj;
880 if (ar.exception == null && ar.result != null) {
881 request.result = ar.result;
882 } else {
883 request.result = new IllegalArgumentException(
884 "Failed to retrieve Forbidden Plmns");
885 if (ar.result == null) {
886 loge("getForbiddenPlmns: Empty response");
887 } else {
888 loge("getForbiddenPlmns: Unknown exception");
889 }
890 }
891 synchronized (request) {
892 request.notifyAll();
893 }
894 break;
895
896 case CMD_GET_FORBIDDEN_PLMNS:
897 request = (MainThreadRequest) msg.obj;
898 uiccCard = getUiccCardFromRequest(request);
899 if (uiccCard == null) {
900 loge("getForbiddenPlmns() UiccCard is null");
901 request.result = new IllegalArgumentException(
902 "getForbiddenPlmns() UiccCard is null");
903 synchronized (request) {
904 request.notifyAll();
905 }
906 break;
907 }
908 Integer appType = (Integer) request.argument;
909 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
910 if (uiccApp == null) {
911 loge("getForbiddenPlmns() no app with specified type -- "
912 + appType);
913 request.result = new IllegalArgumentException("Failed to get UICC App");
914 synchronized (request) {
915 request.notifyAll();
916 }
917 break;
918 } else {
919 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
920 + " specified type -- " + appType);
921 }
922 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
923 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
924 onCompleted);
925 break;
926
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 default:
928 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
929 break;
930 }
931 }
Jake Hambye994d462014-02-03 13:10:13 -0800932
933 private void handleNullReturnEvent(Message msg, String command) {
934 AsyncResult ar = (AsyncResult) msg.obj;
935 MainThreadRequest request = (MainThreadRequest) ar.userObj;
936 if (ar.exception == null) {
937 request.result = true;
938 } else {
939 request.result = false;
940 if (ar.exception instanceof CommandException) {
941 loge(command + ": CommandException: " + ar.exception);
942 } else {
943 loge(command + ": Unknown exception");
944 }
945 }
946 synchronized (request) {
947 request.notifyAll();
948 }
949 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700950 }
951
952 /**
953 * Posts the specified command to be executed on the main thread,
954 * waits for the request to complete, and returns the result.
955 * @see #sendRequestAsync
956 */
957 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800958 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700959 }
960
961 /**
962 * Posts the specified command to be executed on the main thread,
963 * waits for the request to complete, and returns the result.
964 * @see #sendRequestAsync
965 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800966 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
968 throw new RuntimeException("This method will deadlock if called from the main thread.");
969 }
970
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800971 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700972 Message msg = mMainThreadHandler.obtainMessage(command, request);
973 msg.sendToTarget();
974
975 // Wait for the request to complete
976 synchronized (request) {
977 while (request.result == null) {
978 try {
979 request.wait();
980 } catch (InterruptedException e) {
981 // Do nothing, go back and wait until the request is complete
982 }
983 }
984 }
985 return request.result;
986 }
987
988 /**
989 * Asynchronous ("fire and forget") version of sendRequest():
990 * Posts the specified command to be executed on the main thread, and
991 * returns immediately.
992 * @see #sendRequest
993 */
994 private void sendRequestAsync(int command) {
995 mMainThreadHandler.sendEmptyMessage(command);
996 }
997
998 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700999 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
1000 * @see {@link #sendRequest(int,Object)}
1001 */
1002 private void sendRequestAsync(int command, Object argument) {
1003 MainThreadRequest request = new MainThreadRequest(argument);
1004 Message msg = mMainThreadHandler.obtainMessage(command, request);
1005 msg.sendToTarget();
1006 }
1007
1008 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 * Initialize the singleton PhoneInterfaceManager instance.
1010 * This is only done once, at startup, from PhoneApp.onCreate().
1011 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001012 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 synchronized (PhoneInterfaceManager.class) {
1014 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001015 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 } else {
1017 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1018 }
1019 return sInstance;
1020 }
1021 }
1022
1023 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001024 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001025 mApp = app;
1026 mPhone = phone;
1027 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001028 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001029 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1030 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001031 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001032 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001033 mSubscriptionController = SubscriptionController.getInstance();
yinxub1bed742017-04-17 11:45:04 -07001034 mNetworkScanRequestTracker = new NetworkScanRequestTracker();
Wink Saville3ab207e2014-11-20 13:07:20 -08001035
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001036 publish();
1037 }
1038
1039 private void publish() {
1040 if (DBG) log("publish: " + this);
1041
1042 ServiceManager.addService("phone", this);
1043 }
1044
Stuart Scott584921c2015-01-15 17:10:34 -08001045 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001046 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1047 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001048 }
1049
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001050 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1051 Phone phone = getPhoneFromRequest(request);
1052 return phone == null ? null :
1053 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1054 }
1055
Wink Saville36469e72014-06-11 15:17:00 -07001056 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001057 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001058 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001059 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001060 //
1061 // Implementation of the ITelephony interface.
1062 //
1063
1064 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001065 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001066 }
1067
Wink Savilleb564aae2014-10-23 10:18:09 -07001068 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001069 if (DBG) log("dial: " + number);
1070 // No permission check needed here: This is just a wrapper around the
1071 // ACTION_DIAL intent, which is available to any app since it puts up
1072 // the UI before it does anything.
1073
1074 String url = createTelUrl(number);
1075 if (url == null) {
1076 return;
1077 }
1078
1079 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001080 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001081 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1082 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1083 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1084 mApp.startActivity(intent);
1085 }
1086 }
1087
1088 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001089 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001090 }
1091
Wink Savilleb564aae2014-10-23 10:18:09 -07001092 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001093 if (DBG) log("call: " + number);
1094
1095 // This is just a wrapper around the ACTION_CALL intent, but we still
1096 // need to do a permission check since we're calling startActivity()
1097 // from the context of the phone app.
1098 enforceCallPermission();
1099
1100 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1101 != AppOpsManager.MODE_ALLOWED) {
1102 return;
1103 }
1104
1105 String url = createTelUrl(number);
1106 if (url == null) {
1107 return;
1108 }
1109
Wink Saville08874612014-08-31 19:19:58 -07001110 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001111 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001112 if (slist != null) {
1113 for (SubscriptionInfo subInfoRecord : slist) {
1114 if (subInfoRecord.getSubscriptionId() == subId) {
1115 isValid = true;
1116 break;
1117 }
Wink Saville08874612014-08-31 19:19:58 -07001118 }
1119 }
1120 if (isValid == false) {
1121 return;
1122 }
1123
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001124 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001125 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1127 mApp.startActivity(intent);
1128 }
1129
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001130 /**
1131 * End a call based on call state
1132 * @return true is a call was ended
1133 */
1134 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001135 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001136 }
1137
1138 /**
1139 * End a call based on the call state of the subId
1140 * @return true is a call was ended
1141 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001142 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001143 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001144 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001145 }
1146
1147 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001148 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001149 }
1150
Wink Savilleb564aae2014-10-23 10:18:09 -07001151 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001152 if (DBG) log("answerRingingCall...");
1153 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1154 // but that can probably wait till the big TelephonyManager API overhaul.
1155 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1156 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001157 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001158 }
1159
1160 /**
1161 * Make the actual telephony calls to implement answerRingingCall().
1162 * This should only be called from the main thread of the Phone app.
1163 * @see #answerRingingCall
1164 *
1165 * TODO: it would be nice to return true if we answered the call, or
1166 * false if there wasn't actually a ringing incoming call, or some
1167 * other error occurred. (In other words, pass back the return value
1168 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1169 * But that would require calling this method via sendRequest() rather
1170 * than sendRequestAsync(), and right now we don't actually *need* that
1171 * return value, so let's just return void for now.
1172 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001173 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001174 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001175 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001176 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1177 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001178 if (hasActiveCall && hasHoldingCall) {
1179 // Both lines are in use!
1180 // TODO: provide a flag to let the caller specify what
1181 // policy to use if both lines are in use. (The current
1182 // behavior is hardwired to "answer incoming, end ongoing",
1183 // which is how the CALL button is specced to behave.)
1184 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1185 return;
1186 } else {
1187 // answerCall() will automatically hold the current active
1188 // call, if there is one.
1189 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1190 return;
1191 }
1192 } else {
1193 // No call was ringing.
1194 return;
1195 }
1196 }
1197
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001198 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001199 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001200 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001201 public void silenceRinger() {
1202 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001203 }
1204
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001205 @Override
1206 public boolean isOffhook(String callingPackage) {
1207 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001208 }
1209
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001210 @Override
1211 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1212 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1213 return false;
1214 }
1215
Sanket Padawe356d7632015-06-22 14:03:32 -07001216 final Phone phone = getPhone(subId);
1217 if (phone != null) {
1218 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1219 } else {
1220 return false;
1221 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001222 }
1223
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001224 @Override
1225 public boolean isRinging(String callingPackage) {
1226 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001227 }
1228
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001229 @Override
1230 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1231 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1232 return false;
1233 }
1234
Sanket Padawe356d7632015-06-22 14:03:32 -07001235 final Phone phone = getPhone(subId);
1236 if (phone != null) {
1237 return (phone.getState() == PhoneConstants.State.RINGING);
1238 } else {
1239 return false;
1240 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001241 }
1242
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001243 @Override
1244 public boolean isIdle(String callingPackage) {
1245 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001246 }
1247
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001248 @Override
1249 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1250 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1251 return false;
1252 }
1253
Sanket Padawe356d7632015-06-22 14:03:32 -07001254 final Phone phone = getPhone(subId);
1255 if (phone != null) {
1256 return (phone.getState() == PhoneConstants.State.IDLE);
1257 } else {
1258 return false;
1259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001260 }
1261
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001262 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001263 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001264 }
1265
Wink Savilleb564aae2014-10-23 10:18:09 -07001266 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001267 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001268 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1269 }
1270
1271 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001272 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001273 }
1274
Wink Savilleb564aae2014-10-23 10:18:09 -07001275 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001276 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001277 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1278 }
1279
1280 /** {@hide} */
1281 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001282 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001283 }
1284
Wink Savilleb564aae2014-10-23 10:18:09 -07001285 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001287 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 checkSimPin.start();
1289 return checkSimPin.unlockSim(null, pin);
1290 }
1291
Wink Saville9de0f752013-10-22 19:04:03 -07001292 /** {@hide} */
1293 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001294 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001295 }
1296
Wink Savilleb564aae2014-10-23 10:18:09 -07001297 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001298 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001299 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 checkSimPuk.start();
1301 return checkSimPuk.unlockSim(puk, pin);
1302 }
1303
1304 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001305 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306 * a synchronous one.
1307 */
1308 private static class UnlockSim extends Thread {
1309
1310 private final IccCard mSimCard;
1311
1312 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001313 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1314 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001315
1316 // For replies from SimCard interface
1317 private Handler mHandler;
1318
1319 // For async handler to identify request type
1320 private static final int SUPPLY_PIN_COMPLETE = 100;
1321
1322 public UnlockSim(IccCard simCard) {
1323 mSimCard = simCard;
1324 }
1325
1326 @Override
1327 public void run() {
1328 Looper.prepare();
1329 synchronized (UnlockSim.this) {
1330 mHandler = new Handler() {
1331 @Override
1332 public void handleMessage(Message msg) {
1333 AsyncResult ar = (AsyncResult) msg.obj;
1334 switch (msg.what) {
1335 case SUPPLY_PIN_COMPLETE:
1336 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1337 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001338 mRetryCount = msg.arg1;
1339 if (ar.exception != null) {
1340 if (ar.exception instanceof CommandException &&
1341 ((CommandException)(ar.exception)).getCommandError()
1342 == CommandException.Error.PASSWORD_INCORRECT) {
1343 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1344 } else {
1345 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1346 }
1347 } else {
1348 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1349 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001350 mDone = true;
1351 UnlockSim.this.notifyAll();
1352 }
1353 break;
1354 }
1355 }
1356 };
1357 UnlockSim.this.notifyAll();
1358 }
1359 Looper.loop();
1360 }
1361
1362 /*
1363 * Use PIN or PUK to unlock SIM card
1364 *
1365 * If PUK is null, unlock SIM card with PIN
1366 *
1367 * If PUK is not null, unlock SIM card with PUK and set PIN code
1368 */
Wink Saville9de0f752013-10-22 19:04:03 -07001369 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001370
1371 while (mHandler == null) {
1372 try {
1373 wait();
1374 } catch (InterruptedException e) {
1375 Thread.currentThread().interrupt();
1376 }
1377 }
1378 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1379
1380 if (puk == null) {
1381 mSimCard.supplyPin(pin, callback);
1382 } else {
1383 mSimCard.supplyPuk(puk, pin, callback);
1384 }
1385
1386 while (!mDone) {
1387 try {
1388 Log.d(LOG_TAG, "wait for done");
1389 wait();
1390 } catch (InterruptedException e) {
1391 // Restore the interrupted status
1392 Thread.currentThread().interrupt();
1393 }
1394 }
1395 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001396 int[] resultArray = new int[2];
1397 resultArray[0] = mResult;
1398 resultArray[1] = mRetryCount;
1399 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001400 }
1401 }
1402
1403 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001404 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001405
1406 }
1407
Wink Savilleb564aae2014-10-23 10:18:09 -07001408 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001409 // No permission check needed here: this call is harmless, and it's
1410 // needed for the ServiceState.requestStateUpdate() call (which is
1411 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001412 final Phone phone = getPhone(subId);
1413 if (phone != null) {
1414 phone.updateServiceLocation();
1415 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001416 }
1417
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001418 @Override
1419 public boolean isRadioOn(String callingPackage) {
1420 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001421 }
1422
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001423 @Override
1424 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1425 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1426 return false;
1427 }
1428 return isRadioOnForSubscriber(subId);
1429 }
1430
1431 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001432 final Phone phone = getPhone(subId);
1433 if (phone != null) {
1434 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1435 } else {
1436 return false;
1437 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 }
1439
1440 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001441 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001442
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 }
Wink Saville36469e72014-06-11 15:17:00 -07001444
Wink Savilleb564aae2014-10-23 10:18:09 -07001445 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001447 final Phone phone = getPhone(subId);
1448 if (phone != null) {
1449 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1450 }
Wink Saville36469e72014-06-11 15:17:00 -07001451 }
1452
1453 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001454 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001455 }
1456
Wink Savilleb564aae2014-10-23 10:18:09 -07001457 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001458 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001459 final Phone phone = getPhone(subId);
1460 if (phone == null) {
1461 return false;
1462 }
1463 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001464 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001465 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001466 }
1467 return true;
1468 }
Wink Saville36469e72014-06-11 15:17:00 -07001469
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001470 public boolean needMobileRadioShutdown() {
1471 /*
1472 * If any of the Radios are available, it will need to be
1473 * shutdown. So return true if any Radio is available.
1474 */
1475 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1476 Phone phone = PhoneFactory.getPhone(i);
1477 if (phone != null && phone.isRadioAvailable()) return true;
1478 }
1479 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1480 return false;
1481 }
1482
1483 public void shutdownMobileRadios() {
1484 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1485 logv("Shutting down Phone " + i);
1486 shutdownRadioUsingPhoneId(i);
1487 }
1488 }
1489
1490 private void shutdownRadioUsingPhoneId(int phoneId) {
1491 enforceModifyPermission();
1492 Phone phone = PhoneFactory.getPhone(phoneId);
1493 if (phone != null && phone.isRadioAvailable()) {
1494 phone.shutdownRadio();
1495 }
1496 }
1497
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001498 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001499 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1500 if (defaultPhone != null) {
1501 defaultPhone.setRadioPower(turnOn);
1502 return true;
1503 } else {
1504 loge("There's no default phone.");
1505 return false;
1506 }
Wink Saville36469e72014-06-11 15:17:00 -07001507 }
1508
Wink Savilleb564aae2014-10-23 10:18:09 -07001509 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001510 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001511 final Phone phone = getPhone(subId);
1512 if (phone != null) {
1513 phone.setRadioPower(turnOn);
1514 return true;
1515 } else {
1516 return false;
1517 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001518 }
1519
Wink Saville36469e72014-06-11 15:17:00 -07001520 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001521 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 public boolean enableDataConnectivity() {
1523 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001524 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001525 final Phone phone = getPhone(subId);
1526 if (phone != null) {
1527 phone.setDataEnabled(true);
1528 return true;
1529 } else {
1530 return false;
1531 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 }
1533
Wink Saville36469e72014-06-11 15:17:00 -07001534 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001535 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 public boolean disableDataConnectivity() {
1537 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001538 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001539 final Phone phone = getPhone(subId);
1540 if (phone != null) {
1541 phone.setDataEnabled(false);
1542 return true;
1543 } else {
1544 return false;
1545 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001546 }
1547
Sanket Padawe356d7632015-06-22 14:03:32 -07001548 @Override
Jack Yu92586ef2017-05-01 17:00:48 -07001549 public boolean isDataConnectivityPossible(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001550 final Phone phone = getPhone(subId);
1551 if (phone != null) {
Jack Yu92586ef2017-05-01 17:00:48 -07001552 return phone.isDataAllowed();
Sanket Padawe356d7632015-06-22 14:03:32 -07001553 } else {
1554 return false;
1555 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001556 }
1557
1558 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001559 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001560 }
1561
pkanwarf8520782016-11-06 20:37:09 -08001562 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar7e856482016-10-14 19:37:38 -07001563 enforceCallPermission();
pkanwar7e856482016-10-14 19:37:38 -07001564 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1565 return;
1566 }
1567 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1568 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1569 };
1570
Wink Savilleb564aae2014-10-23 10:18:09 -07001571 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001572 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001573 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1574 return false;
1575 }
Wink Saville36469e72014-06-11 15:17:00 -07001576 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001577 }
1578
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001579 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001580 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001581 }
1582
Sanket Padawe5780e442017-03-20 15:04:47 -07001583 public int getCallStateForSlot(int slotIndex) {
1584 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001585 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harold8f68faa2016-12-14 11:27:20 -08001586 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001587 }
1588
Sanket Padawe356d7632015-06-22 14:03:32 -07001589 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001591 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001592 if (phone != null) {
Nathan Harold8f68faa2016-12-14 11:27:20 -08001593 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001594 } else {
Nathan Harold8f68faa2016-12-14 11:27:20 -08001595 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001596 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001597 }
1598
Sanket Padawe356d7632015-06-22 14:03:32 -07001599 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001600 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001601 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001602 if (phone != null) {
1603 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1604 } else {
1605 return TelephonyManager.DATA_ACTIVITY_NONE;
1606 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001607 }
1608
1609 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001610 public Bundle getCellLocation(String callingPackage) {
1611 enforceFineOrCoarseLocationPermission("getCellLocation");
1612
1613 // OP_COARSE_LOCATION controls both fine and coarse location.
1614 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1615 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001616 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001617 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001618 }
1619
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001620 if (checkIfCallerIsSelfOrForegroundUser() ||
1621 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001622 if (DBG_LOC) log("getCellLocation: is active user");
1623 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001624 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001625 if (phone == null) {
1626 return null;
1627 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07001628
1629 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1630 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001631 return data;
1632 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001633 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001634 return null;
1635 }
1636 }
1637
Svetoslav64fad262015-04-14 14:35:21 -07001638 private void enforceFineOrCoarseLocationPermission(String message) {
1639 try {
1640 mApp.enforceCallingOrSelfPermission(
1641 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1642 } catch (SecurityException e) {
1643 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1644 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1645 // is the weaker precondition
1646 mApp.enforceCallingOrSelfPermission(
1647 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1648 }
1649 }
1650
1651
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001652 @Override
1653 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001654 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001655 }
1656
Sanket Padawe356d7632015-06-22 14:03:32 -07001657 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001658 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001659 mApp.enforceCallingOrSelfPermission(
1660 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001661 final Phone phone = getPhone(subId);
1662 if (phone != null) {
1663 phone.enableLocationUpdates();
1664 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001665 }
1666
1667 @Override
1668 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001669 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001670 }
1671
Sanket Padawe356d7632015-06-22 14:03:32 -07001672 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001673 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001674 mApp.enforceCallingOrSelfPermission(
1675 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001676 final Phone phone = getPhone(subId);
1677 if (phone != null) {
1678 phone.disableLocationUpdates();
1679 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001680 }
1681
1682 @Override
1683 @SuppressWarnings("unchecked")
1684 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001685 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1686
1687 // OP_COARSE_LOCATION controls both fine and coarse location.
1688 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1689 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1690 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001691 }
1692
1693 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1694 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1695 return null;
1696 }
Svetoslav64fad262015-04-14 14:35:21 -07001697
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001698 if (checkIfCallerIsSelfOrForegroundUser() ||
1699 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001700 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1701
1702 ArrayList<NeighboringCellInfo> cells = null;
1703
Sooraj Sasindran22882212016-07-18 11:57:25 -07001704 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001705 try {
1706 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran22882212016-07-18 11:57:25 -07001707 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001708 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001709 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001710 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001711 }
1712 return cells;
1713 } else {
1714 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1715 return null;
1716 }
1717 }
1718
1719
1720 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001721 public List<CellInfo> getAllCellInfo(String callingPackage) {
1722 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1723
1724 // OP_COARSE_LOCATION controls both fine and coarse location.
1725 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1726 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1727 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001728 }
1729
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001730 if (checkIfCallerIsSelfOrForegroundUser() ||
1731 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001732 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran22882212016-07-18 11:57:25 -07001733 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001734 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1735 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran22882212016-07-18 11:57:25 -07001736 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1737 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001738 }
1739 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001740 } else {
1741 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1742 return null;
1743 }
1744 }
1745
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001746 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001747 public void setCellInfoListRate(int rateInMillis) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001748 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001749 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1750 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001751 }
1752
Shishir Agrawala9f32182016-04-12 12:00:16 -07001753 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001754 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001755 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1756 return null;
1757 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001758 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001759 return phone == null ? null : phone.getImei();
1760 }
1761
1762 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001763 public String getMeidForSlot(int slotIndex, String callingPackage) {
1764 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1765 return null;
1766 }
1767 Phone phone = PhoneFactory.getPhone(slotIndex);
1768 return phone == null ? null : phone.getMeid();
1769 }
1770
1771 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001772 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001773 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1774 return null;
1775 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001776 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001777 return phone == null ? null : phone.getDeviceSvn();
1778 }
1779
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001780 //
1781 // Internal helper methods.
1782 //
1783
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001784 /**
1785 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1786 */
1787 private boolean checkCallerInteractAcrossUsersFull() {
1788 return mPhone.getContext().checkCallingOrSelfPermission(
1789 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1790 == PackageManager.PERMISSION_GRANTED;
1791 }
1792
Jake Hambye994d462014-02-03 13:10:13 -08001793 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001794 boolean ok;
1795
1796 boolean self = Binder.getCallingUid() == Process.myUid();
1797 if (!self) {
1798 // Get the caller's user id then clear the calling identity
1799 // which will be restored in the finally clause.
1800 int callingUser = UserHandle.getCallingUserId();
1801 long ident = Binder.clearCallingIdentity();
1802
1803 try {
1804 // With calling identity cleared the current user is the foreground user.
1805 int foregroundUser = ActivityManager.getCurrentUser();
1806 ok = (foregroundUser == callingUser);
1807 if (DBG_LOC) {
1808 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1809 + " callingUser=" + callingUser + " ok=" + ok);
1810 }
1811 } catch (Exception ex) {
1812 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1813 ok = false;
1814 } finally {
1815 Binder.restoreCallingIdentity(ident);
1816 }
1817 } else {
1818 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1819 ok = true;
1820 }
1821 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1822 return ok;
1823 }
1824
1825 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001826 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1827 *
1828 * @throws SecurityException if the caller does not have the required permission
1829 */
1830 private void enforceModifyPermission() {
1831 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1832 }
1833
1834 /**
Junda Liua2e36012014-07-09 18:30:01 -07001835 * Make sure either system app or the caller has carrier privilege.
1836 *
1837 * @throws SecurityException if the caller does not have the required permission/privilege
1838 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001839 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001840 int permission = mApp.checkCallingOrSelfPermission(
1841 android.Manifest.permission.MODIFY_PHONE_STATE);
1842 if (permission == PackageManager.PERMISSION_GRANTED) {
1843 return;
1844 }
1845
1846 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001847 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001848 }
1849
1850 /**
1851 * Make sure the caller has carrier privilege.
1852 *
1853 * @throws SecurityException if the caller does not have the required permission
1854 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001855 private void enforceCarrierPrivilege(int subId) {
1856 if (getCarrierPrivilegeStatus(subId) !=
1857 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001858 loge("No Carrier Privilege.");
1859 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001860 }
1861 }
1862
1863 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001864 * Make sure the caller has the CALL_PHONE permission.
1865 *
1866 * @throws SecurityException if the caller does not have the required permission
1867 */
1868 private void enforceCallPermission() {
1869 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1870 }
1871
Stuart Scott8eef64f2015-04-08 15:13:54 -07001872 private void enforceConnectivityInternalPermission() {
1873 mApp.enforceCallingOrSelfPermission(
1874 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1875 "ConnectivityService");
1876 }
1877
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001878 private String createTelUrl(String number) {
1879 if (TextUtils.isEmpty(number)) {
1880 return null;
1881 }
1882
Jake Hambye994d462014-02-03 13:10:13 -08001883 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001884 }
1885
Ihab Awadf9e92732013-12-05 18:02:52 -08001886 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001887 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1888 }
1889
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001890 private static void logv(String msg) {
1891 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1892 }
1893
Ihab Awadf9e92732013-12-05 18:02:52 -08001894 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001895 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1896 }
1897
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001898 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001899 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001900 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001901 }
1902
Sanket Padawe356d7632015-06-22 14:03:32 -07001903 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001904 public int getActivePhoneTypeForSlot(int slotIndex) {
1905 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001906 if (phone == null) {
1907 return PhoneConstants.PHONE_TYPE_NONE;
1908 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001909 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001910 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001911 }
1912
1913 /**
1914 * Returns the CDMA ERI icon index to display
1915 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001916 @Override
1917 public int getCdmaEriIconIndex(String callingPackage) {
1918 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001919 }
1920
Sanket Padawe356d7632015-06-22 14:03:32 -07001921 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001922 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1923 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1924 return -1;
1925 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001926 final Phone phone = getPhone(subId);
1927 if (phone != null) {
1928 return phone.getCdmaEriIconIndex();
1929 } else {
1930 return -1;
1931 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001932 }
1933
1934 /**
1935 * Returns the CDMA ERI icon mode,
1936 * 0 - ON
1937 * 1 - FLASHING
1938 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001939 @Override
1940 public int getCdmaEriIconMode(String callingPackage) {
1941 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001942 }
1943
Sanket Padawe356d7632015-06-22 14:03:32 -07001944 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001945 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1946 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1947 return -1;
1948 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001949 final Phone phone = getPhone(subId);
1950 if (phone != null) {
1951 return phone.getCdmaEriIconMode();
1952 } else {
1953 return -1;
1954 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001955 }
1956
1957 /**
1958 * Returns the CDMA ERI text,
1959 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001960 @Override
1961 public String getCdmaEriText(String callingPackage) {
1962 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001963 }
1964
Sanket Padawe356d7632015-06-22 14:03:32 -07001965 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001966 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1967 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1968 return null;
1969 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001970 final Phone phone = getPhone(subId);
1971 if (phone != null) {
1972 return phone.getCdmaEriText();
1973 } else {
1974 return null;
1975 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001976 }
1977
1978 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001979 * Returns the CDMA MDN.
1980 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001981 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001982 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001983 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001984 final Phone phone = getPhone(subId);
1985 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1986 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001987 } else {
1988 return null;
1989 }
1990 }
1991
1992 /**
1993 * Returns the CDMA MIN.
1994 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001995 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001996 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001997 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001998 final Phone phone = getPhone(subId);
1999 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2000 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07002001 } else {
2002 return null;
2003 }
2004 }
2005
2006 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002007 * Returns true if CDMA provisioning needs to run.
2008 */
2009 public boolean needsOtaServiceProvisioning() {
2010 return mPhone.needsOtaServiceProvisioning();
2011 }
2012
2013 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002014 * Sets the voice mail number of a given subId.
2015 */
2016 @Override
2017 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002018 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002019 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2020 new Pair<String, String>(alphaTag, number), new Integer(subId));
2021 return success;
2022 }
2023
Ta-wei Yen87c49842016-05-13 21:19:52 -07002024 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07002025 public void setVisualVoicemailEnabled(String callingPackage,
2026 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
2027 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2028 if (!TextUtils.equals(callingPackage,
2029 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2030 enforceModifyPermissionOrCarrierPrivilege(
2031 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
2032 }
2033 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2034 }
2035
2036 @Override
2037 public boolean isVisualVoicemailEnabled(String callingPackage,
2038 PhoneAccountHandle phoneAccountHandle) {
2039 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2040 return false;
2041 }
2042 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2043 }
2044
2045 @Override
Ta-wei Yen12551102017-03-06 16:00:44 -08002046 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yen243b6372017-01-10 16:17:08 -08002047 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2048 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2049 return null;
2050 }
Ta-wei Yen243b6372017-01-10 16:17:08 -08002051 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2052 }
2053
2054 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002055 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2056 VisualVoicemailSmsFilterSettings settings) {
2057 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002058 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002059 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2060 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002061 }
2062
2063 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002064 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2065 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002066 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002067 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002068 }
2069
2070 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002071 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2072 String callingPackage, int subId) {
2073 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002074 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002075 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002076 }
2077
2078 @Override
Ta-wei Yen1b455992016-12-27 14:52:32 -08002079 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002080 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002081 return VisualVoicemailSmsFilterConfig
Ta-wei Yen1b455992016-12-27 14:52:32 -08002082 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2083 }
2084
2085 @Override
2086 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2087 String number, int port, String text, PendingIntent sentIntent) {
2088 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen463efd12017-01-06 15:29:25 -08002089 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen1b455992016-12-27 14:52:32 -08002090 enforceSendSmsPermission();
2091 // Make the calls as the phone process.
2092 final long identity = Binder.clearCallingIdentity();
2093 try {
2094 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2095 if (port == 0) {
2096 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2097 sentIntent, null, false);
2098 } else {
2099 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2100 smsManager.sendDataMessageWithSelfPermissions(number, null,
2101 (short) port, data, sentIntent, null);
2102 }
2103 } finally {
2104 Binder.restoreCallingIdentity(identity);
2105 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002106 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002107 /**
fionaxuef039d42016-11-14 13:36:14 -08002108 * Sets the voice activation state of a given subId.
2109 */
2110 @Override
2111 public void setVoiceActivationState(int subId, int activationState) {
2112 enforceModifyPermissionOrCarrierPrivilege(subId);
2113 final Phone phone = getPhone(subId);
2114 if (phone != null) {
2115 phone.setVoiceActivationState(activationState);
2116 } else {
2117 loge("setVoiceActivationState fails with invalid subId: " + subId);
2118 }
2119 }
2120
2121 /**
2122 * Sets the data activation state of a given subId.
2123 */
2124 @Override
2125 public void setDataActivationState(int subId, int activationState) {
2126 enforceModifyPermissionOrCarrierPrivilege(subId);
2127 final Phone phone = getPhone(subId);
2128 if (phone != null) {
2129 phone.setDataActivationState(activationState);
2130 } else {
2131 loge("setVoiceActivationState fails with invalid subId: " + subId);
2132 }
2133 }
2134
2135 /**
2136 * Returns the voice activation state of a given subId.
2137 */
2138 @Override
2139 public int getVoiceActivationState(int subId, String callingPackage) {
2140 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2141 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2142 }
2143 final Phone phone = getPhone(subId);
2144 if (phone != null) {
2145 return phone.getVoiceActivationState();
2146 } else {
2147 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2148 }
2149 }
2150
2151 /**
2152 * Returns the data activation state of a given subId.
2153 */
2154 @Override
2155 public int getDataActivationState(int subId, String callingPackage) {
2156 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2157 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2158 }
2159 final Phone phone = getPhone(subId);
2160 if (phone != null) {
2161 return phone.getDataActivationState();
2162 } else {
2163 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2164 }
2165 }
2166
2167 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002168 * Returns the unread count of voicemails
2169 */
2170 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002171 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002172 }
2173
2174 /**
2175 * Returns the unread count of voicemails for a subId
2176 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002177 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002178 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002179 final Phone phone = getPhone(subId);
2180 if (phone != null) {
2181 return phone.getVoiceMessageCount();
2182 } else {
2183 return 0;
2184 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002185 }
2186
2187 /**
pkanwar8efe6572017-01-19 13:43:16 -08002188 * returns true, if the device is in a state where both voice and data
2189 * are supported simultaneously. This can change based on location or network condition.
2190 */
2191 @Override
2192 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2193 final Phone phone = getPhone(subId);
2194 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2195 }
2196
2197 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002198 * Returns the data network type.
2199 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002200 *
2201 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2202 */
2203 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002204 public int getNetworkType() {
2205 final Phone phone = getPhone(getDefaultSubscription());
2206 if (phone != null) {
2207 return phone.getServiceState().getDataNetworkType();
2208 } else {
2209 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2210 }
Wink Saville36469e72014-06-11 15:17:00 -07002211 }
2212
2213 /**
2214 * Returns the network type for a subId
2215 */
2216 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002217 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2218 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2219 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2220 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002221
Sanket Padawe356d7632015-06-22 14:03:32 -07002222 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002223 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002224 return phone.getServiceState().getDataNetworkType();
2225 } else {
2226 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2227 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002228 }
2229
2230 /**
2231 * Returns the data network type
2232 */
2233 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002234 public int getDataNetworkType(String callingPackage) {
2235 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002236 }
2237
2238 /**
2239 * Returns the data network type for a subId
2240 */
2241 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002242 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2243 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2244 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2245 }
2246
Sanket Padawe356d7632015-06-22 14:03:32 -07002247 final Phone phone = getPhone(subId);
2248 if (phone != null) {
2249 return phone.getServiceState().getDataNetworkType();
2250 } else {
2251 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2252 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002253 }
2254
2255 /**
Wink Saville36469e72014-06-11 15:17:00 -07002256 * Returns the Voice network type for a subId
2257 */
2258 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002259 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2260 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2261 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2262 }
2263
Sanket Padawe356d7632015-06-22 14:03:32 -07002264 final Phone phone = getPhone(subId);
2265 if (phone != null) {
2266 return phone.getServiceState().getVoiceNetworkType();
2267 } else {
2268 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2269 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002270 }
2271
2272 /**
2273 * @return true if a ICC card is present
2274 */
2275 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002276 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002277 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2278 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002279 }
2280
2281 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002282 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002283 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002284 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002285 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2286 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002287 final Phone phone = getPhone(subId[0]);
2288 if (subId != null && phone != null) {
2289 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002290 } else {
2291 return false;
2292 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002293 }
2294
2295 /**
2296 * Return if the current radio is LTE on CDMA. This
2297 * is a tri-state return value as for a period of time
2298 * the mode may be unknown.
2299 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002300 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002301 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002302 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002303 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002304 @Override
2305 public int getLteOnCdmaMode(String callingPackage) {
2306 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002307 }
2308
Sanket Padawe356d7632015-06-22 14:03:32 -07002309 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002310 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2311 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2312 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2313 }
2314
Sanket Padawe356d7632015-06-22 14:03:32 -07002315 final Phone phone = getPhone(subId);
2316 if (phone == null) {
2317 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2318 } else {
2319 return phone.getLteOnCdmaMode();
2320 }
Wink Saville36469e72014-06-11 15:17:00 -07002321 }
2322
2323 public void setPhone(Phone phone) {
2324 mPhone = phone;
2325 }
2326
2327 /**
2328 * {@hide}
2329 * Returns Default subId, 0 in the case of single standby.
2330 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002331 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002332 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002333 }
2334
Shishir Agrawala9f32182016-04-12 12:00:16 -07002335 private int getSlotForDefaultSubscription() {
2336 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2337 }
2338
Wink Savilleb564aae2014-10-23 10:18:09 -07002339 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002340 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002341 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002342
2343 /**
2344 * @see android.telephony.TelephonyManager.WifiCallingChoices
2345 */
2346 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002347 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2348 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002349 }
2350
2351 /**
2352 * @see android.telephony.TelephonyManager.WifiCallingChoices
2353 */
2354 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002355 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2356 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2357 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002358 }
2359
Sailesh Nepald1e68152013-12-12 19:08:02 -08002360 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002361 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002362 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002363 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002364
Shishir Agrawal566b7612013-10-28 14:41:00 -07002365 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002366 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002367 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002368
Ajay Nambiabd73502015-12-03 13:50:00 -08002369 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002370 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002371 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002372 if (DBG) log("iccOpenLogicalChannel: " + response);
2373 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002374 }
2375
2376 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002377 public boolean iccCloseLogicalChannel(int subId, int channel) {
2378 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002379
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002380 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002381 if (channel < 0) {
2382 return false;
2383 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002384 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002385 if (DBG) log("iccCloseLogicalChannel: " + success);
2386 return success;
2387 }
2388
2389 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002390 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002391 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002392 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002393
2394 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002395 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2396 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002397 " data=" + data);
2398 }
2399
2400 if (channel < 0) {
2401 return "";
2402 }
2403
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002404 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002405 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002406 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2407
Shishir Agrawal566b7612013-10-28 14:41:00 -07002408 // Append the returned status code to the end of the response payload.
2409 String s = Integer.toHexString(
2410 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002411 if (response.payload != null) {
2412 s = IccUtils.bytesToHexString(response.payload) + s;
2413 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002414 return s;
2415 }
Jake Hambye994d462014-02-03 13:10:13 -08002416
Evan Charltonc66da362014-05-16 14:06:40 -07002417 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002418 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002419 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002420 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002421
2422 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002423 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2424 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002425 }
2426
2427 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002428 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002429 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2430
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002431 // Append the returned status code to the end of the response payload.
2432 String s = Integer.toHexString(
2433 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002434 if (response.payload != null) {
2435 s = IccUtils.bytesToHexString(response.payload) + s;
2436 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002437 return s;
2438 }
2439
2440 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002441 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002442 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002443 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002444
2445 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002446 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002447 p1 + " " + p2 + " " + p3 + ":" + filePath);
2448 }
2449
2450 IccIoResult response =
2451 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002452 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2453 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002454
2455 if (DBG) {
2456 log("Exchange SIM_IO [R]" + response);
2457 }
2458
2459 byte[] result = null;
2460 int length = 2;
2461 if (response.payload != null) {
2462 length = 2 + response.payload.length;
2463 result = new byte[length];
2464 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2465 } else {
2466 result = new byte[length];
2467 }
2468
2469 result[length - 1] = (byte) response.sw2;
2470 result[length - 2] = (byte) response.sw1;
2471 return result;
2472 }
2473
Nathan Haroldb3014052017-01-25 15:57:32 -08002474 /**
2475 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2476 * on a particular subscription
2477 */
2478 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002479 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2480 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002481 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2482 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2483 return null;
2484 }
2485 Object response = sendRequest(
2486 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2487 if (response instanceof String[]) {
2488 return (String[]) response;
2489 }
2490 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2491 return null;
2492 }
2493
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002494 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002495 public String sendEnvelopeWithStatus(int subId, String content) {
2496 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002497
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002498 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002499 if (response.payload == null) {
2500 return "";
2501 }
2502
2503 // Append the returned status code to the end of the response payload.
2504 String s = Integer.toHexString(
2505 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2506 s = IccUtils.bytesToHexString(response.payload) + s;
2507 return s;
2508 }
2509
Jake Hambye994d462014-02-03 13:10:13 -08002510 /**
2511 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2512 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2513 *
2514 * @param itemID the ID of the item to read
2515 * @return the NV item as a String, or null on error.
2516 */
2517 @Override
2518 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002519 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002520 if (DBG) log("nvReadItem: item " + itemID);
2521 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2522 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2523 return value;
2524 }
2525
2526 /**
2527 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2528 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2529 *
2530 * @param itemID the ID of the item to read
2531 * @param itemValue the value to write, as a String
2532 * @return true on success; false on any failure
2533 */
2534 @Override
2535 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002536 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002537 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2538 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2539 new Pair<Integer, String>(itemID, itemValue));
2540 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2541 return success;
2542 }
2543
2544 /**
2545 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2546 * Used for device configuration by some CDMA operators.
2547 *
2548 * @param preferredRoamingList byte array containing the new PRL
2549 * @return true on success; false on any failure
2550 */
2551 @Override
2552 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002553 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002554 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2555 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2556 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2557 return success;
2558 }
2559
2560 /**
2561 * Perform the specified type of NV config reset.
2562 * Used for device configuration by some CDMA operators.
2563 *
2564 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2565 * @return true on success; false on any failure
2566 */
2567 @Override
2568 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002569 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002570 if (DBG) log("nvResetConfig: type " + resetType);
2571 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2572 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2573 return success;
2574 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002575
2576 /**
Wink Saville36469e72014-06-11 15:17:00 -07002577 * {@hide}
2578 * Returns Default sim, 0 in the case of single standby.
2579 */
2580 public int getDefaultSim() {
2581 //TODO Need to get it from Telephony Devcontroller
2582 return 0;
2583 }
2584
Svet Ganovb320e182015-04-16 12:30:10 -07002585 public String[] getPcscfAddress(String apnType, String callingPackage) {
2586 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2587 return new String[0];
2588 }
2589
2590
ram87fca6f2014-07-18 18:58:44 +05302591 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002592 }
2593
Brad Ebinger76681002017-01-23 13:50:20 -08002594 /**
2595 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2596 * feature or {@link null} if the service is not available. If an ImsServiceController is
2597 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2598 * feature updates.
2599 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002600 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002601 IImsServiceFeatureListener callback) {
2602 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002603 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002604 callback);
2605 }
2606
Wink Saville36469e72014-06-11 15:17:00 -07002607 public void setImsRegistrationState(boolean registered) {
2608 enforceModifyPermission();
2609 mPhone.setImsRegistrationState(registered);
2610 }
2611
2612 /**
Stuart Scott54788802015-03-30 13:18:01 -07002613 * Set the network selection mode to automatic.
2614 *
2615 */
2616 @Override
2617 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002618 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002619 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2620 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2621 }
2622
2623 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002624 * Set the network selection mode to manual with the selected carrier.
2625 */
2626 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002627 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2628 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002629 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002630 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002631 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2632 persistSelection);
2633 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002634 }
2635
2636 /**
2637 * Scans for available networks.
2638 */
2639 @Override
2640 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002641 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002642 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2643 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2644 CMD_PERFORM_NETWORK_SCAN, null, subId);
2645 return result;
2646 }
2647
2648 /**
yinxub1bed742017-04-17 11:45:04 -07002649 * Starts a new network scan and returns the id of this scan.
yinxu34eed212017-04-12 16:03:22 -07002650 *
yinxub1bed742017-04-17 11:45:04 -07002651 * @param subId id of the subscription
2652 * @param request contains the radio access networks with bands/channels to scan
2653 * @param messenger callback messenger for scan results or errors
2654 * @param binder for the purpose of auto clean when the user thread crashes
yinxu34eed212017-04-12 16:03:22 -07002655 * @return the id of the requested scan which can be used to stop the scan.
2656 */
2657 @Override
2658 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
2659 IBinder binder) {
yinxub1bed742017-04-17 11:45:04 -07002660 enforceModifyPermissionOrCarrierPrivilege(subId);
2661 return mNetworkScanRequestTracker.startNetworkScan(
2662 request, messenger, binder, getPhone(subId));
yinxu34eed212017-04-12 16:03:22 -07002663 }
2664
2665 /**
2666 * Stops an existing network scan with the given scanId.
yinxub1bed742017-04-17 11:45:04 -07002667 *
2668 * @param subId id of the subscription
2669 * @param scanId id of the scan that needs to be stopped
yinxu34eed212017-04-12 16:03:22 -07002670 */
2671 @Override
2672 public void stopNetworkScan(int subId, int scanId) {
yinxub1bed742017-04-17 11:45:04 -07002673 enforceModifyPermissionOrCarrierPrivilege(subId);
2674 mNetworkScanRequestTracker.stopNetworkScan(scanId);
yinxu34eed212017-04-12 16:03:22 -07002675 }
2676
2677 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002678 * Get the calculated preferred network type.
2679 * Used for debugging incorrect network type.
2680 *
2681 * @return the preferred network type, defined in RILConstants.java.
2682 */
2683 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002684 public int getCalculatedPreferredNetworkType(String callingPackage) {
2685 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2686 return RILConstants.PREFERRED_NETWORK_MODE;
2687 }
2688
Amit Mahajan43330e02014-11-18 11:54:45 -08002689 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002690 }
2691
2692 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002693 * Get the preferred network type.
2694 * Used for device configuration by some CDMA operators.
2695 *
2696 * @return the preferred network type, defined in RILConstants.java.
2697 */
2698 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002699 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002700 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002701 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002702 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002703 int networkType = (result != null ? result[0] : -1);
2704 if (DBG) log("getPreferredNetworkType: " + networkType);
2705 return networkType;
2706 }
2707
2708 /**
2709 * Set the preferred network type.
2710 * Used for device configuration by some CDMA operators.
2711 *
2712 * @param networkType the preferred network type, defined in RILConstants.java.
2713 * @return true on success; false on any failure.
2714 */
2715 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002716 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002717 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002718 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2719 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002720 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002721 if (success) {
2722 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002723 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002724 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002725 return success;
2726 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002727
2728 /**
Junda Liu475951f2014-11-07 16:45:03 -08002729 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2730 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2731 * tethering.
2732 *
2733 * @return 0: Not required. 1: required. 2: Not set.
2734 * @hide
2735 */
2736 @Override
2737 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002738 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002739 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2740 Settings.Global.TETHER_DUN_REQUIRED, 2);
2741 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2742 // config_tether_apndata.
2743 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2744 dunRequired = 1;
2745 }
2746 return dunRequired;
2747 }
2748
2749 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002750 * Set mobile data enabled
2751 * Used by the user through settings etc to turn on/off mobile data
2752 *
2753 * @param enable {@code true} turn turn data on, else {@code false}
2754 */
2755 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002756 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002757 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002758 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002759 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002760 Phone phone = PhoneFactory.getPhone(phoneId);
2761 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002762 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002763 phone.setDataEnabled(enable);
2764 } else {
2765 loge("setDataEnabled: no phone for subId=" + subId);
2766 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002767 }
2768
2769 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002770 * Get whether mobile data is enabled.
2771 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002772 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002773 *
2774 * @return {@code true} if data is enabled else {@code false}
2775 */
2776 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002777 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002778 try {
2779 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2780 null);
2781 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002782 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002783 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002784 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002785 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002786 Phone phone = PhoneFactory.getPhone(phoneId);
2787 if (phone != null) {
2788 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002789 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002790 return retVal;
2791 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002792 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002793 return false;
2794 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002795 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002796
2797 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002798 public int getCarrierPrivilegeStatus(int subId) {
2799 final Phone phone = getPhone(subId);
2800 if (phone == null) {
2801 loge("getCarrierPrivilegeStatus: Invalid subId");
2802 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2803 }
2804 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002805 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002806 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002807 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2808 }
2809 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002810 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002811 }
Junda Liu29340342014-07-10 15:23:27 -07002812
2813 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002814 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002815 if (TextUtils.isEmpty(pkgName))
2816 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002817 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002818 if (card == null) {
2819 loge("checkCarrierPrivilegesForPackage: No UICC");
2820 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2821 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002822 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2823 }
2824
2825 @Override
2826 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002827 if (TextUtils.isEmpty(pkgName))
2828 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002829 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2830 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2831 UiccCard card = UiccController.getInstance().getUiccCard(i);
2832 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002833 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002834 continue;
2835 }
2836
2837 result = card.getCarrierPrivilegeStatus(
2838 mPhone.getContext().getPackageManager(), pkgName);
2839 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2840 break;
2841 }
2842 }
2843
2844 return result;
Junda Liu29340342014-07-10 15:23:27 -07002845 }
Derek Tan89e89d42014-07-08 17:00:10 -07002846
2847 @Override
Junda Liue64de782015-04-16 17:19:16 -07002848 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2849 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2850 loge("phoneId " + phoneId + " is not valid.");
2851 return null;
2852 }
2853 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002854 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002855 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002856 return null ;
2857 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002858 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002859 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002860 }
2861
Amith Yamasani6e118872016-02-19 12:53:51 -08002862 @Override
2863 public List<String> getPackagesWithCarrierPrivileges() {
2864 PackageManager pm = mPhone.getContext().getPackageManager();
2865 List<String> privilegedPackages = new ArrayList<>();
2866 List<PackageInfo> packages = null;
2867 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2868 UiccCard card = UiccController.getInstance().getUiccCard(i);
2869 if (card == null) {
2870 // No UICC in that slot.
2871 continue;
2872 }
2873 if (card.hasCarrierPrivilegeRules()) {
2874 if (packages == null) {
2875 // Only check packages in user 0 for now
2876 packages = pm.getInstalledPackagesAsUser(
2877 PackageManager.MATCH_DISABLED_COMPONENTS
2878 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2879 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2880 }
2881 for (int p = packages.size() - 1; p >= 0; p--) {
2882 PackageInfo pkgInfo = packages.get(p);
2883 if (pkgInfo != null && pkgInfo.packageName != null
2884 && card.getCarrierPrivilegeStatus(pkgInfo)
2885 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2886 privilegedPackages.add(pkgInfo.packageName);
2887 }
2888 }
2889 }
2890 }
2891 return privilegedPackages;
2892 }
2893
Wink Savilleb564aae2014-10-23 10:18:09 -07002894 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002895 final Phone phone = getPhone(subId);
2896 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002897 if (card == null) {
2898 loge("getIccId: No UICC");
2899 return null;
2900 }
2901 String iccId = card.getIccId();
2902 if (TextUtils.isEmpty(iccId)) {
2903 loge("getIccId: ICC ID is null or empty.");
2904 return null;
2905 }
2906 return iccId;
2907 }
2908
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002909 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002910 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2911 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002912 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002913
Jeff Sharkey85190e62014-12-05 09:40:12 -08002914 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002915 final Phone phone = getPhone(subId);
2916 if (phone == null) {
2917 return false;
2918 }
2919 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002920
2921 if (DBG_MERGE) {
2922 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2923 + subscriberId + " to " + number);
2924 }
2925
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002926 if (TextUtils.isEmpty(iccId)) {
2927 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002928 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002929
Jeff Sharkey85190e62014-12-05 09:40:12 -08002930 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2931
2932 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002933 if (alphaTag == null) {
2934 editor.remove(alphaTagPrefKey);
2935 } else {
2936 editor.putString(alphaTagPrefKey, alphaTag);
2937 }
2938
Jeff Sharkey85190e62014-12-05 09:40:12 -08002939 // Record both the line number and IMSI for this ICCID, since we need to
2940 // track all merged IMSIs based on line number
2941 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2942 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002943 if (number == null) {
2944 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002945 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002946 } else {
2947 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002948 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002949 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002950
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002951 editor.commit();
2952 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002953 }
2954
2955 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002956 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002957 // This is open to apps with WRITE_SMS.
2958 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002959 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002960 return null;
2961 }
Derek Tan97ebb422014-09-05 16:55:38 -07002962
2963 String iccId = getIccId(subId);
2964 if (iccId != null) {
2965 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002966 if (DBG_MERGE) {
2967 log("getLine1NumberForDisplay returning " +
2968 mTelephonySharedPreferences.getString(numberPrefKey, null));
2969 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002970 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002971 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002972 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002973 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002974 }
2975
2976 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002977 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2978 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2979 return null;
2980 }
Derek Tan97ebb422014-09-05 16:55:38 -07002981
2982 String iccId = getIccId(subId);
2983 if (iccId != null) {
2984 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002985 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002986 }
Derek Tan97ebb422014-09-05 16:55:38 -07002987 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002988 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002989
2990 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002991 public String[] getMergedSubscriberIds(String callingPackage) {
2992 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2993 return null;
2994 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002995 final Context context = mPhone.getContext();
2996 final TelephonyManager tele = TelephonyManager.from(context);
2997 final SubscriptionManager sub = SubscriptionManager.from(context);
2998
2999 // Figure out what subscribers are currently active
3000 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07003001 // Clear calling identity, when calling TelephonyManager, because callerUid must be
3002 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
3003 final long identity = Binder.clearCallingIdentity();
3004 try {
3005 final int[] subIds = sub.getActiveSubscriptionIdList();
3006 for (int subId : subIds) {
3007 activeSubscriberIds.add(tele.getSubscriberId(subId));
3008 }
3009 } finally {
3010 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08003011 }
3012
3013 // First pass, find a number override for an active subscriber
3014 String mergeNumber = null;
3015 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
3016 for (String key : prefs.keySet()) {
3017 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
3018 final String subscriberId = (String) prefs.get(key);
3019 if (activeSubscriberIds.contains(subscriberId)) {
3020 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
3021 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3022 mergeNumber = (String) prefs.get(numberKey);
3023 if (DBG_MERGE) {
3024 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3025 + " for active subscriber " + subscriberId);
3026 }
3027 if (!TextUtils.isEmpty(mergeNumber)) {
3028 break;
3029 }
3030 }
3031 }
3032 }
3033
3034 // Shortcut when no active merged subscribers
3035 if (TextUtils.isEmpty(mergeNumber)) {
3036 return null;
3037 }
3038
3039 // Second pass, find all subscribers under that line override
3040 final ArraySet<String> result = new ArraySet<>();
3041 for (String key : prefs.keySet()) {
3042 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3043 final String number = (String) prefs.get(key);
3044 if (mergeNumber.equals(number)) {
3045 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3046 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3047 final String subscriberId = (String) prefs.get(subscriberKey);
3048 if (!TextUtils.isEmpty(subscriberId)) {
3049 result.add(subscriberId);
3050 }
3051 }
3052 }
3053 }
3054
3055 final String[] resultArray = result.toArray(new String[result.size()]);
3056 Arrays.sort(resultArray);
3057 if (DBG_MERGE) {
3058 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3059 }
3060 return resultArray;
3061 }
3062
3063 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003064 public boolean setOperatorBrandOverride(int subId, String brand) {
3065 enforceCarrierPrivilege(subId);
3066 final Phone phone = getPhone(subId);
3067 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003068 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003069
3070 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003071 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003072 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3073 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003074 enforceCarrierPrivilege(subId);
3075 final Phone phone = getPhone(subId);
3076 if (phone == null) {
3077 return false;
3078 }
3079 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003080 cdmaNonRoamingList);
3081 }
3082
3083 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003084 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003085 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3086 enforceModifyPermission();
3087
3088 int returnValue = 0;
3089 try {
3090 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3091 if(result.exception == null) {
3092 if (result.result != null) {
3093 byte[] responseData = (byte[])(result.result);
3094 if(responseData.length > oemResp.length) {
3095 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3096 responseData.length + "bytes. Buffer Size is " +
3097 oemResp.length + "bytes.");
3098 }
3099 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3100 returnValue = responseData.length;
3101 }
3102 } else {
3103 CommandException ex = (CommandException) result.exception;
3104 returnValue = ex.getCommandError().ordinal();
3105 if(returnValue > 0) returnValue *= -1;
3106 }
3107 } catch (RuntimeException e) {
3108 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3109 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3110 if(returnValue > 0) returnValue *= -1;
3111 }
3112
3113 return returnValue;
3114 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003115
3116 @Override
3117 public void setRadioCapability(RadioAccessFamily[] rafs) {
3118 try {
3119 ProxyController.getInstance().setRadioCapability(rafs);
3120 } catch (RuntimeException e) {
3121 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3122 }
3123 }
3124
3125 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003126 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3127 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3128 return RadioAccessFamily.RAF_UNKNOWN;
3129 }
3130
Wink Saville5d475dd2014-10-17 15:00:58 -07003131 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3132 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003133
3134 @Override
3135 public void enableVideoCalling(boolean enable) {
3136 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003137 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003138 }
3139
3140 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003141 public boolean isVideoCallingEnabled(String callingPackage) {
3142 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3143 return false;
3144 }
3145
Andrew Lee77527ac2014-10-21 16:57:39 -07003146 // Check the user preference and the system-level IMS setting. Even if the user has
3147 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3148 // In the long run, we may instead need to check if there exists a connection service
3149 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003150 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3151 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003152 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003153 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003154
Andrew Leea1239f22015-03-02 17:44:07 -08003155 @Override
3156 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003157 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003158 }
3159
3160 @Override
3161 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003162 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003163 }
3164
Andrew Lee9431b832015-03-09 18:46:45 -07003165 @Override
3166 public boolean isTtyModeSupported() {
3167 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3168 TelephonyManager telephonyManager =
3169 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003170 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003171 }
3172
3173 @Override
3174 public boolean isHearingAidCompatibilitySupported() {
3175 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3176 }
3177
Sanket Padawe7310cc72015-01-14 09:53:20 -08003178 /**
3179 * Returns the unique device ID of phone, for example, the IMEI for
3180 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3181 *
3182 * <p>Requires Permission:
3183 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3184 */
3185 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003186 public String getDeviceId(String callingPackage) {
3187 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3188 return null;
3189 }
3190
Sanket Padawe7310cc72015-01-14 09:53:20 -08003191 final Phone phone = PhoneFactory.getPhone(0);
3192 if (phone != null) {
3193 return phone.getDeviceId();
3194 } else {
3195 return null;
3196 }
3197 }
3198
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003199 /*
3200 * {@hide}
3201 * Returns the IMS Registration Status
3202 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003203 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003204 public boolean isImsRegistered() {
3205 return mPhone.isImsRegistered();
3206 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003207
3208 @Override
3209 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3210 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3211 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003212
Nathan Haroldc55097a2015-03-11 18:14:50 -07003213 /*
3214 * {@hide}
3215 * Returns the IMS Registration Status
3216 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003217 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003218 return mPhone.isWifiCallingEnabled();
3219 }
3220
3221 /*
3222 * {@hide}
3223 * Returns the IMS Registration Status
3224 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003225 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003226 return mPhone.isVolteEnabled();
3227 }
Svet Ganovb320e182015-04-16 12:30:10 -07003228
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003229 /*
3230 * {@hide} Returns the IMS Registration Status
3231 */
3232 public boolean isVideoTelephonyAvailable() {
3233 return mPhone.isVideoEnabled();
3234 }
3235
Svet Ganovb320e182015-04-16 12:30:10 -07003236 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003237 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003238 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003239 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3240
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003241 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003242 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003243 } catch (SecurityException e) {
3244 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3245 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003246 }
Svet Ganovb320e182015-04-16 12:30:10 -07003247
3248 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3249 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3250 return false;
3251 }
3252
3253 return true;
3254 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003255
Makoto Onukifee69342015-06-29 14:44:50 -07003256 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003257 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003258 */
3259 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003260 // Default SMS app can always read it.
3261 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3262 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3263 return true;
3264 }
3265 try {
3266 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003267 } catch (SecurityException readPhoneStateSecurityException) {
3268 try {
3269 // Can be read with READ_SMS too.
3270 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3271 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3272 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3273 } catch (SecurityException readSmsSecurityException) {
3274 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3275 // permissions
3276 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3277 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3278 " or " + android.Manifest.permission.READ_SMS + ".");
3279 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003280 }
Makoto Onukifee69342015-06-29 14:44:50 -07003281 }
3282
Stuart Scott8eef64f2015-04-08 15:13:54 -07003283 @Override
3284 public void factoryReset(int subId) {
3285 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003286 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3287 return;
3288 }
3289
Svet Ganovcc087f82015-05-12 20:35:54 -07003290 final long identity = Binder.clearCallingIdentity();
3291 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003292 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3293 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003294 // Enable data
3295 setDataEnabled(subId, true);
3296 // Set network selection mode to automatic
3297 setNetworkSelectionModeAutomatic(subId);
3298 // Set preferred mobile network type to the best available
3299 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3300 // Turn off roaming
3301 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3302 }
3303 } finally {
3304 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003305 }
3306 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003307
3308 @Override
3309 public String getLocaleFromDefaultSim() {
3310 // We query all subscriptions instead of just the active ones, because
3311 // this might be called early on in the provisioning flow when the
3312 // subscriptions potentially aren't active yet.
3313 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3314 if (slist == null || slist.isEmpty()) {
3315 return null;
3316 }
3317
3318 // This function may be called very early, say, from the setup wizard, at
3319 // which point we won't have a default subscription set. If that's the case
3320 // we just choose the first, which will be valid in "most cases".
3321 final int defaultSubId = getDefaultSubscription();
3322 SubscriptionInfo info = null;
3323 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3324 info = slist.get(0);
3325 } else {
3326 for (SubscriptionInfo item : slist) {
3327 if (item.getSubscriptionId() == defaultSubId) {
3328 info = item;
3329 break;
3330 }
3331 }
3332
3333 if (info == null) {
3334 return null;
3335 }
3336 }
3337
3338 // Try and fetch the locale from the carrier properties or from the SIM language
3339 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003340 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003341 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003342 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003343 if (defaultPhone != null) {
3344 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3345 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003346 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003347 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3348 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003349 } else {
3350 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003351 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003352 }
3353 }
3354
Narayan Kamath011676f2015-07-29 12:04:08 +01003355 // The SIM language preferences only store a language (e.g. fr = French), not an
3356 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3357 // the SIM and carrier preferences does not include a country we add the country
3358 // determined from the SIM MCC to provide an exact locale.
3359 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003360 if (mccLocale != null) {
3361 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3362 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003363 }
3364
Tony Hill183b2de2015-06-24 14:53:58 +01003365 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003366 return null;
3367 }
3368
3369 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3370 final long identity = Binder.clearCallingIdentity();
3371 try {
3372 return mSubscriptionController.getAllSubInfoList(
3373 mPhone.getContext().getOpPackageName());
3374 } finally {
3375 Binder.restoreCallingIdentity(identity);
3376 }
3377 }
3378
3379 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3380 final long identity = Binder.clearCallingIdentity();
3381 try {
3382 return mSubscriptionController.getActiveSubscriptionInfoList(
3383 mPhone.getContext().getOpPackageName());
3384 } finally {
3385 Binder.restoreCallingIdentity(identity);
3386 }
3387 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003388
3389 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003390 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3391 * representing the state of the modem.
3392 *
3393 * NOTE: This clears the modem state, so there should only every be one caller.
3394 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003395 */
3396 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003397 public void requestModemActivityInfo(ResultReceiver result) {
3398 enforceModifyPermission();
3399
3400 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3401 Bundle bundle = new Bundle();
3402 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3403 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003404 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003405
3406 /**
3407 * {@hide}
3408 * Returns the service state information on specified subscription.
3409 */
3410 @Override
3411 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3412
3413 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3414 return null;
3415 }
3416
3417 final Phone phone = getPhone(subId);
3418 if (phone == null) {
3419 return null;
3420 }
3421
3422 return phone.getServiceState();
3423 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003424
3425 /**
3426 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3427 *
3428 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3429 * voicemail ringtone.
3430 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3431 * PhoneAccount.
3432 */
3433 @Override
3434 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3435 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3436 if (phone == null) {
3437 return null;
3438 }
3439
3440 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3441 }
3442
3443 /**
3444 * Returns whether vibration is set for voicemail notification in Phone settings.
3445 *
3446 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3447 * voicemail vibration setting.
3448 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3449 */
3450 @Override
3451 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3452 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3453 if (phone == null) {
3454 return false;
3455 }
3456
3457 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3458 }
3459
Youhan Wange64578a2016-05-02 15:32:42 -07003460 /**
3461 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3462 *
3463 * @throws SecurityException if the caller does not have the required permission
3464 */
3465 private void enforceReadPrivilegedPermission() {
3466 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3467 null);
3468 }
3469
3470 /**
Ta-wei Yen1b455992016-12-27 14:52:32 -08003471 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3472 * permission.
3473 *
3474 * @throws SecurityException if the caller does not have the required permission
3475 */
3476 private void enforceSendSmsPermission() {
3477 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3478 }
3479
3480 /**
Ta-wei Yen463efd12017-01-06 15:29:25 -08003481 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003482 *
Ta-wei Yen463efd12017-01-06 15:29:25 -08003483 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003484 */
Ta-wei Yen463efd12017-01-06 15:29:25 -08003485 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
3486 String vvmPackage = RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId)
3487 .getPackageName();
3488 if (!callingPackage.equals(vvmPackage)) {
3489 throw new SecurityException("Caller not current active visual voicemail package[" +
3490 vvmPackage + "]");
Ta-wei Yen1b455992016-12-27 14:52:32 -08003491 }
3492 }
3493
3494 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003495 * Return the application ID for the app type.
3496 *
3497 * @param subId the subscription ID that this request applies to.
3498 * @param appType the uicc app type.
3499 * @return Application ID for specificied app type, or null if no uicc.
3500 */
3501 @Override
3502 public String getAidForAppType(int subId, int appType) {
3503 enforceReadPrivilegedPermission();
3504 Phone phone = getPhone(subId);
3505 if (phone == null) {
3506 return null;
3507 }
3508 String aid = null;
3509 try {
3510 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3511 .getApplicationByType(appType).getAid();
3512 } catch (Exception e) {
3513 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3514 }
3515 return aid;
3516 }
3517
Youhan Wang4001d252016-05-11 10:29:41 -07003518 /**
3519 * Return the Electronic Serial Number.
3520 *
3521 * @param subId the subscription ID that this request applies to.
3522 * @return ESN or null if error.
3523 */
3524 @Override
3525 public String getEsn(int subId) {
3526 enforceReadPrivilegedPermission();
3527 Phone phone = getPhone(subId);
3528 if (phone == null) {
3529 return null;
3530 }
3531 String esn = null;
3532 try {
3533 esn = phone.getEsn();
3534 } catch (Exception e) {
3535 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3536 }
3537 return esn;
3538 }
3539
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003540 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003541 * Return the Preferred Roaming List Version.
3542 *
3543 * @param subId the subscription ID that this request applies to.
3544 * @return PRLVersion or null if error.
3545 */
3546 @Override
3547 public String getCdmaPrlVersion(int subId) {
3548 enforceReadPrivilegedPermission();
3549 Phone phone = getPhone(subId);
3550 if (phone == null) {
3551 return null;
3552 }
3553 String cdmaPrlVersion = null;
3554 try {
3555 cdmaPrlVersion = phone.getCdmaPrlVersion();
3556 } catch (Exception e) {
3557 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3558 }
3559 return cdmaPrlVersion;
3560 }
3561
3562 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003563 * Get snapshot of Telephony histograms
3564 * @return List of Telephony histograms
3565 * @hide
3566 */
3567 @Override
3568 public List<TelephonyHistogram> getTelephonyHistograms() {
3569 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3570 return RIL.getTelephonyRILTimingHistograms();
3571 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003572
3573 /**
3574 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003575 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003576 * Require system privileges. In the future we may add this to carrier APIs.
3577 *
3578 * @return The number of carriers set successfully, should match length of carriers
3579 */
3580 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003581 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003582 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003583 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003584 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3585 return retVal[0];
3586 }
3587
3588 /**
3589 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003590 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003591 * Require system privileges. In the future we may add this to carrier APIs.
3592 *
3593 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3594 * means all carriers are allowed.
3595 */
3596 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003597 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003598 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003599 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003600 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3601 }
3602
fionaxu59545b42016-05-25 15:53:37 -07003603 /**
3604 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3605 * @param subId the subscription ID that this action applies to.
3606 * @param enabled control enable or disable metered apns.
3607 * {@hide}
3608 */
3609 @Override
3610 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3611 enforceModifyPermission();
3612 final Phone phone = getPhone(subId);
3613 if (phone == null) {
3614 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3615 return;
3616 }
3617 try {
3618 phone.carrierActionSetMeteredApnsEnabled(enabled);
3619 } catch (Exception e) {
3620 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3621 }
3622 }
3623
3624 /**
3625 * Action set from carrier signalling broadcast receivers to enable/disable radio
3626 * @param subId the subscription ID that this action applies to.
3627 * @param enabled control enable or disable radio.
3628 * {@hide}
3629 */
3630 @Override
3631 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3632 enforceModifyPermission();
3633 final Phone phone = getPhone(subId);
3634 if (phone == null) {
3635 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3636 return;
3637 }
3638 try {
3639 phone.carrierActionSetRadioEnabled(enabled);
3640 } catch (Exception e) {
3641 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3642 }
3643 }
3644
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003645 /**
3646 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3647 * bug report is being generated.
3648 */
3649 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003650 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003651 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3652 != PackageManager.PERMISSION_GRANTED) {
3653 writer.println("Permission Denial: can't dump Phone from pid="
3654 + Binder.getCallingPid()
3655 + ", uid=" + Binder.getCallingUid()
3656 + "without permission "
3657 + android.Manifest.permission.DUMP);
3658 return;
3659 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003660 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003661 }
Jack Yueb89b242016-06-22 13:27:47 -07003662
3663 /**
Jack Yuc83dc1c2017-05-26 16:07:50 -07003664 * Get aggregated video call data usage since boot.
3665 *
3666 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
3667 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07003668 * {@hide}
3669 */
3670 @Override
Jack Yuc83dc1c2017-05-26 16:07:50 -07003671 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07003672 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3673 null);
3674
Jack Yuc83dc1c2017-05-26 16:07:50 -07003675 // NetworkStatsService keeps tracking the active network interface and identity. It
3676 // records the delta with the corresponding network identity. We just return the total video
3677 // call data usage snapshot since boot.
3678 Phone phone = getPhone(subId);
3679 if (phone != null) {
3680 return phone.getVtDataUsage(perUidStats);
Jack Yueb89b242016-06-22 13:27:47 -07003681 }
Jack Yuc83dc1c2017-05-26 16:07:50 -07003682 return null;
Jack Yueb89b242016-06-22 13:27:47 -07003683 }
Jack Yu75ab2952016-07-08 14:29:33 -07003684
3685 /**
3686 * Policy control of data connection. Usually used when data limit is passed.
3687 * @param enabled True if enabling the data, otherwise disabling.
3688 * @param subId Subscription index
3689 * {@hide}
3690 */
3691 @Override
3692 public void setPolicyDataEnabled(boolean enabled, int subId) {
3693 enforceModifyPermission();
3694 Phone phone = getPhone(subId);
3695 if (phone != null) {
3696 phone.setPolicyDataEnabled(enabled);
3697 }
3698 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003699
3700 /**
3701 * Get Client request stats
3702 * @return List of Client Request Stats
3703 * @hide
3704 */
3705 @Override
3706 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3707 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3708 return null;
3709 }
3710
3711 Phone phone = getPhone(subId);
3712 if (phone != null) {
3713 return phone.getClientRequestStats();
3714 }
3715
3716 return null;
3717 }
3718
3719 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3720 if (workSource != null) {
3721 return workSource;
3722 }
3723
3724 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3725 workSource = new WorkSource(uid, packageName);
3726 return workSource;
3727 }
Jack Yu346de222017-02-16 15:32:43 -08003728
3729 /**
Grace Chenf4a72232017-03-24 17:21:30 -07003730 * Set SIM card power state.
Jack Yu346de222017-02-16 15:32:43 -08003731 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003732 * @param slotIndex SIM slot id.
Grace Chenf4a72232017-03-24 17:21:30 -07003733 * @param state State of SIM (power down, power up, pass through)
3734 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
3735 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
3736 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yu346de222017-02-16 15:32:43 -08003737 *
3738 **/
3739 @Override
Grace Chenf4a72232017-03-24 17:21:30 -07003740 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yu346de222017-02-16 15:32:43 -08003741 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003742 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003743 if (subId == null || subId.length == 0) {
3744 return;
3745 }
3746
3747 final Phone phone = getPhone(subId[0]);
3748 if (phone != null) {
Grace Chenf4a72232017-03-24 17:21:30 -07003749 phone.setSimPowerState(state);
Jack Yu346de222017-02-16 15:32:43 -08003750 }
3751 }
shuoqfef36a32017-01-10 13:02:18 -08003752
3753 /**
3754 * Check if phone is in emergency callback mode
3755 * @return true if phone is in emergency callback mode
3756 * @param subId sub id
3757 */
3758 public boolean getEmergencyCallbackMode(int subId) {
3759 final Phone phone = getPhone(subId);
3760 if (phone != null) {
3761 return phone.isInEcm();
3762 } else {
3763 return false;
3764 }
3765 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08003766
3767 /**
3768 * Get the current signal strength information for the given subscription.
3769 * Because this information is not updated when the device is in a low power state
3770 * it should not be relied-upon to be current.
3771 * @param subId Subscription index
3772 * @return the most recent cached signal strength info from the modem
3773 */
3774 @Override
3775 public SignalStrength getSignalStrength(int subId) {
3776 Phone p = getPhone(subId);
3777 if (p == null) {
3778 return null;
3779 }
3780
3781 return p.getSignalStrength();
3782 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003783}