blob: 2d72056de088634bffae8166c4c1a423fbb20c8d [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 Yen1b455992016-12-27 14:52:32 -080021import android.Manifest.permission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.ActivityManager;
23import android.app.AppOpsManager;
Ta-wei Yen1b455992016-12-27 14:52:32 -080024import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070025import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.content.Context;
27import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070028import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080029import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070030import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.net.Uri;
32import android.os.AsyncResult;
33import android.os.Binder;
34import android.os.Bundle;
35import android.os.Handler;
36import android.os.Looper;
37import android.os.Message;
38import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070039import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.ServiceManager;
41import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070042import android.os.UserManager;
Sooraj Sasindran22882212016-07-18 11:57:25 -070043import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070044import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080045import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070046import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080047import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080048import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070049import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070050import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.telephony.CellInfo;
Sooraj Sasindran22882212016-07-18 11:57:25 -070052import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070053import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070054import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080055import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070056import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.telephony.ServiceState;
Ta-wei Yen1b455992016-12-27 14:52:32 -080058import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080059import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080060import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070061import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070062import android.telephony.TelephonyManager;
63import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080065import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080067import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080068import android.util.Slog;
Ta-wei Yen1b455992016-12-27 14:52:32 -080069
Andrew Lee312e8172014-10-23 17:01:36 -070070import com.android.ims.ImsManager;
Brad Ebinger76681002017-01-23 13:50:20 -080071import com.android.ims.internal.IImsServiceController;
72import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070073import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070074import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070075import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080078import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010079import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070080import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070082import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070083import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070084import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070085import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070086import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080087import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070088import com.android.internal.telephony.uicc.IccIoResult;
89import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080090import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070091import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080092import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070093import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080094import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070095import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080096import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yen1b455992016-12-27 14:52:32 -080097
Ta-wei Yenc236d6b2016-06-21 13:33:12 -070098import java.io.FileDescriptor;
99import java.io.PrintWriter;
Ta-wei Yen1b455992016-12-27 14:52:32 -0800100import java.io.UnsupportedEncodingException;
101import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800103import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800104import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100105import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800106import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107
108/**
109 * Implementation of the ITelephony interface.
110 */
Santos Cordon117fee72014-05-16 17:56:12 -0700111public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700112 private static final String LOG_TAG = "PhoneInterfaceManager";
113 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
114 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800115 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116
117 // Message codes used with mMainThreadHandler
118 private static final int CMD_HANDLE_PIN_MMI = 1;
119 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
120 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
121 private static final int CMD_ANSWER_RINGING_CALL = 4;
122 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700123 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
124 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700125 private static final int CMD_OPEN_CHANNEL = 9;
126 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
127 private static final int CMD_CLOSE_CHANNEL = 11;
128 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800129 private static final int CMD_NV_READ_ITEM = 13;
130 private static final int EVENT_NV_READ_ITEM_DONE = 14;
131 private static final int CMD_NV_WRITE_ITEM = 15;
132 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
133 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
134 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
135 private static final int CMD_NV_RESET_CONFIG = 19;
136 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800137 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
138 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
139 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
140 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800141 private static final int CMD_SEND_ENVELOPE = 25;
142 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700143 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
144 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
145 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
146 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
147 private static final int CMD_EXCHANGE_SIM_IO = 31;
148 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800149 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
150 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700151 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
152 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700153 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
154 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700155 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
156 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
157 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
158 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700159 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
160 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
161 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
162 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar7e856482016-10-14 19:37:38 -0700163 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800164 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
165 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700166
167 /** The singleton instance. */
168 private static PhoneInterfaceManager sInstance;
169
Wink Saville3ab207e2014-11-20 13:07:20 -0800170 private PhoneGlobals mApp;
171 private Phone mPhone;
172 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700173 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800174 private AppOpsManager mAppOps;
175 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800176 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800177 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700178
Derek Tan97ebb422014-09-05 16:55:38 -0700179 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
180 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800181 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700182
183 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700184 * A request object to use for transmitting data to an ICC.
185 */
186 private static final class IccAPDUArgument {
187 public int channel, cla, command, p1, p2, p3;
188 public String data;
189
190 public IccAPDUArgument(int channel, int cla, int command,
191 int p1, int p2, int p3, String data) {
192 this.channel = channel;
193 this.cla = cla;
194 this.command = command;
195 this.p1 = p1;
196 this.p2 = p2;
197 this.p3 = p3;
198 this.data = data;
199 }
200 }
201
202 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700203 * A request object to use for transmitting data to an ICC.
204 */
205 private static final class ManualNetworkSelectionArgument {
206 public OperatorInfo operatorInfo;
207 public boolean persistSelection;
208
209 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
210 this.operatorInfo = operatorInfo;
211 this.persistSelection = persistSelection;
212 }
213 }
214
215 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700216 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
217 * request after sending. The main thread will notify the request when it is complete.
218 */
219 private static final class MainThreadRequest {
220 /** The argument to use for the request */
221 public Object argument;
222 /** The result of the request that is run on the main thread */
223 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800224 // The subscriber id that this request applies to. Defaults to
225 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
226 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227
228 public MainThreadRequest(Object argument) {
229 this.argument = argument;
230 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800231
232 public MainThreadRequest(Object argument, Integer subId) {
233 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800234 if (subId != null) {
235 this.subId = subId;
236 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800237 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238 }
239
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800240 private static final class IncomingThirdPartyCallArgs {
241 public final ComponentName component;
242 public final String callId;
243 public final String callerDisplayName;
244
245 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
246 String callerDisplayName) {
247 this.component = component;
248 this.callId = callId;
249 this.callerDisplayName = callerDisplayName;
250 }
251 }
252
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700253 /**
254 * A handler that processes messages on the main thread in the phone process. Since many
255 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
256 * inbound binder threads to the main thread in the phone process. The Binder thread
257 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
258 * on, which will be notified when the operation completes and will contain the result of the
259 * request.
260 *
261 * <p>If a MainThreadRequest object is provided in the msg.obj field,
262 * note that request.result must be set to something non-null for the calling thread to
263 * unblock.
264 */
265 private final class MainThreadHandler extends Handler {
266 @Override
267 public void handleMessage(Message msg) {
268 MainThreadRequest request;
269 Message onCompleted;
270 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800271 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700272 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273
274 switch (msg.what) {
pkanwar7e856482016-10-14 19:37:38 -0700275 case CMD_HANDLE_USSD_REQUEST: {
276 request = (MainThreadRequest) msg.obj;
277 final Phone phone = getPhoneFromRequest(request);
278 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
279 String ussdRequest = ussdObject.first;
280 ResultReceiver wrappedCallback = ussdObject.second;
281 request.result = phone != null ?
282 phone.handleUssdRequest(ussdRequest, wrappedCallback)
283 :false;
284 // Wake up the requesting thread
285 synchronized (request) {
286 request.notifyAll();
287 }
288 break;
289 }
290
Yorke Lee716f67e2015-06-17 15:39:16 -0700291 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700292 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700293 final Phone phone = getPhoneFromRequest(request);
294 request.result = phone != null ?
295 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
296 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700297 // Wake up the requesting thread
298 synchronized (request) {
299 request.notifyAll();
300 }
301 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700302 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303
304 case CMD_HANDLE_NEIGHBORING_CELL:
305 request = (MainThreadRequest) msg.obj;
306 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
307 request);
Sooraj Sasindran22882212016-07-18 11:57:25 -0700308 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309 break;
310
311 case EVENT_NEIGHBORING_CELL_DONE:
312 ar = (AsyncResult) msg.obj;
313 request = (MainThreadRequest) ar.userObj;
314 if (ar.exception == null && ar.result != null) {
315 request.result = ar.result;
316 } else {
317 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800318 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700319 }
320 // Wake up the requesting thread
321 synchronized (request) {
322 request.notifyAll();
323 }
324 break;
325
326 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700327 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800328 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700329 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700330 break;
331
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 case CMD_END_CALL:
333 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800334 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700335 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700336 Phone phone = getPhone(end_subId);
337 if (phone == null) {
338 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
339 break;
340 }
341 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700342 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
343 // CDMA: If the user presses the Power button we treat it as
344 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700345 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700346 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
347 // GSM: End the call as per the Phone state
348 hungUp = PhoneUtils.hangup(mCM);
349 } else {
350 throw new IllegalStateException("Unexpected phone type: " + phoneType);
351 }
352 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
353 request.result = hungUp;
354 // Wake up the requesting thread
355 synchronized (request) {
356 request.notifyAll();
357 }
358 break;
359
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700360 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700361 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700362 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800363 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700364 if (uiccCard == null) {
365 loge("iccTransmitApduLogicalChannel: No UICC");
366 request.result = new IccIoResult(0x6F, 0, (byte[])null);
367 synchronized (request) {
368 request.notifyAll();
369 }
370 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700371 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
372 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700373 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700374 iccArgument.channel, iccArgument.cla, iccArgument.command,
375 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700376 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700377 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700378 break;
379
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700380 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700381 ar = (AsyncResult) msg.obj;
382 request = (MainThreadRequest) ar.userObj;
383 if (ar.exception == null && ar.result != null) {
384 request.result = ar.result;
385 } else {
386 request.result = new IccIoResult(0x6F, 0, (byte[])null);
387 if (ar.result == null) {
388 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800389 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700390 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800391 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700392 } else {
393 loge("iccTransmitApduLogicalChannel: Unknown exception");
394 }
395 }
396 synchronized (request) {
397 request.notifyAll();
398 }
399 break;
400
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700401 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
402 request = (MainThreadRequest) msg.obj;
403 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800404 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700405 if (uiccCard == null) {
406 loge("iccTransmitApduBasicChannel: No UICC");
407 request.result = new IccIoResult(0x6F, 0, (byte[])null);
408 synchronized (request) {
409 request.notifyAll();
410 }
411 } else {
412 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
413 request);
414 uiccCard.iccTransmitApduBasicChannel(
415 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
416 iccArgument.p3, iccArgument.data, onCompleted);
417 }
418 break;
419
420 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
421 ar = (AsyncResult) msg.obj;
422 request = (MainThreadRequest) ar.userObj;
423 if (ar.exception == null && ar.result != null) {
424 request.result = ar.result;
425 } else {
426 request.result = new IccIoResult(0x6F, 0, (byte[])null);
427 if (ar.result == null) {
428 loge("iccTransmitApduBasicChannel: Empty response");
429 } else if (ar.exception instanceof CommandException) {
430 loge("iccTransmitApduBasicChannel: CommandException: " +
431 ar.exception);
432 } else {
433 loge("iccTransmitApduBasicChannel: Unknown exception");
434 }
435 }
436 synchronized (request) {
437 request.notifyAll();
438 }
439 break;
440
441 case CMD_EXCHANGE_SIM_IO:
442 request = (MainThreadRequest) msg.obj;
443 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800444 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700445 if (uiccCard == null) {
446 loge("iccExchangeSimIO: No UICC");
447 request.result = new IccIoResult(0x6F, 0, (byte[])null);
448 synchronized (request) {
449 request.notifyAll();
450 }
451 } else {
452 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
453 request);
454 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
455 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
456 iccArgument.data, onCompleted);
457 }
458 break;
459
460 case EVENT_EXCHANGE_SIM_IO_DONE:
461 ar = (AsyncResult) msg.obj;
462 request = (MainThreadRequest) ar.userObj;
463 if (ar.exception == null && ar.result != null) {
464 request.result = ar.result;
465 } else {
466 request.result = new IccIoResult(0x6f, 0, (byte[])null);
467 }
468 synchronized (request) {
469 request.notifyAll();
470 }
471 break;
472
Derek Tan4d5e5c12014-02-04 11:54:58 -0800473 case CMD_SEND_ENVELOPE:
474 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800475 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700476 if (uiccCard == null) {
477 loge("sendEnvelopeWithStatus: No UICC");
478 request.result = new IccIoResult(0x6F, 0, (byte[])null);
479 synchronized (request) {
480 request.notifyAll();
481 }
482 } else {
483 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
484 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
485 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800486 break;
487
488 case EVENT_SEND_ENVELOPE_DONE:
489 ar = (AsyncResult) msg.obj;
490 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700491 if (ar.exception == null && ar.result != null) {
492 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800493 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700494 request.result = new IccIoResult(0x6F, 0, (byte[])null);
495 if (ar.result == null) {
496 loge("sendEnvelopeWithStatus: Empty response");
497 } else if (ar.exception instanceof CommandException) {
498 loge("sendEnvelopeWithStatus: CommandException: " +
499 ar.exception);
500 } else {
501 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
502 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800503 }
504 synchronized (request) {
505 request.notifyAll();
506 }
507 break;
508
Shishir Agrawal566b7612013-10-28 14:41:00 -0700509 case CMD_OPEN_CHANNEL:
510 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800511 uiccCard = getUiccCardFromRequest(request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800512 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700513 if (uiccCard == null) {
514 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800515 request.result = new IccOpenLogicalChannelResponse(-1,
516 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700517 synchronized (request) {
518 request.notifyAll();
519 }
520 } else {
521 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800522 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
523 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700524 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700525 break;
526
527 case EVENT_OPEN_CHANNEL_DONE:
528 ar = (AsyncResult) msg.obj;
529 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700530 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700531 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700532 int[] result = (int[]) ar.result;
533 int channelId = result[0];
534 byte[] selectResponse = null;
535 if (result.length > 1) {
536 selectResponse = new byte[result.length - 1];
537 for (int i = 1; i < result.length; ++i) {
538 selectResponse[i - 1] = (byte) result[i];
539 }
540 }
541 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700542 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700543 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700544 if (ar.result == null) {
545 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700546 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700547 if (ar.exception != null) {
548 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
549 }
550
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700551 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700552 if (ar.exception instanceof CommandException) {
553 CommandException.Error error =
554 ((CommandException) (ar.exception)).getCommandError();
555 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700556 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700557 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700558 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700559 }
560 }
561 openChannelResp = new IccOpenLogicalChannelResponse(
562 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700563 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700564 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700565 synchronized (request) {
566 request.notifyAll();
567 }
568 break;
569
570 case CMD_CLOSE_CHANNEL:
571 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800572 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700573 if (uiccCard == null) {
574 loge("iccCloseLogicalChannel: No UICC");
575 request.result = new IccIoResult(0x6F, 0, (byte[])null);
576 synchronized (request) {
577 request.notifyAll();
578 }
579 } else {
580 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
581 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
582 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700583 break;
584
585 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800586 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
587 break;
588
589 case CMD_NV_READ_ITEM:
590 request = (MainThreadRequest) msg.obj;
591 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
592 mPhone.nvReadItem((Integer) request.argument, onCompleted);
593 break;
594
595 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700596 ar = (AsyncResult) msg.obj;
597 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800598 if (ar.exception == null && ar.result != null) {
599 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700600 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800601 request.result = "";
602 if (ar.result == null) {
603 loge("nvReadItem: Empty response");
604 } else if (ar.exception instanceof CommandException) {
605 loge("nvReadItem: CommandException: " +
606 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700607 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800608 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700609 }
610 }
611 synchronized (request) {
612 request.notifyAll();
613 }
614 break;
615
Jake Hambye994d462014-02-03 13:10:13 -0800616 case CMD_NV_WRITE_ITEM:
617 request = (MainThreadRequest) msg.obj;
618 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
619 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
620 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
621 break;
622
623 case EVENT_NV_WRITE_ITEM_DONE:
624 handleNullReturnEvent(msg, "nvWriteItem");
625 break;
626
627 case CMD_NV_WRITE_CDMA_PRL:
628 request = (MainThreadRequest) msg.obj;
629 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
630 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
631 break;
632
633 case EVENT_NV_WRITE_CDMA_PRL_DONE:
634 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
635 break;
636
637 case CMD_NV_RESET_CONFIG:
638 request = (MainThreadRequest) msg.obj;
639 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
640 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
641 break;
642
643 case EVENT_NV_RESET_CONFIG_DONE:
644 handleNullReturnEvent(msg, "nvResetConfig");
645 break;
646
Jake Hamby7c27be32014-03-03 13:25:59 -0800647 case CMD_GET_PREFERRED_NETWORK_TYPE:
648 request = (MainThreadRequest) msg.obj;
649 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700650 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800651 break;
652
653 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
654 ar = (AsyncResult) msg.obj;
655 request = (MainThreadRequest) ar.userObj;
656 if (ar.exception == null && ar.result != null) {
657 request.result = ar.result; // Integer
658 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800659 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800660 if (ar.result == null) {
661 loge("getPreferredNetworkType: Empty response");
662 } else if (ar.exception instanceof CommandException) {
663 loge("getPreferredNetworkType: CommandException: " +
664 ar.exception);
665 } else {
666 loge("getPreferredNetworkType: Unknown exception");
667 }
668 }
669 synchronized (request) {
670 request.notifyAll();
671 }
672 break;
673
674 case CMD_SET_PREFERRED_NETWORK_TYPE:
675 request = (MainThreadRequest) msg.obj;
676 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
677 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700678 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800679 break;
680
681 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
682 handleNullReturnEvent(msg, "setPreferredNetworkType");
683 break;
684
Steven Liu4bf01bc2014-07-17 11:05:29 -0500685 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
686 request = (MainThreadRequest)msg.obj;
687 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
688 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
689 break;
690
691 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
692 ar = (AsyncResult)msg.obj;
693 request = (MainThreadRequest)ar.userObj;
694 request.result = ar;
695 synchronized (request) {
696 request.notifyAll();
697 }
698 break;
699
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800700 case CMD_SET_VOICEMAIL_NUMBER:
701 request = (MainThreadRequest) msg.obj;
702 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
703 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800704 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
705 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800706 break;
707
708 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
709 handleNullReturnEvent(msg, "setVoicemailNumber");
710 break;
711
Stuart Scott54788802015-03-30 13:18:01 -0700712 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
713 request = (MainThreadRequest) msg.obj;
714 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
715 request);
716 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
717 break;
718
719 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
720 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
721 break;
722
Shishir Agrawal302c8692015-06-19 13:49:39 -0700723 case CMD_PERFORM_NETWORK_SCAN:
724 request = (MainThreadRequest) msg.obj;
725 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
726 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
727 break;
728
729 case EVENT_PERFORM_NETWORK_SCAN_DONE:
730 ar = (AsyncResult) msg.obj;
731 request = (MainThreadRequest) ar.userObj;
732 CellNetworkScanResult cellScanResult;
733 if (ar.exception == null && ar.result != null) {
734 cellScanResult = new CellNetworkScanResult(
735 CellNetworkScanResult.STATUS_SUCCESS,
736 (List<OperatorInfo>) ar.result);
737 } else {
738 if (ar.result == null) {
739 loge("getCellNetworkScanResults: Empty response");
740 }
741 if (ar.exception != null) {
742 loge("getCellNetworkScanResults: Exception: " + ar.exception);
743 }
744 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
745 if (ar.exception instanceof CommandException) {
746 CommandException.Error error =
747 ((CommandException) (ar.exception)).getCommandError();
748 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
749 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
750 } else if (error == CommandException.Error.GENERIC_FAILURE) {
751 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
752 }
753 }
754 cellScanResult = new CellNetworkScanResult(errorCode, null);
755 }
756 request.result = cellScanResult;
757 synchronized (request) {
758 request.notifyAll();
759 }
760 break;
761
762 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
763 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700764 ManualNetworkSelectionArgument selArg =
765 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700766 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
767 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700768 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
769 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700770 break;
771
772 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
773 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
774 break;
775
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700776 case CMD_GET_MODEM_ACTIVITY_INFO:
777 request = (MainThreadRequest) msg.obj;
778 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700779 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700780 break;
781
782 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
783 ar = (AsyncResult) msg.obj;
784 request = (MainThreadRequest) ar.userObj;
785 if (ar.exception == null && ar.result != null) {
786 request.result = ar.result;
787 } else {
788 if (ar.result == null) {
789 loge("queryModemActivityInfo: Empty response");
790 } else if (ar.exception instanceof CommandException) {
791 loge("queryModemActivityInfo: CommandException: " +
792 ar.exception);
793 } else {
794 loge("queryModemActivityInfo: Unknown exception");
795 }
796 }
Amit Mahajand4766222016-01-28 15:28:28 -0800797 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
798 if (request.result == null) {
799 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
800 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700801 synchronized (request) {
802 request.notifyAll();
803 }
804 break;
805
Meng Wang1a7c35a2016-05-05 20:56:15 -0700806 case CMD_SET_ALLOWED_CARRIERS:
807 request = (MainThreadRequest) msg.obj;
808 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
809 mPhone.setAllowedCarriers(
810 (List<CarrierIdentifier>) request.argument,
811 onCompleted);
812 break;
813
814 case EVENT_SET_ALLOWED_CARRIERS_DONE:
815 ar = (AsyncResult) msg.obj;
816 request = (MainThreadRequest) ar.userObj;
817 if (ar.exception == null && ar.result != null) {
818 request.result = ar.result;
819 } else {
820 if (ar.result == null) {
821 loge("setAllowedCarriers: Empty response");
822 } else if (ar.exception instanceof CommandException) {
823 loge("setAllowedCarriers: CommandException: " +
824 ar.exception);
825 } else {
826 loge("setAllowedCarriers: Unknown exception");
827 }
828 }
829 // Result cannot be null. Return -1 on error.
830 if (request.result == null) {
831 request.result = new int[]{-1};
832 }
833 synchronized (request) {
834 request.notifyAll();
835 }
836 break;
837
838 case CMD_GET_ALLOWED_CARRIERS:
839 request = (MainThreadRequest) msg.obj;
840 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
841 mPhone.getAllowedCarriers(onCompleted);
842 break;
843
844 case EVENT_GET_ALLOWED_CARRIERS_DONE:
845 ar = (AsyncResult) msg.obj;
846 request = (MainThreadRequest) ar.userObj;
847 if (ar.exception == null && ar.result != null) {
848 request.result = ar.result;
849 } else {
850 if (ar.result == null) {
851 loge("getAllowedCarriers: Empty response");
852 } else if (ar.exception instanceof CommandException) {
853 loge("getAllowedCarriers: CommandException: " +
854 ar.exception);
855 } else {
856 loge("getAllowedCarriers: Unknown exception");
857 }
858 }
859 // Result cannot be null. Return empty list of CarrierIdentifier.
860 if (request.result == null) {
861 request.result = new ArrayList<CarrierIdentifier>(0);
862 }
863 synchronized (request) {
864 request.notifyAll();
865 }
866 break;
867
Nathan Haroldb3014052017-01-25 15:57:32 -0800868 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
869 ar = (AsyncResult) msg.obj;
870 request = (MainThreadRequest) ar.userObj;
871 if (ar.exception == null && ar.result != null) {
872 request.result = ar.result;
873 } else {
874 request.result = new IllegalArgumentException(
875 "Failed to retrieve Forbidden Plmns");
876 if (ar.result == null) {
877 loge("getForbiddenPlmns: Empty response");
878 } else {
879 loge("getForbiddenPlmns: Unknown exception");
880 }
881 }
882 synchronized (request) {
883 request.notifyAll();
884 }
885 break;
886
887 case CMD_GET_FORBIDDEN_PLMNS:
888 request = (MainThreadRequest) msg.obj;
889 uiccCard = getUiccCardFromRequest(request);
890 if (uiccCard == null) {
891 loge("getForbiddenPlmns() UiccCard is null");
892 request.result = new IllegalArgumentException(
893 "getForbiddenPlmns() UiccCard is null");
894 synchronized (request) {
895 request.notifyAll();
896 }
897 break;
898 }
899 Integer appType = (Integer) request.argument;
900 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
901 if (uiccApp == null) {
902 loge("getForbiddenPlmns() no app with specified type -- "
903 + appType);
904 request.result = new IllegalArgumentException("Failed to get UICC App");
905 synchronized (request) {
906 request.notifyAll();
907 }
908 break;
909 } else {
910 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
911 + " specified type -- " + appType);
912 }
913 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
914 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
915 onCompleted);
916 break;
917
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700918 default:
919 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
920 break;
921 }
922 }
Jake Hambye994d462014-02-03 13:10:13 -0800923
924 private void handleNullReturnEvent(Message msg, String command) {
925 AsyncResult ar = (AsyncResult) msg.obj;
926 MainThreadRequest request = (MainThreadRequest) ar.userObj;
927 if (ar.exception == null) {
928 request.result = true;
929 } else {
930 request.result = false;
931 if (ar.exception instanceof CommandException) {
932 loge(command + ": CommandException: " + ar.exception);
933 } else {
934 loge(command + ": Unknown exception");
935 }
936 }
937 synchronized (request) {
938 request.notifyAll();
939 }
940 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 }
942
943 /**
944 * Posts the specified command to be executed on the main thread,
945 * waits for the request to complete, and returns the result.
946 * @see #sendRequestAsync
947 */
948 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800949 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700950 }
951
952 /**
953 * Posts the specified command to be executed on the main thread,
954 * waits for the request to complete, and returns the result.
955 * @see #sendRequestAsync
956 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800957 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
959 throw new RuntimeException("This method will deadlock if called from the main thread.");
960 }
961
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800962 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 Message msg = mMainThreadHandler.obtainMessage(command, request);
964 msg.sendToTarget();
965
966 // Wait for the request to complete
967 synchronized (request) {
968 while (request.result == null) {
969 try {
970 request.wait();
971 } catch (InterruptedException e) {
972 // Do nothing, go back and wait until the request is complete
973 }
974 }
975 }
976 return request.result;
977 }
978
979 /**
980 * Asynchronous ("fire and forget") version of sendRequest():
981 * Posts the specified command to be executed on the main thread, and
982 * returns immediately.
983 * @see #sendRequest
984 */
985 private void sendRequestAsync(int command) {
986 mMainThreadHandler.sendEmptyMessage(command);
987 }
988
989 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700990 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
991 * @see {@link #sendRequest(int,Object)}
992 */
993 private void sendRequestAsync(int command, Object argument) {
994 MainThreadRequest request = new MainThreadRequest(argument);
995 Message msg = mMainThreadHandler.obtainMessage(command, request);
996 msg.sendToTarget();
997 }
998
999 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 * Initialize the singleton PhoneInterfaceManager instance.
1001 * This is only done once, at startup, from PhoneApp.onCreate().
1002 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001003 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001004 synchronized (PhoneInterfaceManager.class) {
1005 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001006 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001007 } else {
1008 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1009 }
1010 return sInstance;
1011 }
1012 }
1013
1014 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001015 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 mApp = app;
1017 mPhone = phone;
1018 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001019 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1021 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001022 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001023 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001024 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001025
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026 publish();
1027 }
1028
1029 private void publish() {
1030 if (DBG) log("publish: " + this);
1031
1032 ServiceManager.addService("phone", this);
1033 }
1034
Stuart Scott584921c2015-01-15 17:10:34 -08001035 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001036 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1037 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001038 }
1039
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001040 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1041 Phone phone = getPhoneFromRequest(request);
1042 return phone == null ? null :
1043 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1044 }
1045
Wink Saville36469e72014-06-11 15:17:00 -07001046 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001047 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001048 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001049 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001050 //
1051 // Implementation of the ITelephony interface.
1052 //
1053
1054 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001055 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001056 }
1057
Wink Savilleb564aae2014-10-23 10:18:09 -07001058 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 if (DBG) log("dial: " + number);
1060 // No permission check needed here: This is just a wrapper around the
1061 // ACTION_DIAL intent, which is available to any app since it puts up
1062 // the UI before it does anything.
1063
1064 String url = createTelUrl(number);
1065 if (url == null) {
1066 return;
1067 }
1068
1069 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001070 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001071 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1072 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1073 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1074 mApp.startActivity(intent);
1075 }
1076 }
1077
1078 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001079 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001080 }
1081
Wink Savilleb564aae2014-10-23 10:18:09 -07001082 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 if (DBG) log("call: " + number);
1084
1085 // This is just a wrapper around the ACTION_CALL intent, but we still
1086 // need to do a permission check since we're calling startActivity()
1087 // from the context of the phone app.
1088 enforceCallPermission();
1089
1090 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1091 != AppOpsManager.MODE_ALLOWED) {
1092 return;
1093 }
1094
1095 String url = createTelUrl(number);
1096 if (url == null) {
1097 return;
1098 }
1099
Wink Saville08874612014-08-31 19:19:58 -07001100 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001101 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001102 if (slist != null) {
1103 for (SubscriptionInfo subInfoRecord : slist) {
1104 if (subInfoRecord.getSubscriptionId() == subId) {
1105 isValid = true;
1106 break;
1107 }
Wink Saville08874612014-08-31 19:19:58 -07001108 }
1109 }
1110 if (isValid == false) {
1111 return;
1112 }
1113
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001114 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001115 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001116 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1117 mApp.startActivity(intent);
1118 }
1119
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001120 /**
1121 * End a call based on call state
1122 * @return true is a call was ended
1123 */
1124 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001125 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001126 }
1127
1128 /**
1129 * End a call based on the call state of the subId
1130 * @return true is a call was ended
1131 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001132 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001133 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001134 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001135 }
1136
1137 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001138 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001139 }
1140
Wink Savilleb564aae2014-10-23 10:18:09 -07001141 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001142 if (DBG) log("answerRingingCall...");
1143 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1144 // but that can probably wait till the big TelephonyManager API overhaul.
1145 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1146 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001147 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 }
1149
1150 /**
1151 * Make the actual telephony calls to implement answerRingingCall().
1152 * This should only be called from the main thread of the Phone app.
1153 * @see #answerRingingCall
1154 *
1155 * TODO: it would be nice to return true if we answered the call, or
1156 * false if there wasn't actually a ringing incoming call, or some
1157 * other error occurred. (In other words, pass back the return value
1158 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1159 * But that would require calling this method via sendRequest() rather
1160 * than sendRequestAsync(), and right now we don't actually *need* that
1161 * return value, so let's just return void for now.
1162 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001163 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001164 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001165 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001166 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1167 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001168 if (hasActiveCall && hasHoldingCall) {
1169 // Both lines are in use!
1170 // TODO: provide a flag to let the caller specify what
1171 // policy to use if both lines are in use. (The current
1172 // behavior is hardwired to "answer incoming, end ongoing",
1173 // which is how the CALL button is specced to behave.)
1174 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1175 return;
1176 } else {
1177 // answerCall() will automatically hold the current active
1178 // call, if there is one.
1179 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1180 return;
1181 }
1182 } else {
1183 // No call was ringing.
1184 return;
1185 }
1186 }
1187
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001188 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001189 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001190 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001191 public void silenceRinger() {
1192 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 }
1194
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001195 @Override
1196 public boolean isOffhook(String callingPackage) {
1197 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001198 }
1199
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001200 @Override
1201 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1202 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1203 return false;
1204 }
1205
Sanket Padawe356d7632015-06-22 14:03:32 -07001206 final Phone phone = getPhone(subId);
1207 if (phone != null) {
1208 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1209 } else {
1210 return false;
1211 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001212 }
1213
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001214 @Override
1215 public boolean isRinging(String callingPackage) {
1216 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001217 }
1218
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001219 @Override
1220 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1221 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1222 return false;
1223 }
1224
Sanket Padawe356d7632015-06-22 14:03:32 -07001225 final Phone phone = getPhone(subId);
1226 if (phone != null) {
1227 return (phone.getState() == PhoneConstants.State.RINGING);
1228 } else {
1229 return false;
1230 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001231 }
1232
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001233 @Override
1234 public boolean isIdle(String callingPackage) {
1235 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001236 }
1237
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001238 @Override
1239 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1240 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1241 return false;
1242 }
1243
Sanket Padawe356d7632015-06-22 14:03:32 -07001244 final Phone phone = getPhone(subId);
1245 if (phone != null) {
1246 return (phone.getState() == PhoneConstants.State.IDLE);
1247 } else {
1248 return false;
1249 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001250 }
1251
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001252 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001253 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001254 }
1255
Wink Savilleb564aae2014-10-23 10:18:09 -07001256 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001257 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001258 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1259 }
1260
1261 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001262 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001263 }
1264
Wink Savilleb564aae2014-10-23 10:18:09 -07001265 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001266 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001267 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1268 }
1269
1270 /** {@hide} */
1271 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001272 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001273 }
1274
Wink Savilleb564aae2014-10-23 10:18:09 -07001275 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001277 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001278 checkSimPin.start();
1279 return checkSimPin.unlockSim(null, pin);
1280 }
1281
Wink Saville9de0f752013-10-22 19:04:03 -07001282 /** {@hide} */
1283 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001284 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001285 }
1286
Wink Savilleb564aae2014-10-23 10:18:09 -07001287 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001289 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001290 checkSimPuk.start();
1291 return checkSimPuk.unlockSim(puk, pin);
1292 }
1293
1294 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001295 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 * a synchronous one.
1297 */
1298 private static class UnlockSim extends Thread {
1299
1300 private final IccCard mSimCard;
1301
1302 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001303 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1304 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305
1306 // For replies from SimCard interface
1307 private Handler mHandler;
1308
1309 // For async handler to identify request type
1310 private static final int SUPPLY_PIN_COMPLETE = 100;
1311
1312 public UnlockSim(IccCard simCard) {
1313 mSimCard = simCard;
1314 }
1315
1316 @Override
1317 public void run() {
1318 Looper.prepare();
1319 synchronized (UnlockSim.this) {
1320 mHandler = new Handler() {
1321 @Override
1322 public void handleMessage(Message msg) {
1323 AsyncResult ar = (AsyncResult) msg.obj;
1324 switch (msg.what) {
1325 case SUPPLY_PIN_COMPLETE:
1326 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1327 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001328 mRetryCount = msg.arg1;
1329 if (ar.exception != null) {
1330 if (ar.exception instanceof CommandException &&
1331 ((CommandException)(ar.exception)).getCommandError()
1332 == CommandException.Error.PASSWORD_INCORRECT) {
1333 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1334 } else {
1335 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1336 }
1337 } else {
1338 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1339 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001340 mDone = true;
1341 UnlockSim.this.notifyAll();
1342 }
1343 break;
1344 }
1345 }
1346 };
1347 UnlockSim.this.notifyAll();
1348 }
1349 Looper.loop();
1350 }
1351
1352 /*
1353 * Use PIN or PUK to unlock SIM card
1354 *
1355 * If PUK is null, unlock SIM card with PIN
1356 *
1357 * If PUK is not null, unlock SIM card with PUK and set PIN code
1358 */
Wink Saville9de0f752013-10-22 19:04:03 -07001359 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001360
1361 while (mHandler == null) {
1362 try {
1363 wait();
1364 } catch (InterruptedException e) {
1365 Thread.currentThread().interrupt();
1366 }
1367 }
1368 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1369
1370 if (puk == null) {
1371 mSimCard.supplyPin(pin, callback);
1372 } else {
1373 mSimCard.supplyPuk(puk, pin, callback);
1374 }
1375
1376 while (!mDone) {
1377 try {
1378 Log.d(LOG_TAG, "wait for done");
1379 wait();
1380 } catch (InterruptedException e) {
1381 // Restore the interrupted status
1382 Thread.currentThread().interrupt();
1383 }
1384 }
1385 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001386 int[] resultArray = new int[2];
1387 resultArray[0] = mResult;
1388 resultArray[1] = mRetryCount;
1389 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001390 }
1391 }
1392
1393 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001394 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001395
1396 }
1397
Wink Savilleb564aae2014-10-23 10:18:09 -07001398 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 // No permission check needed here: this call is harmless, and it's
1400 // needed for the ServiceState.requestStateUpdate() call (which is
1401 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001402 final Phone phone = getPhone(subId);
1403 if (phone != null) {
1404 phone.updateServiceLocation();
1405 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 }
1407
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001408 @Override
1409 public boolean isRadioOn(String callingPackage) {
1410 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001411 }
1412
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001413 @Override
1414 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1415 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1416 return false;
1417 }
1418 return isRadioOnForSubscriber(subId);
1419 }
1420
1421 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001422 final Phone phone = getPhone(subId);
1423 if (phone != null) {
1424 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1425 } else {
1426 return false;
1427 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001428 }
1429
1430 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001431 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001432
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 }
Wink Saville36469e72014-06-11 15:17:00 -07001434
Wink Savilleb564aae2014-10-23 10:18:09 -07001435 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001436 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001437 final Phone phone = getPhone(subId);
1438 if (phone != null) {
1439 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1440 }
Wink Saville36469e72014-06-11 15:17:00 -07001441 }
1442
1443 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001444 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001445 }
1446
Wink Savilleb564aae2014-10-23 10:18:09 -07001447 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001448 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001449 final Phone phone = getPhone(subId);
1450 if (phone == null) {
1451 return false;
1452 }
1453 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001454 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001455 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 }
1457 return true;
1458 }
Wink Saville36469e72014-06-11 15:17:00 -07001459
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001460 public boolean needMobileRadioShutdown() {
1461 /*
1462 * If any of the Radios are available, it will need to be
1463 * shutdown. So return true if any Radio is available.
1464 */
1465 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1466 Phone phone = PhoneFactory.getPhone(i);
1467 if (phone != null && phone.isRadioAvailable()) return true;
1468 }
1469 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1470 return false;
1471 }
1472
1473 public void shutdownMobileRadios() {
1474 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1475 logv("Shutting down Phone " + i);
1476 shutdownRadioUsingPhoneId(i);
1477 }
1478 }
1479
1480 private void shutdownRadioUsingPhoneId(int phoneId) {
1481 enforceModifyPermission();
1482 Phone phone = PhoneFactory.getPhone(phoneId);
1483 if (phone != null && phone.isRadioAvailable()) {
1484 phone.shutdownRadio();
1485 }
1486 }
1487
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001488 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001489 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1490 if (defaultPhone != null) {
1491 defaultPhone.setRadioPower(turnOn);
1492 return true;
1493 } else {
1494 loge("There's no default phone.");
1495 return false;
1496 }
Wink Saville36469e72014-06-11 15:17:00 -07001497 }
1498
Wink Savilleb564aae2014-10-23 10:18:09 -07001499 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001501 final Phone phone = getPhone(subId);
1502 if (phone != null) {
1503 phone.setRadioPower(turnOn);
1504 return true;
1505 } else {
1506 return false;
1507 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001508 }
1509
Wink Saville36469e72014-06-11 15:17:00 -07001510 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001511 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001512 public boolean enableDataConnectivity() {
1513 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001514 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001515 final Phone phone = getPhone(subId);
1516 if (phone != null) {
1517 phone.setDataEnabled(true);
1518 return true;
1519 } else {
1520 return false;
1521 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 }
1523
Wink Saville36469e72014-06-11 15:17:00 -07001524 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001525 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001526 public boolean disableDataConnectivity() {
1527 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001528 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001529 final Phone phone = getPhone(subId);
1530 if (phone != null) {
1531 phone.setDataEnabled(false);
1532 return true;
1533 } else {
1534 return false;
1535 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 }
1537
Wink Saville36469e72014-06-11 15:17:00 -07001538 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001539 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001540 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001541 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001542 final Phone phone = getPhone(subId);
1543 if (phone != null) {
1544 return phone.isDataConnectivityPossible();
1545 } else {
1546 return false;
1547 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001548 }
1549
1550 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001551 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001552 }
1553
pkanwarf8520782016-11-06 20:37:09 -08001554 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar7e856482016-10-14 19:37:38 -07001555 enforceCallPermission();
pkanwar7e856482016-10-14 19:37:38 -07001556 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1557 return;
1558 }
1559 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1560 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1561 };
1562
Wink Savilleb564aae2014-10-23 10:18:09 -07001563 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001564 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001565 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1566 return false;
1567 }
Wink Saville36469e72014-06-11 15:17:00 -07001568 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001569 }
1570
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001571 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001572 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001573 }
1574
Sanket Padawe5780e442017-03-20 15:04:47 -07001575 public int getCallStateForSlot(int slotIndex) {
1576 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001577 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1578 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001579 }
1580
Sanket Padawe356d7632015-06-22 14:03:32 -07001581 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001582 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001583 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001584 if (phone != null) {
1585 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1586 } else {
1587 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1588 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001589 }
1590
Sanket Padawe356d7632015-06-22 14:03:32 -07001591 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001592 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001593 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001594 if (phone != null) {
1595 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1596 } else {
1597 return TelephonyManager.DATA_ACTIVITY_NONE;
1598 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001599 }
1600
1601 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001602 public Bundle getCellLocation(String callingPackage) {
1603 enforceFineOrCoarseLocationPermission("getCellLocation");
1604
1605 // OP_COARSE_LOCATION controls both fine and coarse location.
1606 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1607 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001608 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001609 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001610 }
1611
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001612 if (checkIfCallerIsSelfOrForegroundUser() ||
1613 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001614 if (DBG_LOC) log("getCellLocation: is active user");
1615 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001616 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001617 if (phone == null) {
1618 return null;
1619 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07001620
1621 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1622 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001623 return data;
1624 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001625 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001626 return null;
1627 }
1628 }
1629
Svetoslav64fad262015-04-14 14:35:21 -07001630 private void enforceFineOrCoarseLocationPermission(String message) {
1631 try {
1632 mApp.enforceCallingOrSelfPermission(
1633 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1634 } catch (SecurityException e) {
1635 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1636 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1637 // is the weaker precondition
1638 mApp.enforceCallingOrSelfPermission(
1639 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1640 }
1641 }
1642
1643
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001644 @Override
1645 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001646 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001647 }
1648
Sanket Padawe356d7632015-06-22 14:03:32 -07001649 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001650 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001651 mApp.enforceCallingOrSelfPermission(
1652 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001653 final Phone phone = getPhone(subId);
1654 if (phone != null) {
1655 phone.enableLocationUpdates();
1656 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 }
1658
1659 @Override
1660 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001661 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001662 }
1663
Sanket Padawe356d7632015-06-22 14:03:32 -07001664 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001665 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001666 mApp.enforceCallingOrSelfPermission(
1667 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001668 final Phone phone = getPhone(subId);
1669 if (phone != null) {
1670 phone.disableLocationUpdates();
1671 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001672 }
1673
1674 @Override
1675 @SuppressWarnings("unchecked")
1676 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001677 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1678
1679 // OP_COARSE_LOCATION controls both fine and coarse location.
1680 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1681 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1682 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001683 }
1684
1685 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1686 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1687 return null;
1688 }
Svetoslav64fad262015-04-14 14:35:21 -07001689
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001690 if (checkIfCallerIsSelfOrForegroundUser() ||
1691 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001692 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1693
1694 ArrayList<NeighboringCellInfo> cells = null;
1695
Sooraj Sasindran22882212016-07-18 11:57:25 -07001696 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001697 try {
1698 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran22882212016-07-18 11:57:25 -07001699 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001700 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001701 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001702 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001703 }
1704 return cells;
1705 } else {
1706 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1707 return null;
1708 }
1709 }
1710
1711
1712 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001713 public List<CellInfo> getAllCellInfo(String callingPackage) {
1714 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1715
1716 // OP_COARSE_LOCATION controls both fine and coarse location.
1717 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1718 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1719 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001720 }
1721
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001722 if (checkIfCallerIsSelfOrForegroundUser() ||
1723 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001724 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran22882212016-07-18 11:57:25 -07001725 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001726 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1727 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran22882212016-07-18 11:57:25 -07001728 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1729 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001730 }
1731 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001732 } else {
1733 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1734 return null;
1735 }
1736 }
1737
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001738 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001739 public void setCellInfoListRate(int rateInMillis) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001740 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001741 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1742 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001743 }
1744
Shishir Agrawala9f32182016-04-12 12:00:16 -07001745 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001746 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001747 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1748 return null;
1749 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001750 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001751 return phone == null ? null : phone.getImei();
1752 }
1753
1754 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001755 public String getMeidForSlot(int slotIndex, String callingPackage) {
1756 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1757 return null;
1758 }
1759 Phone phone = PhoneFactory.getPhone(slotIndex);
1760 return phone == null ? null : phone.getMeid();
1761 }
1762
1763 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001764 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001765 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1766 return null;
1767 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001768 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001769 return phone == null ? null : phone.getDeviceSvn();
1770 }
1771
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001772 //
1773 // Internal helper methods.
1774 //
1775
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001776 /**
1777 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1778 */
1779 private boolean checkCallerInteractAcrossUsersFull() {
1780 return mPhone.getContext().checkCallingOrSelfPermission(
1781 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1782 == PackageManager.PERMISSION_GRANTED;
1783 }
1784
Jake Hambye994d462014-02-03 13:10:13 -08001785 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001786 boolean ok;
1787
1788 boolean self = Binder.getCallingUid() == Process.myUid();
1789 if (!self) {
1790 // Get the caller's user id then clear the calling identity
1791 // which will be restored in the finally clause.
1792 int callingUser = UserHandle.getCallingUserId();
1793 long ident = Binder.clearCallingIdentity();
1794
1795 try {
1796 // With calling identity cleared the current user is the foreground user.
1797 int foregroundUser = ActivityManager.getCurrentUser();
1798 ok = (foregroundUser == callingUser);
1799 if (DBG_LOC) {
1800 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1801 + " callingUser=" + callingUser + " ok=" + ok);
1802 }
1803 } catch (Exception ex) {
1804 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1805 ok = false;
1806 } finally {
1807 Binder.restoreCallingIdentity(ident);
1808 }
1809 } else {
1810 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1811 ok = true;
1812 }
1813 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1814 return ok;
1815 }
1816
1817 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001818 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1819 *
1820 * @throws SecurityException if the caller does not have the required permission
1821 */
1822 private void enforceModifyPermission() {
1823 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1824 }
1825
1826 /**
Junda Liua2e36012014-07-09 18:30:01 -07001827 * Make sure either system app or the caller has carrier privilege.
1828 *
1829 * @throws SecurityException if the caller does not have the required permission/privilege
1830 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001831 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001832 int permission = mApp.checkCallingOrSelfPermission(
1833 android.Manifest.permission.MODIFY_PHONE_STATE);
1834 if (permission == PackageManager.PERMISSION_GRANTED) {
1835 return;
1836 }
1837
1838 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001839 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001840 }
1841
1842 /**
1843 * Make sure the caller has carrier privilege.
1844 *
1845 * @throws SecurityException if the caller does not have the required permission
1846 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001847 private void enforceCarrierPrivilege(int subId) {
1848 if (getCarrierPrivilegeStatus(subId) !=
1849 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001850 loge("No Carrier Privilege.");
1851 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001852 }
1853 }
1854
1855 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001856 * Make sure the caller has the CALL_PHONE permission.
1857 *
1858 * @throws SecurityException if the caller does not have the required permission
1859 */
1860 private void enforceCallPermission() {
1861 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1862 }
1863
Stuart Scott8eef64f2015-04-08 15:13:54 -07001864 private void enforceConnectivityInternalPermission() {
1865 mApp.enforceCallingOrSelfPermission(
1866 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1867 "ConnectivityService");
1868 }
1869
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001870 private String createTelUrl(String number) {
1871 if (TextUtils.isEmpty(number)) {
1872 return null;
1873 }
1874
Jake Hambye994d462014-02-03 13:10:13 -08001875 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001876 }
1877
Ihab Awadf9e92732013-12-05 18:02:52 -08001878 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001879 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1880 }
1881
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001882 private static void logv(String msg) {
1883 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1884 }
1885
Ihab Awadf9e92732013-12-05 18:02:52 -08001886 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001887 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1888 }
1889
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001890 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001891 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001892 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001893 }
1894
Sanket Padawe356d7632015-06-22 14:03:32 -07001895 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001896 public int getActivePhoneTypeForSlot(int slotIndex) {
1897 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001898 if (phone == null) {
1899 return PhoneConstants.PHONE_TYPE_NONE;
1900 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001901 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001902 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001903 }
1904
1905 /**
1906 * Returns the CDMA ERI icon index to display
1907 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001908 @Override
1909 public int getCdmaEriIconIndex(String callingPackage) {
1910 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001911 }
1912
Sanket Padawe356d7632015-06-22 14:03:32 -07001913 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001914 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1915 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1916 return -1;
1917 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001918 final Phone phone = getPhone(subId);
1919 if (phone != null) {
1920 return phone.getCdmaEriIconIndex();
1921 } else {
1922 return -1;
1923 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001924 }
1925
1926 /**
1927 * Returns the CDMA ERI icon mode,
1928 * 0 - ON
1929 * 1 - FLASHING
1930 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001931 @Override
1932 public int getCdmaEriIconMode(String callingPackage) {
1933 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001934 }
1935
Sanket Padawe356d7632015-06-22 14:03:32 -07001936 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001937 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1938 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1939 return -1;
1940 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001941 final Phone phone = getPhone(subId);
1942 if (phone != null) {
1943 return phone.getCdmaEriIconMode();
1944 } else {
1945 return -1;
1946 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001947 }
1948
1949 /**
1950 * Returns the CDMA ERI text,
1951 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001952 @Override
1953 public String getCdmaEriText(String callingPackage) {
1954 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001955 }
1956
Sanket Padawe356d7632015-06-22 14:03:32 -07001957 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001958 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1959 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1960 return null;
1961 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001962 final Phone phone = getPhone(subId);
1963 if (phone != null) {
1964 return phone.getCdmaEriText();
1965 } else {
1966 return null;
1967 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001968 }
1969
1970 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001971 * Returns the CDMA MDN.
1972 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001973 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001974 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001975 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001976 final Phone phone = getPhone(subId);
1977 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1978 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001979 } else {
1980 return null;
1981 }
1982 }
1983
1984 /**
1985 * Returns the CDMA MIN.
1986 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001987 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001988 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001989 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001990 final Phone phone = getPhone(subId);
1991 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1992 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001993 } else {
1994 return null;
1995 }
1996 }
1997
1998 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001999 * Returns true if CDMA provisioning needs to run.
2000 */
2001 public boolean needsOtaServiceProvisioning() {
2002 return mPhone.needsOtaServiceProvisioning();
2003 }
2004
2005 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002006 * Sets the voice mail number of a given subId.
2007 */
2008 @Override
2009 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002010 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002011 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2012 new Pair<String, String>(alphaTag, number), new Integer(subId));
2013 return success;
2014 }
2015
Ta-wei Yen87c49842016-05-13 21:19:52 -07002016 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07002017 public void setVisualVoicemailEnabled(String callingPackage,
2018 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
2019 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2020 if (!TextUtils.equals(callingPackage,
2021 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2022 enforceModifyPermissionOrCarrierPrivilege(
2023 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
2024 }
2025 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2026 }
2027
2028 @Override
2029 public boolean isVisualVoicemailEnabled(String callingPackage,
2030 PhoneAccountHandle phoneAccountHandle) {
2031 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2032 return false;
2033 }
2034 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2035 }
2036
2037 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002038 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2039 VisualVoicemailSmsFilterSettings settings) {
2040 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002041 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002042 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2043 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002044 }
2045
2046 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002047 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2048 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002049 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002050 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002051 }
2052
2053 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002054 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2055 String callingPackage, int subId) {
2056 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002057 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002058 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002059 }
2060
2061 @Override
Ta-wei Yen1b455992016-12-27 14:52:32 -08002062 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002063 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002064 return VisualVoicemailSmsFilterConfig
Ta-wei Yen1b455992016-12-27 14:52:32 -08002065 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2066 }
2067
2068 @Override
2069 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2070 String number, int port, String text, PendingIntent sentIntent) {
2071 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2072 enforceDefaultDialer(callingPackage);
2073 enforceSendSmsPermission();
2074 // Make the calls as the phone process.
2075 final long identity = Binder.clearCallingIdentity();
2076 try {
2077 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2078 if (port == 0) {
2079 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2080 sentIntent, null, false);
2081 } else {
2082 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2083 smsManager.sendDataMessageWithSelfPermissions(number, null,
2084 (short) port, data, sentIntent, null);
2085 }
2086 } finally {
2087 Binder.restoreCallingIdentity(identity);
2088 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002089 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002090 /**
fionaxuef039d42016-11-14 13:36:14 -08002091 * Sets the voice activation state of a given subId.
2092 */
2093 @Override
2094 public void setVoiceActivationState(int subId, int activationState) {
2095 enforceModifyPermissionOrCarrierPrivilege(subId);
2096 final Phone phone = getPhone(subId);
2097 if (phone != null) {
2098 phone.setVoiceActivationState(activationState);
2099 } else {
2100 loge("setVoiceActivationState fails with invalid subId: " + subId);
2101 }
2102 }
2103
2104 /**
2105 * Sets the data activation state of a given subId.
2106 */
2107 @Override
2108 public void setDataActivationState(int subId, int activationState) {
2109 enforceModifyPermissionOrCarrierPrivilege(subId);
2110 final Phone phone = getPhone(subId);
2111 if (phone != null) {
2112 phone.setDataActivationState(activationState);
2113 } else {
2114 loge("setVoiceActivationState fails with invalid subId: " + subId);
2115 }
2116 }
2117
2118 /**
2119 * Returns the voice activation state of a given subId.
2120 */
2121 @Override
2122 public int getVoiceActivationState(int subId, String callingPackage) {
2123 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2124 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2125 }
2126 final Phone phone = getPhone(subId);
2127 if (phone != null) {
2128 return phone.getVoiceActivationState();
2129 } else {
2130 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2131 }
2132 }
2133
2134 /**
2135 * Returns the data activation state of a given subId.
2136 */
2137 @Override
2138 public int getDataActivationState(int subId, String callingPackage) {
2139 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2140 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2141 }
2142 final Phone phone = getPhone(subId);
2143 if (phone != null) {
2144 return phone.getDataActivationState();
2145 } else {
2146 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2147 }
2148 }
2149
2150 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002151 * Returns the unread count of voicemails
2152 */
2153 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002154 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002155 }
2156
2157 /**
2158 * Returns the unread count of voicemails for a subId
2159 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002160 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002161 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002162 final Phone phone = getPhone(subId);
2163 if (phone != null) {
2164 return phone.getVoiceMessageCount();
2165 } else {
2166 return 0;
2167 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002168 }
2169
2170 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002171 * Returns the data network type.
2172 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002173 *
2174 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2175 */
2176 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002177 public int getNetworkType() {
2178 final Phone phone = getPhone(getDefaultSubscription());
2179 if (phone != null) {
2180 return phone.getServiceState().getDataNetworkType();
2181 } else {
2182 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2183 }
Wink Saville36469e72014-06-11 15:17:00 -07002184 }
2185
2186 /**
2187 * Returns the network type for a subId
2188 */
2189 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002190 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2191 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2192 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2193 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002194
Sanket Padawe356d7632015-06-22 14:03:32 -07002195 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002196 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002197 return phone.getServiceState().getDataNetworkType();
2198 } else {
2199 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2200 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002201 }
2202
2203 /**
2204 * Returns the data network type
2205 */
2206 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002207 public int getDataNetworkType(String callingPackage) {
2208 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002209 }
2210
2211 /**
2212 * Returns the data network type for a subId
2213 */
2214 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002215 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2216 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2217 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2218 }
2219
Sanket Padawe356d7632015-06-22 14:03:32 -07002220 final Phone phone = getPhone(subId);
2221 if (phone != null) {
2222 return phone.getServiceState().getDataNetworkType();
2223 } else {
2224 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2225 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002226 }
2227
2228 /**
Wink Saville36469e72014-06-11 15:17:00 -07002229 * Returns the Voice network type for a subId
2230 */
2231 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002232 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2233 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2234 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2235 }
2236
Sanket Padawe356d7632015-06-22 14:03:32 -07002237 final Phone phone = getPhone(subId);
2238 if (phone != null) {
2239 return phone.getServiceState().getVoiceNetworkType();
2240 } else {
2241 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2242 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002243 }
2244
2245 /**
2246 * @return true if a ICC card is present
2247 */
2248 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002249 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002250 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2251 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002252 }
2253
2254 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002255 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002256 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002257 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002258 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2259 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002260 final Phone phone = getPhone(subId[0]);
2261 if (subId != null && phone != null) {
2262 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002263 } else {
2264 return false;
2265 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002266 }
2267
2268 /**
2269 * Return if the current radio is LTE on CDMA. This
2270 * is a tri-state return value as for a period of time
2271 * the mode may be unknown.
2272 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002273 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002274 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002275 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002276 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002277 @Override
2278 public int getLteOnCdmaMode(String callingPackage) {
2279 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002280 }
2281
Sanket Padawe356d7632015-06-22 14:03:32 -07002282 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002283 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2284 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2285 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2286 }
2287
Sanket Padawe356d7632015-06-22 14:03:32 -07002288 final Phone phone = getPhone(subId);
2289 if (phone == null) {
2290 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2291 } else {
2292 return phone.getLteOnCdmaMode();
2293 }
Wink Saville36469e72014-06-11 15:17:00 -07002294 }
2295
2296 public void setPhone(Phone phone) {
2297 mPhone = phone;
2298 }
2299
2300 /**
2301 * {@hide}
2302 * Returns Default subId, 0 in the case of single standby.
2303 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002304 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002305 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002306 }
2307
Shishir Agrawala9f32182016-04-12 12:00:16 -07002308 private int getSlotForDefaultSubscription() {
2309 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2310 }
2311
Wink Savilleb564aae2014-10-23 10:18:09 -07002312 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002313 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002314 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002315
2316 /**
2317 * @see android.telephony.TelephonyManager.WifiCallingChoices
2318 */
2319 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002320 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2321 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002322 }
2323
2324 /**
2325 * @see android.telephony.TelephonyManager.WifiCallingChoices
2326 */
2327 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002328 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2329 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2330 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002331 }
2332
Sailesh Nepald1e68152013-12-12 19:08:02 -08002333 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002334 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002335 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002336 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002337
Shishir Agrawal566b7612013-10-28 14:41:00 -07002338 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002339 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002340 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002341
Ajay Nambiabd73502015-12-03 13:50:00 -08002342 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002343 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002344 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002345 if (DBG) log("iccOpenLogicalChannel: " + response);
2346 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002347 }
2348
2349 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002350 public boolean iccCloseLogicalChannel(int subId, int channel) {
2351 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002352
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002353 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002354 if (channel < 0) {
2355 return false;
2356 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002357 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002358 if (DBG) log("iccCloseLogicalChannel: " + success);
2359 return success;
2360 }
2361
2362 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002363 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002364 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002365 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002366
2367 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002368 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2369 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002370 " data=" + data);
2371 }
2372
2373 if (channel < 0) {
2374 return "";
2375 }
2376
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002377 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002378 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002379 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2380
Shishir Agrawal566b7612013-10-28 14:41:00 -07002381 // Append the returned status code to the end of the response payload.
2382 String s = Integer.toHexString(
2383 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002384 if (response.payload != null) {
2385 s = IccUtils.bytesToHexString(response.payload) + s;
2386 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002387 return s;
2388 }
Jake Hambye994d462014-02-03 13:10:13 -08002389
Evan Charltonc66da362014-05-16 14:06:40 -07002390 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002391 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002392 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002393 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002394
2395 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002396 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2397 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002398 }
2399
2400 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002401 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002402 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2403
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002404 // Append the returned status code to the end of the response payload.
2405 String s = Integer.toHexString(
2406 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002407 if (response.payload != null) {
2408 s = IccUtils.bytesToHexString(response.payload) + s;
2409 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002410 return s;
2411 }
2412
2413 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002414 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002415 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002416 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002417
2418 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002419 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002420 p1 + " " + p2 + " " + p3 + ":" + filePath);
2421 }
2422
2423 IccIoResult response =
2424 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002425 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2426 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002427
2428 if (DBG) {
2429 log("Exchange SIM_IO [R]" + response);
2430 }
2431
2432 byte[] result = null;
2433 int length = 2;
2434 if (response.payload != null) {
2435 length = 2 + response.payload.length;
2436 result = new byte[length];
2437 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2438 } else {
2439 result = new byte[length];
2440 }
2441
2442 result[length - 1] = (byte) response.sw2;
2443 result[length - 2] = (byte) response.sw1;
2444 return result;
2445 }
2446
Nathan Haroldb3014052017-01-25 15:57:32 -08002447 /**
2448 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2449 * on a particular subscription
2450 */
2451 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002452 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2453 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002454 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2455 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2456 return null;
2457 }
2458 Object response = sendRequest(
2459 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2460 if (response instanceof String[]) {
2461 return (String[]) response;
2462 }
2463 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2464 return null;
2465 }
2466
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002467 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002468 public String sendEnvelopeWithStatus(int subId, String content) {
2469 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002470
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002471 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002472 if (response.payload == null) {
2473 return "";
2474 }
2475
2476 // Append the returned status code to the end of the response payload.
2477 String s = Integer.toHexString(
2478 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2479 s = IccUtils.bytesToHexString(response.payload) + s;
2480 return s;
2481 }
2482
Jake Hambye994d462014-02-03 13:10:13 -08002483 /**
2484 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2485 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2486 *
2487 * @param itemID the ID of the item to read
2488 * @return the NV item as a String, or null on error.
2489 */
2490 @Override
2491 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002492 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002493 if (DBG) log("nvReadItem: item " + itemID);
2494 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2495 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2496 return value;
2497 }
2498
2499 /**
2500 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2501 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2502 *
2503 * @param itemID the ID of the item to read
2504 * @param itemValue the value to write, as a String
2505 * @return true on success; false on any failure
2506 */
2507 @Override
2508 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002509 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002510 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2511 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2512 new Pair<Integer, String>(itemID, itemValue));
2513 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2514 return success;
2515 }
2516
2517 /**
2518 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2519 * Used for device configuration by some CDMA operators.
2520 *
2521 * @param preferredRoamingList byte array containing the new PRL
2522 * @return true on success; false on any failure
2523 */
2524 @Override
2525 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002526 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002527 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2528 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2529 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2530 return success;
2531 }
2532
2533 /**
2534 * Perform the specified type of NV config reset.
2535 * Used for device configuration by some CDMA operators.
2536 *
2537 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2538 * @return true on success; false on any failure
2539 */
2540 @Override
2541 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002542 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002543 if (DBG) log("nvResetConfig: type " + resetType);
2544 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2545 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2546 return success;
2547 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002548
2549 /**
Wink Saville36469e72014-06-11 15:17:00 -07002550 * {@hide}
2551 * Returns Default sim, 0 in the case of single standby.
2552 */
2553 public int getDefaultSim() {
2554 //TODO Need to get it from Telephony Devcontroller
2555 return 0;
2556 }
2557
Svet Ganovb320e182015-04-16 12:30:10 -07002558 public String[] getPcscfAddress(String apnType, String callingPackage) {
2559 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2560 return new String[0];
2561 }
2562
2563
ram87fca6f2014-07-18 18:58:44 +05302564 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002565 }
2566
Brad Ebinger76681002017-01-23 13:50:20 -08002567 /**
2568 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2569 * feature or {@link null} if the service is not available. If an ImsServiceController is
2570 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2571 * feature updates.
2572 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002573 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002574 IImsServiceFeatureListener callback) {
2575 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002576 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002577 callback);
2578 }
2579
Wink Saville36469e72014-06-11 15:17:00 -07002580 public void setImsRegistrationState(boolean registered) {
2581 enforceModifyPermission();
2582 mPhone.setImsRegistrationState(registered);
2583 }
2584
2585 /**
Stuart Scott54788802015-03-30 13:18:01 -07002586 * Set the network selection mode to automatic.
2587 *
2588 */
2589 @Override
2590 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002591 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002592 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2593 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2594 }
2595
2596 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002597 * Set the network selection mode to manual with the selected carrier.
2598 */
2599 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002600 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2601 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002602 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002603 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002604 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2605 persistSelection);
2606 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002607 }
2608
2609 /**
2610 * Scans for available networks.
2611 */
2612 @Override
2613 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002614 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002615 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2616 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2617 CMD_PERFORM_NETWORK_SCAN, null, subId);
2618 return result;
2619 }
2620
2621 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002622 * Get the calculated preferred network type.
2623 * Used for debugging incorrect network type.
2624 *
2625 * @return the preferred network type, defined in RILConstants.java.
2626 */
2627 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002628 public int getCalculatedPreferredNetworkType(String callingPackage) {
2629 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2630 return RILConstants.PREFERRED_NETWORK_MODE;
2631 }
2632
Amit Mahajan43330e02014-11-18 11:54:45 -08002633 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002634 }
2635
2636 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002637 * Get the preferred network type.
2638 * Used for device configuration by some CDMA operators.
2639 *
2640 * @return the preferred network type, defined in RILConstants.java.
2641 */
2642 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002643 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002644 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002645 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002646 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002647 int networkType = (result != null ? result[0] : -1);
2648 if (DBG) log("getPreferredNetworkType: " + networkType);
2649 return networkType;
2650 }
2651
2652 /**
2653 * Set the preferred network type.
2654 * Used for device configuration by some CDMA operators.
2655 *
2656 * @param networkType the preferred network type, defined in RILConstants.java.
2657 * @return true on success; false on any failure.
2658 */
2659 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002660 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002661 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002662 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2663 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002664 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002665 if (success) {
2666 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002667 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002668 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002669 return success;
2670 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002671
2672 /**
Junda Liu475951f2014-11-07 16:45:03 -08002673 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2674 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2675 * tethering.
2676 *
2677 * @return 0: Not required. 1: required. 2: Not set.
2678 * @hide
2679 */
2680 @Override
2681 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002682 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002683 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2684 Settings.Global.TETHER_DUN_REQUIRED, 2);
2685 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2686 // config_tether_apndata.
2687 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2688 dunRequired = 1;
2689 }
2690 return dunRequired;
2691 }
2692
2693 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002694 * Set mobile data enabled
2695 * Used by the user through settings etc to turn on/off mobile data
2696 *
2697 * @param enable {@code true} turn turn data on, else {@code false}
2698 */
2699 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002700 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002701 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002702 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002703 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002704 Phone phone = PhoneFactory.getPhone(phoneId);
2705 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002706 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002707 phone.setDataEnabled(enable);
2708 } else {
2709 loge("setDataEnabled: no phone for subId=" + subId);
2710 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002711 }
2712
2713 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002714 * Get whether mobile data is enabled.
2715 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002716 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002717 *
2718 * @return {@code true} if data is enabled else {@code false}
2719 */
2720 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002721 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002722 try {
2723 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2724 null);
2725 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002726 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002727 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002728 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002729 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002730 Phone phone = PhoneFactory.getPhone(phoneId);
2731 if (phone != null) {
2732 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002733 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002734 return retVal;
2735 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002736 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002737 return false;
2738 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002739 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002740
2741 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002742 public int getCarrierPrivilegeStatus(int subId) {
2743 final Phone phone = getPhone(subId);
2744 if (phone == null) {
2745 loge("getCarrierPrivilegeStatus: Invalid subId");
2746 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2747 }
2748 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002749 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002750 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002751 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2752 }
2753 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002754 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002755 }
Junda Liu29340342014-07-10 15:23:27 -07002756
2757 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002758 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002759 if (TextUtils.isEmpty(pkgName))
2760 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002761 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002762 if (card == null) {
2763 loge("checkCarrierPrivilegesForPackage: No UICC");
2764 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2765 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002766 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2767 }
2768
2769 @Override
2770 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002771 if (TextUtils.isEmpty(pkgName))
2772 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002773 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2774 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2775 UiccCard card = UiccController.getInstance().getUiccCard(i);
2776 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002777 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002778 continue;
2779 }
2780
2781 result = card.getCarrierPrivilegeStatus(
2782 mPhone.getContext().getPackageManager(), pkgName);
2783 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2784 break;
2785 }
2786 }
2787
2788 return result;
Junda Liu29340342014-07-10 15:23:27 -07002789 }
Derek Tan89e89d42014-07-08 17:00:10 -07002790
2791 @Override
Junda Liue64de782015-04-16 17:19:16 -07002792 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2793 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2794 loge("phoneId " + phoneId + " is not valid.");
2795 return null;
2796 }
2797 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002798 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002799 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002800 return null ;
2801 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002802 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002803 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002804 }
2805
Amith Yamasani6e118872016-02-19 12:53:51 -08002806 @Override
2807 public List<String> getPackagesWithCarrierPrivileges() {
2808 PackageManager pm = mPhone.getContext().getPackageManager();
2809 List<String> privilegedPackages = new ArrayList<>();
2810 List<PackageInfo> packages = null;
2811 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2812 UiccCard card = UiccController.getInstance().getUiccCard(i);
2813 if (card == null) {
2814 // No UICC in that slot.
2815 continue;
2816 }
2817 if (card.hasCarrierPrivilegeRules()) {
2818 if (packages == null) {
2819 // Only check packages in user 0 for now
2820 packages = pm.getInstalledPackagesAsUser(
2821 PackageManager.MATCH_DISABLED_COMPONENTS
2822 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2823 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2824 }
2825 for (int p = packages.size() - 1; p >= 0; p--) {
2826 PackageInfo pkgInfo = packages.get(p);
2827 if (pkgInfo != null && pkgInfo.packageName != null
2828 && card.getCarrierPrivilegeStatus(pkgInfo)
2829 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2830 privilegedPackages.add(pkgInfo.packageName);
2831 }
2832 }
2833 }
2834 }
2835 return privilegedPackages;
2836 }
2837
Wink Savilleb564aae2014-10-23 10:18:09 -07002838 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002839 final Phone phone = getPhone(subId);
2840 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002841 if (card == null) {
2842 loge("getIccId: No UICC");
2843 return null;
2844 }
2845 String iccId = card.getIccId();
2846 if (TextUtils.isEmpty(iccId)) {
2847 loge("getIccId: ICC ID is null or empty.");
2848 return null;
2849 }
2850 return iccId;
2851 }
2852
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002853 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002854 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2855 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002856 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002857
Jeff Sharkey85190e62014-12-05 09:40:12 -08002858 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002859 final Phone phone = getPhone(subId);
2860 if (phone == null) {
2861 return false;
2862 }
2863 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002864
2865 if (DBG_MERGE) {
2866 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2867 + subscriberId + " to " + number);
2868 }
2869
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002870 if (TextUtils.isEmpty(iccId)) {
2871 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002872 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002873
Jeff Sharkey85190e62014-12-05 09:40:12 -08002874 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2875
2876 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002877 if (alphaTag == null) {
2878 editor.remove(alphaTagPrefKey);
2879 } else {
2880 editor.putString(alphaTagPrefKey, alphaTag);
2881 }
2882
Jeff Sharkey85190e62014-12-05 09:40:12 -08002883 // Record both the line number and IMSI for this ICCID, since we need to
2884 // track all merged IMSIs based on line number
2885 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2886 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002887 if (number == null) {
2888 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002889 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002890 } else {
2891 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002892 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002893 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002894
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002895 editor.commit();
2896 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002897 }
2898
2899 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002900 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002901 // This is open to apps with WRITE_SMS.
2902 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002903 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002904 return null;
2905 }
Derek Tan97ebb422014-09-05 16:55:38 -07002906
2907 String iccId = getIccId(subId);
2908 if (iccId != null) {
2909 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002910 if (DBG_MERGE) {
2911 log("getLine1NumberForDisplay returning " +
2912 mTelephonySharedPreferences.getString(numberPrefKey, null));
2913 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002914 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002915 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002916 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002917 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002918 }
2919
2920 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002921 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2922 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2923 return null;
2924 }
Derek Tan97ebb422014-09-05 16:55:38 -07002925
2926 String iccId = getIccId(subId);
2927 if (iccId != null) {
2928 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002929 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002930 }
Derek Tan97ebb422014-09-05 16:55:38 -07002931 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002932 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002933
2934 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002935 public String[] getMergedSubscriberIds(String callingPackage) {
2936 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2937 return null;
2938 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002939 final Context context = mPhone.getContext();
2940 final TelephonyManager tele = TelephonyManager.from(context);
2941 final SubscriptionManager sub = SubscriptionManager.from(context);
2942
2943 // Figure out what subscribers are currently active
2944 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002945 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2946 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2947 final long identity = Binder.clearCallingIdentity();
2948 try {
2949 final int[] subIds = sub.getActiveSubscriptionIdList();
2950 for (int subId : subIds) {
2951 activeSubscriberIds.add(tele.getSubscriberId(subId));
2952 }
2953 } finally {
2954 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002955 }
2956
2957 // First pass, find a number override for an active subscriber
2958 String mergeNumber = null;
2959 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2960 for (String key : prefs.keySet()) {
2961 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2962 final String subscriberId = (String) prefs.get(key);
2963 if (activeSubscriberIds.contains(subscriberId)) {
2964 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2965 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2966 mergeNumber = (String) prefs.get(numberKey);
2967 if (DBG_MERGE) {
2968 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2969 + " for active subscriber " + subscriberId);
2970 }
2971 if (!TextUtils.isEmpty(mergeNumber)) {
2972 break;
2973 }
2974 }
2975 }
2976 }
2977
2978 // Shortcut when no active merged subscribers
2979 if (TextUtils.isEmpty(mergeNumber)) {
2980 return null;
2981 }
2982
2983 // Second pass, find all subscribers under that line override
2984 final ArraySet<String> result = new ArraySet<>();
2985 for (String key : prefs.keySet()) {
2986 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2987 final String number = (String) prefs.get(key);
2988 if (mergeNumber.equals(number)) {
2989 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2990 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2991 final String subscriberId = (String) prefs.get(subscriberKey);
2992 if (!TextUtils.isEmpty(subscriberId)) {
2993 result.add(subscriberId);
2994 }
2995 }
2996 }
2997 }
2998
2999 final String[] resultArray = result.toArray(new String[result.size()]);
3000 Arrays.sort(resultArray);
3001 if (DBG_MERGE) {
3002 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3003 }
3004 return resultArray;
3005 }
3006
3007 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003008 public boolean setOperatorBrandOverride(int subId, String brand) {
3009 enforceCarrierPrivilege(subId);
3010 final Phone phone = getPhone(subId);
3011 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003012 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003013
3014 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003015 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003016 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3017 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003018 enforceCarrierPrivilege(subId);
3019 final Phone phone = getPhone(subId);
3020 if (phone == null) {
3021 return false;
3022 }
3023 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003024 cdmaNonRoamingList);
3025 }
3026
3027 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003028 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003029 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3030 enforceModifyPermission();
3031
3032 int returnValue = 0;
3033 try {
3034 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3035 if(result.exception == null) {
3036 if (result.result != null) {
3037 byte[] responseData = (byte[])(result.result);
3038 if(responseData.length > oemResp.length) {
3039 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3040 responseData.length + "bytes. Buffer Size is " +
3041 oemResp.length + "bytes.");
3042 }
3043 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3044 returnValue = responseData.length;
3045 }
3046 } else {
3047 CommandException ex = (CommandException) result.exception;
3048 returnValue = ex.getCommandError().ordinal();
3049 if(returnValue > 0) returnValue *= -1;
3050 }
3051 } catch (RuntimeException e) {
3052 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3053 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3054 if(returnValue > 0) returnValue *= -1;
3055 }
3056
3057 return returnValue;
3058 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003059
3060 @Override
3061 public void setRadioCapability(RadioAccessFamily[] rafs) {
3062 try {
3063 ProxyController.getInstance().setRadioCapability(rafs);
3064 } catch (RuntimeException e) {
3065 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3066 }
3067 }
3068
3069 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003070 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3071 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3072 return RadioAccessFamily.RAF_UNKNOWN;
3073 }
3074
Wink Saville5d475dd2014-10-17 15:00:58 -07003075 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3076 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003077
3078 @Override
3079 public void enableVideoCalling(boolean enable) {
3080 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003081 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003082 }
3083
3084 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003085 public boolean isVideoCallingEnabled(String callingPackage) {
3086 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3087 return false;
3088 }
3089
Andrew Lee77527ac2014-10-21 16:57:39 -07003090 // Check the user preference and the system-level IMS setting. Even if the user has
3091 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3092 // In the long run, we may instead need to check if there exists a connection service
3093 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003094 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3095 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003096 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003097 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003098
Andrew Leea1239f22015-03-02 17:44:07 -08003099 @Override
3100 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003101 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003102 }
3103
3104 @Override
3105 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003106 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003107 }
3108
Andrew Lee9431b832015-03-09 18:46:45 -07003109 @Override
3110 public boolean isTtyModeSupported() {
3111 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3112 TelephonyManager telephonyManager =
3113 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003114 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003115 }
3116
3117 @Override
3118 public boolean isHearingAidCompatibilitySupported() {
3119 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3120 }
3121
Sanket Padawe7310cc72015-01-14 09:53:20 -08003122 /**
3123 * Returns the unique device ID of phone, for example, the IMEI for
3124 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3125 *
3126 * <p>Requires Permission:
3127 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3128 */
3129 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003130 public String getDeviceId(String callingPackage) {
3131 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3132 return null;
3133 }
3134
Sanket Padawe7310cc72015-01-14 09:53:20 -08003135 final Phone phone = PhoneFactory.getPhone(0);
3136 if (phone != null) {
3137 return phone.getDeviceId();
3138 } else {
3139 return null;
3140 }
3141 }
3142
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003143 /*
3144 * {@hide}
3145 * Returns the IMS Registration Status
3146 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003147 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003148 public boolean isImsRegistered() {
3149 return mPhone.isImsRegistered();
3150 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003151
3152 @Override
3153 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3154 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3155 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003156
Nathan Haroldc55097a2015-03-11 18:14:50 -07003157 /*
3158 * {@hide}
3159 * Returns the IMS Registration Status
3160 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003161 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003162 return mPhone.isWifiCallingEnabled();
3163 }
3164
3165 /*
3166 * {@hide}
3167 * Returns the IMS Registration Status
3168 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003169 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003170 return mPhone.isVolteEnabled();
3171 }
Svet Ganovb320e182015-04-16 12:30:10 -07003172
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003173 /*
3174 * {@hide} Returns the IMS Registration Status
3175 */
3176 public boolean isVideoTelephonyAvailable() {
3177 return mPhone.isVideoEnabled();
3178 }
3179
Svet Ganovb320e182015-04-16 12:30:10 -07003180 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003181 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003182 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003183 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3184
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003185 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003186 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003187 } catch (SecurityException e) {
3188 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3189 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003190 }
Svet Ganovb320e182015-04-16 12:30:10 -07003191
3192 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3193 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3194 return false;
3195 }
3196
3197 return true;
3198 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003199
Makoto Onukifee69342015-06-29 14:44:50 -07003200 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003201 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003202 */
3203 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003204 // Default SMS app can always read it.
3205 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3206 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3207 return true;
3208 }
3209 try {
3210 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003211 } catch (SecurityException readPhoneStateSecurityException) {
3212 try {
3213 // Can be read with READ_SMS too.
3214 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3215 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3216 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3217 } catch (SecurityException readSmsSecurityException) {
3218 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3219 // permissions
3220 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3221 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3222 " or " + android.Manifest.permission.READ_SMS + ".");
3223 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003224 }
Makoto Onukifee69342015-06-29 14:44:50 -07003225 }
3226
Stuart Scott8eef64f2015-04-08 15:13:54 -07003227 @Override
3228 public void factoryReset(int subId) {
3229 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003230 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3231 return;
3232 }
3233
Svet Ganovcc087f82015-05-12 20:35:54 -07003234 final long identity = Binder.clearCallingIdentity();
3235 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003236 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3237 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003238 // Enable data
3239 setDataEnabled(subId, true);
3240 // Set network selection mode to automatic
3241 setNetworkSelectionModeAutomatic(subId);
3242 // Set preferred mobile network type to the best available
3243 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3244 // Turn off roaming
3245 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3246 }
3247 } finally {
3248 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003249 }
3250 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003251
3252 @Override
3253 public String getLocaleFromDefaultSim() {
3254 // We query all subscriptions instead of just the active ones, because
3255 // this might be called early on in the provisioning flow when the
3256 // subscriptions potentially aren't active yet.
3257 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3258 if (slist == null || slist.isEmpty()) {
3259 return null;
3260 }
3261
3262 // This function may be called very early, say, from the setup wizard, at
3263 // which point we won't have a default subscription set. If that's the case
3264 // we just choose the first, which will be valid in "most cases".
3265 final int defaultSubId = getDefaultSubscription();
3266 SubscriptionInfo info = null;
3267 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3268 info = slist.get(0);
3269 } else {
3270 for (SubscriptionInfo item : slist) {
3271 if (item.getSubscriptionId() == defaultSubId) {
3272 info = item;
3273 break;
3274 }
3275 }
3276
3277 if (info == null) {
3278 return null;
3279 }
3280 }
3281
3282 // Try and fetch the locale from the carrier properties or from the SIM language
3283 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003284 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003285 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003286 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003287 if (defaultPhone != null) {
3288 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3289 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003290 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003291 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3292 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003293 } else {
3294 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003295 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003296 }
3297 }
3298
Narayan Kamath011676f2015-07-29 12:04:08 +01003299 // The SIM language preferences only store a language (e.g. fr = French), not an
3300 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3301 // the SIM and carrier preferences does not include a country we add the country
3302 // determined from the SIM MCC to provide an exact locale.
3303 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003304 if (mccLocale != null) {
3305 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3306 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003307 }
3308
Tony Hill183b2de2015-06-24 14:53:58 +01003309 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003310 return null;
3311 }
3312
3313 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3314 final long identity = Binder.clearCallingIdentity();
3315 try {
3316 return mSubscriptionController.getAllSubInfoList(
3317 mPhone.getContext().getOpPackageName());
3318 } finally {
3319 Binder.restoreCallingIdentity(identity);
3320 }
3321 }
3322
3323 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3324 final long identity = Binder.clearCallingIdentity();
3325 try {
3326 return mSubscriptionController.getActiveSubscriptionInfoList(
3327 mPhone.getContext().getOpPackageName());
3328 } finally {
3329 Binder.restoreCallingIdentity(identity);
3330 }
3331 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003332
3333 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003334 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3335 * representing the state of the modem.
3336 *
3337 * NOTE: This clears the modem state, so there should only every be one caller.
3338 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003339 */
3340 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003341 public void requestModemActivityInfo(ResultReceiver result) {
3342 enforceModifyPermission();
3343
3344 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3345 Bundle bundle = new Bundle();
3346 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3347 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003348 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003349
3350 /**
3351 * {@hide}
3352 * Returns the service state information on specified subscription.
3353 */
3354 @Override
3355 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3356
3357 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3358 return null;
3359 }
3360
3361 final Phone phone = getPhone(subId);
3362 if (phone == null) {
3363 return null;
3364 }
3365
3366 return phone.getServiceState();
3367 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003368
3369 /**
3370 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3371 *
3372 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3373 * voicemail ringtone.
3374 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3375 * PhoneAccount.
3376 */
3377 @Override
3378 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3379 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3380 if (phone == null) {
3381 return null;
3382 }
3383
3384 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3385 }
3386
3387 /**
3388 * Returns whether vibration is set for voicemail notification in Phone settings.
3389 *
3390 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3391 * voicemail vibration setting.
3392 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3393 */
3394 @Override
3395 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3396 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3397 if (phone == null) {
3398 return false;
3399 }
3400
3401 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3402 }
3403
Youhan Wange64578a2016-05-02 15:32:42 -07003404 /**
3405 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3406 *
3407 * @throws SecurityException if the caller does not have the required permission
3408 */
3409 private void enforceReadPrivilegedPermission() {
3410 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3411 null);
3412 }
3413
3414 /**
Ta-wei Yen1b455992016-12-27 14:52:32 -08003415 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3416 * permission.
3417 *
3418 * @throws SecurityException if the caller does not have the required permission
3419 */
3420 private void enforceSendSmsPermission() {
3421 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3422 }
3423
3424 /**
3425 * Make sure called from the default dialer
3426 *
3427 * @throws SecurityException if the caller is not the default dialer
3428 */
3429 private void enforceDefaultDialer(String callingPackage) {
3430 TelecomManager telecomManager = mPhone.getContext().getSystemService(TelecomManager.class);
3431 if (!callingPackage.equals(telecomManager.getDefaultDialerPackage())) {
3432 throw new SecurityException("Caller not default dialer.");
3433 }
3434 }
3435
3436 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003437 * Return the application ID for the app type.
3438 *
3439 * @param subId the subscription ID that this request applies to.
3440 * @param appType the uicc app type.
3441 * @return Application ID for specificied app type, or null if no uicc.
3442 */
3443 @Override
3444 public String getAidForAppType(int subId, int appType) {
3445 enforceReadPrivilegedPermission();
3446 Phone phone = getPhone(subId);
3447 if (phone == null) {
3448 return null;
3449 }
3450 String aid = null;
3451 try {
3452 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3453 .getApplicationByType(appType).getAid();
3454 } catch (Exception e) {
3455 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3456 }
3457 return aid;
3458 }
3459
Youhan Wang4001d252016-05-11 10:29:41 -07003460 /**
3461 * Return the Electronic Serial Number.
3462 *
3463 * @param subId the subscription ID that this request applies to.
3464 * @return ESN or null if error.
3465 */
3466 @Override
3467 public String getEsn(int subId) {
3468 enforceReadPrivilegedPermission();
3469 Phone phone = getPhone(subId);
3470 if (phone == null) {
3471 return null;
3472 }
3473 String esn = null;
3474 try {
3475 esn = phone.getEsn();
3476 } catch (Exception e) {
3477 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3478 }
3479 return esn;
3480 }
3481
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003482 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003483 * Return the Preferred Roaming List Version.
3484 *
3485 * @param subId the subscription ID that this request applies to.
3486 * @return PRLVersion or null if error.
3487 */
3488 @Override
3489 public String getCdmaPrlVersion(int subId) {
3490 enforceReadPrivilegedPermission();
3491 Phone phone = getPhone(subId);
3492 if (phone == null) {
3493 return null;
3494 }
3495 String cdmaPrlVersion = null;
3496 try {
3497 cdmaPrlVersion = phone.getCdmaPrlVersion();
3498 } catch (Exception e) {
3499 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3500 }
3501 return cdmaPrlVersion;
3502 }
3503
3504 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003505 * Get snapshot of Telephony histograms
3506 * @return List of Telephony histograms
3507 * @hide
3508 */
3509 @Override
3510 public List<TelephonyHistogram> getTelephonyHistograms() {
3511 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3512 return RIL.getTelephonyRILTimingHistograms();
3513 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003514
3515 /**
3516 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003517 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003518 * Require system privileges. In the future we may add this to carrier APIs.
3519 *
3520 * @return The number of carriers set successfully, should match length of carriers
3521 */
3522 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003523 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003524 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003525 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003526 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3527 return retVal[0];
3528 }
3529
3530 /**
3531 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003532 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003533 * Require system privileges. In the future we may add this to carrier APIs.
3534 *
3535 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3536 * means all carriers are allowed.
3537 */
3538 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003539 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003540 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003541 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003542 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3543 }
3544
fionaxu59545b42016-05-25 15:53:37 -07003545 /**
3546 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3547 * @param subId the subscription ID that this action applies to.
3548 * @param enabled control enable or disable metered apns.
3549 * {@hide}
3550 */
3551 @Override
3552 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3553 enforceModifyPermission();
3554 final Phone phone = getPhone(subId);
3555 if (phone == null) {
3556 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3557 return;
3558 }
3559 try {
3560 phone.carrierActionSetMeteredApnsEnabled(enabled);
3561 } catch (Exception e) {
3562 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3563 }
3564 }
3565
3566 /**
3567 * Action set from carrier signalling broadcast receivers to enable/disable radio
3568 * @param subId the subscription ID that this action applies to.
3569 * @param enabled control enable or disable radio.
3570 * {@hide}
3571 */
3572 @Override
3573 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3574 enforceModifyPermission();
3575 final Phone phone = getPhone(subId);
3576 if (phone == null) {
3577 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3578 return;
3579 }
3580 try {
3581 phone.carrierActionSetRadioEnabled(enabled);
3582 } catch (Exception e) {
3583 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3584 }
3585 }
3586
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003587 /**
3588 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3589 * bug report is being generated.
3590 */
3591 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003592 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003593 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3594 != PackageManager.PERMISSION_GRANTED) {
3595 writer.println("Permission Denial: can't dump Phone from pid="
3596 + Binder.getCallingPid()
3597 + ", uid=" + Binder.getCallingUid()
3598 + "without permission "
3599 + android.Manifest.permission.DUMP);
3600 return;
3601 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003602 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003603 }
Jack Yueb89b242016-06-22 13:27:47 -07003604
3605 /**
3606 * Get aggregated video call data usage from all subscriptions since boot.
3607 * @return total data usage in bytes
3608 * {@hide}
3609 */
3610 @Override
3611 public long getVtDataUsage() {
3612 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3613 null);
3614
3615 // NetworkStatsService keeps tracking the active network interface and identity. It will
3616 // record the delta with the corresponding network identity. What we need to do here is
3617 // returning total video call data usage from all subscriptions since boot.
3618
3619 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3620 // simultaneous VT calls.
3621 final Phone[] phones = PhoneFactory.getPhones();
3622 long total = 0;
3623 for (Phone phone : phones) {
3624 total += phone.getVtDataUsage();
3625 }
3626 return total;
3627 }
Jack Yu75ab2952016-07-08 14:29:33 -07003628
3629 /**
3630 * Policy control of data connection. Usually used when data limit is passed.
3631 * @param enabled True if enabling the data, otherwise disabling.
3632 * @param subId Subscription index
3633 * {@hide}
3634 */
3635 @Override
3636 public void setPolicyDataEnabled(boolean enabled, int subId) {
3637 enforceModifyPermission();
3638 Phone phone = getPhone(subId);
3639 if (phone != null) {
3640 phone.setPolicyDataEnabled(enabled);
3641 }
3642 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003643
3644 /**
3645 * Get Client request stats
3646 * @return List of Client Request Stats
3647 * @hide
3648 */
3649 @Override
3650 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3651 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3652 return null;
3653 }
3654
3655 Phone phone = getPhone(subId);
3656 if (phone != null) {
3657 return phone.getClientRequestStats();
3658 }
3659
3660 return null;
3661 }
3662
3663 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3664 if (workSource != null) {
3665 return workSource;
3666 }
3667
3668 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3669 workSource = new WorkSource(uid, packageName);
3670 return workSource;
3671 }
Jack Yu346de222017-02-16 15:32:43 -08003672
3673 /**
3674 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3675 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003676 * @param slotIndex SIM slot id.
Jack Yu346de222017-02-16 15:32:43 -08003677 * @param powerUp True if powering up the SIM, otherwise powering down
3678 *
3679 **/
3680 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003681 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yu346de222017-02-16 15:32:43 -08003682 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003683 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003684 if (subId == null || subId.length == 0) {
3685 return;
3686 }
3687
3688 final Phone phone = getPhone(subId[0]);
3689 if (phone != null) {
3690 phone.setSimPowerState(powerUp);
3691 }
3692 }
shuoqfef36a32017-01-10 13:02:18 -08003693
3694 /**
3695 * Check if phone is in emergency callback mode
3696 * @return true if phone is in emergency callback mode
3697 * @param subId sub id
3698 */
3699 public boolean getEmergencyCallbackMode(int subId) {
3700 final Phone phone = getPhone(subId);
3701 if (phone != null) {
3702 return phone.isInEcm();
3703 } else {
3704 return false;
3705 }
3706 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003707}