blob: d3f1776ffc9d0bf2f2b88efc6827bfc93867b917 [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;
Nathan Haroldb3014052017-01-25 15:57:32 -0800163 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
164 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700165
166 /** The singleton instance. */
167 private static PhoneInterfaceManager sInstance;
168
Wink Saville3ab207e2014-11-20 13:07:20 -0800169 private PhoneGlobals mApp;
170 private Phone mPhone;
171 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700172 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800173 private AppOpsManager mAppOps;
174 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800175 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800176 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700177
Derek Tan97ebb422014-09-05 16:55:38 -0700178 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
179 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800180 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700181
182 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700183 * A request object to use for transmitting data to an ICC.
184 */
185 private static final class IccAPDUArgument {
186 public int channel, cla, command, p1, p2, p3;
187 public String data;
188
189 public IccAPDUArgument(int channel, int cla, int command,
190 int p1, int p2, int p3, String data) {
191 this.channel = channel;
192 this.cla = cla;
193 this.command = command;
194 this.p1 = p1;
195 this.p2 = p2;
196 this.p3 = p3;
197 this.data = data;
198 }
199 }
200
201 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700202 * A request object to use for transmitting data to an ICC.
203 */
204 private static final class ManualNetworkSelectionArgument {
205 public OperatorInfo operatorInfo;
206 public boolean persistSelection;
207
208 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
209 this.operatorInfo = operatorInfo;
210 this.persistSelection = persistSelection;
211 }
212 }
213
214 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700215 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
216 * request after sending. The main thread will notify the request when it is complete.
217 */
218 private static final class MainThreadRequest {
219 /** The argument to use for the request */
220 public Object argument;
221 /** The result of the request that is run on the main thread */
222 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800223 // The subscriber id that this request applies to. Defaults to
224 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
225 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226
227 public MainThreadRequest(Object argument) {
228 this.argument = argument;
229 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800230
231 public MainThreadRequest(Object argument, Integer subId) {
232 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800233 if (subId != null) {
234 this.subId = subId;
235 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800236 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700237 }
238
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800239 private static final class IncomingThirdPartyCallArgs {
240 public final ComponentName component;
241 public final String callId;
242 public final String callerDisplayName;
243
244 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
245 String callerDisplayName) {
246 this.component = component;
247 this.callId = callId;
248 this.callerDisplayName = callerDisplayName;
249 }
250 }
251
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252 /**
253 * A handler that processes messages on the main thread in the phone process. Since many
254 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
255 * inbound binder threads to the main thread in the phone process. The Binder thread
256 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
257 * on, which will be notified when the operation completes and will contain the result of the
258 * request.
259 *
260 * <p>If a MainThreadRequest object is provided in the msg.obj field,
261 * note that request.result must be set to something non-null for the calling thread to
262 * unblock.
263 */
264 private final class MainThreadHandler extends Handler {
265 @Override
266 public void handleMessage(Message msg) {
267 MainThreadRequest request;
268 Message onCompleted;
269 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800270 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700271 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272
273 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700274 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700275 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700276 final Phone phone = getPhoneFromRequest(request);
277 request.result = phone != null ?
278 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
279 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700280 // Wake up the requesting thread
281 synchronized (request) {
282 request.notifyAll();
283 }
284 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700285 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286
287 case CMD_HANDLE_NEIGHBORING_CELL:
288 request = (MainThreadRequest) msg.obj;
289 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
290 request);
Sooraj Sasindran22882212016-07-18 11:57:25 -0700291 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700292 break;
293
294 case EVENT_NEIGHBORING_CELL_DONE:
295 ar = (AsyncResult) msg.obj;
296 request = (MainThreadRequest) ar.userObj;
297 if (ar.exception == null && ar.result != null) {
298 request.result = ar.result;
299 } else {
300 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800301 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 }
303 // Wake up the requesting thread
304 synchronized (request) {
305 request.notifyAll();
306 }
307 break;
308
309 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700310 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800311 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700312 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700313 break;
314
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 case CMD_END_CALL:
316 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800317 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700318 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700319 Phone phone = getPhone(end_subId);
320 if (phone == null) {
321 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
322 break;
323 }
324 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
326 // CDMA: If the user presses the Power button we treat it as
327 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700328 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700329 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
330 // GSM: End the call as per the Phone state
331 hungUp = PhoneUtils.hangup(mCM);
332 } else {
333 throw new IllegalStateException("Unexpected phone type: " + phoneType);
334 }
335 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
336 request.result = hungUp;
337 // Wake up the requesting thread
338 synchronized (request) {
339 request.notifyAll();
340 }
341 break;
342
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700343 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700344 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700345 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800346 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700347 if (uiccCard == null) {
348 loge("iccTransmitApduLogicalChannel: No UICC");
349 request.result = new IccIoResult(0x6F, 0, (byte[])null);
350 synchronized (request) {
351 request.notifyAll();
352 }
353 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700354 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
355 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700356 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700357 iccArgument.channel, iccArgument.cla, iccArgument.command,
358 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700359 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700360 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700361 break;
362
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700363 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700364 ar = (AsyncResult) msg.obj;
365 request = (MainThreadRequest) ar.userObj;
366 if (ar.exception == null && ar.result != null) {
367 request.result = ar.result;
368 } else {
369 request.result = new IccIoResult(0x6F, 0, (byte[])null);
370 if (ar.result == null) {
371 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800372 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700373 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800374 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700375 } else {
376 loge("iccTransmitApduLogicalChannel: Unknown exception");
377 }
378 }
379 synchronized (request) {
380 request.notifyAll();
381 }
382 break;
383
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700384 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
385 request = (MainThreadRequest) msg.obj;
386 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800387 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700388 if (uiccCard == null) {
389 loge("iccTransmitApduBasicChannel: No UICC");
390 request.result = new IccIoResult(0x6F, 0, (byte[])null);
391 synchronized (request) {
392 request.notifyAll();
393 }
394 } else {
395 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
396 request);
397 uiccCard.iccTransmitApduBasicChannel(
398 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
399 iccArgument.p3, iccArgument.data, onCompleted);
400 }
401 break;
402
403 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
404 ar = (AsyncResult) msg.obj;
405 request = (MainThreadRequest) ar.userObj;
406 if (ar.exception == null && ar.result != null) {
407 request.result = ar.result;
408 } else {
409 request.result = new IccIoResult(0x6F, 0, (byte[])null);
410 if (ar.result == null) {
411 loge("iccTransmitApduBasicChannel: Empty response");
412 } else if (ar.exception instanceof CommandException) {
413 loge("iccTransmitApduBasicChannel: CommandException: " +
414 ar.exception);
415 } else {
416 loge("iccTransmitApduBasicChannel: Unknown exception");
417 }
418 }
419 synchronized (request) {
420 request.notifyAll();
421 }
422 break;
423
424 case CMD_EXCHANGE_SIM_IO:
425 request = (MainThreadRequest) msg.obj;
426 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800427 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700428 if (uiccCard == null) {
429 loge("iccExchangeSimIO: No UICC");
430 request.result = new IccIoResult(0x6F, 0, (byte[])null);
431 synchronized (request) {
432 request.notifyAll();
433 }
434 } else {
435 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
436 request);
437 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
438 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
439 iccArgument.data, onCompleted);
440 }
441 break;
442
443 case EVENT_EXCHANGE_SIM_IO_DONE:
444 ar = (AsyncResult) msg.obj;
445 request = (MainThreadRequest) ar.userObj;
446 if (ar.exception == null && ar.result != null) {
447 request.result = ar.result;
448 } else {
449 request.result = new IccIoResult(0x6f, 0, (byte[])null);
450 }
451 synchronized (request) {
452 request.notifyAll();
453 }
454 break;
455
Derek Tan4d5e5c12014-02-04 11:54:58 -0800456 case CMD_SEND_ENVELOPE:
457 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800458 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700459 if (uiccCard == null) {
460 loge("sendEnvelopeWithStatus: No UICC");
461 request.result = new IccIoResult(0x6F, 0, (byte[])null);
462 synchronized (request) {
463 request.notifyAll();
464 }
465 } else {
466 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
467 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
468 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800469 break;
470
471 case EVENT_SEND_ENVELOPE_DONE:
472 ar = (AsyncResult) msg.obj;
473 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700474 if (ar.exception == null && ar.result != null) {
475 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800476 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700477 request.result = new IccIoResult(0x6F, 0, (byte[])null);
478 if (ar.result == null) {
479 loge("sendEnvelopeWithStatus: Empty response");
480 } else if (ar.exception instanceof CommandException) {
481 loge("sendEnvelopeWithStatus: CommandException: " +
482 ar.exception);
483 } else {
484 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
485 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800486 }
487 synchronized (request) {
488 request.notifyAll();
489 }
490 break;
491
Shishir Agrawal566b7612013-10-28 14:41:00 -0700492 case CMD_OPEN_CHANNEL:
493 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800494 uiccCard = getUiccCardFromRequest(request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800495 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700496 if (uiccCard == null) {
497 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800498 request.result = new IccOpenLogicalChannelResponse(-1,
499 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700500 synchronized (request) {
501 request.notifyAll();
502 }
503 } else {
504 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800505 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
506 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700507 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700508 break;
509
510 case EVENT_OPEN_CHANNEL_DONE:
511 ar = (AsyncResult) msg.obj;
512 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700513 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700514 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700515 int[] result = (int[]) ar.result;
516 int channelId = result[0];
517 byte[] selectResponse = null;
518 if (result.length > 1) {
519 selectResponse = new byte[result.length - 1];
520 for (int i = 1; i < result.length; ++i) {
521 selectResponse[i - 1] = (byte) result[i];
522 }
523 }
524 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700525 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700526 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700527 if (ar.result == null) {
528 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700529 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700530 if (ar.exception != null) {
531 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
532 }
533
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700534 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700535 if (ar.exception instanceof CommandException) {
536 CommandException.Error error =
537 ((CommandException) (ar.exception)).getCommandError();
538 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700539 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700540 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700541 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700542 }
543 }
544 openChannelResp = new IccOpenLogicalChannelResponse(
545 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700546 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700547 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700548 synchronized (request) {
549 request.notifyAll();
550 }
551 break;
552
553 case CMD_CLOSE_CHANNEL:
554 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800555 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700556 if (uiccCard == null) {
557 loge("iccCloseLogicalChannel: No UICC");
558 request.result = new IccIoResult(0x6F, 0, (byte[])null);
559 synchronized (request) {
560 request.notifyAll();
561 }
562 } else {
563 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
564 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
565 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700566 break;
567
568 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800569 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
570 break;
571
572 case CMD_NV_READ_ITEM:
573 request = (MainThreadRequest) msg.obj;
574 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
575 mPhone.nvReadItem((Integer) request.argument, onCompleted);
576 break;
577
578 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700579 ar = (AsyncResult) msg.obj;
580 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800581 if (ar.exception == null && ar.result != null) {
582 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700583 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800584 request.result = "";
585 if (ar.result == null) {
586 loge("nvReadItem: Empty response");
587 } else if (ar.exception instanceof CommandException) {
588 loge("nvReadItem: CommandException: " +
589 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700590 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800591 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700592 }
593 }
594 synchronized (request) {
595 request.notifyAll();
596 }
597 break;
598
Jake Hambye994d462014-02-03 13:10:13 -0800599 case CMD_NV_WRITE_ITEM:
600 request = (MainThreadRequest) msg.obj;
601 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
602 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
603 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
604 break;
605
606 case EVENT_NV_WRITE_ITEM_DONE:
607 handleNullReturnEvent(msg, "nvWriteItem");
608 break;
609
610 case CMD_NV_WRITE_CDMA_PRL:
611 request = (MainThreadRequest) msg.obj;
612 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
613 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
614 break;
615
616 case EVENT_NV_WRITE_CDMA_PRL_DONE:
617 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
618 break;
619
620 case CMD_NV_RESET_CONFIG:
621 request = (MainThreadRequest) msg.obj;
622 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
623 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
624 break;
625
626 case EVENT_NV_RESET_CONFIG_DONE:
627 handleNullReturnEvent(msg, "nvResetConfig");
628 break;
629
Jake Hamby7c27be32014-03-03 13:25:59 -0800630 case CMD_GET_PREFERRED_NETWORK_TYPE:
631 request = (MainThreadRequest) msg.obj;
632 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700633 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800634 break;
635
636 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
637 ar = (AsyncResult) msg.obj;
638 request = (MainThreadRequest) ar.userObj;
639 if (ar.exception == null && ar.result != null) {
640 request.result = ar.result; // Integer
641 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800642 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800643 if (ar.result == null) {
644 loge("getPreferredNetworkType: Empty response");
645 } else if (ar.exception instanceof CommandException) {
646 loge("getPreferredNetworkType: CommandException: " +
647 ar.exception);
648 } else {
649 loge("getPreferredNetworkType: Unknown exception");
650 }
651 }
652 synchronized (request) {
653 request.notifyAll();
654 }
655 break;
656
657 case CMD_SET_PREFERRED_NETWORK_TYPE:
658 request = (MainThreadRequest) msg.obj;
659 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
660 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700661 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800662 break;
663
664 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
665 handleNullReturnEvent(msg, "setPreferredNetworkType");
666 break;
667
Steven Liu4bf01bc2014-07-17 11:05:29 -0500668 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
669 request = (MainThreadRequest)msg.obj;
670 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
671 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
672 break;
673
674 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
675 ar = (AsyncResult)msg.obj;
676 request = (MainThreadRequest)ar.userObj;
677 request.result = ar;
678 synchronized (request) {
679 request.notifyAll();
680 }
681 break;
682
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800683 case CMD_SET_VOICEMAIL_NUMBER:
684 request = (MainThreadRequest) msg.obj;
685 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
686 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800687 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
688 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800689 break;
690
691 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
692 handleNullReturnEvent(msg, "setVoicemailNumber");
693 break;
694
Stuart Scott54788802015-03-30 13:18:01 -0700695 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
696 request = (MainThreadRequest) msg.obj;
697 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
698 request);
699 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
700 break;
701
702 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
703 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
704 break;
705
Shishir Agrawal302c8692015-06-19 13:49:39 -0700706 case CMD_PERFORM_NETWORK_SCAN:
707 request = (MainThreadRequest) msg.obj;
708 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
709 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
710 break;
711
712 case EVENT_PERFORM_NETWORK_SCAN_DONE:
713 ar = (AsyncResult) msg.obj;
714 request = (MainThreadRequest) ar.userObj;
715 CellNetworkScanResult cellScanResult;
716 if (ar.exception == null && ar.result != null) {
717 cellScanResult = new CellNetworkScanResult(
718 CellNetworkScanResult.STATUS_SUCCESS,
719 (List<OperatorInfo>) ar.result);
720 } else {
721 if (ar.result == null) {
722 loge("getCellNetworkScanResults: Empty response");
723 }
724 if (ar.exception != null) {
725 loge("getCellNetworkScanResults: Exception: " + ar.exception);
726 }
727 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
728 if (ar.exception instanceof CommandException) {
729 CommandException.Error error =
730 ((CommandException) (ar.exception)).getCommandError();
731 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
732 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
733 } else if (error == CommandException.Error.GENERIC_FAILURE) {
734 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
735 }
736 }
737 cellScanResult = new CellNetworkScanResult(errorCode, null);
738 }
739 request.result = cellScanResult;
740 synchronized (request) {
741 request.notifyAll();
742 }
743 break;
744
745 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
746 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700747 ManualNetworkSelectionArgument selArg =
748 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700749 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
750 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700751 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
752 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700753 break;
754
755 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
756 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
757 break;
758
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700759 case CMD_GET_MODEM_ACTIVITY_INFO:
760 request = (MainThreadRequest) msg.obj;
761 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700762 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700763 break;
764
765 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
766 ar = (AsyncResult) msg.obj;
767 request = (MainThreadRequest) ar.userObj;
768 if (ar.exception == null && ar.result != null) {
769 request.result = ar.result;
770 } else {
771 if (ar.result == null) {
772 loge("queryModemActivityInfo: Empty response");
773 } else if (ar.exception instanceof CommandException) {
774 loge("queryModemActivityInfo: CommandException: " +
775 ar.exception);
776 } else {
777 loge("queryModemActivityInfo: Unknown exception");
778 }
779 }
Amit Mahajand4766222016-01-28 15:28:28 -0800780 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
781 if (request.result == null) {
782 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
783 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700784 synchronized (request) {
785 request.notifyAll();
786 }
787 break;
788
Meng Wang1a7c35a2016-05-05 20:56:15 -0700789 case CMD_SET_ALLOWED_CARRIERS:
790 request = (MainThreadRequest) msg.obj;
791 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
792 mPhone.setAllowedCarriers(
793 (List<CarrierIdentifier>) request.argument,
794 onCompleted);
795 break;
796
797 case EVENT_SET_ALLOWED_CARRIERS_DONE:
798 ar = (AsyncResult) msg.obj;
799 request = (MainThreadRequest) ar.userObj;
800 if (ar.exception == null && ar.result != null) {
801 request.result = ar.result;
802 } else {
803 if (ar.result == null) {
804 loge("setAllowedCarriers: Empty response");
805 } else if (ar.exception instanceof CommandException) {
806 loge("setAllowedCarriers: CommandException: " +
807 ar.exception);
808 } else {
809 loge("setAllowedCarriers: Unknown exception");
810 }
811 }
812 // Result cannot be null. Return -1 on error.
813 if (request.result == null) {
814 request.result = new int[]{-1};
815 }
816 synchronized (request) {
817 request.notifyAll();
818 }
819 break;
820
821 case CMD_GET_ALLOWED_CARRIERS:
822 request = (MainThreadRequest) msg.obj;
823 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
824 mPhone.getAllowedCarriers(onCompleted);
825 break;
826
827 case EVENT_GET_ALLOWED_CARRIERS_DONE:
828 ar = (AsyncResult) msg.obj;
829 request = (MainThreadRequest) ar.userObj;
830 if (ar.exception == null && ar.result != null) {
831 request.result = ar.result;
832 } else {
833 if (ar.result == null) {
834 loge("getAllowedCarriers: Empty response");
835 } else if (ar.exception instanceof CommandException) {
836 loge("getAllowedCarriers: CommandException: " +
837 ar.exception);
838 } else {
839 loge("getAllowedCarriers: Unknown exception");
840 }
841 }
842 // Result cannot be null. Return empty list of CarrierIdentifier.
843 if (request.result == null) {
844 request.result = new ArrayList<CarrierIdentifier>(0);
845 }
846 synchronized (request) {
847 request.notifyAll();
848 }
849 break;
850
Nathan Haroldb3014052017-01-25 15:57:32 -0800851 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
852 ar = (AsyncResult) msg.obj;
853 request = (MainThreadRequest) ar.userObj;
854 if (ar.exception == null && ar.result != null) {
855 request.result = ar.result;
856 } else {
857 request.result = new IllegalArgumentException(
858 "Failed to retrieve Forbidden Plmns");
859 if (ar.result == null) {
860 loge("getForbiddenPlmns: Empty response");
861 } else {
862 loge("getForbiddenPlmns: Unknown exception");
863 }
864 }
865 synchronized (request) {
866 request.notifyAll();
867 }
868 break;
869
870 case CMD_GET_FORBIDDEN_PLMNS:
871 request = (MainThreadRequest) msg.obj;
872 uiccCard = getUiccCardFromRequest(request);
873 if (uiccCard == null) {
874 loge("getForbiddenPlmns() UiccCard is null");
875 request.result = new IllegalArgumentException(
876 "getForbiddenPlmns() UiccCard is null");
877 synchronized (request) {
878 request.notifyAll();
879 }
880 break;
881 }
882 Integer appType = (Integer) request.argument;
883 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
884 if (uiccApp == null) {
885 loge("getForbiddenPlmns() no app with specified type -- "
886 + appType);
887 request.result = new IllegalArgumentException("Failed to get UICC App");
888 synchronized (request) {
889 request.notifyAll();
890 }
891 break;
892 } else {
893 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
894 + " specified type -- " + appType);
895 }
896 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
897 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
898 onCompleted);
899 break;
900
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700901 default:
902 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
903 break;
904 }
905 }
Jake Hambye994d462014-02-03 13:10:13 -0800906
907 private void handleNullReturnEvent(Message msg, String command) {
908 AsyncResult ar = (AsyncResult) msg.obj;
909 MainThreadRequest request = (MainThreadRequest) ar.userObj;
910 if (ar.exception == null) {
911 request.result = true;
912 } else {
913 request.result = false;
914 if (ar.exception instanceof CommandException) {
915 loge(command + ": CommandException: " + ar.exception);
916 } else {
917 loge(command + ": Unknown exception");
918 }
919 }
920 synchronized (request) {
921 request.notifyAll();
922 }
923 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 }
925
926 /**
927 * Posts the specified command to be executed on the main thread,
928 * waits for the request to complete, and returns the result.
929 * @see #sendRequestAsync
930 */
931 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800932 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700933 }
934
935 /**
936 * Posts the specified command to be executed on the main thread,
937 * waits for the request to complete, and returns the result.
938 * @see #sendRequestAsync
939 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800940 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
942 throw new RuntimeException("This method will deadlock if called from the main thread.");
943 }
944
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800945 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 Message msg = mMainThreadHandler.obtainMessage(command, request);
947 msg.sendToTarget();
948
949 // Wait for the request to complete
950 synchronized (request) {
951 while (request.result == null) {
952 try {
953 request.wait();
954 } catch (InterruptedException e) {
955 // Do nothing, go back and wait until the request is complete
956 }
957 }
958 }
959 return request.result;
960 }
961
962 /**
963 * Asynchronous ("fire and forget") version of sendRequest():
964 * Posts the specified command to be executed on the main thread, and
965 * returns immediately.
966 * @see #sendRequest
967 */
968 private void sendRequestAsync(int command) {
969 mMainThreadHandler.sendEmptyMessage(command);
970 }
971
972 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700973 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
974 * @see {@link #sendRequest(int,Object)}
975 */
976 private void sendRequestAsync(int command, Object argument) {
977 MainThreadRequest request = new MainThreadRequest(argument);
978 Message msg = mMainThreadHandler.obtainMessage(command, request);
979 msg.sendToTarget();
980 }
981
982 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700983 * Initialize the singleton PhoneInterfaceManager instance.
984 * This is only done once, at startup, from PhoneApp.onCreate().
985 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700986 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700987 synchronized (PhoneInterfaceManager.class) {
988 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700989 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 } else {
991 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
992 }
993 return sInstance;
994 }
995 }
996
997 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700998 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700999 mApp = app;
1000 mPhone = phone;
1001 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001002 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001003 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1004 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001005 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001006 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001007 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001008
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 publish();
1010 }
1011
1012 private void publish() {
1013 if (DBG) log("publish: " + this);
1014
1015 ServiceManager.addService("phone", this);
1016 }
1017
Stuart Scott584921c2015-01-15 17:10:34 -08001018 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001019 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1020 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001021 }
1022
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001023 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1024 Phone phone = getPhoneFromRequest(request);
1025 return phone == null ? null :
1026 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1027 }
1028
Wink Saville36469e72014-06-11 15:17:00 -07001029 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001030 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001031 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001032 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 //
1034 // Implementation of the ITelephony interface.
1035 //
1036
1037 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001038 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001039 }
1040
Wink Savilleb564aae2014-10-23 10:18:09 -07001041 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001042 if (DBG) log("dial: " + number);
1043 // No permission check needed here: This is just a wrapper around the
1044 // ACTION_DIAL intent, which is available to any app since it puts up
1045 // the UI before it does anything.
1046
1047 String url = createTelUrl(number);
1048 if (url == null) {
1049 return;
1050 }
1051
1052 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001053 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1055 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1056 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1057 mApp.startActivity(intent);
1058 }
1059 }
1060
1061 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001062 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001063 }
1064
Wink Savilleb564aae2014-10-23 10:18:09 -07001065 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001066 if (DBG) log("call: " + number);
1067
1068 // This is just a wrapper around the ACTION_CALL intent, but we still
1069 // need to do a permission check since we're calling startActivity()
1070 // from the context of the phone app.
1071 enforceCallPermission();
1072
1073 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1074 != AppOpsManager.MODE_ALLOWED) {
1075 return;
1076 }
1077
1078 String url = createTelUrl(number);
1079 if (url == null) {
1080 return;
1081 }
1082
Wink Saville08874612014-08-31 19:19:58 -07001083 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001084 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001085 if (slist != null) {
1086 for (SubscriptionInfo subInfoRecord : slist) {
1087 if (subInfoRecord.getSubscriptionId() == subId) {
1088 isValid = true;
1089 break;
1090 }
Wink Saville08874612014-08-31 19:19:58 -07001091 }
1092 }
1093 if (isValid == false) {
1094 return;
1095 }
1096
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001097 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001098 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1100 mApp.startActivity(intent);
1101 }
1102
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001103 /**
1104 * End a call based on call state
1105 * @return true is a call was ended
1106 */
1107 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001108 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001109 }
1110
1111 /**
1112 * End a call based on the call state of the subId
1113 * @return true is a call was ended
1114 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001115 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001116 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001117 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 }
1119
1120 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001121 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001122 }
1123
Wink Savilleb564aae2014-10-23 10:18:09 -07001124 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 if (DBG) log("answerRingingCall...");
1126 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1127 // but that can probably wait till the big TelephonyManager API overhaul.
1128 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1129 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001130 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001131 }
1132
1133 /**
1134 * Make the actual telephony calls to implement answerRingingCall().
1135 * This should only be called from the main thread of the Phone app.
1136 * @see #answerRingingCall
1137 *
1138 * TODO: it would be nice to return true if we answered the call, or
1139 * false if there wasn't actually a ringing incoming call, or some
1140 * other error occurred. (In other words, pass back the return value
1141 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1142 * But that would require calling this method via sendRequest() rather
1143 * than sendRequestAsync(), and right now we don't actually *need* that
1144 * return value, so let's just return void for now.
1145 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001146 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001147 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001149 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1150 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001151 if (hasActiveCall && hasHoldingCall) {
1152 // Both lines are in use!
1153 // TODO: provide a flag to let the caller specify what
1154 // policy to use if both lines are in use. (The current
1155 // behavior is hardwired to "answer incoming, end ongoing",
1156 // which is how the CALL button is specced to behave.)
1157 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1158 return;
1159 } else {
1160 // answerCall() will automatically hold the current active
1161 // call, if there is one.
1162 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1163 return;
1164 }
1165 } else {
1166 // No call was ringing.
1167 return;
1168 }
1169 }
1170
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001171 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001172 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001173 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001174 public void silenceRinger() {
1175 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001176 }
1177
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001178 @Override
1179 public boolean isOffhook(String callingPackage) {
1180 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001181 }
1182
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001183 @Override
1184 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1185 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1186 return false;
1187 }
1188
Sanket Padawe356d7632015-06-22 14:03:32 -07001189 final Phone phone = getPhone(subId);
1190 if (phone != null) {
1191 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1192 } else {
1193 return false;
1194 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 }
1196
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001197 @Override
1198 public boolean isRinging(String callingPackage) {
1199 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001200 }
1201
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001202 @Override
1203 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1204 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1205 return false;
1206 }
1207
Sanket Padawe356d7632015-06-22 14:03:32 -07001208 final Phone phone = getPhone(subId);
1209 if (phone != null) {
1210 return (phone.getState() == PhoneConstants.State.RINGING);
1211 } else {
1212 return false;
1213 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001214 }
1215
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001216 @Override
1217 public boolean isIdle(String callingPackage) {
1218 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001219 }
1220
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001221 @Override
1222 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1223 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1224 return false;
1225 }
1226
Sanket Padawe356d7632015-06-22 14:03:32 -07001227 final Phone phone = getPhone(subId);
1228 if (phone != null) {
1229 return (phone.getState() == PhoneConstants.State.IDLE);
1230 } else {
1231 return false;
1232 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 }
1234
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001235 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001236 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001237 }
1238
Wink Savilleb564aae2014-10-23 10:18:09 -07001239 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001240 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001241 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1242 }
1243
1244 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001245 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001246 }
1247
Wink Savilleb564aae2014-10-23 10:18:09 -07001248 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001249 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001250 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1251 }
1252
1253 /** {@hide} */
1254 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001255 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001256 }
1257
Wink Savilleb564aae2014-10-23 10:18:09 -07001258 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001259 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001260 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 checkSimPin.start();
1262 return checkSimPin.unlockSim(null, pin);
1263 }
1264
Wink Saville9de0f752013-10-22 19:04:03 -07001265 /** {@hide} */
1266 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001267 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001268 }
1269
Wink Savilleb564aae2014-10-23 10:18:09 -07001270 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001271 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001272 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001273 checkSimPuk.start();
1274 return checkSimPuk.unlockSim(puk, pin);
1275 }
1276
1277 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001278 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001279 * a synchronous one.
1280 */
1281 private static class UnlockSim extends Thread {
1282
1283 private final IccCard mSimCard;
1284
1285 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001286 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1287 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288
1289 // For replies from SimCard interface
1290 private Handler mHandler;
1291
1292 // For async handler to identify request type
1293 private static final int SUPPLY_PIN_COMPLETE = 100;
1294
1295 public UnlockSim(IccCard simCard) {
1296 mSimCard = simCard;
1297 }
1298
1299 @Override
1300 public void run() {
1301 Looper.prepare();
1302 synchronized (UnlockSim.this) {
1303 mHandler = new Handler() {
1304 @Override
1305 public void handleMessage(Message msg) {
1306 AsyncResult ar = (AsyncResult) msg.obj;
1307 switch (msg.what) {
1308 case SUPPLY_PIN_COMPLETE:
1309 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1310 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001311 mRetryCount = msg.arg1;
1312 if (ar.exception != null) {
1313 if (ar.exception instanceof CommandException &&
1314 ((CommandException)(ar.exception)).getCommandError()
1315 == CommandException.Error.PASSWORD_INCORRECT) {
1316 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1317 } else {
1318 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1319 }
1320 } else {
1321 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1322 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001323 mDone = true;
1324 UnlockSim.this.notifyAll();
1325 }
1326 break;
1327 }
1328 }
1329 };
1330 UnlockSim.this.notifyAll();
1331 }
1332 Looper.loop();
1333 }
1334
1335 /*
1336 * Use PIN or PUK to unlock SIM card
1337 *
1338 * If PUK is null, unlock SIM card with PIN
1339 *
1340 * If PUK is not null, unlock SIM card with PUK and set PIN code
1341 */
Wink Saville9de0f752013-10-22 19:04:03 -07001342 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001343
1344 while (mHandler == null) {
1345 try {
1346 wait();
1347 } catch (InterruptedException e) {
1348 Thread.currentThread().interrupt();
1349 }
1350 }
1351 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1352
1353 if (puk == null) {
1354 mSimCard.supplyPin(pin, callback);
1355 } else {
1356 mSimCard.supplyPuk(puk, pin, callback);
1357 }
1358
1359 while (!mDone) {
1360 try {
1361 Log.d(LOG_TAG, "wait for done");
1362 wait();
1363 } catch (InterruptedException e) {
1364 // Restore the interrupted status
1365 Thread.currentThread().interrupt();
1366 }
1367 }
1368 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001369 int[] resultArray = new int[2];
1370 resultArray[0] = mResult;
1371 resultArray[1] = mRetryCount;
1372 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 }
1374 }
1375
1376 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001377 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001378
1379 }
1380
Wink Savilleb564aae2014-10-23 10:18:09 -07001381 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001382 // No permission check needed here: this call is harmless, and it's
1383 // needed for the ServiceState.requestStateUpdate() call (which is
1384 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001385 final Phone phone = getPhone(subId);
1386 if (phone != null) {
1387 phone.updateServiceLocation();
1388 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001389 }
1390
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001391 @Override
1392 public boolean isRadioOn(String callingPackage) {
1393 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001394 }
1395
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001396 @Override
1397 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1398 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1399 return false;
1400 }
1401 return isRadioOnForSubscriber(subId);
1402 }
1403
1404 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001405 final Phone phone = getPhone(subId);
1406 if (phone != null) {
1407 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1408 } else {
1409 return false;
1410 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001411 }
1412
1413 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001414 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001415
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001416 }
Wink Saville36469e72014-06-11 15:17:00 -07001417
Wink Savilleb564aae2014-10-23 10:18:09 -07001418 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001419 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001420 final Phone phone = getPhone(subId);
1421 if (phone != null) {
1422 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1423 }
Wink Saville36469e72014-06-11 15:17:00 -07001424 }
1425
1426 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001427 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001428 }
1429
Wink Savilleb564aae2014-10-23 10:18:09 -07001430 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001431 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001432 final Phone phone = getPhone(subId);
1433 if (phone == null) {
1434 return false;
1435 }
1436 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001437 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001438 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 }
1440 return true;
1441 }
Wink Saville36469e72014-06-11 15:17:00 -07001442
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001443 public boolean needMobileRadioShutdown() {
1444 /*
1445 * If any of the Radios are available, it will need to be
1446 * shutdown. So return true if any Radio is available.
1447 */
1448 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1449 Phone phone = PhoneFactory.getPhone(i);
1450 if (phone != null && phone.isRadioAvailable()) return true;
1451 }
1452 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1453 return false;
1454 }
1455
1456 public void shutdownMobileRadios() {
1457 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1458 logv("Shutting down Phone " + i);
1459 shutdownRadioUsingPhoneId(i);
1460 }
1461 }
1462
1463 private void shutdownRadioUsingPhoneId(int phoneId) {
1464 enforceModifyPermission();
1465 Phone phone = PhoneFactory.getPhone(phoneId);
1466 if (phone != null && phone.isRadioAvailable()) {
1467 phone.shutdownRadio();
1468 }
1469 }
1470
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001472 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1473 if (defaultPhone != null) {
1474 defaultPhone.setRadioPower(turnOn);
1475 return true;
1476 } else {
1477 loge("There's no default phone.");
1478 return false;
1479 }
Wink Saville36469e72014-06-11 15:17:00 -07001480 }
1481
Wink Savilleb564aae2014-10-23 10:18:09 -07001482 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001483 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001484 final Phone phone = getPhone(subId);
1485 if (phone != null) {
1486 phone.setRadioPower(turnOn);
1487 return true;
1488 } else {
1489 return false;
1490 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001491 }
1492
Wink Saville36469e72014-06-11 15:17:00 -07001493 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001494 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001495 public boolean enableDataConnectivity() {
1496 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001497 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001498 final Phone phone = getPhone(subId);
1499 if (phone != null) {
1500 phone.setDataEnabled(true);
1501 return true;
1502 } else {
1503 return false;
1504 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001505 }
1506
Wink Saville36469e72014-06-11 15:17:00 -07001507 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001508 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001509 public boolean disableDataConnectivity() {
1510 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001511 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001512 final Phone phone = getPhone(subId);
1513 if (phone != null) {
1514 phone.setDataEnabled(false);
1515 return true;
1516 } else {
1517 return false;
1518 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001519 }
1520
Wink Saville36469e72014-06-11 15:17:00 -07001521 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001522 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001523 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001524 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001525 final Phone phone = getPhone(subId);
1526 if (phone != null) {
1527 return phone.isDataConnectivityPossible();
1528 } else {
1529 return false;
1530 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001531 }
1532
1533 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001534 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001535 }
1536
Wink Savilleb564aae2014-10-23 10:18:09 -07001537 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001538 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001539 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1540 return false;
1541 }
Wink Saville36469e72014-06-11 15:17:00 -07001542 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001543 }
1544
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001546 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001547 }
1548
Sanket Padawe5780e442017-03-20 15:04:47 -07001549 public int getCallStateForSlot(int slotIndex) {
1550 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001551 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1552 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001553 }
1554
Sanket Padawe356d7632015-06-22 14:03:32 -07001555 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001556 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001557 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001558 if (phone != null) {
1559 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1560 } else {
1561 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1562 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001563 }
1564
Sanket Padawe356d7632015-06-22 14:03:32 -07001565 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001566 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001567 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001568 if (phone != null) {
1569 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1570 } else {
1571 return TelephonyManager.DATA_ACTIVITY_NONE;
1572 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001573 }
1574
1575 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001576 public Bundle getCellLocation(String callingPackage) {
1577 enforceFineOrCoarseLocationPermission("getCellLocation");
1578
1579 // OP_COARSE_LOCATION controls both fine and coarse location.
1580 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1581 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001582 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001583 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001584 }
1585
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001586 if (checkIfCallerIsSelfOrForegroundUser() ||
1587 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001588 if (DBG_LOC) log("getCellLocation: is active user");
1589 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001590 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001591 if (phone == null) {
1592 return null;
1593 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07001594
1595 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1596 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001597 return data;
1598 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001599 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001600 return null;
1601 }
1602 }
1603
Svetoslav64fad262015-04-14 14:35:21 -07001604 private void enforceFineOrCoarseLocationPermission(String message) {
1605 try {
1606 mApp.enforceCallingOrSelfPermission(
1607 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1608 } catch (SecurityException e) {
1609 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1610 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1611 // is the weaker precondition
1612 mApp.enforceCallingOrSelfPermission(
1613 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1614 }
1615 }
1616
1617
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001618 @Override
1619 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001620 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001621 }
1622
Sanket Padawe356d7632015-06-22 14:03:32 -07001623 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001624 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001625 mApp.enforceCallingOrSelfPermission(
1626 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001627 final Phone phone = getPhone(subId);
1628 if (phone != null) {
1629 phone.enableLocationUpdates();
1630 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001631 }
1632
1633 @Override
1634 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001635 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001636 }
1637
Sanket Padawe356d7632015-06-22 14:03:32 -07001638 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001639 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001640 mApp.enforceCallingOrSelfPermission(
1641 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001642 final Phone phone = getPhone(subId);
1643 if (phone != null) {
1644 phone.disableLocationUpdates();
1645 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001646 }
1647
1648 @Override
1649 @SuppressWarnings("unchecked")
1650 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001651 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1652
1653 // OP_COARSE_LOCATION controls both fine and coarse location.
1654 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1655 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1656 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 }
1658
1659 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1660 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1661 return null;
1662 }
Svetoslav64fad262015-04-14 14:35:21 -07001663
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001664 if (checkIfCallerIsSelfOrForegroundUser() ||
1665 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001666 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1667
1668 ArrayList<NeighboringCellInfo> cells = null;
1669
Sooraj Sasindran22882212016-07-18 11:57:25 -07001670 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001671 try {
1672 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran22882212016-07-18 11:57:25 -07001673 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001674 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001675 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001676 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001677 }
1678 return cells;
1679 } else {
1680 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1681 return null;
1682 }
1683 }
1684
1685
1686 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001687 public List<CellInfo> getAllCellInfo(String callingPackage) {
1688 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1689
1690 // OP_COARSE_LOCATION controls both fine and coarse location.
1691 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1692 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1693 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001694 }
1695
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001696 if (checkIfCallerIsSelfOrForegroundUser() ||
1697 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001698 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran22882212016-07-18 11:57:25 -07001699 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001700 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1701 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran22882212016-07-18 11:57:25 -07001702 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1703 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001704 }
1705 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001706 } else {
1707 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1708 return null;
1709 }
1710 }
1711
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001712 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001713 public void setCellInfoListRate(int rateInMillis) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001714 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001715 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1716 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001717 }
1718
Shishir Agrawala9f32182016-04-12 12:00:16 -07001719 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001720 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001721 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1722 return null;
1723 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001724 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001725 return phone == null ? null : phone.getImei();
1726 }
1727
1728 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001729 public String getMeidForSlot(int slotIndex, String callingPackage) {
1730 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1731 return null;
1732 }
1733 Phone phone = PhoneFactory.getPhone(slotIndex);
1734 return phone == null ? null : phone.getMeid();
1735 }
1736
1737 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001738 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001739 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1740 return null;
1741 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001742 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001743 return phone == null ? null : phone.getDeviceSvn();
1744 }
1745
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001746 //
1747 // Internal helper methods.
1748 //
1749
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001750 /**
1751 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1752 */
1753 private boolean checkCallerInteractAcrossUsersFull() {
1754 return mPhone.getContext().checkCallingOrSelfPermission(
1755 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1756 == PackageManager.PERMISSION_GRANTED;
1757 }
1758
Jake Hambye994d462014-02-03 13:10:13 -08001759 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001760 boolean ok;
1761
1762 boolean self = Binder.getCallingUid() == Process.myUid();
1763 if (!self) {
1764 // Get the caller's user id then clear the calling identity
1765 // which will be restored in the finally clause.
1766 int callingUser = UserHandle.getCallingUserId();
1767 long ident = Binder.clearCallingIdentity();
1768
1769 try {
1770 // With calling identity cleared the current user is the foreground user.
1771 int foregroundUser = ActivityManager.getCurrentUser();
1772 ok = (foregroundUser == callingUser);
1773 if (DBG_LOC) {
1774 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1775 + " callingUser=" + callingUser + " ok=" + ok);
1776 }
1777 } catch (Exception ex) {
1778 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1779 ok = false;
1780 } finally {
1781 Binder.restoreCallingIdentity(ident);
1782 }
1783 } else {
1784 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1785 ok = true;
1786 }
1787 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1788 return ok;
1789 }
1790
1791 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001792 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1793 *
1794 * @throws SecurityException if the caller does not have the required permission
1795 */
1796 private void enforceModifyPermission() {
1797 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1798 }
1799
1800 /**
Junda Liua2e36012014-07-09 18:30:01 -07001801 * Make sure either system app or the caller has carrier privilege.
1802 *
1803 * @throws SecurityException if the caller does not have the required permission/privilege
1804 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001805 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001806 int permission = mApp.checkCallingOrSelfPermission(
1807 android.Manifest.permission.MODIFY_PHONE_STATE);
1808 if (permission == PackageManager.PERMISSION_GRANTED) {
1809 return;
1810 }
1811
1812 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001813 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001814 }
1815
1816 /**
1817 * Make sure the caller has carrier privilege.
1818 *
1819 * @throws SecurityException if the caller does not have the required permission
1820 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001821 private void enforceCarrierPrivilege(int subId) {
1822 if (getCarrierPrivilegeStatus(subId) !=
1823 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001824 loge("No Carrier Privilege.");
1825 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001826 }
1827 }
1828
1829 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001830 * Make sure the caller has the CALL_PHONE permission.
1831 *
1832 * @throws SecurityException if the caller does not have the required permission
1833 */
1834 private void enforceCallPermission() {
1835 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1836 }
1837
Stuart Scott8eef64f2015-04-08 15:13:54 -07001838 private void enforceConnectivityInternalPermission() {
1839 mApp.enforceCallingOrSelfPermission(
1840 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1841 "ConnectivityService");
1842 }
1843
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001844 private String createTelUrl(String number) {
1845 if (TextUtils.isEmpty(number)) {
1846 return null;
1847 }
1848
Jake Hambye994d462014-02-03 13:10:13 -08001849 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001850 }
1851
Ihab Awadf9e92732013-12-05 18:02:52 -08001852 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001853 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1854 }
1855
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001856 private static void logv(String msg) {
1857 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1858 }
1859
Ihab Awadf9e92732013-12-05 18:02:52 -08001860 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001861 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1862 }
1863
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001864 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001865 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001866 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001867 }
1868
Sanket Padawe356d7632015-06-22 14:03:32 -07001869 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001870 public int getActivePhoneTypeForSlot(int slotIndex) {
1871 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001872 if (phone == null) {
1873 return PhoneConstants.PHONE_TYPE_NONE;
1874 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001875 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001876 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001877 }
1878
1879 /**
1880 * Returns the CDMA ERI icon index to display
1881 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001882 @Override
1883 public int getCdmaEriIconIndex(String callingPackage) {
1884 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001885 }
1886
Sanket Padawe356d7632015-06-22 14:03:32 -07001887 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001888 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1889 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1890 return -1;
1891 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001892 final Phone phone = getPhone(subId);
1893 if (phone != null) {
1894 return phone.getCdmaEriIconIndex();
1895 } else {
1896 return -1;
1897 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001898 }
1899
1900 /**
1901 * Returns the CDMA ERI icon mode,
1902 * 0 - ON
1903 * 1 - FLASHING
1904 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001905 @Override
1906 public int getCdmaEriIconMode(String callingPackage) {
1907 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001908 }
1909
Sanket Padawe356d7632015-06-22 14:03:32 -07001910 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001911 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1912 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1913 return -1;
1914 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001915 final Phone phone = getPhone(subId);
1916 if (phone != null) {
1917 return phone.getCdmaEriIconMode();
1918 } else {
1919 return -1;
1920 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001921 }
1922
1923 /**
1924 * Returns the CDMA ERI text,
1925 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001926 @Override
1927 public String getCdmaEriText(String callingPackage) {
1928 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001929 }
1930
Sanket Padawe356d7632015-06-22 14:03:32 -07001931 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001932 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1933 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1934 return null;
1935 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001936 final Phone phone = getPhone(subId);
1937 if (phone != null) {
1938 return phone.getCdmaEriText();
1939 } else {
1940 return null;
1941 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001942 }
1943
1944 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001945 * Returns the CDMA MDN.
1946 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001947 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001948 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001949 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001950 final Phone phone = getPhone(subId);
1951 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1952 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001953 } else {
1954 return null;
1955 }
1956 }
1957
1958 /**
1959 * Returns the CDMA MIN.
1960 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001961 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001962 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001963 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001964 final Phone phone = getPhone(subId);
1965 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1966 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001967 } else {
1968 return null;
1969 }
1970 }
1971
1972 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001973 * Returns true if CDMA provisioning needs to run.
1974 */
1975 public boolean needsOtaServiceProvisioning() {
1976 return mPhone.needsOtaServiceProvisioning();
1977 }
1978
1979 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001980 * Sets the voice mail number of a given subId.
1981 */
1982 @Override
1983 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001984 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001985 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1986 new Pair<String, String>(alphaTag, number), new Integer(subId));
1987 return success;
1988 }
1989
Ta-wei Yen87c49842016-05-13 21:19:52 -07001990 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001991 public void setVisualVoicemailEnabled(String callingPackage,
1992 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1993 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1994 if (!TextUtils.equals(callingPackage,
1995 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1996 enforceModifyPermissionOrCarrierPrivilege(
1997 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1998 }
1999 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2000 }
2001
2002 @Override
2003 public boolean isVisualVoicemailEnabled(String callingPackage,
2004 PhoneAccountHandle phoneAccountHandle) {
2005 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2006 return false;
2007 }
2008 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2009 }
2010
2011 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002012 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2013 VisualVoicemailSmsFilterSettings settings) {
2014 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002015 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002016 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2017 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002018 }
2019
2020 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002021 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2022 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002023 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002024 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002025 }
2026
2027 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002028 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2029 String callingPackage, int subId) {
2030 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002031 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002032 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002033 }
2034
2035 @Override
Ta-wei Yen1b455992016-12-27 14:52:32 -08002036 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002037 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002038 return VisualVoicemailSmsFilterConfig
Ta-wei Yen1b455992016-12-27 14:52:32 -08002039 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2040 }
2041
2042 @Override
2043 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2044 String number, int port, String text, PendingIntent sentIntent) {
2045 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2046 enforceDefaultDialer(callingPackage);
2047 enforceSendSmsPermission();
2048 // Make the calls as the phone process.
2049 final long identity = Binder.clearCallingIdentity();
2050 try {
2051 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2052 if (port == 0) {
2053 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2054 sentIntent, null, false);
2055 } else {
2056 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2057 smsManager.sendDataMessageWithSelfPermissions(number, null,
2058 (short) port, data, sentIntent, null);
2059 }
2060 } finally {
2061 Binder.restoreCallingIdentity(identity);
2062 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002063 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002064 /**
fionaxuef039d42016-11-14 13:36:14 -08002065 * Sets the voice activation state of a given subId.
2066 */
2067 @Override
2068 public void setVoiceActivationState(int subId, int activationState) {
2069 enforceModifyPermissionOrCarrierPrivilege(subId);
2070 final Phone phone = getPhone(subId);
2071 if (phone != null) {
2072 phone.setVoiceActivationState(activationState);
2073 } else {
2074 loge("setVoiceActivationState fails with invalid subId: " + subId);
2075 }
2076 }
2077
2078 /**
2079 * Sets the data activation state of a given subId.
2080 */
2081 @Override
2082 public void setDataActivationState(int subId, int activationState) {
2083 enforceModifyPermissionOrCarrierPrivilege(subId);
2084 final Phone phone = getPhone(subId);
2085 if (phone != null) {
2086 phone.setDataActivationState(activationState);
2087 } else {
2088 loge("setVoiceActivationState fails with invalid subId: " + subId);
2089 }
2090 }
2091
2092 /**
2093 * Returns the voice activation state of a given subId.
2094 */
2095 @Override
2096 public int getVoiceActivationState(int subId, String callingPackage) {
2097 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2098 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2099 }
2100 final Phone phone = getPhone(subId);
2101 if (phone != null) {
2102 return phone.getVoiceActivationState();
2103 } else {
2104 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2105 }
2106 }
2107
2108 /**
2109 * Returns the data activation state of a given subId.
2110 */
2111 @Override
2112 public int getDataActivationState(int subId, String callingPackage) {
2113 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2114 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2115 }
2116 final Phone phone = getPhone(subId);
2117 if (phone != null) {
2118 return phone.getDataActivationState();
2119 } else {
2120 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2121 }
2122 }
2123
2124 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002125 * Returns the unread count of voicemails
2126 */
2127 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002128 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002129 }
2130
2131 /**
2132 * Returns the unread count of voicemails for a subId
2133 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002134 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002135 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002136 final Phone phone = getPhone(subId);
2137 if (phone != null) {
2138 return phone.getVoiceMessageCount();
2139 } else {
2140 return 0;
2141 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002142 }
2143
2144 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002145 * Returns the data network type.
2146 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002147 *
2148 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2149 */
2150 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002151 public int getNetworkType() {
2152 final Phone phone = getPhone(getDefaultSubscription());
2153 if (phone != null) {
2154 return phone.getServiceState().getDataNetworkType();
2155 } else {
2156 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2157 }
Wink Saville36469e72014-06-11 15:17:00 -07002158 }
2159
2160 /**
2161 * Returns the network type for a subId
2162 */
2163 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002164 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2165 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2166 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2167 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002168
Sanket Padawe356d7632015-06-22 14:03:32 -07002169 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002170 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002171 return phone.getServiceState().getDataNetworkType();
2172 } else {
2173 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2174 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002175 }
2176
2177 /**
2178 * Returns the data network type
2179 */
2180 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002181 public int getDataNetworkType(String callingPackage) {
2182 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002183 }
2184
2185 /**
2186 * Returns the data network type for a subId
2187 */
2188 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002189 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2190 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2191 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2192 }
2193
Sanket Padawe356d7632015-06-22 14:03:32 -07002194 final Phone phone = getPhone(subId);
2195 if (phone != null) {
2196 return phone.getServiceState().getDataNetworkType();
2197 } else {
2198 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2199 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002200 }
2201
2202 /**
Wink Saville36469e72014-06-11 15:17:00 -07002203 * Returns the Voice network type for a subId
2204 */
2205 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002206 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2207 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2208 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2209 }
2210
Sanket Padawe356d7632015-06-22 14:03:32 -07002211 final Phone phone = getPhone(subId);
2212 if (phone != null) {
2213 return phone.getServiceState().getVoiceNetworkType();
2214 } else {
2215 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2216 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002217 }
2218
2219 /**
2220 * @return true if a ICC card is present
2221 */
2222 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002223 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002224 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2225 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002226 }
2227
2228 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002229 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002230 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002231 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002232 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2233 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002234 final Phone phone = getPhone(subId[0]);
2235 if (subId != null && phone != null) {
2236 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002237 } else {
2238 return false;
2239 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002240 }
2241
2242 /**
2243 * Return if the current radio is LTE on CDMA. This
2244 * is a tri-state return value as for a period of time
2245 * the mode may be unknown.
2246 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002247 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002248 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002249 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002250 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002251 @Override
2252 public int getLteOnCdmaMode(String callingPackage) {
2253 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002254 }
2255
Sanket Padawe356d7632015-06-22 14:03:32 -07002256 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002257 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2258 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2259 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2260 }
2261
Sanket Padawe356d7632015-06-22 14:03:32 -07002262 final Phone phone = getPhone(subId);
2263 if (phone == null) {
2264 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2265 } else {
2266 return phone.getLteOnCdmaMode();
2267 }
Wink Saville36469e72014-06-11 15:17:00 -07002268 }
2269
2270 public void setPhone(Phone phone) {
2271 mPhone = phone;
2272 }
2273
2274 /**
2275 * {@hide}
2276 * Returns Default subId, 0 in the case of single standby.
2277 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002278 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002279 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002280 }
2281
Shishir Agrawala9f32182016-04-12 12:00:16 -07002282 private int getSlotForDefaultSubscription() {
2283 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2284 }
2285
Wink Savilleb564aae2014-10-23 10:18:09 -07002286 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002287 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002288 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002289
2290 /**
2291 * @see android.telephony.TelephonyManager.WifiCallingChoices
2292 */
2293 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002294 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2295 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002296 }
2297
2298 /**
2299 * @see android.telephony.TelephonyManager.WifiCallingChoices
2300 */
2301 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002302 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2303 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2304 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002305 }
2306
Sailesh Nepald1e68152013-12-12 19:08:02 -08002307 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002308 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002309 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002310 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002311
Shishir Agrawal566b7612013-10-28 14:41:00 -07002312 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002313 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002314 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002315
Ajay Nambiabd73502015-12-03 13:50:00 -08002316 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002317 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002318 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002319 if (DBG) log("iccOpenLogicalChannel: " + response);
2320 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002321 }
2322
2323 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002324 public boolean iccCloseLogicalChannel(int subId, int channel) {
2325 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002326
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002327 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002328 if (channel < 0) {
2329 return false;
2330 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002331 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002332 if (DBG) log("iccCloseLogicalChannel: " + success);
2333 return success;
2334 }
2335
2336 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002337 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002338 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002339 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002340
2341 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002342 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2343 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002344 " data=" + data);
2345 }
2346
2347 if (channel < 0) {
2348 return "";
2349 }
2350
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002351 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002352 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002353 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2354
Shishir Agrawal566b7612013-10-28 14:41:00 -07002355 // Append the returned status code to the end of the response payload.
2356 String s = Integer.toHexString(
2357 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002358 if (response.payload != null) {
2359 s = IccUtils.bytesToHexString(response.payload) + s;
2360 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002361 return s;
2362 }
Jake Hambye994d462014-02-03 13:10:13 -08002363
Evan Charltonc66da362014-05-16 14:06:40 -07002364 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002365 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002366 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002367 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002368
2369 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002370 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2371 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002372 }
2373
2374 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002375 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002376 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2377
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002378 // Append the returned status code to the end of the response payload.
2379 String s = Integer.toHexString(
2380 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002381 if (response.payload != null) {
2382 s = IccUtils.bytesToHexString(response.payload) + s;
2383 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002384 return s;
2385 }
2386
2387 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002388 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002389 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002390 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002391
2392 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002393 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002394 p1 + " " + p2 + " " + p3 + ":" + filePath);
2395 }
2396
2397 IccIoResult response =
2398 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002399 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2400 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002401
2402 if (DBG) {
2403 log("Exchange SIM_IO [R]" + response);
2404 }
2405
2406 byte[] result = null;
2407 int length = 2;
2408 if (response.payload != null) {
2409 length = 2 + response.payload.length;
2410 result = new byte[length];
2411 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2412 } else {
2413 result = new byte[length];
2414 }
2415
2416 result[length - 1] = (byte) response.sw2;
2417 result[length - 2] = (byte) response.sw1;
2418 return result;
2419 }
2420
Nathan Haroldb3014052017-01-25 15:57:32 -08002421 /**
2422 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2423 * on a particular subscription
2424 */
2425 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002426 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2427 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002428 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2429 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2430 return null;
2431 }
2432 Object response = sendRequest(
2433 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2434 if (response instanceof String[]) {
2435 return (String[]) response;
2436 }
2437 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2438 return null;
2439 }
2440
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002441 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002442 public String sendEnvelopeWithStatus(int subId, String content) {
2443 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002444
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002445 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002446 if (response.payload == null) {
2447 return "";
2448 }
2449
2450 // Append the returned status code to the end of the response payload.
2451 String s = Integer.toHexString(
2452 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2453 s = IccUtils.bytesToHexString(response.payload) + s;
2454 return s;
2455 }
2456
Jake Hambye994d462014-02-03 13:10:13 -08002457 /**
2458 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2459 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2460 *
2461 * @param itemID the ID of the item to read
2462 * @return the NV item as a String, or null on error.
2463 */
2464 @Override
2465 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002466 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002467 if (DBG) log("nvReadItem: item " + itemID);
2468 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2469 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2470 return value;
2471 }
2472
2473 /**
2474 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2475 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2476 *
2477 * @param itemID the ID of the item to read
2478 * @param itemValue the value to write, as a String
2479 * @return true on success; false on any failure
2480 */
2481 @Override
2482 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002483 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002484 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2485 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2486 new Pair<Integer, String>(itemID, itemValue));
2487 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2488 return success;
2489 }
2490
2491 /**
2492 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2493 * Used for device configuration by some CDMA operators.
2494 *
2495 * @param preferredRoamingList byte array containing the new PRL
2496 * @return true on success; false on any failure
2497 */
2498 @Override
2499 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002500 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002501 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2502 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2503 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2504 return success;
2505 }
2506
2507 /**
2508 * Perform the specified type of NV config reset.
2509 * Used for device configuration by some CDMA operators.
2510 *
2511 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2512 * @return true on success; false on any failure
2513 */
2514 @Override
2515 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002516 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002517 if (DBG) log("nvResetConfig: type " + resetType);
2518 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2519 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2520 return success;
2521 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002522
2523 /**
Wink Saville36469e72014-06-11 15:17:00 -07002524 * {@hide}
2525 * Returns Default sim, 0 in the case of single standby.
2526 */
2527 public int getDefaultSim() {
2528 //TODO Need to get it from Telephony Devcontroller
2529 return 0;
2530 }
2531
Svet Ganovb320e182015-04-16 12:30:10 -07002532 public String[] getPcscfAddress(String apnType, String callingPackage) {
2533 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2534 return new String[0];
2535 }
2536
2537
ram87fca6f2014-07-18 18:58:44 +05302538 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002539 }
2540
Brad Ebinger76681002017-01-23 13:50:20 -08002541 /**
2542 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2543 * feature or {@link null} if the service is not available. If an ImsServiceController is
2544 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2545 * feature updates.
2546 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002547 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002548 IImsServiceFeatureListener callback) {
2549 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002550 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002551 callback);
2552 }
2553
Wink Saville36469e72014-06-11 15:17:00 -07002554 public void setImsRegistrationState(boolean registered) {
2555 enforceModifyPermission();
2556 mPhone.setImsRegistrationState(registered);
2557 }
2558
2559 /**
Stuart Scott54788802015-03-30 13:18:01 -07002560 * Set the network selection mode to automatic.
2561 *
2562 */
2563 @Override
2564 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002565 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002566 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2567 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2568 }
2569
2570 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002571 * Set the network selection mode to manual with the selected carrier.
2572 */
2573 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002574 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2575 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002576 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002577 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002578 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2579 persistSelection);
2580 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002581 }
2582
2583 /**
2584 * Scans for available networks.
2585 */
2586 @Override
2587 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002588 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002589 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2590 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2591 CMD_PERFORM_NETWORK_SCAN, null, subId);
2592 return result;
2593 }
2594
2595 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002596 * Get the calculated preferred network type.
2597 * Used for debugging incorrect network type.
2598 *
2599 * @return the preferred network type, defined in RILConstants.java.
2600 */
2601 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002602 public int getCalculatedPreferredNetworkType(String callingPackage) {
2603 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2604 return RILConstants.PREFERRED_NETWORK_MODE;
2605 }
2606
Amit Mahajan43330e02014-11-18 11:54:45 -08002607 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002608 }
2609
2610 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002611 * Get the preferred network type.
2612 * Used for device configuration by some CDMA operators.
2613 *
2614 * @return the preferred network type, defined in RILConstants.java.
2615 */
2616 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002617 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002618 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002619 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002620 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002621 int networkType = (result != null ? result[0] : -1);
2622 if (DBG) log("getPreferredNetworkType: " + networkType);
2623 return networkType;
2624 }
2625
2626 /**
2627 * Set the preferred network type.
2628 * Used for device configuration by some CDMA operators.
2629 *
2630 * @param networkType the preferred network type, defined in RILConstants.java.
2631 * @return true on success; false on any failure.
2632 */
2633 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002634 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002635 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002636 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2637 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002638 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002639 if (success) {
2640 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002641 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002642 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002643 return success;
2644 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002645
2646 /**
Junda Liu475951f2014-11-07 16:45:03 -08002647 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2648 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2649 * tethering.
2650 *
2651 * @return 0: Not required. 1: required. 2: Not set.
2652 * @hide
2653 */
2654 @Override
2655 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002656 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002657 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2658 Settings.Global.TETHER_DUN_REQUIRED, 2);
2659 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2660 // config_tether_apndata.
2661 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2662 dunRequired = 1;
2663 }
2664 return dunRequired;
2665 }
2666
2667 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002668 * Set mobile data enabled
2669 * Used by the user through settings etc to turn on/off mobile data
2670 *
2671 * @param enable {@code true} turn turn data on, else {@code false}
2672 */
2673 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002674 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002675 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002676 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002677 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002678 Phone phone = PhoneFactory.getPhone(phoneId);
2679 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002680 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002681 phone.setDataEnabled(enable);
2682 } else {
2683 loge("setDataEnabled: no phone for subId=" + subId);
2684 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002685 }
2686
2687 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002688 * Get whether mobile data is enabled.
2689 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002690 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002691 *
2692 * @return {@code true} if data is enabled else {@code false}
2693 */
2694 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002695 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002696 try {
2697 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2698 null);
2699 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002700 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002701 }
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) {
2706 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002707 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002708 return retVal;
2709 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002710 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002711 return false;
2712 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002713 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002714
2715 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002716 public int getCarrierPrivilegeStatus(int subId) {
2717 final Phone phone = getPhone(subId);
2718 if (phone == null) {
2719 loge("getCarrierPrivilegeStatus: Invalid subId");
2720 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2721 }
2722 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002723 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002724 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002725 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2726 }
2727 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002728 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002729 }
Junda Liu29340342014-07-10 15:23:27 -07002730
2731 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002732 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002733 if (TextUtils.isEmpty(pkgName))
2734 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002735 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002736 if (card == null) {
2737 loge("checkCarrierPrivilegesForPackage: No UICC");
2738 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2739 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002740 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2741 }
2742
2743 @Override
2744 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002745 if (TextUtils.isEmpty(pkgName))
2746 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002747 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2748 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2749 UiccCard card = UiccController.getInstance().getUiccCard(i);
2750 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002751 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002752 continue;
2753 }
2754
2755 result = card.getCarrierPrivilegeStatus(
2756 mPhone.getContext().getPackageManager(), pkgName);
2757 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2758 break;
2759 }
2760 }
2761
2762 return result;
Junda Liu29340342014-07-10 15:23:27 -07002763 }
Derek Tan89e89d42014-07-08 17:00:10 -07002764
2765 @Override
Junda Liue64de782015-04-16 17:19:16 -07002766 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2767 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2768 loge("phoneId " + phoneId + " is not valid.");
2769 return null;
2770 }
2771 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002772 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002773 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002774 return null ;
2775 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002776 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002777 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002778 }
2779
Amith Yamasani6e118872016-02-19 12:53:51 -08002780 @Override
2781 public List<String> getPackagesWithCarrierPrivileges() {
2782 PackageManager pm = mPhone.getContext().getPackageManager();
2783 List<String> privilegedPackages = new ArrayList<>();
2784 List<PackageInfo> packages = null;
2785 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2786 UiccCard card = UiccController.getInstance().getUiccCard(i);
2787 if (card == null) {
2788 // No UICC in that slot.
2789 continue;
2790 }
2791 if (card.hasCarrierPrivilegeRules()) {
2792 if (packages == null) {
2793 // Only check packages in user 0 for now
2794 packages = pm.getInstalledPackagesAsUser(
2795 PackageManager.MATCH_DISABLED_COMPONENTS
2796 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2797 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2798 }
2799 for (int p = packages.size() - 1; p >= 0; p--) {
2800 PackageInfo pkgInfo = packages.get(p);
2801 if (pkgInfo != null && pkgInfo.packageName != null
2802 && card.getCarrierPrivilegeStatus(pkgInfo)
2803 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2804 privilegedPackages.add(pkgInfo.packageName);
2805 }
2806 }
2807 }
2808 }
2809 return privilegedPackages;
2810 }
2811
Wink Savilleb564aae2014-10-23 10:18:09 -07002812 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002813 final Phone phone = getPhone(subId);
2814 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002815 if (card == null) {
2816 loge("getIccId: No UICC");
2817 return null;
2818 }
2819 String iccId = card.getIccId();
2820 if (TextUtils.isEmpty(iccId)) {
2821 loge("getIccId: ICC ID is null or empty.");
2822 return null;
2823 }
2824 return iccId;
2825 }
2826
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002827 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002828 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2829 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002830 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002831
Jeff Sharkey85190e62014-12-05 09:40:12 -08002832 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002833 final Phone phone = getPhone(subId);
2834 if (phone == null) {
2835 return false;
2836 }
2837 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002838
2839 if (DBG_MERGE) {
2840 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2841 + subscriberId + " to " + number);
2842 }
2843
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002844 if (TextUtils.isEmpty(iccId)) {
2845 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002846 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002847
Jeff Sharkey85190e62014-12-05 09:40:12 -08002848 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2849
2850 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002851 if (alphaTag == null) {
2852 editor.remove(alphaTagPrefKey);
2853 } else {
2854 editor.putString(alphaTagPrefKey, alphaTag);
2855 }
2856
Jeff Sharkey85190e62014-12-05 09:40:12 -08002857 // Record both the line number and IMSI for this ICCID, since we need to
2858 // track all merged IMSIs based on line number
2859 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2860 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002861 if (number == null) {
2862 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002863 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002864 } else {
2865 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002866 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002867 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002868
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002869 editor.commit();
2870 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002871 }
2872
2873 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002874 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002875 // This is open to apps with WRITE_SMS.
2876 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002877 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002878 return null;
2879 }
Derek Tan97ebb422014-09-05 16:55:38 -07002880
2881 String iccId = getIccId(subId);
2882 if (iccId != null) {
2883 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002884 if (DBG_MERGE) {
2885 log("getLine1NumberForDisplay returning " +
2886 mTelephonySharedPreferences.getString(numberPrefKey, null));
2887 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002888 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002889 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002890 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002891 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002892 }
2893
2894 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002895 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2896 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2897 return null;
2898 }
Derek Tan97ebb422014-09-05 16:55:38 -07002899
2900 String iccId = getIccId(subId);
2901 if (iccId != null) {
2902 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002903 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002904 }
Derek Tan97ebb422014-09-05 16:55:38 -07002905 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002906 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002907
2908 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002909 public String[] getMergedSubscriberIds(String callingPackage) {
2910 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2911 return null;
2912 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002913 final Context context = mPhone.getContext();
2914 final TelephonyManager tele = TelephonyManager.from(context);
2915 final SubscriptionManager sub = SubscriptionManager.from(context);
2916
2917 // Figure out what subscribers are currently active
2918 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002919 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2920 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2921 final long identity = Binder.clearCallingIdentity();
2922 try {
2923 final int[] subIds = sub.getActiveSubscriptionIdList();
2924 for (int subId : subIds) {
2925 activeSubscriberIds.add(tele.getSubscriberId(subId));
2926 }
2927 } finally {
2928 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002929 }
2930
2931 // First pass, find a number override for an active subscriber
2932 String mergeNumber = null;
2933 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2934 for (String key : prefs.keySet()) {
2935 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2936 final String subscriberId = (String) prefs.get(key);
2937 if (activeSubscriberIds.contains(subscriberId)) {
2938 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2939 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2940 mergeNumber = (String) prefs.get(numberKey);
2941 if (DBG_MERGE) {
2942 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2943 + " for active subscriber " + subscriberId);
2944 }
2945 if (!TextUtils.isEmpty(mergeNumber)) {
2946 break;
2947 }
2948 }
2949 }
2950 }
2951
2952 // Shortcut when no active merged subscribers
2953 if (TextUtils.isEmpty(mergeNumber)) {
2954 return null;
2955 }
2956
2957 // Second pass, find all subscribers under that line override
2958 final ArraySet<String> result = new ArraySet<>();
2959 for (String key : prefs.keySet()) {
2960 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2961 final String number = (String) prefs.get(key);
2962 if (mergeNumber.equals(number)) {
2963 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2964 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2965 final String subscriberId = (String) prefs.get(subscriberKey);
2966 if (!TextUtils.isEmpty(subscriberId)) {
2967 result.add(subscriberId);
2968 }
2969 }
2970 }
2971 }
2972
2973 final String[] resultArray = result.toArray(new String[result.size()]);
2974 Arrays.sort(resultArray);
2975 if (DBG_MERGE) {
2976 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2977 }
2978 return resultArray;
2979 }
2980
2981 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002982 public boolean setOperatorBrandOverride(int subId, String brand) {
2983 enforceCarrierPrivilege(subId);
2984 final Phone phone = getPhone(subId);
2985 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002986 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002987
2988 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002989 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002990 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2991 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002992 enforceCarrierPrivilege(subId);
2993 final Phone phone = getPhone(subId);
2994 if (phone == null) {
2995 return false;
2996 }
2997 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002998 cdmaNonRoamingList);
2999 }
3000
3001 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003002 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003003 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3004 enforceModifyPermission();
3005
3006 int returnValue = 0;
3007 try {
3008 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3009 if(result.exception == null) {
3010 if (result.result != null) {
3011 byte[] responseData = (byte[])(result.result);
3012 if(responseData.length > oemResp.length) {
3013 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3014 responseData.length + "bytes. Buffer Size is " +
3015 oemResp.length + "bytes.");
3016 }
3017 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3018 returnValue = responseData.length;
3019 }
3020 } else {
3021 CommandException ex = (CommandException) result.exception;
3022 returnValue = ex.getCommandError().ordinal();
3023 if(returnValue > 0) returnValue *= -1;
3024 }
3025 } catch (RuntimeException e) {
3026 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3027 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3028 if(returnValue > 0) returnValue *= -1;
3029 }
3030
3031 return returnValue;
3032 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003033
3034 @Override
3035 public void setRadioCapability(RadioAccessFamily[] rafs) {
3036 try {
3037 ProxyController.getInstance().setRadioCapability(rafs);
3038 } catch (RuntimeException e) {
3039 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3040 }
3041 }
3042
3043 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003044 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3045 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3046 return RadioAccessFamily.RAF_UNKNOWN;
3047 }
3048
Wink Saville5d475dd2014-10-17 15:00:58 -07003049 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3050 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003051
3052 @Override
3053 public void enableVideoCalling(boolean enable) {
3054 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003055 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003056 }
3057
3058 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003059 public boolean isVideoCallingEnabled(String callingPackage) {
3060 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3061 return false;
3062 }
3063
Andrew Lee77527ac2014-10-21 16:57:39 -07003064 // Check the user preference and the system-level IMS setting. Even if the user has
3065 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3066 // In the long run, we may instead need to check if there exists a connection service
3067 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003068 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3069 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003070 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003071 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003072
Andrew Leea1239f22015-03-02 17:44:07 -08003073 @Override
3074 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003075 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003076 }
3077
3078 @Override
3079 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003080 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003081 }
3082
Andrew Lee9431b832015-03-09 18:46:45 -07003083 @Override
3084 public boolean isTtyModeSupported() {
3085 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3086 TelephonyManager telephonyManager =
3087 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003088 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003089 }
3090
3091 @Override
3092 public boolean isHearingAidCompatibilitySupported() {
3093 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3094 }
3095
Sanket Padawe7310cc72015-01-14 09:53:20 -08003096 /**
3097 * Returns the unique device ID of phone, for example, the IMEI for
3098 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3099 *
3100 * <p>Requires Permission:
3101 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3102 */
3103 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003104 public String getDeviceId(String callingPackage) {
3105 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3106 return null;
3107 }
3108
Sanket Padawe7310cc72015-01-14 09:53:20 -08003109 final Phone phone = PhoneFactory.getPhone(0);
3110 if (phone != null) {
3111 return phone.getDeviceId();
3112 } else {
3113 return null;
3114 }
3115 }
3116
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003117 /*
3118 * {@hide}
3119 * Returns the IMS Registration Status
3120 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003121 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003122 public boolean isImsRegistered() {
3123 return mPhone.isImsRegistered();
3124 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003125
3126 @Override
3127 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3128 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3129 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003130
Nathan Haroldc55097a2015-03-11 18:14:50 -07003131 /*
3132 * {@hide}
3133 * Returns the IMS Registration Status
3134 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003135 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003136 return mPhone.isWifiCallingEnabled();
3137 }
3138
3139 /*
3140 * {@hide}
3141 * Returns the IMS Registration Status
3142 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003143 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003144 return mPhone.isVolteEnabled();
3145 }
Svet Ganovb320e182015-04-16 12:30:10 -07003146
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003147 /*
3148 * {@hide} Returns the IMS Registration Status
3149 */
3150 public boolean isVideoTelephonyAvailable() {
3151 return mPhone.isVideoEnabled();
3152 }
3153
Svet Ganovb320e182015-04-16 12:30:10 -07003154 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003155 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003156 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003157 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3158
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003159 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003160 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003161 } catch (SecurityException e) {
3162 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3163 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003164 }
Svet Ganovb320e182015-04-16 12:30:10 -07003165
3166 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3167 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3168 return false;
3169 }
3170
3171 return true;
3172 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003173
Makoto Onukifee69342015-06-29 14:44:50 -07003174 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003175 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003176 */
3177 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003178 // Default SMS app can always read it.
3179 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3180 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3181 return true;
3182 }
3183 try {
3184 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003185 } catch (SecurityException readPhoneStateSecurityException) {
3186 try {
3187 // Can be read with READ_SMS too.
3188 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3189 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3190 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3191 } catch (SecurityException readSmsSecurityException) {
3192 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3193 // permissions
3194 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3195 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3196 " or " + android.Manifest.permission.READ_SMS + ".");
3197 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003198 }
Makoto Onukifee69342015-06-29 14:44:50 -07003199 }
3200
Stuart Scott8eef64f2015-04-08 15:13:54 -07003201 @Override
3202 public void factoryReset(int subId) {
3203 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003204 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3205 return;
3206 }
3207
Svet Ganovcc087f82015-05-12 20:35:54 -07003208 final long identity = Binder.clearCallingIdentity();
3209 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003210 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3211 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003212 // Enable data
3213 setDataEnabled(subId, true);
3214 // Set network selection mode to automatic
3215 setNetworkSelectionModeAutomatic(subId);
3216 // Set preferred mobile network type to the best available
3217 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3218 // Turn off roaming
3219 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3220 }
3221 } finally {
3222 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003223 }
3224 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003225
3226 @Override
3227 public String getLocaleFromDefaultSim() {
3228 // We query all subscriptions instead of just the active ones, because
3229 // this might be called early on in the provisioning flow when the
3230 // subscriptions potentially aren't active yet.
3231 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3232 if (slist == null || slist.isEmpty()) {
3233 return null;
3234 }
3235
3236 // This function may be called very early, say, from the setup wizard, at
3237 // which point we won't have a default subscription set. If that's the case
3238 // we just choose the first, which will be valid in "most cases".
3239 final int defaultSubId = getDefaultSubscription();
3240 SubscriptionInfo info = null;
3241 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3242 info = slist.get(0);
3243 } else {
3244 for (SubscriptionInfo item : slist) {
3245 if (item.getSubscriptionId() == defaultSubId) {
3246 info = item;
3247 break;
3248 }
3249 }
3250
3251 if (info == null) {
3252 return null;
3253 }
3254 }
3255
3256 // Try and fetch the locale from the carrier properties or from the SIM language
3257 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003258 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003259 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003260 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003261 if (defaultPhone != null) {
3262 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3263 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003264 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003265 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3266 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003267 } else {
3268 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003269 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003270 }
3271 }
3272
Narayan Kamath011676f2015-07-29 12:04:08 +01003273 // The SIM language preferences only store a language (e.g. fr = French), not an
3274 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3275 // the SIM and carrier preferences does not include a country we add the country
3276 // determined from the SIM MCC to provide an exact locale.
3277 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003278 if (mccLocale != null) {
3279 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3280 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003281 }
3282
Tony Hill183b2de2015-06-24 14:53:58 +01003283 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003284 return null;
3285 }
3286
3287 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3288 final long identity = Binder.clearCallingIdentity();
3289 try {
3290 return mSubscriptionController.getAllSubInfoList(
3291 mPhone.getContext().getOpPackageName());
3292 } finally {
3293 Binder.restoreCallingIdentity(identity);
3294 }
3295 }
3296
3297 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3298 final long identity = Binder.clearCallingIdentity();
3299 try {
3300 return mSubscriptionController.getActiveSubscriptionInfoList(
3301 mPhone.getContext().getOpPackageName());
3302 } finally {
3303 Binder.restoreCallingIdentity(identity);
3304 }
3305 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003306
3307 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003308 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3309 * representing the state of the modem.
3310 *
3311 * NOTE: This clears the modem state, so there should only every be one caller.
3312 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003313 */
3314 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003315 public void requestModemActivityInfo(ResultReceiver result) {
3316 enforceModifyPermission();
3317
3318 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3319 Bundle bundle = new Bundle();
3320 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3321 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003322 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003323
3324 /**
3325 * {@hide}
3326 * Returns the service state information on specified subscription.
3327 */
3328 @Override
3329 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3330
3331 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3332 return null;
3333 }
3334
3335 final Phone phone = getPhone(subId);
3336 if (phone == null) {
3337 return null;
3338 }
3339
3340 return phone.getServiceState();
3341 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003342
3343 /**
3344 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3345 *
3346 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3347 * voicemail ringtone.
3348 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3349 * PhoneAccount.
3350 */
3351 @Override
3352 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3353 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3354 if (phone == null) {
3355 return null;
3356 }
3357
3358 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3359 }
3360
3361 /**
3362 * Returns whether vibration is set for voicemail notification in Phone settings.
3363 *
3364 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3365 * voicemail vibration setting.
3366 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3367 */
3368 @Override
3369 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3370 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3371 if (phone == null) {
3372 return false;
3373 }
3374
3375 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3376 }
3377
Youhan Wange64578a2016-05-02 15:32:42 -07003378 /**
3379 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3380 *
3381 * @throws SecurityException if the caller does not have the required permission
3382 */
3383 private void enforceReadPrivilegedPermission() {
3384 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3385 null);
3386 }
3387
3388 /**
Ta-wei Yen1b455992016-12-27 14:52:32 -08003389 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3390 * permission.
3391 *
3392 * @throws SecurityException if the caller does not have the required permission
3393 */
3394 private void enforceSendSmsPermission() {
3395 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3396 }
3397
3398 /**
3399 * Make sure called from the default dialer
3400 *
3401 * @throws SecurityException if the caller is not the default dialer
3402 */
3403 private void enforceDefaultDialer(String callingPackage) {
3404 TelecomManager telecomManager = mPhone.getContext().getSystemService(TelecomManager.class);
3405 if (!callingPackage.equals(telecomManager.getDefaultDialerPackage())) {
3406 throw new SecurityException("Caller not default dialer.");
3407 }
3408 }
3409
3410 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003411 * Return the application ID for the app type.
3412 *
3413 * @param subId the subscription ID that this request applies to.
3414 * @param appType the uicc app type.
3415 * @return Application ID for specificied app type, or null if no uicc.
3416 */
3417 @Override
3418 public String getAidForAppType(int subId, int appType) {
3419 enforceReadPrivilegedPermission();
3420 Phone phone = getPhone(subId);
3421 if (phone == null) {
3422 return null;
3423 }
3424 String aid = null;
3425 try {
3426 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3427 .getApplicationByType(appType).getAid();
3428 } catch (Exception e) {
3429 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3430 }
3431 return aid;
3432 }
3433
Youhan Wang4001d252016-05-11 10:29:41 -07003434 /**
3435 * Return the Electronic Serial Number.
3436 *
3437 * @param subId the subscription ID that this request applies to.
3438 * @return ESN or null if error.
3439 */
3440 @Override
3441 public String getEsn(int subId) {
3442 enforceReadPrivilegedPermission();
3443 Phone phone = getPhone(subId);
3444 if (phone == null) {
3445 return null;
3446 }
3447 String esn = null;
3448 try {
3449 esn = phone.getEsn();
3450 } catch (Exception e) {
3451 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3452 }
3453 return esn;
3454 }
3455
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003456 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003457 * Return the Preferred Roaming List Version.
3458 *
3459 * @param subId the subscription ID that this request applies to.
3460 * @return PRLVersion or null if error.
3461 */
3462 @Override
3463 public String getCdmaPrlVersion(int subId) {
3464 enforceReadPrivilegedPermission();
3465 Phone phone = getPhone(subId);
3466 if (phone == null) {
3467 return null;
3468 }
3469 String cdmaPrlVersion = null;
3470 try {
3471 cdmaPrlVersion = phone.getCdmaPrlVersion();
3472 } catch (Exception e) {
3473 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3474 }
3475 return cdmaPrlVersion;
3476 }
3477
3478 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003479 * Get snapshot of Telephony histograms
3480 * @return List of Telephony histograms
3481 * @hide
3482 */
3483 @Override
3484 public List<TelephonyHistogram> getTelephonyHistograms() {
3485 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3486 return RIL.getTelephonyRILTimingHistograms();
3487 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003488
3489 /**
3490 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003491 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003492 * Require system privileges. In the future we may add this to carrier APIs.
3493 *
3494 * @return The number of carriers set successfully, should match length of carriers
3495 */
3496 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003497 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003498 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003499 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003500 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3501 return retVal[0];
3502 }
3503
3504 /**
3505 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003506 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003507 * Require system privileges. In the future we may add this to carrier APIs.
3508 *
3509 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3510 * means all carriers are allowed.
3511 */
3512 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003513 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003514 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003515 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003516 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3517 }
3518
fionaxu59545b42016-05-25 15:53:37 -07003519 /**
3520 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3521 * @param subId the subscription ID that this action applies to.
3522 * @param enabled control enable or disable metered apns.
3523 * {@hide}
3524 */
3525 @Override
3526 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3527 enforceModifyPermission();
3528 final Phone phone = getPhone(subId);
3529 if (phone == null) {
3530 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3531 return;
3532 }
3533 try {
3534 phone.carrierActionSetMeteredApnsEnabled(enabled);
3535 } catch (Exception e) {
3536 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3537 }
3538 }
3539
3540 /**
3541 * Action set from carrier signalling broadcast receivers to enable/disable radio
3542 * @param subId the subscription ID that this action applies to.
3543 * @param enabled control enable or disable radio.
3544 * {@hide}
3545 */
3546 @Override
3547 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3548 enforceModifyPermission();
3549 final Phone phone = getPhone(subId);
3550 if (phone == null) {
3551 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3552 return;
3553 }
3554 try {
3555 phone.carrierActionSetRadioEnabled(enabled);
3556 } catch (Exception e) {
3557 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3558 }
3559 }
3560
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003561 /**
3562 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3563 * bug report is being generated.
3564 */
3565 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003566 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003567 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3568 != PackageManager.PERMISSION_GRANTED) {
3569 writer.println("Permission Denial: can't dump Phone from pid="
3570 + Binder.getCallingPid()
3571 + ", uid=" + Binder.getCallingUid()
3572 + "without permission "
3573 + android.Manifest.permission.DUMP);
3574 return;
3575 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003576 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003577 }
Jack Yueb89b242016-06-22 13:27:47 -07003578
3579 /**
3580 * Get aggregated video call data usage from all subscriptions since boot.
3581 * @return total data usage in bytes
3582 * {@hide}
3583 */
3584 @Override
3585 public long getVtDataUsage() {
3586 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3587 null);
3588
3589 // NetworkStatsService keeps tracking the active network interface and identity. It will
3590 // record the delta with the corresponding network identity. What we need to do here is
3591 // returning total video call data usage from all subscriptions since boot.
3592
3593 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3594 // simultaneous VT calls.
3595 final Phone[] phones = PhoneFactory.getPhones();
3596 long total = 0;
3597 for (Phone phone : phones) {
3598 total += phone.getVtDataUsage();
3599 }
3600 return total;
3601 }
Jack Yu75ab2952016-07-08 14:29:33 -07003602
3603 /**
3604 * Policy control of data connection. Usually used when data limit is passed.
3605 * @param enabled True if enabling the data, otherwise disabling.
3606 * @param subId Subscription index
3607 * {@hide}
3608 */
3609 @Override
3610 public void setPolicyDataEnabled(boolean enabled, int subId) {
3611 enforceModifyPermission();
3612 Phone phone = getPhone(subId);
3613 if (phone != null) {
3614 phone.setPolicyDataEnabled(enabled);
3615 }
3616 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003617
3618 /**
3619 * Get Client request stats
3620 * @return List of Client Request Stats
3621 * @hide
3622 */
3623 @Override
3624 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3625 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3626 return null;
3627 }
3628
3629 Phone phone = getPhone(subId);
3630 if (phone != null) {
3631 return phone.getClientRequestStats();
3632 }
3633
3634 return null;
3635 }
3636
3637 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3638 if (workSource != null) {
3639 return workSource;
3640 }
3641
3642 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3643 workSource = new WorkSource(uid, packageName);
3644 return workSource;
3645 }
Jack Yu346de222017-02-16 15:32:43 -08003646
3647 /**
3648 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3649 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003650 * @param slotIndex SIM slot id.
Jack Yu346de222017-02-16 15:32:43 -08003651 * @param powerUp True if powering up the SIM, otherwise powering down
3652 *
3653 **/
3654 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003655 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yu346de222017-02-16 15:32:43 -08003656 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003657 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003658 if (subId == null || subId.length == 0) {
3659 return;
3660 }
3661
3662 final Phone phone = getPhone(subId[0]);
3663 if (phone != null) {
3664 phone.setSimPowerState(powerUp);
3665 }
3666 }
shuoqfef36a32017-01-10 13:02:18 -08003667
3668 /**
3669 * Check if phone is in emergency callback mode
3670 * @return true if phone is in emergency callback mode
3671 * @param subId sub id
3672 */
3673 public boolean getEmergencyCallbackMode(int subId) {
3674 final Phone phone = getPhone(subId);
3675 if (phone != null) {
3676 return phone.isInEcm();
3677 } else {
3678 return false;
3679 }
3680 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003681}