blob: 9c92a047b26390e78aa36cf27f9bf74f502d96d3 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Ta-wei Yen30a69c82016-12-27 14:52:32 -080021import android.Manifest.permission;
Ta-wei Yendca928f2017-01-10 16:17:08 -080022import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.ActivityManager;
24import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080025import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070026import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.Context;
28import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070029import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080030import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070031import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.net.Uri;
33import android.os.AsyncResult;
34import android.os.Binder;
35import android.os.Bundle;
36import android.os.Handler;
37import android.os.Looper;
38import android.os.Message;
39import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070040import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.ServiceManager;
42import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070043import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070044import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070045import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080046import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070047import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080048import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080049import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070050import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070051import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.CellInfo;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070053import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070054import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070055import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080056import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070057import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.telephony.ServiceState;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080059import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080060import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080061import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070062import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070063import android.telephony.TelephonyManager;
64import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080066import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080068import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080069import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080070
Andrew Lee312e8172014-10-23 17:01:36 -070071import com.android.ims.ImsManager;
Brad Ebinger51f743a2017-01-23 13:50:20 -080072import com.android.ims.internal.IImsServiceController;
73import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070074import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070075import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070076import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080079import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010080import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070081import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070083import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070084import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070085import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070086import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070087import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080088import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070089import com.android.internal.telephony.uicc.IccIoResult;
90import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070091import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070092import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080093import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070094import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080095import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yen527a9c02017-01-06 15:29:25 -080096import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080097
Ta-wei Yenc236d6b2016-06-21 13:33:12 -070098import java.io.FileDescriptor;
99import java.io.PrintWriter;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800100import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700101import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800102import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800103import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100104import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800105import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106
107/**
108 * Implementation of the ITelephony interface.
109 */
Santos Cordon117fee72014-05-16 17:56:12 -0700110public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111 private static final String LOG_TAG = "PhoneInterfaceManager";
112 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
113 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800114 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115
116 // Message codes used with mMainThreadHandler
117 private static final int CMD_HANDLE_PIN_MMI = 1;
118 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
119 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
120 private static final int CMD_ANSWER_RINGING_CALL = 4;
121 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700122 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
123 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700124 private static final int CMD_OPEN_CHANNEL = 9;
125 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
126 private static final int CMD_CLOSE_CHANNEL = 11;
127 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800128 private static final int CMD_NV_READ_ITEM = 13;
129 private static final int EVENT_NV_READ_ITEM_DONE = 14;
130 private static final int CMD_NV_WRITE_ITEM = 15;
131 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
132 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
133 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
134 private static final int CMD_NV_RESET_CONFIG = 19;
135 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800136 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
137 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
138 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
139 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800140 private static final int CMD_SEND_ENVELOPE = 25;
141 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700142 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
143 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
144 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
145 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
146 private static final int CMD_EXCHANGE_SIM_IO = 31;
147 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800148 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
149 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700150 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
151 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700152 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
153 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700154 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
155 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
156 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
157 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700158 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
159 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
160 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
161 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700162 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163
164 /** The singleton instance. */
165 private static PhoneInterfaceManager sInstance;
166
Wink Saville3ab207e2014-11-20 13:07:20 -0800167 private PhoneGlobals mApp;
168 private Phone mPhone;
169 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700170 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800171 private AppOpsManager mAppOps;
172 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800173 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800174 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700175
Derek Tan97ebb422014-09-05 16:55:38 -0700176 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
177 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800178 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700179
180 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700181 * A request object to use for transmitting data to an ICC.
182 */
183 private static final class IccAPDUArgument {
184 public int channel, cla, command, p1, p2, p3;
185 public String data;
186
187 public IccAPDUArgument(int channel, int cla, int command,
188 int p1, int p2, int p3, String data) {
189 this.channel = channel;
190 this.cla = cla;
191 this.command = command;
192 this.p1 = p1;
193 this.p2 = p2;
194 this.p3 = p3;
195 this.data = data;
196 }
197 }
198
199 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700200 * A request object to use for transmitting data to an ICC.
201 */
202 private static final class ManualNetworkSelectionArgument {
203 public OperatorInfo operatorInfo;
204 public boolean persistSelection;
205
206 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
207 this.operatorInfo = operatorInfo;
208 this.persistSelection = persistSelection;
209 }
210 }
211
212 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700213 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
214 * request after sending. The main thread will notify the request when it is complete.
215 */
216 private static final class MainThreadRequest {
217 /** The argument to use for the request */
218 public Object argument;
219 /** The result of the request that is run on the main thread */
220 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800221 // The subscriber id that this request applies to. Defaults to
222 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
223 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224
225 public MainThreadRequest(Object argument) {
226 this.argument = argument;
227 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800228
229 public MainThreadRequest(Object argument, Integer subId) {
230 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800231 if (subId != null) {
232 this.subId = subId;
233 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800234 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700235 }
236
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800237 private static final class IncomingThirdPartyCallArgs {
238 public final ComponentName component;
239 public final String callId;
240 public final String callerDisplayName;
241
242 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
243 String callerDisplayName) {
244 this.component = component;
245 this.callId = callId;
246 this.callerDisplayName = callerDisplayName;
247 }
248 }
249
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250 /**
251 * A handler that processes messages on the main thread in the phone process. Since many
252 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
253 * inbound binder threads to the main thread in the phone process. The Binder thread
254 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
255 * on, which will be notified when the operation completes and will contain the result of the
256 * request.
257 *
258 * <p>If a MainThreadRequest object is provided in the msg.obj field,
259 * note that request.result must be set to something non-null for the calling thread to
260 * unblock.
261 */
262 private final class MainThreadHandler extends Handler {
263 @Override
264 public void handleMessage(Message msg) {
265 MainThreadRequest request;
266 Message onCompleted;
267 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800268 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700269 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700270
271 switch (msg.what) {
pkanwar32d516d2016-10-14 19:37:38 -0700272 case CMD_HANDLE_USSD_REQUEST: {
273 request = (MainThreadRequest) msg.obj;
274 final Phone phone = getPhoneFromRequest(request);
275 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
276 String ussdRequest = ussdObject.first;
277 ResultReceiver wrappedCallback = ussdObject.second;
278 request.result = phone != null ?
279 phone.handleUssdRequest(ussdRequest, wrappedCallback)
280 :false;
281 // Wake up the requesting thread
282 synchronized (request) {
283 request.notifyAll();
284 }
285 break;
286 }
287
Yorke Lee716f67e2015-06-17 15:39:16 -0700288 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700290 final Phone phone = getPhoneFromRequest(request);
291 request.result = phone != null ?
292 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
293 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294 // Wake up the requesting thread
295 synchronized (request) {
296 request.notifyAll();
297 }
298 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700299 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300
301 case CMD_HANDLE_NEIGHBORING_CELL:
302 request = (MainThreadRequest) msg.obj;
303 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
304 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700305 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 break;
307
308 case EVENT_NEIGHBORING_CELL_DONE:
309 ar = (AsyncResult) msg.obj;
310 request = (MainThreadRequest) ar.userObj;
311 if (ar.exception == null && ar.result != null) {
312 request.result = ar.result;
313 } else {
314 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800315 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700316 }
317 // Wake up the requesting thread
318 synchronized (request) {
319 request.notifyAll();
320 }
321 break;
322
323 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700324 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800325 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700326 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700327 break;
328
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329 case CMD_END_CALL:
330 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800331 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700332 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700333 Phone phone = getPhone(end_subId);
334 if (phone == null) {
335 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
336 break;
337 }
338 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
340 // CDMA: If the user presses the Power button we treat it as
341 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700342 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
344 // GSM: End the call as per the Phone state
345 hungUp = PhoneUtils.hangup(mCM);
346 } else {
347 throw new IllegalStateException("Unexpected phone type: " + phoneType);
348 }
349 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
350 request.result = hungUp;
351 // Wake up the requesting thread
352 synchronized (request) {
353 request.notifyAll();
354 }
355 break;
356
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700357 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700358 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700359 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800360 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700361 if (uiccCard == null) {
362 loge("iccTransmitApduLogicalChannel: No UICC");
363 request.result = new IccIoResult(0x6F, 0, (byte[])null);
364 synchronized (request) {
365 request.notifyAll();
366 }
367 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700368 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
369 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700370 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700371 iccArgument.channel, iccArgument.cla, iccArgument.command,
372 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700373 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700374 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700375 break;
376
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700377 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700378 ar = (AsyncResult) msg.obj;
379 request = (MainThreadRequest) ar.userObj;
380 if (ar.exception == null && ar.result != null) {
381 request.result = ar.result;
382 } else {
383 request.result = new IccIoResult(0x6F, 0, (byte[])null);
384 if (ar.result == null) {
385 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800386 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700387 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800388 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700389 } else {
390 loge("iccTransmitApduLogicalChannel: Unknown exception");
391 }
392 }
393 synchronized (request) {
394 request.notifyAll();
395 }
396 break;
397
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700398 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
399 request = (MainThreadRequest) msg.obj;
400 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800401 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700402 if (uiccCard == null) {
403 loge("iccTransmitApduBasicChannel: No UICC");
404 request.result = new IccIoResult(0x6F, 0, (byte[])null);
405 synchronized (request) {
406 request.notifyAll();
407 }
408 } else {
409 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
410 request);
411 uiccCard.iccTransmitApduBasicChannel(
412 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
413 iccArgument.p3, iccArgument.data, onCompleted);
414 }
415 break;
416
417 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
418 ar = (AsyncResult) msg.obj;
419 request = (MainThreadRequest) ar.userObj;
420 if (ar.exception == null && ar.result != null) {
421 request.result = ar.result;
422 } else {
423 request.result = new IccIoResult(0x6F, 0, (byte[])null);
424 if (ar.result == null) {
425 loge("iccTransmitApduBasicChannel: Empty response");
426 } else if (ar.exception instanceof CommandException) {
427 loge("iccTransmitApduBasicChannel: CommandException: " +
428 ar.exception);
429 } else {
430 loge("iccTransmitApduBasicChannel: Unknown exception");
431 }
432 }
433 synchronized (request) {
434 request.notifyAll();
435 }
436 break;
437
438 case CMD_EXCHANGE_SIM_IO:
439 request = (MainThreadRequest) msg.obj;
440 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800441 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700442 if (uiccCard == null) {
443 loge("iccExchangeSimIO: No UICC");
444 request.result = new IccIoResult(0x6F, 0, (byte[])null);
445 synchronized (request) {
446 request.notifyAll();
447 }
448 } else {
449 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
450 request);
451 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
452 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
453 iccArgument.data, onCompleted);
454 }
455 break;
456
457 case EVENT_EXCHANGE_SIM_IO_DONE:
458 ar = (AsyncResult) msg.obj;
459 request = (MainThreadRequest) ar.userObj;
460 if (ar.exception == null && ar.result != null) {
461 request.result = ar.result;
462 } else {
463 request.result = new IccIoResult(0x6f, 0, (byte[])null);
464 }
465 synchronized (request) {
466 request.notifyAll();
467 }
468 break;
469
Derek Tan4d5e5c12014-02-04 11:54:58 -0800470 case CMD_SEND_ENVELOPE:
471 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800472 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700473 if (uiccCard == null) {
474 loge("sendEnvelopeWithStatus: No UICC");
475 request.result = new IccIoResult(0x6F, 0, (byte[])null);
476 synchronized (request) {
477 request.notifyAll();
478 }
479 } else {
480 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
481 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
482 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800483 break;
484
485 case EVENT_SEND_ENVELOPE_DONE:
486 ar = (AsyncResult) msg.obj;
487 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700488 if (ar.exception == null && ar.result != null) {
489 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800490 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700491 request.result = new IccIoResult(0x6F, 0, (byte[])null);
492 if (ar.result == null) {
493 loge("sendEnvelopeWithStatus: Empty response");
494 } else if (ar.exception instanceof CommandException) {
495 loge("sendEnvelopeWithStatus: CommandException: " +
496 ar.exception);
497 } else {
498 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
499 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800500 }
501 synchronized (request) {
502 request.notifyAll();
503 }
504 break;
505
Shishir Agrawal566b7612013-10-28 14:41:00 -0700506 case CMD_OPEN_CHANNEL:
507 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800508 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700509 if (uiccCard == null) {
510 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800511 request.result = new IccOpenLogicalChannelResponse(-1,
512 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700513 synchronized (request) {
514 request.notifyAll();
515 }
516 } else {
517 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
518 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
519 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700520 break;
521
522 case EVENT_OPEN_CHANNEL_DONE:
523 ar = (AsyncResult) msg.obj;
524 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700525 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700526 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700527 int[] result = (int[]) ar.result;
528 int channelId = result[0];
529 byte[] selectResponse = null;
530 if (result.length > 1) {
531 selectResponse = new byte[result.length - 1];
532 for (int i = 1; i < result.length; ++i) {
533 selectResponse[i - 1] = (byte) result[i];
534 }
535 }
536 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700537 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700538 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700539 if (ar.result == null) {
540 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700541 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700542 if (ar.exception != null) {
543 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
544 }
545
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700546 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700547 if (ar.exception instanceof CommandException) {
548 CommandException.Error error =
549 ((CommandException) (ar.exception)).getCommandError();
550 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700551 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700552 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700553 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700554 }
555 }
556 openChannelResp = new IccOpenLogicalChannelResponse(
557 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700558 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700559 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700560 synchronized (request) {
561 request.notifyAll();
562 }
563 break;
564
565 case CMD_CLOSE_CHANNEL:
566 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800567 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700568 if (uiccCard == null) {
569 loge("iccCloseLogicalChannel: No UICC");
570 request.result = new IccIoResult(0x6F, 0, (byte[])null);
571 synchronized (request) {
572 request.notifyAll();
573 }
574 } else {
575 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
576 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
577 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700578 break;
579
580 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800581 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
582 break;
583
584 case CMD_NV_READ_ITEM:
585 request = (MainThreadRequest) msg.obj;
586 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
587 mPhone.nvReadItem((Integer) request.argument, onCompleted);
588 break;
589
590 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700591 ar = (AsyncResult) msg.obj;
592 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800593 if (ar.exception == null && ar.result != null) {
594 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700595 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800596 request.result = "";
597 if (ar.result == null) {
598 loge("nvReadItem: Empty response");
599 } else if (ar.exception instanceof CommandException) {
600 loge("nvReadItem: CommandException: " +
601 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700602 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800603 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700604 }
605 }
606 synchronized (request) {
607 request.notifyAll();
608 }
609 break;
610
Jake Hambye994d462014-02-03 13:10:13 -0800611 case CMD_NV_WRITE_ITEM:
612 request = (MainThreadRequest) msg.obj;
613 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
614 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
615 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
616 break;
617
618 case EVENT_NV_WRITE_ITEM_DONE:
619 handleNullReturnEvent(msg, "nvWriteItem");
620 break;
621
622 case CMD_NV_WRITE_CDMA_PRL:
623 request = (MainThreadRequest) msg.obj;
624 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
625 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
626 break;
627
628 case EVENT_NV_WRITE_CDMA_PRL_DONE:
629 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
630 break;
631
632 case CMD_NV_RESET_CONFIG:
633 request = (MainThreadRequest) msg.obj;
634 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
635 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
636 break;
637
638 case EVENT_NV_RESET_CONFIG_DONE:
639 handleNullReturnEvent(msg, "nvResetConfig");
640 break;
641
Jake Hamby7c27be32014-03-03 13:25:59 -0800642 case CMD_GET_PREFERRED_NETWORK_TYPE:
643 request = (MainThreadRequest) msg.obj;
644 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700645 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800646 break;
647
648 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
649 ar = (AsyncResult) msg.obj;
650 request = (MainThreadRequest) ar.userObj;
651 if (ar.exception == null && ar.result != null) {
652 request.result = ar.result; // Integer
653 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800654 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800655 if (ar.result == null) {
656 loge("getPreferredNetworkType: Empty response");
657 } else if (ar.exception instanceof CommandException) {
658 loge("getPreferredNetworkType: CommandException: " +
659 ar.exception);
660 } else {
661 loge("getPreferredNetworkType: Unknown exception");
662 }
663 }
664 synchronized (request) {
665 request.notifyAll();
666 }
667 break;
668
669 case CMD_SET_PREFERRED_NETWORK_TYPE:
670 request = (MainThreadRequest) msg.obj;
671 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
672 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700673 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800674 break;
675
676 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
677 handleNullReturnEvent(msg, "setPreferredNetworkType");
678 break;
679
Steven Liu4bf01bc2014-07-17 11:05:29 -0500680 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
681 request = (MainThreadRequest)msg.obj;
682 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
683 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
684 break;
685
686 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
687 ar = (AsyncResult)msg.obj;
688 request = (MainThreadRequest)ar.userObj;
689 request.result = ar;
690 synchronized (request) {
691 request.notifyAll();
692 }
693 break;
694
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800695 case CMD_SET_VOICEMAIL_NUMBER:
696 request = (MainThreadRequest) msg.obj;
697 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
698 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800699 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
700 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800701 break;
702
703 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
704 handleNullReturnEvent(msg, "setVoicemailNumber");
705 break;
706
Stuart Scott54788802015-03-30 13:18:01 -0700707 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
708 request = (MainThreadRequest) msg.obj;
709 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
710 request);
711 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
712 break;
713
714 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
715 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
716 break;
717
Shishir Agrawal302c8692015-06-19 13:49:39 -0700718 case CMD_PERFORM_NETWORK_SCAN:
719 request = (MainThreadRequest) msg.obj;
720 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
721 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
722 break;
723
724 case EVENT_PERFORM_NETWORK_SCAN_DONE:
725 ar = (AsyncResult) msg.obj;
726 request = (MainThreadRequest) ar.userObj;
727 CellNetworkScanResult cellScanResult;
728 if (ar.exception == null && ar.result != null) {
729 cellScanResult = new CellNetworkScanResult(
730 CellNetworkScanResult.STATUS_SUCCESS,
731 (List<OperatorInfo>) ar.result);
732 } else {
733 if (ar.result == null) {
734 loge("getCellNetworkScanResults: Empty response");
735 }
736 if (ar.exception != null) {
737 loge("getCellNetworkScanResults: Exception: " + ar.exception);
738 }
739 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
740 if (ar.exception instanceof CommandException) {
741 CommandException.Error error =
742 ((CommandException) (ar.exception)).getCommandError();
743 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
744 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
745 } else if (error == CommandException.Error.GENERIC_FAILURE) {
746 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
747 }
748 }
749 cellScanResult = new CellNetworkScanResult(errorCode, null);
750 }
751 request.result = cellScanResult;
752 synchronized (request) {
753 request.notifyAll();
754 }
755 break;
756
757 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
758 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700759 ManualNetworkSelectionArgument selArg =
760 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700761 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
762 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700763 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
764 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700765 break;
766
767 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
768 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
769 break;
770
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700771 case CMD_GET_MODEM_ACTIVITY_INFO:
772 request = (MainThreadRequest) msg.obj;
773 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700774 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700775 break;
776
777 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
778 ar = (AsyncResult) msg.obj;
779 request = (MainThreadRequest) ar.userObj;
780 if (ar.exception == null && ar.result != null) {
781 request.result = ar.result;
782 } else {
783 if (ar.result == null) {
784 loge("queryModemActivityInfo: Empty response");
785 } else if (ar.exception instanceof CommandException) {
786 loge("queryModemActivityInfo: CommandException: " +
787 ar.exception);
788 } else {
789 loge("queryModemActivityInfo: Unknown exception");
790 }
791 }
Amit Mahajand4766222016-01-28 15:28:28 -0800792 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
793 if (request.result == null) {
794 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
795 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700796 synchronized (request) {
797 request.notifyAll();
798 }
799 break;
800
Meng Wang1a7c35a2016-05-05 20:56:15 -0700801 case CMD_SET_ALLOWED_CARRIERS:
802 request = (MainThreadRequest) msg.obj;
803 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
804 mPhone.setAllowedCarriers(
805 (List<CarrierIdentifier>) request.argument,
806 onCompleted);
807 break;
808
809 case EVENT_SET_ALLOWED_CARRIERS_DONE:
810 ar = (AsyncResult) msg.obj;
811 request = (MainThreadRequest) ar.userObj;
812 if (ar.exception == null && ar.result != null) {
813 request.result = ar.result;
814 } else {
815 if (ar.result == null) {
816 loge("setAllowedCarriers: Empty response");
817 } else if (ar.exception instanceof CommandException) {
818 loge("setAllowedCarriers: CommandException: " +
819 ar.exception);
820 } else {
821 loge("setAllowedCarriers: Unknown exception");
822 }
823 }
824 // Result cannot be null. Return -1 on error.
825 if (request.result == null) {
826 request.result = new int[]{-1};
827 }
828 synchronized (request) {
829 request.notifyAll();
830 }
831 break;
832
833 case CMD_GET_ALLOWED_CARRIERS:
834 request = (MainThreadRequest) msg.obj;
835 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
836 mPhone.getAllowedCarriers(onCompleted);
837 break;
838
839 case EVENT_GET_ALLOWED_CARRIERS_DONE:
840 ar = (AsyncResult) msg.obj;
841 request = (MainThreadRequest) ar.userObj;
842 if (ar.exception == null && ar.result != null) {
843 request.result = ar.result;
844 } else {
845 if (ar.result == null) {
846 loge("getAllowedCarriers: Empty response");
847 } else if (ar.exception instanceof CommandException) {
848 loge("getAllowedCarriers: CommandException: " +
849 ar.exception);
850 } else {
851 loge("getAllowedCarriers: Unknown exception");
852 }
853 }
854 // Result cannot be null. Return empty list of CarrierIdentifier.
855 if (request.result == null) {
856 request.result = new ArrayList<CarrierIdentifier>(0);
857 }
858 synchronized (request) {
859 request.notifyAll();
860 }
861 break;
862
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700863 default:
864 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
865 break;
866 }
867 }
Jake Hambye994d462014-02-03 13:10:13 -0800868
869 private void handleNullReturnEvent(Message msg, String command) {
870 AsyncResult ar = (AsyncResult) msg.obj;
871 MainThreadRequest request = (MainThreadRequest) ar.userObj;
872 if (ar.exception == null) {
873 request.result = true;
874 } else {
875 request.result = false;
876 if (ar.exception instanceof CommandException) {
877 loge(command + ": CommandException: " + ar.exception);
878 } else {
879 loge(command + ": Unknown exception");
880 }
881 }
882 synchronized (request) {
883 request.notifyAll();
884 }
885 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700886 }
887
888 /**
889 * Posts the specified command to be executed on the main thread,
890 * waits for the request to complete, and returns the result.
891 * @see #sendRequestAsync
892 */
893 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800894 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700895 }
896
897 /**
898 * Posts the specified command to be executed on the main thread,
899 * waits for the request to complete, and returns the result.
900 * @see #sendRequestAsync
901 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800902 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700903 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
904 throw new RuntimeException("This method will deadlock if called from the main thread.");
905 }
906
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800907 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700908 Message msg = mMainThreadHandler.obtainMessage(command, request);
909 msg.sendToTarget();
910
911 // Wait for the request to complete
912 synchronized (request) {
913 while (request.result == null) {
914 try {
915 request.wait();
916 } catch (InterruptedException e) {
917 // Do nothing, go back and wait until the request is complete
918 }
919 }
920 }
921 return request.result;
922 }
923
924 /**
925 * Asynchronous ("fire and forget") version of sendRequest():
926 * Posts the specified command to be executed on the main thread, and
927 * returns immediately.
928 * @see #sendRequest
929 */
930 private void sendRequestAsync(int command) {
931 mMainThreadHandler.sendEmptyMessage(command);
932 }
933
934 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700935 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
936 * @see {@link #sendRequest(int,Object)}
937 */
938 private void sendRequestAsync(int command, Object argument) {
939 MainThreadRequest request = new MainThreadRequest(argument);
940 Message msg = mMainThreadHandler.obtainMessage(command, request);
941 msg.sendToTarget();
942 }
943
944 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 * Initialize the singleton PhoneInterfaceManager instance.
946 * This is only done once, at startup, from PhoneApp.onCreate().
947 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700948 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700949 synchronized (PhoneInterfaceManager.class) {
950 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700951 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700952 } else {
953 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
954 }
955 return sInstance;
956 }
957 }
958
959 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700960 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700961 mApp = app;
962 mPhone = phone;
963 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700964 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
966 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700967 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700968 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800969 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800970
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 publish();
972 }
973
974 private void publish() {
975 if (DBG) log("publish: " + this);
976
977 ServiceManager.addService("phone", this);
978 }
979
Stuart Scott584921c2015-01-15 17:10:34 -0800980 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800981 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
982 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800983 }
984
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800985 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
986 Phone phone = getPhoneFromRequest(request);
987 return phone == null ? null :
988 UiccController.getInstance().getUiccCard(phone.getPhoneId());
989 }
990
Wink Saville36469e72014-06-11 15:17:00 -0700991 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700992 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800993 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700994 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700995 //
996 // Implementation of the ITelephony interface.
997 //
998
999 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001000 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001001 }
1002
Wink Savilleb564aae2014-10-23 10:18:09 -07001003 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001004 if (DBG) log("dial: " + number);
1005 // No permission check needed here: This is just a wrapper around the
1006 // ACTION_DIAL intent, which is available to any app since it puts up
1007 // the UI before it does anything.
1008
1009 String url = createTelUrl(number);
1010 if (url == null) {
1011 return;
1012 }
1013
1014 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001015 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1017 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1018 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1019 mApp.startActivity(intent);
1020 }
1021 }
1022
1023 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001024 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001025 }
1026
Wink Savilleb564aae2014-10-23 10:18:09 -07001027 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001028 if (DBG) log("call: " + number);
1029
1030 // This is just a wrapper around the ACTION_CALL intent, but we still
1031 // need to do a permission check since we're calling startActivity()
1032 // from the context of the phone app.
1033 enforceCallPermission();
1034
1035 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1036 != AppOpsManager.MODE_ALLOWED) {
1037 return;
1038 }
1039
1040 String url = createTelUrl(number);
1041 if (url == null) {
1042 return;
1043 }
1044
Wink Saville08874612014-08-31 19:19:58 -07001045 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001046 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001047 if (slist != null) {
1048 for (SubscriptionInfo subInfoRecord : slist) {
1049 if (subInfoRecord.getSubscriptionId() == subId) {
1050 isValid = true;
1051 break;
1052 }
Wink Saville08874612014-08-31 19:19:58 -07001053 }
1054 }
1055 if (isValid == false) {
1056 return;
1057 }
1058
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001060 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001061 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1062 mApp.startActivity(intent);
1063 }
1064
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001065 /**
1066 * End a call based on call state
1067 * @return true is a call was ended
1068 */
1069 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001070 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001071 }
1072
1073 /**
1074 * End a call based on the call state of the subId
1075 * @return true is a call was ended
1076 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001077 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001078 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001079 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001080 }
1081
1082 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001083 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001084 }
1085
Wink Savilleb564aae2014-10-23 10:18:09 -07001086 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001087 if (DBG) log("answerRingingCall...");
1088 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1089 // but that can probably wait till the big TelephonyManager API overhaul.
1090 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1091 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001092 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001093 }
1094
1095 /**
1096 * Make the actual telephony calls to implement answerRingingCall().
1097 * This should only be called from the main thread of the Phone app.
1098 * @see #answerRingingCall
1099 *
1100 * TODO: it would be nice to return true if we answered the call, or
1101 * false if there wasn't actually a ringing incoming call, or some
1102 * other error occurred. (In other words, pass back the return value
1103 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1104 * But that would require calling this method via sendRequest() rather
1105 * than sendRequestAsync(), and right now we don't actually *need* that
1106 * return value, so let's just return void for now.
1107 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001108 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001109 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001110 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001111 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1112 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001113 if (hasActiveCall && hasHoldingCall) {
1114 // Both lines are in use!
1115 // TODO: provide a flag to let the caller specify what
1116 // policy to use if both lines are in use. (The current
1117 // behavior is hardwired to "answer incoming, end ongoing",
1118 // which is how the CALL button is specced to behave.)
1119 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1120 return;
1121 } else {
1122 // answerCall() will automatically hold the current active
1123 // call, if there is one.
1124 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1125 return;
1126 }
1127 } else {
1128 // No call was ringing.
1129 return;
1130 }
1131 }
1132
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001133 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001134 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001135 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001136 public void silenceRinger() {
1137 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001138 }
1139
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001140 @Override
1141 public boolean isOffhook(String callingPackage) {
1142 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001143 }
1144
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001145 @Override
1146 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1147 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1148 return false;
1149 }
1150
Sanket Padawe356d7632015-06-22 14:03:32 -07001151 final Phone phone = getPhone(subId);
1152 if (phone != null) {
1153 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1154 } else {
1155 return false;
1156 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001157 }
1158
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001159 @Override
1160 public boolean isRinging(String callingPackage) {
1161 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001162 }
1163
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001164 @Override
1165 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1166 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1167 return false;
1168 }
1169
Sanket Padawe356d7632015-06-22 14:03:32 -07001170 final Phone phone = getPhone(subId);
1171 if (phone != null) {
1172 return (phone.getState() == PhoneConstants.State.RINGING);
1173 } else {
1174 return false;
1175 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001176 }
1177
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001178 @Override
1179 public boolean isIdle(String callingPackage) {
1180 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001181 }
1182
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001183 @Override
1184 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1185 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1186 return false;
1187 }
1188
Sanket Padawe356d7632015-06-22 14:03:32 -07001189 final Phone phone = getPhone(subId);
1190 if (phone != null) {
1191 return (phone.getState() == PhoneConstants.State.IDLE);
1192 } else {
1193 return false;
1194 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 }
1196
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001197 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001198 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001199 }
1200
Wink Savilleb564aae2014-10-23 10:18:09 -07001201 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001202 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001203 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1204 }
1205
1206 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001207 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001208 }
1209
Wink Savilleb564aae2014-10-23 10:18:09 -07001210 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001211 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001212 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1213 }
1214
1215 /** {@hide} */
1216 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001217 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001218 }
1219
Wink Savilleb564aae2014-10-23 10:18:09 -07001220 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001221 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001222 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001223 checkSimPin.start();
1224 return checkSimPin.unlockSim(null, pin);
1225 }
1226
Wink Saville9de0f752013-10-22 19:04:03 -07001227 /** {@hide} */
1228 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001229 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001230 }
1231
Wink Savilleb564aae2014-10-23 10:18:09 -07001232 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001234 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001235 checkSimPuk.start();
1236 return checkSimPuk.unlockSim(puk, pin);
1237 }
1238
1239 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001240 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001241 * a synchronous one.
1242 */
1243 private static class UnlockSim extends Thread {
1244
1245 private final IccCard mSimCard;
1246
1247 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001248 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1249 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001250
1251 // For replies from SimCard interface
1252 private Handler mHandler;
1253
1254 // For async handler to identify request type
1255 private static final int SUPPLY_PIN_COMPLETE = 100;
1256
1257 public UnlockSim(IccCard simCard) {
1258 mSimCard = simCard;
1259 }
1260
1261 @Override
1262 public void run() {
1263 Looper.prepare();
1264 synchronized (UnlockSim.this) {
1265 mHandler = new Handler() {
1266 @Override
1267 public void handleMessage(Message msg) {
1268 AsyncResult ar = (AsyncResult) msg.obj;
1269 switch (msg.what) {
1270 case SUPPLY_PIN_COMPLETE:
1271 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1272 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001273 mRetryCount = msg.arg1;
1274 if (ar.exception != null) {
1275 if (ar.exception instanceof CommandException &&
1276 ((CommandException)(ar.exception)).getCommandError()
1277 == CommandException.Error.PASSWORD_INCORRECT) {
1278 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1279 } else {
1280 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1281 }
1282 } else {
1283 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1284 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 mDone = true;
1286 UnlockSim.this.notifyAll();
1287 }
1288 break;
1289 }
1290 }
1291 };
1292 UnlockSim.this.notifyAll();
1293 }
1294 Looper.loop();
1295 }
1296
1297 /*
1298 * Use PIN or PUK to unlock SIM card
1299 *
1300 * If PUK is null, unlock SIM card with PIN
1301 *
1302 * If PUK is not null, unlock SIM card with PUK and set PIN code
1303 */
Wink Saville9de0f752013-10-22 19:04:03 -07001304 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305
1306 while (mHandler == null) {
1307 try {
1308 wait();
1309 } catch (InterruptedException e) {
1310 Thread.currentThread().interrupt();
1311 }
1312 }
1313 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1314
1315 if (puk == null) {
1316 mSimCard.supplyPin(pin, callback);
1317 } else {
1318 mSimCard.supplyPuk(puk, pin, callback);
1319 }
1320
1321 while (!mDone) {
1322 try {
1323 Log.d(LOG_TAG, "wait for done");
1324 wait();
1325 } catch (InterruptedException e) {
1326 // Restore the interrupted status
1327 Thread.currentThread().interrupt();
1328 }
1329 }
1330 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001331 int[] resultArray = new int[2];
1332 resultArray[0] = mResult;
1333 resultArray[1] = mRetryCount;
1334 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001335 }
1336 }
1337
1338 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001339 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001340
1341 }
1342
Wink Savilleb564aae2014-10-23 10:18:09 -07001343 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 // No permission check needed here: this call is harmless, and it's
1345 // needed for the ServiceState.requestStateUpdate() call (which is
1346 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001347 final Phone phone = getPhone(subId);
1348 if (phone != null) {
1349 phone.updateServiceLocation();
1350 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351 }
1352
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001353 @Override
1354 public boolean isRadioOn(String callingPackage) {
1355 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001356 }
1357
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001358 @Override
1359 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1360 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1361 return false;
1362 }
1363 return isRadioOnForSubscriber(subId);
1364 }
1365
1366 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001367 final Phone phone = getPhone(subId);
1368 if (phone != null) {
1369 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1370 } else {
1371 return false;
1372 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 }
1374
1375 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001376 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001377
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001378 }
Wink Saville36469e72014-06-11 15:17:00 -07001379
Wink Savilleb564aae2014-10-23 10:18:09 -07001380 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001381 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001382 final Phone phone = getPhone(subId);
1383 if (phone != null) {
1384 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1385 }
Wink Saville36469e72014-06-11 15:17:00 -07001386 }
1387
1388 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001389 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001390 }
1391
Wink Savilleb564aae2014-10-23 10:18:09 -07001392 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001393 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001394 final Phone phone = getPhone(subId);
1395 if (phone == null) {
1396 return false;
1397 }
1398 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001399 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001400 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001401 }
1402 return true;
1403 }
Wink Saville36469e72014-06-11 15:17:00 -07001404
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001405 public boolean needMobileRadioShutdown() {
1406 /*
1407 * If any of the Radios are available, it will need to be
1408 * shutdown. So return true if any Radio is available.
1409 */
1410 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1411 Phone phone = PhoneFactory.getPhone(i);
1412 if (phone != null && phone.isRadioAvailable()) return true;
1413 }
1414 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1415 return false;
1416 }
1417
1418 public void shutdownMobileRadios() {
1419 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1420 logv("Shutting down Phone " + i);
1421 shutdownRadioUsingPhoneId(i);
1422 }
1423 }
1424
1425 private void shutdownRadioUsingPhoneId(int phoneId) {
1426 enforceModifyPermission();
1427 Phone phone = PhoneFactory.getPhone(phoneId);
1428 if (phone != null && phone.isRadioAvailable()) {
1429 phone.shutdownRadio();
1430 }
1431 }
1432
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001434 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1435 if (defaultPhone != null) {
1436 defaultPhone.setRadioPower(turnOn);
1437 return true;
1438 } else {
1439 loge("There's no default phone.");
1440 return false;
1441 }
Wink Saville36469e72014-06-11 15:17:00 -07001442 }
1443
Wink Savilleb564aae2014-10-23 10:18:09 -07001444 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001445 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001446 final Phone phone = getPhone(subId);
1447 if (phone != null) {
1448 phone.setRadioPower(turnOn);
1449 return true;
1450 } else {
1451 return false;
1452 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001453 }
1454
Wink Saville36469e72014-06-11 15:17:00 -07001455 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001456 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 public boolean enableDataConnectivity() {
1458 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001459 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001460 final Phone phone = getPhone(subId);
1461 if (phone != null) {
1462 phone.setDataEnabled(true);
1463 return true;
1464 } else {
1465 return false;
1466 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001467 }
1468
Wink Saville36469e72014-06-11 15:17:00 -07001469 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001470 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 public boolean disableDataConnectivity() {
1472 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001473 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001474 final Phone phone = getPhone(subId);
1475 if (phone != null) {
1476 phone.setDataEnabled(false);
1477 return true;
1478 } else {
1479 return false;
1480 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001481 }
1482
Wink Saville36469e72014-06-11 15:17:00 -07001483 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001484 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001485 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001486 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001487 final Phone phone = getPhone(subId);
1488 if (phone != null) {
1489 return phone.isDataConnectivityPossible();
1490 } else {
1491 return false;
1492 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001493 }
1494
1495 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001496 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001497 }
1498
pkanwarae03a6b2016-11-06 20:37:09 -08001499 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001500 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001501 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1502 return;
1503 }
1504 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1505 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1506 };
1507
1508
Wink Savilleb564aae2014-10-23 10:18:09 -07001509 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001510 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001511 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1512 return false;
1513 }
Wink Saville36469e72014-06-11 15:17:00 -07001514 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001515 }
1516
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001517 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001518 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001519 }
1520
Shishir Agrawala9f32182016-04-12 12:00:16 -07001521 public int getCallStateForSlot(int slotId) {
1522 Phone phone = PhoneFactory.getPhone(slotId);
1523 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1524 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001525 }
1526
Sanket Padawe356d7632015-06-22 14:03:32 -07001527 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001528 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001529 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001530 if (phone != null) {
1531 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1532 } else {
1533 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1534 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001535 }
1536
Sanket Padawe356d7632015-06-22 14:03:32 -07001537 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001538 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001539 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001540 if (phone != null) {
1541 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1542 } else {
1543 return TelephonyManager.DATA_ACTIVITY_NONE;
1544 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545 }
1546
1547 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001548 public Bundle getCellLocation(String callingPackage) {
1549 enforceFineOrCoarseLocationPermission("getCellLocation");
1550
1551 // OP_COARSE_LOCATION controls both fine and coarse location.
1552 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1553 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001554 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001555 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001556 }
1557
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001558 if (checkIfCallerIsSelfOrForegroundUser() ||
1559 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001560 if (DBG_LOC) log("getCellLocation: is active user");
1561 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001562 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001563 if (phone == null) {
1564 return null;
1565 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001566
1567 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1568 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001569 return data;
1570 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001571 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001572 return null;
1573 }
1574 }
1575
Svetoslav64fad262015-04-14 14:35:21 -07001576 private void enforceFineOrCoarseLocationPermission(String message) {
1577 try {
1578 mApp.enforceCallingOrSelfPermission(
1579 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1580 } catch (SecurityException e) {
1581 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1582 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1583 // is the weaker precondition
1584 mApp.enforceCallingOrSelfPermission(
1585 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1586 }
1587 }
1588
1589
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 @Override
1591 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001592 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001593 }
1594
Sanket Padawe356d7632015-06-22 14:03:32 -07001595 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001596 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001597 mApp.enforceCallingOrSelfPermission(
1598 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001599 final Phone phone = getPhone(subId);
1600 if (phone != null) {
1601 phone.enableLocationUpdates();
1602 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 }
1604
1605 @Override
1606 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001607 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001608 }
1609
Sanket Padawe356d7632015-06-22 14:03:32 -07001610 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001611 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001612 mApp.enforceCallingOrSelfPermission(
1613 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001614 final Phone phone = getPhone(subId);
1615 if (phone != null) {
1616 phone.disableLocationUpdates();
1617 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001618 }
1619
1620 @Override
1621 @SuppressWarnings("unchecked")
1622 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001623 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1624
1625 // OP_COARSE_LOCATION controls both fine and coarse location.
1626 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1627 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1628 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001629 }
1630
1631 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1632 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1633 return null;
1634 }
Svetoslav64fad262015-04-14 14:35:21 -07001635
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001636 if (checkIfCallerIsSelfOrForegroundUser() ||
1637 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001638 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1639
1640 ArrayList<NeighboringCellInfo> cells = null;
1641
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001642 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001643 try {
1644 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001645 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001646 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001647 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001648 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001649 }
1650 return cells;
1651 } else {
1652 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1653 return null;
1654 }
1655 }
1656
1657
1658 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001659 public List<CellInfo> getAllCellInfo(String callingPackage) {
1660 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1661
1662 // OP_COARSE_LOCATION controls both fine and coarse location.
1663 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1664 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1665 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001666 }
1667
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001668 if (checkIfCallerIsSelfOrForegroundUser() ||
1669 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001670 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001671 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001672 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1673 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001674 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1675 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001676 }
1677 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001678 } else {
1679 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1680 return null;
1681 }
1682 }
1683
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001684 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001685 public void setCellInfoListRate(int rateInMillis) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001686 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1687 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001688 }
1689
Shishir Agrawala9f32182016-04-12 12:00:16 -07001690 @Override
1691 public String getImeiForSlot(int slotId, String callingPackage) {
1692 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1693 return null;
1694 }
1695 Phone phone = PhoneFactory.getPhone(slotId);
1696 return phone == null ? null : phone.getImei();
1697 }
1698
1699 @Override
1700 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1701 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1702 return null;
1703 }
1704 Phone phone = PhoneFactory.getPhone(slotId);
1705 return phone == null ? null : phone.getDeviceSvn();
1706 }
1707
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001708 //
1709 // Internal helper methods.
1710 //
1711
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001712 /**
1713 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1714 */
1715 private boolean checkCallerInteractAcrossUsersFull() {
1716 return mPhone.getContext().checkCallingOrSelfPermission(
1717 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1718 == PackageManager.PERMISSION_GRANTED;
1719 }
1720
Jake Hambye994d462014-02-03 13:10:13 -08001721 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001722 boolean ok;
1723
1724 boolean self = Binder.getCallingUid() == Process.myUid();
1725 if (!self) {
1726 // Get the caller's user id then clear the calling identity
1727 // which will be restored in the finally clause.
1728 int callingUser = UserHandle.getCallingUserId();
1729 long ident = Binder.clearCallingIdentity();
1730
1731 try {
1732 // With calling identity cleared the current user is the foreground user.
1733 int foregroundUser = ActivityManager.getCurrentUser();
1734 ok = (foregroundUser == callingUser);
1735 if (DBG_LOC) {
1736 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1737 + " callingUser=" + callingUser + " ok=" + ok);
1738 }
1739 } catch (Exception ex) {
1740 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1741 ok = false;
1742 } finally {
1743 Binder.restoreCallingIdentity(ident);
1744 }
1745 } else {
1746 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1747 ok = true;
1748 }
1749 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1750 return ok;
1751 }
1752
1753 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001754 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1755 *
1756 * @throws SecurityException if the caller does not have the required permission
1757 */
1758 private void enforceModifyPermission() {
1759 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1760 }
1761
1762 /**
Junda Liua2e36012014-07-09 18:30:01 -07001763 * Make sure either system app or the caller has carrier privilege.
1764 *
1765 * @throws SecurityException if the caller does not have the required permission/privilege
1766 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001767 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001768 int permission = mApp.checkCallingOrSelfPermission(
1769 android.Manifest.permission.MODIFY_PHONE_STATE);
1770 if (permission == PackageManager.PERMISSION_GRANTED) {
1771 return;
1772 }
1773
1774 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001775 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001776 }
1777
1778 /**
1779 * Make sure the caller has carrier privilege.
1780 *
1781 * @throws SecurityException if the caller does not have the required permission
1782 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001783 private void enforceCarrierPrivilege(int subId) {
1784 if (getCarrierPrivilegeStatus(subId) !=
1785 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001786 loge("No Carrier Privilege.");
1787 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001788 }
1789 }
1790
1791 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001792 * Make sure the caller has the CALL_PHONE permission.
1793 *
1794 * @throws SecurityException if the caller does not have the required permission
1795 */
1796 private void enforceCallPermission() {
1797 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1798 }
1799
Stuart Scott8eef64f2015-04-08 15:13:54 -07001800 private void enforceConnectivityInternalPermission() {
1801 mApp.enforceCallingOrSelfPermission(
1802 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1803 "ConnectivityService");
1804 }
1805
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001806 private String createTelUrl(String number) {
1807 if (TextUtils.isEmpty(number)) {
1808 return null;
1809 }
1810
Jake Hambye994d462014-02-03 13:10:13 -08001811 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001812 }
1813
Ihab Awadf9e92732013-12-05 18:02:52 -08001814 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001815 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1816 }
1817
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001818 private static void logv(String msg) {
1819 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1820 }
1821
Ihab Awadf9e92732013-12-05 18:02:52 -08001822 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001823 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1824 }
1825
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001826 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001827 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001828 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001829 }
1830
Sanket Padawe356d7632015-06-22 14:03:32 -07001831 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001832 public int getActivePhoneTypeForSlot(int slotId) {
1833 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001834 if (phone == null) {
1835 return PhoneConstants.PHONE_TYPE_NONE;
1836 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001837 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001838 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001839 }
1840
1841 /**
1842 * Returns the CDMA ERI icon index to display
1843 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001844 @Override
1845 public int getCdmaEriIconIndex(String callingPackage) {
1846 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001847 }
1848
Sanket Padawe356d7632015-06-22 14:03:32 -07001849 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001850 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1851 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1852 return -1;
1853 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001854 final Phone phone = getPhone(subId);
1855 if (phone != null) {
1856 return phone.getCdmaEriIconIndex();
1857 } else {
1858 return -1;
1859 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001860 }
1861
1862 /**
1863 * Returns the CDMA ERI icon mode,
1864 * 0 - ON
1865 * 1 - FLASHING
1866 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001867 @Override
1868 public int getCdmaEriIconMode(String callingPackage) {
1869 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001870 }
1871
Sanket Padawe356d7632015-06-22 14:03:32 -07001872 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001873 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1874 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1875 return -1;
1876 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001877 final Phone phone = getPhone(subId);
1878 if (phone != null) {
1879 return phone.getCdmaEriIconMode();
1880 } else {
1881 return -1;
1882 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001883 }
1884
1885 /**
1886 * Returns the CDMA ERI text,
1887 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001888 @Override
1889 public String getCdmaEriText(String callingPackage) {
1890 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001891 }
1892
Sanket Padawe356d7632015-06-22 14:03:32 -07001893 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001894 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1895 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1896 return null;
1897 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001898 final Phone phone = getPhone(subId);
1899 if (phone != null) {
1900 return phone.getCdmaEriText();
1901 } else {
1902 return null;
1903 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001904 }
1905
1906 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001907 * Returns the CDMA MDN.
1908 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001909 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001910 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001911 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001912 final Phone phone = getPhone(subId);
1913 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1914 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001915 } else {
1916 return null;
1917 }
1918 }
1919
1920 /**
1921 * Returns the CDMA MIN.
1922 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001923 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001924 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001925 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001926 final Phone phone = getPhone(subId);
1927 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1928 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001929 } else {
1930 return null;
1931 }
1932 }
1933
1934 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001935 * Returns true if CDMA provisioning needs to run.
1936 */
1937 public boolean needsOtaServiceProvisioning() {
1938 return mPhone.needsOtaServiceProvisioning();
1939 }
1940
1941 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001942 * Sets the voice mail number of a given subId.
1943 */
1944 @Override
1945 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001946 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001947 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1948 new Pair<String, String>(alphaTag, number), new Integer(subId));
1949 return success;
1950 }
1951
Ta-wei Yen87c49842016-05-13 21:19:52 -07001952 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001953 public void setVisualVoicemailEnabled(String callingPackage,
1954 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1955 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1956 if (!TextUtils.equals(callingPackage,
1957 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1958 enforceModifyPermissionOrCarrierPrivilege(
1959 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1960 }
1961 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
1962 }
1963
1964 @Override
1965 public boolean isVisualVoicemailEnabled(String callingPackage,
1966 PhoneAccountHandle phoneAccountHandle) {
1967 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
1968 return false;
1969 }
1970 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
1971 }
1972
1973 @Override
Ta-wei Yendca928f2017-01-10 16:17:08 -08001974 public String getVisualVoicemailPackageName(String callingPackage,
1975 @Nullable PhoneAccountHandle phoneAccountHandle) {
1976 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1977 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
1978 return null;
1979 }
1980 int subId = PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
1981 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
1982 }
1983
1984 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001985 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1986 VisualVoicemailSmsFilterSettings settings) {
1987 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001988 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001989 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1990 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001991 }
1992
1993 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001994 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1995 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001996 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001997 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001998 }
1999
2000 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002001 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2002 String callingPackage, int subId) {
2003 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002004 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002005 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002006 }
2007
2008 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002009 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002010 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002011 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002012 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2013 }
2014
2015 @Override
2016 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2017 String number, int port, String text, PendingIntent sentIntent) {
2018 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002019 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002020 enforceSendSmsPermission();
2021 // Make the calls as the phone process.
2022 final long identity = Binder.clearCallingIdentity();
2023 try {
2024 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2025 if (port == 0) {
2026 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2027 sentIntent, null, false);
2028 } else {
2029 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2030 smsManager.sendDataMessageWithSelfPermissions(number, null,
2031 (short) port, data, sentIntent, null);
2032 }
2033 } finally {
2034 Binder.restoreCallingIdentity(identity);
2035 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002036 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002037 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002038 * Returns the unread count of voicemails
2039 */
2040 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002041 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002042 }
2043
2044 /**
2045 * Returns the unread count of voicemails for a subId
2046 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002047 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002048 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002049 final Phone phone = getPhone(subId);
2050 if (phone != null) {
2051 return phone.getVoiceMessageCount();
2052 } else {
2053 return 0;
2054 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002055 }
2056
2057 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002058 * returns true, if the device is in a state where both voice and data
2059 * are supported simultaneously. This can change based on location or network condition.
2060 */
2061 @Override
2062 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2063 final Phone phone = getPhone(subId);
2064 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2065 }
2066
2067 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002068 * Returns the data network type.
2069 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002070 *
2071 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2072 */
2073 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002074 public int getNetworkType() {
2075 final Phone phone = getPhone(getDefaultSubscription());
2076 if (phone != null) {
2077 return phone.getServiceState().getDataNetworkType();
2078 } else {
2079 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2080 }
Wink Saville36469e72014-06-11 15:17:00 -07002081 }
2082
2083 /**
2084 * Returns the network type for a subId
2085 */
2086 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002087 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2088 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2089 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2090 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002091
Sanket Padawe356d7632015-06-22 14:03:32 -07002092 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002093 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002094 return phone.getServiceState().getDataNetworkType();
2095 } else {
2096 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2097 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002098 }
2099
2100 /**
2101 * Returns the data network type
2102 */
2103 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002104 public int getDataNetworkType(String callingPackage) {
2105 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002106 }
2107
2108 /**
2109 * Returns the data network type for a subId
2110 */
2111 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002112 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2113 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2114 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2115 }
2116
Sanket Padawe356d7632015-06-22 14:03:32 -07002117 final Phone phone = getPhone(subId);
2118 if (phone != null) {
2119 return phone.getServiceState().getDataNetworkType();
2120 } else {
2121 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2122 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002123 }
2124
2125 /**
Wink Saville36469e72014-06-11 15:17:00 -07002126 * Returns the Voice network type for a subId
2127 */
2128 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002129 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2130 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2131 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2132 }
2133
Sanket Padawe356d7632015-06-22 14:03:32 -07002134 final Phone phone = getPhone(subId);
2135 if (phone != null) {
2136 return phone.getServiceState().getVoiceNetworkType();
2137 } else {
2138 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2139 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002140 }
2141
2142 /**
2143 * @return true if a ICC card is present
2144 */
2145 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002146 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002147 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002148 }
2149
2150 /**
2151 * @return true if a ICC card is present for a slotId
2152 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002153 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002154 public boolean hasIccCardUsingSlotId(int slotId) {
fionaxu6e6c68b2016-06-23 13:31:32 -07002155 final Phone phone = PhoneFactory.getPhone(slotId);
2156 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002157 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002158 } else {
2159 return false;
2160 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002161 }
2162
2163 /**
2164 * Return if the current radio is LTE on CDMA. This
2165 * is a tri-state return value as for a period of time
2166 * the mode may be unknown.
2167 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002168 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002169 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002170 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002171 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002172 @Override
2173 public int getLteOnCdmaMode(String callingPackage) {
2174 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002175 }
2176
Sanket Padawe356d7632015-06-22 14:03:32 -07002177 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002178 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2179 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2180 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2181 }
2182
Sanket Padawe356d7632015-06-22 14:03:32 -07002183 final Phone phone = getPhone(subId);
2184 if (phone == null) {
2185 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2186 } else {
2187 return phone.getLteOnCdmaMode();
2188 }
Wink Saville36469e72014-06-11 15:17:00 -07002189 }
2190
2191 public void setPhone(Phone phone) {
2192 mPhone = phone;
2193 }
2194
2195 /**
2196 * {@hide}
2197 * Returns Default subId, 0 in the case of single standby.
2198 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002199 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002200 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002201 }
2202
Shishir Agrawala9f32182016-04-12 12:00:16 -07002203 private int getSlotForDefaultSubscription() {
2204 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2205 }
2206
Wink Savilleb564aae2014-10-23 10:18:09 -07002207 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002208 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002209 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002210
2211 /**
2212 * @see android.telephony.TelephonyManager.WifiCallingChoices
2213 */
2214 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002215 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2216 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002217 }
2218
2219 /**
2220 * @see android.telephony.TelephonyManager.WifiCallingChoices
2221 */
2222 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002223 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2224 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2225 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002226 }
2227
Sailesh Nepald1e68152013-12-12 19:08:02 -08002228 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002229 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002230 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002231 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002232
Shishir Agrawal566b7612013-10-28 14:41:00 -07002233 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002234 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2235 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002236
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002237 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002238 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002239 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002240 if (DBG) log("iccOpenLogicalChannel: " + response);
2241 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002242 }
2243
2244 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002245 public boolean iccCloseLogicalChannel(int subId, int channel) {
2246 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002247
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002248 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002249 if (channel < 0) {
2250 return false;
2251 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002252 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002253 if (DBG) log("iccCloseLogicalChannel: " + success);
2254 return success;
2255 }
2256
2257 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002258 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002259 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002260 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002261
2262 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002263 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2264 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002265 " data=" + data);
2266 }
2267
2268 if (channel < 0) {
2269 return "";
2270 }
2271
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002272 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002273 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002274 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2275
Shishir Agrawal566b7612013-10-28 14:41:00 -07002276 // Append the returned status code to the end of the response payload.
2277 String s = Integer.toHexString(
2278 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002279 if (response.payload != null) {
2280 s = IccUtils.bytesToHexString(response.payload) + s;
2281 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002282 return s;
2283 }
Jake Hambye994d462014-02-03 13:10:13 -08002284
Evan Charltonc66da362014-05-16 14:06:40 -07002285 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002286 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002287 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002288 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002289
2290 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002291 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2292 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002293 }
2294
2295 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002296 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002297 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2298
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002299 // Append the returned status code to the end of the response payload.
2300 String s = Integer.toHexString(
2301 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002302 if (response.payload != null) {
2303 s = IccUtils.bytesToHexString(response.payload) + s;
2304 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002305 return s;
2306 }
2307
2308 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002309 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002310 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002311 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002312
2313 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002314 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002315 p1 + " " + p2 + " " + p3 + ":" + filePath);
2316 }
2317
2318 IccIoResult response =
2319 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002320 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2321 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002322
2323 if (DBG) {
2324 log("Exchange SIM_IO [R]" + response);
2325 }
2326
2327 byte[] result = null;
2328 int length = 2;
2329 if (response.payload != null) {
2330 length = 2 + response.payload.length;
2331 result = new byte[length];
2332 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2333 } else {
2334 result = new byte[length];
2335 }
2336
2337 result[length - 1] = (byte) response.sw2;
2338 result[length - 2] = (byte) response.sw1;
2339 return result;
2340 }
2341
2342 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002343 public String sendEnvelopeWithStatus(int subId, String content) {
2344 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002345
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002346 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002347 if (response.payload == null) {
2348 return "";
2349 }
2350
2351 // Append the returned status code to the end of the response payload.
2352 String s = Integer.toHexString(
2353 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2354 s = IccUtils.bytesToHexString(response.payload) + s;
2355 return s;
2356 }
2357
Jake Hambye994d462014-02-03 13:10:13 -08002358 /**
2359 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2360 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2361 *
2362 * @param itemID the ID of the item to read
2363 * @return the NV item as a String, or null on error.
2364 */
2365 @Override
2366 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002367 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002368 if (DBG) log("nvReadItem: item " + itemID);
2369 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2370 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2371 return value;
2372 }
2373
2374 /**
2375 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2376 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2377 *
2378 * @param itemID the ID of the item to read
2379 * @param itemValue the value to write, as a String
2380 * @return true on success; false on any failure
2381 */
2382 @Override
2383 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002384 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002385 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2386 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2387 new Pair<Integer, String>(itemID, itemValue));
2388 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2389 return success;
2390 }
2391
2392 /**
2393 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2394 * Used for device configuration by some CDMA operators.
2395 *
2396 * @param preferredRoamingList byte array containing the new PRL
2397 * @return true on success; false on any failure
2398 */
2399 @Override
2400 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002401 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002402 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2403 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2404 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2405 return success;
2406 }
2407
2408 /**
2409 * Perform the specified type of NV config reset.
2410 * Used for device configuration by some CDMA operators.
2411 *
2412 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2413 * @return true on success; false on any failure
2414 */
2415 @Override
2416 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002417 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002418 if (DBG) log("nvResetConfig: type " + resetType);
2419 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2420 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2421 return success;
2422 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002423
2424 /**
Wink Saville36469e72014-06-11 15:17:00 -07002425 * {@hide}
2426 * Returns Default sim, 0 in the case of single standby.
2427 */
2428 public int getDefaultSim() {
2429 //TODO Need to get it from Telephony Devcontroller
2430 return 0;
2431 }
2432
Svet Ganovb320e182015-04-16 12:30:10 -07002433 public String[] getPcscfAddress(String apnType, String callingPackage) {
2434 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2435 return new String[0];
2436 }
2437
2438
ram87fca6f2014-07-18 18:58:44 +05302439 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002440 }
2441
Brad Ebinger51f743a2017-01-23 13:50:20 -08002442 /**
2443 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2444 * feature or {@link null} if the service is not available. If an ImsServiceController is
2445 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2446 * feature updates.
2447 */
2448 public IImsServiceController getImsServiceControllerAndListen(int slotId, int feature,
2449 IImsServiceFeatureListener callback) {
2450 enforceModifyPermission();
2451 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotId, feature,
2452 callback);
2453 }
2454
Wink Saville36469e72014-06-11 15:17:00 -07002455 public void setImsRegistrationState(boolean registered) {
2456 enforceModifyPermission();
2457 mPhone.setImsRegistrationState(registered);
2458 }
2459
2460 /**
Stuart Scott54788802015-03-30 13:18:01 -07002461 * Set the network selection mode to automatic.
2462 *
2463 */
2464 @Override
2465 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002466 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002467 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2468 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2469 }
2470
2471 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002472 * Set the network selection mode to manual with the selected carrier.
2473 */
2474 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002475 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2476 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002477 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002478 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002479 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2480 persistSelection);
2481 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002482 }
2483
2484 /**
2485 * Scans for available networks.
2486 */
2487 @Override
2488 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002489 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002490 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2491 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2492 CMD_PERFORM_NETWORK_SCAN, null, subId);
2493 return result;
2494 }
2495
2496 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002497 * Get the calculated preferred network type.
2498 * Used for debugging incorrect network type.
2499 *
2500 * @return the preferred network type, defined in RILConstants.java.
2501 */
2502 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002503 public int getCalculatedPreferredNetworkType(String callingPackage) {
2504 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2505 return RILConstants.PREFERRED_NETWORK_MODE;
2506 }
2507
Amit Mahajan43330e02014-11-18 11:54:45 -08002508 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002509 }
2510
2511 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002512 * Get the preferred network type.
2513 * Used for device configuration by some CDMA operators.
2514 *
2515 * @return the preferred network type, defined in RILConstants.java.
2516 */
2517 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002518 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002519 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002520 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002521 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002522 int networkType = (result != null ? result[0] : -1);
2523 if (DBG) log("getPreferredNetworkType: " + networkType);
2524 return networkType;
2525 }
2526
2527 /**
2528 * Set the preferred network type.
2529 * Used for device configuration by some CDMA operators.
2530 *
2531 * @param networkType the preferred network type, defined in RILConstants.java.
2532 * @return true on success; false on any failure.
2533 */
2534 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002535 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002536 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002537 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2538 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002539 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002540 if (success) {
2541 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002542 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002543 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002544 return success;
2545 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002546
2547 /**
Junda Liu475951f2014-11-07 16:45:03 -08002548 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2549 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2550 * tethering.
2551 *
2552 * @return 0: Not required. 1: required. 2: Not set.
2553 * @hide
2554 */
2555 @Override
2556 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002557 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002558 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2559 Settings.Global.TETHER_DUN_REQUIRED, 2);
2560 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2561 // config_tether_apndata.
2562 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2563 dunRequired = 1;
2564 }
2565 return dunRequired;
2566 }
2567
2568 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002569 * Set mobile data enabled
2570 * Used by the user through settings etc to turn on/off mobile data
2571 *
2572 * @param enable {@code true} turn turn data on, else {@code false}
2573 */
2574 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002575 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002576 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002577 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002578 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002579 Phone phone = PhoneFactory.getPhone(phoneId);
2580 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002581 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002582 phone.setDataEnabled(enable);
2583 } else {
2584 loge("setDataEnabled: no phone for subId=" + subId);
2585 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002586 }
2587
2588 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002589 * Get whether mobile data is enabled.
2590 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002591 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002592 *
2593 * @return {@code true} if data is enabled else {@code false}
2594 */
2595 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002596 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002597 try {
2598 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2599 null);
2600 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002601 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002602 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002603 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002604 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002605 Phone phone = PhoneFactory.getPhone(phoneId);
2606 if (phone != null) {
2607 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002608 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002609 return retVal;
2610 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002611 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002612 return false;
2613 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002614 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002615
2616 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002617 public int getCarrierPrivilegeStatus(int subId) {
2618 final Phone phone = getPhone(subId);
2619 if (phone == null) {
2620 loge("getCarrierPrivilegeStatus: Invalid subId");
2621 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2622 }
2623 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002624 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002625 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002626 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2627 }
2628 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002629 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002630 }
Junda Liu29340342014-07-10 15:23:27 -07002631
2632 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002633 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002634 if (TextUtils.isEmpty(pkgName))
2635 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002636 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002637 if (card == null) {
2638 loge("checkCarrierPrivilegesForPackage: No UICC");
2639 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2640 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002641 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2642 }
2643
2644 @Override
2645 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002646 if (TextUtils.isEmpty(pkgName))
2647 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002648 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2649 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2650 UiccCard card = UiccController.getInstance().getUiccCard(i);
2651 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002652 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002653 continue;
2654 }
2655
2656 result = card.getCarrierPrivilegeStatus(
2657 mPhone.getContext().getPackageManager(), pkgName);
2658 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2659 break;
2660 }
2661 }
2662
2663 return result;
Junda Liu29340342014-07-10 15:23:27 -07002664 }
Derek Tan89e89d42014-07-08 17:00:10 -07002665
2666 @Override
Junda Liue64de782015-04-16 17:19:16 -07002667 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2668 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2669 loge("phoneId " + phoneId + " is not valid.");
2670 return null;
2671 }
2672 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002673 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002674 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002675 return null ;
2676 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002677 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002678 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002679 }
2680
Amith Yamasani6e118872016-02-19 12:53:51 -08002681 @Override
2682 public List<String> getPackagesWithCarrierPrivileges() {
2683 PackageManager pm = mPhone.getContext().getPackageManager();
2684 List<String> privilegedPackages = new ArrayList<>();
2685 List<PackageInfo> packages = null;
2686 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2687 UiccCard card = UiccController.getInstance().getUiccCard(i);
2688 if (card == null) {
2689 // No UICC in that slot.
2690 continue;
2691 }
2692 if (card.hasCarrierPrivilegeRules()) {
2693 if (packages == null) {
2694 // Only check packages in user 0 for now
2695 packages = pm.getInstalledPackagesAsUser(
2696 PackageManager.MATCH_DISABLED_COMPONENTS
2697 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2698 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2699 }
2700 for (int p = packages.size() - 1; p >= 0; p--) {
2701 PackageInfo pkgInfo = packages.get(p);
2702 if (pkgInfo != null && pkgInfo.packageName != null
2703 && card.getCarrierPrivilegeStatus(pkgInfo)
2704 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2705 privilegedPackages.add(pkgInfo.packageName);
2706 }
2707 }
2708 }
2709 }
2710 return privilegedPackages;
2711 }
2712
Wink Savilleb564aae2014-10-23 10:18:09 -07002713 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002714 final Phone phone = getPhone(subId);
2715 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002716 if (card == null) {
2717 loge("getIccId: No UICC");
2718 return null;
2719 }
2720 String iccId = card.getIccId();
2721 if (TextUtils.isEmpty(iccId)) {
2722 loge("getIccId: ICC ID is null or empty.");
2723 return null;
2724 }
2725 return iccId;
2726 }
2727
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002728 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002729 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2730 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002731 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002732
Jeff Sharkey85190e62014-12-05 09:40:12 -08002733 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002734 final Phone phone = getPhone(subId);
2735 if (phone == null) {
2736 return false;
2737 }
2738 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002739
2740 if (DBG_MERGE) {
2741 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2742 + subscriberId + " to " + number);
2743 }
2744
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002745 if (TextUtils.isEmpty(iccId)) {
2746 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002747 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002748
Jeff Sharkey85190e62014-12-05 09:40:12 -08002749 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2750
2751 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002752 if (alphaTag == null) {
2753 editor.remove(alphaTagPrefKey);
2754 } else {
2755 editor.putString(alphaTagPrefKey, alphaTag);
2756 }
2757
Jeff Sharkey85190e62014-12-05 09:40:12 -08002758 // Record both the line number and IMSI for this ICCID, since we need to
2759 // track all merged IMSIs based on line number
2760 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2761 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002762 if (number == null) {
2763 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002764 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002765 } else {
2766 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002767 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002768 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002769
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002770 editor.commit();
2771 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002772 }
2773
2774 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002775 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002776 // This is open to apps with WRITE_SMS.
2777 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002778 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002779 return null;
2780 }
Derek Tan97ebb422014-09-05 16:55:38 -07002781
2782 String iccId = getIccId(subId);
2783 if (iccId != null) {
2784 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002785 if (DBG_MERGE) {
2786 log("getLine1NumberForDisplay returning " +
2787 mTelephonySharedPreferences.getString(numberPrefKey, null));
2788 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002789 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002790 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002791 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002792 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002793 }
2794
2795 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002796 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2797 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2798 return null;
2799 }
Derek Tan97ebb422014-09-05 16:55:38 -07002800
2801 String iccId = getIccId(subId);
2802 if (iccId != null) {
2803 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002804 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002805 }
Derek Tan97ebb422014-09-05 16:55:38 -07002806 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002807 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002808
2809 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002810 public String[] getMergedSubscriberIds(String callingPackage) {
2811 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2812 return null;
2813 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002814 final Context context = mPhone.getContext();
2815 final TelephonyManager tele = TelephonyManager.from(context);
2816 final SubscriptionManager sub = SubscriptionManager.from(context);
2817
2818 // Figure out what subscribers are currently active
2819 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002820 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2821 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2822 final long identity = Binder.clearCallingIdentity();
2823 try {
2824 final int[] subIds = sub.getActiveSubscriptionIdList();
2825 for (int subId : subIds) {
2826 activeSubscriberIds.add(tele.getSubscriberId(subId));
2827 }
2828 } finally {
2829 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002830 }
2831
2832 // First pass, find a number override for an active subscriber
2833 String mergeNumber = null;
2834 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2835 for (String key : prefs.keySet()) {
2836 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2837 final String subscriberId = (String) prefs.get(key);
2838 if (activeSubscriberIds.contains(subscriberId)) {
2839 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2840 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2841 mergeNumber = (String) prefs.get(numberKey);
2842 if (DBG_MERGE) {
2843 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2844 + " for active subscriber " + subscriberId);
2845 }
2846 if (!TextUtils.isEmpty(mergeNumber)) {
2847 break;
2848 }
2849 }
2850 }
2851 }
2852
2853 // Shortcut when no active merged subscribers
2854 if (TextUtils.isEmpty(mergeNumber)) {
2855 return null;
2856 }
2857
2858 // Second pass, find all subscribers under that line override
2859 final ArraySet<String> result = new ArraySet<>();
2860 for (String key : prefs.keySet()) {
2861 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2862 final String number = (String) prefs.get(key);
2863 if (mergeNumber.equals(number)) {
2864 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2865 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2866 final String subscriberId = (String) prefs.get(subscriberKey);
2867 if (!TextUtils.isEmpty(subscriberId)) {
2868 result.add(subscriberId);
2869 }
2870 }
2871 }
2872 }
2873
2874 final String[] resultArray = result.toArray(new String[result.size()]);
2875 Arrays.sort(resultArray);
2876 if (DBG_MERGE) {
2877 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2878 }
2879 return resultArray;
2880 }
2881
2882 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002883 public boolean setOperatorBrandOverride(int subId, String brand) {
2884 enforceCarrierPrivilege(subId);
2885 final Phone phone = getPhone(subId);
2886 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002887 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002888
2889 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002890 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002891 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2892 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002893 enforceCarrierPrivilege(subId);
2894 final Phone phone = getPhone(subId);
2895 if (phone == null) {
2896 return false;
2897 }
2898 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002899 cdmaNonRoamingList);
2900 }
2901
2902 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002903 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2904 enforceModifyPermission();
2905
2906 int returnValue = 0;
2907 try {
2908 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2909 if(result.exception == null) {
2910 if (result.result != null) {
2911 byte[] responseData = (byte[])(result.result);
2912 if(responseData.length > oemResp.length) {
2913 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2914 responseData.length + "bytes. Buffer Size is " +
2915 oemResp.length + "bytes.");
2916 }
2917 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2918 returnValue = responseData.length;
2919 }
2920 } else {
2921 CommandException ex = (CommandException) result.exception;
2922 returnValue = ex.getCommandError().ordinal();
2923 if(returnValue > 0) returnValue *= -1;
2924 }
2925 } catch (RuntimeException e) {
2926 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2927 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2928 if(returnValue > 0) returnValue *= -1;
2929 }
2930
2931 return returnValue;
2932 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002933
2934 @Override
2935 public void setRadioCapability(RadioAccessFamily[] rafs) {
2936 try {
2937 ProxyController.getInstance().setRadioCapability(rafs);
2938 } catch (RuntimeException e) {
2939 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2940 }
2941 }
2942
2943 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002944 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2945 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2946 return RadioAccessFamily.RAF_UNKNOWN;
2947 }
2948
Wink Saville5d475dd2014-10-17 15:00:58 -07002949 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2950 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002951
2952 @Override
2953 public void enableVideoCalling(boolean enable) {
2954 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002955 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002956 }
2957
2958 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002959 public boolean isVideoCallingEnabled(String callingPackage) {
2960 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2961 return false;
2962 }
2963
Andrew Lee77527ac2014-10-21 16:57:39 -07002964 // Check the user preference and the system-level IMS setting. Even if the user has
2965 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2966 // In the long run, we may instead need to check if there exists a connection service
2967 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002968 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2969 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002970 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002971 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002972
Andrew Leea1239f22015-03-02 17:44:07 -08002973 @Override
2974 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002975 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002976 }
2977
2978 @Override
2979 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002980 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002981 }
2982
Andrew Lee9431b832015-03-09 18:46:45 -07002983 @Override
2984 public boolean isTtyModeSupported() {
2985 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2986 TelephonyManager telephonyManager =
2987 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08002988 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07002989 }
2990
2991 @Override
2992 public boolean isHearingAidCompatibilitySupported() {
2993 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2994 }
2995
Sanket Padawe7310cc72015-01-14 09:53:20 -08002996 /**
2997 * Returns the unique device ID of phone, for example, the IMEI for
2998 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2999 *
3000 * <p>Requires Permission:
3001 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3002 */
3003 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003004 public String getDeviceId(String callingPackage) {
3005 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3006 return null;
3007 }
3008
Sanket Padawe7310cc72015-01-14 09:53:20 -08003009 final Phone phone = PhoneFactory.getPhone(0);
3010 if (phone != null) {
3011 return phone.getDeviceId();
3012 } else {
3013 return null;
3014 }
3015 }
3016
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003017 /*
3018 * {@hide}
3019 * Returns the IMS Registration Status
3020 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003021 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003022 public boolean isImsRegistered() {
3023 return mPhone.isImsRegistered();
3024 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003025
3026 @Override
3027 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3028 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3029 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003030
Nathan Haroldc55097a2015-03-11 18:14:50 -07003031 /*
3032 * {@hide}
3033 * Returns the IMS Registration Status
3034 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003035 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003036 return mPhone.isWifiCallingEnabled();
3037 }
3038
3039 /*
3040 * {@hide}
3041 * Returns the IMS Registration Status
3042 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003043 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003044 return mPhone.isVolteEnabled();
3045 }
Svet Ganovb320e182015-04-16 12:30:10 -07003046
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003047 /*
3048 * {@hide} Returns the IMS Registration Status
3049 */
3050 public boolean isVideoTelephonyAvailable() {
3051 return mPhone.isVideoEnabled();
3052 }
3053
Svet Ganovb320e182015-04-16 12:30:10 -07003054 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003055 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003056 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003057 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3058
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003059 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003060 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003061 } catch (SecurityException e) {
3062 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3063 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003064 }
Svet Ganovb320e182015-04-16 12:30:10 -07003065
3066 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3067 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3068 return false;
3069 }
3070
3071 return true;
3072 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003073
Makoto Onukifee69342015-06-29 14:44:50 -07003074 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003075 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003076 */
3077 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003078 // Default SMS app can always read it.
3079 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3080 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3081 return true;
3082 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003083
Makoto Onukie4072d12015-08-03 15:12:23 -07003084 try {
3085 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003086 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003087 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003088 // Can be read with READ_SMS too.
3089 try {
3090 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3091 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3092 if (opCode != AppOpsManager.OP_NONE) {
3093 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3094 == AppOpsManager.MODE_ALLOWED;
3095 } else {
3096 return true;
3097 }
3098 } catch (SecurityException readSmsSecurityException) {
3099 }
3100 // Can be read with READ_PHONE_NUMBER too.
3101 try {
3102 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBER,
3103 message);
3104 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBER);
3105 if (opCode != AppOpsManager.OP_NONE) {
3106 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3107 == AppOpsManager.MODE_ALLOWED;
3108 } else {
3109 return true;
3110 }
3111 } catch (SecurityException readPhoneNumberSecurityException) {
3112 }
3113
3114 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3115 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3116 ", " + android.Manifest.permission.READ_SMS + ", or " +
3117 android.Manifest.permission.READ_PHONE_NUMBER);
Makoto Onukifee69342015-06-29 14:44:50 -07003118 }
3119
Stuart Scott8eef64f2015-04-08 15:13:54 -07003120 @Override
3121 public void factoryReset(int subId) {
3122 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003123 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3124 return;
3125 }
3126
Svet Ganovcc087f82015-05-12 20:35:54 -07003127 final long identity = Binder.clearCallingIdentity();
3128 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003129 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3130 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003131 // Enable data
3132 setDataEnabled(subId, true);
3133 // Set network selection mode to automatic
3134 setNetworkSelectionModeAutomatic(subId);
3135 // Set preferred mobile network type to the best available
3136 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3137 // Turn off roaming
3138 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3139 }
3140 } finally {
3141 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003142 }
3143 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003144
3145 @Override
3146 public String getLocaleFromDefaultSim() {
3147 // We query all subscriptions instead of just the active ones, because
3148 // this might be called early on in the provisioning flow when the
3149 // subscriptions potentially aren't active yet.
3150 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3151 if (slist == null || slist.isEmpty()) {
3152 return null;
3153 }
3154
3155 // This function may be called very early, say, from the setup wizard, at
3156 // which point we won't have a default subscription set. If that's the case
3157 // we just choose the first, which will be valid in "most cases".
3158 final int defaultSubId = getDefaultSubscription();
3159 SubscriptionInfo info = null;
3160 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3161 info = slist.get(0);
3162 } else {
3163 for (SubscriptionInfo item : slist) {
3164 if (item.getSubscriptionId() == defaultSubId) {
3165 info = item;
3166 break;
3167 }
3168 }
3169
3170 if (info == null) {
3171 return null;
3172 }
3173 }
3174
3175 // Try and fetch the locale from the carrier properties or from the SIM language
3176 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003177 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003178 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003179 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003180 if (defaultPhone != null) {
3181 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3182 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003183 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003184 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3185 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003186 } else {
3187 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003188 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003189 }
3190 }
3191
Narayan Kamath011676f2015-07-29 12:04:08 +01003192 // The SIM language preferences only store a language (e.g. fr = French), not an
3193 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3194 // the SIM and carrier preferences does not include a country we add the country
3195 // determined from the SIM MCC to provide an exact locale.
3196 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003197 if (mccLocale != null) {
3198 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3199 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003200 }
3201
Tony Hill183b2de2015-06-24 14:53:58 +01003202 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003203 return null;
3204 }
3205
3206 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3207 final long identity = Binder.clearCallingIdentity();
3208 try {
3209 return mSubscriptionController.getAllSubInfoList(
3210 mPhone.getContext().getOpPackageName());
3211 } finally {
3212 Binder.restoreCallingIdentity(identity);
3213 }
3214 }
3215
3216 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3217 final long identity = Binder.clearCallingIdentity();
3218 try {
3219 return mSubscriptionController.getActiveSubscriptionInfoList(
3220 mPhone.getContext().getOpPackageName());
3221 } finally {
3222 Binder.restoreCallingIdentity(identity);
3223 }
3224 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003225
3226 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003227 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3228 * representing the state of the modem.
3229 *
3230 * NOTE: This clears the modem state, so there should only every be one caller.
3231 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003232 */
3233 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003234 public void requestModemActivityInfo(ResultReceiver result) {
3235 enforceModifyPermission();
3236
3237 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3238 Bundle bundle = new Bundle();
3239 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3240 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003241 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003242
3243 /**
3244 * {@hide}
3245 * Returns the service state information on specified subscription.
3246 */
3247 @Override
3248 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3249
3250 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3251 return null;
3252 }
3253
3254 final Phone phone = getPhone(subId);
3255 if (phone == null) {
3256 return null;
3257 }
3258
3259 return phone.getServiceState();
3260 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003261
3262 /**
3263 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3264 *
3265 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3266 * voicemail ringtone.
3267 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3268 * PhoneAccount.
3269 */
3270 @Override
3271 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3272 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3273 if (phone == null) {
3274 return null;
3275 }
3276
3277 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3278 }
3279
3280 /**
3281 * Returns whether vibration is set for voicemail notification in Phone settings.
3282 *
3283 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3284 * voicemail vibration setting.
3285 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3286 */
3287 @Override
3288 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3289 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3290 if (phone == null) {
3291 return false;
3292 }
3293
3294 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3295 }
3296
Youhan Wange64578a2016-05-02 15:32:42 -07003297 /**
3298 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3299 *
3300 * @throws SecurityException if the caller does not have the required permission
3301 */
3302 private void enforceReadPrivilegedPermission() {
3303 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3304 null);
3305 }
3306
3307 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003308 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3309 * permission.
3310 *
3311 * @throws SecurityException if the caller does not have the required permission
3312 */
3313 private void enforceSendSmsPermission() {
3314 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3315 }
3316
3317 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003318 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003319 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003320 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003321 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003322 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
3323 String vvmPackage = RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId)
3324 .getPackageName();
3325 if (!callingPackage.equals(vvmPackage)) {
3326 throw new SecurityException("Caller not current active visual voicemail package[" +
3327 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003328 }
3329 }
3330
3331 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003332 * Return the application ID for the app type.
3333 *
3334 * @param subId the subscription ID that this request applies to.
3335 * @param appType the uicc app type.
3336 * @return Application ID for specificied app type, or null if no uicc.
3337 */
3338 @Override
3339 public String getAidForAppType(int subId, int appType) {
3340 enforceReadPrivilegedPermission();
3341 Phone phone = getPhone(subId);
3342 if (phone == null) {
3343 return null;
3344 }
3345 String aid = null;
3346 try {
3347 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3348 .getApplicationByType(appType).getAid();
3349 } catch (Exception e) {
3350 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3351 }
3352 return aid;
3353 }
3354
Youhan Wang4001d252016-05-11 10:29:41 -07003355 /**
3356 * Return the Electronic Serial Number.
3357 *
3358 * @param subId the subscription ID that this request applies to.
3359 * @return ESN or null if error.
3360 */
3361 @Override
3362 public String getEsn(int subId) {
3363 enforceReadPrivilegedPermission();
3364 Phone phone = getPhone(subId);
3365 if (phone == null) {
3366 return null;
3367 }
3368 String esn = null;
3369 try {
3370 esn = phone.getEsn();
3371 } catch (Exception e) {
3372 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3373 }
3374 return esn;
3375 }
3376
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003377 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003378 * Return the Preferred Roaming List Version.
3379 *
3380 * @param subId the subscription ID that this request applies to.
3381 * @return PRLVersion or null if error.
3382 */
3383 @Override
3384 public String getCdmaPrlVersion(int subId) {
3385 enforceReadPrivilegedPermission();
3386 Phone phone = getPhone(subId);
3387 if (phone == null) {
3388 return null;
3389 }
3390 String cdmaPrlVersion = null;
3391 try {
3392 cdmaPrlVersion = phone.getCdmaPrlVersion();
3393 } catch (Exception e) {
3394 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3395 }
3396 return cdmaPrlVersion;
3397 }
3398
3399 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003400 * Get snapshot of Telephony histograms
3401 * @return List of Telephony histograms
3402 * @hide
3403 */
3404 @Override
3405 public List<TelephonyHistogram> getTelephonyHistograms() {
3406 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3407 return RIL.getTelephonyRILTimingHistograms();
3408 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003409
3410 /**
3411 * {@hide}
3412 * Set the allowed carrier list for slotId
3413 * Require system privileges. In the future we may add this to carrier APIs.
3414 *
3415 * @return The number of carriers set successfully, should match length of carriers
3416 */
3417 @Override
3418 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3419 enforceModifyPermission();
3420 int subId = SubscriptionManager.getSubId(slotId)[0];
3421 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3422 return retVal[0];
3423 }
3424
3425 /**
3426 * {@hide}
3427 * Get the allowed carrier list for slotId.
3428 * Require system privileges. In the future we may add this to carrier APIs.
3429 *
3430 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3431 * means all carriers are allowed.
3432 */
3433 @Override
3434 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3435 enforceReadPrivilegedPermission();
3436 int subId = SubscriptionManager.getSubId(slotId)[0];
3437 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3438 }
3439
fionaxu59545b42016-05-25 15:53:37 -07003440 /**
3441 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3442 * @param subId the subscription ID that this action applies to.
3443 * @param enabled control enable or disable metered apns.
3444 * {@hide}
3445 */
3446 @Override
3447 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3448 enforceModifyPermission();
3449 final Phone phone = getPhone(subId);
3450 if (phone == null) {
3451 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3452 return;
3453 }
3454 try {
3455 phone.carrierActionSetMeteredApnsEnabled(enabled);
3456 } catch (Exception e) {
3457 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3458 }
3459 }
3460
3461 /**
3462 * Action set from carrier signalling broadcast receivers to enable/disable radio
3463 * @param subId the subscription ID that this action applies to.
3464 * @param enabled control enable or disable radio.
3465 * {@hide}
3466 */
3467 @Override
3468 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3469 enforceModifyPermission();
3470 final Phone phone = getPhone(subId);
3471 if (phone == null) {
3472 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3473 return;
3474 }
3475 try {
3476 phone.carrierActionSetRadioEnabled(enabled);
3477 } catch (Exception e) {
3478 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3479 }
3480 }
3481
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003482 /**
3483 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3484 * bug report is being generated.
3485 */
3486 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003487 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003488 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3489 != PackageManager.PERMISSION_GRANTED) {
3490 writer.println("Permission Denial: can't dump Phone from pid="
3491 + Binder.getCallingPid()
3492 + ", uid=" + Binder.getCallingUid()
3493 + "without permission "
3494 + android.Manifest.permission.DUMP);
3495 return;
3496 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003497 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003498 }
Jack Yueb89b242016-06-22 13:27:47 -07003499
3500 /**
3501 * Get aggregated video call data usage from all subscriptions since boot.
3502 * @return total data usage in bytes
3503 * {@hide}
3504 */
3505 @Override
3506 public long getVtDataUsage() {
3507 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3508 null);
3509
3510 // NetworkStatsService keeps tracking the active network interface and identity. It will
3511 // record the delta with the corresponding network identity. What we need to do here is
3512 // returning total video call data usage from all subscriptions since boot.
3513
3514 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3515 // simultaneous VT calls.
3516 final Phone[] phones = PhoneFactory.getPhones();
3517 long total = 0;
3518 for (Phone phone : phones) {
3519 total += phone.getVtDataUsage();
3520 }
3521 return total;
3522 }
Jack Yu75ab2952016-07-08 14:29:33 -07003523
3524 /**
3525 * Policy control of data connection. Usually used when data limit is passed.
3526 * @param enabled True if enabling the data, otherwise disabling.
3527 * @param subId Subscription index
3528 * {@hide}
3529 */
3530 @Override
3531 public void setPolicyDataEnabled(boolean enabled, int subId) {
3532 enforceModifyPermission();
3533 Phone phone = getPhone(subId);
3534 if (phone != null) {
3535 phone.setPolicyDataEnabled(enabled);
3536 }
3537 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003538
3539 /**
3540 * Get Client request stats
3541 * @return List of Client Request Stats
3542 * @hide
3543 */
3544 @Override
3545 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3546 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3547 return null;
3548 }
3549
3550 Phone phone = getPhone(subId);
3551 if (phone != null) {
3552 return phone.getClientRequestStats();
3553 }
3554
3555 return null;
3556 }
3557
3558 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3559 if (workSource != null) {
3560 return workSource;
3561 }
3562
3563 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3564 workSource = new WorkSource(uid, packageName);
3565 return workSource;
3566 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003567}