blob: 9d16bad64e302727941968c414de73c4db8603ca [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
pkanwar7e856482016-10-14 19:37:38 -07001547 public void handleUssdRequest(String ussdRequest, ResultReceiver wrappedCallback) {
1548 enforceCallPermission();
1549 int subId = getDefaultSubscription();
1550 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1551 return;
1552 }
1553 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1554 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1555 };
1556
1557
Wink Savilleb564aae2014-10-23 10:18:09 -07001558 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001559 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001560 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1561 return false;
1562 }
Wink Saville36469e72014-06-11 15:17:00 -07001563 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001564 }
1565
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001566 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001567 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001568 }
1569
Sanket Padawe5780e442017-03-20 15:04:47 -07001570 public int getCallStateForSlot(int slotIndex) {
1571 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001572 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1573 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001574 }
1575
Sanket Padawe356d7632015-06-22 14:03:32 -07001576 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001577 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001578 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001579 if (phone != null) {
1580 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1581 } else {
1582 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1583 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001584 }
1585
Sanket Padawe356d7632015-06-22 14:03:32 -07001586 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001587 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001588 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001589 if (phone != null) {
1590 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1591 } else {
1592 return TelephonyManager.DATA_ACTIVITY_NONE;
1593 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001594 }
1595
1596 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001597 public Bundle getCellLocation(String callingPackage) {
1598 enforceFineOrCoarseLocationPermission("getCellLocation");
1599
1600 // OP_COARSE_LOCATION controls both fine and coarse location.
1601 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1602 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001603 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001604 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001605 }
1606
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001607 if (checkIfCallerIsSelfOrForegroundUser() ||
1608 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001609 if (DBG_LOC) log("getCellLocation: is active user");
1610 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001611 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001612 if (phone == null) {
1613 return null;
1614 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07001615
1616 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1617 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001618 return data;
1619 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001620 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001621 return null;
1622 }
1623 }
1624
Svetoslav64fad262015-04-14 14:35:21 -07001625 private void enforceFineOrCoarseLocationPermission(String message) {
1626 try {
1627 mApp.enforceCallingOrSelfPermission(
1628 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1629 } catch (SecurityException e) {
1630 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1631 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1632 // is the weaker precondition
1633 mApp.enforceCallingOrSelfPermission(
1634 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1635 }
1636 }
1637
1638
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001639 @Override
1640 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001641 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001642 }
1643
Sanket Padawe356d7632015-06-22 14:03:32 -07001644 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001645 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001646 mApp.enforceCallingOrSelfPermission(
1647 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001648 final Phone phone = getPhone(subId);
1649 if (phone != null) {
1650 phone.enableLocationUpdates();
1651 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001652 }
1653
1654 @Override
1655 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001656 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001657 }
1658
Sanket Padawe356d7632015-06-22 14:03:32 -07001659 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001660 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001661 mApp.enforceCallingOrSelfPermission(
1662 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001663 final Phone phone = getPhone(subId);
1664 if (phone != null) {
1665 phone.disableLocationUpdates();
1666 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001667 }
1668
1669 @Override
1670 @SuppressWarnings("unchecked")
1671 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001672 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1673
1674 // OP_COARSE_LOCATION controls both fine and coarse location.
1675 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1676 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1677 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001678 }
1679
1680 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1681 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1682 return null;
1683 }
Svetoslav64fad262015-04-14 14:35:21 -07001684
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001685 if (checkIfCallerIsSelfOrForegroundUser() ||
1686 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001687 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1688
1689 ArrayList<NeighboringCellInfo> cells = null;
1690
Sooraj Sasindran22882212016-07-18 11:57:25 -07001691 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001692 try {
1693 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran22882212016-07-18 11:57:25 -07001694 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001695 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001696 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001697 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001698 }
1699 return cells;
1700 } else {
1701 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1702 return null;
1703 }
1704 }
1705
1706
1707 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001708 public List<CellInfo> getAllCellInfo(String callingPackage) {
1709 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1710
1711 // OP_COARSE_LOCATION controls both fine and coarse location.
1712 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1713 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1714 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001715 }
1716
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001717 if (checkIfCallerIsSelfOrForegroundUser() ||
1718 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001719 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran22882212016-07-18 11:57:25 -07001720 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001721 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1722 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran22882212016-07-18 11:57:25 -07001723 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1724 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001725 }
1726 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001727 } else {
1728 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1729 return null;
1730 }
1731 }
1732
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001733 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001734 public void setCellInfoListRate(int rateInMillis) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001735 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001736 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1737 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001738 }
1739
Shishir Agrawala9f32182016-04-12 12:00:16 -07001740 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001741 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001742 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1743 return null;
1744 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001745 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001746 return phone == null ? null : phone.getImei();
1747 }
1748
1749 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001750 public String getMeidForSlot(int slotIndex, String callingPackage) {
1751 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1752 return null;
1753 }
1754 Phone phone = PhoneFactory.getPhone(slotIndex);
1755 return phone == null ? null : phone.getMeid();
1756 }
1757
1758 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001759 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001760 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1761 return null;
1762 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001763 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001764 return phone == null ? null : phone.getDeviceSvn();
1765 }
1766
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001767 //
1768 // Internal helper methods.
1769 //
1770
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001771 /**
1772 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1773 */
1774 private boolean checkCallerInteractAcrossUsersFull() {
1775 return mPhone.getContext().checkCallingOrSelfPermission(
1776 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1777 == PackageManager.PERMISSION_GRANTED;
1778 }
1779
Jake Hambye994d462014-02-03 13:10:13 -08001780 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001781 boolean ok;
1782
1783 boolean self = Binder.getCallingUid() == Process.myUid();
1784 if (!self) {
1785 // Get the caller's user id then clear the calling identity
1786 // which will be restored in the finally clause.
1787 int callingUser = UserHandle.getCallingUserId();
1788 long ident = Binder.clearCallingIdentity();
1789
1790 try {
1791 // With calling identity cleared the current user is the foreground user.
1792 int foregroundUser = ActivityManager.getCurrentUser();
1793 ok = (foregroundUser == callingUser);
1794 if (DBG_LOC) {
1795 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1796 + " callingUser=" + callingUser + " ok=" + ok);
1797 }
1798 } catch (Exception ex) {
1799 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1800 ok = false;
1801 } finally {
1802 Binder.restoreCallingIdentity(ident);
1803 }
1804 } else {
1805 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1806 ok = true;
1807 }
1808 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1809 return ok;
1810 }
1811
1812 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001813 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1814 *
1815 * @throws SecurityException if the caller does not have the required permission
1816 */
1817 private void enforceModifyPermission() {
1818 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1819 }
1820
1821 /**
Junda Liua2e36012014-07-09 18:30:01 -07001822 * Make sure either system app or the caller has carrier privilege.
1823 *
1824 * @throws SecurityException if the caller does not have the required permission/privilege
1825 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001826 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001827 int permission = mApp.checkCallingOrSelfPermission(
1828 android.Manifest.permission.MODIFY_PHONE_STATE);
1829 if (permission == PackageManager.PERMISSION_GRANTED) {
1830 return;
1831 }
1832
1833 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001834 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001835 }
1836
1837 /**
1838 * Make sure the caller has carrier privilege.
1839 *
1840 * @throws SecurityException if the caller does not have the required permission
1841 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001842 private void enforceCarrierPrivilege(int subId) {
1843 if (getCarrierPrivilegeStatus(subId) !=
1844 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001845 loge("No Carrier Privilege.");
1846 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001847 }
1848 }
1849
1850 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001851 * Make sure the caller has the CALL_PHONE permission.
1852 *
1853 * @throws SecurityException if the caller does not have the required permission
1854 */
1855 private void enforceCallPermission() {
1856 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1857 }
1858
Stuart Scott8eef64f2015-04-08 15:13:54 -07001859 private void enforceConnectivityInternalPermission() {
1860 mApp.enforceCallingOrSelfPermission(
1861 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1862 "ConnectivityService");
1863 }
1864
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001865 private String createTelUrl(String number) {
1866 if (TextUtils.isEmpty(number)) {
1867 return null;
1868 }
1869
Jake Hambye994d462014-02-03 13:10:13 -08001870 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001871 }
1872
Ihab Awadf9e92732013-12-05 18:02:52 -08001873 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001874 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1875 }
1876
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001877 private static void logv(String msg) {
1878 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1879 }
1880
Ihab Awadf9e92732013-12-05 18:02:52 -08001881 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001882 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1883 }
1884
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001885 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001886 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001887 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001888 }
1889
Sanket Padawe356d7632015-06-22 14:03:32 -07001890 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001891 public int getActivePhoneTypeForSlot(int slotIndex) {
1892 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001893 if (phone == null) {
1894 return PhoneConstants.PHONE_TYPE_NONE;
1895 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001896 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001897 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001898 }
1899
1900 /**
1901 * Returns the CDMA ERI icon index to display
1902 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001903 @Override
1904 public int getCdmaEriIconIndex(String callingPackage) {
1905 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001906 }
1907
Sanket Padawe356d7632015-06-22 14:03:32 -07001908 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001909 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1910 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1911 return -1;
1912 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001913 final Phone phone = getPhone(subId);
1914 if (phone != null) {
1915 return phone.getCdmaEriIconIndex();
1916 } else {
1917 return -1;
1918 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001919 }
1920
1921 /**
1922 * Returns the CDMA ERI icon mode,
1923 * 0 - ON
1924 * 1 - FLASHING
1925 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001926 @Override
1927 public int getCdmaEriIconMode(String callingPackage) {
1928 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001929 }
1930
Sanket Padawe356d7632015-06-22 14:03:32 -07001931 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001932 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1933 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1934 return -1;
1935 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001936 final Phone phone = getPhone(subId);
1937 if (phone != null) {
1938 return phone.getCdmaEriIconMode();
1939 } else {
1940 return -1;
1941 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001942 }
1943
1944 /**
1945 * Returns the CDMA ERI text,
1946 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001947 @Override
1948 public String getCdmaEriText(String callingPackage) {
1949 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001950 }
1951
Sanket Padawe356d7632015-06-22 14:03:32 -07001952 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001953 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1954 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1955 return null;
1956 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001957 final Phone phone = getPhone(subId);
1958 if (phone != null) {
1959 return phone.getCdmaEriText();
1960 } else {
1961 return null;
1962 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001963 }
1964
1965 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001966 * Returns the CDMA MDN.
1967 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001968 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001969 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001970 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001971 final Phone phone = getPhone(subId);
1972 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1973 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001974 } else {
1975 return null;
1976 }
1977 }
1978
1979 /**
1980 * Returns the CDMA MIN.
1981 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001982 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001983 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001984 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001985 final Phone phone = getPhone(subId);
1986 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1987 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001988 } else {
1989 return null;
1990 }
1991 }
1992
1993 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001994 * Returns true if CDMA provisioning needs to run.
1995 */
1996 public boolean needsOtaServiceProvisioning() {
1997 return mPhone.needsOtaServiceProvisioning();
1998 }
1999
2000 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002001 * Sets the voice mail number of a given subId.
2002 */
2003 @Override
2004 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002005 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002006 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2007 new Pair<String, String>(alphaTag, number), new Integer(subId));
2008 return success;
2009 }
2010
Ta-wei Yen87c49842016-05-13 21:19:52 -07002011 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07002012 public void setVisualVoicemailEnabled(String callingPackage,
2013 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
2014 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2015 if (!TextUtils.equals(callingPackage,
2016 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2017 enforceModifyPermissionOrCarrierPrivilege(
2018 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
2019 }
2020 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2021 }
2022
2023 @Override
2024 public boolean isVisualVoicemailEnabled(String callingPackage,
2025 PhoneAccountHandle phoneAccountHandle) {
2026 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2027 return false;
2028 }
2029 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2030 }
2031
2032 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002033 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2034 VisualVoicemailSmsFilterSettings settings) {
2035 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002036 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002037 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2038 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002039 }
2040
2041 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002042 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2043 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002044 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002045 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002046 }
2047
2048 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002049 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2050 String callingPackage, int subId) {
2051 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002052 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002053 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002054 }
2055
2056 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002057 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
2058 String packageName, int subId) {
2059 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002060 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002061 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002062 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002063 /**
fionaxuef039d42016-11-14 13:36:14 -08002064 * Sets the voice activation state of a given subId.
2065 */
2066 @Override
2067 public void setVoiceActivationState(int subId, int activationState) {
2068 enforceModifyPermissionOrCarrierPrivilege(subId);
2069 final Phone phone = getPhone(subId);
2070 if (phone != null) {
2071 phone.setVoiceActivationState(activationState);
2072 } else {
2073 loge("setVoiceActivationState fails with invalid subId: " + subId);
2074 }
2075 }
2076
2077 /**
2078 * Sets the data activation state of a given subId.
2079 */
2080 @Override
2081 public void setDataActivationState(int subId, int activationState) {
2082 enforceModifyPermissionOrCarrierPrivilege(subId);
2083 final Phone phone = getPhone(subId);
2084 if (phone != null) {
2085 phone.setDataActivationState(activationState);
2086 } else {
2087 loge("setVoiceActivationState fails with invalid subId: " + subId);
2088 }
2089 }
2090
2091 /**
2092 * Returns the voice activation state of a given subId.
2093 */
2094 @Override
2095 public int getVoiceActivationState(int subId, String callingPackage) {
2096 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2097 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2098 }
2099 final Phone phone = getPhone(subId);
2100 if (phone != null) {
2101 return phone.getVoiceActivationState();
2102 } else {
2103 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2104 }
2105 }
2106
2107 /**
2108 * Returns the data activation state of a given subId.
2109 */
2110 @Override
2111 public int getDataActivationState(int subId, String callingPackage) {
2112 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2113 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2114 }
2115 final Phone phone = getPhone(subId);
2116 if (phone != null) {
2117 return phone.getDataActivationState();
2118 } else {
2119 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2120 }
2121 }
2122
2123 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002124 * Returns the unread count of voicemails
2125 */
2126 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002127 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002128 }
2129
2130 /**
2131 * Returns the unread count of voicemails for a subId
2132 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002133 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002134 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002135 final Phone phone = getPhone(subId);
2136 if (phone != null) {
2137 return phone.getVoiceMessageCount();
2138 } else {
2139 return 0;
2140 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002141 }
2142
2143 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002144 * Returns the data network type.
2145 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002146 *
2147 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2148 */
2149 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002150 public int getNetworkType() {
2151 final Phone phone = getPhone(getDefaultSubscription());
2152 if (phone != null) {
2153 return phone.getServiceState().getDataNetworkType();
2154 } else {
2155 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2156 }
Wink Saville36469e72014-06-11 15:17:00 -07002157 }
2158
2159 /**
2160 * Returns the network type for a subId
2161 */
2162 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002163 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2164 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2165 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2166 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002167
Sanket Padawe356d7632015-06-22 14:03:32 -07002168 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002169 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002170 return phone.getServiceState().getDataNetworkType();
2171 } else {
2172 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2173 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002174 }
2175
2176 /**
2177 * Returns the data network type
2178 */
2179 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002180 public int getDataNetworkType(String callingPackage) {
2181 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002182 }
2183
2184 /**
2185 * Returns the data network type for a subId
2186 */
2187 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002188 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2189 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2190 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2191 }
2192
Sanket Padawe356d7632015-06-22 14:03:32 -07002193 final Phone phone = getPhone(subId);
2194 if (phone != null) {
2195 return phone.getServiceState().getDataNetworkType();
2196 } else {
2197 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2198 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002199 }
2200
2201 /**
Wink Saville36469e72014-06-11 15:17:00 -07002202 * Returns the Voice network type for a subId
2203 */
2204 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002205 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2206 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2207 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2208 }
2209
Sanket Padawe356d7632015-06-22 14:03:32 -07002210 final Phone phone = getPhone(subId);
2211 if (phone != null) {
2212 return phone.getServiceState().getVoiceNetworkType();
2213 } else {
2214 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2215 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002216 }
2217
2218 /**
2219 * @return true if a ICC card is present
2220 */
2221 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002222 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002223 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2224 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002225 }
2226
2227 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002228 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002229 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002230 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002231 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2232 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002233 final Phone phone = getPhone(subId[0]);
2234 if (subId != null && phone != null) {
2235 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002236 } else {
2237 return false;
2238 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002239 }
2240
2241 /**
2242 * Return if the current radio is LTE on CDMA. This
2243 * is a tri-state return value as for a period of time
2244 * the mode may be unknown.
2245 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002246 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002247 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002248 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002249 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002250 @Override
2251 public int getLteOnCdmaMode(String callingPackage) {
2252 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002253 }
2254
Sanket Padawe356d7632015-06-22 14:03:32 -07002255 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002256 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2257 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2258 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2259 }
2260
Sanket Padawe356d7632015-06-22 14:03:32 -07002261 final Phone phone = getPhone(subId);
2262 if (phone == null) {
2263 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2264 } else {
2265 return phone.getLteOnCdmaMode();
2266 }
Wink Saville36469e72014-06-11 15:17:00 -07002267 }
2268
2269 public void setPhone(Phone phone) {
2270 mPhone = phone;
2271 }
2272
2273 /**
2274 * {@hide}
2275 * Returns Default subId, 0 in the case of single standby.
2276 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002277 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002278 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002279 }
2280
Shishir Agrawala9f32182016-04-12 12:00:16 -07002281 private int getSlotForDefaultSubscription() {
2282 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2283 }
2284
Wink Savilleb564aae2014-10-23 10:18:09 -07002285 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002286 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002287 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002288
2289 /**
2290 * @see android.telephony.TelephonyManager.WifiCallingChoices
2291 */
2292 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002293 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2294 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002295 }
2296
2297 /**
2298 * @see android.telephony.TelephonyManager.WifiCallingChoices
2299 */
2300 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002301 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2302 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2303 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002304 }
2305
Sailesh Nepald1e68152013-12-12 19:08:02 -08002306 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002307 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002308 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002309 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002310
Shishir Agrawal566b7612013-10-28 14:41:00 -07002311 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002312 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002313 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002314
Ajay Nambiabd73502015-12-03 13:50:00 -08002315 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002316 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002317 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002318 if (DBG) log("iccOpenLogicalChannel: " + response);
2319 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002320 }
2321
2322 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002323 public boolean iccCloseLogicalChannel(int subId, int channel) {
2324 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002325
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002326 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002327 if (channel < 0) {
2328 return false;
2329 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002330 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002331 if (DBG) log("iccCloseLogicalChannel: " + success);
2332 return success;
2333 }
2334
2335 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002336 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002337 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002338 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002339
2340 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002341 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2342 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002343 " data=" + data);
2344 }
2345
2346 if (channel < 0) {
2347 return "";
2348 }
2349
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002350 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002351 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002352 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2353
Shishir Agrawal566b7612013-10-28 14:41:00 -07002354 // Append the returned status code to the end of the response payload.
2355 String s = Integer.toHexString(
2356 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002357 if (response.payload != null) {
2358 s = IccUtils.bytesToHexString(response.payload) + s;
2359 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002360 return s;
2361 }
Jake Hambye994d462014-02-03 13:10:13 -08002362
Evan Charltonc66da362014-05-16 14:06:40 -07002363 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002364 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002365 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002366 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002367
2368 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002369 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2370 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002371 }
2372
2373 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002374 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002375 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2376
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002377 // Append the returned status code to the end of the response payload.
2378 String s = Integer.toHexString(
2379 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002380 if (response.payload != null) {
2381 s = IccUtils.bytesToHexString(response.payload) + s;
2382 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002383 return s;
2384 }
2385
2386 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002387 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002388 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002389 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002390
2391 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002392 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002393 p1 + " " + p2 + " " + p3 + ":" + filePath);
2394 }
2395
2396 IccIoResult response =
2397 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002398 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2399 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002400
2401 if (DBG) {
2402 log("Exchange SIM_IO [R]" + response);
2403 }
2404
2405 byte[] result = null;
2406 int length = 2;
2407 if (response.payload != null) {
2408 length = 2 + response.payload.length;
2409 result = new byte[length];
2410 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2411 } else {
2412 result = new byte[length];
2413 }
2414
2415 result[length - 1] = (byte) response.sw2;
2416 result[length - 2] = (byte) response.sw1;
2417 return result;
2418 }
2419
Nathan Haroldb3014052017-01-25 15:57:32 -08002420 /**
2421 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2422 * on a particular subscription
2423 */
2424 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002425 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2426 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002427 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2428 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2429 return null;
2430 }
2431 Object response = sendRequest(
2432 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2433 if (response instanceof String[]) {
2434 return (String[]) response;
2435 }
2436 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2437 return null;
2438 }
2439
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002440 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002441 public String sendEnvelopeWithStatus(int subId, String content) {
2442 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002443
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002444 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002445 if (response.payload == null) {
2446 return "";
2447 }
2448
2449 // Append the returned status code to the end of the response payload.
2450 String s = Integer.toHexString(
2451 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2452 s = IccUtils.bytesToHexString(response.payload) + s;
2453 return s;
2454 }
2455
Jake Hambye994d462014-02-03 13:10:13 -08002456 /**
2457 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2458 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2459 *
2460 * @param itemID the ID of the item to read
2461 * @return the NV item as a String, or null on error.
2462 */
2463 @Override
2464 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002465 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002466 if (DBG) log("nvReadItem: item " + itemID);
2467 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2468 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2469 return value;
2470 }
2471
2472 /**
2473 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2474 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2475 *
2476 * @param itemID the ID of the item to read
2477 * @param itemValue the value to write, as a String
2478 * @return true on success; false on any failure
2479 */
2480 @Override
2481 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002482 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002483 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2484 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2485 new Pair<Integer, String>(itemID, itemValue));
2486 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2487 return success;
2488 }
2489
2490 /**
2491 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2492 * Used for device configuration by some CDMA operators.
2493 *
2494 * @param preferredRoamingList byte array containing the new PRL
2495 * @return true on success; false on any failure
2496 */
2497 @Override
2498 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002499 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002500 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2501 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2502 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2503 return success;
2504 }
2505
2506 /**
2507 * Perform the specified type of NV config reset.
2508 * Used for device configuration by some CDMA operators.
2509 *
2510 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2511 * @return true on success; false on any failure
2512 */
2513 @Override
2514 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002515 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002516 if (DBG) log("nvResetConfig: type " + resetType);
2517 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2518 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2519 return success;
2520 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002521
2522 /**
Wink Saville36469e72014-06-11 15:17:00 -07002523 * {@hide}
2524 * Returns Default sim, 0 in the case of single standby.
2525 */
2526 public int getDefaultSim() {
2527 //TODO Need to get it from Telephony Devcontroller
2528 return 0;
2529 }
2530
Svet Ganovb320e182015-04-16 12:30:10 -07002531 public String[] getPcscfAddress(String apnType, String callingPackage) {
2532 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2533 return new String[0];
2534 }
2535
2536
ram87fca6f2014-07-18 18:58:44 +05302537 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002538 }
2539
Brad Ebinger76681002017-01-23 13:50:20 -08002540 /**
2541 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2542 * feature or {@link null} if the service is not available. If an ImsServiceController is
2543 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2544 * feature updates.
2545 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002546 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002547 IImsServiceFeatureListener callback) {
2548 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002549 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002550 callback);
2551 }
2552
Wink Saville36469e72014-06-11 15:17:00 -07002553 public void setImsRegistrationState(boolean registered) {
2554 enforceModifyPermission();
2555 mPhone.setImsRegistrationState(registered);
2556 }
2557
2558 /**
Stuart Scott54788802015-03-30 13:18:01 -07002559 * Set the network selection mode to automatic.
2560 *
2561 */
2562 @Override
2563 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002564 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002565 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2566 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2567 }
2568
2569 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002570 * Set the network selection mode to manual with the selected carrier.
2571 */
2572 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002573 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2574 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002575 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002576 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002577 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2578 persistSelection);
2579 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002580 }
2581
2582 /**
2583 * Scans for available networks.
2584 */
2585 @Override
2586 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002587 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002588 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2589 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2590 CMD_PERFORM_NETWORK_SCAN, null, subId);
2591 return result;
2592 }
2593
2594 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002595 * Get the calculated preferred network type.
2596 * Used for debugging incorrect network type.
2597 *
2598 * @return the preferred network type, defined in RILConstants.java.
2599 */
2600 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002601 public int getCalculatedPreferredNetworkType(String callingPackage) {
2602 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2603 return RILConstants.PREFERRED_NETWORK_MODE;
2604 }
2605
Amit Mahajan43330e02014-11-18 11:54:45 -08002606 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002607 }
2608
2609 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002610 * Get the preferred network type.
2611 * Used for device configuration by some CDMA operators.
2612 *
2613 * @return the preferred network type, defined in RILConstants.java.
2614 */
2615 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002616 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002617 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002618 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002619 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002620 int networkType = (result != null ? result[0] : -1);
2621 if (DBG) log("getPreferredNetworkType: " + networkType);
2622 return networkType;
2623 }
2624
2625 /**
2626 * Set the preferred network type.
2627 * Used for device configuration by some CDMA operators.
2628 *
2629 * @param networkType the preferred network type, defined in RILConstants.java.
2630 * @return true on success; false on any failure.
2631 */
2632 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002633 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002634 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002635 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2636 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002637 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002638 if (success) {
2639 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002640 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002641 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002642 return success;
2643 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002644
2645 /**
Junda Liu475951f2014-11-07 16:45:03 -08002646 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2647 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2648 * tethering.
2649 *
2650 * @return 0: Not required. 1: required. 2: Not set.
2651 * @hide
2652 */
2653 @Override
2654 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002655 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002656 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2657 Settings.Global.TETHER_DUN_REQUIRED, 2);
2658 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2659 // config_tether_apndata.
2660 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2661 dunRequired = 1;
2662 }
2663 return dunRequired;
2664 }
2665
2666 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002667 * Set mobile data enabled
2668 * Used by the user through settings etc to turn on/off mobile data
2669 *
2670 * @param enable {@code true} turn turn data on, else {@code false}
2671 */
2672 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002673 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002674 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002675 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002676 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002677 Phone phone = PhoneFactory.getPhone(phoneId);
2678 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002679 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002680 phone.setDataEnabled(enable);
2681 } else {
2682 loge("setDataEnabled: no phone for subId=" + subId);
2683 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002684 }
2685
2686 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002687 * Get whether mobile data is enabled.
2688 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002689 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002690 *
2691 * @return {@code true} if data is enabled else {@code false}
2692 */
2693 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002694 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002695 try {
2696 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2697 null);
2698 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002699 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002700 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002701 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002702 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002703 Phone phone = PhoneFactory.getPhone(phoneId);
2704 if (phone != null) {
2705 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002706 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002707 return retVal;
2708 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002709 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002710 return false;
2711 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002712 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002713
2714 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002715 public int getCarrierPrivilegeStatus(int subId) {
2716 final Phone phone = getPhone(subId);
2717 if (phone == null) {
2718 loge("getCarrierPrivilegeStatus: Invalid subId");
2719 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2720 }
2721 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002722 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002723 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002724 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2725 }
2726 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002727 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002728 }
Junda Liu29340342014-07-10 15:23:27 -07002729
2730 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002731 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002732 if (TextUtils.isEmpty(pkgName))
2733 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002734 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002735 if (card == null) {
2736 loge("checkCarrierPrivilegesForPackage: No UICC");
2737 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2738 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002739 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2740 }
2741
2742 @Override
2743 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002744 if (TextUtils.isEmpty(pkgName))
2745 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002746 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2747 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2748 UiccCard card = UiccController.getInstance().getUiccCard(i);
2749 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002750 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002751 continue;
2752 }
2753
2754 result = card.getCarrierPrivilegeStatus(
2755 mPhone.getContext().getPackageManager(), pkgName);
2756 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2757 break;
2758 }
2759 }
2760
2761 return result;
Junda Liu29340342014-07-10 15:23:27 -07002762 }
Derek Tan89e89d42014-07-08 17:00:10 -07002763
2764 @Override
Junda Liue64de782015-04-16 17:19:16 -07002765 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2766 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2767 loge("phoneId " + phoneId + " is not valid.");
2768 return null;
2769 }
2770 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002771 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002772 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002773 return null ;
2774 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002775 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002776 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002777 }
2778
Amith Yamasani6e118872016-02-19 12:53:51 -08002779 @Override
2780 public List<String> getPackagesWithCarrierPrivileges() {
2781 PackageManager pm = mPhone.getContext().getPackageManager();
2782 List<String> privilegedPackages = new ArrayList<>();
2783 List<PackageInfo> packages = null;
2784 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2785 UiccCard card = UiccController.getInstance().getUiccCard(i);
2786 if (card == null) {
2787 // No UICC in that slot.
2788 continue;
2789 }
2790 if (card.hasCarrierPrivilegeRules()) {
2791 if (packages == null) {
2792 // Only check packages in user 0 for now
2793 packages = pm.getInstalledPackagesAsUser(
2794 PackageManager.MATCH_DISABLED_COMPONENTS
2795 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2796 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2797 }
2798 for (int p = packages.size() - 1; p >= 0; p--) {
2799 PackageInfo pkgInfo = packages.get(p);
2800 if (pkgInfo != null && pkgInfo.packageName != null
2801 && card.getCarrierPrivilegeStatus(pkgInfo)
2802 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2803 privilegedPackages.add(pkgInfo.packageName);
2804 }
2805 }
2806 }
2807 }
2808 return privilegedPackages;
2809 }
2810
Wink Savilleb564aae2014-10-23 10:18:09 -07002811 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002812 final Phone phone = getPhone(subId);
2813 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002814 if (card == null) {
2815 loge("getIccId: No UICC");
2816 return null;
2817 }
2818 String iccId = card.getIccId();
2819 if (TextUtils.isEmpty(iccId)) {
2820 loge("getIccId: ICC ID is null or empty.");
2821 return null;
2822 }
2823 return iccId;
2824 }
2825
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002826 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002827 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2828 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002829 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002830
Jeff Sharkey85190e62014-12-05 09:40:12 -08002831 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002832 final Phone phone = getPhone(subId);
2833 if (phone == null) {
2834 return false;
2835 }
2836 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002837
2838 if (DBG_MERGE) {
2839 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2840 + subscriberId + " to " + number);
2841 }
2842
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002843 if (TextUtils.isEmpty(iccId)) {
2844 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002845 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002846
Jeff Sharkey85190e62014-12-05 09:40:12 -08002847 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2848
2849 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002850 if (alphaTag == null) {
2851 editor.remove(alphaTagPrefKey);
2852 } else {
2853 editor.putString(alphaTagPrefKey, alphaTag);
2854 }
2855
Jeff Sharkey85190e62014-12-05 09:40:12 -08002856 // Record both the line number and IMSI for this ICCID, since we need to
2857 // track all merged IMSIs based on line number
2858 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2859 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002860 if (number == null) {
2861 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002862 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002863 } else {
2864 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002865 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002866 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002867
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002868 editor.commit();
2869 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002870 }
2871
2872 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002873 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002874 // This is open to apps with WRITE_SMS.
2875 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002876 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002877 return null;
2878 }
Derek Tan97ebb422014-09-05 16:55:38 -07002879
2880 String iccId = getIccId(subId);
2881 if (iccId != null) {
2882 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002883 if (DBG_MERGE) {
2884 log("getLine1NumberForDisplay returning " +
2885 mTelephonySharedPreferences.getString(numberPrefKey, null));
2886 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002887 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002888 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002889 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002890 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002891 }
2892
2893 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002894 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2895 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2896 return null;
2897 }
Derek Tan97ebb422014-09-05 16:55:38 -07002898
2899 String iccId = getIccId(subId);
2900 if (iccId != null) {
2901 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002902 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002903 }
Derek Tan97ebb422014-09-05 16:55:38 -07002904 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002905 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002906
2907 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002908 public String[] getMergedSubscriberIds(String callingPackage) {
2909 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2910 return null;
2911 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002912 final Context context = mPhone.getContext();
2913 final TelephonyManager tele = TelephonyManager.from(context);
2914 final SubscriptionManager sub = SubscriptionManager.from(context);
2915
2916 // Figure out what subscribers are currently active
2917 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002918 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2919 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2920 final long identity = Binder.clearCallingIdentity();
2921 try {
2922 final int[] subIds = sub.getActiveSubscriptionIdList();
2923 for (int subId : subIds) {
2924 activeSubscriberIds.add(tele.getSubscriberId(subId));
2925 }
2926 } finally {
2927 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002928 }
2929
2930 // First pass, find a number override for an active subscriber
2931 String mergeNumber = null;
2932 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2933 for (String key : prefs.keySet()) {
2934 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2935 final String subscriberId = (String) prefs.get(key);
2936 if (activeSubscriberIds.contains(subscriberId)) {
2937 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2938 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2939 mergeNumber = (String) prefs.get(numberKey);
2940 if (DBG_MERGE) {
2941 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2942 + " for active subscriber " + subscriberId);
2943 }
2944 if (!TextUtils.isEmpty(mergeNumber)) {
2945 break;
2946 }
2947 }
2948 }
2949 }
2950
2951 // Shortcut when no active merged subscribers
2952 if (TextUtils.isEmpty(mergeNumber)) {
2953 return null;
2954 }
2955
2956 // Second pass, find all subscribers under that line override
2957 final ArraySet<String> result = new ArraySet<>();
2958 for (String key : prefs.keySet()) {
2959 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2960 final String number = (String) prefs.get(key);
2961 if (mergeNumber.equals(number)) {
2962 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2963 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2964 final String subscriberId = (String) prefs.get(subscriberKey);
2965 if (!TextUtils.isEmpty(subscriberId)) {
2966 result.add(subscriberId);
2967 }
2968 }
2969 }
2970 }
2971
2972 final String[] resultArray = result.toArray(new String[result.size()]);
2973 Arrays.sort(resultArray);
2974 if (DBG_MERGE) {
2975 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2976 }
2977 return resultArray;
2978 }
2979
2980 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002981 public boolean setOperatorBrandOverride(int subId, String brand) {
2982 enforceCarrierPrivilege(subId);
2983 final Phone phone = getPhone(subId);
2984 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002985 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002986
2987 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002988 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002989 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2990 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002991 enforceCarrierPrivilege(subId);
2992 final Phone phone = getPhone(subId);
2993 if (phone == null) {
2994 return false;
2995 }
2996 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002997 cdmaNonRoamingList);
2998 }
2999
3000 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003001 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003002 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3003 enforceModifyPermission();
3004
3005 int returnValue = 0;
3006 try {
3007 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3008 if(result.exception == null) {
3009 if (result.result != null) {
3010 byte[] responseData = (byte[])(result.result);
3011 if(responseData.length > oemResp.length) {
3012 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3013 responseData.length + "bytes. Buffer Size is " +
3014 oemResp.length + "bytes.");
3015 }
3016 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3017 returnValue = responseData.length;
3018 }
3019 } else {
3020 CommandException ex = (CommandException) result.exception;
3021 returnValue = ex.getCommandError().ordinal();
3022 if(returnValue > 0) returnValue *= -1;
3023 }
3024 } catch (RuntimeException e) {
3025 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3026 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3027 if(returnValue > 0) returnValue *= -1;
3028 }
3029
3030 return returnValue;
3031 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003032
3033 @Override
3034 public void setRadioCapability(RadioAccessFamily[] rafs) {
3035 try {
3036 ProxyController.getInstance().setRadioCapability(rafs);
3037 } catch (RuntimeException e) {
3038 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3039 }
3040 }
3041
3042 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003043 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3044 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3045 return RadioAccessFamily.RAF_UNKNOWN;
3046 }
3047
Wink Saville5d475dd2014-10-17 15:00:58 -07003048 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3049 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003050
3051 @Override
3052 public void enableVideoCalling(boolean enable) {
3053 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003054 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003055 }
3056
3057 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003058 public boolean isVideoCallingEnabled(String callingPackage) {
3059 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3060 return false;
3061 }
3062
Andrew Lee77527ac2014-10-21 16:57:39 -07003063 // Check the user preference and the system-level IMS setting. Even if the user has
3064 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3065 // In the long run, we may instead need to check if there exists a connection service
3066 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003067 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3068 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003069 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003070 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003071
Andrew Leea1239f22015-03-02 17:44:07 -08003072 @Override
3073 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003074 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003075 }
3076
3077 @Override
3078 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003079 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003080 }
3081
Andrew Lee9431b832015-03-09 18:46:45 -07003082 @Override
3083 public boolean isTtyModeSupported() {
3084 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3085 TelephonyManager telephonyManager =
3086 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003087 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003088 }
3089
3090 @Override
3091 public boolean isHearingAidCompatibilitySupported() {
3092 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3093 }
3094
Sanket Padawe7310cc72015-01-14 09:53:20 -08003095 /**
3096 * Returns the unique device ID of phone, for example, the IMEI for
3097 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3098 *
3099 * <p>Requires Permission:
3100 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3101 */
3102 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003103 public String getDeviceId(String callingPackage) {
3104 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3105 return null;
3106 }
3107
Sanket Padawe7310cc72015-01-14 09:53:20 -08003108 final Phone phone = PhoneFactory.getPhone(0);
3109 if (phone != null) {
3110 return phone.getDeviceId();
3111 } else {
3112 return null;
3113 }
3114 }
3115
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003116 /*
3117 * {@hide}
3118 * Returns the IMS Registration Status
3119 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003120 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003121 public boolean isImsRegistered() {
3122 return mPhone.isImsRegistered();
3123 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003124
3125 @Override
3126 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3127 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3128 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003129
Nathan Haroldc55097a2015-03-11 18:14:50 -07003130 /*
3131 * {@hide}
3132 * Returns the IMS Registration Status
3133 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003134 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003135 return mPhone.isWifiCallingEnabled();
3136 }
3137
3138 /*
3139 * {@hide}
3140 * Returns the IMS Registration Status
3141 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003142 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003143 return mPhone.isVolteEnabled();
3144 }
Svet Ganovb320e182015-04-16 12:30:10 -07003145
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003146 /*
3147 * {@hide} Returns the IMS Registration Status
3148 */
3149 public boolean isVideoTelephonyAvailable() {
3150 return mPhone.isVideoEnabled();
3151 }
3152
Svet Ganovb320e182015-04-16 12:30:10 -07003153 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003154 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003155 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003156 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3157
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003158 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003159 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003160 } catch (SecurityException e) {
3161 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3162 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003163 }
Svet Ganovb320e182015-04-16 12:30:10 -07003164
3165 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3166 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3167 return false;
3168 }
3169
3170 return true;
3171 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003172
Makoto Onukifee69342015-06-29 14:44:50 -07003173 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003174 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003175 */
3176 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003177 // Default SMS app can always read it.
3178 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3179 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3180 return true;
3181 }
3182 try {
3183 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003184 } catch (SecurityException readPhoneStateSecurityException) {
3185 try {
3186 // Can be read with READ_SMS too.
3187 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3188 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3189 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3190 } catch (SecurityException readSmsSecurityException) {
3191 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3192 // permissions
3193 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3194 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3195 " or " + android.Manifest.permission.READ_SMS + ".");
3196 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003197 }
Makoto Onukifee69342015-06-29 14:44:50 -07003198 }
3199
Stuart Scott8eef64f2015-04-08 15:13:54 -07003200 @Override
3201 public void factoryReset(int subId) {
3202 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003203 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3204 return;
3205 }
3206
Svet Ganovcc087f82015-05-12 20:35:54 -07003207 final long identity = Binder.clearCallingIdentity();
3208 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003209 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3210 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003211 // Enable data
3212 setDataEnabled(subId, true);
3213 // Set network selection mode to automatic
3214 setNetworkSelectionModeAutomatic(subId);
3215 // Set preferred mobile network type to the best available
3216 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3217 // Turn off roaming
3218 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3219 }
3220 } finally {
3221 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003222 }
3223 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003224
3225 @Override
3226 public String getLocaleFromDefaultSim() {
3227 // We query all subscriptions instead of just the active ones, because
3228 // this might be called early on in the provisioning flow when the
3229 // subscriptions potentially aren't active yet.
3230 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3231 if (slist == null || slist.isEmpty()) {
3232 return null;
3233 }
3234
3235 // This function may be called very early, say, from the setup wizard, at
3236 // which point we won't have a default subscription set. If that's the case
3237 // we just choose the first, which will be valid in "most cases".
3238 final int defaultSubId = getDefaultSubscription();
3239 SubscriptionInfo info = null;
3240 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3241 info = slist.get(0);
3242 } else {
3243 for (SubscriptionInfo item : slist) {
3244 if (item.getSubscriptionId() == defaultSubId) {
3245 info = item;
3246 break;
3247 }
3248 }
3249
3250 if (info == null) {
3251 return null;
3252 }
3253 }
3254
3255 // Try and fetch the locale from the carrier properties or from the SIM language
3256 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003257 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003258 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003259 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003260 if (defaultPhone != null) {
3261 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3262 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003263 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003264 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3265 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003266 } else {
3267 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003268 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003269 }
3270 }
3271
Narayan Kamath011676f2015-07-29 12:04:08 +01003272 // The SIM language preferences only store a language (e.g. fr = French), not an
3273 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3274 // the SIM and carrier preferences does not include a country we add the country
3275 // determined from the SIM MCC to provide an exact locale.
3276 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003277 if (mccLocale != null) {
3278 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3279 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003280 }
3281
Tony Hill183b2de2015-06-24 14:53:58 +01003282 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003283 return null;
3284 }
3285
3286 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3287 final long identity = Binder.clearCallingIdentity();
3288 try {
3289 return mSubscriptionController.getAllSubInfoList(
3290 mPhone.getContext().getOpPackageName());
3291 } finally {
3292 Binder.restoreCallingIdentity(identity);
3293 }
3294 }
3295
3296 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3297 final long identity = Binder.clearCallingIdentity();
3298 try {
3299 return mSubscriptionController.getActiveSubscriptionInfoList(
3300 mPhone.getContext().getOpPackageName());
3301 } finally {
3302 Binder.restoreCallingIdentity(identity);
3303 }
3304 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003305
3306 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003307 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3308 * representing the state of the modem.
3309 *
3310 * NOTE: This clears the modem state, so there should only every be one caller.
3311 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003312 */
3313 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003314 public void requestModemActivityInfo(ResultReceiver result) {
3315 enforceModifyPermission();
3316
3317 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3318 Bundle bundle = new Bundle();
3319 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3320 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003321 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003322
3323 /**
3324 * {@hide}
3325 * Returns the service state information on specified subscription.
3326 */
3327 @Override
3328 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3329
3330 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3331 return null;
3332 }
3333
3334 final Phone phone = getPhone(subId);
3335 if (phone == null) {
3336 return null;
3337 }
3338
3339 return phone.getServiceState();
3340 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003341
3342 /**
3343 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3344 *
3345 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3346 * voicemail ringtone.
3347 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3348 * PhoneAccount.
3349 */
3350 @Override
3351 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3352 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3353 if (phone == null) {
3354 return null;
3355 }
3356
3357 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3358 }
3359
3360 /**
3361 * Returns whether vibration is set for voicemail notification in Phone settings.
3362 *
3363 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3364 * voicemail vibration setting.
3365 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3366 */
3367 @Override
3368 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3369 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3370 if (phone == null) {
3371 return false;
3372 }
3373
3374 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3375 }
3376
Youhan Wange64578a2016-05-02 15:32:42 -07003377 /**
3378 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3379 *
3380 * @throws SecurityException if the caller does not have the required permission
3381 */
3382 private void enforceReadPrivilegedPermission() {
3383 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3384 null);
3385 }
3386
3387 /**
3388 * Return the application ID for the app type.
3389 *
3390 * @param subId the subscription ID that this request applies to.
3391 * @param appType the uicc app type.
3392 * @return Application ID for specificied app type, or null if no uicc.
3393 */
3394 @Override
3395 public String getAidForAppType(int subId, int appType) {
3396 enforceReadPrivilegedPermission();
3397 Phone phone = getPhone(subId);
3398 if (phone == null) {
3399 return null;
3400 }
3401 String aid = null;
3402 try {
3403 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3404 .getApplicationByType(appType).getAid();
3405 } catch (Exception e) {
3406 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3407 }
3408 return aid;
3409 }
3410
Youhan Wang4001d252016-05-11 10:29:41 -07003411 /**
3412 * Return the Electronic Serial Number.
3413 *
3414 * @param subId the subscription ID that this request applies to.
3415 * @return ESN or null if error.
3416 */
3417 @Override
3418 public String getEsn(int subId) {
3419 enforceReadPrivilegedPermission();
3420 Phone phone = getPhone(subId);
3421 if (phone == null) {
3422 return null;
3423 }
3424 String esn = null;
3425 try {
3426 esn = phone.getEsn();
3427 } catch (Exception e) {
3428 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3429 }
3430 return esn;
3431 }
3432
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003433 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003434 * Return the Preferred Roaming List Version.
3435 *
3436 * @param subId the subscription ID that this request applies to.
3437 * @return PRLVersion or null if error.
3438 */
3439 @Override
3440 public String getCdmaPrlVersion(int subId) {
3441 enforceReadPrivilegedPermission();
3442 Phone phone = getPhone(subId);
3443 if (phone == null) {
3444 return null;
3445 }
3446 String cdmaPrlVersion = null;
3447 try {
3448 cdmaPrlVersion = phone.getCdmaPrlVersion();
3449 } catch (Exception e) {
3450 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3451 }
3452 return cdmaPrlVersion;
3453 }
3454
3455 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003456 * Get snapshot of Telephony histograms
3457 * @return List of Telephony histograms
3458 * @hide
3459 */
3460 @Override
3461 public List<TelephonyHistogram> getTelephonyHistograms() {
3462 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3463 return RIL.getTelephonyRILTimingHistograms();
3464 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003465
3466 /**
3467 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003468 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003469 * Require system privileges. In the future we may add this to carrier APIs.
3470 *
3471 * @return The number of carriers set successfully, should match length of carriers
3472 */
3473 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003474 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003475 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003476 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003477 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3478 return retVal[0];
3479 }
3480
3481 /**
3482 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003483 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003484 * Require system privileges. In the future we may add this to carrier APIs.
3485 *
3486 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3487 * means all carriers are allowed.
3488 */
3489 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003490 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003491 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003492 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003493 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3494 }
3495
fionaxu59545b42016-05-25 15:53:37 -07003496 /**
3497 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3498 * @param subId the subscription ID that this action applies to.
3499 * @param enabled control enable or disable metered apns.
3500 * {@hide}
3501 */
3502 @Override
3503 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3504 enforceModifyPermission();
3505 final Phone phone = getPhone(subId);
3506 if (phone == null) {
3507 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3508 return;
3509 }
3510 try {
3511 phone.carrierActionSetMeteredApnsEnabled(enabled);
3512 } catch (Exception e) {
3513 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3514 }
3515 }
3516
3517 /**
3518 * Action set from carrier signalling broadcast receivers to enable/disable radio
3519 * @param subId the subscription ID that this action applies to.
3520 * @param enabled control enable or disable radio.
3521 * {@hide}
3522 */
3523 @Override
3524 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3525 enforceModifyPermission();
3526 final Phone phone = getPhone(subId);
3527 if (phone == null) {
3528 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3529 return;
3530 }
3531 try {
3532 phone.carrierActionSetRadioEnabled(enabled);
3533 } catch (Exception e) {
3534 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3535 }
3536 }
3537
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003538 /**
3539 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3540 * bug report is being generated.
3541 */
3542 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003543 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003544 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3545 != PackageManager.PERMISSION_GRANTED) {
3546 writer.println("Permission Denial: can't dump Phone from pid="
3547 + Binder.getCallingPid()
3548 + ", uid=" + Binder.getCallingUid()
3549 + "without permission "
3550 + android.Manifest.permission.DUMP);
3551 return;
3552 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003553 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003554 }
Jack Yueb89b242016-06-22 13:27:47 -07003555
3556 /**
3557 * Get aggregated video call data usage from all subscriptions since boot.
3558 * @return total data usage in bytes
3559 * {@hide}
3560 */
3561 @Override
3562 public long getVtDataUsage() {
3563 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3564 null);
3565
3566 // NetworkStatsService keeps tracking the active network interface and identity. It will
3567 // record the delta with the corresponding network identity. What we need to do here is
3568 // returning total video call data usage from all subscriptions since boot.
3569
3570 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3571 // simultaneous VT calls.
3572 final Phone[] phones = PhoneFactory.getPhones();
3573 long total = 0;
3574 for (Phone phone : phones) {
3575 total += phone.getVtDataUsage();
3576 }
3577 return total;
3578 }
Jack Yu75ab2952016-07-08 14:29:33 -07003579
3580 /**
3581 * Policy control of data connection. Usually used when data limit is passed.
3582 * @param enabled True if enabling the data, otherwise disabling.
3583 * @param subId Subscription index
3584 * {@hide}
3585 */
3586 @Override
3587 public void setPolicyDataEnabled(boolean enabled, int subId) {
3588 enforceModifyPermission();
3589 Phone phone = getPhone(subId);
3590 if (phone != null) {
3591 phone.setPolicyDataEnabled(enabled);
3592 }
3593 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003594
3595 /**
3596 * Get Client request stats
3597 * @return List of Client Request Stats
3598 * @hide
3599 */
3600 @Override
3601 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3602 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3603 return null;
3604 }
3605
3606 Phone phone = getPhone(subId);
3607 if (phone != null) {
3608 return phone.getClientRequestStats();
3609 }
3610
3611 return null;
3612 }
3613
3614 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3615 if (workSource != null) {
3616 return workSource;
3617 }
3618
3619 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3620 workSource = new WorkSource(uid, packageName);
3621 return workSource;
3622 }
Jack Yu346de222017-02-16 15:32:43 -08003623
3624 /**
3625 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3626 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003627 * @param slotIndex SIM slot id.
Jack Yu346de222017-02-16 15:32:43 -08003628 * @param powerUp True if powering up the SIM, otherwise powering down
3629 *
3630 **/
3631 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003632 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yu346de222017-02-16 15:32:43 -08003633 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003634 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003635 if (subId == null || subId.length == 0) {
3636 return;
3637 }
3638
3639 final Phone phone = getPhone(subId[0]);
3640 if (phone != null) {
3641 phone.setSimPowerState(powerUp);
3642 }
3643 }
shuoqfef36a32017-01-10 13:02:18 -08003644
3645 /**
3646 * Check if phone is in emergency callback mode
3647 * @return true if phone is in emergency callback mode
3648 * @param subId sub id
3649 */
3650 public boolean getEmergencyCallbackMode(int subId) {
3651 final Phone phone = getPhone(subId);
3652 if (phone != null) {
3653 return phone.isInEcm();
3654 } else {
3655 return false;
3656 }
3657 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003658}