blob: 7e211b6778240ebeec72de7a8dac03089bc97c85 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.ActivityManager;
22import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070023import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.Context;
25import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070026import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080027import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070028import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Looper;
35import android.os.Message;
36import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070037import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.ServiceManager;
39import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070040import android.os.UserManager;
Sooraj Sasindran600a37a2016-07-18 11:57:25 -070041import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070042import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080043import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070044import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080045import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080046import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070047import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070048import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070049import android.telephony.CellInfo;
Sooraj Sasindran600a37a2016-07-18 11:57:25 -070050import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070051import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070052import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080053import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070054import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080056import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080057import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070058import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070059import android.telephony.TelephonyManager;
60import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080062import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080064import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080065import android.util.Slog;
Andrew Lee312e8172014-10-23 17:01:36 -070066import com.android.ims.ImsManager;
Brad Ebinger76681002017-01-23 13:50:20 -080067import com.android.ims.internal.IImsServiceController;
68import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070069import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070070import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070071import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070072import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080074import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010075import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070076import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070078import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070079import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070080import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070081import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070082import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080083import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070084import com.android.internal.telephony.uicc.IccIoResult;
85import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080086import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070087import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080088import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070089import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080090import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070091import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080092import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -070093import java.io.FileDescriptor;
94import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080096import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080097import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010098import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080099import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100
101/**
102 * Implementation of the ITelephony interface.
103 */
Santos Cordon117fee72014-05-16 17:56:12 -0700104public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700105 private static final String LOG_TAG = "PhoneInterfaceManager";
106 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
107 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800108 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109
110 // Message codes used with mMainThreadHandler
111 private static final int CMD_HANDLE_PIN_MMI = 1;
112 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
113 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
114 private static final int CMD_ANSWER_RINGING_CALL = 4;
115 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700116 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
117 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700118 private static final int CMD_OPEN_CHANNEL = 9;
119 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
120 private static final int CMD_CLOSE_CHANNEL = 11;
121 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800122 private static final int CMD_NV_READ_ITEM = 13;
123 private static final int EVENT_NV_READ_ITEM_DONE = 14;
124 private static final int CMD_NV_WRITE_ITEM = 15;
125 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
126 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
127 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
128 private static final int CMD_NV_RESET_CONFIG = 19;
129 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800130 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
131 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
132 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
133 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800134 private static final int CMD_SEND_ENVELOPE = 25;
135 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700136 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
137 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
138 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
139 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
140 private static final int CMD_EXCHANGE_SIM_IO = 31;
141 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800142 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
143 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700144 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
145 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700146 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
147 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700148 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
149 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
150 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
151 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700152 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
153 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
154 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
155 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
Nathan Haroldb3014052017-01-25 15:57:32 -0800156 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
157 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700158
159 /** The singleton instance. */
160 private static PhoneInterfaceManager sInstance;
161
Wink Saville3ab207e2014-11-20 13:07:20 -0800162 private PhoneGlobals mApp;
163 private Phone mPhone;
164 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700165 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800166 private AppOpsManager mAppOps;
167 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800168 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800169 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700170
Derek Tan97ebb422014-09-05 16:55:38 -0700171 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
172 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800173 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700174
175 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700176 * A request object to use for transmitting data to an ICC.
177 */
178 private static final class IccAPDUArgument {
179 public int channel, cla, command, p1, p2, p3;
180 public String data;
181
182 public IccAPDUArgument(int channel, int cla, int command,
183 int p1, int p2, int p3, String data) {
184 this.channel = channel;
185 this.cla = cla;
186 this.command = command;
187 this.p1 = p1;
188 this.p2 = p2;
189 this.p3 = p3;
190 this.data = data;
191 }
192 }
193
194 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700195 * A request object to use for transmitting data to an ICC.
196 */
197 private static final class ManualNetworkSelectionArgument {
198 public OperatorInfo operatorInfo;
199 public boolean persistSelection;
200
201 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
202 this.operatorInfo = operatorInfo;
203 this.persistSelection = persistSelection;
204 }
205 }
206
207 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700208 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
209 * request after sending. The main thread will notify the request when it is complete.
210 */
211 private static final class MainThreadRequest {
212 /** The argument to use for the request */
213 public Object argument;
214 /** The result of the request that is run on the main thread */
215 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800216 // The subscriber id that this request applies to. Defaults to
217 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
218 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700219
220 public MainThreadRequest(Object argument) {
221 this.argument = argument;
222 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800223
224 public MainThreadRequest(Object argument, Integer subId) {
225 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800226 if (subId != null) {
227 this.subId = subId;
228 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800229 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700230 }
231
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800232 private static final class IncomingThirdPartyCallArgs {
233 public final ComponentName component;
234 public final String callId;
235 public final String callerDisplayName;
236
237 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
238 String callerDisplayName) {
239 this.component = component;
240 this.callId = callId;
241 this.callerDisplayName = callerDisplayName;
242 }
243 }
244
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700245 /**
246 * A handler that processes messages on the main thread in the phone process. Since many
247 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
248 * inbound binder threads to the main thread in the phone process. The Binder thread
249 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
250 * on, which will be notified when the operation completes and will contain the result of the
251 * request.
252 *
253 * <p>If a MainThreadRequest object is provided in the msg.obj field,
254 * note that request.result must be set to something non-null for the calling thread to
255 * unblock.
256 */
257 private final class MainThreadHandler extends Handler {
258 @Override
259 public void handleMessage(Message msg) {
260 MainThreadRequest request;
261 Message onCompleted;
262 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800263 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700264 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700265
266 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700267 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700268 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700269 final Phone phone = getPhoneFromRequest(request);
270 request.result = phone != null ?
271 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
272 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273 // Wake up the requesting thread
274 synchronized (request) {
275 request.notifyAll();
276 }
277 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700278 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279
280 case CMD_HANDLE_NEIGHBORING_CELL:
281 request = (MainThreadRequest) msg.obj;
282 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
283 request);
Sooraj Sasindran600a37a2016-07-18 11:57:25 -0700284 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700285 break;
286
287 case EVENT_NEIGHBORING_CELL_DONE:
288 ar = (AsyncResult) msg.obj;
289 request = (MainThreadRequest) ar.userObj;
290 if (ar.exception == null && ar.result != null) {
291 request.result = ar.result;
292 } else {
293 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800294 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700295 }
296 // Wake up the requesting thread
297 synchronized (request) {
298 request.notifyAll();
299 }
300 break;
301
302 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700303 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800304 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700305 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 break;
307
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308 case CMD_END_CALL:
309 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800310 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700311 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700312 Phone phone = getPhone(end_subId);
313 if (phone == null) {
314 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
315 break;
316 }
317 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700318 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
319 // CDMA: If the user presses the Power button we treat it as
320 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700321 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700322 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
323 // GSM: End the call as per the Phone state
324 hungUp = PhoneUtils.hangup(mCM);
325 } else {
326 throw new IllegalStateException("Unexpected phone type: " + phoneType);
327 }
328 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
329 request.result = hungUp;
330 // Wake up the requesting thread
331 synchronized (request) {
332 request.notifyAll();
333 }
334 break;
335
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700336 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700337 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700338 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800339 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700340 if (uiccCard == null) {
341 loge("iccTransmitApduLogicalChannel: No UICC");
342 request.result = new IccIoResult(0x6F, 0, (byte[])null);
343 synchronized (request) {
344 request.notifyAll();
345 }
346 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700347 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
348 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700349 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700350 iccArgument.channel, iccArgument.cla, iccArgument.command,
351 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700352 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700353 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700354 break;
355
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700356 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700357 ar = (AsyncResult) msg.obj;
358 request = (MainThreadRequest) ar.userObj;
359 if (ar.exception == null && ar.result != null) {
360 request.result = ar.result;
361 } else {
362 request.result = new IccIoResult(0x6F, 0, (byte[])null);
363 if (ar.result == null) {
364 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800365 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700366 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800367 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700368 } else {
369 loge("iccTransmitApduLogicalChannel: Unknown exception");
370 }
371 }
372 synchronized (request) {
373 request.notifyAll();
374 }
375 break;
376
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700377 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
378 request = (MainThreadRequest) msg.obj;
379 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800380 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700381 if (uiccCard == null) {
382 loge("iccTransmitApduBasicChannel: No UICC");
383 request.result = new IccIoResult(0x6F, 0, (byte[])null);
384 synchronized (request) {
385 request.notifyAll();
386 }
387 } else {
388 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
389 request);
390 uiccCard.iccTransmitApduBasicChannel(
391 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
392 iccArgument.p3, iccArgument.data, onCompleted);
393 }
394 break;
395
396 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
397 ar = (AsyncResult) msg.obj;
398 request = (MainThreadRequest) ar.userObj;
399 if (ar.exception == null && ar.result != null) {
400 request.result = ar.result;
401 } else {
402 request.result = new IccIoResult(0x6F, 0, (byte[])null);
403 if (ar.result == null) {
404 loge("iccTransmitApduBasicChannel: Empty response");
405 } else if (ar.exception instanceof CommandException) {
406 loge("iccTransmitApduBasicChannel: CommandException: " +
407 ar.exception);
408 } else {
409 loge("iccTransmitApduBasicChannel: Unknown exception");
410 }
411 }
412 synchronized (request) {
413 request.notifyAll();
414 }
415 break;
416
417 case CMD_EXCHANGE_SIM_IO:
418 request = (MainThreadRequest) msg.obj;
419 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800420 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700421 if (uiccCard == null) {
422 loge("iccExchangeSimIO: No UICC");
423 request.result = new IccIoResult(0x6F, 0, (byte[])null);
424 synchronized (request) {
425 request.notifyAll();
426 }
427 } else {
428 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
429 request);
430 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
431 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
432 iccArgument.data, onCompleted);
433 }
434 break;
435
436 case EVENT_EXCHANGE_SIM_IO_DONE:
437 ar = (AsyncResult) msg.obj;
438 request = (MainThreadRequest) ar.userObj;
439 if (ar.exception == null && ar.result != null) {
440 request.result = ar.result;
441 } else {
442 request.result = new IccIoResult(0x6f, 0, (byte[])null);
443 }
444 synchronized (request) {
445 request.notifyAll();
446 }
447 break;
448
Derek Tan4d5e5c12014-02-04 11:54:58 -0800449 case CMD_SEND_ENVELOPE:
450 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800451 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700452 if (uiccCard == null) {
453 loge("sendEnvelopeWithStatus: No UICC");
454 request.result = new IccIoResult(0x6F, 0, (byte[])null);
455 synchronized (request) {
456 request.notifyAll();
457 }
458 } else {
459 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
460 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
461 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800462 break;
463
464 case EVENT_SEND_ENVELOPE_DONE:
465 ar = (AsyncResult) msg.obj;
466 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700467 if (ar.exception == null && ar.result != null) {
468 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800469 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700470 request.result = new IccIoResult(0x6F, 0, (byte[])null);
471 if (ar.result == null) {
472 loge("sendEnvelopeWithStatus: Empty response");
473 } else if (ar.exception instanceof CommandException) {
474 loge("sendEnvelopeWithStatus: CommandException: " +
475 ar.exception);
476 } else {
477 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
478 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800479 }
480 synchronized (request) {
481 request.notifyAll();
482 }
483 break;
484
Shishir Agrawal566b7612013-10-28 14:41:00 -0700485 case CMD_OPEN_CHANNEL:
486 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800487 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700488 if (uiccCard == null) {
489 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800490 request.result = new IccOpenLogicalChannelResponse(-1,
491 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700492 synchronized (request) {
493 request.notifyAll();
494 }
495 } else {
496 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
497 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
498 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700499 break;
500
501 case EVENT_OPEN_CHANNEL_DONE:
502 ar = (AsyncResult) msg.obj;
503 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700504 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700505 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700506 int[] result = (int[]) ar.result;
507 int channelId = result[0];
508 byte[] selectResponse = null;
509 if (result.length > 1) {
510 selectResponse = new byte[result.length - 1];
511 for (int i = 1; i < result.length; ++i) {
512 selectResponse[i - 1] = (byte) result[i];
513 }
514 }
515 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700516 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700517 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700518 if (ar.result == null) {
519 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700520 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700521 if (ar.exception != null) {
522 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
523 }
524
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700525 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700526 if (ar.exception instanceof CommandException) {
527 CommandException.Error error =
528 ((CommandException) (ar.exception)).getCommandError();
529 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700530 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700531 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700532 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700533 }
534 }
535 openChannelResp = new IccOpenLogicalChannelResponse(
536 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700537 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700538 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700539 synchronized (request) {
540 request.notifyAll();
541 }
542 break;
543
544 case CMD_CLOSE_CHANNEL:
545 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800546 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700547 if (uiccCard == null) {
548 loge("iccCloseLogicalChannel: No UICC");
549 request.result = new IccIoResult(0x6F, 0, (byte[])null);
550 synchronized (request) {
551 request.notifyAll();
552 }
553 } else {
554 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
555 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
556 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700557 break;
558
559 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800560 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
561 break;
562
563 case CMD_NV_READ_ITEM:
564 request = (MainThreadRequest) msg.obj;
565 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
566 mPhone.nvReadItem((Integer) request.argument, onCompleted);
567 break;
568
569 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700570 ar = (AsyncResult) msg.obj;
571 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800572 if (ar.exception == null && ar.result != null) {
573 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700574 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800575 request.result = "";
576 if (ar.result == null) {
577 loge("nvReadItem: Empty response");
578 } else if (ar.exception instanceof CommandException) {
579 loge("nvReadItem: CommandException: " +
580 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700581 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800582 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700583 }
584 }
585 synchronized (request) {
586 request.notifyAll();
587 }
588 break;
589
Jake Hambye994d462014-02-03 13:10:13 -0800590 case CMD_NV_WRITE_ITEM:
591 request = (MainThreadRequest) msg.obj;
592 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
593 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
594 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
595 break;
596
597 case EVENT_NV_WRITE_ITEM_DONE:
598 handleNullReturnEvent(msg, "nvWriteItem");
599 break;
600
601 case CMD_NV_WRITE_CDMA_PRL:
602 request = (MainThreadRequest) msg.obj;
603 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
604 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
605 break;
606
607 case EVENT_NV_WRITE_CDMA_PRL_DONE:
608 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
609 break;
610
611 case CMD_NV_RESET_CONFIG:
612 request = (MainThreadRequest) msg.obj;
613 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
614 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
615 break;
616
617 case EVENT_NV_RESET_CONFIG_DONE:
618 handleNullReturnEvent(msg, "nvResetConfig");
619 break;
620
Jake Hamby7c27be32014-03-03 13:25:59 -0800621 case CMD_GET_PREFERRED_NETWORK_TYPE:
622 request = (MainThreadRequest) msg.obj;
623 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700624 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800625 break;
626
627 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
628 ar = (AsyncResult) msg.obj;
629 request = (MainThreadRequest) ar.userObj;
630 if (ar.exception == null && ar.result != null) {
631 request.result = ar.result; // Integer
632 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800633 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800634 if (ar.result == null) {
635 loge("getPreferredNetworkType: Empty response");
636 } else if (ar.exception instanceof CommandException) {
637 loge("getPreferredNetworkType: CommandException: " +
638 ar.exception);
639 } else {
640 loge("getPreferredNetworkType: Unknown exception");
641 }
642 }
643 synchronized (request) {
644 request.notifyAll();
645 }
646 break;
647
648 case CMD_SET_PREFERRED_NETWORK_TYPE:
649 request = (MainThreadRequest) msg.obj;
650 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
651 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700652 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800653 break;
654
655 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
656 handleNullReturnEvent(msg, "setPreferredNetworkType");
657 break;
658
Steven Liu4bf01bc2014-07-17 11:05:29 -0500659 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
660 request = (MainThreadRequest)msg.obj;
661 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
662 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
663 break;
664
665 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
666 ar = (AsyncResult)msg.obj;
667 request = (MainThreadRequest)ar.userObj;
668 request.result = ar;
669 synchronized (request) {
670 request.notifyAll();
671 }
672 break;
673
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800674 case CMD_SET_VOICEMAIL_NUMBER:
675 request = (MainThreadRequest) msg.obj;
676 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
677 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800678 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
679 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800680 break;
681
682 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
683 handleNullReturnEvent(msg, "setVoicemailNumber");
684 break;
685
Stuart Scott54788802015-03-30 13:18:01 -0700686 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
687 request = (MainThreadRequest) msg.obj;
688 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
689 request);
690 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
691 break;
692
693 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
694 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
695 break;
696
Shishir Agrawal302c8692015-06-19 13:49:39 -0700697 case CMD_PERFORM_NETWORK_SCAN:
698 request = (MainThreadRequest) msg.obj;
699 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
700 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
701 break;
702
703 case EVENT_PERFORM_NETWORK_SCAN_DONE:
704 ar = (AsyncResult) msg.obj;
705 request = (MainThreadRequest) ar.userObj;
706 CellNetworkScanResult cellScanResult;
707 if (ar.exception == null && ar.result != null) {
708 cellScanResult = new CellNetworkScanResult(
709 CellNetworkScanResult.STATUS_SUCCESS,
710 (List<OperatorInfo>) ar.result);
711 } else {
712 if (ar.result == null) {
713 loge("getCellNetworkScanResults: Empty response");
714 }
715 if (ar.exception != null) {
716 loge("getCellNetworkScanResults: Exception: " + ar.exception);
717 }
718 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
719 if (ar.exception instanceof CommandException) {
720 CommandException.Error error =
721 ((CommandException) (ar.exception)).getCommandError();
722 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
723 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
724 } else if (error == CommandException.Error.GENERIC_FAILURE) {
725 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
726 }
727 }
728 cellScanResult = new CellNetworkScanResult(errorCode, null);
729 }
730 request.result = cellScanResult;
731 synchronized (request) {
732 request.notifyAll();
733 }
734 break;
735
736 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
737 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700738 ManualNetworkSelectionArgument selArg =
739 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700740 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
741 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700742 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
743 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700744 break;
745
746 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
747 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
748 break;
749
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700750 case CMD_GET_MODEM_ACTIVITY_INFO:
751 request = (MainThreadRequest) msg.obj;
752 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700753 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700754 break;
755
756 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
757 ar = (AsyncResult) msg.obj;
758 request = (MainThreadRequest) ar.userObj;
759 if (ar.exception == null && ar.result != null) {
760 request.result = ar.result;
761 } else {
762 if (ar.result == null) {
763 loge("queryModemActivityInfo: Empty response");
764 } else if (ar.exception instanceof CommandException) {
765 loge("queryModemActivityInfo: CommandException: " +
766 ar.exception);
767 } else {
768 loge("queryModemActivityInfo: Unknown exception");
769 }
770 }
Amit Mahajand4766222016-01-28 15:28:28 -0800771 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
772 if (request.result == null) {
773 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
774 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700775 synchronized (request) {
776 request.notifyAll();
777 }
778 break;
779
Meng Wang1a7c35a2016-05-05 20:56:15 -0700780 case CMD_SET_ALLOWED_CARRIERS:
781 request = (MainThreadRequest) msg.obj;
782 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
783 mPhone.setAllowedCarriers(
784 (List<CarrierIdentifier>) request.argument,
785 onCompleted);
786 break;
787
788 case EVENT_SET_ALLOWED_CARRIERS_DONE:
789 ar = (AsyncResult) msg.obj;
790 request = (MainThreadRequest) ar.userObj;
791 if (ar.exception == null && ar.result != null) {
792 request.result = ar.result;
793 } else {
794 if (ar.result == null) {
795 loge("setAllowedCarriers: Empty response");
796 } else if (ar.exception instanceof CommandException) {
797 loge("setAllowedCarriers: CommandException: " +
798 ar.exception);
799 } else {
800 loge("setAllowedCarriers: Unknown exception");
801 }
802 }
803 // Result cannot be null. Return -1 on error.
804 if (request.result == null) {
805 request.result = new int[]{-1};
806 }
807 synchronized (request) {
808 request.notifyAll();
809 }
810 break;
811
812 case CMD_GET_ALLOWED_CARRIERS:
813 request = (MainThreadRequest) msg.obj;
814 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
815 mPhone.getAllowedCarriers(onCompleted);
816 break;
817
818 case EVENT_GET_ALLOWED_CARRIERS_DONE:
819 ar = (AsyncResult) msg.obj;
820 request = (MainThreadRequest) ar.userObj;
821 if (ar.exception == null && ar.result != null) {
822 request.result = ar.result;
823 } else {
824 if (ar.result == null) {
825 loge("getAllowedCarriers: Empty response");
826 } else if (ar.exception instanceof CommandException) {
827 loge("getAllowedCarriers: CommandException: " +
828 ar.exception);
829 } else {
830 loge("getAllowedCarriers: Unknown exception");
831 }
832 }
833 // Result cannot be null. Return empty list of CarrierIdentifier.
834 if (request.result == null) {
835 request.result = new ArrayList<CarrierIdentifier>(0);
836 }
837 synchronized (request) {
838 request.notifyAll();
839 }
840 break;
841
Nathan Haroldb3014052017-01-25 15:57:32 -0800842 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
843 ar = (AsyncResult) msg.obj;
844 request = (MainThreadRequest) ar.userObj;
845 if (ar.exception == null && ar.result != null) {
846 request.result = ar.result;
847 } else {
848 request.result = new IllegalArgumentException(
849 "Failed to retrieve Forbidden Plmns");
850 if (ar.result == null) {
851 loge("getForbiddenPlmns: Empty response");
852 } else {
853 loge("getForbiddenPlmns: Unknown exception");
854 }
855 }
856 synchronized (request) {
857 request.notifyAll();
858 }
859 break;
860
861 case CMD_GET_FORBIDDEN_PLMNS:
862 request = (MainThreadRequest) msg.obj;
863 uiccCard = getUiccCardFromRequest(request);
864 if (uiccCard == null) {
865 loge("getForbiddenPlmns() UiccCard is null");
866 request.result = new IllegalArgumentException(
867 "getForbiddenPlmns() UiccCard is null");
868 synchronized (request) {
869 request.notifyAll();
870 }
871 break;
872 }
873 Integer appType = (Integer) request.argument;
874 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
875 if (uiccApp == null) {
876 loge("getForbiddenPlmns() no app with specified type -- "
877 + appType);
878 request.result = new IllegalArgumentException("Failed to get UICC App");
879 synchronized (request) {
880 request.notifyAll();
881 }
882 break;
883 } else {
884 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
885 + " specified type -- " + appType);
886 }
887 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
888 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
889 onCompleted);
890 break;
891
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700892 default:
893 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
894 break;
895 }
896 }
Jake Hambye994d462014-02-03 13:10:13 -0800897
898 private void handleNullReturnEvent(Message msg, String command) {
899 AsyncResult ar = (AsyncResult) msg.obj;
900 MainThreadRequest request = (MainThreadRequest) ar.userObj;
901 if (ar.exception == null) {
902 request.result = true;
903 } else {
904 request.result = false;
905 if (ar.exception instanceof CommandException) {
906 loge(command + ": CommandException: " + ar.exception);
907 } else {
908 loge(command + ": Unknown exception");
909 }
910 }
911 synchronized (request) {
912 request.notifyAll();
913 }
914 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700915 }
916
917 /**
918 * Posts the specified command to be executed on the main thread,
919 * waits for the request to complete, and returns the result.
920 * @see #sendRequestAsync
921 */
922 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800923 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -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 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800931 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700932 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
933 throw new RuntimeException("This method will deadlock if called from the main thread.");
934 }
935
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800936 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 Message msg = mMainThreadHandler.obtainMessage(command, request);
938 msg.sendToTarget();
939
940 // Wait for the request to complete
941 synchronized (request) {
942 while (request.result == null) {
943 try {
944 request.wait();
945 } catch (InterruptedException e) {
946 // Do nothing, go back and wait until the request is complete
947 }
948 }
949 }
950 return request.result;
951 }
952
953 /**
954 * Asynchronous ("fire and forget") version of sendRequest():
955 * Posts the specified command to be executed on the main thread, and
956 * returns immediately.
957 * @see #sendRequest
958 */
959 private void sendRequestAsync(int command) {
960 mMainThreadHandler.sendEmptyMessage(command);
961 }
962
963 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700964 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
965 * @see {@link #sendRequest(int,Object)}
966 */
967 private void sendRequestAsync(int command, Object argument) {
968 MainThreadRequest request = new MainThreadRequest(argument);
969 Message msg = mMainThreadHandler.obtainMessage(command, request);
970 msg.sendToTarget();
971 }
972
973 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700974 * Initialize the singleton PhoneInterfaceManager instance.
975 * This is only done once, at startup, from PhoneApp.onCreate().
976 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700977 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700978 synchronized (PhoneInterfaceManager.class) {
979 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700980 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700981 } else {
982 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
983 }
984 return sInstance;
985 }
986 }
987
988 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700989 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 mApp = app;
991 mPhone = phone;
992 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700993 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700994 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
995 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700996 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700997 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800998 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800999
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 publish();
1001 }
1002
1003 private void publish() {
1004 if (DBG) log("publish: " + this);
1005
1006 ServiceManager.addService("phone", this);
1007 }
1008
Stuart Scott584921c2015-01-15 17:10:34 -08001009 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001010 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1011 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001012 }
1013
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001014 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1015 Phone phone = getPhoneFromRequest(request);
1016 return phone == null ? null :
1017 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1018 }
1019
Wink Saville36469e72014-06-11 15:17:00 -07001020 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001021 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001022 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001023 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001024 //
1025 // Implementation of the ITelephony interface.
1026 //
1027
1028 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001029 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001030 }
1031
Wink Savilleb564aae2014-10-23 10:18:09 -07001032 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 if (DBG) log("dial: " + number);
1034 // No permission check needed here: This is just a wrapper around the
1035 // ACTION_DIAL intent, which is available to any app since it puts up
1036 // the UI before it does anything.
1037
1038 String url = createTelUrl(number);
1039 if (url == null) {
1040 return;
1041 }
1042
1043 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001044 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001045 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1046 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1047 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1048 mApp.startActivity(intent);
1049 }
1050 }
1051
1052 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001053 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001054 }
1055
Wink Savilleb564aae2014-10-23 10:18:09 -07001056 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001057 if (DBG) log("call: " + number);
1058
1059 // This is just a wrapper around the ACTION_CALL intent, but we still
1060 // need to do a permission check since we're calling startActivity()
1061 // from the context of the phone app.
1062 enforceCallPermission();
1063
1064 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1065 != AppOpsManager.MODE_ALLOWED) {
1066 return;
1067 }
1068
1069 String url = createTelUrl(number);
1070 if (url == null) {
1071 return;
1072 }
1073
Wink Saville08874612014-08-31 19:19:58 -07001074 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001075 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001076 if (slist != null) {
1077 for (SubscriptionInfo subInfoRecord : slist) {
1078 if (subInfoRecord.getSubscriptionId() == subId) {
1079 isValid = true;
1080 break;
1081 }
Wink Saville08874612014-08-31 19:19:58 -07001082 }
1083 }
1084 if (isValid == false) {
1085 return;
1086 }
1087
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001088 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001089 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001090 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1091 mApp.startActivity(intent);
1092 }
1093
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001094 /**
1095 * End a call based on call state
1096 * @return true is a call was ended
1097 */
1098 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001099 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001100 }
1101
1102 /**
1103 * End a call based on the call state of the subId
1104 * @return true is a call was ended
1105 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001106 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001107 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001108 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 }
1110
1111 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001112 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001113 }
1114
Wink Savilleb564aae2014-10-23 10:18:09 -07001115 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001116 if (DBG) log("answerRingingCall...");
1117 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1118 // but that can probably wait till the big TelephonyManager API overhaul.
1119 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1120 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001121 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 }
1123
1124 /**
1125 * Make the actual telephony calls to implement answerRingingCall().
1126 * This should only be called from the main thread of the Phone app.
1127 * @see #answerRingingCall
1128 *
1129 * TODO: it would be nice to return true if we answered the call, or
1130 * false if there wasn't actually a ringing incoming call, or some
1131 * other error occurred. (In other words, pass back the return value
1132 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1133 * But that would require calling this method via sendRequest() rather
1134 * than sendRequestAsync(), and right now we don't actually *need* that
1135 * return value, so let's just return void for now.
1136 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001137 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001138 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001139 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001140 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1141 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001142 if (hasActiveCall && hasHoldingCall) {
1143 // Both lines are in use!
1144 // TODO: provide a flag to let the caller specify what
1145 // policy to use if both lines are in use. (The current
1146 // behavior is hardwired to "answer incoming, end ongoing",
1147 // which is how the CALL button is specced to behave.)
1148 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1149 return;
1150 } else {
1151 // answerCall() will automatically hold the current active
1152 // call, if there is one.
1153 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1154 return;
1155 }
1156 } else {
1157 // No call was ringing.
1158 return;
1159 }
1160 }
1161
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001162 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001163 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001164 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001165 public void silenceRinger() {
1166 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167 }
1168
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001169 @Override
1170 public boolean isOffhook(String callingPackage) {
1171 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001172 }
1173
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001174 @Override
1175 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1176 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1177 return false;
1178 }
1179
Sanket Padawe356d7632015-06-22 14:03:32 -07001180 final Phone phone = getPhone(subId);
1181 if (phone != null) {
1182 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1183 } else {
1184 return false;
1185 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001186 }
1187
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001188 @Override
1189 public boolean isRinging(String callingPackage) {
1190 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001191 }
1192
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001193 @Override
1194 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1195 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1196 return false;
1197 }
1198
Sanket Padawe356d7632015-06-22 14:03:32 -07001199 final Phone phone = getPhone(subId);
1200 if (phone != null) {
1201 return (phone.getState() == PhoneConstants.State.RINGING);
1202 } else {
1203 return false;
1204 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 }
1206
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001207 @Override
1208 public boolean isIdle(String callingPackage) {
1209 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001210 }
1211
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001212 @Override
1213 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1214 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1215 return false;
1216 }
1217
Sanket Padawe356d7632015-06-22 14:03:32 -07001218 final Phone phone = getPhone(subId);
1219 if (phone != null) {
1220 return (phone.getState() == PhoneConstants.State.IDLE);
1221 } else {
1222 return false;
1223 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001224 }
1225
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001226 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001227 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001228 }
1229
Wink Savilleb564aae2014-10-23 10:18:09 -07001230 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001231 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001232 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1233 }
1234
1235 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001236 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001237 }
1238
Wink Savilleb564aae2014-10-23 10:18:09 -07001239 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001240 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001241 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1242 }
1243
1244 /** {@hide} */
1245 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001246 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001247 }
1248
Wink Savilleb564aae2014-10-23 10:18:09 -07001249 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001250 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001251 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001252 checkSimPin.start();
1253 return checkSimPin.unlockSim(null, pin);
1254 }
1255
Wink Saville9de0f752013-10-22 19:04:03 -07001256 /** {@hide} */
1257 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001258 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001259 }
1260
Wink Savilleb564aae2014-10-23 10:18:09 -07001261 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001262 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001263 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001264 checkSimPuk.start();
1265 return checkSimPuk.unlockSim(puk, pin);
1266 }
1267
1268 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001269 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001270 * a synchronous one.
1271 */
1272 private static class UnlockSim extends Thread {
1273
1274 private final IccCard mSimCard;
1275
1276 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001277 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1278 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001279
1280 // For replies from SimCard interface
1281 private Handler mHandler;
1282
1283 // For async handler to identify request type
1284 private static final int SUPPLY_PIN_COMPLETE = 100;
1285
1286 public UnlockSim(IccCard simCard) {
1287 mSimCard = simCard;
1288 }
1289
1290 @Override
1291 public void run() {
1292 Looper.prepare();
1293 synchronized (UnlockSim.this) {
1294 mHandler = new Handler() {
1295 @Override
1296 public void handleMessage(Message msg) {
1297 AsyncResult ar = (AsyncResult) msg.obj;
1298 switch (msg.what) {
1299 case SUPPLY_PIN_COMPLETE:
1300 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1301 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001302 mRetryCount = msg.arg1;
1303 if (ar.exception != null) {
1304 if (ar.exception instanceof CommandException &&
1305 ((CommandException)(ar.exception)).getCommandError()
1306 == CommandException.Error.PASSWORD_INCORRECT) {
1307 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1308 } else {
1309 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1310 }
1311 } else {
1312 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1313 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001314 mDone = true;
1315 UnlockSim.this.notifyAll();
1316 }
1317 break;
1318 }
1319 }
1320 };
1321 UnlockSim.this.notifyAll();
1322 }
1323 Looper.loop();
1324 }
1325
1326 /*
1327 * Use PIN or PUK to unlock SIM card
1328 *
1329 * If PUK is null, unlock SIM card with PIN
1330 *
1331 * If PUK is not null, unlock SIM card with PUK and set PIN code
1332 */
Wink Saville9de0f752013-10-22 19:04:03 -07001333 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001334
1335 while (mHandler == null) {
1336 try {
1337 wait();
1338 } catch (InterruptedException e) {
1339 Thread.currentThread().interrupt();
1340 }
1341 }
1342 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1343
1344 if (puk == null) {
1345 mSimCard.supplyPin(pin, callback);
1346 } else {
1347 mSimCard.supplyPuk(puk, pin, callback);
1348 }
1349
1350 while (!mDone) {
1351 try {
1352 Log.d(LOG_TAG, "wait for done");
1353 wait();
1354 } catch (InterruptedException e) {
1355 // Restore the interrupted status
1356 Thread.currentThread().interrupt();
1357 }
1358 }
1359 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001360 int[] resultArray = new int[2];
1361 resultArray[0] = mResult;
1362 resultArray[1] = mRetryCount;
1363 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001364 }
1365 }
1366
1367 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001368 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001369
1370 }
1371
Wink Savilleb564aae2014-10-23 10:18:09 -07001372 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 // No permission check needed here: this call is harmless, and it's
1374 // needed for the ServiceState.requestStateUpdate() call (which is
1375 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001376 final Phone phone = getPhone(subId);
1377 if (phone != null) {
1378 phone.updateServiceLocation();
1379 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001380 }
1381
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001382 @Override
1383 public boolean isRadioOn(String callingPackage) {
1384 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001385 }
1386
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001387 @Override
1388 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1389 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1390 return false;
1391 }
1392 return isRadioOnForSubscriber(subId);
1393 }
1394
1395 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001396 final Phone phone = getPhone(subId);
1397 if (phone != null) {
1398 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1399 } else {
1400 return false;
1401 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 }
1403
1404 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001405 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001406
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001407 }
Wink Saville36469e72014-06-11 15:17:00 -07001408
Wink Savilleb564aae2014-10-23 10:18:09 -07001409 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001410 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001411 final Phone phone = getPhone(subId);
1412 if (phone != null) {
1413 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1414 }
Wink Saville36469e72014-06-11 15:17:00 -07001415 }
1416
1417 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001418 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001419 }
1420
Wink Savilleb564aae2014-10-23 10:18:09 -07001421 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001422 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001423 final Phone phone = getPhone(subId);
1424 if (phone == null) {
1425 return false;
1426 }
1427 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001428 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001429 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001430 }
1431 return true;
1432 }
Wink Saville36469e72014-06-11 15:17:00 -07001433
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001434 public boolean needMobileRadioShutdown() {
1435 /*
1436 * If any of the Radios are available, it will need to be
1437 * shutdown. So return true if any Radio is available.
1438 */
1439 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1440 Phone phone = PhoneFactory.getPhone(i);
1441 if (phone != null && phone.isRadioAvailable()) return true;
1442 }
1443 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1444 return false;
1445 }
1446
1447 public void shutdownMobileRadios() {
1448 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1449 logv("Shutting down Phone " + i);
1450 shutdownRadioUsingPhoneId(i);
1451 }
1452 }
1453
1454 private void shutdownRadioUsingPhoneId(int phoneId) {
1455 enforceModifyPermission();
1456 Phone phone = PhoneFactory.getPhone(phoneId);
1457 if (phone != null && phone.isRadioAvailable()) {
1458 phone.shutdownRadio();
1459 }
1460 }
1461
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001462 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001463 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1464 if (defaultPhone != null) {
1465 defaultPhone.setRadioPower(turnOn);
1466 return true;
1467 } else {
1468 loge("There's no default phone.");
1469 return false;
1470 }
Wink Saville36469e72014-06-11 15:17:00 -07001471 }
1472
Wink Savilleb564aae2014-10-23 10:18:09 -07001473 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001474 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001475 final Phone phone = getPhone(subId);
1476 if (phone != null) {
1477 phone.setRadioPower(turnOn);
1478 return true;
1479 } else {
1480 return false;
1481 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 }
1483
Wink Saville36469e72014-06-11 15:17:00 -07001484 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001485 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001486 public boolean enableDataConnectivity() {
1487 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001488 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001489 final Phone phone = getPhone(subId);
1490 if (phone != null) {
1491 phone.setDataEnabled(true);
1492 return true;
1493 } else {
1494 return false;
1495 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001496 }
1497
Wink Saville36469e72014-06-11 15:17:00 -07001498 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001499 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 public boolean disableDataConnectivity() {
1501 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001502 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001503 final Phone phone = getPhone(subId);
1504 if (phone != null) {
1505 phone.setDataEnabled(false);
1506 return true;
1507 } else {
1508 return false;
1509 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001510 }
1511
Wink Saville36469e72014-06-11 15:17:00 -07001512 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001513 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001514 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001515 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001516 final Phone phone = getPhone(subId);
1517 if (phone != null) {
1518 return phone.isDataConnectivityPossible();
1519 } else {
1520 return false;
1521 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 }
1523
1524 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001525 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001526 }
1527
Wink Savilleb564aae2014-10-23 10:18:09 -07001528 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001529 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001530 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1531 return false;
1532 }
Wink Saville36469e72014-06-11 15:17:00 -07001533 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001534 }
1535
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001537 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001538 }
1539
Sanket Padawe5780e442017-03-20 15:04:47 -07001540 public int getCallStateForSlot(int slotIndex) {
1541 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001542 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1543 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001544 }
1545
Sanket Padawe356d7632015-06-22 14:03:32 -07001546 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001547 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001548 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001549 if (phone != null) {
1550 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1551 } else {
1552 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1553 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001554 }
1555
Sanket Padawe356d7632015-06-22 14:03:32 -07001556 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001557 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001558 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001559 if (phone != null) {
1560 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1561 } else {
1562 return TelephonyManager.DATA_ACTIVITY_NONE;
1563 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001564 }
1565
1566 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001567 public Bundle getCellLocation(String callingPackage) {
1568 enforceFineOrCoarseLocationPermission("getCellLocation");
1569
1570 // OP_COARSE_LOCATION controls both fine and coarse location.
1571 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1572 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001573 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001574 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001575 }
1576
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001577 if (checkIfCallerIsSelfOrForegroundUser() ||
1578 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001579 if (DBG_LOC) log("getCellLocation: is active user");
1580 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001581 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001582 if (phone == null) {
1583 return null;
1584 }
Sooraj Sasindran600a37a2016-07-18 11:57:25 -07001585
1586 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1587 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001588 return data;
1589 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001590 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001591 return null;
1592 }
1593 }
1594
Svetoslav64fad262015-04-14 14:35:21 -07001595 private void enforceFineOrCoarseLocationPermission(String message) {
1596 try {
1597 mApp.enforceCallingOrSelfPermission(
1598 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1599 } catch (SecurityException e) {
1600 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1601 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1602 // is the weaker precondition
1603 mApp.enforceCallingOrSelfPermission(
1604 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1605 }
1606 }
1607
1608
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001609 @Override
1610 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001611 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001612 }
1613
Sanket Padawe356d7632015-06-22 14:03:32 -07001614 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001615 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001616 mApp.enforceCallingOrSelfPermission(
1617 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001618 final Phone phone = getPhone(subId);
1619 if (phone != null) {
1620 phone.enableLocationUpdates();
1621 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001622 }
1623
1624 @Override
1625 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001626 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001627 }
1628
Sanket Padawe356d7632015-06-22 14:03:32 -07001629 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001630 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001631 mApp.enforceCallingOrSelfPermission(
1632 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001633 final Phone phone = getPhone(subId);
1634 if (phone != null) {
1635 phone.disableLocationUpdates();
1636 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001637 }
1638
1639 @Override
1640 @SuppressWarnings("unchecked")
1641 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001642 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1643
1644 // OP_COARSE_LOCATION controls both fine and coarse location.
1645 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1646 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1647 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001648 }
1649
1650 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1651 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1652 return null;
1653 }
Svetoslav64fad262015-04-14 14:35:21 -07001654
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001655 if (checkIfCallerIsSelfOrForegroundUser() ||
1656 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1658
1659 ArrayList<NeighboringCellInfo> cells = null;
1660
Sooraj Sasindran600a37a2016-07-18 11:57:25 -07001661 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001662 try {
1663 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran600a37a2016-07-18 11:57:25 -07001664 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001665 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001666 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001667 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001668 }
1669 return cells;
1670 } else {
1671 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1672 return null;
1673 }
1674 }
1675
1676
1677 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001678 public List<CellInfo> getAllCellInfo(String callingPackage) {
1679 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1680
1681 // OP_COARSE_LOCATION controls both fine and coarse location.
1682 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1683 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1684 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001685 }
1686
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001687 if (checkIfCallerIsSelfOrForegroundUser() ||
1688 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001689 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran600a37a2016-07-18 11:57:25 -07001690 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001691 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1692 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran600a37a2016-07-18 11:57:25 -07001693 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1694 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001695 }
1696 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001697 } else {
1698 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1699 return null;
1700 }
1701 }
1702
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001703 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001704 public void setCellInfoListRate(int rateInMillis) {
Sooraj Sasindran600a37a2016-07-18 11:57:25 -07001705 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1706 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001707 }
1708
Shishir Agrawala9f32182016-04-12 12:00:16 -07001709 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001710 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001711 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1712 return null;
1713 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001714 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001715 return phone == null ? null : phone.getImei();
1716 }
1717
1718 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001719 public String getMeidForSlot(int slotIndex, String callingPackage) {
1720 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1721 return null;
1722 }
1723 Phone phone = PhoneFactory.getPhone(slotIndex);
1724 return phone == null ? null : phone.getMeid();
1725 }
1726
1727 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001728 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001729 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1730 return null;
1731 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001732 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001733 return phone == null ? null : phone.getDeviceSvn();
1734 }
1735
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001736 //
1737 // Internal helper methods.
1738 //
1739
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001740 /**
1741 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1742 */
1743 private boolean checkCallerInteractAcrossUsersFull() {
1744 return mPhone.getContext().checkCallingOrSelfPermission(
1745 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1746 == PackageManager.PERMISSION_GRANTED;
1747 }
1748
Jake Hambye994d462014-02-03 13:10:13 -08001749 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001750 boolean ok;
1751
1752 boolean self = Binder.getCallingUid() == Process.myUid();
1753 if (!self) {
1754 // Get the caller's user id then clear the calling identity
1755 // which will be restored in the finally clause.
1756 int callingUser = UserHandle.getCallingUserId();
1757 long ident = Binder.clearCallingIdentity();
1758
1759 try {
1760 // With calling identity cleared the current user is the foreground user.
1761 int foregroundUser = ActivityManager.getCurrentUser();
1762 ok = (foregroundUser == callingUser);
1763 if (DBG_LOC) {
1764 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1765 + " callingUser=" + callingUser + " ok=" + ok);
1766 }
1767 } catch (Exception ex) {
1768 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1769 ok = false;
1770 } finally {
1771 Binder.restoreCallingIdentity(ident);
1772 }
1773 } else {
1774 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1775 ok = true;
1776 }
1777 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1778 return ok;
1779 }
1780
1781 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001782 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1783 *
1784 * @throws SecurityException if the caller does not have the required permission
1785 */
1786 private void enforceModifyPermission() {
1787 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1788 }
1789
1790 /**
Junda Liua2e36012014-07-09 18:30:01 -07001791 * Make sure either system app or the caller has carrier privilege.
1792 *
1793 * @throws SecurityException if the caller does not have the required permission/privilege
1794 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001795 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001796 int permission = mApp.checkCallingOrSelfPermission(
1797 android.Manifest.permission.MODIFY_PHONE_STATE);
1798 if (permission == PackageManager.PERMISSION_GRANTED) {
1799 return;
1800 }
1801
1802 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001803 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001804 }
1805
1806 /**
1807 * Make sure the caller has carrier privilege.
1808 *
1809 * @throws SecurityException if the caller does not have the required permission
1810 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001811 private void enforceCarrierPrivilege(int subId) {
1812 if (getCarrierPrivilegeStatus(subId) !=
1813 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001814 loge("No Carrier Privilege.");
1815 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001816 }
1817 }
1818
1819 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001820 * Make sure the caller has the CALL_PHONE permission.
1821 *
1822 * @throws SecurityException if the caller does not have the required permission
1823 */
1824 private void enforceCallPermission() {
1825 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1826 }
1827
Stuart Scott8eef64f2015-04-08 15:13:54 -07001828 private void enforceConnectivityInternalPermission() {
1829 mApp.enforceCallingOrSelfPermission(
1830 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1831 "ConnectivityService");
1832 }
1833
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001834 private String createTelUrl(String number) {
1835 if (TextUtils.isEmpty(number)) {
1836 return null;
1837 }
1838
Jake Hambye994d462014-02-03 13:10:13 -08001839 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001840 }
1841
Ihab Awadf9e92732013-12-05 18:02:52 -08001842 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001843 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1844 }
1845
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001846 private static void logv(String msg) {
1847 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1848 }
1849
Ihab Awadf9e92732013-12-05 18:02:52 -08001850 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001851 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1852 }
1853
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001854 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001855 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001856 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001857 }
1858
Sanket Padawe356d7632015-06-22 14:03:32 -07001859 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001860 public int getActivePhoneTypeForSlot(int slotIndex) {
1861 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001862 if (phone == null) {
1863 return PhoneConstants.PHONE_TYPE_NONE;
1864 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001865 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001866 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001867 }
1868
1869 /**
1870 * Returns the CDMA ERI icon index to display
1871 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001872 @Override
1873 public int getCdmaEriIconIndex(String callingPackage) {
1874 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001875 }
1876
Sanket Padawe356d7632015-06-22 14:03:32 -07001877 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001878 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1879 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1880 return -1;
1881 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001882 final Phone phone = getPhone(subId);
1883 if (phone != null) {
1884 return phone.getCdmaEriIconIndex();
1885 } else {
1886 return -1;
1887 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001888 }
1889
1890 /**
1891 * Returns the CDMA ERI icon mode,
1892 * 0 - ON
1893 * 1 - FLASHING
1894 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001895 @Override
1896 public int getCdmaEriIconMode(String callingPackage) {
1897 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001898 }
1899
Sanket Padawe356d7632015-06-22 14:03:32 -07001900 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001901 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1902 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1903 return -1;
1904 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001905 final Phone phone = getPhone(subId);
1906 if (phone != null) {
1907 return phone.getCdmaEriIconMode();
1908 } else {
1909 return -1;
1910 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001911 }
1912
1913 /**
1914 * Returns the CDMA ERI text,
1915 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001916 @Override
1917 public String getCdmaEriText(String callingPackage) {
1918 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001919 }
1920
Sanket Padawe356d7632015-06-22 14:03:32 -07001921 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001922 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1923 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1924 return null;
1925 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001926 final Phone phone = getPhone(subId);
1927 if (phone != null) {
1928 return phone.getCdmaEriText();
1929 } else {
1930 return null;
1931 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001932 }
1933
1934 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001935 * Returns the CDMA MDN.
1936 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001937 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001938 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001939 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001940 final Phone phone = getPhone(subId);
1941 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1942 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001943 } else {
1944 return null;
1945 }
1946 }
1947
1948 /**
1949 * Returns the CDMA MIN.
1950 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001951 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001952 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001953 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001954 final Phone phone = getPhone(subId);
1955 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1956 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001957 } else {
1958 return null;
1959 }
1960 }
1961
1962 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001963 * Returns true if CDMA provisioning needs to run.
1964 */
1965 public boolean needsOtaServiceProvisioning() {
1966 return mPhone.needsOtaServiceProvisioning();
1967 }
1968
1969 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001970 * Sets the voice mail number of a given subId.
1971 */
1972 @Override
1973 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001974 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001975 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1976 new Pair<String, String>(alphaTag, number), new Integer(subId));
1977 return success;
1978 }
1979
Ta-wei Yen87c49842016-05-13 21:19:52 -07001980 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001981 public void setVisualVoicemailEnabled(String callingPackage,
1982 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1983 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1984 if (!TextUtils.equals(callingPackage,
1985 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1986 enforceModifyPermissionOrCarrierPrivilege(
1987 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1988 }
1989 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
1990 }
1991
1992 @Override
1993 public boolean isVisualVoicemailEnabled(String callingPackage,
1994 PhoneAccountHandle phoneAccountHandle) {
1995 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
1996 return false;
1997 }
1998 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
1999 }
2000
2001 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002002 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2003 VisualVoicemailSmsFilterSettings settings) {
2004 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002005 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002006 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2007 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002008 }
2009
2010 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002011 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2012 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002013 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002014 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002015 }
2016
2017 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002018 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2019 String callingPackage, int subId) {
2020 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002021 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002022 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002023 }
2024
2025 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002026 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
2027 String packageName, int subId) {
2028 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002029 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002030 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002031 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002032 /**
fionaxuef039d42016-11-14 13:36:14 -08002033 * Sets the voice activation state of a given subId.
2034 */
2035 @Override
2036 public void setVoiceActivationState(int subId, int activationState) {
2037 enforceModifyPermissionOrCarrierPrivilege(subId);
2038 final Phone phone = getPhone(subId);
2039 if (phone != null) {
2040 phone.setVoiceActivationState(activationState);
2041 } else {
2042 loge("setVoiceActivationState fails with invalid subId: " + subId);
2043 }
2044 }
2045
2046 /**
2047 * Sets the data activation state of a given subId.
2048 */
2049 @Override
2050 public void setDataActivationState(int subId, int activationState) {
2051 enforceModifyPermissionOrCarrierPrivilege(subId);
2052 final Phone phone = getPhone(subId);
2053 if (phone != null) {
2054 phone.setDataActivationState(activationState);
2055 } else {
2056 loge("setVoiceActivationState fails with invalid subId: " + subId);
2057 }
2058 }
2059
2060 /**
2061 * Returns the voice activation state of a given subId.
2062 */
2063 @Override
2064 public int getVoiceActivationState(int subId, String callingPackage) {
2065 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2066 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2067 }
2068 final Phone phone = getPhone(subId);
2069 if (phone != null) {
2070 return phone.getVoiceActivationState();
2071 } else {
2072 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2073 }
2074 }
2075
2076 /**
2077 * Returns the data activation state of a given subId.
2078 */
2079 @Override
2080 public int getDataActivationState(int subId, String callingPackage) {
2081 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2082 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2083 }
2084 final Phone phone = getPhone(subId);
2085 if (phone != null) {
2086 return phone.getDataActivationState();
2087 } else {
2088 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2089 }
2090 }
2091
2092 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002093 * Returns the unread count of voicemails
2094 */
2095 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002096 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002097 }
2098
2099 /**
2100 * Returns the unread count of voicemails for a subId
2101 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002102 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002103 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002104 final Phone phone = getPhone(subId);
2105 if (phone != null) {
2106 return phone.getVoiceMessageCount();
2107 } else {
2108 return 0;
2109 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002110 }
2111
2112 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002113 * Returns the data network type.
2114 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002115 *
2116 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2117 */
2118 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002119 public int getNetworkType() {
2120 final Phone phone = getPhone(getDefaultSubscription());
2121 if (phone != null) {
2122 return phone.getServiceState().getDataNetworkType();
2123 } else {
2124 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2125 }
Wink Saville36469e72014-06-11 15:17:00 -07002126 }
2127
2128 /**
2129 * Returns the network type for a subId
2130 */
2131 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002132 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2133 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2134 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2135 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002136
Sanket Padawe356d7632015-06-22 14:03:32 -07002137 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002138 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002139 return phone.getServiceState().getDataNetworkType();
2140 } else {
2141 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2142 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002143 }
2144
2145 /**
2146 * Returns the data network type
2147 */
2148 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002149 public int getDataNetworkType(String callingPackage) {
2150 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002151 }
2152
2153 /**
2154 * Returns the data network type for a subId
2155 */
2156 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002157 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2158 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2159 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2160 }
2161
Sanket Padawe356d7632015-06-22 14:03:32 -07002162 final Phone phone = getPhone(subId);
2163 if (phone != null) {
2164 return phone.getServiceState().getDataNetworkType();
2165 } else {
2166 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2167 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002168 }
2169
2170 /**
Wink Saville36469e72014-06-11 15:17:00 -07002171 * Returns the Voice network type for a subId
2172 */
2173 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002174 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2175 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2176 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2177 }
2178
Sanket Padawe356d7632015-06-22 14:03:32 -07002179 final Phone phone = getPhone(subId);
2180 if (phone != null) {
2181 return phone.getServiceState().getVoiceNetworkType();
2182 } else {
2183 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2184 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002185 }
2186
2187 /**
2188 * @return true if a ICC card is present
2189 */
2190 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002191 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002192 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2193 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002194 }
2195
2196 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002197 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002198 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002199 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002200 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2201 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002202 final Phone phone = getPhone(subId[0]);
2203 if (subId != null && phone != null) {
2204 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002205 } else {
2206 return false;
2207 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002208 }
2209
2210 /**
2211 * Return if the current radio is LTE on CDMA. This
2212 * is a tri-state return value as for a period of time
2213 * the mode may be unknown.
2214 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002215 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002216 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002217 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002218 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002219 @Override
2220 public int getLteOnCdmaMode(String callingPackage) {
2221 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002222 }
2223
Sanket Padawe356d7632015-06-22 14:03:32 -07002224 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002225 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2226 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2227 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2228 }
2229
Sanket Padawe356d7632015-06-22 14:03:32 -07002230 final Phone phone = getPhone(subId);
2231 if (phone == null) {
2232 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2233 } else {
2234 return phone.getLteOnCdmaMode();
2235 }
Wink Saville36469e72014-06-11 15:17:00 -07002236 }
2237
2238 public void setPhone(Phone phone) {
2239 mPhone = phone;
2240 }
2241
2242 /**
2243 * {@hide}
2244 * Returns Default subId, 0 in the case of single standby.
2245 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002246 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002247 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002248 }
2249
Shishir Agrawala9f32182016-04-12 12:00:16 -07002250 private int getSlotForDefaultSubscription() {
2251 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2252 }
2253
Wink Savilleb564aae2014-10-23 10:18:09 -07002254 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002255 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002256 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002257
2258 /**
2259 * @see android.telephony.TelephonyManager.WifiCallingChoices
2260 */
2261 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002262 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2263 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002264 }
2265
2266 /**
2267 * @see android.telephony.TelephonyManager.WifiCallingChoices
2268 */
2269 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002270 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2271 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2272 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002273 }
2274
Sailesh Nepald1e68152013-12-12 19:08:02 -08002275 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002276 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002277 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002278 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002279
Shishir Agrawal566b7612013-10-28 14:41:00 -07002280 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002281 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2282 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002283
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002284 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002285 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002286 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002287 if (DBG) log("iccOpenLogicalChannel: " + response);
2288 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002289 }
2290
2291 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002292 public boolean iccCloseLogicalChannel(int subId, int channel) {
2293 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002294
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002295 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002296 if (channel < 0) {
2297 return false;
2298 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002299 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002300 if (DBG) log("iccCloseLogicalChannel: " + success);
2301 return success;
2302 }
2303
2304 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002305 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002306 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002307 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002308
2309 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002310 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2311 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002312 " data=" + data);
2313 }
2314
2315 if (channel < 0) {
2316 return "";
2317 }
2318
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002319 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002320 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002321 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2322
Shishir Agrawal566b7612013-10-28 14:41:00 -07002323 // Append the returned status code to the end of the response payload.
2324 String s = Integer.toHexString(
2325 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002326 if (response.payload != null) {
2327 s = IccUtils.bytesToHexString(response.payload) + s;
2328 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002329 return s;
2330 }
Jake Hambye994d462014-02-03 13:10:13 -08002331
Evan Charltonc66da362014-05-16 14:06:40 -07002332 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002333 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002334 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002335 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002336
2337 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002338 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2339 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002340 }
2341
2342 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002343 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002344 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2345
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002346 // Append the returned status code to the end of the response payload.
2347 String s = Integer.toHexString(
2348 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002349 if (response.payload != null) {
2350 s = IccUtils.bytesToHexString(response.payload) + s;
2351 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002352 return s;
2353 }
2354
2355 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002356 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002357 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002358 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002359
2360 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002361 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002362 p1 + " " + p2 + " " + p3 + ":" + filePath);
2363 }
2364
2365 IccIoResult response =
2366 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002367 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2368 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002369
2370 if (DBG) {
2371 log("Exchange SIM_IO [R]" + response);
2372 }
2373
2374 byte[] result = null;
2375 int length = 2;
2376 if (response.payload != null) {
2377 length = 2 + response.payload.length;
2378 result = new byte[length];
2379 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2380 } else {
2381 result = new byte[length];
2382 }
2383
2384 result[length - 1] = (byte) response.sw2;
2385 result[length - 2] = (byte) response.sw1;
2386 return result;
2387 }
2388
Nathan Haroldb3014052017-01-25 15:57:32 -08002389 /**
2390 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2391 * on a particular subscription
2392 */
2393 public String[] getForbiddenPlmns(int subId, int appType) {
2394 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
2395 "Requires READ_PRIVILEGED_PHONE_STATE");
2396 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2397 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2398 return null;
2399 }
2400 Object response = sendRequest(
2401 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2402 if (response instanceof String[]) {
2403 return (String[]) response;
2404 }
2405 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2406 return null;
2407 }
2408
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002409 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002410 public String sendEnvelopeWithStatus(int subId, String content) {
2411 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002412
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002413 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002414 if (response.payload == null) {
2415 return "";
2416 }
2417
2418 // Append the returned status code to the end of the response payload.
2419 String s = Integer.toHexString(
2420 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2421 s = IccUtils.bytesToHexString(response.payload) + s;
2422 return s;
2423 }
2424
Jake Hambye994d462014-02-03 13:10:13 -08002425 /**
2426 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2427 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2428 *
2429 * @param itemID the ID of the item to read
2430 * @return the NV item as a String, or null on error.
2431 */
2432 @Override
2433 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002434 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002435 if (DBG) log("nvReadItem: item " + itemID);
2436 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2437 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2438 return value;
2439 }
2440
2441 /**
2442 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2443 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2444 *
2445 * @param itemID the ID of the item to read
2446 * @param itemValue the value to write, as a String
2447 * @return true on success; false on any failure
2448 */
2449 @Override
2450 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002451 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002452 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2453 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2454 new Pair<Integer, String>(itemID, itemValue));
2455 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2456 return success;
2457 }
2458
2459 /**
2460 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2461 * Used for device configuration by some CDMA operators.
2462 *
2463 * @param preferredRoamingList byte array containing the new PRL
2464 * @return true on success; false on any failure
2465 */
2466 @Override
2467 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002468 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002469 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2470 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2471 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2472 return success;
2473 }
2474
2475 /**
2476 * Perform the specified type of NV config reset.
2477 * Used for device configuration by some CDMA operators.
2478 *
2479 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2480 * @return true on success; false on any failure
2481 */
2482 @Override
2483 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002484 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002485 if (DBG) log("nvResetConfig: type " + resetType);
2486 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2487 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2488 return success;
2489 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002490
2491 /**
Wink Saville36469e72014-06-11 15:17:00 -07002492 * {@hide}
2493 * Returns Default sim, 0 in the case of single standby.
2494 */
2495 public int getDefaultSim() {
2496 //TODO Need to get it from Telephony Devcontroller
2497 return 0;
2498 }
2499
Svet Ganovb320e182015-04-16 12:30:10 -07002500 public String[] getPcscfAddress(String apnType, String callingPackage) {
2501 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2502 return new String[0];
2503 }
2504
2505
ram87fca6f2014-07-18 18:58:44 +05302506 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002507 }
2508
Brad Ebinger76681002017-01-23 13:50:20 -08002509 /**
2510 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2511 * feature or {@link null} if the service is not available. If an ImsServiceController is
2512 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2513 * feature updates.
2514 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002515 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002516 IImsServiceFeatureListener callback) {
2517 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002518 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002519 callback);
2520 }
2521
Wink Saville36469e72014-06-11 15:17:00 -07002522 public void setImsRegistrationState(boolean registered) {
2523 enforceModifyPermission();
2524 mPhone.setImsRegistrationState(registered);
2525 }
2526
2527 /**
Stuart Scott54788802015-03-30 13:18:01 -07002528 * Set the network selection mode to automatic.
2529 *
2530 */
2531 @Override
2532 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002533 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002534 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2535 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2536 }
2537
2538 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002539 * Set the network selection mode to manual with the selected carrier.
2540 */
2541 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002542 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2543 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002544 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002545 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002546 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2547 persistSelection);
2548 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002549 }
2550
2551 /**
2552 * Scans for available networks.
2553 */
2554 @Override
2555 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002556 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002557 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2558 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2559 CMD_PERFORM_NETWORK_SCAN, null, subId);
2560 return result;
2561 }
2562
2563 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002564 * Get the calculated preferred network type.
2565 * Used for debugging incorrect network type.
2566 *
2567 * @return the preferred network type, defined in RILConstants.java.
2568 */
2569 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002570 public int getCalculatedPreferredNetworkType(String callingPackage) {
2571 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2572 return RILConstants.PREFERRED_NETWORK_MODE;
2573 }
2574
Amit Mahajan43330e02014-11-18 11:54:45 -08002575 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002576 }
2577
2578 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002579 * Get the preferred network type.
2580 * Used for device configuration by some CDMA operators.
2581 *
2582 * @return the preferred network type, defined in RILConstants.java.
2583 */
2584 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002585 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002586 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002587 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002588 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002589 int networkType = (result != null ? result[0] : -1);
2590 if (DBG) log("getPreferredNetworkType: " + networkType);
2591 return networkType;
2592 }
2593
2594 /**
2595 * Set the preferred network type.
2596 * Used for device configuration by some CDMA operators.
2597 *
2598 * @param networkType the preferred network type, defined in RILConstants.java.
2599 * @return true on success; false on any failure.
2600 */
2601 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002602 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002603 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002604 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2605 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002606 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002607 if (success) {
2608 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002609 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002610 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002611 return success;
2612 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002613
2614 /**
Junda Liu475951f2014-11-07 16:45:03 -08002615 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2616 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2617 * tethering.
2618 *
2619 * @return 0: Not required. 1: required. 2: Not set.
2620 * @hide
2621 */
2622 @Override
2623 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002624 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002625 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2626 Settings.Global.TETHER_DUN_REQUIRED, 2);
2627 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2628 // config_tether_apndata.
2629 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2630 dunRequired = 1;
2631 }
2632 return dunRequired;
2633 }
2634
2635 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002636 * Set mobile data enabled
2637 * Used by the user through settings etc to turn on/off mobile data
2638 *
2639 * @param enable {@code true} turn turn data on, else {@code false}
2640 */
2641 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002642 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002643 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002644 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002645 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002646 Phone phone = PhoneFactory.getPhone(phoneId);
2647 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002648 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002649 phone.setDataEnabled(enable);
2650 } else {
2651 loge("setDataEnabled: no phone for subId=" + subId);
2652 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002653 }
2654
2655 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002656 * Get whether mobile data is enabled.
2657 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002658 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002659 *
2660 * @return {@code true} if data is enabled else {@code false}
2661 */
2662 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002663 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002664 try {
2665 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2666 null);
2667 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002668 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002669 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002670 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002671 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002672 Phone phone = PhoneFactory.getPhone(phoneId);
2673 if (phone != null) {
2674 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002675 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002676 return retVal;
2677 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002678 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002679 return false;
2680 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002681 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002682
2683 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002684 public int getCarrierPrivilegeStatus(int subId) {
2685 final Phone phone = getPhone(subId);
2686 if (phone == null) {
2687 loge("getCarrierPrivilegeStatus: Invalid subId");
2688 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2689 }
2690 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002691 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002692 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002693 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2694 }
2695 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002696 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002697 }
Junda Liu29340342014-07-10 15:23:27 -07002698
2699 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002700 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002701 if (TextUtils.isEmpty(pkgName))
2702 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002703 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002704 if (card == null) {
2705 loge("checkCarrierPrivilegesForPackage: No UICC");
2706 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2707 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002708 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2709 }
2710
2711 @Override
2712 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002713 if (TextUtils.isEmpty(pkgName))
2714 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002715 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2716 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2717 UiccCard card = UiccController.getInstance().getUiccCard(i);
2718 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002719 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002720 continue;
2721 }
2722
2723 result = card.getCarrierPrivilegeStatus(
2724 mPhone.getContext().getPackageManager(), pkgName);
2725 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2726 break;
2727 }
2728 }
2729
2730 return result;
Junda Liu29340342014-07-10 15:23:27 -07002731 }
Derek Tan89e89d42014-07-08 17:00:10 -07002732
2733 @Override
Junda Liue64de782015-04-16 17:19:16 -07002734 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2735 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2736 loge("phoneId " + phoneId + " is not valid.");
2737 return null;
2738 }
2739 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002740 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002741 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002742 return null ;
2743 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002744 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002745 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002746 }
2747
Amith Yamasani6e118872016-02-19 12:53:51 -08002748 @Override
2749 public List<String> getPackagesWithCarrierPrivileges() {
2750 PackageManager pm = mPhone.getContext().getPackageManager();
2751 List<String> privilegedPackages = new ArrayList<>();
2752 List<PackageInfo> packages = null;
2753 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2754 UiccCard card = UiccController.getInstance().getUiccCard(i);
2755 if (card == null) {
2756 // No UICC in that slot.
2757 continue;
2758 }
2759 if (card.hasCarrierPrivilegeRules()) {
2760 if (packages == null) {
2761 // Only check packages in user 0 for now
2762 packages = pm.getInstalledPackagesAsUser(
2763 PackageManager.MATCH_DISABLED_COMPONENTS
2764 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2765 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2766 }
2767 for (int p = packages.size() - 1; p >= 0; p--) {
2768 PackageInfo pkgInfo = packages.get(p);
2769 if (pkgInfo != null && pkgInfo.packageName != null
2770 && card.getCarrierPrivilegeStatus(pkgInfo)
2771 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2772 privilegedPackages.add(pkgInfo.packageName);
2773 }
2774 }
2775 }
2776 }
2777 return privilegedPackages;
2778 }
2779
Wink Savilleb564aae2014-10-23 10:18:09 -07002780 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002781 final Phone phone = getPhone(subId);
2782 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002783 if (card == null) {
2784 loge("getIccId: No UICC");
2785 return null;
2786 }
2787 String iccId = card.getIccId();
2788 if (TextUtils.isEmpty(iccId)) {
2789 loge("getIccId: ICC ID is null or empty.");
2790 return null;
2791 }
2792 return iccId;
2793 }
2794
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002795 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002796 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2797 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002798 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002799
Jeff Sharkey85190e62014-12-05 09:40:12 -08002800 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002801 final Phone phone = getPhone(subId);
2802 if (phone == null) {
2803 return false;
2804 }
2805 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002806
2807 if (DBG_MERGE) {
2808 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2809 + subscriberId + " to " + number);
2810 }
2811
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002812 if (TextUtils.isEmpty(iccId)) {
2813 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002814 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002815
Jeff Sharkey85190e62014-12-05 09:40:12 -08002816 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2817
2818 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002819 if (alphaTag == null) {
2820 editor.remove(alphaTagPrefKey);
2821 } else {
2822 editor.putString(alphaTagPrefKey, alphaTag);
2823 }
2824
Jeff Sharkey85190e62014-12-05 09:40:12 -08002825 // Record both the line number and IMSI for this ICCID, since we need to
2826 // track all merged IMSIs based on line number
2827 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2828 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002829 if (number == null) {
2830 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002831 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002832 } else {
2833 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002834 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002835 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002836
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002837 editor.commit();
2838 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002839 }
2840
2841 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002842 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002843 // This is open to apps with WRITE_SMS.
2844 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002845 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002846 return null;
2847 }
Derek Tan97ebb422014-09-05 16:55:38 -07002848
2849 String iccId = getIccId(subId);
2850 if (iccId != null) {
2851 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002852 if (DBG_MERGE) {
2853 log("getLine1NumberForDisplay returning " +
2854 mTelephonySharedPreferences.getString(numberPrefKey, null));
2855 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002856 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002857 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002858 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002859 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002860 }
2861
2862 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002863 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2864 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2865 return null;
2866 }
Derek Tan97ebb422014-09-05 16:55:38 -07002867
2868 String iccId = getIccId(subId);
2869 if (iccId != null) {
2870 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002871 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002872 }
Derek Tan97ebb422014-09-05 16:55:38 -07002873 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002874 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002875
2876 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002877 public String[] getMergedSubscriberIds(String callingPackage) {
2878 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2879 return null;
2880 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002881 final Context context = mPhone.getContext();
2882 final TelephonyManager tele = TelephonyManager.from(context);
2883 final SubscriptionManager sub = SubscriptionManager.from(context);
2884
2885 // Figure out what subscribers are currently active
2886 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002887 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2888 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2889 final long identity = Binder.clearCallingIdentity();
2890 try {
2891 final int[] subIds = sub.getActiveSubscriptionIdList();
2892 for (int subId : subIds) {
2893 activeSubscriberIds.add(tele.getSubscriberId(subId));
2894 }
2895 } finally {
2896 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002897 }
2898
2899 // First pass, find a number override for an active subscriber
2900 String mergeNumber = null;
2901 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2902 for (String key : prefs.keySet()) {
2903 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2904 final String subscriberId = (String) prefs.get(key);
2905 if (activeSubscriberIds.contains(subscriberId)) {
2906 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2907 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2908 mergeNumber = (String) prefs.get(numberKey);
2909 if (DBG_MERGE) {
2910 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2911 + " for active subscriber " + subscriberId);
2912 }
2913 if (!TextUtils.isEmpty(mergeNumber)) {
2914 break;
2915 }
2916 }
2917 }
2918 }
2919
2920 // Shortcut when no active merged subscribers
2921 if (TextUtils.isEmpty(mergeNumber)) {
2922 return null;
2923 }
2924
2925 // Second pass, find all subscribers under that line override
2926 final ArraySet<String> result = new ArraySet<>();
2927 for (String key : prefs.keySet()) {
2928 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2929 final String number = (String) prefs.get(key);
2930 if (mergeNumber.equals(number)) {
2931 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2932 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2933 final String subscriberId = (String) prefs.get(subscriberKey);
2934 if (!TextUtils.isEmpty(subscriberId)) {
2935 result.add(subscriberId);
2936 }
2937 }
2938 }
2939 }
2940
2941 final String[] resultArray = result.toArray(new String[result.size()]);
2942 Arrays.sort(resultArray);
2943 if (DBG_MERGE) {
2944 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2945 }
2946 return resultArray;
2947 }
2948
2949 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002950 public boolean setOperatorBrandOverride(int subId, String brand) {
2951 enforceCarrierPrivilege(subId);
2952 final Phone phone = getPhone(subId);
2953 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002954 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002955
2956 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002957 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002958 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2959 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002960 enforceCarrierPrivilege(subId);
2961 final Phone phone = getPhone(subId);
2962 if (phone == null) {
2963 return false;
2964 }
2965 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002966 cdmaNonRoamingList);
2967 }
2968
2969 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08002970 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05002971 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2972 enforceModifyPermission();
2973
2974 int returnValue = 0;
2975 try {
2976 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2977 if(result.exception == null) {
2978 if (result.result != null) {
2979 byte[] responseData = (byte[])(result.result);
2980 if(responseData.length > oemResp.length) {
2981 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2982 responseData.length + "bytes. Buffer Size is " +
2983 oemResp.length + "bytes.");
2984 }
2985 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2986 returnValue = responseData.length;
2987 }
2988 } else {
2989 CommandException ex = (CommandException) result.exception;
2990 returnValue = ex.getCommandError().ordinal();
2991 if(returnValue > 0) returnValue *= -1;
2992 }
2993 } catch (RuntimeException e) {
2994 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2995 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2996 if(returnValue > 0) returnValue *= -1;
2997 }
2998
2999 return returnValue;
3000 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003001
3002 @Override
3003 public void setRadioCapability(RadioAccessFamily[] rafs) {
3004 try {
3005 ProxyController.getInstance().setRadioCapability(rafs);
3006 } catch (RuntimeException e) {
3007 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3008 }
3009 }
3010
3011 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003012 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3013 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3014 return RadioAccessFamily.RAF_UNKNOWN;
3015 }
3016
Wink Saville5d475dd2014-10-17 15:00:58 -07003017 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3018 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003019
3020 @Override
3021 public void enableVideoCalling(boolean enable) {
3022 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003023 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003024 }
3025
3026 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003027 public boolean isVideoCallingEnabled(String callingPackage) {
3028 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3029 return false;
3030 }
3031
Andrew Lee77527ac2014-10-21 16:57:39 -07003032 // Check the user preference and the system-level IMS setting. Even if the user has
3033 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3034 // In the long run, we may instead need to check if there exists a connection service
3035 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003036 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3037 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003038 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003039 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003040
Andrew Leea1239f22015-03-02 17:44:07 -08003041 @Override
3042 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003043 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003044 }
3045
3046 @Override
3047 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003048 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003049 }
3050
Andrew Lee9431b832015-03-09 18:46:45 -07003051 @Override
3052 public boolean isTtyModeSupported() {
3053 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3054 TelephonyManager telephonyManager =
3055 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003056 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003057 }
3058
3059 @Override
3060 public boolean isHearingAidCompatibilitySupported() {
3061 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3062 }
3063
Sanket Padawe7310cc72015-01-14 09:53:20 -08003064 /**
3065 * Returns the unique device ID of phone, for example, the IMEI for
3066 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3067 *
3068 * <p>Requires Permission:
3069 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3070 */
3071 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003072 public String getDeviceId(String callingPackage) {
3073 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3074 return null;
3075 }
3076
Sanket Padawe7310cc72015-01-14 09:53:20 -08003077 final Phone phone = PhoneFactory.getPhone(0);
3078 if (phone != null) {
3079 return phone.getDeviceId();
3080 } else {
3081 return null;
3082 }
3083 }
3084
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003085 /*
3086 * {@hide}
3087 * Returns the IMS Registration Status
3088 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003089 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003090 public boolean isImsRegistered() {
3091 return mPhone.isImsRegistered();
3092 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003093
3094 @Override
3095 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3096 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3097 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003098
Nathan Haroldc55097a2015-03-11 18:14:50 -07003099 /*
3100 * {@hide}
3101 * Returns the IMS Registration Status
3102 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003103 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003104 return mPhone.isWifiCallingEnabled();
3105 }
3106
3107 /*
3108 * {@hide}
3109 * Returns the IMS Registration Status
3110 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003111 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003112 return mPhone.isVolteEnabled();
3113 }
Svet Ganovb320e182015-04-16 12:30:10 -07003114
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003115 /*
3116 * {@hide} Returns the IMS Registration Status
3117 */
3118 public boolean isVideoTelephonyAvailable() {
3119 return mPhone.isVideoEnabled();
3120 }
3121
Svet Ganovb320e182015-04-16 12:30:10 -07003122 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003123 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003124 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003125 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3126
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003127 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003128 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003129 } catch (SecurityException e) {
3130 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3131 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003132 }
Svet Ganovb320e182015-04-16 12:30:10 -07003133
3134 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3135 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3136 return false;
3137 }
3138
3139 return true;
3140 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003141
Makoto Onukifee69342015-06-29 14:44:50 -07003142 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003143 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003144 */
3145 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003146 // Default SMS app can always read it.
3147 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3148 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3149 return true;
3150 }
3151 try {
3152 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003153 } catch (SecurityException readPhoneStateSecurityException) {
3154 try {
3155 // Can be read with READ_SMS too.
3156 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3157 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3158 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3159 } catch (SecurityException readSmsSecurityException) {
3160 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3161 // permissions
3162 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3163 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3164 " or " + android.Manifest.permission.READ_SMS + ".");
3165 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003166 }
Makoto Onukifee69342015-06-29 14:44:50 -07003167 }
3168
Stuart Scott8eef64f2015-04-08 15:13:54 -07003169 @Override
3170 public void factoryReset(int subId) {
3171 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003172 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3173 return;
3174 }
3175
Svet Ganovcc087f82015-05-12 20:35:54 -07003176 final long identity = Binder.clearCallingIdentity();
3177 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003178 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3179 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003180 // Enable data
3181 setDataEnabled(subId, true);
3182 // Set network selection mode to automatic
3183 setNetworkSelectionModeAutomatic(subId);
3184 // Set preferred mobile network type to the best available
3185 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3186 // Turn off roaming
3187 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3188 }
3189 } finally {
3190 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003191 }
3192 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003193
3194 @Override
3195 public String getLocaleFromDefaultSim() {
3196 // We query all subscriptions instead of just the active ones, because
3197 // this might be called early on in the provisioning flow when the
3198 // subscriptions potentially aren't active yet.
3199 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3200 if (slist == null || slist.isEmpty()) {
3201 return null;
3202 }
3203
3204 // This function may be called very early, say, from the setup wizard, at
3205 // which point we won't have a default subscription set. If that's the case
3206 // we just choose the first, which will be valid in "most cases".
3207 final int defaultSubId = getDefaultSubscription();
3208 SubscriptionInfo info = null;
3209 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3210 info = slist.get(0);
3211 } else {
3212 for (SubscriptionInfo item : slist) {
3213 if (item.getSubscriptionId() == defaultSubId) {
3214 info = item;
3215 break;
3216 }
3217 }
3218
3219 if (info == null) {
3220 return null;
3221 }
3222 }
3223
3224 // Try and fetch the locale from the carrier properties or from the SIM language
3225 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003226 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003227 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003228 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003229 if (defaultPhone != null) {
3230 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3231 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003232 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003233 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3234 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003235 } else {
3236 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003237 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003238 }
3239 }
3240
Narayan Kamath011676f2015-07-29 12:04:08 +01003241 // The SIM language preferences only store a language (e.g. fr = French), not an
3242 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3243 // the SIM and carrier preferences does not include a country we add the country
3244 // determined from the SIM MCC to provide an exact locale.
3245 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003246 if (mccLocale != null) {
3247 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3248 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003249 }
3250
Tony Hill183b2de2015-06-24 14:53:58 +01003251 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003252 return null;
3253 }
3254
3255 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3256 final long identity = Binder.clearCallingIdentity();
3257 try {
3258 return mSubscriptionController.getAllSubInfoList(
3259 mPhone.getContext().getOpPackageName());
3260 } finally {
3261 Binder.restoreCallingIdentity(identity);
3262 }
3263 }
3264
3265 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3266 final long identity = Binder.clearCallingIdentity();
3267 try {
3268 return mSubscriptionController.getActiveSubscriptionInfoList(
3269 mPhone.getContext().getOpPackageName());
3270 } finally {
3271 Binder.restoreCallingIdentity(identity);
3272 }
3273 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003274
3275 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003276 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3277 * representing the state of the modem.
3278 *
3279 * NOTE: This clears the modem state, so there should only every be one caller.
3280 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003281 */
3282 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003283 public void requestModemActivityInfo(ResultReceiver result) {
3284 enforceModifyPermission();
3285
3286 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3287 Bundle bundle = new Bundle();
3288 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3289 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003290 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003291
3292 /**
3293 * {@hide}
3294 * Returns the service state information on specified subscription.
3295 */
3296 @Override
3297 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3298
3299 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3300 return null;
3301 }
3302
3303 final Phone phone = getPhone(subId);
3304 if (phone == null) {
3305 return null;
3306 }
3307
3308 return phone.getServiceState();
3309 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003310
3311 /**
3312 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3313 *
3314 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3315 * voicemail ringtone.
3316 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3317 * PhoneAccount.
3318 */
3319 @Override
3320 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3321 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3322 if (phone == null) {
3323 return null;
3324 }
3325
3326 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3327 }
3328
3329 /**
3330 * Returns whether vibration is set for voicemail notification in Phone settings.
3331 *
3332 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3333 * voicemail vibration setting.
3334 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3335 */
3336 @Override
3337 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3338 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3339 if (phone == null) {
3340 return false;
3341 }
3342
3343 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3344 }
3345
Youhan Wange64578a2016-05-02 15:32:42 -07003346 /**
3347 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3348 *
3349 * @throws SecurityException if the caller does not have the required permission
3350 */
3351 private void enforceReadPrivilegedPermission() {
3352 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3353 null);
3354 }
3355
3356 /**
3357 * Return the application ID for the app type.
3358 *
3359 * @param subId the subscription ID that this request applies to.
3360 * @param appType the uicc app type.
3361 * @return Application ID for specificied app type, or null if no uicc.
3362 */
3363 @Override
3364 public String getAidForAppType(int subId, int appType) {
3365 enforceReadPrivilegedPermission();
3366 Phone phone = getPhone(subId);
3367 if (phone == null) {
3368 return null;
3369 }
3370 String aid = null;
3371 try {
3372 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3373 .getApplicationByType(appType).getAid();
3374 } catch (Exception e) {
3375 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3376 }
3377 return aid;
3378 }
3379
Youhan Wang4001d252016-05-11 10:29:41 -07003380 /**
3381 * Return the Electronic Serial Number.
3382 *
3383 * @param subId the subscription ID that this request applies to.
3384 * @return ESN or null if error.
3385 */
3386 @Override
3387 public String getEsn(int subId) {
3388 enforceReadPrivilegedPermission();
3389 Phone phone = getPhone(subId);
3390 if (phone == null) {
3391 return null;
3392 }
3393 String esn = null;
3394 try {
3395 esn = phone.getEsn();
3396 } catch (Exception e) {
3397 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3398 }
3399 return esn;
3400 }
3401
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003402 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003403 * Return the Preferred Roaming List Version.
3404 *
3405 * @param subId the subscription ID that this request applies to.
3406 * @return PRLVersion or null if error.
3407 */
3408 @Override
3409 public String getCdmaPrlVersion(int subId) {
3410 enforceReadPrivilegedPermission();
3411 Phone phone = getPhone(subId);
3412 if (phone == null) {
3413 return null;
3414 }
3415 String cdmaPrlVersion = null;
3416 try {
3417 cdmaPrlVersion = phone.getCdmaPrlVersion();
3418 } catch (Exception e) {
3419 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3420 }
3421 return cdmaPrlVersion;
3422 }
3423
3424 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003425 * Get snapshot of Telephony histograms
3426 * @return List of Telephony histograms
3427 * @hide
3428 */
3429 @Override
3430 public List<TelephonyHistogram> getTelephonyHistograms() {
3431 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3432 return RIL.getTelephonyRILTimingHistograms();
3433 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003434
3435 /**
3436 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003437 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003438 * Require system privileges. In the future we may add this to carrier APIs.
3439 *
3440 * @return The number of carriers set successfully, should match length of carriers
3441 */
3442 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003443 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003444 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003445 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003446 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3447 return retVal[0];
3448 }
3449
3450 /**
3451 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003452 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003453 * Require system privileges. In the future we may add this to carrier APIs.
3454 *
3455 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3456 * means all carriers are allowed.
3457 */
3458 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003459 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003460 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003461 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003462 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3463 }
3464
fionaxu59545b42016-05-25 15:53:37 -07003465 /**
3466 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3467 * @param subId the subscription ID that this action applies to.
3468 * @param enabled control enable or disable metered apns.
3469 * {@hide}
3470 */
3471 @Override
3472 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3473 enforceModifyPermission();
3474 final Phone phone = getPhone(subId);
3475 if (phone == null) {
3476 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3477 return;
3478 }
3479 try {
3480 phone.carrierActionSetMeteredApnsEnabled(enabled);
3481 } catch (Exception e) {
3482 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3483 }
3484 }
3485
3486 /**
3487 * Action set from carrier signalling broadcast receivers to enable/disable radio
3488 * @param subId the subscription ID that this action applies to.
3489 * @param enabled control enable or disable radio.
3490 * {@hide}
3491 */
3492 @Override
3493 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3494 enforceModifyPermission();
3495 final Phone phone = getPhone(subId);
3496 if (phone == null) {
3497 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3498 return;
3499 }
3500 try {
3501 phone.carrierActionSetRadioEnabled(enabled);
3502 } catch (Exception e) {
3503 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3504 }
3505 }
3506
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003507 /**
3508 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3509 * bug report is being generated.
3510 */
3511 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003512 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003513 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3514 != PackageManager.PERMISSION_GRANTED) {
3515 writer.println("Permission Denial: can't dump Phone from pid="
3516 + Binder.getCallingPid()
3517 + ", uid=" + Binder.getCallingUid()
3518 + "without permission "
3519 + android.Manifest.permission.DUMP);
3520 return;
3521 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003522 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003523 }
Jack Yueb89b242016-06-22 13:27:47 -07003524
3525 /**
3526 * Get aggregated video call data usage from all subscriptions since boot.
3527 * @return total data usage in bytes
3528 * {@hide}
3529 */
3530 @Override
3531 public long getVtDataUsage() {
3532 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3533 null);
3534
3535 // NetworkStatsService keeps tracking the active network interface and identity. It will
3536 // record the delta with the corresponding network identity. What we need to do here is
3537 // returning total video call data usage from all subscriptions since boot.
3538
3539 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3540 // simultaneous VT calls.
3541 final Phone[] phones = PhoneFactory.getPhones();
3542 long total = 0;
3543 for (Phone phone : phones) {
3544 total += phone.getVtDataUsage();
3545 }
3546 return total;
3547 }
Jack Yu75ab2952016-07-08 14:29:33 -07003548
3549 /**
3550 * Policy control of data connection. Usually used when data limit is passed.
3551 * @param enabled True if enabling the data, otherwise disabling.
3552 * @param subId Subscription index
3553 * {@hide}
3554 */
3555 @Override
3556 public void setPolicyDataEnabled(boolean enabled, int subId) {
3557 enforceModifyPermission();
3558 Phone phone = getPhone(subId);
3559 if (phone != null) {
3560 phone.setPolicyDataEnabled(enabled);
3561 }
3562 }
Sooraj Sasindran600a37a2016-07-18 11:57:25 -07003563
3564 /**
3565 * Get Client request stats
3566 * @return List of Client Request Stats
3567 * @hide
3568 */
3569 @Override
3570 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3571 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3572 return null;
3573 }
3574
3575 Phone phone = getPhone(subId);
3576 if (phone != null) {
3577 return phone.getClientRequestStats();
3578 }
3579
3580 return null;
3581 }
3582
3583 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3584 if (workSource != null) {
3585 return workSource;
3586 }
3587
3588 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3589 workSource = new WorkSource(uid, packageName);
3590 return workSource;
3591 }
Jack Yu346de222017-02-16 15:32:43 -08003592
3593 /**
3594 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3595 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003596 * @param slotIndex SIM slot id.
Jack Yu346de222017-02-16 15:32:43 -08003597 * @param powerUp True if powering up the SIM, otherwise powering down
3598 *
3599 **/
3600 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003601 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yu346de222017-02-16 15:32:43 -08003602 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003603 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003604 if (subId == null || subId.length == 0) {
3605 return;
3606 }
3607
3608 final Phone phone = getPhone(subId[0]);
3609 if (phone != null) {
3610 phone.setSimPowerState(powerUp);
3611 }
3612 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003613}