blob: 8670ae224405756c2489a54c93533ac3ed578e11 [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
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.ActivityManager;
22import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070023import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.Context;
25import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070026import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080027import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070028import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Looper;
35import android.os.Message;
36import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070037import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.ServiceManager;
39import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070040import android.os.UserManager;
Sooraj Sasindran22882212016-07-18 11:57:25 -070041import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070042import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080043import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070044import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080045import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080046import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070047import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070048import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.telephony.CellInfo;
Sooraj Sasindran22882212016-07-18 11:57:25 -070050import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070051import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070052import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080053import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070054import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080056import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080057import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070058import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070059import android.telephony.TelephonyManager;
60import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080062import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080064import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080065import android.util.Slog;
Andrew Lee312e8172014-10-23 17:01:36 -070066import com.android.ims.ImsManager;
Brad Ebinger76681002017-01-23 13:50:20 -080067import com.android.ims.internal.IImsServiceController;
68import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070069import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070070import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070071import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080074import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010075import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070076import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070078import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070079import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070080import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070081import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070082import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080083import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070084import com.android.internal.telephony.uicc.IccIoResult;
85import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080086import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070087import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080088import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070089import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080090import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070091import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080092import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -070093import java.io.FileDescriptor;
94import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080096import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080097import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010098import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080099import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100
101/**
102 * Implementation of the ITelephony interface.
103 */
Santos Cordon117fee72014-05-16 17:56:12 -0700104public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105 private static final String LOG_TAG = "PhoneInterfaceManager";
106 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
107 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800108 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109
110 // Message codes used with mMainThreadHandler
111 private static final int CMD_HANDLE_PIN_MMI = 1;
112 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
113 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
114 private static final int CMD_ANSWER_RINGING_CALL = 4;
115 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700116 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
117 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700118 private static final int CMD_OPEN_CHANNEL = 9;
119 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
120 private static final int CMD_CLOSE_CHANNEL = 11;
121 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800122 private static final int CMD_NV_READ_ITEM = 13;
123 private static final int EVENT_NV_READ_ITEM_DONE = 14;
124 private static final int CMD_NV_WRITE_ITEM = 15;
125 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
126 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
127 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
128 private static final int CMD_NV_RESET_CONFIG = 19;
129 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800130 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
131 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
132 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
133 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800134 private static final int CMD_SEND_ENVELOPE = 25;
135 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700136 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
137 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
138 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
139 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
140 private static final int CMD_EXCHANGE_SIM_IO = 31;
141 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800142 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
143 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700144 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
145 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700146 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
147 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700148 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
149 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
150 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
151 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700152 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
153 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
154 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
155 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar7e856482016-10-14 19:37:38 -0700156 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800157 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
158 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700159
160 /** The singleton instance. */
161 private static PhoneInterfaceManager sInstance;
162
Wink Saville3ab207e2014-11-20 13:07:20 -0800163 private PhoneGlobals mApp;
164 private Phone mPhone;
165 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700166 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800167 private AppOpsManager mAppOps;
168 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800169 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800170 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171
Derek Tan97ebb422014-09-05 16:55:38 -0700172 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
173 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800174 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700175
176 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700177 * A request object to use for transmitting data to an ICC.
178 */
179 private static final class IccAPDUArgument {
180 public int channel, cla, command, p1, p2, p3;
181 public String data;
182
183 public IccAPDUArgument(int channel, int cla, int command,
184 int p1, int p2, int p3, String data) {
185 this.channel = channel;
186 this.cla = cla;
187 this.command = command;
188 this.p1 = p1;
189 this.p2 = p2;
190 this.p3 = p3;
191 this.data = data;
192 }
193 }
194
195 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700196 * A request object to use for transmitting data to an ICC.
197 */
198 private static final class ManualNetworkSelectionArgument {
199 public OperatorInfo operatorInfo;
200 public boolean persistSelection;
201
202 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
203 this.operatorInfo = operatorInfo;
204 this.persistSelection = persistSelection;
205 }
206 }
207
208 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
210 * request after sending. The main thread will notify the request when it is complete.
211 */
212 private static final class MainThreadRequest {
213 /** The argument to use for the request */
214 public Object argument;
215 /** The result of the request that is run on the main thread */
216 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800217 // The subscriber id that this request applies to. Defaults to
218 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
219 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220
221 public MainThreadRequest(Object argument) {
222 this.argument = argument;
223 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800224
225 public MainThreadRequest(Object argument, Integer subId) {
226 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800227 if (subId != null) {
228 this.subId = subId;
229 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800230 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700231 }
232
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800233 private static final class IncomingThirdPartyCallArgs {
234 public final ComponentName component;
235 public final String callId;
236 public final String callerDisplayName;
237
238 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
239 String callerDisplayName) {
240 this.component = component;
241 this.callId = callId;
242 this.callerDisplayName = callerDisplayName;
243 }
244 }
245
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 /**
247 * A handler that processes messages on the main thread in the phone process. Since many
248 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
249 * inbound binder threads to the main thread in the phone process. The Binder thread
250 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
251 * on, which will be notified when the operation completes and will contain the result of the
252 * request.
253 *
254 * <p>If a MainThreadRequest object is provided in the msg.obj field,
255 * note that request.result must be set to something non-null for the calling thread to
256 * unblock.
257 */
258 private final class MainThreadHandler extends Handler {
259 @Override
260 public void handleMessage(Message msg) {
261 MainThreadRequest request;
262 Message onCompleted;
263 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800264 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700265 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266
267 switch (msg.what) {
pkanwar7e856482016-10-14 19:37:38 -0700268 case CMD_HANDLE_USSD_REQUEST: {
269 request = (MainThreadRequest) msg.obj;
270 final Phone phone = getPhoneFromRequest(request);
271 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
272 String ussdRequest = ussdObject.first;
273 ResultReceiver wrappedCallback = ussdObject.second;
274 request.result = phone != null ?
275 phone.handleUssdRequest(ussdRequest, wrappedCallback)
276 :false;
277 // Wake up the requesting thread
278 synchronized (request) {
279 request.notifyAll();
280 }
281 break;
282 }
283
Yorke Lee716f67e2015-06-17 15:39:16 -0700284 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700285 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700286 final Phone phone = getPhoneFromRequest(request);
287 request.result = phone != null ?
288 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
289 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 // Wake up the requesting thread
291 synchronized (request) {
292 request.notifyAll();
293 }
294 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700295 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296
297 case CMD_HANDLE_NEIGHBORING_CELL:
298 request = (MainThreadRequest) msg.obj;
299 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
300 request);
Sooraj Sasindran22882212016-07-18 11:57:25 -0700301 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 break;
303
304 case EVENT_NEIGHBORING_CELL_DONE:
305 ar = (AsyncResult) msg.obj;
306 request = (MainThreadRequest) ar.userObj;
307 if (ar.exception == null && ar.result != null) {
308 request.result = ar.result;
309 } else {
310 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800311 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700312 }
313 // Wake up the requesting thread
314 synchronized (request) {
315 request.notifyAll();
316 }
317 break;
318
319 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700320 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800321 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700322 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323 break;
324
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 case CMD_END_CALL:
326 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800327 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700328 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700329 Phone phone = getPhone(end_subId);
330 if (phone == null) {
331 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
332 break;
333 }
334 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700335 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
336 // CDMA: If the user presses the Power button we treat it as
337 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700338 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
340 // GSM: End the call as per the Phone state
341 hungUp = PhoneUtils.hangup(mCM);
342 } else {
343 throw new IllegalStateException("Unexpected phone type: " + phoneType);
344 }
345 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
346 request.result = hungUp;
347 // Wake up the requesting thread
348 synchronized (request) {
349 request.notifyAll();
350 }
351 break;
352
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700353 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700354 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700355 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800356 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700357 if (uiccCard == null) {
358 loge("iccTransmitApduLogicalChannel: No UICC");
359 request.result = new IccIoResult(0x6F, 0, (byte[])null);
360 synchronized (request) {
361 request.notifyAll();
362 }
363 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700364 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
365 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700366 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700367 iccArgument.channel, iccArgument.cla, iccArgument.command,
368 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700369 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700370 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700371 break;
372
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700373 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700374 ar = (AsyncResult) msg.obj;
375 request = (MainThreadRequest) ar.userObj;
376 if (ar.exception == null && ar.result != null) {
377 request.result = ar.result;
378 } else {
379 request.result = new IccIoResult(0x6F, 0, (byte[])null);
380 if (ar.result == null) {
381 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800382 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700383 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800384 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700385 } else {
386 loge("iccTransmitApduLogicalChannel: Unknown exception");
387 }
388 }
389 synchronized (request) {
390 request.notifyAll();
391 }
392 break;
393
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700394 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
395 request = (MainThreadRequest) msg.obj;
396 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800397 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700398 if (uiccCard == null) {
399 loge("iccTransmitApduBasicChannel: No UICC");
400 request.result = new IccIoResult(0x6F, 0, (byte[])null);
401 synchronized (request) {
402 request.notifyAll();
403 }
404 } else {
405 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
406 request);
407 uiccCard.iccTransmitApduBasicChannel(
408 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
409 iccArgument.p3, iccArgument.data, onCompleted);
410 }
411 break;
412
413 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
414 ar = (AsyncResult) msg.obj;
415 request = (MainThreadRequest) ar.userObj;
416 if (ar.exception == null && ar.result != null) {
417 request.result = ar.result;
418 } else {
419 request.result = new IccIoResult(0x6F, 0, (byte[])null);
420 if (ar.result == null) {
421 loge("iccTransmitApduBasicChannel: Empty response");
422 } else if (ar.exception instanceof CommandException) {
423 loge("iccTransmitApduBasicChannel: CommandException: " +
424 ar.exception);
425 } else {
426 loge("iccTransmitApduBasicChannel: Unknown exception");
427 }
428 }
429 synchronized (request) {
430 request.notifyAll();
431 }
432 break;
433
434 case CMD_EXCHANGE_SIM_IO:
435 request = (MainThreadRequest) msg.obj;
436 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800437 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700438 if (uiccCard == null) {
439 loge("iccExchangeSimIO: No UICC");
440 request.result = new IccIoResult(0x6F, 0, (byte[])null);
441 synchronized (request) {
442 request.notifyAll();
443 }
444 } else {
445 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
446 request);
447 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
448 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
449 iccArgument.data, onCompleted);
450 }
451 break;
452
453 case EVENT_EXCHANGE_SIM_IO_DONE:
454 ar = (AsyncResult) msg.obj;
455 request = (MainThreadRequest) ar.userObj;
456 if (ar.exception == null && ar.result != null) {
457 request.result = ar.result;
458 } else {
459 request.result = new IccIoResult(0x6f, 0, (byte[])null);
460 }
461 synchronized (request) {
462 request.notifyAll();
463 }
464 break;
465
Derek Tan4d5e5c12014-02-04 11:54:58 -0800466 case CMD_SEND_ENVELOPE:
467 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800468 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700469 if (uiccCard == null) {
470 loge("sendEnvelopeWithStatus: No UICC");
471 request.result = new IccIoResult(0x6F, 0, (byte[])null);
472 synchronized (request) {
473 request.notifyAll();
474 }
475 } else {
476 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
477 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
478 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800479 break;
480
481 case EVENT_SEND_ENVELOPE_DONE:
482 ar = (AsyncResult) msg.obj;
483 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700484 if (ar.exception == null && ar.result != null) {
485 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800486 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700487 request.result = new IccIoResult(0x6F, 0, (byte[])null);
488 if (ar.result == null) {
489 loge("sendEnvelopeWithStatus: Empty response");
490 } else if (ar.exception instanceof CommandException) {
491 loge("sendEnvelopeWithStatus: CommandException: " +
492 ar.exception);
493 } else {
494 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
495 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800496 }
497 synchronized (request) {
498 request.notifyAll();
499 }
500 break;
501
Shishir Agrawal566b7612013-10-28 14:41:00 -0700502 case CMD_OPEN_CHANNEL:
503 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800504 uiccCard = getUiccCardFromRequest(request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800505 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700506 if (uiccCard == null) {
507 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800508 request.result = new IccOpenLogicalChannelResponse(-1,
509 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700510 synchronized (request) {
511 request.notifyAll();
512 }
513 } else {
514 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800515 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
516 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700517 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700518 break;
519
520 case EVENT_OPEN_CHANNEL_DONE:
521 ar = (AsyncResult) msg.obj;
522 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700523 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700524 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700525 int[] result = (int[]) ar.result;
526 int channelId = result[0];
527 byte[] selectResponse = null;
528 if (result.length > 1) {
529 selectResponse = new byte[result.length - 1];
530 for (int i = 1; i < result.length; ++i) {
531 selectResponse[i - 1] = (byte) result[i];
532 }
533 }
534 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700535 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700536 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700537 if (ar.result == null) {
538 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700539 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700540 if (ar.exception != null) {
541 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
542 }
543
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700544 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700545 if (ar.exception instanceof CommandException) {
546 CommandException.Error error =
547 ((CommandException) (ar.exception)).getCommandError();
548 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700549 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700550 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700551 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700552 }
553 }
554 openChannelResp = new IccOpenLogicalChannelResponse(
555 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700556 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700557 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700558 synchronized (request) {
559 request.notifyAll();
560 }
561 break;
562
563 case CMD_CLOSE_CHANNEL:
564 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800565 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700566 if (uiccCard == null) {
567 loge("iccCloseLogicalChannel: No UICC");
568 request.result = new IccIoResult(0x6F, 0, (byte[])null);
569 synchronized (request) {
570 request.notifyAll();
571 }
572 } else {
573 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
574 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
575 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700576 break;
577
578 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800579 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
580 break;
581
582 case CMD_NV_READ_ITEM:
583 request = (MainThreadRequest) msg.obj;
584 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
585 mPhone.nvReadItem((Integer) request.argument, onCompleted);
586 break;
587
588 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700589 ar = (AsyncResult) msg.obj;
590 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800591 if (ar.exception == null && ar.result != null) {
592 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700593 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800594 request.result = "";
595 if (ar.result == null) {
596 loge("nvReadItem: Empty response");
597 } else if (ar.exception instanceof CommandException) {
598 loge("nvReadItem: CommandException: " +
599 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700600 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800601 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700602 }
603 }
604 synchronized (request) {
605 request.notifyAll();
606 }
607 break;
608
Jake Hambye994d462014-02-03 13:10:13 -0800609 case CMD_NV_WRITE_ITEM:
610 request = (MainThreadRequest) msg.obj;
611 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
612 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
613 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
614 break;
615
616 case EVENT_NV_WRITE_ITEM_DONE:
617 handleNullReturnEvent(msg, "nvWriteItem");
618 break;
619
620 case CMD_NV_WRITE_CDMA_PRL:
621 request = (MainThreadRequest) msg.obj;
622 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
623 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
624 break;
625
626 case EVENT_NV_WRITE_CDMA_PRL_DONE:
627 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
628 break;
629
630 case CMD_NV_RESET_CONFIG:
631 request = (MainThreadRequest) msg.obj;
632 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
633 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
634 break;
635
636 case EVENT_NV_RESET_CONFIG_DONE:
637 handleNullReturnEvent(msg, "nvResetConfig");
638 break;
639
Jake Hamby7c27be32014-03-03 13:25:59 -0800640 case CMD_GET_PREFERRED_NETWORK_TYPE:
641 request = (MainThreadRequest) msg.obj;
642 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700643 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800644 break;
645
646 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
647 ar = (AsyncResult) msg.obj;
648 request = (MainThreadRequest) ar.userObj;
649 if (ar.exception == null && ar.result != null) {
650 request.result = ar.result; // Integer
651 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800652 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800653 if (ar.result == null) {
654 loge("getPreferredNetworkType: Empty response");
655 } else if (ar.exception instanceof CommandException) {
656 loge("getPreferredNetworkType: CommandException: " +
657 ar.exception);
658 } else {
659 loge("getPreferredNetworkType: Unknown exception");
660 }
661 }
662 synchronized (request) {
663 request.notifyAll();
664 }
665 break;
666
667 case CMD_SET_PREFERRED_NETWORK_TYPE:
668 request = (MainThreadRequest) msg.obj;
669 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
670 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700671 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800672 break;
673
674 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
675 handleNullReturnEvent(msg, "setPreferredNetworkType");
676 break;
677
Steven Liu4bf01bc2014-07-17 11:05:29 -0500678 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
679 request = (MainThreadRequest)msg.obj;
680 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
681 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
682 break;
683
684 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
685 ar = (AsyncResult)msg.obj;
686 request = (MainThreadRequest)ar.userObj;
687 request.result = ar;
688 synchronized (request) {
689 request.notifyAll();
690 }
691 break;
692
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800693 case CMD_SET_VOICEMAIL_NUMBER:
694 request = (MainThreadRequest) msg.obj;
695 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
696 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800697 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
698 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800699 break;
700
701 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
702 handleNullReturnEvent(msg, "setVoicemailNumber");
703 break;
704
Stuart Scott54788802015-03-30 13:18:01 -0700705 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
706 request = (MainThreadRequest) msg.obj;
707 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
708 request);
709 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
710 break;
711
712 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
713 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
714 break;
715
Shishir Agrawal302c8692015-06-19 13:49:39 -0700716 case CMD_PERFORM_NETWORK_SCAN:
717 request = (MainThreadRequest) msg.obj;
718 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
719 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
720 break;
721
722 case EVENT_PERFORM_NETWORK_SCAN_DONE:
723 ar = (AsyncResult) msg.obj;
724 request = (MainThreadRequest) ar.userObj;
725 CellNetworkScanResult cellScanResult;
726 if (ar.exception == null && ar.result != null) {
727 cellScanResult = new CellNetworkScanResult(
728 CellNetworkScanResult.STATUS_SUCCESS,
729 (List<OperatorInfo>) ar.result);
730 } else {
731 if (ar.result == null) {
732 loge("getCellNetworkScanResults: Empty response");
733 }
734 if (ar.exception != null) {
735 loge("getCellNetworkScanResults: Exception: " + ar.exception);
736 }
737 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
738 if (ar.exception instanceof CommandException) {
739 CommandException.Error error =
740 ((CommandException) (ar.exception)).getCommandError();
741 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
742 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
743 } else if (error == CommandException.Error.GENERIC_FAILURE) {
744 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
745 }
746 }
747 cellScanResult = new CellNetworkScanResult(errorCode, null);
748 }
749 request.result = cellScanResult;
750 synchronized (request) {
751 request.notifyAll();
752 }
753 break;
754
755 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
756 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700757 ManualNetworkSelectionArgument selArg =
758 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700759 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
760 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700761 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
762 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700763 break;
764
765 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
766 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
767 break;
768
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700769 case CMD_GET_MODEM_ACTIVITY_INFO:
770 request = (MainThreadRequest) msg.obj;
771 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700772 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700773 break;
774
775 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
776 ar = (AsyncResult) msg.obj;
777 request = (MainThreadRequest) ar.userObj;
778 if (ar.exception == null && ar.result != null) {
779 request.result = ar.result;
780 } else {
781 if (ar.result == null) {
782 loge("queryModemActivityInfo: Empty response");
783 } else if (ar.exception instanceof CommandException) {
784 loge("queryModemActivityInfo: CommandException: " +
785 ar.exception);
786 } else {
787 loge("queryModemActivityInfo: Unknown exception");
788 }
789 }
Amit Mahajand4766222016-01-28 15:28:28 -0800790 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
791 if (request.result == null) {
792 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
793 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700794 synchronized (request) {
795 request.notifyAll();
796 }
797 break;
798
Meng Wang1a7c35a2016-05-05 20:56:15 -0700799 case CMD_SET_ALLOWED_CARRIERS:
800 request = (MainThreadRequest) msg.obj;
801 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
802 mPhone.setAllowedCarriers(
803 (List<CarrierIdentifier>) request.argument,
804 onCompleted);
805 break;
806
807 case EVENT_SET_ALLOWED_CARRIERS_DONE:
808 ar = (AsyncResult) msg.obj;
809 request = (MainThreadRequest) ar.userObj;
810 if (ar.exception == null && ar.result != null) {
811 request.result = ar.result;
812 } else {
813 if (ar.result == null) {
814 loge("setAllowedCarriers: Empty response");
815 } else if (ar.exception instanceof CommandException) {
816 loge("setAllowedCarriers: CommandException: " +
817 ar.exception);
818 } else {
819 loge("setAllowedCarriers: Unknown exception");
820 }
821 }
822 // Result cannot be null. Return -1 on error.
823 if (request.result == null) {
824 request.result = new int[]{-1};
825 }
826 synchronized (request) {
827 request.notifyAll();
828 }
829 break;
830
831 case CMD_GET_ALLOWED_CARRIERS:
832 request = (MainThreadRequest) msg.obj;
833 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
834 mPhone.getAllowedCarriers(onCompleted);
835 break;
836
837 case EVENT_GET_ALLOWED_CARRIERS_DONE:
838 ar = (AsyncResult) msg.obj;
839 request = (MainThreadRequest) ar.userObj;
840 if (ar.exception == null && ar.result != null) {
841 request.result = ar.result;
842 } else {
843 if (ar.result == null) {
844 loge("getAllowedCarriers: Empty response");
845 } else if (ar.exception instanceof CommandException) {
846 loge("getAllowedCarriers: CommandException: " +
847 ar.exception);
848 } else {
849 loge("getAllowedCarriers: Unknown exception");
850 }
851 }
852 // Result cannot be null. Return empty list of CarrierIdentifier.
853 if (request.result == null) {
854 request.result = new ArrayList<CarrierIdentifier>(0);
855 }
856 synchronized (request) {
857 request.notifyAll();
858 }
859 break;
860
Nathan Haroldb3014052017-01-25 15:57:32 -0800861 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
862 ar = (AsyncResult) msg.obj;
863 request = (MainThreadRequest) ar.userObj;
864 if (ar.exception == null && ar.result != null) {
865 request.result = ar.result;
866 } else {
867 request.result = new IllegalArgumentException(
868 "Failed to retrieve Forbidden Plmns");
869 if (ar.result == null) {
870 loge("getForbiddenPlmns: Empty response");
871 } else {
872 loge("getForbiddenPlmns: Unknown exception");
873 }
874 }
875 synchronized (request) {
876 request.notifyAll();
877 }
878 break;
879
880 case CMD_GET_FORBIDDEN_PLMNS:
881 request = (MainThreadRequest) msg.obj;
882 uiccCard = getUiccCardFromRequest(request);
883 if (uiccCard == null) {
884 loge("getForbiddenPlmns() UiccCard is null");
885 request.result = new IllegalArgumentException(
886 "getForbiddenPlmns() UiccCard is null");
887 synchronized (request) {
888 request.notifyAll();
889 }
890 break;
891 }
892 Integer appType = (Integer) request.argument;
893 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
894 if (uiccApp == null) {
895 loge("getForbiddenPlmns() no app with specified type -- "
896 + appType);
897 request.result = new IllegalArgumentException("Failed to get UICC App");
898 synchronized (request) {
899 request.notifyAll();
900 }
901 break;
902 } else {
903 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
904 + " specified type -- " + appType);
905 }
906 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
907 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
908 onCompleted);
909 break;
910
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700911 default:
912 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
913 break;
914 }
915 }
Jake Hambye994d462014-02-03 13:10:13 -0800916
917 private void handleNullReturnEvent(Message msg, String command) {
918 AsyncResult ar = (AsyncResult) msg.obj;
919 MainThreadRequest request = (MainThreadRequest) ar.userObj;
920 if (ar.exception == null) {
921 request.result = true;
922 } else {
923 request.result = false;
924 if (ar.exception instanceof CommandException) {
925 loge(command + ": CommandException: " + ar.exception);
926 } else {
927 loge(command + ": Unknown exception");
928 }
929 }
930 synchronized (request) {
931 request.notifyAll();
932 }
933 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700934 }
935
936 /**
937 * Posts the specified command to be executed on the main thread,
938 * waits for the request to complete, and returns the result.
939 * @see #sendRequestAsync
940 */
941 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800942 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700943 }
944
945 /**
946 * Posts the specified command to be executed on the main thread,
947 * waits for the request to complete, and returns the result.
948 * @see #sendRequestAsync
949 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800950 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700951 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
952 throw new RuntimeException("This method will deadlock if called from the main thread.");
953 }
954
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800955 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700956 Message msg = mMainThreadHandler.obtainMessage(command, request);
957 msg.sendToTarget();
958
959 // Wait for the request to complete
960 synchronized (request) {
961 while (request.result == null) {
962 try {
963 request.wait();
964 } catch (InterruptedException e) {
965 // Do nothing, go back and wait until the request is complete
966 }
967 }
968 }
969 return request.result;
970 }
971
972 /**
973 * Asynchronous ("fire and forget") version of sendRequest():
974 * Posts the specified command to be executed on the main thread, and
975 * returns immediately.
976 * @see #sendRequest
977 */
978 private void sendRequestAsync(int command) {
979 mMainThreadHandler.sendEmptyMessage(command);
980 }
981
982 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700983 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
984 * @see {@link #sendRequest(int,Object)}
985 */
986 private void sendRequestAsync(int command, Object argument) {
987 MainThreadRequest request = new MainThreadRequest(argument);
988 Message msg = mMainThreadHandler.obtainMessage(command, request);
989 msg.sendToTarget();
990 }
991
992 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700993 * Initialize the singleton PhoneInterfaceManager instance.
994 * This is only done once, at startup, from PhoneApp.onCreate().
995 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700996 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997 synchronized (PhoneInterfaceManager.class) {
998 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700999 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 } else {
1001 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1002 }
1003 return sInstance;
1004 }
1005 }
1006
1007 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001008 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 mApp = app;
1010 mPhone = phone;
1011 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001012 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1014 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001015 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001016 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001017 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001018
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 publish();
1020 }
1021
1022 private void publish() {
1023 if (DBG) log("publish: " + this);
1024
1025 ServiceManager.addService("phone", this);
1026 }
1027
Stuart Scott584921c2015-01-15 17:10:34 -08001028 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001029 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1030 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001031 }
1032
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001033 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1034 Phone phone = getPhoneFromRequest(request);
1035 return phone == null ? null :
1036 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1037 }
1038
Wink Saville36469e72014-06-11 15:17:00 -07001039 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001040 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001041 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001042 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001043 //
1044 // Implementation of the ITelephony interface.
1045 //
1046
1047 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001048 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001049 }
1050
Wink Savilleb564aae2014-10-23 10:18:09 -07001051 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001052 if (DBG) log("dial: " + number);
1053 // No permission check needed here: This is just a wrapper around the
1054 // ACTION_DIAL intent, which is available to any app since it puts up
1055 // the UI before it does anything.
1056
1057 String url = createTelUrl(number);
1058 if (url == null) {
1059 return;
1060 }
1061
1062 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001063 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001064 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1065 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1066 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1067 mApp.startActivity(intent);
1068 }
1069 }
1070
1071 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001072 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001073 }
1074
Wink Savilleb564aae2014-10-23 10:18:09 -07001075 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001076 if (DBG) log("call: " + number);
1077
1078 // This is just a wrapper around the ACTION_CALL intent, but we still
1079 // need to do a permission check since we're calling startActivity()
1080 // from the context of the phone app.
1081 enforceCallPermission();
1082
1083 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1084 != AppOpsManager.MODE_ALLOWED) {
1085 return;
1086 }
1087
1088 String url = createTelUrl(number);
1089 if (url == null) {
1090 return;
1091 }
1092
Wink Saville08874612014-08-31 19:19:58 -07001093 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001094 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001095 if (slist != null) {
1096 for (SubscriptionInfo subInfoRecord : slist) {
1097 if (subInfoRecord.getSubscriptionId() == subId) {
1098 isValid = true;
1099 break;
1100 }
Wink Saville08874612014-08-31 19:19:58 -07001101 }
1102 }
1103 if (isValid == false) {
1104 return;
1105 }
1106
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001107 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001108 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1110 mApp.startActivity(intent);
1111 }
1112
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001113 /**
1114 * End a call based on call state
1115 * @return true is a call was ended
1116 */
1117 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001118 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001119 }
1120
1121 /**
1122 * End a call based on the call state of the subId
1123 * @return true is a call was ended
1124 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001125 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001127 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001128 }
1129
1130 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001131 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001132 }
1133
Wink Savilleb564aae2014-10-23 10:18:09 -07001134 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001135 if (DBG) log("answerRingingCall...");
1136 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1137 // but that can probably wait till the big TelephonyManager API overhaul.
1138 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1139 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001140 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001141 }
1142
1143 /**
1144 * Make the actual telephony calls to implement answerRingingCall().
1145 * This should only be called from the main thread of the Phone app.
1146 * @see #answerRingingCall
1147 *
1148 * TODO: it would be nice to return true if we answered the call, or
1149 * false if there wasn't actually a ringing incoming call, or some
1150 * other error occurred. (In other words, pass back the return value
1151 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1152 * But that would require calling this method via sendRequest() rather
1153 * than sendRequestAsync(), and right now we don't actually *need* that
1154 * return value, so let's just return void for now.
1155 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001156 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001157 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001158 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001159 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1160 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001161 if (hasActiveCall && hasHoldingCall) {
1162 // Both lines are in use!
1163 // TODO: provide a flag to let the caller specify what
1164 // policy to use if both lines are in use. (The current
1165 // behavior is hardwired to "answer incoming, end ongoing",
1166 // which is how the CALL button is specced to behave.)
1167 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1168 return;
1169 } else {
1170 // answerCall() will automatically hold the current active
1171 // call, if there is one.
1172 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1173 return;
1174 }
1175 } else {
1176 // No call was ringing.
1177 return;
1178 }
1179 }
1180
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001181 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001182 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001183 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001184 public void silenceRinger() {
1185 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001186 }
1187
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001188 @Override
1189 public boolean isOffhook(String callingPackage) {
1190 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001191 }
1192
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001193 @Override
1194 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1195 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1196 return false;
1197 }
1198
Sanket Padawe356d7632015-06-22 14:03:32 -07001199 final Phone phone = getPhone(subId);
1200 if (phone != null) {
1201 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1202 } else {
1203 return false;
1204 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 }
1206
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001207 @Override
1208 public boolean isRinging(String callingPackage) {
1209 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001210 }
1211
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001212 @Override
1213 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1214 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1215 return false;
1216 }
1217
Sanket Padawe356d7632015-06-22 14:03:32 -07001218 final Phone phone = getPhone(subId);
1219 if (phone != null) {
1220 return (phone.getState() == PhoneConstants.State.RINGING);
1221 } else {
1222 return false;
1223 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001224 }
1225
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001226 @Override
1227 public boolean isIdle(String callingPackage) {
1228 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001229 }
1230
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001231 @Override
1232 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1233 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1234 return false;
1235 }
1236
Sanket Padawe356d7632015-06-22 14:03:32 -07001237 final Phone phone = getPhone(subId);
1238 if (phone != null) {
1239 return (phone.getState() == PhoneConstants.State.IDLE);
1240 } else {
1241 return false;
1242 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001243 }
1244
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001245 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001246 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001247 }
1248
Wink Savilleb564aae2014-10-23 10:18:09 -07001249 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001250 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001251 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1252 }
1253
1254 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001255 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001256 }
1257
Wink Savilleb564aae2014-10-23 10:18:09 -07001258 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001259 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001260 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1261 }
1262
1263 /** {@hide} */
1264 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001265 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001266 }
1267
Wink Savilleb564aae2014-10-23 10:18:09 -07001268 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001269 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001270 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001271 checkSimPin.start();
1272 return checkSimPin.unlockSim(null, pin);
1273 }
1274
Wink Saville9de0f752013-10-22 19:04:03 -07001275 /** {@hide} */
1276 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001277 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001278 }
1279
Wink Savilleb564aae2014-10-23 10:18:09 -07001280 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001281 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001282 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283 checkSimPuk.start();
1284 return checkSimPuk.unlockSim(puk, pin);
1285 }
1286
1287 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001288 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001289 * a synchronous one.
1290 */
1291 private static class UnlockSim extends Thread {
1292
1293 private final IccCard mSimCard;
1294
1295 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001296 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1297 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001298
1299 // For replies from SimCard interface
1300 private Handler mHandler;
1301
1302 // For async handler to identify request type
1303 private static final int SUPPLY_PIN_COMPLETE = 100;
1304
1305 public UnlockSim(IccCard simCard) {
1306 mSimCard = simCard;
1307 }
1308
1309 @Override
1310 public void run() {
1311 Looper.prepare();
1312 synchronized (UnlockSim.this) {
1313 mHandler = new Handler() {
1314 @Override
1315 public void handleMessage(Message msg) {
1316 AsyncResult ar = (AsyncResult) msg.obj;
1317 switch (msg.what) {
1318 case SUPPLY_PIN_COMPLETE:
1319 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1320 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001321 mRetryCount = msg.arg1;
1322 if (ar.exception != null) {
1323 if (ar.exception instanceof CommandException &&
1324 ((CommandException)(ar.exception)).getCommandError()
1325 == CommandException.Error.PASSWORD_INCORRECT) {
1326 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1327 } else {
1328 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1329 }
1330 } else {
1331 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1332 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333 mDone = true;
1334 UnlockSim.this.notifyAll();
1335 }
1336 break;
1337 }
1338 }
1339 };
1340 UnlockSim.this.notifyAll();
1341 }
1342 Looper.loop();
1343 }
1344
1345 /*
1346 * Use PIN or PUK to unlock SIM card
1347 *
1348 * If PUK is null, unlock SIM card with PIN
1349 *
1350 * If PUK is not null, unlock SIM card with PUK and set PIN code
1351 */
Wink Saville9de0f752013-10-22 19:04:03 -07001352 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353
1354 while (mHandler == null) {
1355 try {
1356 wait();
1357 } catch (InterruptedException e) {
1358 Thread.currentThread().interrupt();
1359 }
1360 }
1361 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1362
1363 if (puk == null) {
1364 mSimCard.supplyPin(pin, callback);
1365 } else {
1366 mSimCard.supplyPuk(puk, pin, callback);
1367 }
1368
1369 while (!mDone) {
1370 try {
1371 Log.d(LOG_TAG, "wait for done");
1372 wait();
1373 } catch (InterruptedException e) {
1374 // Restore the interrupted status
1375 Thread.currentThread().interrupt();
1376 }
1377 }
1378 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001379 int[] resultArray = new int[2];
1380 resultArray[0] = mResult;
1381 resultArray[1] = mRetryCount;
1382 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001383 }
1384 }
1385
1386 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001387 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001388
1389 }
1390
Wink Savilleb564aae2014-10-23 10:18:09 -07001391 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 // No permission check needed here: this call is harmless, and it's
1393 // needed for the ServiceState.requestStateUpdate() call (which is
1394 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001395 final Phone phone = getPhone(subId);
1396 if (phone != null) {
1397 phone.updateServiceLocation();
1398 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 }
1400
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001401 @Override
1402 public boolean isRadioOn(String callingPackage) {
1403 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001404 }
1405
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001406 @Override
1407 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1408 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1409 return false;
1410 }
1411 return isRadioOnForSubscriber(subId);
1412 }
1413
1414 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001415 final Phone phone = getPhone(subId);
1416 if (phone != null) {
1417 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1418 } else {
1419 return false;
1420 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001421 }
1422
1423 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001424 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001425
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001426 }
Wink Saville36469e72014-06-11 15:17:00 -07001427
Wink Savilleb564aae2014-10-23 10:18:09 -07001428 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001429 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001430 final Phone phone = getPhone(subId);
1431 if (phone != null) {
1432 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1433 }
Wink Saville36469e72014-06-11 15:17:00 -07001434 }
1435
1436 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001437 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001438 }
1439
Wink Savilleb564aae2014-10-23 10:18:09 -07001440 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001441 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001442 final Phone phone = getPhone(subId);
1443 if (phone == null) {
1444 return false;
1445 }
1446 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001447 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001448 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001449 }
1450 return true;
1451 }
Wink Saville36469e72014-06-11 15:17:00 -07001452
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001453 public boolean needMobileRadioShutdown() {
1454 /*
1455 * If any of the Radios are available, it will need to be
1456 * shutdown. So return true if any Radio is available.
1457 */
1458 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1459 Phone phone = PhoneFactory.getPhone(i);
1460 if (phone != null && phone.isRadioAvailable()) return true;
1461 }
1462 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1463 return false;
1464 }
1465
1466 public void shutdownMobileRadios() {
1467 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1468 logv("Shutting down Phone " + i);
1469 shutdownRadioUsingPhoneId(i);
1470 }
1471 }
1472
1473 private void shutdownRadioUsingPhoneId(int phoneId) {
1474 enforceModifyPermission();
1475 Phone phone = PhoneFactory.getPhone(phoneId);
1476 if (phone != null && phone.isRadioAvailable()) {
1477 phone.shutdownRadio();
1478 }
1479 }
1480
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001481 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001482 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1483 if (defaultPhone != null) {
1484 defaultPhone.setRadioPower(turnOn);
1485 return true;
1486 } else {
1487 loge("There's no default phone.");
1488 return false;
1489 }
Wink Saville36469e72014-06-11 15:17:00 -07001490 }
1491
Wink Savilleb564aae2014-10-23 10:18:09 -07001492 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001493 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001494 final Phone phone = getPhone(subId);
1495 if (phone != null) {
1496 phone.setRadioPower(turnOn);
1497 return true;
1498 } else {
1499 return false;
1500 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001501 }
1502
Wink Saville36469e72014-06-11 15:17:00 -07001503 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001504 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001505 public boolean enableDataConnectivity() {
1506 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001507 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001508 final Phone phone = getPhone(subId);
1509 if (phone != null) {
1510 phone.setDataEnabled(true);
1511 return true;
1512 } else {
1513 return false;
1514 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001515 }
1516
Wink Saville36469e72014-06-11 15:17:00 -07001517 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001518 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001519 public boolean disableDataConnectivity() {
1520 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001521 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001522 final Phone phone = getPhone(subId);
1523 if (phone != null) {
1524 phone.setDataEnabled(false);
1525 return true;
1526 } else {
1527 return false;
1528 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001529 }
1530
Wink Saville36469e72014-06-11 15:17:00 -07001531 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001532 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001533 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001534 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001535 final Phone phone = getPhone(subId);
1536 if (phone != null) {
1537 return phone.isDataConnectivityPossible();
1538 } else {
1539 return false;
1540 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001541 }
1542
1543 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001544 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001545 }
1546
pkanwarf8520782016-11-06 20:37:09 -08001547 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar7e856482016-10-14 19:37:38 -07001548 enforceCallPermission();
pkanwar7e856482016-10-14 19:37:38 -07001549 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1550 return;
1551 }
1552 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1553 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1554 };
1555
Wink Savilleb564aae2014-10-23 10:18:09 -07001556 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001557 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001558 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1559 return false;
1560 }
Wink Saville36469e72014-06-11 15:17:00 -07001561 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001562 }
1563
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001564 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001565 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001566 }
1567
Sanket Padawe5780e442017-03-20 15:04:47 -07001568 public int getCallStateForSlot(int slotIndex) {
1569 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001570 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1571 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001572 }
1573
Sanket Padawe356d7632015-06-22 14:03:32 -07001574 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001575 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001576 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001577 if (phone != null) {
1578 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1579 } else {
1580 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1581 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001582 }
1583
Sanket Padawe356d7632015-06-22 14:03:32 -07001584 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001585 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001586 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001587 if (phone != null) {
1588 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1589 } else {
1590 return TelephonyManager.DATA_ACTIVITY_NONE;
1591 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001592 }
1593
1594 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001595 public Bundle getCellLocation(String callingPackage) {
1596 enforceFineOrCoarseLocationPermission("getCellLocation");
1597
1598 // OP_COARSE_LOCATION controls both fine and coarse location.
1599 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1600 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001601 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001602 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 }
1604
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001605 if (checkIfCallerIsSelfOrForegroundUser() ||
1606 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001607 if (DBG_LOC) log("getCellLocation: is active user");
1608 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001609 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001610 if (phone == null) {
1611 return null;
1612 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07001613
1614 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1615 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001616 return data;
1617 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001618 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001619 return null;
1620 }
1621 }
1622
Svetoslav64fad262015-04-14 14:35:21 -07001623 private void enforceFineOrCoarseLocationPermission(String message) {
1624 try {
1625 mApp.enforceCallingOrSelfPermission(
1626 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1627 } catch (SecurityException e) {
1628 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1629 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1630 // is the weaker precondition
1631 mApp.enforceCallingOrSelfPermission(
1632 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1633 }
1634 }
1635
1636
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001637 @Override
1638 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001639 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001640 }
1641
Sanket Padawe356d7632015-06-22 14:03:32 -07001642 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001643 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001644 mApp.enforceCallingOrSelfPermission(
1645 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001646 final Phone phone = getPhone(subId);
1647 if (phone != null) {
1648 phone.enableLocationUpdates();
1649 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001650 }
1651
1652 @Override
1653 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001654 disableLocationUpdatesForSubscriber(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 disableLocationUpdatesForSubscriber(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.disableLocationUpdates();
1664 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001665 }
1666
1667 @Override
1668 @SuppressWarnings("unchecked")
1669 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001670 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1671
1672 // OP_COARSE_LOCATION controls both fine and coarse location.
1673 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1674 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1675 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001676 }
1677
1678 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1679 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1680 return null;
1681 }
Svetoslav64fad262015-04-14 14:35:21 -07001682
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001683 if (checkIfCallerIsSelfOrForegroundUser() ||
1684 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001685 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1686
1687 ArrayList<NeighboringCellInfo> cells = null;
1688
Sooraj Sasindran22882212016-07-18 11:57:25 -07001689 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001690 try {
1691 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran22882212016-07-18 11:57:25 -07001692 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001693 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001694 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001695 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001696 }
1697 return cells;
1698 } else {
1699 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1700 return null;
1701 }
1702 }
1703
1704
1705 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001706 public List<CellInfo> getAllCellInfo(String callingPackage) {
1707 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1708
1709 // OP_COARSE_LOCATION controls both fine and coarse location.
1710 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1711 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1712 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001713 }
1714
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001715 if (checkIfCallerIsSelfOrForegroundUser() ||
1716 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001717 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran22882212016-07-18 11:57:25 -07001718 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001719 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1720 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran22882212016-07-18 11:57:25 -07001721 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1722 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001723 }
1724 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001725 } else {
1726 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1727 return null;
1728 }
1729 }
1730
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001731 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001732 public void setCellInfoListRate(int rateInMillis) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001733 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001734 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1735 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001736 }
1737
Shishir Agrawala9f32182016-04-12 12:00:16 -07001738 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001739 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001740 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1741 return null;
1742 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001743 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001744 return phone == null ? null : phone.getImei();
1745 }
1746
1747 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001748 public String getMeidForSlot(int slotIndex, String callingPackage) {
1749 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1750 return null;
1751 }
1752 Phone phone = PhoneFactory.getPhone(slotIndex);
1753 return phone == null ? null : phone.getMeid();
1754 }
1755
1756 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001757 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001758 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1759 return null;
1760 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001761 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001762 return phone == null ? null : phone.getDeviceSvn();
1763 }
1764
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001765 //
1766 // Internal helper methods.
1767 //
1768
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001769 /**
1770 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1771 */
1772 private boolean checkCallerInteractAcrossUsersFull() {
1773 return mPhone.getContext().checkCallingOrSelfPermission(
1774 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1775 == PackageManager.PERMISSION_GRANTED;
1776 }
1777
Jake Hambye994d462014-02-03 13:10:13 -08001778 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001779 boolean ok;
1780
1781 boolean self = Binder.getCallingUid() == Process.myUid();
1782 if (!self) {
1783 // Get the caller's user id then clear the calling identity
1784 // which will be restored in the finally clause.
1785 int callingUser = UserHandle.getCallingUserId();
1786 long ident = Binder.clearCallingIdentity();
1787
1788 try {
1789 // With calling identity cleared the current user is the foreground user.
1790 int foregroundUser = ActivityManager.getCurrentUser();
1791 ok = (foregroundUser == callingUser);
1792 if (DBG_LOC) {
1793 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1794 + " callingUser=" + callingUser + " ok=" + ok);
1795 }
1796 } catch (Exception ex) {
1797 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1798 ok = false;
1799 } finally {
1800 Binder.restoreCallingIdentity(ident);
1801 }
1802 } else {
1803 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1804 ok = true;
1805 }
1806 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1807 return ok;
1808 }
1809
1810 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001811 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1812 *
1813 * @throws SecurityException if the caller does not have the required permission
1814 */
1815 private void enforceModifyPermission() {
1816 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1817 }
1818
1819 /**
Junda Liua2e36012014-07-09 18:30:01 -07001820 * Make sure either system app or the caller has carrier privilege.
1821 *
1822 * @throws SecurityException if the caller does not have the required permission/privilege
1823 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001824 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001825 int permission = mApp.checkCallingOrSelfPermission(
1826 android.Manifest.permission.MODIFY_PHONE_STATE);
1827 if (permission == PackageManager.PERMISSION_GRANTED) {
1828 return;
1829 }
1830
1831 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001832 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001833 }
1834
1835 /**
1836 * Make sure the caller has carrier privilege.
1837 *
1838 * @throws SecurityException if the caller does not have the required permission
1839 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001840 private void enforceCarrierPrivilege(int subId) {
1841 if (getCarrierPrivilegeStatus(subId) !=
1842 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001843 loge("No Carrier Privilege.");
1844 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001845 }
1846 }
1847
1848 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001849 * Make sure the caller has the CALL_PHONE permission.
1850 *
1851 * @throws SecurityException if the caller does not have the required permission
1852 */
1853 private void enforceCallPermission() {
1854 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1855 }
1856
Stuart Scott8eef64f2015-04-08 15:13:54 -07001857 private void enforceConnectivityInternalPermission() {
1858 mApp.enforceCallingOrSelfPermission(
1859 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1860 "ConnectivityService");
1861 }
1862
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001863 private String createTelUrl(String number) {
1864 if (TextUtils.isEmpty(number)) {
1865 return null;
1866 }
1867
Jake Hambye994d462014-02-03 13:10:13 -08001868 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001869 }
1870
Ihab Awadf9e92732013-12-05 18:02:52 -08001871 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001872 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1873 }
1874
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001875 private static void logv(String msg) {
1876 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1877 }
1878
Ihab Awadf9e92732013-12-05 18:02:52 -08001879 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001880 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1881 }
1882
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001883 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001884 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001885 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001886 }
1887
Sanket Padawe356d7632015-06-22 14:03:32 -07001888 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001889 public int getActivePhoneTypeForSlot(int slotIndex) {
1890 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001891 if (phone == null) {
1892 return PhoneConstants.PHONE_TYPE_NONE;
1893 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001894 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001895 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001896 }
1897
1898 /**
1899 * Returns the CDMA ERI icon index to display
1900 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001901 @Override
1902 public int getCdmaEriIconIndex(String callingPackage) {
1903 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001904 }
1905
Sanket Padawe356d7632015-06-22 14:03:32 -07001906 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001907 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1908 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1909 return -1;
1910 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001911 final Phone phone = getPhone(subId);
1912 if (phone != null) {
1913 return phone.getCdmaEriIconIndex();
1914 } else {
1915 return -1;
1916 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001917 }
1918
1919 /**
1920 * Returns the CDMA ERI icon mode,
1921 * 0 - ON
1922 * 1 - FLASHING
1923 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001924 @Override
1925 public int getCdmaEriIconMode(String callingPackage) {
1926 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001927 }
1928
Sanket Padawe356d7632015-06-22 14:03:32 -07001929 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001930 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1931 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1932 return -1;
1933 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001934 final Phone phone = getPhone(subId);
1935 if (phone != null) {
1936 return phone.getCdmaEriIconMode();
1937 } else {
1938 return -1;
1939 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001940 }
1941
1942 /**
1943 * Returns the CDMA ERI text,
1944 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001945 @Override
1946 public String getCdmaEriText(String callingPackage) {
1947 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001948 }
1949
Sanket Padawe356d7632015-06-22 14:03:32 -07001950 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001951 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1952 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1953 return null;
1954 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001955 final Phone phone = getPhone(subId);
1956 if (phone != null) {
1957 return phone.getCdmaEriText();
1958 } else {
1959 return null;
1960 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001961 }
1962
1963 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001964 * Returns the CDMA MDN.
1965 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001966 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001967 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001968 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001969 final Phone phone = getPhone(subId);
1970 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1971 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001972 } else {
1973 return null;
1974 }
1975 }
1976
1977 /**
1978 * Returns the CDMA MIN.
1979 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001980 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001981 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001982 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001983 final Phone phone = getPhone(subId);
1984 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1985 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001986 } else {
1987 return null;
1988 }
1989 }
1990
1991 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001992 * Returns true if CDMA provisioning needs to run.
1993 */
1994 public boolean needsOtaServiceProvisioning() {
1995 return mPhone.needsOtaServiceProvisioning();
1996 }
1997
1998 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001999 * Sets the voice mail number of a given subId.
2000 */
2001 @Override
2002 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002003 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002004 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2005 new Pair<String, String>(alphaTag, number), new Integer(subId));
2006 return success;
2007 }
2008
Ta-wei Yen87c49842016-05-13 21:19:52 -07002009 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07002010 public void setVisualVoicemailEnabled(String callingPackage,
2011 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
2012 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2013 if (!TextUtils.equals(callingPackage,
2014 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2015 enforceModifyPermissionOrCarrierPrivilege(
2016 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
2017 }
2018 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2019 }
2020
2021 @Override
2022 public boolean isVisualVoicemailEnabled(String callingPackage,
2023 PhoneAccountHandle phoneAccountHandle) {
2024 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2025 return false;
2026 }
2027 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2028 }
2029
2030 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002031 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2032 VisualVoicemailSmsFilterSettings settings) {
2033 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002034 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002035 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2036 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002037 }
2038
2039 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002040 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2041 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002042 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002043 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002044 }
2045
2046 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002047 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2048 String callingPackage, int subId) {
2049 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002050 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002051 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002052 }
2053
2054 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002055 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
2056 String packageName, int subId) {
2057 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002058 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002059 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002060 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002061 /**
fionaxuef039d42016-11-14 13:36:14 -08002062 * Sets the voice activation state of a given subId.
2063 */
2064 @Override
2065 public void setVoiceActivationState(int subId, int activationState) {
2066 enforceModifyPermissionOrCarrierPrivilege(subId);
2067 final Phone phone = getPhone(subId);
2068 if (phone != null) {
2069 phone.setVoiceActivationState(activationState);
2070 } else {
2071 loge("setVoiceActivationState fails with invalid subId: " + subId);
2072 }
2073 }
2074
2075 /**
2076 * Sets the data activation state of a given subId.
2077 */
2078 @Override
2079 public void setDataActivationState(int subId, int activationState) {
2080 enforceModifyPermissionOrCarrierPrivilege(subId);
2081 final Phone phone = getPhone(subId);
2082 if (phone != null) {
2083 phone.setDataActivationState(activationState);
2084 } else {
2085 loge("setVoiceActivationState fails with invalid subId: " + subId);
2086 }
2087 }
2088
2089 /**
2090 * Returns the voice activation state of a given subId.
2091 */
2092 @Override
2093 public int getVoiceActivationState(int subId, String callingPackage) {
2094 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2095 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2096 }
2097 final Phone phone = getPhone(subId);
2098 if (phone != null) {
2099 return phone.getVoiceActivationState();
2100 } else {
2101 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2102 }
2103 }
2104
2105 /**
2106 * Returns the data activation state of a given subId.
2107 */
2108 @Override
2109 public int getDataActivationState(int subId, String callingPackage) {
2110 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2111 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2112 }
2113 final Phone phone = getPhone(subId);
2114 if (phone != null) {
2115 return phone.getDataActivationState();
2116 } else {
2117 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2118 }
2119 }
2120
2121 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002122 * Returns the unread count of voicemails
2123 */
2124 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002125 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002126 }
2127
2128 /**
2129 * Returns the unread count of voicemails for a subId
2130 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002131 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002132 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002133 final Phone phone = getPhone(subId);
2134 if (phone != null) {
2135 return phone.getVoiceMessageCount();
2136 } else {
2137 return 0;
2138 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002139 }
2140
2141 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002142 * Returns the data network type.
2143 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002144 *
2145 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2146 */
2147 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002148 public int getNetworkType() {
2149 final Phone phone = getPhone(getDefaultSubscription());
2150 if (phone != null) {
2151 return phone.getServiceState().getDataNetworkType();
2152 } else {
2153 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2154 }
Wink Saville36469e72014-06-11 15:17:00 -07002155 }
2156
2157 /**
2158 * Returns the network type for a subId
2159 */
2160 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002161 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2162 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2163 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2164 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002165
Sanket Padawe356d7632015-06-22 14:03:32 -07002166 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002167 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002168 return phone.getServiceState().getDataNetworkType();
2169 } else {
2170 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2171 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002172 }
2173
2174 /**
2175 * Returns the data network type
2176 */
2177 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002178 public int getDataNetworkType(String callingPackage) {
2179 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002180 }
2181
2182 /**
2183 * Returns the data network type for a subId
2184 */
2185 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002186 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2187 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2188 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2189 }
2190
Sanket Padawe356d7632015-06-22 14:03:32 -07002191 final Phone phone = getPhone(subId);
2192 if (phone != null) {
2193 return phone.getServiceState().getDataNetworkType();
2194 } else {
2195 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2196 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002197 }
2198
2199 /**
Wink Saville36469e72014-06-11 15:17:00 -07002200 * Returns the Voice network type for a subId
2201 */
2202 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002203 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2204 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2205 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2206 }
2207
Sanket Padawe356d7632015-06-22 14:03:32 -07002208 final Phone phone = getPhone(subId);
2209 if (phone != null) {
2210 return phone.getServiceState().getVoiceNetworkType();
2211 } else {
2212 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2213 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002214 }
2215
2216 /**
2217 * @return true if a ICC card is present
2218 */
2219 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002220 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002221 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2222 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002223 }
2224
2225 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002226 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002227 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002228 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002229 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2230 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002231 final Phone phone = getPhone(subId[0]);
2232 if (subId != null && phone != null) {
2233 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002234 } else {
2235 return false;
2236 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002237 }
2238
2239 /**
2240 * Return if the current radio is LTE on CDMA. This
2241 * is a tri-state return value as for a period of time
2242 * the mode may be unknown.
2243 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002244 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002245 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002246 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002247 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002248 @Override
2249 public int getLteOnCdmaMode(String callingPackage) {
2250 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002251 }
2252
Sanket Padawe356d7632015-06-22 14:03:32 -07002253 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002254 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2255 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2256 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2257 }
2258
Sanket Padawe356d7632015-06-22 14:03:32 -07002259 final Phone phone = getPhone(subId);
2260 if (phone == null) {
2261 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2262 } else {
2263 return phone.getLteOnCdmaMode();
2264 }
Wink Saville36469e72014-06-11 15:17:00 -07002265 }
2266
2267 public void setPhone(Phone phone) {
2268 mPhone = phone;
2269 }
2270
2271 /**
2272 * {@hide}
2273 * Returns Default subId, 0 in the case of single standby.
2274 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002275 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002276 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002277 }
2278
Shishir Agrawala9f32182016-04-12 12:00:16 -07002279 private int getSlotForDefaultSubscription() {
2280 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2281 }
2282
Wink Savilleb564aae2014-10-23 10:18:09 -07002283 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002284 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002285 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002286
2287 /**
2288 * @see android.telephony.TelephonyManager.WifiCallingChoices
2289 */
2290 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002291 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2292 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002293 }
2294
2295 /**
2296 * @see android.telephony.TelephonyManager.WifiCallingChoices
2297 */
2298 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002299 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2300 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2301 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002302 }
2303
Sailesh Nepald1e68152013-12-12 19:08:02 -08002304 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002305 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002306 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002307 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002308
Shishir Agrawal566b7612013-10-28 14:41:00 -07002309 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002310 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002311 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002312
Ajay Nambiabd73502015-12-03 13:50:00 -08002313 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002314 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002315 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002316 if (DBG) log("iccOpenLogicalChannel: " + response);
2317 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002318 }
2319
2320 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002321 public boolean iccCloseLogicalChannel(int subId, int channel) {
2322 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002323
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002324 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002325 if (channel < 0) {
2326 return false;
2327 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002328 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002329 if (DBG) log("iccCloseLogicalChannel: " + success);
2330 return success;
2331 }
2332
2333 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002334 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002335 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002336 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002337
2338 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002339 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2340 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002341 " data=" + data);
2342 }
2343
2344 if (channel < 0) {
2345 return "";
2346 }
2347
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002348 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002349 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002350 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2351
Shishir Agrawal566b7612013-10-28 14:41:00 -07002352 // Append the returned status code to the end of the response payload.
2353 String s = Integer.toHexString(
2354 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002355 if (response.payload != null) {
2356 s = IccUtils.bytesToHexString(response.payload) + s;
2357 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002358 return s;
2359 }
Jake Hambye994d462014-02-03 13:10:13 -08002360
Evan Charltonc66da362014-05-16 14:06:40 -07002361 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002362 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002363 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002364 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002365
2366 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002367 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2368 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002369 }
2370
2371 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002372 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002373 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2374
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002375 // Append the returned status code to the end of the response payload.
2376 String s = Integer.toHexString(
2377 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002378 if (response.payload != null) {
2379 s = IccUtils.bytesToHexString(response.payload) + s;
2380 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002381 return s;
2382 }
2383
2384 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002385 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002386 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002387 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002388
2389 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002390 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002391 p1 + " " + p2 + " " + p3 + ":" + filePath);
2392 }
2393
2394 IccIoResult response =
2395 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002396 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2397 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002398
2399 if (DBG) {
2400 log("Exchange SIM_IO [R]" + response);
2401 }
2402
2403 byte[] result = null;
2404 int length = 2;
2405 if (response.payload != null) {
2406 length = 2 + response.payload.length;
2407 result = new byte[length];
2408 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2409 } else {
2410 result = new byte[length];
2411 }
2412
2413 result[length - 1] = (byte) response.sw2;
2414 result[length - 2] = (byte) response.sw1;
2415 return result;
2416 }
2417
Nathan Haroldb3014052017-01-25 15:57:32 -08002418 /**
2419 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2420 * on a particular subscription
2421 */
2422 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002423 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2424 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002425 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2426 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2427 return null;
2428 }
2429 Object response = sendRequest(
2430 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2431 if (response instanceof String[]) {
2432 return (String[]) response;
2433 }
2434 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2435 return null;
2436 }
2437
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002438 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002439 public String sendEnvelopeWithStatus(int subId, String content) {
2440 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002441
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002442 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002443 if (response.payload == null) {
2444 return "";
2445 }
2446
2447 // Append the returned status code to the end of the response payload.
2448 String s = Integer.toHexString(
2449 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2450 s = IccUtils.bytesToHexString(response.payload) + s;
2451 return s;
2452 }
2453
Jake Hambye994d462014-02-03 13:10:13 -08002454 /**
2455 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2456 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2457 *
2458 * @param itemID the ID of the item to read
2459 * @return the NV item as a String, or null on error.
2460 */
2461 @Override
2462 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002463 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002464 if (DBG) log("nvReadItem: item " + itemID);
2465 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2466 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2467 return value;
2468 }
2469
2470 /**
2471 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2472 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2473 *
2474 * @param itemID the ID of the item to read
2475 * @param itemValue the value to write, as a String
2476 * @return true on success; false on any failure
2477 */
2478 @Override
2479 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002480 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002481 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2482 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2483 new Pair<Integer, String>(itemID, itemValue));
2484 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2485 return success;
2486 }
2487
2488 /**
2489 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2490 * Used for device configuration by some CDMA operators.
2491 *
2492 * @param preferredRoamingList byte array containing the new PRL
2493 * @return true on success; false on any failure
2494 */
2495 @Override
2496 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002497 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002498 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2499 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2500 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2501 return success;
2502 }
2503
2504 /**
2505 * Perform the specified type of NV config reset.
2506 * Used for device configuration by some CDMA operators.
2507 *
2508 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2509 * @return true on success; false on any failure
2510 */
2511 @Override
2512 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002513 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002514 if (DBG) log("nvResetConfig: type " + resetType);
2515 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2516 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2517 return success;
2518 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002519
2520 /**
Wink Saville36469e72014-06-11 15:17:00 -07002521 * {@hide}
2522 * Returns Default sim, 0 in the case of single standby.
2523 */
2524 public int getDefaultSim() {
2525 //TODO Need to get it from Telephony Devcontroller
2526 return 0;
2527 }
2528
Svet Ganovb320e182015-04-16 12:30:10 -07002529 public String[] getPcscfAddress(String apnType, String callingPackage) {
2530 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2531 return new String[0];
2532 }
2533
2534
ram87fca6f2014-07-18 18:58:44 +05302535 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002536 }
2537
Brad Ebinger76681002017-01-23 13:50:20 -08002538 /**
2539 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2540 * feature or {@link null} if the service is not available. If an ImsServiceController is
2541 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2542 * feature updates.
2543 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002544 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002545 IImsServiceFeatureListener callback) {
2546 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002547 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002548 callback);
2549 }
2550
Wink Saville36469e72014-06-11 15:17:00 -07002551 public void setImsRegistrationState(boolean registered) {
2552 enforceModifyPermission();
2553 mPhone.setImsRegistrationState(registered);
2554 }
2555
2556 /**
Stuart Scott54788802015-03-30 13:18:01 -07002557 * Set the network selection mode to automatic.
2558 *
2559 */
2560 @Override
2561 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002562 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002563 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2564 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2565 }
2566
2567 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002568 * Set the network selection mode to manual with the selected carrier.
2569 */
2570 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002571 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2572 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002573 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002574 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002575 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2576 persistSelection);
2577 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002578 }
2579
2580 /**
2581 * Scans for available networks.
2582 */
2583 @Override
2584 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002585 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002586 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2587 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2588 CMD_PERFORM_NETWORK_SCAN, null, subId);
2589 return result;
2590 }
2591
2592 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002593 * Get the calculated preferred network type.
2594 * Used for debugging incorrect network type.
2595 *
2596 * @return the preferred network type, defined in RILConstants.java.
2597 */
2598 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002599 public int getCalculatedPreferredNetworkType(String callingPackage) {
2600 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2601 return RILConstants.PREFERRED_NETWORK_MODE;
2602 }
2603
Amit Mahajan43330e02014-11-18 11:54:45 -08002604 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002605 }
2606
2607 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002608 * Get the preferred network type.
2609 * Used for device configuration by some CDMA operators.
2610 *
2611 * @return the preferred network type, defined in RILConstants.java.
2612 */
2613 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002614 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002615 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002616 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002617 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002618 int networkType = (result != null ? result[0] : -1);
2619 if (DBG) log("getPreferredNetworkType: " + networkType);
2620 return networkType;
2621 }
2622
2623 /**
2624 * Set the preferred network type.
2625 * Used for device configuration by some CDMA operators.
2626 *
2627 * @param networkType the preferred network type, defined in RILConstants.java.
2628 * @return true on success; false on any failure.
2629 */
2630 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002631 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002632 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002633 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2634 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002635 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002636 if (success) {
2637 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002638 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002639 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002640 return success;
2641 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002642
2643 /**
Junda Liu475951f2014-11-07 16:45:03 -08002644 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2645 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2646 * tethering.
2647 *
2648 * @return 0: Not required. 1: required. 2: Not set.
2649 * @hide
2650 */
2651 @Override
2652 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002653 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002654 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2655 Settings.Global.TETHER_DUN_REQUIRED, 2);
2656 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2657 // config_tether_apndata.
2658 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2659 dunRequired = 1;
2660 }
2661 return dunRequired;
2662 }
2663
2664 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002665 * Set mobile data enabled
2666 * Used by the user through settings etc to turn on/off mobile data
2667 *
2668 * @param enable {@code true} turn turn data on, else {@code false}
2669 */
2670 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002671 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002672 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002673 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002674 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002675 Phone phone = PhoneFactory.getPhone(phoneId);
2676 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002677 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002678 phone.setDataEnabled(enable);
2679 } else {
2680 loge("setDataEnabled: no phone for subId=" + subId);
2681 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002682 }
2683
2684 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002685 * Get whether mobile data is enabled.
2686 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002687 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002688 *
2689 * @return {@code true} if data is enabled else {@code false}
2690 */
2691 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002692 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002693 try {
2694 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2695 null);
2696 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002697 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002698 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002699 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002700 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002701 Phone phone = PhoneFactory.getPhone(phoneId);
2702 if (phone != null) {
2703 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002704 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002705 return retVal;
2706 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002707 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002708 return false;
2709 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002710 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002711
2712 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002713 public int getCarrierPrivilegeStatus(int subId) {
2714 final Phone phone = getPhone(subId);
2715 if (phone == null) {
2716 loge("getCarrierPrivilegeStatus: Invalid subId");
2717 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2718 }
2719 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002720 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002721 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002722 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2723 }
2724 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002725 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002726 }
Junda Liu29340342014-07-10 15:23:27 -07002727
2728 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002729 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002730 if (TextUtils.isEmpty(pkgName))
2731 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002732 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002733 if (card == null) {
2734 loge("checkCarrierPrivilegesForPackage: No UICC");
2735 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2736 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002737 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2738 }
2739
2740 @Override
2741 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002742 if (TextUtils.isEmpty(pkgName))
2743 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002744 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2745 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2746 UiccCard card = UiccController.getInstance().getUiccCard(i);
2747 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002748 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002749 continue;
2750 }
2751
2752 result = card.getCarrierPrivilegeStatus(
2753 mPhone.getContext().getPackageManager(), pkgName);
2754 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2755 break;
2756 }
2757 }
2758
2759 return result;
Junda Liu29340342014-07-10 15:23:27 -07002760 }
Derek Tan89e89d42014-07-08 17:00:10 -07002761
2762 @Override
Junda Liue64de782015-04-16 17:19:16 -07002763 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2764 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2765 loge("phoneId " + phoneId + " is not valid.");
2766 return null;
2767 }
2768 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002769 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002770 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002771 return null ;
2772 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002773 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002774 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002775 }
2776
Amith Yamasani6e118872016-02-19 12:53:51 -08002777 @Override
2778 public List<String> getPackagesWithCarrierPrivileges() {
2779 PackageManager pm = mPhone.getContext().getPackageManager();
2780 List<String> privilegedPackages = new ArrayList<>();
2781 List<PackageInfo> packages = null;
2782 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2783 UiccCard card = UiccController.getInstance().getUiccCard(i);
2784 if (card == null) {
2785 // No UICC in that slot.
2786 continue;
2787 }
2788 if (card.hasCarrierPrivilegeRules()) {
2789 if (packages == null) {
2790 // Only check packages in user 0 for now
2791 packages = pm.getInstalledPackagesAsUser(
2792 PackageManager.MATCH_DISABLED_COMPONENTS
2793 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2794 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2795 }
2796 for (int p = packages.size() - 1; p >= 0; p--) {
2797 PackageInfo pkgInfo = packages.get(p);
2798 if (pkgInfo != null && pkgInfo.packageName != null
2799 && card.getCarrierPrivilegeStatus(pkgInfo)
2800 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2801 privilegedPackages.add(pkgInfo.packageName);
2802 }
2803 }
2804 }
2805 }
2806 return privilegedPackages;
2807 }
2808
Wink Savilleb564aae2014-10-23 10:18:09 -07002809 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002810 final Phone phone = getPhone(subId);
2811 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002812 if (card == null) {
2813 loge("getIccId: No UICC");
2814 return null;
2815 }
2816 String iccId = card.getIccId();
2817 if (TextUtils.isEmpty(iccId)) {
2818 loge("getIccId: ICC ID is null or empty.");
2819 return null;
2820 }
2821 return iccId;
2822 }
2823
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002824 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002825 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2826 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002827 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002828
Jeff Sharkey85190e62014-12-05 09:40:12 -08002829 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002830 final Phone phone = getPhone(subId);
2831 if (phone == null) {
2832 return false;
2833 }
2834 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002835
2836 if (DBG_MERGE) {
2837 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2838 + subscriberId + " to " + number);
2839 }
2840
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002841 if (TextUtils.isEmpty(iccId)) {
2842 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002843 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002844
Jeff Sharkey85190e62014-12-05 09:40:12 -08002845 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2846
2847 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002848 if (alphaTag == null) {
2849 editor.remove(alphaTagPrefKey);
2850 } else {
2851 editor.putString(alphaTagPrefKey, alphaTag);
2852 }
2853
Jeff Sharkey85190e62014-12-05 09:40:12 -08002854 // Record both the line number and IMSI for this ICCID, since we need to
2855 // track all merged IMSIs based on line number
2856 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2857 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002858 if (number == null) {
2859 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002860 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002861 } else {
2862 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002863 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002864 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002865
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002866 editor.commit();
2867 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002868 }
2869
2870 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002871 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002872 // This is open to apps with WRITE_SMS.
2873 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002874 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002875 return null;
2876 }
Derek Tan97ebb422014-09-05 16:55:38 -07002877
2878 String iccId = getIccId(subId);
2879 if (iccId != null) {
2880 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002881 if (DBG_MERGE) {
2882 log("getLine1NumberForDisplay returning " +
2883 mTelephonySharedPreferences.getString(numberPrefKey, null));
2884 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002885 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002886 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002887 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002888 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002889 }
2890
2891 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002892 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2893 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2894 return null;
2895 }
Derek Tan97ebb422014-09-05 16:55:38 -07002896
2897 String iccId = getIccId(subId);
2898 if (iccId != null) {
2899 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002900 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002901 }
Derek Tan97ebb422014-09-05 16:55:38 -07002902 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002903 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002904
2905 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002906 public String[] getMergedSubscriberIds(String callingPackage) {
2907 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2908 return null;
2909 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002910 final Context context = mPhone.getContext();
2911 final TelephonyManager tele = TelephonyManager.from(context);
2912 final SubscriptionManager sub = SubscriptionManager.from(context);
2913
2914 // Figure out what subscribers are currently active
2915 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002916 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2917 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2918 final long identity = Binder.clearCallingIdentity();
2919 try {
2920 final int[] subIds = sub.getActiveSubscriptionIdList();
2921 for (int subId : subIds) {
2922 activeSubscriberIds.add(tele.getSubscriberId(subId));
2923 }
2924 } finally {
2925 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002926 }
2927
2928 // First pass, find a number override for an active subscriber
2929 String mergeNumber = null;
2930 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2931 for (String key : prefs.keySet()) {
2932 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2933 final String subscriberId = (String) prefs.get(key);
2934 if (activeSubscriberIds.contains(subscriberId)) {
2935 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2936 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2937 mergeNumber = (String) prefs.get(numberKey);
2938 if (DBG_MERGE) {
2939 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2940 + " for active subscriber " + subscriberId);
2941 }
2942 if (!TextUtils.isEmpty(mergeNumber)) {
2943 break;
2944 }
2945 }
2946 }
2947 }
2948
2949 // Shortcut when no active merged subscribers
2950 if (TextUtils.isEmpty(mergeNumber)) {
2951 return null;
2952 }
2953
2954 // Second pass, find all subscribers under that line override
2955 final ArraySet<String> result = new ArraySet<>();
2956 for (String key : prefs.keySet()) {
2957 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2958 final String number = (String) prefs.get(key);
2959 if (mergeNumber.equals(number)) {
2960 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2961 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2962 final String subscriberId = (String) prefs.get(subscriberKey);
2963 if (!TextUtils.isEmpty(subscriberId)) {
2964 result.add(subscriberId);
2965 }
2966 }
2967 }
2968 }
2969
2970 final String[] resultArray = result.toArray(new String[result.size()]);
2971 Arrays.sort(resultArray);
2972 if (DBG_MERGE) {
2973 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2974 }
2975 return resultArray;
2976 }
2977
2978 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002979 public boolean setOperatorBrandOverride(int subId, String brand) {
2980 enforceCarrierPrivilege(subId);
2981 final Phone phone = getPhone(subId);
2982 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002983 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002984
2985 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002986 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002987 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2988 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002989 enforceCarrierPrivilege(subId);
2990 final Phone phone = getPhone(subId);
2991 if (phone == null) {
2992 return false;
2993 }
2994 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002995 cdmaNonRoamingList);
2996 }
2997
2998 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08002999 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003000 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3001 enforceModifyPermission();
3002
3003 int returnValue = 0;
3004 try {
3005 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3006 if(result.exception == null) {
3007 if (result.result != null) {
3008 byte[] responseData = (byte[])(result.result);
3009 if(responseData.length > oemResp.length) {
3010 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3011 responseData.length + "bytes. Buffer Size is " +
3012 oemResp.length + "bytes.");
3013 }
3014 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3015 returnValue = responseData.length;
3016 }
3017 } else {
3018 CommandException ex = (CommandException) result.exception;
3019 returnValue = ex.getCommandError().ordinal();
3020 if(returnValue > 0) returnValue *= -1;
3021 }
3022 } catch (RuntimeException e) {
3023 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3024 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3025 if(returnValue > 0) returnValue *= -1;
3026 }
3027
3028 return returnValue;
3029 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003030
3031 @Override
3032 public void setRadioCapability(RadioAccessFamily[] rafs) {
3033 try {
3034 ProxyController.getInstance().setRadioCapability(rafs);
3035 } catch (RuntimeException e) {
3036 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3037 }
3038 }
3039
3040 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003041 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3042 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3043 return RadioAccessFamily.RAF_UNKNOWN;
3044 }
3045
Wink Saville5d475dd2014-10-17 15:00:58 -07003046 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3047 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003048
3049 @Override
3050 public void enableVideoCalling(boolean enable) {
3051 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003052 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003053 }
3054
3055 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003056 public boolean isVideoCallingEnabled(String callingPackage) {
3057 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3058 return false;
3059 }
3060
Andrew Lee77527ac2014-10-21 16:57:39 -07003061 // Check the user preference and the system-level IMS setting. Even if the user has
3062 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3063 // In the long run, we may instead need to check if there exists a connection service
3064 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003065 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3066 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003067 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003068 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003069
Andrew Leea1239f22015-03-02 17:44:07 -08003070 @Override
3071 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003072 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003073 }
3074
3075 @Override
3076 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003077 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003078 }
3079
Andrew Lee9431b832015-03-09 18:46:45 -07003080 @Override
3081 public boolean isTtyModeSupported() {
3082 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3083 TelephonyManager telephonyManager =
3084 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003085 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003086 }
3087
3088 @Override
3089 public boolean isHearingAidCompatibilitySupported() {
3090 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3091 }
3092
Sanket Padawe7310cc72015-01-14 09:53:20 -08003093 /**
3094 * Returns the unique device ID of phone, for example, the IMEI for
3095 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3096 *
3097 * <p>Requires Permission:
3098 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3099 */
3100 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003101 public String getDeviceId(String callingPackage) {
3102 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3103 return null;
3104 }
3105
Sanket Padawe7310cc72015-01-14 09:53:20 -08003106 final Phone phone = PhoneFactory.getPhone(0);
3107 if (phone != null) {
3108 return phone.getDeviceId();
3109 } else {
3110 return null;
3111 }
3112 }
3113
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003114 /*
3115 * {@hide}
3116 * Returns the IMS Registration Status
3117 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003118 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003119 public boolean isImsRegistered() {
3120 return mPhone.isImsRegistered();
3121 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003122
3123 @Override
3124 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3125 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3126 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003127
Nathan Haroldc55097a2015-03-11 18:14:50 -07003128 /*
3129 * {@hide}
3130 * Returns the IMS Registration Status
3131 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003132 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003133 return mPhone.isWifiCallingEnabled();
3134 }
3135
3136 /*
3137 * {@hide}
3138 * Returns the IMS Registration Status
3139 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003140 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003141 return mPhone.isVolteEnabled();
3142 }
Svet Ganovb320e182015-04-16 12:30:10 -07003143
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003144 /*
3145 * {@hide} Returns the IMS Registration Status
3146 */
3147 public boolean isVideoTelephonyAvailable() {
3148 return mPhone.isVideoEnabled();
3149 }
3150
Svet Ganovb320e182015-04-16 12:30:10 -07003151 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003152 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003153 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003154 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3155
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003156 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003157 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003158 } catch (SecurityException e) {
3159 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3160 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003161 }
Svet Ganovb320e182015-04-16 12:30:10 -07003162
3163 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3164 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3165 return false;
3166 }
3167
3168 return true;
3169 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003170
Makoto Onukifee69342015-06-29 14:44:50 -07003171 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003172 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003173 */
3174 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003175 // Default SMS app can always read it.
3176 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3177 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3178 return true;
3179 }
3180 try {
3181 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003182 } catch (SecurityException readPhoneStateSecurityException) {
3183 try {
3184 // Can be read with READ_SMS too.
3185 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3186 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3187 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3188 } catch (SecurityException readSmsSecurityException) {
3189 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3190 // permissions
3191 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3192 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3193 " or " + android.Manifest.permission.READ_SMS + ".");
3194 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003195 }
Makoto Onukifee69342015-06-29 14:44:50 -07003196 }
3197
Stuart Scott8eef64f2015-04-08 15:13:54 -07003198 @Override
3199 public void factoryReset(int subId) {
3200 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003201 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3202 return;
3203 }
3204
Svet Ganovcc087f82015-05-12 20:35:54 -07003205 final long identity = Binder.clearCallingIdentity();
3206 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003207 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3208 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003209 // Enable data
3210 setDataEnabled(subId, true);
3211 // Set network selection mode to automatic
3212 setNetworkSelectionModeAutomatic(subId);
3213 // Set preferred mobile network type to the best available
3214 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3215 // Turn off roaming
3216 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3217 }
3218 } finally {
3219 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003220 }
3221 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003222
3223 @Override
3224 public String getLocaleFromDefaultSim() {
3225 // We query all subscriptions instead of just the active ones, because
3226 // this might be called early on in the provisioning flow when the
3227 // subscriptions potentially aren't active yet.
3228 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3229 if (slist == null || slist.isEmpty()) {
3230 return null;
3231 }
3232
3233 // This function may be called very early, say, from the setup wizard, at
3234 // which point we won't have a default subscription set. If that's the case
3235 // we just choose the first, which will be valid in "most cases".
3236 final int defaultSubId = getDefaultSubscription();
3237 SubscriptionInfo info = null;
3238 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3239 info = slist.get(0);
3240 } else {
3241 for (SubscriptionInfo item : slist) {
3242 if (item.getSubscriptionId() == defaultSubId) {
3243 info = item;
3244 break;
3245 }
3246 }
3247
3248 if (info == null) {
3249 return null;
3250 }
3251 }
3252
3253 // Try and fetch the locale from the carrier properties or from the SIM language
3254 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003255 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003256 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003257 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003258 if (defaultPhone != null) {
3259 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3260 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003261 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003262 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3263 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003264 } else {
3265 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003266 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003267 }
3268 }
3269
Narayan Kamath011676f2015-07-29 12:04:08 +01003270 // The SIM language preferences only store a language (e.g. fr = French), not an
3271 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3272 // the SIM and carrier preferences does not include a country we add the country
3273 // determined from the SIM MCC to provide an exact locale.
3274 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003275 if (mccLocale != null) {
3276 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3277 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003278 }
3279
Tony Hill183b2de2015-06-24 14:53:58 +01003280 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003281 return null;
3282 }
3283
3284 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3285 final long identity = Binder.clearCallingIdentity();
3286 try {
3287 return mSubscriptionController.getAllSubInfoList(
3288 mPhone.getContext().getOpPackageName());
3289 } finally {
3290 Binder.restoreCallingIdentity(identity);
3291 }
3292 }
3293
3294 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3295 final long identity = Binder.clearCallingIdentity();
3296 try {
3297 return mSubscriptionController.getActiveSubscriptionInfoList(
3298 mPhone.getContext().getOpPackageName());
3299 } finally {
3300 Binder.restoreCallingIdentity(identity);
3301 }
3302 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003303
3304 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003305 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3306 * representing the state of the modem.
3307 *
3308 * NOTE: This clears the modem state, so there should only every be one caller.
3309 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003310 */
3311 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003312 public void requestModemActivityInfo(ResultReceiver result) {
3313 enforceModifyPermission();
3314
3315 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3316 Bundle bundle = new Bundle();
3317 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3318 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003319 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003320
3321 /**
3322 * {@hide}
3323 * Returns the service state information on specified subscription.
3324 */
3325 @Override
3326 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3327
3328 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3329 return null;
3330 }
3331
3332 final Phone phone = getPhone(subId);
3333 if (phone == null) {
3334 return null;
3335 }
3336
3337 return phone.getServiceState();
3338 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003339
3340 /**
3341 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3342 *
3343 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3344 * voicemail ringtone.
3345 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3346 * PhoneAccount.
3347 */
3348 @Override
3349 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3350 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3351 if (phone == null) {
3352 return null;
3353 }
3354
3355 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3356 }
3357
3358 /**
3359 * Returns whether vibration is set for voicemail notification in Phone settings.
3360 *
3361 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3362 * voicemail vibration setting.
3363 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3364 */
3365 @Override
3366 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3367 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3368 if (phone == null) {
3369 return false;
3370 }
3371
3372 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3373 }
3374
Youhan Wange64578a2016-05-02 15:32:42 -07003375 /**
3376 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3377 *
3378 * @throws SecurityException if the caller does not have the required permission
3379 */
3380 private void enforceReadPrivilegedPermission() {
3381 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3382 null);
3383 }
3384
3385 /**
3386 * Return the application ID for the app type.
3387 *
3388 * @param subId the subscription ID that this request applies to.
3389 * @param appType the uicc app type.
3390 * @return Application ID for specificied app type, or null if no uicc.
3391 */
3392 @Override
3393 public String getAidForAppType(int subId, int appType) {
3394 enforceReadPrivilegedPermission();
3395 Phone phone = getPhone(subId);
3396 if (phone == null) {
3397 return null;
3398 }
3399 String aid = null;
3400 try {
3401 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3402 .getApplicationByType(appType).getAid();
3403 } catch (Exception e) {
3404 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3405 }
3406 return aid;
3407 }
3408
Youhan Wang4001d252016-05-11 10:29:41 -07003409 /**
3410 * Return the Electronic Serial Number.
3411 *
3412 * @param subId the subscription ID that this request applies to.
3413 * @return ESN or null if error.
3414 */
3415 @Override
3416 public String getEsn(int subId) {
3417 enforceReadPrivilegedPermission();
3418 Phone phone = getPhone(subId);
3419 if (phone == null) {
3420 return null;
3421 }
3422 String esn = null;
3423 try {
3424 esn = phone.getEsn();
3425 } catch (Exception e) {
3426 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3427 }
3428 return esn;
3429 }
3430
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003431 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003432 * Return the Preferred Roaming List Version.
3433 *
3434 * @param subId the subscription ID that this request applies to.
3435 * @return PRLVersion or null if error.
3436 */
3437 @Override
3438 public String getCdmaPrlVersion(int subId) {
3439 enforceReadPrivilegedPermission();
3440 Phone phone = getPhone(subId);
3441 if (phone == null) {
3442 return null;
3443 }
3444 String cdmaPrlVersion = null;
3445 try {
3446 cdmaPrlVersion = phone.getCdmaPrlVersion();
3447 } catch (Exception e) {
3448 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3449 }
3450 return cdmaPrlVersion;
3451 }
3452
3453 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003454 * Get snapshot of Telephony histograms
3455 * @return List of Telephony histograms
3456 * @hide
3457 */
3458 @Override
3459 public List<TelephonyHistogram> getTelephonyHistograms() {
3460 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3461 return RIL.getTelephonyRILTimingHistograms();
3462 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003463
3464 /**
3465 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003466 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003467 * Require system privileges. In the future we may add this to carrier APIs.
3468 *
3469 * @return The number of carriers set successfully, should match length of carriers
3470 */
3471 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003472 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003473 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003474 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003475 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3476 return retVal[0];
3477 }
3478
3479 /**
3480 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003481 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003482 * Require system privileges. In the future we may add this to carrier APIs.
3483 *
3484 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3485 * means all carriers are allowed.
3486 */
3487 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003488 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003489 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003490 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003491 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3492 }
3493
fionaxu59545b42016-05-25 15:53:37 -07003494 /**
3495 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3496 * @param subId the subscription ID that this action applies to.
3497 * @param enabled control enable or disable metered apns.
3498 * {@hide}
3499 */
3500 @Override
3501 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3502 enforceModifyPermission();
3503 final Phone phone = getPhone(subId);
3504 if (phone == null) {
3505 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3506 return;
3507 }
3508 try {
3509 phone.carrierActionSetMeteredApnsEnabled(enabled);
3510 } catch (Exception e) {
3511 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3512 }
3513 }
3514
3515 /**
3516 * Action set from carrier signalling broadcast receivers to enable/disable radio
3517 * @param subId the subscription ID that this action applies to.
3518 * @param enabled control enable or disable radio.
3519 * {@hide}
3520 */
3521 @Override
3522 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3523 enforceModifyPermission();
3524 final Phone phone = getPhone(subId);
3525 if (phone == null) {
3526 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3527 return;
3528 }
3529 try {
3530 phone.carrierActionSetRadioEnabled(enabled);
3531 } catch (Exception e) {
3532 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3533 }
3534 }
3535
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003536 /**
3537 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3538 * bug report is being generated.
3539 */
3540 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003541 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003542 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3543 != PackageManager.PERMISSION_GRANTED) {
3544 writer.println("Permission Denial: can't dump Phone from pid="
3545 + Binder.getCallingPid()
3546 + ", uid=" + Binder.getCallingUid()
3547 + "without permission "
3548 + android.Manifest.permission.DUMP);
3549 return;
3550 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003551 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003552 }
Jack Yueb89b242016-06-22 13:27:47 -07003553
3554 /**
3555 * Get aggregated video call data usage from all subscriptions since boot.
3556 * @return total data usage in bytes
3557 * {@hide}
3558 */
3559 @Override
3560 public long getVtDataUsage() {
3561 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3562 null);
3563
3564 // NetworkStatsService keeps tracking the active network interface and identity. It will
3565 // record the delta with the corresponding network identity. What we need to do here is
3566 // returning total video call data usage from all subscriptions since boot.
3567
3568 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3569 // simultaneous VT calls.
3570 final Phone[] phones = PhoneFactory.getPhones();
3571 long total = 0;
3572 for (Phone phone : phones) {
3573 total += phone.getVtDataUsage();
3574 }
3575 return total;
3576 }
Jack Yu75ab2952016-07-08 14:29:33 -07003577
3578 /**
3579 * Policy control of data connection. Usually used when data limit is passed.
3580 * @param enabled True if enabling the data, otherwise disabling.
3581 * @param subId Subscription index
3582 * {@hide}
3583 */
3584 @Override
3585 public void setPolicyDataEnabled(boolean enabled, int subId) {
3586 enforceModifyPermission();
3587 Phone phone = getPhone(subId);
3588 if (phone != null) {
3589 phone.setPolicyDataEnabled(enabled);
3590 }
3591 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003592
3593 /**
3594 * Get Client request stats
3595 * @return List of Client Request Stats
3596 * @hide
3597 */
3598 @Override
3599 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3600 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3601 return null;
3602 }
3603
3604 Phone phone = getPhone(subId);
3605 if (phone != null) {
3606 return phone.getClientRequestStats();
3607 }
3608
3609 return null;
3610 }
3611
3612 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3613 if (workSource != null) {
3614 return workSource;
3615 }
3616
3617 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3618 workSource = new WorkSource(uid, packageName);
3619 return workSource;
3620 }
Jack Yu346de222017-02-16 15:32:43 -08003621
3622 /**
3623 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3624 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003625 * @param slotIndex SIM slot id.
Jack Yu346de222017-02-16 15:32:43 -08003626 * @param powerUp True if powering up the SIM, otherwise powering down
3627 *
3628 **/
3629 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003630 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yu346de222017-02-16 15:32:43 -08003631 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003632 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003633 if (subId == null || subId.length == 0) {
3634 return;
3635 }
3636
3637 final Phone phone = getPhone(subId[0]);
3638 if (phone != null) {
3639 phone.setSimPowerState(powerUp);
3640 }
3641 }
shuoqfef36a32017-01-10 13:02:18 -08003642
3643 /**
3644 * Check if phone is in emergency callback mode
3645 * @return true if phone is in emergency callback mode
3646 * @param subId sub id
3647 */
3648 public boolean getEmergencyCallbackMode(int subId) {
3649 final Phone phone = getPhone(subId);
3650 if (phone != null) {
3651 return phone.isInEcm();
3652 } else {
3653 return false;
3654 }
3655 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003656}