blob: 520f388bd8d3dbb497fc794e91331eef5fcd3bf9 [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) {
Jack Yua8d8cb82017-01-16 10:15:34 -08001686 enforceModifyPermission();
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001687 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1688 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001689 }
1690
Shishir Agrawala9f32182016-04-12 12:00:16 -07001691 @Override
1692 public String getImeiForSlot(int slotId, String callingPackage) {
1693 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1694 return null;
1695 }
1696 Phone phone = PhoneFactory.getPhone(slotId);
1697 return phone == null ? null : phone.getImei();
1698 }
1699
1700 @Override
1701 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1702 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1703 return null;
1704 }
1705 Phone phone = PhoneFactory.getPhone(slotId);
1706 return phone == null ? null : phone.getDeviceSvn();
1707 }
1708
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001709 //
1710 // Internal helper methods.
1711 //
1712
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001713 /**
1714 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1715 */
1716 private boolean checkCallerInteractAcrossUsersFull() {
1717 return mPhone.getContext().checkCallingOrSelfPermission(
1718 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1719 == PackageManager.PERMISSION_GRANTED;
1720 }
1721
Jake Hambye994d462014-02-03 13:10:13 -08001722 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001723 boolean ok;
1724
1725 boolean self = Binder.getCallingUid() == Process.myUid();
1726 if (!self) {
1727 // Get the caller's user id then clear the calling identity
1728 // which will be restored in the finally clause.
1729 int callingUser = UserHandle.getCallingUserId();
1730 long ident = Binder.clearCallingIdentity();
1731
1732 try {
1733 // With calling identity cleared the current user is the foreground user.
1734 int foregroundUser = ActivityManager.getCurrentUser();
1735 ok = (foregroundUser == callingUser);
1736 if (DBG_LOC) {
1737 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1738 + " callingUser=" + callingUser + " ok=" + ok);
1739 }
1740 } catch (Exception ex) {
1741 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1742 ok = false;
1743 } finally {
1744 Binder.restoreCallingIdentity(ident);
1745 }
1746 } else {
1747 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1748 ok = true;
1749 }
1750 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1751 return ok;
1752 }
1753
1754 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001755 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1756 *
1757 * @throws SecurityException if the caller does not have the required permission
1758 */
1759 private void enforceModifyPermission() {
1760 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1761 }
1762
1763 /**
Junda Liua2e36012014-07-09 18:30:01 -07001764 * Make sure either system app or the caller has carrier privilege.
1765 *
1766 * @throws SecurityException if the caller does not have the required permission/privilege
1767 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001768 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001769 int permission = mApp.checkCallingOrSelfPermission(
1770 android.Manifest.permission.MODIFY_PHONE_STATE);
1771 if (permission == PackageManager.PERMISSION_GRANTED) {
1772 return;
1773 }
1774
1775 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001776 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001777 }
1778
1779 /**
1780 * Make sure the caller has carrier privilege.
1781 *
1782 * @throws SecurityException if the caller does not have the required permission
1783 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001784 private void enforceCarrierPrivilege(int subId) {
1785 if (getCarrierPrivilegeStatus(subId) !=
1786 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001787 loge("No Carrier Privilege.");
1788 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001789 }
1790 }
1791
1792 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001793 * Make sure the caller has the CALL_PHONE permission.
1794 *
1795 * @throws SecurityException if the caller does not have the required permission
1796 */
1797 private void enforceCallPermission() {
1798 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1799 }
1800
Stuart Scott8eef64f2015-04-08 15:13:54 -07001801 private void enforceConnectivityInternalPermission() {
1802 mApp.enforceCallingOrSelfPermission(
1803 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1804 "ConnectivityService");
1805 }
1806
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001807 private String createTelUrl(String number) {
1808 if (TextUtils.isEmpty(number)) {
1809 return null;
1810 }
1811
Jake Hambye994d462014-02-03 13:10:13 -08001812 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001813 }
1814
Ihab Awadf9e92732013-12-05 18:02:52 -08001815 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001816 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1817 }
1818
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001819 private static void logv(String msg) {
1820 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1821 }
1822
Ihab Awadf9e92732013-12-05 18:02:52 -08001823 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001824 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1825 }
1826
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001827 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001828 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001829 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001830 }
1831
Sanket Padawe356d7632015-06-22 14:03:32 -07001832 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001833 public int getActivePhoneTypeForSlot(int slotId) {
1834 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001835 if (phone == null) {
1836 return PhoneConstants.PHONE_TYPE_NONE;
1837 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001838 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001839 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001840 }
1841
1842 /**
1843 * Returns the CDMA ERI icon index to display
1844 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001845 @Override
1846 public int getCdmaEriIconIndex(String callingPackage) {
1847 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001848 }
1849
Sanket Padawe356d7632015-06-22 14:03:32 -07001850 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001851 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1852 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1853 return -1;
1854 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001855 final Phone phone = getPhone(subId);
1856 if (phone != null) {
1857 return phone.getCdmaEriIconIndex();
1858 } else {
1859 return -1;
1860 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001861 }
1862
1863 /**
1864 * Returns the CDMA ERI icon mode,
1865 * 0 - ON
1866 * 1 - FLASHING
1867 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001868 @Override
1869 public int getCdmaEriIconMode(String callingPackage) {
1870 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001871 }
1872
Sanket Padawe356d7632015-06-22 14:03:32 -07001873 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001874 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1875 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1876 return -1;
1877 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001878 final Phone phone = getPhone(subId);
1879 if (phone != null) {
1880 return phone.getCdmaEriIconMode();
1881 } else {
1882 return -1;
1883 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001884 }
1885
1886 /**
1887 * Returns the CDMA ERI text,
1888 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001889 @Override
1890 public String getCdmaEriText(String callingPackage) {
1891 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001892 }
1893
Sanket Padawe356d7632015-06-22 14:03:32 -07001894 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001895 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1896 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1897 return null;
1898 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001899 final Phone phone = getPhone(subId);
1900 if (phone != null) {
1901 return phone.getCdmaEriText();
1902 } else {
1903 return null;
1904 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001905 }
1906
1907 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001908 * Returns the CDMA MDN.
1909 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001910 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001911 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001912 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001913 final Phone phone = getPhone(subId);
1914 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1915 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001916 } else {
1917 return null;
1918 }
1919 }
1920
1921 /**
1922 * Returns the CDMA MIN.
1923 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001924 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001925 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001926 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001927 final Phone phone = getPhone(subId);
1928 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1929 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001930 } else {
1931 return null;
1932 }
1933 }
1934
1935 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001936 * Returns true if CDMA provisioning needs to run.
1937 */
1938 public boolean needsOtaServiceProvisioning() {
1939 return mPhone.needsOtaServiceProvisioning();
1940 }
1941
1942 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001943 * Sets the voice mail number of a given subId.
1944 */
1945 @Override
1946 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001947 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001948 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1949 new Pair<String, String>(alphaTag, number), new Integer(subId));
1950 return success;
1951 }
1952
Ta-wei Yen87c49842016-05-13 21:19:52 -07001953 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001954 public void setVisualVoicemailEnabled(String callingPackage,
1955 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1956 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1957 if (!TextUtils.equals(callingPackage,
1958 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1959 enforceModifyPermissionOrCarrierPrivilege(
1960 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1961 }
1962 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
1963 }
1964
1965 @Override
1966 public boolean isVisualVoicemailEnabled(String callingPackage,
1967 PhoneAccountHandle phoneAccountHandle) {
1968 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
1969 return false;
1970 }
1971 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
1972 }
1973
1974 @Override
Ta-wei Yendca928f2017-01-10 16:17:08 -08001975 public String getVisualVoicemailPackageName(String callingPackage,
1976 @Nullable PhoneAccountHandle phoneAccountHandle) {
1977 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1978 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
1979 return null;
1980 }
1981 int subId = PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
1982 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
1983 }
1984
1985 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001986 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1987 VisualVoicemailSmsFilterSettings settings) {
1988 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001989 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001990 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1991 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001992 }
1993
1994 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001995 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1996 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001997 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001998 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001999 }
2000
2001 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002002 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2003 String callingPackage, int subId) {
2004 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002005 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002006 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002007 }
2008
2009 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002010 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002011 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002012 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002013 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2014 }
2015
2016 @Override
2017 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2018 String number, int port, String text, PendingIntent sentIntent) {
2019 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002020 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002021 enforceSendSmsPermission();
2022 // Make the calls as the phone process.
2023 final long identity = Binder.clearCallingIdentity();
2024 try {
2025 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2026 if (port == 0) {
2027 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2028 sentIntent, null, false);
2029 } else {
2030 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2031 smsManager.sendDataMessageWithSelfPermissions(number, null,
2032 (short) port, data, sentIntent, null);
2033 }
2034 } finally {
2035 Binder.restoreCallingIdentity(identity);
2036 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002037 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002038 /**
fionaxu0152e512016-11-14 13:36:14 -08002039 * Sets the voice activation state of a given subId.
2040 */
2041 @Override
2042 public void setVoiceActivationState(int subId, int activationState) {
2043 enforceModifyPermissionOrCarrierPrivilege(subId);
2044 final Phone phone = getPhone(subId);
2045 if (phone != null) {
2046 phone.setVoiceActivationState(activationState);
2047 } else {
2048 loge("setVoiceActivationState fails with invalid subId: " + subId);
2049 }
2050 }
2051
2052 /**
2053 * Sets the data activation state of a given subId.
2054 */
2055 @Override
2056 public void setDataActivationState(int subId, int activationState) {
2057 enforceModifyPermissionOrCarrierPrivilege(subId);
2058 final Phone phone = getPhone(subId);
2059 if (phone != null) {
2060 phone.setDataActivationState(activationState);
2061 } else {
2062 loge("setVoiceActivationState fails with invalid subId: " + subId);
2063 }
2064 }
2065
2066 /**
2067 * Returns the voice activation state of a given subId.
2068 */
2069 @Override
2070 public int getVoiceActivationState(int subId, String callingPackage) {
2071 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2072 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2073 }
2074 final Phone phone = getPhone(subId);
2075 if (phone != null) {
2076 return phone.getVoiceActivationState();
2077 } else {
2078 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2079 }
2080 }
2081
2082 /**
2083 * Returns the data activation state of a given subId.
2084 */
2085 @Override
2086 public int getDataActivationState(int subId, String callingPackage) {
2087 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2088 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2089 }
2090 final Phone phone = getPhone(subId);
2091 if (phone != null) {
2092 return phone.getDataActivationState();
2093 } else {
2094 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2095 }
2096 }
2097
2098 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002099 * Returns the unread count of voicemails
2100 */
2101 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002102 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002103 }
2104
2105 /**
2106 * Returns the unread count of voicemails for a subId
2107 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002108 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002109 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002110 final Phone phone = getPhone(subId);
2111 if (phone != null) {
2112 return phone.getVoiceMessageCount();
2113 } else {
2114 return 0;
2115 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002116 }
2117
2118 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002119 * returns true, if the device is in a state where both voice and data
2120 * are supported simultaneously. This can change based on location or network condition.
2121 */
2122 @Override
2123 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2124 final Phone phone = getPhone(subId);
2125 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2126 }
2127
2128 /**
fionaxue559f972017-01-24 22:31:12 -08002129 * Send the dialer code if called from the current default dialer and the input code follows the
2130 * format of *#*#<code>#*#*
2131 * <p>
2132 * Requires Permission:
2133 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2134 *
2135 * @param inputCode The dialer code to send
2136 * @return true if successfully sent, false otherwise
2137 */
2138 @Override
2139 public boolean sendDialerCode(String callingPackage, String inputCode) {
2140 enforceModifyPermission();
2141 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2142 if (TextUtils.equals(callingPackage,
2143 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2144 final Phone phone = getPhone(getDefaultSubscription());
2145 if (phone != null) {
2146 return phone.sendDialerCode(inputCode);
2147 }
2148 }
2149 return false;
2150 }
2151
2152 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002153 * Returns the data network type.
2154 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002155 *
2156 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2157 */
2158 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002159 public int getNetworkType() {
2160 final Phone phone = getPhone(getDefaultSubscription());
2161 if (phone != null) {
2162 return phone.getServiceState().getDataNetworkType();
2163 } else {
2164 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2165 }
Wink Saville36469e72014-06-11 15:17:00 -07002166 }
2167
2168 /**
2169 * Returns the network type for a subId
2170 */
2171 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002172 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2173 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2174 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2175 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002176
Sanket Padawe356d7632015-06-22 14:03:32 -07002177 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002178 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002179 return phone.getServiceState().getDataNetworkType();
2180 } else {
2181 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2182 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002183 }
2184
2185 /**
2186 * Returns the data network type
2187 */
2188 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002189 public int getDataNetworkType(String callingPackage) {
2190 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002191 }
2192
2193 /**
2194 * Returns the data network type for a subId
2195 */
2196 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002197 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2198 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2199 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2200 }
2201
Sanket Padawe356d7632015-06-22 14:03:32 -07002202 final Phone phone = getPhone(subId);
2203 if (phone != null) {
2204 return phone.getServiceState().getDataNetworkType();
2205 } else {
2206 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2207 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002208 }
2209
2210 /**
Wink Saville36469e72014-06-11 15:17:00 -07002211 * Returns the Voice network type for a subId
2212 */
2213 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002214 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2215 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2216 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2217 }
2218
Sanket Padawe356d7632015-06-22 14:03:32 -07002219 final Phone phone = getPhone(subId);
2220 if (phone != null) {
2221 return phone.getServiceState().getVoiceNetworkType();
2222 } else {
2223 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2224 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002225 }
2226
2227 /**
2228 * @return true if a ICC card is present
2229 */
2230 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002231 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002232 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002233 }
2234
2235 /**
2236 * @return true if a ICC card is present for a slotId
2237 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002238 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002239 public boolean hasIccCardUsingSlotId(int slotId) {
fionaxu6e6c68b2016-06-23 13:31:32 -07002240 final Phone phone = PhoneFactory.getPhone(slotId);
2241 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002242 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002243 } else {
2244 return false;
2245 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002246 }
2247
2248 /**
2249 * Return if the current radio is LTE on CDMA. This
2250 * is a tri-state return value as for a period of time
2251 * the mode may be unknown.
2252 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002253 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002254 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002255 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002256 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002257 @Override
2258 public int getLteOnCdmaMode(String callingPackage) {
2259 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002260 }
2261
Sanket Padawe356d7632015-06-22 14:03:32 -07002262 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002263 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2264 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2265 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2266 }
2267
Sanket Padawe356d7632015-06-22 14:03:32 -07002268 final Phone phone = getPhone(subId);
2269 if (phone == null) {
2270 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2271 } else {
2272 return phone.getLteOnCdmaMode();
2273 }
Wink Saville36469e72014-06-11 15:17:00 -07002274 }
2275
2276 public void setPhone(Phone phone) {
2277 mPhone = phone;
2278 }
2279
2280 /**
2281 * {@hide}
2282 * Returns Default subId, 0 in the case of single standby.
2283 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002284 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002285 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002286 }
2287
Shishir Agrawala9f32182016-04-12 12:00:16 -07002288 private int getSlotForDefaultSubscription() {
2289 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2290 }
2291
Wink Savilleb564aae2014-10-23 10:18:09 -07002292 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002293 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002294 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002295
2296 /**
2297 * @see android.telephony.TelephonyManager.WifiCallingChoices
2298 */
2299 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002300 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2301 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002302 }
2303
2304 /**
2305 * @see android.telephony.TelephonyManager.WifiCallingChoices
2306 */
2307 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002308 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2309 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2310 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002311 }
2312
Sailesh Nepald1e68152013-12-12 19:08:02 -08002313 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002314 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002315 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002316 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002317
Shishir Agrawal566b7612013-10-28 14:41:00 -07002318 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002319 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2320 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002321
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002322 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002323 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002324 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002325 if (DBG) log("iccOpenLogicalChannel: " + response);
2326 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002327 }
2328
2329 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002330 public boolean iccCloseLogicalChannel(int subId, int channel) {
2331 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002332
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002333 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002334 if (channel < 0) {
2335 return false;
2336 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002337 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002338 if (DBG) log("iccCloseLogicalChannel: " + success);
2339 return success;
2340 }
2341
2342 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002343 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002344 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002345 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002346
2347 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002348 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2349 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002350 " data=" + data);
2351 }
2352
2353 if (channel < 0) {
2354 return "";
2355 }
2356
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002357 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002358 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002359 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2360
Shishir Agrawal566b7612013-10-28 14:41:00 -07002361 // Append the returned status code to the end of the response payload.
2362 String s = Integer.toHexString(
2363 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002364 if (response.payload != null) {
2365 s = IccUtils.bytesToHexString(response.payload) + s;
2366 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002367 return s;
2368 }
Jake Hambye994d462014-02-03 13:10:13 -08002369
Evan Charltonc66da362014-05-16 14:06:40 -07002370 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002371 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002372 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002373 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002374
2375 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002376 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2377 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002378 }
2379
2380 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002381 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002382 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2383
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002384 // Append the returned status code to the end of the response payload.
2385 String s = Integer.toHexString(
2386 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002387 if (response.payload != null) {
2388 s = IccUtils.bytesToHexString(response.payload) + s;
2389 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002390 return s;
2391 }
2392
2393 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002394 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002395 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002396 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002397
2398 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002399 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002400 p1 + " " + p2 + " " + p3 + ":" + filePath);
2401 }
2402
2403 IccIoResult response =
2404 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002405 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2406 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002407
2408 if (DBG) {
2409 log("Exchange SIM_IO [R]" + response);
2410 }
2411
2412 byte[] result = null;
2413 int length = 2;
2414 if (response.payload != null) {
2415 length = 2 + response.payload.length;
2416 result = new byte[length];
2417 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2418 } else {
2419 result = new byte[length];
2420 }
2421
2422 result[length - 1] = (byte) response.sw2;
2423 result[length - 2] = (byte) response.sw1;
2424 return result;
2425 }
2426
2427 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002428 public String sendEnvelopeWithStatus(int subId, String content) {
2429 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002430
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002431 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002432 if (response.payload == null) {
2433 return "";
2434 }
2435
2436 // Append the returned status code to the end of the response payload.
2437 String s = Integer.toHexString(
2438 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2439 s = IccUtils.bytesToHexString(response.payload) + s;
2440 return s;
2441 }
2442
Jake Hambye994d462014-02-03 13:10:13 -08002443 /**
2444 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2445 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2446 *
2447 * @param itemID the ID of the item to read
2448 * @return the NV item as a String, or null on error.
2449 */
2450 @Override
2451 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002452 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002453 if (DBG) log("nvReadItem: item " + itemID);
2454 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2455 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2456 return value;
2457 }
2458
2459 /**
2460 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2461 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2462 *
2463 * @param itemID the ID of the item to read
2464 * @param itemValue the value to write, as a String
2465 * @return true on success; false on any failure
2466 */
2467 @Override
2468 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002469 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002470 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2471 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2472 new Pair<Integer, String>(itemID, itemValue));
2473 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2474 return success;
2475 }
2476
2477 /**
2478 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2479 * Used for device configuration by some CDMA operators.
2480 *
2481 * @param preferredRoamingList byte array containing the new PRL
2482 * @return true on success; false on any failure
2483 */
2484 @Override
2485 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002486 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002487 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2488 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2489 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2490 return success;
2491 }
2492
2493 /**
2494 * Perform the specified type of NV config reset.
2495 * Used for device configuration by some CDMA operators.
2496 *
2497 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2498 * @return true on success; false on any failure
2499 */
2500 @Override
2501 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002502 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002503 if (DBG) log("nvResetConfig: type " + resetType);
2504 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2505 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2506 return success;
2507 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002508
2509 /**
Wink Saville36469e72014-06-11 15:17:00 -07002510 * {@hide}
2511 * Returns Default sim, 0 in the case of single standby.
2512 */
2513 public int getDefaultSim() {
2514 //TODO Need to get it from Telephony Devcontroller
2515 return 0;
2516 }
2517
Svet Ganovb320e182015-04-16 12:30:10 -07002518 public String[] getPcscfAddress(String apnType, String callingPackage) {
2519 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2520 return new String[0];
2521 }
2522
2523
ram87fca6f2014-07-18 18:58:44 +05302524 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002525 }
2526
Brad Ebinger51f743a2017-01-23 13:50:20 -08002527 /**
2528 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2529 * feature or {@link null} if the service is not available. If an ImsServiceController is
2530 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2531 * feature updates.
2532 */
2533 public IImsServiceController getImsServiceControllerAndListen(int slotId, int feature,
2534 IImsServiceFeatureListener callback) {
2535 enforceModifyPermission();
2536 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotId, feature,
2537 callback);
2538 }
2539
Wink Saville36469e72014-06-11 15:17:00 -07002540 public void setImsRegistrationState(boolean registered) {
2541 enforceModifyPermission();
2542 mPhone.setImsRegistrationState(registered);
2543 }
2544
2545 /**
Stuart Scott54788802015-03-30 13:18:01 -07002546 * Set the network selection mode to automatic.
2547 *
2548 */
2549 @Override
2550 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002551 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002552 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2553 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2554 }
2555
2556 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002557 * Set the network selection mode to manual with the selected carrier.
2558 */
2559 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002560 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2561 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002562 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002563 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002564 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2565 persistSelection);
2566 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002567 }
2568
2569 /**
2570 * Scans for available networks.
2571 */
2572 @Override
2573 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002574 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002575 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2576 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2577 CMD_PERFORM_NETWORK_SCAN, null, subId);
2578 return result;
2579 }
2580
2581 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002582 * Get the calculated preferred network type.
2583 * Used for debugging incorrect network type.
2584 *
2585 * @return the preferred network type, defined in RILConstants.java.
2586 */
2587 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002588 public int getCalculatedPreferredNetworkType(String callingPackage) {
2589 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2590 return RILConstants.PREFERRED_NETWORK_MODE;
2591 }
2592
Amit Mahajan43330e02014-11-18 11:54:45 -08002593 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002594 }
2595
2596 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002597 * Get the preferred network type.
2598 * Used for device configuration by some CDMA operators.
2599 *
2600 * @return the preferred network type, defined in RILConstants.java.
2601 */
2602 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002603 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002604 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002605 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002606 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002607 int networkType = (result != null ? result[0] : -1);
2608 if (DBG) log("getPreferredNetworkType: " + networkType);
2609 return networkType;
2610 }
2611
2612 /**
2613 * Set the preferred network type.
2614 * Used for device configuration by some CDMA operators.
2615 *
2616 * @param networkType the preferred network type, defined in RILConstants.java.
2617 * @return true on success; false on any failure.
2618 */
2619 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002620 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002621 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002622 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2623 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002624 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002625 if (success) {
2626 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002627 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002628 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002629 return success;
2630 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002631
2632 /**
Junda Liu475951f2014-11-07 16:45:03 -08002633 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2634 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2635 * tethering.
2636 *
2637 * @return 0: Not required. 1: required. 2: Not set.
2638 * @hide
2639 */
2640 @Override
2641 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002642 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002643 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2644 Settings.Global.TETHER_DUN_REQUIRED, 2);
2645 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2646 // config_tether_apndata.
2647 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2648 dunRequired = 1;
2649 }
2650 return dunRequired;
2651 }
2652
2653 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002654 * Set mobile data enabled
2655 * Used by the user through settings etc to turn on/off mobile data
2656 *
2657 * @param enable {@code true} turn turn data on, else {@code false}
2658 */
2659 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002660 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002661 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002662 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002663 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002664 Phone phone = PhoneFactory.getPhone(phoneId);
2665 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002666 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002667 phone.setDataEnabled(enable);
2668 } else {
2669 loge("setDataEnabled: no phone for subId=" + subId);
2670 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002671 }
2672
2673 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002674 * Get whether mobile data is enabled.
2675 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002676 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002677 *
2678 * @return {@code true} if data is enabled else {@code false}
2679 */
2680 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002681 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002682 try {
2683 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2684 null);
2685 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002686 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002687 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002688 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002689 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002690 Phone phone = PhoneFactory.getPhone(phoneId);
2691 if (phone != null) {
2692 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002693 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002694 return retVal;
2695 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002696 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002697 return false;
2698 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002699 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002700
2701 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002702 public int getCarrierPrivilegeStatus(int subId) {
2703 final Phone phone = getPhone(subId);
2704 if (phone == null) {
2705 loge("getCarrierPrivilegeStatus: Invalid subId");
2706 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2707 }
2708 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002709 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002710 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002711 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2712 }
2713 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002714 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002715 }
Junda Liu29340342014-07-10 15:23:27 -07002716
2717 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002718 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002719 if (TextUtils.isEmpty(pkgName))
2720 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002721 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002722 if (card == null) {
2723 loge("checkCarrierPrivilegesForPackage: No UICC");
2724 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2725 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002726 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2727 }
2728
2729 @Override
2730 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002731 if (TextUtils.isEmpty(pkgName))
2732 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002733 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2734 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2735 UiccCard card = UiccController.getInstance().getUiccCard(i);
2736 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002737 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002738 continue;
2739 }
2740
2741 result = card.getCarrierPrivilegeStatus(
2742 mPhone.getContext().getPackageManager(), pkgName);
2743 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2744 break;
2745 }
2746 }
2747
2748 return result;
Junda Liu29340342014-07-10 15:23:27 -07002749 }
Derek Tan89e89d42014-07-08 17:00:10 -07002750
2751 @Override
Junda Liue64de782015-04-16 17:19:16 -07002752 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2753 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2754 loge("phoneId " + phoneId + " is not valid.");
2755 return null;
2756 }
2757 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002758 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002759 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002760 return null ;
2761 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002762 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002763 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002764 }
2765
Amith Yamasani6e118872016-02-19 12:53:51 -08002766 @Override
2767 public List<String> getPackagesWithCarrierPrivileges() {
2768 PackageManager pm = mPhone.getContext().getPackageManager();
2769 List<String> privilegedPackages = new ArrayList<>();
2770 List<PackageInfo> packages = null;
2771 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2772 UiccCard card = UiccController.getInstance().getUiccCard(i);
2773 if (card == null) {
2774 // No UICC in that slot.
2775 continue;
2776 }
2777 if (card.hasCarrierPrivilegeRules()) {
2778 if (packages == null) {
2779 // Only check packages in user 0 for now
2780 packages = pm.getInstalledPackagesAsUser(
2781 PackageManager.MATCH_DISABLED_COMPONENTS
2782 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2783 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2784 }
2785 for (int p = packages.size() - 1; p >= 0; p--) {
2786 PackageInfo pkgInfo = packages.get(p);
2787 if (pkgInfo != null && pkgInfo.packageName != null
2788 && card.getCarrierPrivilegeStatus(pkgInfo)
2789 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2790 privilegedPackages.add(pkgInfo.packageName);
2791 }
2792 }
2793 }
2794 }
2795 return privilegedPackages;
2796 }
2797
Wink Savilleb564aae2014-10-23 10:18:09 -07002798 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002799 final Phone phone = getPhone(subId);
2800 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002801 if (card == null) {
2802 loge("getIccId: No UICC");
2803 return null;
2804 }
2805 String iccId = card.getIccId();
2806 if (TextUtils.isEmpty(iccId)) {
2807 loge("getIccId: ICC ID is null or empty.");
2808 return null;
2809 }
2810 return iccId;
2811 }
2812
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002813 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002814 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2815 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002816 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002817
Jeff Sharkey85190e62014-12-05 09:40:12 -08002818 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002819 final Phone phone = getPhone(subId);
2820 if (phone == null) {
2821 return false;
2822 }
2823 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002824
2825 if (DBG_MERGE) {
2826 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2827 + subscriberId + " to " + number);
2828 }
2829
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002830 if (TextUtils.isEmpty(iccId)) {
2831 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002832 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002833
Jeff Sharkey85190e62014-12-05 09:40:12 -08002834 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2835
2836 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002837 if (alphaTag == null) {
2838 editor.remove(alphaTagPrefKey);
2839 } else {
2840 editor.putString(alphaTagPrefKey, alphaTag);
2841 }
2842
Jeff Sharkey85190e62014-12-05 09:40:12 -08002843 // Record both the line number and IMSI for this ICCID, since we need to
2844 // track all merged IMSIs based on line number
2845 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2846 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002847 if (number == null) {
2848 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002849 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002850 } else {
2851 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002852 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002853 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002854
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002855 editor.commit();
2856 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002857 }
2858
2859 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002860 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002861 // This is open to apps with WRITE_SMS.
2862 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002863 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002864 return null;
2865 }
Derek Tan97ebb422014-09-05 16:55:38 -07002866
2867 String iccId = getIccId(subId);
2868 if (iccId != null) {
2869 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002870 if (DBG_MERGE) {
2871 log("getLine1NumberForDisplay returning " +
2872 mTelephonySharedPreferences.getString(numberPrefKey, null));
2873 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002874 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002875 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002876 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002877 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002878 }
2879
2880 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002881 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2882 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2883 return null;
2884 }
Derek Tan97ebb422014-09-05 16:55:38 -07002885
2886 String iccId = getIccId(subId);
2887 if (iccId != null) {
2888 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002889 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002890 }
Derek Tan97ebb422014-09-05 16:55:38 -07002891 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002892 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002893
2894 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002895 public String[] getMergedSubscriberIds(String callingPackage) {
2896 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2897 return null;
2898 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002899 final Context context = mPhone.getContext();
2900 final TelephonyManager tele = TelephonyManager.from(context);
2901 final SubscriptionManager sub = SubscriptionManager.from(context);
2902
2903 // Figure out what subscribers are currently active
2904 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002905 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2906 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2907 final long identity = Binder.clearCallingIdentity();
2908 try {
2909 final int[] subIds = sub.getActiveSubscriptionIdList();
2910 for (int subId : subIds) {
2911 activeSubscriberIds.add(tele.getSubscriberId(subId));
2912 }
2913 } finally {
2914 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002915 }
2916
2917 // First pass, find a number override for an active subscriber
2918 String mergeNumber = null;
2919 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2920 for (String key : prefs.keySet()) {
2921 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2922 final String subscriberId = (String) prefs.get(key);
2923 if (activeSubscriberIds.contains(subscriberId)) {
2924 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2925 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2926 mergeNumber = (String) prefs.get(numberKey);
2927 if (DBG_MERGE) {
2928 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2929 + " for active subscriber " + subscriberId);
2930 }
2931 if (!TextUtils.isEmpty(mergeNumber)) {
2932 break;
2933 }
2934 }
2935 }
2936 }
2937
2938 // Shortcut when no active merged subscribers
2939 if (TextUtils.isEmpty(mergeNumber)) {
2940 return null;
2941 }
2942
2943 // Second pass, find all subscribers under that line override
2944 final ArraySet<String> result = new ArraySet<>();
2945 for (String key : prefs.keySet()) {
2946 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2947 final String number = (String) prefs.get(key);
2948 if (mergeNumber.equals(number)) {
2949 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2950 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2951 final String subscriberId = (String) prefs.get(subscriberKey);
2952 if (!TextUtils.isEmpty(subscriberId)) {
2953 result.add(subscriberId);
2954 }
2955 }
2956 }
2957 }
2958
2959 final String[] resultArray = result.toArray(new String[result.size()]);
2960 Arrays.sort(resultArray);
2961 if (DBG_MERGE) {
2962 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2963 }
2964 return resultArray;
2965 }
2966
2967 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002968 public boolean setOperatorBrandOverride(int subId, String brand) {
2969 enforceCarrierPrivilege(subId);
2970 final Phone phone = getPhone(subId);
2971 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002972 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002973
2974 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002975 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002976 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2977 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002978 enforceCarrierPrivilege(subId);
2979 final Phone phone = getPhone(subId);
2980 if (phone == null) {
2981 return false;
2982 }
2983 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002984 cdmaNonRoamingList);
2985 }
2986
2987 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002988 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2989 enforceModifyPermission();
2990
2991 int returnValue = 0;
2992 try {
2993 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2994 if(result.exception == null) {
2995 if (result.result != null) {
2996 byte[] responseData = (byte[])(result.result);
2997 if(responseData.length > oemResp.length) {
2998 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2999 responseData.length + "bytes. Buffer Size is " +
3000 oemResp.length + "bytes.");
3001 }
3002 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3003 returnValue = responseData.length;
3004 }
3005 } else {
3006 CommandException ex = (CommandException) result.exception;
3007 returnValue = ex.getCommandError().ordinal();
3008 if(returnValue > 0) returnValue *= -1;
3009 }
3010 } catch (RuntimeException e) {
3011 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3012 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3013 if(returnValue > 0) returnValue *= -1;
3014 }
3015
3016 return returnValue;
3017 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003018
3019 @Override
3020 public void setRadioCapability(RadioAccessFamily[] rafs) {
3021 try {
3022 ProxyController.getInstance().setRadioCapability(rafs);
3023 } catch (RuntimeException e) {
3024 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3025 }
3026 }
3027
3028 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003029 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3030 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3031 return RadioAccessFamily.RAF_UNKNOWN;
3032 }
3033
Wink Saville5d475dd2014-10-17 15:00:58 -07003034 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3035 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003036
3037 @Override
3038 public void enableVideoCalling(boolean enable) {
3039 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003040 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003041 }
3042
3043 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003044 public boolean isVideoCallingEnabled(String callingPackage) {
3045 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3046 return false;
3047 }
3048
Andrew Lee77527ac2014-10-21 16:57:39 -07003049 // Check the user preference and the system-level IMS setting. Even if the user has
3050 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3051 // In the long run, we may instead need to check if there exists a connection service
3052 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003053 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3054 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003055 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003056 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003057
Andrew Leea1239f22015-03-02 17:44:07 -08003058 @Override
3059 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003060 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003061 }
3062
3063 @Override
3064 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003065 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003066 }
3067
Andrew Lee9431b832015-03-09 18:46:45 -07003068 @Override
3069 public boolean isTtyModeSupported() {
3070 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3071 TelephonyManager telephonyManager =
3072 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003073 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003074 }
3075
3076 @Override
3077 public boolean isHearingAidCompatibilitySupported() {
3078 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3079 }
3080
Sanket Padawe7310cc72015-01-14 09:53:20 -08003081 /**
3082 * Returns the unique device ID of phone, for example, the IMEI for
3083 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3084 *
3085 * <p>Requires Permission:
3086 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3087 */
3088 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003089 public String getDeviceId(String callingPackage) {
3090 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3091 return null;
3092 }
3093
Sanket Padawe7310cc72015-01-14 09:53:20 -08003094 final Phone phone = PhoneFactory.getPhone(0);
3095 if (phone != null) {
3096 return phone.getDeviceId();
3097 } else {
3098 return null;
3099 }
3100 }
3101
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003102 /*
3103 * {@hide}
3104 * Returns the IMS Registration Status
3105 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003106 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003107 public boolean isImsRegistered() {
3108 return mPhone.isImsRegistered();
3109 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003110
3111 @Override
3112 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3113 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3114 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003115
Nathan Haroldc55097a2015-03-11 18:14:50 -07003116 /*
3117 * {@hide}
3118 * Returns the IMS Registration Status
3119 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003120 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003121 return mPhone.isWifiCallingEnabled();
3122 }
3123
3124 /*
3125 * {@hide}
3126 * Returns the IMS Registration Status
3127 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003128 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003129 return mPhone.isVolteEnabled();
3130 }
Svet Ganovb320e182015-04-16 12:30:10 -07003131
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003132 /*
3133 * {@hide} Returns the IMS Registration Status
3134 */
3135 public boolean isVideoTelephonyAvailable() {
3136 return mPhone.isVideoEnabled();
3137 }
3138
Svet Ganovb320e182015-04-16 12:30:10 -07003139 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003140 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003141 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003142 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3143
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003144 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003145 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003146 } catch (SecurityException e) {
3147 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3148 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003149 }
Svet Ganovb320e182015-04-16 12:30:10 -07003150
3151 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3152 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3153 return false;
3154 }
3155
3156 return true;
3157 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003158
Makoto Onukifee69342015-06-29 14:44:50 -07003159 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003160 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003161 */
3162 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003163 // Default SMS app can always read it.
3164 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3165 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3166 return true;
3167 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003168
Makoto Onukie4072d12015-08-03 15:12:23 -07003169 try {
3170 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003171 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003172 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003173 // Can be read with READ_SMS too.
3174 try {
3175 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3176 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3177 if (opCode != AppOpsManager.OP_NONE) {
3178 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3179 == AppOpsManager.MODE_ALLOWED;
3180 } else {
3181 return true;
3182 }
3183 } catch (SecurityException readSmsSecurityException) {
3184 }
3185 // Can be read with READ_PHONE_NUMBER too.
3186 try {
3187 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBER,
3188 message);
3189 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBER);
3190 if (opCode != AppOpsManager.OP_NONE) {
3191 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3192 == AppOpsManager.MODE_ALLOWED;
3193 } else {
3194 return true;
3195 }
3196 } catch (SecurityException readPhoneNumberSecurityException) {
3197 }
3198
3199 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3200 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3201 ", " + android.Manifest.permission.READ_SMS + ", or " +
3202 android.Manifest.permission.READ_PHONE_NUMBER);
Makoto Onukifee69342015-06-29 14:44:50 -07003203 }
3204
Stuart Scott8eef64f2015-04-08 15:13:54 -07003205 @Override
3206 public void factoryReset(int subId) {
3207 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003208 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3209 return;
3210 }
3211
Svet Ganovcc087f82015-05-12 20:35:54 -07003212 final long identity = Binder.clearCallingIdentity();
3213 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003214 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3215 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003216 // Enable data
3217 setDataEnabled(subId, true);
3218 // Set network selection mode to automatic
3219 setNetworkSelectionModeAutomatic(subId);
3220 // Set preferred mobile network type to the best available
3221 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3222 // Turn off roaming
3223 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3224 }
3225 } finally {
3226 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003227 }
3228 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003229
3230 @Override
3231 public String getLocaleFromDefaultSim() {
3232 // We query all subscriptions instead of just the active ones, because
3233 // this might be called early on in the provisioning flow when the
3234 // subscriptions potentially aren't active yet.
3235 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3236 if (slist == null || slist.isEmpty()) {
3237 return null;
3238 }
3239
3240 // This function may be called very early, say, from the setup wizard, at
3241 // which point we won't have a default subscription set. If that's the case
3242 // we just choose the first, which will be valid in "most cases".
3243 final int defaultSubId = getDefaultSubscription();
3244 SubscriptionInfo info = null;
3245 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3246 info = slist.get(0);
3247 } else {
3248 for (SubscriptionInfo item : slist) {
3249 if (item.getSubscriptionId() == defaultSubId) {
3250 info = item;
3251 break;
3252 }
3253 }
3254
3255 if (info == null) {
3256 return null;
3257 }
3258 }
3259
3260 // Try and fetch the locale from the carrier properties or from the SIM language
3261 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003262 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003263 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003264 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003265 if (defaultPhone != null) {
3266 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3267 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003268 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003269 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3270 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003271 } else {
3272 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003273 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003274 }
3275 }
3276
Narayan Kamath011676f2015-07-29 12:04:08 +01003277 // The SIM language preferences only store a language (e.g. fr = French), not an
3278 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3279 // the SIM and carrier preferences does not include a country we add the country
3280 // determined from the SIM MCC to provide an exact locale.
3281 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003282 if (mccLocale != null) {
3283 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3284 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003285 }
3286
Tony Hill183b2de2015-06-24 14:53:58 +01003287 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003288 return null;
3289 }
3290
3291 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3292 final long identity = Binder.clearCallingIdentity();
3293 try {
3294 return mSubscriptionController.getAllSubInfoList(
3295 mPhone.getContext().getOpPackageName());
3296 } finally {
3297 Binder.restoreCallingIdentity(identity);
3298 }
3299 }
3300
3301 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3302 final long identity = Binder.clearCallingIdentity();
3303 try {
3304 return mSubscriptionController.getActiveSubscriptionInfoList(
3305 mPhone.getContext().getOpPackageName());
3306 } finally {
3307 Binder.restoreCallingIdentity(identity);
3308 }
3309 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003310
3311 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003312 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3313 * representing the state of the modem.
3314 *
3315 * NOTE: This clears the modem state, so there should only every be one caller.
3316 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003317 */
3318 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003319 public void requestModemActivityInfo(ResultReceiver result) {
3320 enforceModifyPermission();
3321
3322 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3323 Bundle bundle = new Bundle();
3324 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3325 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003326 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003327
3328 /**
3329 * {@hide}
3330 * Returns the service state information on specified subscription.
3331 */
3332 @Override
3333 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3334
3335 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3336 return null;
3337 }
3338
3339 final Phone phone = getPhone(subId);
3340 if (phone == null) {
3341 return null;
3342 }
3343
3344 return phone.getServiceState();
3345 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003346
3347 /**
3348 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3349 *
3350 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3351 * voicemail ringtone.
3352 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3353 * PhoneAccount.
3354 */
3355 @Override
3356 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3357 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3358 if (phone == null) {
3359 return null;
3360 }
3361
3362 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3363 }
3364
3365 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003366 * Sets the per-account voicemail ringtone.
3367 *
3368 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3369 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3370 *
3371 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3372 * voicemail ringtone.
3373 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3374 * PhoneAccount.
3375 */
3376 @Override
3377 public void setVoicemailRingtoneUri(String callingPackage,
3378 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3379 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3380 if (!TextUtils.equals(callingPackage,
3381 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3382 enforceModifyPermissionOrCarrierPrivilege(
3383 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3384 }
3385 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3386 if (phone == null){
3387 throw new IllegalArgumentException("The phoneAccountHandle does not correspond to an "
3388 + "active phone account.");
3389 }
3390 VoicemailNotificationSettingsUtil.setRingtoneUri(phone, uri);
3391 }
3392
3393 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003394 * Returns whether vibration is set for voicemail notification in Phone settings.
3395 *
3396 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3397 * voicemail vibration setting.
3398 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3399 */
3400 @Override
3401 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3402 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3403 if (phone == null) {
3404 return false;
3405 }
3406
3407 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3408 }
3409
Youhan Wange64578a2016-05-02 15:32:42 -07003410 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003411 * Sets the per-account voicemail vibration.
3412 *
3413 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3414 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3415 *
3416 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3417 * voicemail vibration setting.
3418 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3419 * specific PhoneAccount.
3420 */
3421 @Override
3422 public void setVoicemailVibrationEnabled(String callingPackage,
3423 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3424 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3425 if (!TextUtils.equals(callingPackage,
3426 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3427 enforceModifyPermissionOrCarrierPrivilege(
3428 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3429 }
3430
3431 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3432 if (phone == null){
3433 throw new IllegalArgumentException("The phoneAccountHandle does not correspond to an "
3434 + "active phone account.");
3435 }
3436 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone, enabled);
3437 }
3438
3439 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003440 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3441 *
3442 * @throws SecurityException if the caller does not have the required permission
3443 */
3444 private void enforceReadPrivilegedPermission() {
3445 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3446 null);
3447 }
3448
3449 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003450 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3451 * permission.
3452 *
3453 * @throws SecurityException if the caller does not have the required permission
3454 */
3455 private void enforceSendSmsPermission() {
3456 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3457 }
3458
3459 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003460 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003461 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003462 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003463 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003464 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
3465 String vvmPackage = RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId)
3466 .getPackageName();
3467 if (!callingPackage.equals(vvmPackage)) {
3468 throw new SecurityException("Caller not current active visual voicemail package[" +
3469 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003470 }
3471 }
3472
3473 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003474 * Return the application ID for the app type.
3475 *
3476 * @param subId the subscription ID that this request applies to.
3477 * @param appType the uicc app type.
3478 * @return Application ID for specificied app type, or null if no uicc.
3479 */
3480 @Override
3481 public String getAidForAppType(int subId, int appType) {
3482 enforceReadPrivilegedPermission();
3483 Phone phone = getPhone(subId);
3484 if (phone == null) {
3485 return null;
3486 }
3487 String aid = null;
3488 try {
3489 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3490 .getApplicationByType(appType).getAid();
3491 } catch (Exception e) {
3492 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3493 }
3494 return aid;
3495 }
3496
Youhan Wang4001d252016-05-11 10:29:41 -07003497 /**
3498 * Return the Electronic Serial Number.
3499 *
3500 * @param subId the subscription ID that this request applies to.
3501 * @return ESN or null if error.
3502 */
3503 @Override
3504 public String getEsn(int subId) {
3505 enforceReadPrivilegedPermission();
3506 Phone phone = getPhone(subId);
3507 if (phone == null) {
3508 return null;
3509 }
3510 String esn = null;
3511 try {
3512 esn = phone.getEsn();
3513 } catch (Exception e) {
3514 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3515 }
3516 return esn;
3517 }
3518
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003519 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003520 * Return the Preferred Roaming List Version.
3521 *
3522 * @param subId the subscription ID that this request applies to.
3523 * @return PRLVersion or null if error.
3524 */
3525 @Override
3526 public String getCdmaPrlVersion(int subId) {
3527 enforceReadPrivilegedPermission();
3528 Phone phone = getPhone(subId);
3529 if (phone == null) {
3530 return null;
3531 }
3532 String cdmaPrlVersion = null;
3533 try {
3534 cdmaPrlVersion = phone.getCdmaPrlVersion();
3535 } catch (Exception e) {
3536 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3537 }
3538 return cdmaPrlVersion;
3539 }
3540
3541 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003542 * Get snapshot of Telephony histograms
3543 * @return List of Telephony histograms
3544 * @hide
3545 */
3546 @Override
3547 public List<TelephonyHistogram> getTelephonyHistograms() {
3548 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3549 return RIL.getTelephonyRILTimingHistograms();
3550 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003551
3552 /**
3553 * {@hide}
3554 * Set the allowed carrier list for slotId
3555 * Require system privileges. In the future we may add this to carrier APIs.
3556 *
3557 * @return The number of carriers set successfully, should match length of carriers
3558 */
3559 @Override
3560 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3561 enforceModifyPermission();
3562 int subId = SubscriptionManager.getSubId(slotId)[0];
Meng Wang9b7c4e92017-02-17 11:41:27 -08003563 if (carriers == null) {
3564 throw new NullPointerException("carriers cannot be null");
3565 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003566 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3567 return retVal[0];
3568 }
3569
3570 /**
3571 * {@hide}
3572 * Get the allowed carrier list for slotId.
3573 * Require system privileges. In the future we may add this to carrier APIs.
3574 *
3575 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3576 * means all carriers are allowed.
3577 */
3578 @Override
3579 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3580 enforceReadPrivilegedPermission();
3581 int subId = SubscriptionManager.getSubId(slotId)[0];
3582 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3583 }
3584
fionaxu59545b42016-05-25 15:53:37 -07003585 /**
3586 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3587 * @param subId the subscription ID that this action applies to.
3588 * @param enabled control enable or disable metered apns.
3589 * {@hide}
3590 */
3591 @Override
3592 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3593 enforceModifyPermission();
3594 final Phone phone = getPhone(subId);
3595 if (phone == null) {
3596 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3597 return;
3598 }
3599 try {
3600 phone.carrierActionSetMeteredApnsEnabled(enabled);
3601 } catch (Exception e) {
3602 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3603 }
3604 }
3605
3606 /**
3607 * Action set from carrier signalling broadcast receivers to enable/disable radio
3608 * @param subId the subscription ID that this action applies to.
3609 * @param enabled control enable or disable radio.
3610 * {@hide}
3611 */
3612 @Override
3613 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3614 enforceModifyPermission();
3615 final Phone phone = getPhone(subId);
3616 if (phone == null) {
3617 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3618 return;
3619 }
3620 try {
3621 phone.carrierActionSetRadioEnabled(enabled);
3622 } catch (Exception e) {
3623 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3624 }
3625 }
3626
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003627 /**
3628 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3629 * bug report is being generated.
3630 */
3631 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003632 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003633 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3634 != PackageManager.PERMISSION_GRANTED) {
3635 writer.println("Permission Denial: can't dump Phone from pid="
3636 + Binder.getCallingPid()
3637 + ", uid=" + Binder.getCallingUid()
3638 + "without permission "
3639 + android.Manifest.permission.DUMP);
3640 return;
3641 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003642 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003643 }
Jack Yueb89b242016-06-22 13:27:47 -07003644
3645 /**
3646 * Get aggregated video call data usage from all subscriptions since boot.
3647 * @return total data usage in bytes
3648 * {@hide}
3649 */
3650 @Override
3651 public long getVtDataUsage() {
3652 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3653 null);
3654
3655 // NetworkStatsService keeps tracking the active network interface and identity. It will
3656 // record the delta with the corresponding network identity. What we need to do here is
3657 // returning total video call data usage from all subscriptions since boot.
3658
3659 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3660 // simultaneous VT calls.
3661 final Phone[] phones = PhoneFactory.getPhones();
3662 long total = 0;
3663 for (Phone phone : phones) {
3664 total += phone.getVtDataUsage();
3665 }
3666 return total;
3667 }
Jack Yu75ab2952016-07-08 14:29:33 -07003668
3669 /**
3670 * Policy control of data connection. Usually used when data limit is passed.
3671 * @param enabled True if enabling the data, otherwise disabling.
3672 * @param subId Subscription index
3673 * {@hide}
3674 */
3675 @Override
3676 public void setPolicyDataEnabled(boolean enabled, int subId) {
3677 enforceModifyPermission();
3678 Phone phone = getPhone(subId);
3679 if (phone != null) {
3680 phone.setPolicyDataEnabled(enabled);
3681 }
3682 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003683
3684 /**
3685 * Get Client request stats
3686 * @return List of Client Request Stats
3687 * @hide
3688 */
3689 @Override
3690 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3691 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3692 return null;
3693 }
3694
3695 Phone phone = getPhone(subId);
3696 if (phone != null) {
3697 return phone.getClientRequestStats();
3698 }
3699
3700 return null;
3701 }
3702
3703 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3704 if (workSource != null) {
3705 return workSource;
3706 }
3707
3708 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3709 workSource = new WorkSource(uid, packageName);
3710 return workSource;
3711 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003712}