blob: 6ba2f3723de477f45d02163fd825b457cf9a449d [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 /**
pkanwar8efe6572017-01-19 13:43:16 -08002142 * returns true, if the device is in a state where both voice and data
2143 * are supported simultaneously. This can change based on location or network condition.
2144 */
2145 @Override
2146 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2147 final Phone phone = getPhone(subId);
2148 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2149 }
2150
2151 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002152 * Returns the data network type.
2153 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002154 *
2155 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2156 */
2157 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002158 public int getNetworkType() {
2159 final Phone phone = getPhone(getDefaultSubscription());
2160 if (phone != null) {
2161 return phone.getServiceState().getDataNetworkType();
2162 } else {
2163 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2164 }
Wink Saville36469e72014-06-11 15:17:00 -07002165 }
2166
2167 /**
2168 * Returns the network type for a subId
2169 */
2170 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002171 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2172 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2173 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2174 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002175
Sanket Padawe356d7632015-06-22 14:03:32 -07002176 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002177 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002178 return phone.getServiceState().getDataNetworkType();
2179 } else {
2180 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2181 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002182 }
2183
2184 /**
2185 * Returns the data network type
2186 */
2187 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002188 public int getDataNetworkType(String callingPackage) {
2189 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002190 }
2191
2192 /**
2193 * Returns the data network type for a subId
2194 */
2195 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002196 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2197 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2198 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2199 }
2200
Sanket Padawe356d7632015-06-22 14:03:32 -07002201 final Phone phone = getPhone(subId);
2202 if (phone != null) {
2203 return phone.getServiceState().getDataNetworkType();
2204 } else {
2205 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2206 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002207 }
2208
2209 /**
Wink Saville36469e72014-06-11 15:17:00 -07002210 * Returns the Voice network type for a subId
2211 */
2212 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002213 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2214 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2215 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2216 }
2217
Sanket Padawe356d7632015-06-22 14:03:32 -07002218 final Phone phone = getPhone(subId);
2219 if (phone != null) {
2220 return phone.getServiceState().getVoiceNetworkType();
2221 } else {
2222 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2223 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002224 }
2225
2226 /**
2227 * @return true if a ICC card is present
2228 */
2229 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002230 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002231 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2232 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002233 }
2234
2235 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002236 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002237 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002238 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002239 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2240 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002241 final Phone phone = getPhone(subId[0]);
2242 if (subId != null && phone != null) {
2243 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002244 } else {
2245 return false;
2246 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002247 }
2248
2249 /**
2250 * Return if the current radio is LTE on CDMA. This
2251 * is a tri-state return value as for a period of time
2252 * the mode may be unknown.
2253 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002254 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002255 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002256 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002257 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002258 @Override
2259 public int getLteOnCdmaMode(String callingPackage) {
2260 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002261 }
2262
Sanket Padawe356d7632015-06-22 14:03:32 -07002263 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002264 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2265 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2266 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2267 }
2268
Sanket Padawe356d7632015-06-22 14:03:32 -07002269 final Phone phone = getPhone(subId);
2270 if (phone == null) {
2271 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2272 } else {
2273 return phone.getLteOnCdmaMode();
2274 }
Wink Saville36469e72014-06-11 15:17:00 -07002275 }
2276
2277 public void setPhone(Phone phone) {
2278 mPhone = phone;
2279 }
2280
2281 /**
2282 * {@hide}
2283 * Returns Default subId, 0 in the case of single standby.
2284 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002285 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002286 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002287 }
2288
Shishir Agrawala9f32182016-04-12 12:00:16 -07002289 private int getSlotForDefaultSubscription() {
2290 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2291 }
2292
Wink Savilleb564aae2014-10-23 10:18:09 -07002293 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002294 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002295 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002296
2297 /**
2298 * @see android.telephony.TelephonyManager.WifiCallingChoices
2299 */
2300 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002301 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2302 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002303 }
2304
2305 /**
2306 * @see android.telephony.TelephonyManager.WifiCallingChoices
2307 */
2308 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002309 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2310 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2311 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002312 }
2313
Sailesh Nepald1e68152013-12-12 19:08:02 -08002314 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002315 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002316 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002317 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002318
Shishir Agrawal566b7612013-10-28 14:41:00 -07002319 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002320 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002321 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002322
Ajay Nambiabd73502015-12-03 13:50:00 -08002323 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002324 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002325 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002326 if (DBG) log("iccOpenLogicalChannel: " + response);
2327 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002328 }
2329
2330 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002331 public boolean iccCloseLogicalChannel(int subId, int channel) {
2332 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002333
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002334 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002335 if (channel < 0) {
2336 return false;
2337 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002338 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002339 if (DBG) log("iccCloseLogicalChannel: " + success);
2340 return success;
2341 }
2342
2343 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002344 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002345 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002346 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002347
2348 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002349 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2350 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002351 " data=" + data);
2352 }
2353
2354 if (channel < 0) {
2355 return "";
2356 }
2357
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002358 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002359 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002360 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2361
Shishir Agrawal566b7612013-10-28 14:41:00 -07002362 // Append the returned status code to the end of the response payload.
2363 String s = Integer.toHexString(
2364 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002365 if (response.payload != null) {
2366 s = IccUtils.bytesToHexString(response.payload) + s;
2367 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002368 return s;
2369 }
Jake Hambye994d462014-02-03 13:10:13 -08002370
Evan Charltonc66da362014-05-16 14:06:40 -07002371 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002372 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002373 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002374 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002375
2376 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002377 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2378 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002379 }
2380
2381 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002382 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002383 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2384
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002385 // Append the returned status code to the end of the response payload.
2386 String s = Integer.toHexString(
2387 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002388 if (response.payload != null) {
2389 s = IccUtils.bytesToHexString(response.payload) + s;
2390 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002391 return s;
2392 }
2393
2394 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002395 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002396 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002397 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002398
2399 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002400 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002401 p1 + " " + p2 + " " + p3 + ":" + filePath);
2402 }
2403
2404 IccIoResult response =
2405 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002406 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2407 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002408
2409 if (DBG) {
2410 log("Exchange SIM_IO [R]" + response);
2411 }
2412
2413 byte[] result = null;
2414 int length = 2;
2415 if (response.payload != null) {
2416 length = 2 + response.payload.length;
2417 result = new byte[length];
2418 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2419 } else {
2420 result = new byte[length];
2421 }
2422
2423 result[length - 1] = (byte) response.sw2;
2424 result[length - 2] = (byte) response.sw1;
2425 return result;
2426 }
2427
Nathan Haroldb3014052017-01-25 15:57:32 -08002428 /**
2429 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2430 * on a particular subscription
2431 */
2432 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002433 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2434 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002435 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2436 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2437 return null;
2438 }
2439 Object response = sendRequest(
2440 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2441 if (response instanceof String[]) {
2442 return (String[]) response;
2443 }
2444 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2445 return null;
2446 }
2447
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002448 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002449 public String sendEnvelopeWithStatus(int subId, String content) {
2450 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002451
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002452 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002453 if (response.payload == null) {
2454 return "";
2455 }
2456
2457 // Append the returned status code to the end of the response payload.
2458 String s = Integer.toHexString(
2459 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2460 s = IccUtils.bytesToHexString(response.payload) + s;
2461 return s;
2462 }
2463
Jake Hambye994d462014-02-03 13:10:13 -08002464 /**
2465 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2466 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2467 *
2468 * @param itemID the ID of the item to read
2469 * @return the NV item as a String, or null on error.
2470 */
2471 @Override
2472 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002473 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002474 if (DBG) log("nvReadItem: item " + itemID);
2475 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2476 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2477 return value;
2478 }
2479
2480 /**
2481 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2482 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2483 *
2484 * @param itemID the ID of the item to read
2485 * @param itemValue the value to write, as a String
2486 * @return true on success; false on any failure
2487 */
2488 @Override
2489 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002490 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002491 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2492 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2493 new Pair<Integer, String>(itemID, itemValue));
2494 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2495 return success;
2496 }
2497
2498 /**
2499 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2500 * Used for device configuration by some CDMA operators.
2501 *
2502 * @param preferredRoamingList byte array containing the new PRL
2503 * @return true on success; false on any failure
2504 */
2505 @Override
2506 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002507 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002508 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2509 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2510 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2511 return success;
2512 }
2513
2514 /**
2515 * Perform the specified type of NV config reset.
2516 * Used for device configuration by some CDMA operators.
2517 *
2518 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2519 * @return true on success; false on any failure
2520 */
2521 @Override
2522 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002523 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002524 if (DBG) log("nvResetConfig: type " + resetType);
2525 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2526 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2527 return success;
2528 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002529
2530 /**
Wink Saville36469e72014-06-11 15:17:00 -07002531 * {@hide}
2532 * Returns Default sim, 0 in the case of single standby.
2533 */
2534 public int getDefaultSim() {
2535 //TODO Need to get it from Telephony Devcontroller
2536 return 0;
2537 }
2538
Svet Ganovb320e182015-04-16 12:30:10 -07002539 public String[] getPcscfAddress(String apnType, String callingPackage) {
2540 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2541 return new String[0];
2542 }
2543
2544
ram87fca6f2014-07-18 18:58:44 +05302545 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002546 }
2547
Brad Ebinger76681002017-01-23 13:50:20 -08002548 /**
2549 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2550 * feature or {@link null} if the service is not available. If an ImsServiceController is
2551 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2552 * feature updates.
2553 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002554 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002555 IImsServiceFeatureListener callback) {
2556 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002557 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002558 callback);
2559 }
2560
Wink Saville36469e72014-06-11 15:17:00 -07002561 public void setImsRegistrationState(boolean registered) {
2562 enforceModifyPermission();
2563 mPhone.setImsRegistrationState(registered);
2564 }
2565
2566 /**
Stuart Scott54788802015-03-30 13:18:01 -07002567 * Set the network selection mode to automatic.
2568 *
2569 */
2570 @Override
2571 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002572 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002573 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2574 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2575 }
2576
2577 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002578 * Set the network selection mode to manual with the selected carrier.
2579 */
2580 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002581 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2582 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002583 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002584 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002585 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2586 persistSelection);
2587 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002588 }
2589
2590 /**
2591 * Scans for available networks.
2592 */
2593 @Override
2594 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002595 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002596 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2597 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2598 CMD_PERFORM_NETWORK_SCAN, null, subId);
2599 return result;
2600 }
2601
2602 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002603 * Get the calculated preferred network type.
2604 * Used for debugging incorrect network type.
2605 *
2606 * @return the preferred network type, defined in RILConstants.java.
2607 */
2608 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002609 public int getCalculatedPreferredNetworkType(String callingPackage) {
2610 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2611 return RILConstants.PREFERRED_NETWORK_MODE;
2612 }
2613
Amit Mahajan43330e02014-11-18 11:54:45 -08002614 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002615 }
2616
2617 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002618 * Get the preferred network type.
2619 * Used for device configuration by some CDMA operators.
2620 *
2621 * @return the preferred network type, defined in RILConstants.java.
2622 */
2623 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002624 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002625 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002626 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002627 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002628 int networkType = (result != null ? result[0] : -1);
2629 if (DBG) log("getPreferredNetworkType: " + networkType);
2630 return networkType;
2631 }
2632
2633 /**
2634 * Set the preferred network type.
2635 * Used for device configuration by some CDMA operators.
2636 *
2637 * @param networkType the preferred network type, defined in RILConstants.java.
2638 * @return true on success; false on any failure.
2639 */
2640 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002641 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002642 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002643 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2644 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002645 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002646 if (success) {
2647 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002648 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002649 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002650 return success;
2651 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002652
2653 /**
Junda Liu475951f2014-11-07 16:45:03 -08002654 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2655 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2656 * tethering.
2657 *
2658 * @return 0: Not required. 1: required. 2: Not set.
2659 * @hide
2660 */
2661 @Override
2662 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002663 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002664 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2665 Settings.Global.TETHER_DUN_REQUIRED, 2);
2666 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2667 // config_tether_apndata.
2668 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2669 dunRequired = 1;
2670 }
2671 return dunRequired;
2672 }
2673
2674 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002675 * Set mobile data enabled
2676 * Used by the user through settings etc to turn on/off mobile data
2677 *
2678 * @param enable {@code true} turn turn data on, else {@code false}
2679 */
2680 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002681 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002682 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002683 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002684 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002685 Phone phone = PhoneFactory.getPhone(phoneId);
2686 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002687 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002688 phone.setDataEnabled(enable);
2689 } else {
2690 loge("setDataEnabled: no phone for subId=" + subId);
2691 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002692 }
2693
2694 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002695 * Get whether mobile data is enabled.
2696 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002697 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002698 *
2699 * @return {@code true} if data is enabled else {@code false}
2700 */
2701 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002702 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002703 try {
2704 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2705 null);
2706 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002707 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002708 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002709 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002710 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002711 Phone phone = PhoneFactory.getPhone(phoneId);
2712 if (phone != null) {
2713 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002714 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002715 return retVal;
2716 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002717 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002718 return false;
2719 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002720 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002721
2722 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002723 public int getCarrierPrivilegeStatus(int subId) {
2724 final Phone phone = getPhone(subId);
2725 if (phone == null) {
2726 loge("getCarrierPrivilegeStatus: Invalid subId");
2727 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2728 }
2729 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002730 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002731 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002732 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2733 }
2734 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002735 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002736 }
Junda Liu29340342014-07-10 15:23:27 -07002737
2738 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002739 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002740 if (TextUtils.isEmpty(pkgName))
2741 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002742 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002743 if (card == null) {
2744 loge("checkCarrierPrivilegesForPackage: No UICC");
2745 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2746 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002747 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2748 }
2749
2750 @Override
2751 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002752 if (TextUtils.isEmpty(pkgName))
2753 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002754 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2755 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2756 UiccCard card = UiccController.getInstance().getUiccCard(i);
2757 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002758 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002759 continue;
2760 }
2761
2762 result = card.getCarrierPrivilegeStatus(
2763 mPhone.getContext().getPackageManager(), pkgName);
2764 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2765 break;
2766 }
2767 }
2768
2769 return result;
Junda Liu29340342014-07-10 15:23:27 -07002770 }
Derek Tan89e89d42014-07-08 17:00:10 -07002771
2772 @Override
Junda Liue64de782015-04-16 17:19:16 -07002773 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2774 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2775 loge("phoneId " + phoneId + " is not valid.");
2776 return null;
2777 }
2778 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002779 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002780 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002781 return null ;
2782 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002783 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002784 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002785 }
2786
Amith Yamasani6e118872016-02-19 12:53:51 -08002787 @Override
2788 public List<String> getPackagesWithCarrierPrivileges() {
2789 PackageManager pm = mPhone.getContext().getPackageManager();
2790 List<String> privilegedPackages = new ArrayList<>();
2791 List<PackageInfo> packages = null;
2792 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2793 UiccCard card = UiccController.getInstance().getUiccCard(i);
2794 if (card == null) {
2795 // No UICC in that slot.
2796 continue;
2797 }
2798 if (card.hasCarrierPrivilegeRules()) {
2799 if (packages == null) {
2800 // Only check packages in user 0 for now
2801 packages = pm.getInstalledPackagesAsUser(
2802 PackageManager.MATCH_DISABLED_COMPONENTS
2803 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2804 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2805 }
2806 for (int p = packages.size() - 1; p >= 0; p--) {
2807 PackageInfo pkgInfo = packages.get(p);
2808 if (pkgInfo != null && pkgInfo.packageName != null
2809 && card.getCarrierPrivilegeStatus(pkgInfo)
2810 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2811 privilegedPackages.add(pkgInfo.packageName);
2812 }
2813 }
2814 }
2815 }
2816 return privilegedPackages;
2817 }
2818
Wink Savilleb564aae2014-10-23 10:18:09 -07002819 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002820 final Phone phone = getPhone(subId);
2821 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002822 if (card == null) {
2823 loge("getIccId: No UICC");
2824 return null;
2825 }
2826 String iccId = card.getIccId();
2827 if (TextUtils.isEmpty(iccId)) {
2828 loge("getIccId: ICC ID is null or empty.");
2829 return null;
2830 }
2831 return iccId;
2832 }
2833
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002834 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002835 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2836 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002837 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002838
Jeff Sharkey85190e62014-12-05 09:40:12 -08002839 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002840 final Phone phone = getPhone(subId);
2841 if (phone == null) {
2842 return false;
2843 }
2844 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002845
2846 if (DBG_MERGE) {
2847 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2848 + subscriberId + " to " + number);
2849 }
2850
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002851 if (TextUtils.isEmpty(iccId)) {
2852 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002853 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002854
Jeff Sharkey85190e62014-12-05 09:40:12 -08002855 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2856
2857 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002858 if (alphaTag == null) {
2859 editor.remove(alphaTagPrefKey);
2860 } else {
2861 editor.putString(alphaTagPrefKey, alphaTag);
2862 }
2863
Jeff Sharkey85190e62014-12-05 09:40:12 -08002864 // Record both the line number and IMSI for this ICCID, since we need to
2865 // track all merged IMSIs based on line number
2866 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2867 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002868 if (number == null) {
2869 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002870 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002871 } else {
2872 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002873 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002874 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002875
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002876 editor.commit();
2877 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002878 }
2879
2880 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002881 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002882 // This is open to apps with WRITE_SMS.
2883 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002884 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002885 return null;
2886 }
Derek Tan97ebb422014-09-05 16:55:38 -07002887
2888 String iccId = getIccId(subId);
2889 if (iccId != null) {
2890 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002891 if (DBG_MERGE) {
2892 log("getLine1NumberForDisplay returning " +
2893 mTelephonySharedPreferences.getString(numberPrefKey, null));
2894 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002895 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002896 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002897 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002898 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002899 }
2900
2901 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002902 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2903 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2904 return null;
2905 }
Derek Tan97ebb422014-09-05 16:55:38 -07002906
2907 String iccId = getIccId(subId);
2908 if (iccId != null) {
2909 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002910 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002911 }
Derek Tan97ebb422014-09-05 16:55:38 -07002912 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002913 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002914
2915 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002916 public String[] getMergedSubscriberIds(String callingPackage) {
2917 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2918 return null;
2919 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002920 final Context context = mPhone.getContext();
2921 final TelephonyManager tele = TelephonyManager.from(context);
2922 final SubscriptionManager sub = SubscriptionManager.from(context);
2923
2924 // Figure out what subscribers are currently active
2925 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002926 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2927 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2928 final long identity = Binder.clearCallingIdentity();
2929 try {
2930 final int[] subIds = sub.getActiveSubscriptionIdList();
2931 for (int subId : subIds) {
2932 activeSubscriberIds.add(tele.getSubscriberId(subId));
2933 }
2934 } finally {
2935 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002936 }
2937
2938 // First pass, find a number override for an active subscriber
2939 String mergeNumber = null;
2940 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2941 for (String key : prefs.keySet()) {
2942 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2943 final String subscriberId = (String) prefs.get(key);
2944 if (activeSubscriberIds.contains(subscriberId)) {
2945 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2946 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2947 mergeNumber = (String) prefs.get(numberKey);
2948 if (DBG_MERGE) {
2949 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2950 + " for active subscriber " + subscriberId);
2951 }
2952 if (!TextUtils.isEmpty(mergeNumber)) {
2953 break;
2954 }
2955 }
2956 }
2957 }
2958
2959 // Shortcut when no active merged subscribers
2960 if (TextUtils.isEmpty(mergeNumber)) {
2961 return null;
2962 }
2963
2964 // Second pass, find all subscribers under that line override
2965 final ArraySet<String> result = new ArraySet<>();
2966 for (String key : prefs.keySet()) {
2967 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2968 final String number = (String) prefs.get(key);
2969 if (mergeNumber.equals(number)) {
2970 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2971 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2972 final String subscriberId = (String) prefs.get(subscriberKey);
2973 if (!TextUtils.isEmpty(subscriberId)) {
2974 result.add(subscriberId);
2975 }
2976 }
2977 }
2978 }
2979
2980 final String[] resultArray = result.toArray(new String[result.size()]);
2981 Arrays.sort(resultArray);
2982 if (DBG_MERGE) {
2983 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2984 }
2985 return resultArray;
2986 }
2987
2988 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002989 public boolean setOperatorBrandOverride(int subId, String brand) {
2990 enforceCarrierPrivilege(subId);
2991 final Phone phone = getPhone(subId);
2992 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002993 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002994
2995 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002996 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002997 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2998 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002999 enforceCarrierPrivilege(subId);
3000 final Phone phone = getPhone(subId);
3001 if (phone == null) {
3002 return false;
3003 }
3004 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003005 cdmaNonRoamingList);
3006 }
3007
3008 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003009 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003010 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3011 enforceModifyPermission();
3012
3013 int returnValue = 0;
3014 try {
3015 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3016 if(result.exception == null) {
3017 if (result.result != null) {
3018 byte[] responseData = (byte[])(result.result);
3019 if(responseData.length > oemResp.length) {
3020 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3021 responseData.length + "bytes. Buffer Size is " +
3022 oemResp.length + "bytes.");
3023 }
3024 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3025 returnValue = responseData.length;
3026 }
3027 } else {
3028 CommandException ex = (CommandException) result.exception;
3029 returnValue = ex.getCommandError().ordinal();
3030 if(returnValue > 0) returnValue *= -1;
3031 }
3032 } catch (RuntimeException e) {
3033 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3034 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3035 if(returnValue > 0) returnValue *= -1;
3036 }
3037
3038 return returnValue;
3039 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003040
3041 @Override
3042 public void setRadioCapability(RadioAccessFamily[] rafs) {
3043 try {
3044 ProxyController.getInstance().setRadioCapability(rafs);
3045 } catch (RuntimeException e) {
3046 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3047 }
3048 }
3049
3050 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003051 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3052 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3053 return RadioAccessFamily.RAF_UNKNOWN;
3054 }
3055
Wink Saville5d475dd2014-10-17 15:00:58 -07003056 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3057 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003058
3059 @Override
3060 public void enableVideoCalling(boolean enable) {
3061 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003062 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003063 }
3064
3065 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003066 public boolean isVideoCallingEnabled(String callingPackage) {
3067 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3068 return false;
3069 }
3070
Andrew Lee77527ac2014-10-21 16:57:39 -07003071 // Check the user preference and the system-level IMS setting. Even if the user has
3072 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3073 // In the long run, we may instead need to check if there exists a connection service
3074 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003075 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3076 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003077 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003078 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003079
Andrew Leea1239f22015-03-02 17:44:07 -08003080 @Override
3081 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003082 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003083 }
3084
3085 @Override
3086 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003087 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003088 }
3089
Andrew Lee9431b832015-03-09 18:46:45 -07003090 @Override
3091 public boolean isTtyModeSupported() {
3092 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3093 TelephonyManager telephonyManager =
3094 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003095 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003096 }
3097
3098 @Override
3099 public boolean isHearingAidCompatibilitySupported() {
3100 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3101 }
3102
Sanket Padawe7310cc72015-01-14 09:53:20 -08003103 /**
3104 * Returns the unique device ID of phone, for example, the IMEI for
3105 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3106 *
3107 * <p>Requires Permission:
3108 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3109 */
3110 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003111 public String getDeviceId(String callingPackage) {
3112 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3113 return null;
3114 }
3115
Sanket Padawe7310cc72015-01-14 09:53:20 -08003116 final Phone phone = PhoneFactory.getPhone(0);
3117 if (phone != null) {
3118 return phone.getDeviceId();
3119 } else {
3120 return null;
3121 }
3122 }
3123
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003124 /*
3125 * {@hide}
3126 * Returns the IMS Registration Status
3127 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003128 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003129 public boolean isImsRegistered() {
3130 return mPhone.isImsRegistered();
3131 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003132
3133 @Override
3134 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3135 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3136 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003137
Nathan Haroldc55097a2015-03-11 18:14:50 -07003138 /*
3139 * {@hide}
3140 * Returns the IMS Registration Status
3141 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003142 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003143 return mPhone.isWifiCallingEnabled();
3144 }
3145
3146 /*
3147 * {@hide}
3148 * Returns the IMS Registration Status
3149 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003150 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003151 return mPhone.isVolteEnabled();
3152 }
Svet Ganovb320e182015-04-16 12:30:10 -07003153
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003154 /*
3155 * {@hide} Returns the IMS Registration Status
3156 */
3157 public boolean isVideoTelephonyAvailable() {
3158 return mPhone.isVideoEnabled();
3159 }
3160
Svet Ganovb320e182015-04-16 12:30:10 -07003161 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003162 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003163 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003164 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3165
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003166 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003167 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003168 } catch (SecurityException e) {
3169 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3170 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003171 }
Svet Ganovb320e182015-04-16 12:30:10 -07003172
3173 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3174 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3175 return false;
3176 }
3177
3178 return true;
3179 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003180
Makoto Onukifee69342015-06-29 14:44:50 -07003181 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003182 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003183 */
3184 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003185 // Default SMS app can always read it.
3186 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3187 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3188 return true;
3189 }
3190 try {
3191 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003192 } catch (SecurityException readPhoneStateSecurityException) {
3193 try {
3194 // Can be read with READ_SMS too.
3195 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3196 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3197 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3198 } catch (SecurityException readSmsSecurityException) {
3199 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3200 // permissions
3201 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3202 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3203 " or " + android.Manifest.permission.READ_SMS + ".");
3204 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003205 }
Makoto Onukifee69342015-06-29 14:44:50 -07003206 }
3207
Stuart Scott8eef64f2015-04-08 15:13:54 -07003208 @Override
3209 public void factoryReset(int subId) {
3210 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003211 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3212 return;
3213 }
3214
Svet Ganovcc087f82015-05-12 20:35:54 -07003215 final long identity = Binder.clearCallingIdentity();
3216 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003217 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3218 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003219 // Enable data
3220 setDataEnabled(subId, true);
3221 // Set network selection mode to automatic
3222 setNetworkSelectionModeAutomatic(subId);
3223 // Set preferred mobile network type to the best available
3224 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3225 // Turn off roaming
3226 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3227 }
3228 } finally {
3229 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003230 }
3231 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003232
3233 @Override
3234 public String getLocaleFromDefaultSim() {
3235 // We query all subscriptions instead of just the active ones, because
3236 // this might be called early on in the provisioning flow when the
3237 // subscriptions potentially aren't active yet.
3238 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3239 if (slist == null || slist.isEmpty()) {
3240 return null;
3241 }
3242
3243 // This function may be called very early, say, from the setup wizard, at
3244 // which point we won't have a default subscription set. If that's the case
3245 // we just choose the first, which will be valid in "most cases".
3246 final int defaultSubId = getDefaultSubscription();
3247 SubscriptionInfo info = null;
3248 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3249 info = slist.get(0);
3250 } else {
3251 for (SubscriptionInfo item : slist) {
3252 if (item.getSubscriptionId() == defaultSubId) {
3253 info = item;
3254 break;
3255 }
3256 }
3257
3258 if (info == null) {
3259 return null;
3260 }
3261 }
3262
3263 // Try and fetch the locale from the carrier properties or from the SIM language
3264 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003265 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003266 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003267 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003268 if (defaultPhone != null) {
3269 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3270 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003271 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003272 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3273 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003274 } else {
3275 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003276 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003277 }
3278 }
3279
Narayan Kamath011676f2015-07-29 12:04:08 +01003280 // The SIM language preferences only store a language (e.g. fr = French), not an
3281 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3282 // the SIM and carrier preferences does not include a country we add the country
3283 // determined from the SIM MCC to provide an exact locale.
3284 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003285 if (mccLocale != null) {
3286 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3287 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003288 }
3289
Tony Hill183b2de2015-06-24 14:53:58 +01003290 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003291 return null;
3292 }
3293
3294 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3295 final long identity = Binder.clearCallingIdentity();
3296 try {
3297 return mSubscriptionController.getAllSubInfoList(
3298 mPhone.getContext().getOpPackageName());
3299 } finally {
3300 Binder.restoreCallingIdentity(identity);
3301 }
3302 }
3303
3304 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3305 final long identity = Binder.clearCallingIdentity();
3306 try {
3307 return mSubscriptionController.getActiveSubscriptionInfoList(
3308 mPhone.getContext().getOpPackageName());
3309 } finally {
3310 Binder.restoreCallingIdentity(identity);
3311 }
3312 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003313
3314 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003315 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3316 * representing the state of the modem.
3317 *
3318 * NOTE: This clears the modem state, so there should only every be one caller.
3319 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003320 */
3321 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003322 public void requestModemActivityInfo(ResultReceiver result) {
3323 enforceModifyPermission();
3324
3325 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3326 Bundle bundle = new Bundle();
3327 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3328 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003329 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003330
3331 /**
3332 * {@hide}
3333 * Returns the service state information on specified subscription.
3334 */
3335 @Override
3336 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3337
3338 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3339 return null;
3340 }
3341
3342 final Phone phone = getPhone(subId);
3343 if (phone == null) {
3344 return null;
3345 }
3346
3347 return phone.getServiceState();
3348 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003349
3350 /**
3351 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3352 *
3353 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3354 * voicemail ringtone.
3355 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3356 * PhoneAccount.
3357 */
3358 @Override
3359 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3360 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3361 if (phone == null) {
3362 return null;
3363 }
3364
3365 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3366 }
3367
3368 /**
3369 * Returns whether vibration is set for voicemail notification in Phone settings.
3370 *
3371 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3372 * voicemail vibration setting.
3373 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3374 */
3375 @Override
3376 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3377 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3378 if (phone == null) {
3379 return false;
3380 }
3381
3382 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3383 }
3384
Youhan Wange64578a2016-05-02 15:32:42 -07003385 /**
3386 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3387 *
3388 * @throws SecurityException if the caller does not have the required permission
3389 */
3390 private void enforceReadPrivilegedPermission() {
3391 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3392 null);
3393 }
3394
3395 /**
3396 * Return the application ID for the app type.
3397 *
3398 * @param subId the subscription ID that this request applies to.
3399 * @param appType the uicc app type.
3400 * @return Application ID for specificied app type, or null if no uicc.
3401 */
3402 @Override
3403 public String getAidForAppType(int subId, int appType) {
3404 enforceReadPrivilegedPermission();
3405 Phone phone = getPhone(subId);
3406 if (phone == null) {
3407 return null;
3408 }
3409 String aid = null;
3410 try {
3411 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3412 .getApplicationByType(appType).getAid();
3413 } catch (Exception e) {
3414 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3415 }
3416 return aid;
3417 }
3418
Youhan Wang4001d252016-05-11 10:29:41 -07003419 /**
3420 * Return the Electronic Serial Number.
3421 *
3422 * @param subId the subscription ID that this request applies to.
3423 * @return ESN or null if error.
3424 */
3425 @Override
3426 public String getEsn(int subId) {
3427 enforceReadPrivilegedPermission();
3428 Phone phone = getPhone(subId);
3429 if (phone == null) {
3430 return null;
3431 }
3432 String esn = null;
3433 try {
3434 esn = phone.getEsn();
3435 } catch (Exception e) {
3436 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3437 }
3438 return esn;
3439 }
3440
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003441 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003442 * Return the Preferred Roaming List Version.
3443 *
3444 * @param subId the subscription ID that this request applies to.
3445 * @return PRLVersion or null if error.
3446 */
3447 @Override
3448 public String getCdmaPrlVersion(int subId) {
3449 enforceReadPrivilegedPermission();
3450 Phone phone = getPhone(subId);
3451 if (phone == null) {
3452 return null;
3453 }
3454 String cdmaPrlVersion = null;
3455 try {
3456 cdmaPrlVersion = phone.getCdmaPrlVersion();
3457 } catch (Exception e) {
3458 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3459 }
3460 return cdmaPrlVersion;
3461 }
3462
3463 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003464 * Get snapshot of Telephony histograms
3465 * @return List of Telephony histograms
3466 * @hide
3467 */
3468 @Override
3469 public List<TelephonyHistogram> getTelephonyHistograms() {
3470 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3471 return RIL.getTelephonyRILTimingHistograms();
3472 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003473
3474 /**
3475 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003476 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003477 * Require system privileges. In the future we may add this to carrier APIs.
3478 *
3479 * @return The number of carriers set successfully, should match length of carriers
3480 */
3481 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003482 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003483 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003484 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003485 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3486 return retVal[0];
3487 }
3488
3489 /**
3490 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003491 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003492 * Require system privileges. In the future we may add this to carrier APIs.
3493 *
3494 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3495 * means all carriers are allowed.
3496 */
3497 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003498 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003499 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003500 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003501 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3502 }
3503
fionaxu59545b42016-05-25 15:53:37 -07003504 /**
3505 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3506 * @param subId the subscription ID that this action applies to.
3507 * @param enabled control enable or disable metered apns.
3508 * {@hide}
3509 */
3510 @Override
3511 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3512 enforceModifyPermission();
3513 final Phone phone = getPhone(subId);
3514 if (phone == null) {
3515 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3516 return;
3517 }
3518 try {
3519 phone.carrierActionSetMeteredApnsEnabled(enabled);
3520 } catch (Exception e) {
3521 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3522 }
3523 }
3524
3525 /**
3526 * Action set from carrier signalling broadcast receivers to enable/disable radio
3527 * @param subId the subscription ID that this action applies to.
3528 * @param enabled control enable or disable radio.
3529 * {@hide}
3530 */
3531 @Override
3532 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3533 enforceModifyPermission();
3534 final Phone phone = getPhone(subId);
3535 if (phone == null) {
3536 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3537 return;
3538 }
3539 try {
3540 phone.carrierActionSetRadioEnabled(enabled);
3541 } catch (Exception e) {
3542 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3543 }
3544 }
3545
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003546 /**
3547 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3548 * bug report is being generated.
3549 */
3550 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003551 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003552 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3553 != PackageManager.PERMISSION_GRANTED) {
3554 writer.println("Permission Denial: can't dump Phone from pid="
3555 + Binder.getCallingPid()
3556 + ", uid=" + Binder.getCallingUid()
3557 + "without permission "
3558 + android.Manifest.permission.DUMP);
3559 return;
3560 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003561 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003562 }
Jack Yueb89b242016-06-22 13:27:47 -07003563
3564 /**
3565 * Get aggregated video call data usage from all subscriptions since boot.
3566 * @return total data usage in bytes
3567 * {@hide}
3568 */
3569 @Override
3570 public long getVtDataUsage() {
3571 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3572 null);
3573
3574 // NetworkStatsService keeps tracking the active network interface and identity. It will
3575 // record the delta with the corresponding network identity. What we need to do here is
3576 // returning total video call data usage from all subscriptions since boot.
3577
3578 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3579 // simultaneous VT calls.
3580 final Phone[] phones = PhoneFactory.getPhones();
3581 long total = 0;
3582 for (Phone phone : phones) {
3583 total += phone.getVtDataUsage();
3584 }
3585 return total;
3586 }
Jack Yu75ab2952016-07-08 14:29:33 -07003587
3588 /**
3589 * Policy control of data connection. Usually used when data limit is passed.
3590 * @param enabled True if enabling the data, otherwise disabling.
3591 * @param subId Subscription index
3592 * {@hide}
3593 */
3594 @Override
3595 public void setPolicyDataEnabled(boolean enabled, int subId) {
3596 enforceModifyPermission();
3597 Phone phone = getPhone(subId);
3598 if (phone != null) {
3599 phone.setPolicyDataEnabled(enabled);
3600 }
3601 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003602
3603 /**
3604 * Get Client request stats
3605 * @return List of Client Request Stats
3606 * @hide
3607 */
3608 @Override
3609 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3610 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3611 return null;
3612 }
3613
3614 Phone phone = getPhone(subId);
3615 if (phone != null) {
3616 return phone.getClientRequestStats();
3617 }
3618
3619 return null;
3620 }
3621
3622 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3623 if (workSource != null) {
3624 return workSource;
3625 }
3626
3627 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3628 workSource = new WorkSource(uid, packageName);
3629 return workSource;
3630 }
Jack Yu346de222017-02-16 15:32:43 -08003631
3632 /**
3633 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3634 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003635 * @param slotIndex SIM slot id.
Jack Yu346de222017-02-16 15:32:43 -08003636 * @param powerUp True if powering up the SIM, otherwise powering down
3637 *
3638 **/
3639 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003640 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yu346de222017-02-16 15:32:43 -08003641 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003642 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003643 if (subId == null || subId.length == 0) {
3644 return;
3645 }
3646
3647 final Phone phone = getPhone(subId[0]);
3648 if (phone != null) {
3649 phone.setSimPowerState(powerUp);
3650 }
3651 }
shuoqfef36a32017-01-10 13:02:18 -08003652
3653 /**
3654 * Check if phone is in emergency callback mode
3655 * @return true if phone is in emergency callback mode
3656 * @param subId sub id
3657 */
3658 public boolean getEmergencyCallbackMode(int subId) {
3659 final Phone phone = getPhone(subId);
3660 if (phone != null) {
3661 return phone.isInEcm();
3662 } else {
3663 return false;
3664 }
3665 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003666}