blob: 2165a48dc5c3228094a68920ccd7aa2b286bee83 [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 Sasindran22882212016-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 Sasindran22882212016-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 Sasindran22882212016-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 Sasindran22882212016-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 Sasindran22882212016-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 Sasindran22882212016-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 Sasindran22882212016-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 Sasindran22882212016-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) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001705 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001706 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1707 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001708 }
1709
Shishir Agrawala9f32182016-04-12 12:00:16 -07001710 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001711 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001712 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1713 return null;
1714 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001715 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001716 return phone == null ? null : phone.getImei();
1717 }
1718
1719 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001720 public String getMeidForSlot(int slotIndex, String callingPackage) {
1721 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1722 return null;
1723 }
1724 Phone phone = PhoneFactory.getPhone(slotIndex);
1725 return phone == null ? null : phone.getMeid();
1726 }
1727
1728 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001729 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001730 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1731 return null;
1732 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001733 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001734 return phone == null ? null : phone.getDeviceSvn();
1735 }
1736
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001737 //
1738 // Internal helper methods.
1739 //
1740
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001741 /**
1742 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1743 */
1744 private boolean checkCallerInteractAcrossUsersFull() {
1745 return mPhone.getContext().checkCallingOrSelfPermission(
1746 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1747 == PackageManager.PERMISSION_GRANTED;
1748 }
1749
Jake Hambye994d462014-02-03 13:10:13 -08001750 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001751 boolean ok;
1752
1753 boolean self = Binder.getCallingUid() == Process.myUid();
1754 if (!self) {
1755 // Get the caller's user id then clear the calling identity
1756 // which will be restored in the finally clause.
1757 int callingUser = UserHandle.getCallingUserId();
1758 long ident = Binder.clearCallingIdentity();
1759
1760 try {
1761 // With calling identity cleared the current user is the foreground user.
1762 int foregroundUser = ActivityManager.getCurrentUser();
1763 ok = (foregroundUser == callingUser);
1764 if (DBG_LOC) {
1765 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1766 + " callingUser=" + callingUser + " ok=" + ok);
1767 }
1768 } catch (Exception ex) {
1769 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1770 ok = false;
1771 } finally {
1772 Binder.restoreCallingIdentity(ident);
1773 }
1774 } else {
1775 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1776 ok = true;
1777 }
1778 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1779 return ok;
1780 }
1781
1782 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001783 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1784 *
1785 * @throws SecurityException if the caller does not have the required permission
1786 */
1787 private void enforceModifyPermission() {
1788 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1789 }
1790
1791 /**
Junda Liua2e36012014-07-09 18:30:01 -07001792 * Make sure either system app or the caller has carrier privilege.
1793 *
1794 * @throws SecurityException if the caller does not have the required permission/privilege
1795 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001796 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001797 int permission = mApp.checkCallingOrSelfPermission(
1798 android.Manifest.permission.MODIFY_PHONE_STATE);
1799 if (permission == PackageManager.PERMISSION_GRANTED) {
1800 return;
1801 }
1802
1803 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001804 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001805 }
1806
1807 /**
1808 * Make sure the caller has carrier privilege.
1809 *
1810 * @throws SecurityException if the caller does not have the required permission
1811 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001812 private void enforceCarrierPrivilege(int subId) {
1813 if (getCarrierPrivilegeStatus(subId) !=
1814 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001815 loge("No Carrier Privilege.");
1816 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001817 }
1818 }
1819
1820 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001821 * Make sure the caller has the CALL_PHONE permission.
1822 *
1823 * @throws SecurityException if the caller does not have the required permission
1824 */
1825 private void enforceCallPermission() {
1826 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1827 }
1828
Stuart Scott8eef64f2015-04-08 15:13:54 -07001829 private void enforceConnectivityInternalPermission() {
1830 mApp.enforceCallingOrSelfPermission(
1831 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1832 "ConnectivityService");
1833 }
1834
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001835 private String createTelUrl(String number) {
1836 if (TextUtils.isEmpty(number)) {
1837 return null;
1838 }
1839
Jake Hambye994d462014-02-03 13:10:13 -08001840 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001841 }
1842
Ihab Awadf9e92732013-12-05 18:02:52 -08001843 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001844 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1845 }
1846
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001847 private static void logv(String msg) {
1848 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1849 }
1850
Ihab Awadf9e92732013-12-05 18:02:52 -08001851 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001852 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1853 }
1854
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001855 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001856 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001857 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001858 }
1859
Sanket Padawe356d7632015-06-22 14:03:32 -07001860 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001861 public int getActivePhoneTypeForSlot(int slotIndex) {
1862 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001863 if (phone == null) {
1864 return PhoneConstants.PHONE_TYPE_NONE;
1865 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001866 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001867 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001868 }
1869
1870 /**
1871 * Returns the CDMA ERI icon index to display
1872 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001873 @Override
1874 public int getCdmaEriIconIndex(String callingPackage) {
1875 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001876 }
1877
Sanket Padawe356d7632015-06-22 14:03:32 -07001878 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001879 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1880 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1881 return -1;
1882 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001883 final Phone phone = getPhone(subId);
1884 if (phone != null) {
1885 return phone.getCdmaEriIconIndex();
1886 } else {
1887 return -1;
1888 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001889 }
1890
1891 /**
1892 * Returns the CDMA ERI icon mode,
1893 * 0 - ON
1894 * 1 - FLASHING
1895 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001896 @Override
1897 public int getCdmaEriIconMode(String callingPackage) {
1898 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001899 }
1900
Sanket Padawe356d7632015-06-22 14:03:32 -07001901 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001902 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1903 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1904 return -1;
1905 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001906 final Phone phone = getPhone(subId);
1907 if (phone != null) {
1908 return phone.getCdmaEriIconMode();
1909 } else {
1910 return -1;
1911 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001912 }
1913
1914 /**
1915 * Returns the CDMA ERI text,
1916 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001917 @Override
1918 public String getCdmaEriText(String callingPackage) {
1919 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001920 }
1921
Sanket Padawe356d7632015-06-22 14:03:32 -07001922 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001923 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1924 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1925 return null;
1926 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001927 final Phone phone = getPhone(subId);
1928 if (phone != null) {
1929 return phone.getCdmaEriText();
1930 } else {
1931 return null;
1932 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001933 }
1934
1935 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001936 * Returns the CDMA MDN.
1937 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001938 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001939 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001940 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001941 final Phone phone = getPhone(subId);
1942 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1943 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001944 } else {
1945 return null;
1946 }
1947 }
1948
1949 /**
1950 * Returns the CDMA MIN.
1951 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001952 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001953 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001954 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001955 final Phone phone = getPhone(subId);
1956 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1957 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001958 } else {
1959 return null;
1960 }
1961 }
1962
1963 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001964 * Returns true if CDMA provisioning needs to run.
1965 */
1966 public boolean needsOtaServiceProvisioning() {
1967 return mPhone.needsOtaServiceProvisioning();
1968 }
1969
1970 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001971 * Sets the voice mail number of a given subId.
1972 */
1973 @Override
1974 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001975 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001976 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1977 new Pair<String, String>(alphaTag, number), new Integer(subId));
1978 return success;
1979 }
1980
Ta-wei Yen87c49842016-05-13 21:19:52 -07001981 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001982 public void setVisualVoicemailEnabled(String callingPackage,
1983 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1984 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1985 if (!TextUtils.equals(callingPackage,
1986 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1987 enforceModifyPermissionOrCarrierPrivilege(
1988 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1989 }
1990 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
1991 }
1992
1993 @Override
1994 public boolean isVisualVoicemailEnabled(String callingPackage,
1995 PhoneAccountHandle phoneAccountHandle) {
1996 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
1997 return false;
1998 }
1999 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2000 }
2001
2002 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002003 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2004 VisualVoicemailSmsFilterSettings settings) {
2005 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002006 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002007 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2008 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002009 }
2010
2011 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002012 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2013 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002014 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002015 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002016 }
2017
2018 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002019 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2020 String callingPackage, int subId) {
2021 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002022 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002023 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002024 }
2025
2026 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002027 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
2028 String packageName, int subId) {
2029 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002030 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002031 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002032 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002033 /**
fionaxuef039d42016-11-14 13:36:14 -08002034 * Sets the voice activation state of a given subId.
2035 */
2036 @Override
2037 public void setVoiceActivationState(int subId, int activationState) {
2038 enforceModifyPermissionOrCarrierPrivilege(subId);
2039 final Phone phone = getPhone(subId);
2040 if (phone != null) {
2041 phone.setVoiceActivationState(activationState);
2042 } else {
2043 loge("setVoiceActivationState fails with invalid subId: " + subId);
2044 }
2045 }
2046
2047 /**
2048 * Sets the data activation state of a given subId.
2049 */
2050 @Override
2051 public void setDataActivationState(int subId, int activationState) {
2052 enforceModifyPermissionOrCarrierPrivilege(subId);
2053 final Phone phone = getPhone(subId);
2054 if (phone != null) {
2055 phone.setDataActivationState(activationState);
2056 } else {
2057 loge("setVoiceActivationState fails with invalid subId: " + subId);
2058 }
2059 }
2060
2061 /**
2062 * Returns the voice activation state of a given subId.
2063 */
2064 @Override
2065 public int getVoiceActivationState(int subId, String callingPackage) {
2066 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2067 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2068 }
2069 final Phone phone = getPhone(subId);
2070 if (phone != null) {
2071 return phone.getVoiceActivationState();
2072 } else {
2073 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2074 }
2075 }
2076
2077 /**
2078 * Returns the data activation state of a given subId.
2079 */
2080 @Override
2081 public int getDataActivationState(int subId, String callingPackage) {
2082 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2083 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2084 }
2085 final Phone phone = getPhone(subId);
2086 if (phone != null) {
2087 return phone.getDataActivationState();
2088 } else {
2089 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2090 }
2091 }
2092
2093 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002094 * Returns the unread count of voicemails
2095 */
2096 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002097 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002098 }
2099
2100 /**
2101 * Returns the unread count of voicemails for a subId
2102 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002103 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002104 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002105 final Phone phone = getPhone(subId);
2106 if (phone != null) {
2107 return phone.getVoiceMessageCount();
2108 } else {
2109 return 0;
2110 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002111 }
2112
2113 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002114 * Returns the data network type.
2115 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002116 *
2117 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2118 */
2119 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002120 public int getNetworkType() {
2121 final Phone phone = getPhone(getDefaultSubscription());
2122 if (phone != null) {
2123 return phone.getServiceState().getDataNetworkType();
2124 } else {
2125 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2126 }
Wink Saville36469e72014-06-11 15:17:00 -07002127 }
2128
2129 /**
2130 * Returns the network type for a subId
2131 */
2132 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002133 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2134 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2135 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2136 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002137
Sanket Padawe356d7632015-06-22 14:03:32 -07002138 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002139 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002140 return phone.getServiceState().getDataNetworkType();
2141 } else {
2142 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2143 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002144 }
2145
2146 /**
2147 * Returns the data network type
2148 */
2149 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002150 public int getDataNetworkType(String callingPackage) {
2151 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002152 }
2153
2154 /**
2155 * Returns the data network type for a subId
2156 */
2157 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002158 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2159 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2160 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2161 }
2162
Sanket Padawe356d7632015-06-22 14:03:32 -07002163 final Phone phone = getPhone(subId);
2164 if (phone != null) {
2165 return phone.getServiceState().getDataNetworkType();
2166 } else {
2167 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2168 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002169 }
2170
2171 /**
Wink Saville36469e72014-06-11 15:17:00 -07002172 * Returns the Voice network type for a subId
2173 */
2174 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002175 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2176 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2177 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2178 }
2179
Sanket Padawe356d7632015-06-22 14:03:32 -07002180 final Phone phone = getPhone(subId);
2181 if (phone != null) {
2182 return phone.getServiceState().getVoiceNetworkType();
2183 } else {
2184 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2185 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002186 }
2187
2188 /**
2189 * @return true if a ICC card is present
2190 */
2191 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002192 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002193 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2194 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002195 }
2196
2197 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002198 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002199 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002200 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002201 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2202 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002203 final Phone phone = getPhone(subId[0]);
2204 if (subId != null && phone != null) {
2205 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002206 } else {
2207 return false;
2208 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002209 }
2210
2211 /**
2212 * Return if the current radio is LTE on CDMA. This
2213 * is a tri-state return value as for a period of time
2214 * the mode may be unknown.
2215 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002216 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002217 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002218 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002219 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002220 @Override
2221 public int getLteOnCdmaMode(String callingPackage) {
2222 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002223 }
2224
Sanket Padawe356d7632015-06-22 14:03:32 -07002225 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002226 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2227 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2228 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2229 }
2230
Sanket Padawe356d7632015-06-22 14:03:32 -07002231 final Phone phone = getPhone(subId);
2232 if (phone == null) {
2233 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2234 } else {
2235 return phone.getLteOnCdmaMode();
2236 }
Wink Saville36469e72014-06-11 15:17:00 -07002237 }
2238
2239 public void setPhone(Phone phone) {
2240 mPhone = phone;
2241 }
2242
2243 /**
2244 * {@hide}
2245 * Returns Default subId, 0 in the case of single standby.
2246 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002247 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002248 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002249 }
2250
Shishir Agrawala9f32182016-04-12 12:00:16 -07002251 private int getSlotForDefaultSubscription() {
2252 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2253 }
2254
Wink Savilleb564aae2014-10-23 10:18:09 -07002255 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002256 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002257 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002258
2259 /**
2260 * @see android.telephony.TelephonyManager.WifiCallingChoices
2261 */
2262 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002263 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2264 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002265 }
2266
2267 /**
2268 * @see android.telephony.TelephonyManager.WifiCallingChoices
2269 */
2270 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002271 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2272 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2273 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002274 }
2275
Sailesh Nepald1e68152013-12-12 19:08:02 -08002276 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002277 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002278 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002279 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002280
Shishir Agrawal566b7612013-10-28 14:41:00 -07002281 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002282 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2283 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002284
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002285 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002286 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002287 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002288 if (DBG) log("iccOpenLogicalChannel: " + response);
2289 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002290 }
2291
2292 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002293 public boolean iccCloseLogicalChannel(int subId, int channel) {
2294 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002295
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002296 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002297 if (channel < 0) {
2298 return false;
2299 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002300 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002301 if (DBG) log("iccCloseLogicalChannel: " + success);
2302 return success;
2303 }
2304
2305 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002306 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002307 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002308 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002309
2310 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002311 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2312 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002313 " data=" + data);
2314 }
2315
2316 if (channel < 0) {
2317 return "";
2318 }
2319
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002320 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002321 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002322 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2323
Shishir Agrawal566b7612013-10-28 14:41:00 -07002324 // Append the returned status code to the end of the response payload.
2325 String s = Integer.toHexString(
2326 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002327 if (response.payload != null) {
2328 s = IccUtils.bytesToHexString(response.payload) + s;
2329 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002330 return s;
2331 }
Jake Hambye994d462014-02-03 13:10:13 -08002332
Evan Charltonc66da362014-05-16 14:06:40 -07002333 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002334 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002335 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002336 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002337
2338 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002339 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2340 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002341 }
2342
2343 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002344 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002345 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2346
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002347 // Append the returned status code to the end of the response payload.
2348 String s = Integer.toHexString(
2349 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002350 if (response.payload != null) {
2351 s = IccUtils.bytesToHexString(response.payload) + s;
2352 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002353 return s;
2354 }
2355
2356 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002357 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002358 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002359 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002360
2361 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002362 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002363 p1 + " " + p2 + " " + p3 + ":" + filePath);
2364 }
2365
2366 IccIoResult response =
2367 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002368 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2369 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002370
2371 if (DBG) {
2372 log("Exchange SIM_IO [R]" + response);
2373 }
2374
2375 byte[] result = null;
2376 int length = 2;
2377 if (response.payload != null) {
2378 length = 2 + response.payload.length;
2379 result = new byte[length];
2380 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2381 } else {
2382 result = new byte[length];
2383 }
2384
2385 result[length - 1] = (byte) response.sw2;
2386 result[length - 2] = (byte) response.sw1;
2387 return result;
2388 }
2389
Nathan Haroldb3014052017-01-25 15:57:32 -08002390 /**
2391 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2392 * on a particular subscription
2393 */
2394 public String[] getForbiddenPlmns(int subId, int appType) {
2395 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
2396 "Requires READ_PRIVILEGED_PHONE_STATE");
2397 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2398 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2399 return null;
2400 }
2401 Object response = sendRequest(
2402 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2403 if (response instanceof String[]) {
2404 return (String[]) response;
2405 }
2406 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2407 return null;
2408 }
2409
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002410 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002411 public String sendEnvelopeWithStatus(int subId, String content) {
2412 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002413
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002414 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002415 if (response.payload == null) {
2416 return "";
2417 }
2418
2419 // Append the returned status code to the end of the response payload.
2420 String s = Integer.toHexString(
2421 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2422 s = IccUtils.bytesToHexString(response.payload) + s;
2423 return s;
2424 }
2425
Jake Hambye994d462014-02-03 13:10:13 -08002426 /**
2427 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2428 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2429 *
2430 * @param itemID the ID of the item to read
2431 * @return the NV item as a String, or null on error.
2432 */
2433 @Override
2434 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002435 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002436 if (DBG) log("nvReadItem: item " + itemID);
2437 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2438 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2439 return value;
2440 }
2441
2442 /**
2443 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2444 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2445 *
2446 * @param itemID the ID of the item to read
2447 * @param itemValue the value to write, as a String
2448 * @return true on success; false on any failure
2449 */
2450 @Override
2451 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002452 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002453 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2454 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2455 new Pair<Integer, String>(itemID, itemValue));
2456 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2457 return success;
2458 }
2459
2460 /**
2461 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2462 * Used for device configuration by some CDMA operators.
2463 *
2464 * @param preferredRoamingList byte array containing the new PRL
2465 * @return true on success; false on any failure
2466 */
2467 @Override
2468 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002469 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002470 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2471 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2472 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2473 return success;
2474 }
2475
2476 /**
2477 * Perform the specified type of NV config reset.
2478 * Used for device configuration by some CDMA operators.
2479 *
2480 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2481 * @return true on success; false on any failure
2482 */
2483 @Override
2484 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002485 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002486 if (DBG) log("nvResetConfig: type " + resetType);
2487 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2488 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2489 return success;
2490 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002491
2492 /**
Wink Saville36469e72014-06-11 15:17:00 -07002493 * {@hide}
2494 * Returns Default sim, 0 in the case of single standby.
2495 */
2496 public int getDefaultSim() {
2497 //TODO Need to get it from Telephony Devcontroller
2498 return 0;
2499 }
2500
Svet Ganovb320e182015-04-16 12:30:10 -07002501 public String[] getPcscfAddress(String apnType, String callingPackage) {
2502 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2503 return new String[0];
2504 }
2505
2506
ram87fca6f2014-07-18 18:58:44 +05302507 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002508 }
2509
Brad Ebinger76681002017-01-23 13:50:20 -08002510 /**
2511 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2512 * feature or {@link null} if the service is not available. If an ImsServiceController is
2513 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2514 * feature updates.
2515 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002516 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002517 IImsServiceFeatureListener callback) {
2518 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002519 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002520 callback);
2521 }
2522
Wink Saville36469e72014-06-11 15:17:00 -07002523 public void setImsRegistrationState(boolean registered) {
2524 enforceModifyPermission();
2525 mPhone.setImsRegistrationState(registered);
2526 }
2527
2528 /**
Stuart Scott54788802015-03-30 13:18:01 -07002529 * Set the network selection mode to automatic.
2530 *
2531 */
2532 @Override
2533 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002534 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002535 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2536 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2537 }
2538
2539 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002540 * Set the network selection mode to manual with the selected carrier.
2541 */
2542 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002543 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2544 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002545 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002546 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002547 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2548 persistSelection);
2549 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002550 }
2551
2552 /**
2553 * Scans for available networks.
2554 */
2555 @Override
2556 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002557 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002558 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2559 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2560 CMD_PERFORM_NETWORK_SCAN, null, subId);
2561 return result;
2562 }
2563
2564 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002565 * Get the calculated preferred network type.
2566 * Used for debugging incorrect network type.
2567 *
2568 * @return the preferred network type, defined in RILConstants.java.
2569 */
2570 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002571 public int getCalculatedPreferredNetworkType(String callingPackage) {
2572 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2573 return RILConstants.PREFERRED_NETWORK_MODE;
2574 }
2575
Amit Mahajan43330e02014-11-18 11:54:45 -08002576 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002577 }
2578
2579 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002580 * Get the preferred network type.
2581 * Used for device configuration by some CDMA operators.
2582 *
2583 * @return the preferred network type, defined in RILConstants.java.
2584 */
2585 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002586 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002587 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002588 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002589 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002590 int networkType = (result != null ? result[0] : -1);
2591 if (DBG) log("getPreferredNetworkType: " + networkType);
2592 return networkType;
2593 }
2594
2595 /**
2596 * Set the preferred network type.
2597 * Used for device configuration by some CDMA operators.
2598 *
2599 * @param networkType the preferred network type, defined in RILConstants.java.
2600 * @return true on success; false on any failure.
2601 */
2602 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002603 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002604 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002605 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2606 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002607 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002608 if (success) {
2609 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002610 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002611 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002612 return success;
2613 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002614
2615 /**
Junda Liu475951f2014-11-07 16:45:03 -08002616 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2617 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2618 * tethering.
2619 *
2620 * @return 0: Not required. 1: required. 2: Not set.
2621 * @hide
2622 */
2623 @Override
2624 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002625 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002626 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2627 Settings.Global.TETHER_DUN_REQUIRED, 2);
2628 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2629 // config_tether_apndata.
2630 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2631 dunRequired = 1;
2632 }
2633 return dunRequired;
2634 }
2635
2636 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002637 * Set mobile data enabled
2638 * Used by the user through settings etc to turn on/off mobile data
2639 *
2640 * @param enable {@code true} turn turn data on, else {@code false}
2641 */
2642 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002643 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002644 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002645 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002646 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002647 Phone phone = PhoneFactory.getPhone(phoneId);
2648 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002649 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002650 phone.setDataEnabled(enable);
2651 } else {
2652 loge("setDataEnabled: no phone for subId=" + subId);
2653 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002654 }
2655
2656 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002657 * Get whether mobile data is enabled.
2658 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002659 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002660 *
2661 * @return {@code true} if data is enabled else {@code false}
2662 */
2663 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002664 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002665 try {
2666 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2667 null);
2668 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002669 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002670 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002671 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002672 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002673 Phone phone = PhoneFactory.getPhone(phoneId);
2674 if (phone != null) {
2675 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002676 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002677 return retVal;
2678 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002679 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002680 return false;
2681 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002682 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002683
2684 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002685 public int getCarrierPrivilegeStatus(int subId) {
2686 final Phone phone = getPhone(subId);
2687 if (phone == null) {
2688 loge("getCarrierPrivilegeStatus: Invalid subId");
2689 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2690 }
2691 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002692 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002693 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002694 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2695 }
2696 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002697 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002698 }
Junda Liu29340342014-07-10 15:23:27 -07002699
2700 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002701 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002702 if (TextUtils.isEmpty(pkgName))
2703 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002704 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002705 if (card == null) {
2706 loge("checkCarrierPrivilegesForPackage: No UICC");
2707 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2708 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002709 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2710 }
2711
2712 @Override
2713 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002714 if (TextUtils.isEmpty(pkgName))
2715 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002716 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2717 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2718 UiccCard card = UiccController.getInstance().getUiccCard(i);
2719 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002720 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002721 continue;
2722 }
2723
2724 result = card.getCarrierPrivilegeStatus(
2725 mPhone.getContext().getPackageManager(), pkgName);
2726 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2727 break;
2728 }
2729 }
2730
2731 return result;
Junda Liu29340342014-07-10 15:23:27 -07002732 }
Derek Tan89e89d42014-07-08 17:00:10 -07002733
2734 @Override
Junda Liue64de782015-04-16 17:19:16 -07002735 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2736 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2737 loge("phoneId " + phoneId + " is not valid.");
2738 return null;
2739 }
2740 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002741 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002742 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002743 return null ;
2744 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002745 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002746 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002747 }
2748
Amith Yamasani6e118872016-02-19 12:53:51 -08002749 @Override
2750 public List<String> getPackagesWithCarrierPrivileges() {
2751 PackageManager pm = mPhone.getContext().getPackageManager();
2752 List<String> privilegedPackages = new ArrayList<>();
2753 List<PackageInfo> packages = null;
2754 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2755 UiccCard card = UiccController.getInstance().getUiccCard(i);
2756 if (card == null) {
2757 // No UICC in that slot.
2758 continue;
2759 }
2760 if (card.hasCarrierPrivilegeRules()) {
2761 if (packages == null) {
2762 // Only check packages in user 0 for now
2763 packages = pm.getInstalledPackagesAsUser(
2764 PackageManager.MATCH_DISABLED_COMPONENTS
2765 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2766 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2767 }
2768 for (int p = packages.size() - 1; p >= 0; p--) {
2769 PackageInfo pkgInfo = packages.get(p);
2770 if (pkgInfo != null && pkgInfo.packageName != null
2771 && card.getCarrierPrivilegeStatus(pkgInfo)
2772 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2773 privilegedPackages.add(pkgInfo.packageName);
2774 }
2775 }
2776 }
2777 }
2778 return privilegedPackages;
2779 }
2780
Wink Savilleb564aae2014-10-23 10:18:09 -07002781 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002782 final Phone phone = getPhone(subId);
2783 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002784 if (card == null) {
2785 loge("getIccId: No UICC");
2786 return null;
2787 }
2788 String iccId = card.getIccId();
2789 if (TextUtils.isEmpty(iccId)) {
2790 loge("getIccId: ICC ID is null or empty.");
2791 return null;
2792 }
2793 return iccId;
2794 }
2795
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002796 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002797 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2798 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002799 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002800
Jeff Sharkey85190e62014-12-05 09:40:12 -08002801 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002802 final Phone phone = getPhone(subId);
2803 if (phone == null) {
2804 return false;
2805 }
2806 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002807
2808 if (DBG_MERGE) {
2809 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2810 + subscriberId + " to " + number);
2811 }
2812
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002813 if (TextUtils.isEmpty(iccId)) {
2814 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002815 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002816
Jeff Sharkey85190e62014-12-05 09:40:12 -08002817 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2818
2819 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002820 if (alphaTag == null) {
2821 editor.remove(alphaTagPrefKey);
2822 } else {
2823 editor.putString(alphaTagPrefKey, alphaTag);
2824 }
2825
Jeff Sharkey85190e62014-12-05 09:40:12 -08002826 // Record both the line number and IMSI for this ICCID, since we need to
2827 // track all merged IMSIs based on line number
2828 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2829 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002830 if (number == null) {
2831 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002832 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002833 } else {
2834 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002835 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002836 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002837
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002838 editor.commit();
2839 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002840 }
2841
2842 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002843 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002844 // This is open to apps with WRITE_SMS.
2845 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002846 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002847 return null;
2848 }
Derek Tan97ebb422014-09-05 16:55:38 -07002849
2850 String iccId = getIccId(subId);
2851 if (iccId != null) {
2852 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002853 if (DBG_MERGE) {
2854 log("getLine1NumberForDisplay returning " +
2855 mTelephonySharedPreferences.getString(numberPrefKey, null));
2856 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002857 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002858 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002859 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002860 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002861 }
2862
2863 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002864 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2865 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2866 return null;
2867 }
Derek Tan97ebb422014-09-05 16:55:38 -07002868
2869 String iccId = getIccId(subId);
2870 if (iccId != null) {
2871 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002872 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002873 }
Derek Tan97ebb422014-09-05 16:55:38 -07002874 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002875 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002876
2877 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002878 public String[] getMergedSubscriberIds(String callingPackage) {
2879 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2880 return null;
2881 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002882 final Context context = mPhone.getContext();
2883 final TelephonyManager tele = TelephonyManager.from(context);
2884 final SubscriptionManager sub = SubscriptionManager.from(context);
2885
2886 // Figure out what subscribers are currently active
2887 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002888 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2889 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2890 final long identity = Binder.clearCallingIdentity();
2891 try {
2892 final int[] subIds = sub.getActiveSubscriptionIdList();
2893 for (int subId : subIds) {
2894 activeSubscriberIds.add(tele.getSubscriberId(subId));
2895 }
2896 } finally {
2897 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002898 }
2899
2900 // First pass, find a number override for an active subscriber
2901 String mergeNumber = null;
2902 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2903 for (String key : prefs.keySet()) {
2904 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2905 final String subscriberId = (String) prefs.get(key);
2906 if (activeSubscriberIds.contains(subscriberId)) {
2907 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2908 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2909 mergeNumber = (String) prefs.get(numberKey);
2910 if (DBG_MERGE) {
2911 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2912 + " for active subscriber " + subscriberId);
2913 }
2914 if (!TextUtils.isEmpty(mergeNumber)) {
2915 break;
2916 }
2917 }
2918 }
2919 }
2920
2921 // Shortcut when no active merged subscribers
2922 if (TextUtils.isEmpty(mergeNumber)) {
2923 return null;
2924 }
2925
2926 // Second pass, find all subscribers under that line override
2927 final ArraySet<String> result = new ArraySet<>();
2928 for (String key : prefs.keySet()) {
2929 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2930 final String number = (String) prefs.get(key);
2931 if (mergeNumber.equals(number)) {
2932 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2933 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2934 final String subscriberId = (String) prefs.get(subscriberKey);
2935 if (!TextUtils.isEmpty(subscriberId)) {
2936 result.add(subscriberId);
2937 }
2938 }
2939 }
2940 }
2941
2942 final String[] resultArray = result.toArray(new String[result.size()]);
2943 Arrays.sort(resultArray);
2944 if (DBG_MERGE) {
2945 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2946 }
2947 return resultArray;
2948 }
2949
2950 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002951 public boolean setOperatorBrandOverride(int subId, String brand) {
2952 enforceCarrierPrivilege(subId);
2953 final Phone phone = getPhone(subId);
2954 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002955 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002956
2957 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002958 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002959 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2960 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002961 enforceCarrierPrivilege(subId);
2962 final Phone phone = getPhone(subId);
2963 if (phone == null) {
2964 return false;
2965 }
2966 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002967 cdmaNonRoamingList);
2968 }
2969
2970 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08002971 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05002972 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2973 enforceModifyPermission();
2974
2975 int returnValue = 0;
2976 try {
2977 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2978 if(result.exception == null) {
2979 if (result.result != null) {
2980 byte[] responseData = (byte[])(result.result);
2981 if(responseData.length > oemResp.length) {
2982 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2983 responseData.length + "bytes. Buffer Size is " +
2984 oemResp.length + "bytes.");
2985 }
2986 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2987 returnValue = responseData.length;
2988 }
2989 } else {
2990 CommandException ex = (CommandException) result.exception;
2991 returnValue = ex.getCommandError().ordinal();
2992 if(returnValue > 0) returnValue *= -1;
2993 }
2994 } catch (RuntimeException e) {
2995 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2996 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2997 if(returnValue > 0) returnValue *= -1;
2998 }
2999
3000 return returnValue;
3001 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003002
3003 @Override
3004 public void setRadioCapability(RadioAccessFamily[] rafs) {
3005 try {
3006 ProxyController.getInstance().setRadioCapability(rafs);
3007 } catch (RuntimeException e) {
3008 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3009 }
3010 }
3011
3012 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003013 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3014 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3015 return RadioAccessFamily.RAF_UNKNOWN;
3016 }
3017
Wink Saville5d475dd2014-10-17 15:00:58 -07003018 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3019 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003020
3021 @Override
3022 public void enableVideoCalling(boolean enable) {
3023 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003024 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003025 }
3026
3027 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003028 public boolean isVideoCallingEnabled(String callingPackage) {
3029 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3030 return false;
3031 }
3032
Andrew Lee77527ac2014-10-21 16:57:39 -07003033 // Check the user preference and the system-level IMS setting. Even if the user has
3034 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3035 // In the long run, we may instead need to check if there exists a connection service
3036 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003037 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3038 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003039 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003040 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003041
Andrew Leea1239f22015-03-02 17:44:07 -08003042 @Override
3043 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003044 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003045 }
3046
3047 @Override
3048 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003049 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003050 }
3051
Andrew Lee9431b832015-03-09 18:46:45 -07003052 @Override
3053 public boolean isTtyModeSupported() {
3054 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3055 TelephonyManager telephonyManager =
3056 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003057 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003058 }
3059
3060 @Override
3061 public boolean isHearingAidCompatibilitySupported() {
3062 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3063 }
3064
Sanket Padawe7310cc72015-01-14 09:53:20 -08003065 /**
3066 * Returns the unique device ID of phone, for example, the IMEI for
3067 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3068 *
3069 * <p>Requires Permission:
3070 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3071 */
3072 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003073 public String getDeviceId(String callingPackage) {
3074 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3075 return null;
3076 }
3077
Sanket Padawe7310cc72015-01-14 09:53:20 -08003078 final Phone phone = PhoneFactory.getPhone(0);
3079 if (phone != null) {
3080 return phone.getDeviceId();
3081 } else {
3082 return null;
3083 }
3084 }
3085
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003086 /*
3087 * {@hide}
3088 * Returns the IMS Registration Status
3089 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003090 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003091 public boolean isImsRegistered() {
3092 return mPhone.isImsRegistered();
3093 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003094
3095 @Override
3096 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3097 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3098 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003099
Nathan Haroldc55097a2015-03-11 18:14:50 -07003100 /*
3101 * {@hide}
3102 * Returns the IMS Registration Status
3103 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003104 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003105 return mPhone.isWifiCallingEnabled();
3106 }
3107
3108 /*
3109 * {@hide}
3110 * Returns the IMS Registration Status
3111 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003112 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003113 return mPhone.isVolteEnabled();
3114 }
Svet Ganovb320e182015-04-16 12:30:10 -07003115
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003116 /*
3117 * {@hide} Returns the IMS Registration Status
3118 */
3119 public boolean isVideoTelephonyAvailable() {
3120 return mPhone.isVideoEnabled();
3121 }
3122
Svet Ganovb320e182015-04-16 12:30:10 -07003123 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003124 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003125 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003126 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3127
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003128 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003129 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003130 } catch (SecurityException e) {
3131 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3132 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003133 }
Svet Ganovb320e182015-04-16 12:30:10 -07003134
3135 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3136 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3137 return false;
3138 }
3139
3140 return true;
3141 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003142
Makoto Onukifee69342015-06-29 14:44:50 -07003143 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003144 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003145 */
3146 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003147 // Default SMS app can always read it.
3148 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3149 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3150 return true;
3151 }
3152 try {
3153 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003154 } catch (SecurityException readPhoneStateSecurityException) {
3155 try {
3156 // Can be read with READ_SMS too.
3157 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3158 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3159 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3160 } catch (SecurityException readSmsSecurityException) {
3161 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3162 // permissions
3163 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3164 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3165 " or " + android.Manifest.permission.READ_SMS + ".");
3166 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003167 }
Makoto Onukifee69342015-06-29 14:44:50 -07003168 }
3169
Stuart Scott8eef64f2015-04-08 15:13:54 -07003170 @Override
3171 public void factoryReset(int subId) {
3172 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003173 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3174 return;
3175 }
3176
Svet Ganovcc087f82015-05-12 20:35:54 -07003177 final long identity = Binder.clearCallingIdentity();
3178 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003179 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3180 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003181 // Enable data
3182 setDataEnabled(subId, true);
3183 // Set network selection mode to automatic
3184 setNetworkSelectionModeAutomatic(subId);
3185 // Set preferred mobile network type to the best available
3186 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3187 // Turn off roaming
3188 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3189 }
3190 } finally {
3191 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003192 }
3193 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003194
3195 @Override
3196 public String getLocaleFromDefaultSim() {
3197 // We query all subscriptions instead of just the active ones, because
3198 // this might be called early on in the provisioning flow when the
3199 // subscriptions potentially aren't active yet.
3200 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3201 if (slist == null || slist.isEmpty()) {
3202 return null;
3203 }
3204
3205 // This function may be called very early, say, from the setup wizard, at
3206 // which point we won't have a default subscription set. If that's the case
3207 // we just choose the first, which will be valid in "most cases".
3208 final int defaultSubId = getDefaultSubscription();
3209 SubscriptionInfo info = null;
3210 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3211 info = slist.get(0);
3212 } else {
3213 for (SubscriptionInfo item : slist) {
3214 if (item.getSubscriptionId() == defaultSubId) {
3215 info = item;
3216 break;
3217 }
3218 }
3219
3220 if (info == null) {
3221 return null;
3222 }
3223 }
3224
3225 // Try and fetch the locale from the carrier properties or from the SIM language
3226 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003227 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003228 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003229 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003230 if (defaultPhone != null) {
3231 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3232 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003233 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003234 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3235 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003236 } else {
3237 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003238 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003239 }
3240 }
3241
Narayan Kamath011676f2015-07-29 12:04:08 +01003242 // The SIM language preferences only store a language (e.g. fr = French), not an
3243 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3244 // the SIM and carrier preferences does not include a country we add the country
3245 // determined from the SIM MCC to provide an exact locale.
3246 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003247 if (mccLocale != null) {
3248 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3249 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003250 }
3251
Tony Hill183b2de2015-06-24 14:53:58 +01003252 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003253 return null;
3254 }
3255
3256 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3257 final long identity = Binder.clearCallingIdentity();
3258 try {
3259 return mSubscriptionController.getAllSubInfoList(
3260 mPhone.getContext().getOpPackageName());
3261 } finally {
3262 Binder.restoreCallingIdentity(identity);
3263 }
3264 }
3265
3266 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3267 final long identity = Binder.clearCallingIdentity();
3268 try {
3269 return mSubscriptionController.getActiveSubscriptionInfoList(
3270 mPhone.getContext().getOpPackageName());
3271 } finally {
3272 Binder.restoreCallingIdentity(identity);
3273 }
3274 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003275
3276 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003277 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3278 * representing the state of the modem.
3279 *
3280 * NOTE: This clears the modem state, so there should only every be one caller.
3281 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003282 */
3283 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003284 public void requestModemActivityInfo(ResultReceiver result) {
3285 enforceModifyPermission();
3286
3287 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3288 Bundle bundle = new Bundle();
3289 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3290 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003291 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003292
3293 /**
3294 * {@hide}
3295 * Returns the service state information on specified subscription.
3296 */
3297 @Override
3298 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3299
3300 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3301 return null;
3302 }
3303
3304 final Phone phone = getPhone(subId);
3305 if (phone == null) {
3306 return null;
3307 }
3308
3309 return phone.getServiceState();
3310 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003311
3312 /**
3313 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3314 *
3315 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3316 * voicemail ringtone.
3317 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3318 * PhoneAccount.
3319 */
3320 @Override
3321 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3322 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3323 if (phone == null) {
3324 return null;
3325 }
3326
3327 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3328 }
3329
3330 /**
3331 * Returns whether vibration is set for voicemail notification in Phone settings.
3332 *
3333 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3334 * voicemail vibration setting.
3335 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3336 */
3337 @Override
3338 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3339 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3340 if (phone == null) {
3341 return false;
3342 }
3343
3344 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3345 }
3346
Youhan Wange64578a2016-05-02 15:32:42 -07003347 /**
3348 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3349 *
3350 * @throws SecurityException if the caller does not have the required permission
3351 */
3352 private void enforceReadPrivilegedPermission() {
3353 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3354 null);
3355 }
3356
3357 /**
3358 * Return the application ID for the app type.
3359 *
3360 * @param subId the subscription ID that this request applies to.
3361 * @param appType the uicc app type.
3362 * @return Application ID for specificied app type, or null if no uicc.
3363 */
3364 @Override
3365 public String getAidForAppType(int subId, int appType) {
3366 enforceReadPrivilegedPermission();
3367 Phone phone = getPhone(subId);
3368 if (phone == null) {
3369 return null;
3370 }
3371 String aid = null;
3372 try {
3373 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3374 .getApplicationByType(appType).getAid();
3375 } catch (Exception e) {
3376 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3377 }
3378 return aid;
3379 }
3380
Youhan Wang4001d252016-05-11 10:29:41 -07003381 /**
3382 * Return the Electronic Serial Number.
3383 *
3384 * @param subId the subscription ID that this request applies to.
3385 * @return ESN or null if error.
3386 */
3387 @Override
3388 public String getEsn(int subId) {
3389 enforceReadPrivilegedPermission();
3390 Phone phone = getPhone(subId);
3391 if (phone == null) {
3392 return null;
3393 }
3394 String esn = null;
3395 try {
3396 esn = phone.getEsn();
3397 } catch (Exception e) {
3398 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3399 }
3400 return esn;
3401 }
3402
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003403 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003404 * Return the Preferred Roaming List Version.
3405 *
3406 * @param subId the subscription ID that this request applies to.
3407 * @return PRLVersion or null if error.
3408 */
3409 @Override
3410 public String getCdmaPrlVersion(int subId) {
3411 enforceReadPrivilegedPermission();
3412 Phone phone = getPhone(subId);
3413 if (phone == null) {
3414 return null;
3415 }
3416 String cdmaPrlVersion = null;
3417 try {
3418 cdmaPrlVersion = phone.getCdmaPrlVersion();
3419 } catch (Exception e) {
3420 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3421 }
3422 return cdmaPrlVersion;
3423 }
3424
3425 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003426 * Get snapshot of Telephony histograms
3427 * @return List of Telephony histograms
3428 * @hide
3429 */
3430 @Override
3431 public List<TelephonyHistogram> getTelephonyHistograms() {
3432 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3433 return RIL.getTelephonyRILTimingHistograms();
3434 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003435
3436 /**
3437 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003438 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003439 * Require system privileges. In the future we may add this to carrier APIs.
3440 *
3441 * @return The number of carriers set successfully, should match length of carriers
3442 */
3443 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003444 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003445 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003446 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003447 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3448 return retVal[0];
3449 }
3450
3451 /**
3452 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003453 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003454 * Require system privileges. In the future we may add this to carrier APIs.
3455 *
3456 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3457 * means all carriers are allowed.
3458 */
3459 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003460 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003461 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003462 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003463 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3464 }
3465
fionaxu59545b42016-05-25 15:53:37 -07003466 /**
3467 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3468 * @param subId the subscription ID that this action applies to.
3469 * @param enabled control enable or disable metered apns.
3470 * {@hide}
3471 */
3472 @Override
3473 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3474 enforceModifyPermission();
3475 final Phone phone = getPhone(subId);
3476 if (phone == null) {
3477 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3478 return;
3479 }
3480 try {
3481 phone.carrierActionSetMeteredApnsEnabled(enabled);
3482 } catch (Exception e) {
3483 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3484 }
3485 }
3486
3487 /**
3488 * Action set from carrier signalling broadcast receivers to enable/disable radio
3489 * @param subId the subscription ID that this action applies to.
3490 * @param enabled control enable or disable radio.
3491 * {@hide}
3492 */
3493 @Override
3494 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3495 enforceModifyPermission();
3496 final Phone phone = getPhone(subId);
3497 if (phone == null) {
3498 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3499 return;
3500 }
3501 try {
3502 phone.carrierActionSetRadioEnabled(enabled);
3503 } catch (Exception e) {
3504 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3505 }
3506 }
3507
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003508 /**
3509 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3510 * bug report is being generated.
3511 */
3512 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003513 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003514 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3515 != PackageManager.PERMISSION_GRANTED) {
3516 writer.println("Permission Denial: can't dump Phone from pid="
3517 + Binder.getCallingPid()
3518 + ", uid=" + Binder.getCallingUid()
3519 + "without permission "
3520 + android.Manifest.permission.DUMP);
3521 return;
3522 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003523 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003524 }
Jack Yueb89b242016-06-22 13:27:47 -07003525
3526 /**
3527 * Get aggregated video call data usage from all subscriptions since boot.
3528 * @return total data usage in bytes
3529 * {@hide}
3530 */
3531 @Override
3532 public long getVtDataUsage() {
3533 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3534 null);
3535
3536 // NetworkStatsService keeps tracking the active network interface and identity. It will
3537 // record the delta with the corresponding network identity. What we need to do here is
3538 // returning total video call data usage from all subscriptions since boot.
3539
3540 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3541 // simultaneous VT calls.
3542 final Phone[] phones = PhoneFactory.getPhones();
3543 long total = 0;
3544 for (Phone phone : phones) {
3545 total += phone.getVtDataUsage();
3546 }
3547 return total;
3548 }
Jack Yu75ab2952016-07-08 14:29:33 -07003549
3550 /**
3551 * Policy control of data connection. Usually used when data limit is passed.
3552 * @param enabled True if enabling the data, otherwise disabling.
3553 * @param subId Subscription index
3554 * {@hide}
3555 */
3556 @Override
3557 public void setPolicyDataEnabled(boolean enabled, int subId) {
3558 enforceModifyPermission();
3559 Phone phone = getPhone(subId);
3560 if (phone != null) {
3561 phone.setPolicyDataEnabled(enabled);
3562 }
3563 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003564
3565 /**
3566 * Get Client request stats
3567 * @return List of Client Request Stats
3568 * @hide
3569 */
3570 @Override
3571 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3572 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3573 return null;
3574 }
3575
3576 Phone phone = getPhone(subId);
3577 if (phone != null) {
3578 return phone.getClientRequestStats();
3579 }
3580
3581 return null;
3582 }
3583
3584 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3585 if (workSource != null) {
3586 return workSource;
3587 }
3588
3589 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3590 workSource = new WorkSource(uid, packageName);
3591 return workSource;
3592 }
Jack Yu346de222017-02-16 15:32:43 -08003593
3594 /**
3595 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3596 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003597 * @param slotIndex SIM slot id.
Jack Yu346de222017-02-16 15:32:43 -08003598 * @param powerUp True if powering up the SIM, otherwise powering down
3599 *
3600 **/
3601 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003602 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yu346de222017-02-16 15:32:43 -08003603 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003604 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003605 if (subId == null || subId.length == 0) {
3606 return;
3607 }
3608
3609 final Phone phone = getPhone(subId[0]);
3610 if (phone != null) {
3611 phone.setSimPowerState(powerUp);
3612 }
3613 }
shuoqfef36a32017-01-10 13:02:18 -08003614
3615 /**
3616 * Check if phone is in emergency callback mode
3617 * @return true if phone is in emergency callback mode
3618 * @param subId sub id
3619 */
3620 public boolean getEmergencyCallbackMode(int subId) {
3621 final Phone phone = getPhone(subId);
3622 if (phone != null) {
3623 return phone.isInEcm();
3624 } else {
3625 return false;
3626 }
3627 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003628}