blob: c202c32f76be5a965fe71d9f342140d5387c7cd7 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Ta-wei Yen1b455992016-12-27 14:52:32 -080021import android.Manifest.permission;
Ta-wei Yen243b6372017-01-10 16:17:08 -080022import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.ActivityManager;
24import android.app.AppOpsManager;
Ta-wei Yen1b455992016-12-27 14:52:32 -080025import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070026import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.Context;
28import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070029import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080030import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070031import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.net.Uri;
33import android.os.AsyncResult;
34import android.os.Binder;
35import android.os.Bundle;
36import android.os.Handler;
37import android.os.Looper;
38import android.os.Message;
39import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070040import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.ServiceManager;
42import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070043import android.os.UserManager;
Sooraj Sasindran22882212016-07-18 11:57:25 -070044import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070045import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080046import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070047import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080048import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080049import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070050import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070051import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.CellInfo;
Sooraj Sasindran22882212016-07-18 11:57:25 -070053import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070054import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070055import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080056import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070057import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.telephony.ServiceState;
Ta-wei Yen1b455992016-12-27 14:52:32 -080059import android.telephony.SmsManager;
Nathan Harold46b42aa2017-03-10 19:38:22 -080060import android.telephony.SignalStrength;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080061import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080062import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070063import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070064import android.telephony.TelephonyManager;
65import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080067import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080069import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080070import android.util.Slog;
Ta-wei Yen1b455992016-12-27 14:52:32 -080071
Andrew Lee312e8172014-10-23 17:01:36 -070072import com.android.ims.ImsManager;
Brad Ebinger76681002017-01-23 13:50:20 -080073import com.android.ims.internal.IImsServiceController;
74import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070075import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070076import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070077import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070079import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080080import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010081import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070082import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070084import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070085import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070086import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070087import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070088import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080089import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070090import com.android.internal.telephony.uicc.IccIoResult;
91import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080092import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070093import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080094import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070095import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080096import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070097import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080098import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yen463efd12017-01-06 15:29:25 -080099import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yen1b455992016-12-27 14:52:32 -0800100
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700101import java.io.FileDescriptor;
102import java.io.PrintWriter;
Ta-wei Yen1b455992016-12-27 14:52:32 -0800103import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800105import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800106import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100107import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800108import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109
110/**
111 * Implementation of the ITelephony interface.
112 */
Santos Cordon117fee72014-05-16 17:56:12 -0700113public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114 private static final String LOG_TAG = "PhoneInterfaceManager";
115 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
116 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800117 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118
119 // Message codes used with mMainThreadHandler
120 private static final int CMD_HANDLE_PIN_MMI = 1;
121 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
122 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
123 private static final int CMD_ANSWER_RINGING_CALL = 4;
124 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700125 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
126 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700127 private static final int CMD_OPEN_CHANNEL = 9;
128 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
129 private static final int CMD_CLOSE_CHANNEL = 11;
130 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800131 private static final int CMD_NV_READ_ITEM = 13;
132 private static final int EVENT_NV_READ_ITEM_DONE = 14;
133 private static final int CMD_NV_WRITE_ITEM = 15;
134 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
135 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
136 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
137 private static final int CMD_NV_RESET_CONFIG = 19;
138 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800139 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
140 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
141 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
142 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800143 private static final int CMD_SEND_ENVELOPE = 25;
144 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700145 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
146 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
147 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
148 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
149 private static final int CMD_EXCHANGE_SIM_IO = 31;
150 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800151 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
152 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700153 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
154 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700155 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
156 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700157 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
158 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
159 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
160 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700161 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
162 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
163 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
164 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar7e856482016-10-14 19:37:38 -0700165 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800166 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
167 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168
169 /** The singleton instance. */
170 private static PhoneInterfaceManager sInstance;
171
Wink Saville3ab207e2014-11-20 13:07:20 -0800172 private PhoneGlobals mApp;
173 private Phone mPhone;
174 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700175 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800176 private AppOpsManager mAppOps;
177 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800178 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800179 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180
Derek Tan97ebb422014-09-05 16:55:38 -0700181 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
182 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800183 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700184
185 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700186 * A request object to use for transmitting data to an ICC.
187 */
188 private static final class IccAPDUArgument {
189 public int channel, cla, command, p1, p2, p3;
190 public String data;
191
192 public IccAPDUArgument(int channel, int cla, int command,
193 int p1, int p2, int p3, String data) {
194 this.channel = channel;
195 this.cla = cla;
196 this.command = command;
197 this.p1 = p1;
198 this.p2 = p2;
199 this.p3 = p3;
200 this.data = data;
201 }
202 }
203
204 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700205 * A request object to use for transmitting data to an ICC.
206 */
207 private static final class ManualNetworkSelectionArgument {
208 public OperatorInfo operatorInfo;
209 public boolean persistSelection;
210
211 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
212 this.operatorInfo = operatorInfo;
213 this.persistSelection = persistSelection;
214 }
215 }
216
217 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700218 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
219 * request after sending. The main thread will notify the request when it is complete.
220 */
221 private static final class MainThreadRequest {
222 /** The argument to use for the request */
223 public Object argument;
224 /** The result of the request that is run on the main thread */
225 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800226 // The subscriber id that this request applies to. Defaults to
227 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
228 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229
230 public MainThreadRequest(Object argument) {
231 this.argument = argument;
232 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800233
234 public MainThreadRequest(Object argument, Integer subId) {
235 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800236 if (subId != null) {
237 this.subId = subId;
238 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800239 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700240 }
241
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800242 private static final class IncomingThirdPartyCallArgs {
243 public final ComponentName component;
244 public final String callId;
245 public final String callerDisplayName;
246
247 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
248 String callerDisplayName) {
249 this.component = component;
250 this.callId = callId;
251 this.callerDisplayName = callerDisplayName;
252 }
253 }
254
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 /**
256 * A handler that processes messages on the main thread in the phone process. Since many
257 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
258 * inbound binder threads to the main thread in the phone process. The Binder thread
259 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
260 * on, which will be notified when the operation completes and will contain the result of the
261 * request.
262 *
263 * <p>If a MainThreadRequest object is provided in the msg.obj field,
264 * note that request.result must be set to something non-null for the calling thread to
265 * unblock.
266 */
267 private final class MainThreadHandler extends Handler {
268 @Override
269 public void handleMessage(Message msg) {
270 MainThreadRequest request;
271 Message onCompleted;
272 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800273 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700274 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700275
276 switch (msg.what) {
pkanwar7e856482016-10-14 19:37:38 -0700277 case CMD_HANDLE_USSD_REQUEST: {
278 request = (MainThreadRequest) msg.obj;
279 final Phone phone = getPhoneFromRequest(request);
280 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
281 String ussdRequest = ussdObject.first;
282 ResultReceiver wrappedCallback = ussdObject.second;
283 request.result = phone != null ?
284 phone.handleUssdRequest(ussdRequest, wrappedCallback)
285 :false;
286 // Wake up the requesting thread
287 synchronized (request) {
288 request.notifyAll();
289 }
290 break;
291 }
292
Yorke Lee716f67e2015-06-17 15:39:16 -0700293 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700295 final Phone phone = getPhoneFromRequest(request);
296 request.result = phone != null ?
297 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
298 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 // Wake up the requesting thread
300 synchronized (request) {
301 request.notifyAll();
302 }
303 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700304 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305
306 case CMD_HANDLE_NEIGHBORING_CELL:
307 request = (MainThreadRequest) msg.obj;
308 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
309 request);
Sooraj Sasindran22882212016-07-18 11:57:25 -0700310 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 break;
312
313 case EVENT_NEIGHBORING_CELL_DONE:
314 ar = (AsyncResult) msg.obj;
315 request = (MainThreadRequest) ar.userObj;
316 if (ar.exception == null && ar.result != null) {
317 request.result = ar.result;
318 } else {
319 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800320 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700321 }
322 // Wake up the requesting thread
323 synchronized (request) {
324 request.notifyAll();
325 }
326 break;
327
328 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700329 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800330 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700331 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 break;
333
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334 case CMD_END_CALL:
335 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800336 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700337 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700338 Phone phone = getPhone(end_subId);
339 if (phone == null) {
340 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
341 break;
342 }
343 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
345 // CDMA: If the user presses the Power button we treat it as
346 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700347 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
349 // GSM: End the call as per the Phone state
350 hungUp = PhoneUtils.hangup(mCM);
351 } else {
352 throw new IllegalStateException("Unexpected phone type: " + phoneType);
353 }
354 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
355 request.result = hungUp;
356 // Wake up the requesting thread
357 synchronized (request) {
358 request.notifyAll();
359 }
360 break;
361
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700362 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700363 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700364 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800365 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700366 if (uiccCard == null) {
367 loge("iccTransmitApduLogicalChannel: No UICC");
368 request.result = new IccIoResult(0x6F, 0, (byte[])null);
369 synchronized (request) {
370 request.notifyAll();
371 }
372 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700373 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
374 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700375 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700376 iccArgument.channel, iccArgument.cla, iccArgument.command,
377 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700378 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700379 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700380 break;
381
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700382 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700383 ar = (AsyncResult) msg.obj;
384 request = (MainThreadRequest) ar.userObj;
385 if (ar.exception == null && ar.result != null) {
386 request.result = ar.result;
387 } else {
388 request.result = new IccIoResult(0x6F, 0, (byte[])null);
389 if (ar.result == null) {
390 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800391 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700392 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800393 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700394 } else {
395 loge("iccTransmitApduLogicalChannel: Unknown exception");
396 }
397 }
398 synchronized (request) {
399 request.notifyAll();
400 }
401 break;
402
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700403 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
404 request = (MainThreadRequest) msg.obj;
405 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800406 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700407 if (uiccCard == null) {
408 loge("iccTransmitApduBasicChannel: No UICC");
409 request.result = new IccIoResult(0x6F, 0, (byte[])null);
410 synchronized (request) {
411 request.notifyAll();
412 }
413 } else {
414 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
415 request);
416 uiccCard.iccTransmitApduBasicChannel(
417 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
418 iccArgument.p3, iccArgument.data, onCompleted);
419 }
420 break;
421
422 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
423 ar = (AsyncResult) msg.obj;
424 request = (MainThreadRequest) ar.userObj;
425 if (ar.exception == null && ar.result != null) {
426 request.result = ar.result;
427 } else {
428 request.result = new IccIoResult(0x6F, 0, (byte[])null);
429 if (ar.result == null) {
430 loge("iccTransmitApduBasicChannel: Empty response");
431 } else if (ar.exception instanceof CommandException) {
432 loge("iccTransmitApduBasicChannel: CommandException: " +
433 ar.exception);
434 } else {
435 loge("iccTransmitApduBasicChannel: Unknown exception");
436 }
437 }
438 synchronized (request) {
439 request.notifyAll();
440 }
441 break;
442
443 case CMD_EXCHANGE_SIM_IO:
444 request = (MainThreadRequest) msg.obj;
445 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800446 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700447 if (uiccCard == null) {
448 loge("iccExchangeSimIO: No UICC");
449 request.result = new IccIoResult(0x6F, 0, (byte[])null);
450 synchronized (request) {
451 request.notifyAll();
452 }
453 } else {
454 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
455 request);
456 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
457 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
458 iccArgument.data, onCompleted);
459 }
460 break;
461
462 case EVENT_EXCHANGE_SIM_IO_DONE:
463 ar = (AsyncResult) msg.obj;
464 request = (MainThreadRequest) ar.userObj;
465 if (ar.exception == null && ar.result != null) {
466 request.result = ar.result;
467 } else {
468 request.result = new IccIoResult(0x6f, 0, (byte[])null);
469 }
470 synchronized (request) {
471 request.notifyAll();
472 }
473 break;
474
Derek Tan4d5e5c12014-02-04 11:54:58 -0800475 case CMD_SEND_ENVELOPE:
476 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800477 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700478 if (uiccCard == null) {
479 loge("sendEnvelopeWithStatus: No UICC");
480 request.result = new IccIoResult(0x6F, 0, (byte[])null);
481 synchronized (request) {
482 request.notifyAll();
483 }
484 } else {
485 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
486 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
487 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800488 break;
489
490 case EVENT_SEND_ENVELOPE_DONE:
491 ar = (AsyncResult) msg.obj;
492 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700493 if (ar.exception == null && ar.result != null) {
494 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800495 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700496 request.result = new IccIoResult(0x6F, 0, (byte[])null);
497 if (ar.result == null) {
498 loge("sendEnvelopeWithStatus: Empty response");
499 } else if (ar.exception instanceof CommandException) {
500 loge("sendEnvelopeWithStatus: CommandException: " +
501 ar.exception);
502 } else {
503 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
504 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800505 }
506 synchronized (request) {
507 request.notifyAll();
508 }
509 break;
510
Shishir Agrawal566b7612013-10-28 14:41:00 -0700511 case CMD_OPEN_CHANNEL:
512 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800513 uiccCard = getUiccCardFromRequest(request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800514 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700515 if (uiccCard == null) {
516 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800517 request.result = new IccOpenLogicalChannelResponse(-1,
518 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700519 synchronized (request) {
520 request.notifyAll();
521 }
522 } else {
523 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800524 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
525 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700526 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700527 break;
528
529 case EVENT_OPEN_CHANNEL_DONE:
530 ar = (AsyncResult) msg.obj;
531 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700532 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700533 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700534 int[] result = (int[]) ar.result;
535 int channelId = result[0];
536 byte[] selectResponse = null;
537 if (result.length > 1) {
538 selectResponse = new byte[result.length - 1];
539 for (int i = 1; i < result.length; ++i) {
540 selectResponse[i - 1] = (byte) result[i];
541 }
542 }
543 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700544 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700545 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700546 if (ar.result == null) {
547 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700548 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700549 if (ar.exception != null) {
550 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
551 }
552
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700553 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700554 if (ar.exception instanceof CommandException) {
555 CommandException.Error error =
556 ((CommandException) (ar.exception)).getCommandError();
557 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700558 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700559 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700560 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700561 }
562 }
563 openChannelResp = new IccOpenLogicalChannelResponse(
564 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700565 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700566 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700567 synchronized (request) {
568 request.notifyAll();
569 }
570 break;
571
572 case CMD_CLOSE_CHANNEL:
573 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800574 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700575 if (uiccCard == null) {
576 loge("iccCloseLogicalChannel: No UICC");
577 request.result = new IccIoResult(0x6F, 0, (byte[])null);
578 synchronized (request) {
579 request.notifyAll();
580 }
581 } else {
582 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
583 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
584 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700585 break;
586
587 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800588 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
589 break;
590
591 case CMD_NV_READ_ITEM:
592 request = (MainThreadRequest) msg.obj;
593 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
594 mPhone.nvReadItem((Integer) request.argument, onCompleted);
595 break;
596
597 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700598 ar = (AsyncResult) msg.obj;
599 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800600 if (ar.exception == null && ar.result != null) {
601 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700602 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800603 request.result = "";
604 if (ar.result == null) {
605 loge("nvReadItem: Empty response");
606 } else if (ar.exception instanceof CommandException) {
607 loge("nvReadItem: CommandException: " +
608 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700609 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800610 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700611 }
612 }
613 synchronized (request) {
614 request.notifyAll();
615 }
616 break;
617
Jake Hambye994d462014-02-03 13:10:13 -0800618 case CMD_NV_WRITE_ITEM:
619 request = (MainThreadRequest) msg.obj;
620 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
621 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
622 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
623 break;
624
625 case EVENT_NV_WRITE_ITEM_DONE:
626 handleNullReturnEvent(msg, "nvWriteItem");
627 break;
628
629 case CMD_NV_WRITE_CDMA_PRL:
630 request = (MainThreadRequest) msg.obj;
631 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
632 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
633 break;
634
635 case EVENT_NV_WRITE_CDMA_PRL_DONE:
636 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
637 break;
638
639 case CMD_NV_RESET_CONFIG:
640 request = (MainThreadRequest) msg.obj;
641 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
642 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
643 break;
644
645 case EVENT_NV_RESET_CONFIG_DONE:
646 handleNullReturnEvent(msg, "nvResetConfig");
647 break;
648
Jake Hamby7c27be32014-03-03 13:25:59 -0800649 case CMD_GET_PREFERRED_NETWORK_TYPE:
650 request = (MainThreadRequest) msg.obj;
651 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700652 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800653 break;
654
655 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
656 ar = (AsyncResult) msg.obj;
657 request = (MainThreadRequest) ar.userObj;
658 if (ar.exception == null && ar.result != null) {
659 request.result = ar.result; // Integer
660 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800661 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800662 if (ar.result == null) {
663 loge("getPreferredNetworkType: Empty response");
664 } else if (ar.exception instanceof CommandException) {
665 loge("getPreferredNetworkType: CommandException: " +
666 ar.exception);
667 } else {
668 loge("getPreferredNetworkType: Unknown exception");
669 }
670 }
671 synchronized (request) {
672 request.notifyAll();
673 }
674 break;
675
676 case CMD_SET_PREFERRED_NETWORK_TYPE:
677 request = (MainThreadRequest) msg.obj;
678 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
679 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700680 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800681 break;
682
683 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
684 handleNullReturnEvent(msg, "setPreferredNetworkType");
685 break;
686
Steven Liu4bf01bc2014-07-17 11:05:29 -0500687 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
688 request = (MainThreadRequest)msg.obj;
689 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
690 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
691 break;
692
693 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
694 ar = (AsyncResult)msg.obj;
695 request = (MainThreadRequest)ar.userObj;
696 request.result = ar;
697 synchronized (request) {
698 request.notifyAll();
699 }
700 break;
701
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800702 case CMD_SET_VOICEMAIL_NUMBER:
703 request = (MainThreadRequest) msg.obj;
704 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
705 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800706 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
707 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800708 break;
709
710 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
711 handleNullReturnEvent(msg, "setVoicemailNumber");
712 break;
713
Stuart Scott54788802015-03-30 13:18:01 -0700714 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
715 request = (MainThreadRequest) msg.obj;
716 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
717 request);
718 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
719 break;
720
721 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
722 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
723 break;
724
Shishir Agrawal302c8692015-06-19 13:49:39 -0700725 case CMD_PERFORM_NETWORK_SCAN:
726 request = (MainThreadRequest) msg.obj;
727 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
728 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
729 break;
730
731 case EVENT_PERFORM_NETWORK_SCAN_DONE:
732 ar = (AsyncResult) msg.obj;
733 request = (MainThreadRequest) ar.userObj;
734 CellNetworkScanResult cellScanResult;
735 if (ar.exception == null && ar.result != null) {
736 cellScanResult = new CellNetworkScanResult(
737 CellNetworkScanResult.STATUS_SUCCESS,
738 (List<OperatorInfo>) ar.result);
739 } else {
740 if (ar.result == null) {
741 loge("getCellNetworkScanResults: Empty response");
742 }
743 if (ar.exception != null) {
744 loge("getCellNetworkScanResults: Exception: " + ar.exception);
745 }
746 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
747 if (ar.exception instanceof CommandException) {
748 CommandException.Error error =
749 ((CommandException) (ar.exception)).getCommandError();
750 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
751 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
752 } else if (error == CommandException.Error.GENERIC_FAILURE) {
753 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
754 }
755 }
756 cellScanResult = new CellNetworkScanResult(errorCode, null);
757 }
758 request.result = cellScanResult;
759 synchronized (request) {
760 request.notifyAll();
761 }
762 break;
763
764 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
765 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700766 ManualNetworkSelectionArgument selArg =
767 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700768 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
769 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700770 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
771 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700772 break;
773
774 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
775 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
776 break;
777
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700778 case CMD_GET_MODEM_ACTIVITY_INFO:
779 request = (MainThreadRequest) msg.obj;
780 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700781 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700782 break;
783
784 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
785 ar = (AsyncResult) msg.obj;
786 request = (MainThreadRequest) ar.userObj;
787 if (ar.exception == null && ar.result != null) {
788 request.result = ar.result;
789 } else {
790 if (ar.result == null) {
791 loge("queryModemActivityInfo: Empty response");
792 } else if (ar.exception instanceof CommandException) {
793 loge("queryModemActivityInfo: CommandException: " +
794 ar.exception);
795 } else {
796 loge("queryModemActivityInfo: Unknown exception");
797 }
798 }
Amit Mahajand4766222016-01-28 15:28:28 -0800799 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
800 if (request.result == null) {
801 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
802 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700803 synchronized (request) {
804 request.notifyAll();
805 }
806 break;
807
Meng Wang1a7c35a2016-05-05 20:56:15 -0700808 case CMD_SET_ALLOWED_CARRIERS:
809 request = (MainThreadRequest) msg.obj;
810 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
811 mPhone.setAllowedCarriers(
812 (List<CarrierIdentifier>) request.argument,
813 onCompleted);
814 break;
815
816 case EVENT_SET_ALLOWED_CARRIERS_DONE:
817 ar = (AsyncResult) msg.obj;
818 request = (MainThreadRequest) ar.userObj;
819 if (ar.exception == null && ar.result != null) {
820 request.result = ar.result;
821 } else {
822 if (ar.result == null) {
823 loge("setAllowedCarriers: Empty response");
824 } else if (ar.exception instanceof CommandException) {
825 loge("setAllowedCarriers: CommandException: " +
826 ar.exception);
827 } else {
828 loge("setAllowedCarriers: Unknown exception");
829 }
830 }
831 // Result cannot be null. Return -1 on error.
832 if (request.result == null) {
833 request.result = new int[]{-1};
834 }
835 synchronized (request) {
836 request.notifyAll();
837 }
838 break;
839
840 case CMD_GET_ALLOWED_CARRIERS:
841 request = (MainThreadRequest) msg.obj;
842 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
843 mPhone.getAllowedCarriers(onCompleted);
844 break;
845
846 case EVENT_GET_ALLOWED_CARRIERS_DONE:
847 ar = (AsyncResult) msg.obj;
848 request = (MainThreadRequest) ar.userObj;
849 if (ar.exception == null && ar.result != null) {
850 request.result = ar.result;
851 } else {
852 if (ar.result == null) {
853 loge("getAllowedCarriers: Empty response");
854 } else if (ar.exception instanceof CommandException) {
855 loge("getAllowedCarriers: CommandException: " +
856 ar.exception);
857 } else {
858 loge("getAllowedCarriers: Unknown exception");
859 }
860 }
861 // Result cannot be null. Return empty list of CarrierIdentifier.
862 if (request.result == null) {
863 request.result = new ArrayList<CarrierIdentifier>(0);
864 }
865 synchronized (request) {
866 request.notifyAll();
867 }
868 break;
869
Nathan Haroldb3014052017-01-25 15:57:32 -0800870 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
871 ar = (AsyncResult) msg.obj;
872 request = (MainThreadRequest) ar.userObj;
873 if (ar.exception == null && ar.result != null) {
874 request.result = ar.result;
875 } else {
876 request.result = new IllegalArgumentException(
877 "Failed to retrieve Forbidden Plmns");
878 if (ar.result == null) {
879 loge("getForbiddenPlmns: Empty response");
880 } else {
881 loge("getForbiddenPlmns: Unknown exception");
882 }
883 }
884 synchronized (request) {
885 request.notifyAll();
886 }
887 break;
888
889 case CMD_GET_FORBIDDEN_PLMNS:
890 request = (MainThreadRequest) msg.obj;
891 uiccCard = getUiccCardFromRequest(request);
892 if (uiccCard == null) {
893 loge("getForbiddenPlmns() UiccCard is null");
894 request.result = new IllegalArgumentException(
895 "getForbiddenPlmns() UiccCard is null");
896 synchronized (request) {
897 request.notifyAll();
898 }
899 break;
900 }
901 Integer appType = (Integer) request.argument;
902 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
903 if (uiccApp == null) {
904 loge("getForbiddenPlmns() no app with specified type -- "
905 + appType);
906 request.result = new IllegalArgumentException("Failed to get UICC App");
907 synchronized (request) {
908 request.notifyAll();
909 }
910 break;
911 } else {
912 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
913 + " specified type -- " + appType);
914 }
915 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
916 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
917 onCompleted);
918 break;
919
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 default:
921 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
922 break;
923 }
924 }
Jake Hambye994d462014-02-03 13:10:13 -0800925
926 private void handleNullReturnEvent(Message msg, String command) {
927 AsyncResult ar = (AsyncResult) msg.obj;
928 MainThreadRequest request = (MainThreadRequest) ar.userObj;
929 if (ar.exception == null) {
930 request.result = true;
931 } else {
932 request.result = false;
933 if (ar.exception instanceof CommandException) {
934 loge(command + ": CommandException: " + ar.exception);
935 } else {
936 loge(command + ": Unknown exception");
937 }
938 }
939 synchronized (request) {
940 request.notifyAll();
941 }
942 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943 }
944
945 /**
946 * Posts the specified command to be executed on the main thread,
947 * waits for the request to complete, and returns the result.
948 * @see #sendRequestAsync
949 */
950 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800951 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700952 }
953
954 /**
955 * Posts the specified command to be executed on the main thread,
956 * waits for the request to complete, and returns the result.
957 * @see #sendRequestAsync
958 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800959 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
961 throw new RuntimeException("This method will deadlock if called from the main thread.");
962 }
963
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800964 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 Message msg = mMainThreadHandler.obtainMessage(command, request);
966 msg.sendToTarget();
967
968 // Wait for the request to complete
969 synchronized (request) {
970 while (request.result == null) {
971 try {
972 request.wait();
973 } catch (InterruptedException e) {
974 // Do nothing, go back and wait until the request is complete
975 }
976 }
977 }
978 return request.result;
979 }
980
981 /**
982 * Asynchronous ("fire and forget") version of sendRequest():
983 * Posts the specified command to be executed on the main thread, and
984 * returns immediately.
985 * @see #sendRequest
986 */
987 private void sendRequestAsync(int command) {
988 mMainThreadHandler.sendEmptyMessage(command);
989 }
990
991 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700992 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
993 * @see {@link #sendRequest(int,Object)}
994 */
995 private void sendRequestAsync(int command, Object argument) {
996 MainThreadRequest request = new MainThreadRequest(argument);
997 Message msg = mMainThreadHandler.obtainMessage(command, request);
998 msg.sendToTarget();
999 }
1000
1001 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001002 * Initialize the singleton PhoneInterfaceManager instance.
1003 * This is only done once, at startup, from PhoneApp.onCreate().
1004 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001005 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001006 synchronized (PhoneInterfaceManager.class) {
1007 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001008 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 } else {
1010 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1011 }
1012 return sInstance;
1013 }
1014 }
1015
1016 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001017 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001018 mApp = app;
1019 mPhone = phone;
1020 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001021 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1023 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001024 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001025 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001026 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001027
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001028 publish();
1029 }
1030
1031 private void publish() {
1032 if (DBG) log("publish: " + this);
1033
1034 ServiceManager.addService("phone", this);
1035 }
1036
Stuart Scott584921c2015-01-15 17:10:34 -08001037 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001038 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1039 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001040 }
1041
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001042 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1043 Phone phone = getPhoneFromRequest(request);
1044 return phone == null ? null :
1045 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1046 }
1047
Wink Saville36469e72014-06-11 15:17:00 -07001048 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001049 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001050 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001051 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001052 //
1053 // Implementation of the ITelephony interface.
1054 //
1055
1056 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001057 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001058 }
1059
Wink Savilleb564aae2014-10-23 10:18:09 -07001060 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001061 if (DBG) log("dial: " + number);
1062 // No permission check needed here: This is just a wrapper around the
1063 // ACTION_DIAL intent, which is available to any app since it puts up
1064 // the UI before it does anything.
1065
1066 String url = createTelUrl(number);
1067 if (url == null) {
1068 return;
1069 }
1070
1071 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001072 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1074 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1075 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1076 mApp.startActivity(intent);
1077 }
1078 }
1079
1080 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001081 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001082 }
1083
Wink Savilleb564aae2014-10-23 10:18:09 -07001084 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001085 if (DBG) log("call: " + number);
1086
1087 // This is just a wrapper around the ACTION_CALL intent, but we still
1088 // need to do a permission check since we're calling startActivity()
1089 // from the context of the phone app.
1090 enforceCallPermission();
1091
1092 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1093 != AppOpsManager.MODE_ALLOWED) {
1094 return;
1095 }
1096
1097 String url = createTelUrl(number);
1098 if (url == null) {
1099 return;
1100 }
1101
Wink Saville08874612014-08-31 19:19:58 -07001102 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001103 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001104 if (slist != null) {
1105 for (SubscriptionInfo subInfoRecord : slist) {
1106 if (subInfoRecord.getSubscriptionId() == subId) {
1107 isValid = true;
1108 break;
1109 }
Wink Saville08874612014-08-31 19:19:58 -07001110 }
1111 }
1112 if (isValid == false) {
1113 return;
1114 }
1115
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001116 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001117 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1119 mApp.startActivity(intent);
1120 }
1121
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 /**
1123 * End a call based on call state
1124 * @return true is a call was ended
1125 */
1126 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001127 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001128 }
1129
1130 /**
1131 * End a call based on the call state of the subId
1132 * @return true is a call was ended
1133 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001134 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001135 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001136 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001137 }
1138
1139 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001140 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001141 }
1142
Wink Savilleb564aae2014-10-23 10:18:09 -07001143 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001144 if (DBG) log("answerRingingCall...");
1145 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1146 // but that can probably wait till the big TelephonyManager API overhaul.
1147 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1148 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001149 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001150 }
1151
1152 /**
1153 * Make the actual telephony calls to implement answerRingingCall().
1154 * This should only be called from the main thread of the Phone app.
1155 * @see #answerRingingCall
1156 *
1157 * TODO: it would be nice to return true if we answered the call, or
1158 * false if there wasn't actually a ringing incoming call, or some
1159 * other error occurred. (In other words, pass back the return value
1160 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1161 * But that would require calling this method via sendRequest() rather
1162 * than sendRequestAsync(), and right now we don't actually *need* that
1163 * return value, so let's just return void for now.
1164 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001165 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001166 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001168 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1169 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 if (hasActiveCall && hasHoldingCall) {
1171 // Both lines are in use!
1172 // TODO: provide a flag to let the caller specify what
1173 // policy to use if both lines are in use. (The current
1174 // behavior is hardwired to "answer incoming, end ongoing",
1175 // which is how the CALL button is specced to behave.)
1176 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1177 return;
1178 } else {
1179 // answerCall() will automatically hold the current active
1180 // call, if there is one.
1181 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1182 return;
1183 }
1184 } else {
1185 // No call was ringing.
1186 return;
1187 }
1188 }
1189
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001190 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001191 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001192 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001193 public void silenceRinger() {
1194 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 }
1196
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001197 @Override
1198 public boolean isOffhook(String callingPackage) {
1199 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001200 }
1201
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001202 @Override
1203 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1204 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1205 return false;
1206 }
1207
Sanket Padawe356d7632015-06-22 14:03:32 -07001208 final Phone phone = getPhone(subId);
1209 if (phone != null) {
1210 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1211 } else {
1212 return false;
1213 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001214 }
1215
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001216 @Override
1217 public boolean isRinging(String callingPackage) {
1218 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001219 }
1220
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001221 @Override
1222 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1223 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1224 return false;
1225 }
1226
Sanket Padawe356d7632015-06-22 14:03:32 -07001227 final Phone phone = getPhone(subId);
1228 if (phone != null) {
1229 return (phone.getState() == PhoneConstants.State.RINGING);
1230 } else {
1231 return false;
1232 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 }
1234
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001235 @Override
1236 public boolean isIdle(String callingPackage) {
1237 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001238 }
1239
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001240 @Override
1241 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1242 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1243 return false;
1244 }
1245
Sanket Padawe356d7632015-06-22 14:03:32 -07001246 final Phone phone = getPhone(subId);
1247 if (phone != null) {
1248 return (phone.getState() == PhoneConstants.State.IDLE);
1249 } else {
1250 return false;
1251 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001252 }
1253
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001254 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001255 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001256 }
1257
Wink Savilleb564aae2014-10-23 10:18:09 -07001258 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001259 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001260 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1261 }
1262
1263 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001264 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001265 }
1266
Wink Savilleb564aae2014-10-23 10:18:09 -07001267 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001268 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001269 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1270 }
1271
1272 /** {@hide} */
1273 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001274 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001275 }
1276
Wink Savilleb564aae2014-10-23 10:18:09 -07001277 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001278 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001279 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001280 checkSimPin.start();
1281 return checkSimPin.unlockSim(null, pin);
1282 }
1283
Wink Saville9de0f752013-10-22 19:04:03 -07001284 /** {@hide} */
1285 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001286 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001287 }
1288
Wink Savilleb564aae2014-10-23 10:18:09 -07001289 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001290 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001291 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001292 checkSimPuk.start();
1293 return checkSimPuk.unlockSim(puk, pin);
1294 }
1295
1296 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001297 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001298 * a synchronous one.
1299 */
1300 private static class UnlockSim extends Thread {
1301
1302 private final IccCard mSimCard;
1303
1304 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001305 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1306 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001307
1308 // For replies from SimCard interface
1309 private Handler mHandler;
1310
1311 // For async handler to identify request type
1312 private static final int SUPPLY_PIN_COMPLETE = 100;
1313
1314 public UnlockSim(IccCard simCard) {
1315 mSimCard = simCard;
1316 }
1317
1318 @Override
1319 public void run() {
1320 Looper.prepare();
1321 synchronized (UnlockSim.this) {
1322 mHandler = new Handler() {
1323 @Override
1324 public void handleMessage(Message msg) {
1325 AsyncResult ar = (AsyncResult) msg.obj;
1326 switch (msg.what) {
1327 case SUPPLY_PIN_COMPLETE:
1328 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1329 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001330 mRetryCount = msg.arg1;
1331 if (ar.exception != null) {
1332 if (ar.exception instanceof CommandException &&
1333 ((CommandException)(ar.exception)).getCommandError()
1334 == CommandException.Error.PASSWORD_INCORRECT) {
1335 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1336 } else {
1337 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1338 }
1339 } else {
1340 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1341 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001342 mDone = true;
1343 UnlockSim.this.notifyAll();
1344 }
1345 break;
1346 }
1347 }
1348 };
1349 UnlockSim.this.notifyAll();
1350 }
1351 Looper.loop();
1352 }
1353
1354 /*
1355 * Use PIN or PUK to unlock SIM card
1356 *
1357 * If PUK is null, unlock SIM card with PIN
1358 *
1359 * If PUK is not null, unlock SIM card with PUK and set PIN code
1360 */
Wink Saville9de0f752013-10-22 19:04:03 -07001361 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362
1363 while (mHandler == null) {
1364 try {
1365 wait();
1366 } catch (InterruptedException e) {
1367 Thread.currentThread().interrupt();
1368 }
1369 }
1370 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1371
1372 if (puk == null) {
1373 mSimCard.supplyPin(pin, callback);
1374 } else {
1375 mSimCard.supplyPuk(puk, pin, callback);
1376 }
1377
1378 while (!mDone) {
1379 try {
1380 Log.d(LOG_TAG, "wait for done");
1381 wait();
1382 } catch (InterruptedException e) {
1383 // Restore the interrupted status
1384 Thread.currentThread().interrupt();
1385 }
1386 }
1387 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001388 int[] resultArray = new int[2];
1389 resultArray[0] = mResult;
1390 resultArray[1] = mRetryCount;
1391 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 }
1393 }
1394
1395 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001396 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001397
1398 }
1399
Wink Savilleb564aae2014-10-23 10:18:09 -07001400 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001401 // No permission check needed here: this call is harmless, and it's
1402 // needed for the ServiceState.requestStateUpdate() call (which is
1403 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001404 final Phone phone = getPhone(subId);
1405 if (phone != null) {
1406 phone.updateServiceLocation();
1407 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 }
1409
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001410 @Override
1411 public boolean isRadioOn(String callingPackage) {
1412 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001413 }
1414
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001415 @Override
1416 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1417 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1418 return false;
1419 }
1420 return isRadioOnForSubscriber(subId);
1421 }
1422
1423 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001424 final Phone phone = getPhone(subId);
1425 if (phone != null) {
1426 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1427 } else {
1428 return false;
1429 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001430 }
1431
1432 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001433 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001434
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 }
Wink Saville36469e72014-06-11 15:17:00 -07001436
Wink Savilleb564aae2014-10-23 10:18:09 -07001437 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001439 final Phone phone = getPhone(subId);
1440 if (phone != null) {
1441 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1442 }
Wink Saville36469e72014-06-11 15:17:00 -07001443 }
1444
1445 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001446 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001447 }
1448
Wink Savilleb564aae2014-10-23 10:18:09 -07001449 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001450 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001451 final Phone phone = getPhone(subId);
1452 if (phone == null) {
1453 return false;
1454 }
1455 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001456 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001457 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001458 }
1459 return true;
1460 }
Wink Saville36469e72014-06-11 15:17:00 -07001461
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001462 public boolean needMobileRadioShutdown() {
1463 /*
1464 * If any of the Radios are available, it will need to be
1465 * shutdown. So return true if any Radio is available.
1466 */
1467 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1468 Phone phone = PhoneFactory.getPhone(i);
1469 if (phone != null && phone.isRadioAvailable()) return true;
1470 }
1471 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1472 return false;
1473 }
1474
1475 public void shutdownMobileRadios() {
1476 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1477 logv("Shutting down Phone " + i);
1478 shutdownRadioUsingPhoneId(i);
1479 }
1480 }
1481
1482 private void shutdownRadioUsingPhoneId(int phoneId) {
1483 enforceModifyPermission();
1484 Phone phone = PhoneFactory.getPhone(phoneId);
1485 if (phone != null && phone.isRadioAvailable()) {
1486 phone.shutdownRadio();
1487 }
1488 }
1489
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001490 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001491 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1492 if (defaultPhone != null) {
1493 defaultPhone.setRadioPower(turnOn);
1494 return true;
1495 } else {
1496 loge("There's no default phone.");
1497 return false;
1498 }
Wink Saville36469e72014-06-11 15:17:00 -07001499 }
1500
Wink Savilleb564aae2014-10-23 10:18:09 -07001501 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001502 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001503 final Phone phone = getPhone(subId);
1504 if (phone != null) {
1505 phone.setRadioPower(turnOn);
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 enableDataConnectivity() {
1515 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001516 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001517 final Phone phone = getPhone(subId);
1518 if (phone != null) {
1519 phone.setDataEnabled(true);
1520 return true;
1521 } else {
1522 return false;
1523 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001524 }
1525
Wink Saville36469e72014-06-11 15:17:00 -07001526 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001527 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001528 public boolean disableDataConnectivity() {
1529 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001530 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001531 final Phone phone = getPhone(subId);
1532 if (phone != null) {
1533 phone.setDataEnabled(false);
1534 return true;
1535 } else {
1536 return false;
1537 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001538 }
1539
Wink Saville36469e72014-06-11 15:17:00 -07001540 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001541 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001542 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001543 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001544 final Phone phone = getPhone(subId);
1545 if (phone != null) {
1546 return phone.isDataConnectivityPossible();
1547 } else {
1548 return false;
1549 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 }
1551
1552 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001553 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001554 }
1555
pkanwarf8520782016-11-06 20:37:09 -08001556 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar7e856482016-10-14 19:37:38 -07001557 enforceCallPermission();
pkanwar7e856482016-10-14 19:37:38 -07001558 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1559 return;
1560 }
1561 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1562 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1563 };
1564
Wink Savilleb564aae2014-10-23 10:18:09 -07001565 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001566 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001567 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1568 return false;
1569 }
Wink Saville36469e72014-06-11 15:17:00 -07001570 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001571 }
1572
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001573 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001574 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001575 }
1576
Sanket Padawe5780e442017-03-20 15:04:47 -07001577 public int getCallStateForSlot(int slotIndex) {
1578 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001579 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1580 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001581 }
1582
Sanket Padawe356d7632015-06-22 14:03:32 -07001583 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001584 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001585 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001586 if (phone != null) {
1587 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1588 } else {
1589 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1590 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001591 }
1592
Sanket Padawe356d7632015-06-22 14:03:32 -07001593 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001594 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001595 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001596 if (phone != null) {
1597 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1598 } else {
1599 return TelephonyManager.DATA_ACTIVITY_NONE;
1600 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001601 }
1602
1603 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001604 public Bundle getCellLocation(String callingPackage) {
1605 enforceFineOrCoarseLocationPermission("getCellLocation");
1606
1607 // OP_COARSE_LOCATION controls both fine and coarse location.
1608 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1609 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001610 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001611 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001612 }
1613
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001614 if (checkIfCallerIsSelfOrForegroundUser() ||
1615 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001616 if (DBG_LOC) log("getCellLocation: is active user");
1617 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001618 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001619 if (phone == null) {
1620 return null;
1621 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07001622
1623 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1624 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001625 return data;
1626 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001627 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001628 return null;
1629 }
1630 }
1631
Svetoslav64fad262015-04-14 14:35:21 -07001632 private void enforceFineOrCoarseLocationPermission(String message) {
1633 try {
1634 mApp.enforceCallingOrSelfPermission(
1635 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1636 } catch (SecurityException e) {
1637 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1638 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1639 // is the weaker precondition
1640 mApp.enforceCallingOrSelfPermission(
1641 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1642 }
1643 }
1644
1645
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001646 @Override
1647 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001648 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001649 }
1650
Sanket Padawe356d7632015-06-22 14:03:32 -07001651 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001652 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001653 mApp.enforceCallingOrSelfPermission(
1654 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001655 final Phone phone = getPhone(subId);
1656 if (phone != null) {
1657 phone.enableLocationUpdates();
1658 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001659 }
1660
1661 @Override
1662 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001663 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001664 }
1665
Sanket Padawe356d7632015-06-22 14:03:32 -07001666 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001667 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001668 mApp.enforceCallingOrSelfPermission(
1669 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001670 final Phone phone = getPhone(subId);
1671 if (phone != null) {
1672 phone.disableLocationUpdates();
1673 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001674 }
1675
1676 @Override
1677 @SuppressWarnings("unchecked")
1678 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001679 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
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
1687 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1688 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1689 return null;
1690 }
Svetoslav64fad262015-04-14 14:35:21 -07001691
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001692 if (checkIfCallerIsSelfOrForegroundUser() ||
1693 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001694 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1695
1696 ArrayList<NeighboringCellInfo> cells = null;
1697
Sooraj Sasindran22882212016-07-18 11:57:25 -07001698 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001699 try {
1700 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran22882212016-07-18 11:57:25 -07001701 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001702 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001703 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001704 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001705 }
1706 return cells;
1707 } else {
1708 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1709 return null;
1710 }
1711 }
1712
1713
1714 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001715 public List<CellInfo> getAllCellInfo(String callingPackage) {
1716 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1717
1718 // OP_COARSE_LOCATION controls both fine and coarse location.
1719 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1720 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1721 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001722 }
1723
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001724 if (checkIfCallerIsSelfOrForegroundUser() ||
1725 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001726 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran22882212016-07-18 11:57:25 -07001727 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001728 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1729 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran22882212016-07-18 11:57:25 -07001730 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1731 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001732 }
1733 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001734 } else {
1735 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1736 return null;
1737 }
1738 }
1739
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001740 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001741 public void setCellInfoListRate(int rateInMillis) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001742 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001743 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1744 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001745 }
1746
Shishir Agrawala9f32182016-04-12 12:00:16 -07001747 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001748 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001749 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1750 return null;
1751 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001752 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001753 return phone == null ? null : phone.getImei();
1754 }
1755
1756 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001757 public String getMeidForSlot(int slotIndex, String callingPackage) {
1758 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1759 return null;
1760 }
1761 Phone phone = PhoneFactory.getPhone(slotIndex);
1762 return phone == null ? null : phone.getMeid();
1763 }
1764
1765 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001766 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001767 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1768 return null;
1769 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001770 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001771 return phone == null ? null : phone.getDeviceSvn();
1772 }
1773
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001774 //
1775 // Internal helper methods.
1776 //
1777
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001778 /**
1779 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1780 */
1781 private boolean checkCallerInteractAcrossUsersFull() {
1782 return mPhone.getContext().checkCallingOrSelfPermission(
1783 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1784 == PackageManager.PERMISSION_GRANTED;
1785 }
1786
Jake Hambye994d462014-02-03 13:10:13 -08001787 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001788 boolean ok;
1789
1790 boolean self = Binder.getCallingUid() == Process.myUid();
1791 if (!self) {
1792 // Get the caller's user id then clear the calling identity
1793 // which will be restored in the finally clause.
1794 int callingUser = UserHandle.getCallingUserId();
1795 long ident = Binder.clearCallingIdentity();
1796
1797 try {
1798 // With calling identity cleared the current user is the foreground user.
1799 int foregroundUser = ActivityManager.getCurrentUser();
1800 ok = (foregroundUser == callingUser);
1801 if (DBG_LOC) {
1802 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1803 + " callingUser=" + callingUser + " ok=" + ok);
1804 }
1805 } catch (Exception ex) {
1806 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1807 ok = false;
1808 } finally {
1809 Binder.restoreCallingIdentity(ident);
1810 }
1811 } else {
1812 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1813 ok = true;
1814 }
1815 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1816 return ok;
1817 }
1818
1819 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001820 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1821 *
1822 * @throws SecurityException if the caller does not have the required permission
1823 */
1824 private void enforceModifyPermission() {
1825 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1826 }
1827
1828 /**
Junda Liua2e36012014-07-09 18:30:01 -07001829 * Make sure either system app or the caller has carrier privilege.
1830 *
1831 * @throws SecurityException if the caller does not have the required permission/privilege
1832 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001833 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001834 int permission = mApp.checkCallingOrSelfPermission(
1835 android.Manifest.permission.MODIFY_PHONE_STATE);
1836 if (permission == PackageManager.PERMISSION_GRANTED) {
1837 return;
1838 }
1839
1840 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001841 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001842 }
1843
1844 /**
1845 * Make sure the caller has carrier privilege.
1846 *
1847 * @throws SecurityException if the caller does not have the required permission
1848 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001849 private void enforceCarrierPrivilege(int subId) {
1850 if (getCarrierPrivilegeStatus(subId) !=
1851 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001852 loge("No Carrier Privilege.");
1853 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001854 }
1855 }
1856
1857 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001858 * Make sure the caller has the CALL_PHONE permission.
1859 *
1860 * @throws SecurityException if the caller does not have the required permission
1861 */
1862 private void enforceCallPermission() {
1863 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1864 }
1865
Stuart Scott8eef64f2015-04-08 15:13:54 -07001866 private void enforceConnectivityInternalPermission() {
1867 mApp.enforceCallingOrSelfPermission(
1868 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1869 "ConnectivityService");
1870 }
1871
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001872 private String createTelUrl(String number) {
1873 if (TextUtils.isEmpty(number)) {
1874 return null;
1875 }
1876
Jake Hambye994d462014-02-03 13:10:13 -08001877 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001878 }
1879
Ihab Awadf9e92732013-12-05 18:02:52 -08001880 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001881 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1882 }
1883
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001884 private static void logv(String msg) {
1885 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1886 }
1887
Ihab Awadf9e92732013-12-05 18:02:52 -08001888 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001889 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1890 }
1891
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001892 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001893 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001894 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001895 }
1896
Sanket Padawe356d7632015-06-22 14:03:32 -07001897 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001898 public int getActivePhoneTypeForSlot(int slotIndex) {
1899 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001900 if (phone == null) {
1901 return PhoneConstants.PHONE_TYPE_NONE;
1902 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001903 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001904 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001905 }
1906
1907 /**
1908 * Returns the CDMA ERI icon index to display
1909 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001910 @Override
1911 public int getCdmaEriIconIndex(String callingPackage) {
1912 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001913 }
1914
Sanket Padawe356d7632015-06-22 14:03:32 -07001915 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001916 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1917 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1918 return -1;
1919 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001920 final Phone phone = getPhone(subId);
1921 if (phone != null) {
1922 return phone.getCdmaEriIconIndex();
1923 } else {
1924 return -1;
1925 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001926 }
1927
1928 /**
1929 * Returns the CDMA ERI icon mode,
1930 * 0 - ON
1931 * 1 - FLASHING
1932 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001933 @Override
1934 public int getCdmaEriIconMode(String callingPackage) {
1935 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001936 }
1937
Sanket Padawe356d7632015-06-22 14:03:32 -07001938 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001939 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1940 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1941 return -1;
1942 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001943 final Phone phone = getPhone(subId);
1944 if (phone != null) {
1945 return phone.getCdmaEriIconMode();
1946 } else {
1947 return -1;
1948 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001949 }
1950
1951 /**
1952 * Returns the CDMA ERI text,
1953 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001954 @Override
1955 public String getCdmaEriText(String callingPackage) {
1956 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001957 }
1958
Sanket Padawe356d7632015-06-22 14:03:32 -07001959 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001960 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1961 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1962 return null;
1963 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001964 final Phone phone = getPhone(subId);
1965 if (phone != null) {
1966 return phone.getCdmaEriText();
1967 } else {
1968 return null;
1969 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001970 }
1971
1972 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001973 * Returns the CDMA MDN.
1974 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001975 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001976 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001977 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001978 final Phone phone = getPhone(subId);
1979 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1980 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001981 } else {
1982 return null;
1983 }
1984 }
1985
1986 /**
1987 * Returns the CDMA MIN.
1988 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001989 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001990 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001991 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001992 final Phone phone = getPhone(subId);
1993 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1994 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001995 } else {
1996 return null;
1997 }
1998 }
1999
2000 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002001 * Returns true if CDMA provisioning needs to run.
2002 */
2003 public boolean needsOtaServiceProvisioning() {
2004 return mPhone.needsOtaServiceProvisioning();
2005 }
2006
2007 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002008 * Sets the voice mail number of a given subId.
2009 */
2010 @Override
2011 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002012 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002013 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2014 new Pair<String, String>(alphaTag, number), new Integer(subId));
2015 return success;
2016 }
2017
Ta-wei Yen87c49842016-05-13 21:19:52 -07002018 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07002019 public void setVisualVoicemailEnabled(String callingPackage,
2020 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
2021 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2022 if (!TextUtils.equals(callingPackage,
2023 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2024 enforceModifyPermissionOrCarrierPrivilege(
2025 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
2026 }
2027 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2028 }
2029
2030 @Override
2031 public boolean isVisualVoicemailEnabled(String callingPackage,
2032 PhoneAccountHandle phoneAccountHandle) {
2033 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2034 return false;
2035 }
2036 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2037 }
2038
2039 @Override
Ta-wei Yen12551102017-03-06 16:00:44 -08002040 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yen243b6372017-01-10 16:17:08 -08002041 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2042 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2043 return null;
2044 }
Ta-wei Yen243b6372017-01-10 16:17:08 -08002045 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2046 }
2047
2048 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002049 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2050 VisualVoicemailSmsFilterSettings settings) {
2051 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002052 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002053 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2054 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002055 }
2056
2057 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002058 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2059 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002060 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002061 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002062 }
2063
2064 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002065 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2066 String callingPackage, int subId) {
2067 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002068 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002069 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002070 }
2071
2072 @Override
Ta-wei Yen1b455992016-12-27 14:52:32 -08002073 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002074 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002075 return VisualVoicemailSmsFilterConfig
Ta-wei Yen1b455992016-12-27 14:52:32 -08002076 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2077 }
2078
2079 @Override
2080 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2081 String number, int port, String text, PendingIntent sentIntent) {
2082 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen463efd12017-01-06 15:29:25 -08002083 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen1b455992016-12-27 14:52:32 -08002084 enforceSendSmsPermission();
2085 // Make the calls as the phone process.
2086 final long identity = Binder.clearCallingIdentity();
2087 try {
2088 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2089 if (port == 0) {
2090 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2091 sentIntent, null, false);
2092 } else {
2093 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2094 smsManager.sendDataMessageWithSelfPermissions(number, null,
2095 (short) port, data, sentIntent, null);
2096 }
2097 } finally {
2098 Binder.restoreCallingIdentity(identity);
2099 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002100 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002101 /**
fionaxuef039d42016-11-14 13:36:14 -08002102 * Sets the voice activation state of a given subId.
2103 */
2104 @Override
2105 public void setVoiceActivationState(int subId, int activationState) {
2106 enforceModifyPermissionOrCarrierPrivilege(subId);
2107 final Phone phone = getPhone(subId);
2108 if (phone != null) {
2109 phone.setVoiceActivationState(activationState);
2110 } else {
2111 loge("setVoiceActivationState fails with invalid subId: " + subId);
2112 }
2113 }
2114
2115 /**
2116 * Sets the data activation state of a given subId.
2117 */
2118 @Override
2119 public void setDataActivationState(int subId, int activationState) {
2120 enforceModifyPermissionOrCarrierPrivilege(subId);
2121 final Phone phone = getPhone(subId);
2122 if (phone != null) {
2123 phone.setDataActivationState(activationState);
2124 } else {
2125 loge("setVoiceActivationState fails with invalid subId: " + subId);
2126 }
2127 }
2128
2129 /**
2130 * Returns the voice activation state of a given subId.
2131 */
2132 @Override
2133 public int getVoiceActivationState(int subId, String callingPackage) {
2134 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2135 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2136 }
2137 final Phone phone = getPhone(subId);
2138 if (phone != null) {
2139 return phone.getVoiceActivationState();
2140 } else {
2141 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2142 }
2143 }
2144
2145 /**
2146 * Returns the data activation state of a given subId.
2147 */
2148 @Override
2149 public int getDataActivationState(int subId, String callingPackage) {
2150 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2151 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2152 }
2153 final Phone phone = getPhone(subId);
2154 if (phone != null) {
2155 return phone.getDataActivationState();
2156 } else {
2157 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2158 }
2159 }
2160
2161 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002162 * Returns the unread count of voicemails
2163 */
2164 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002165 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002166 }
2167
2168 /**
2169 * Returns the unread count of voicemails for a subId
2170 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002171 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002172 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002173 final Phone phone = getPhone(subId);
2174 if (phone != null) {
2175 return phone.getVoiceMessageCount();
2176 } else {
2177 return 0;
2178 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002179 }
2180
2181 /**
pkanwar8efe6572017-01-19 13:43:16 -08002182 * returns true, if the device is in a state where both voice and data
2183 * are supported simultaneously. This can change based on location or network condition.
2184 */
2185 @Override
2186 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2187 final Phone phone = getPhone(subId);
2188 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2189 }
2190
2191 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002192 * Returns the data network type.
2193 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002194 *
2195 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2196 */
2197 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002198 public int getNetworkType() {
2199 final Phone phone = getPhone(getDefaultSubscription());
2200 if (phone != null) {
2201 return phone.getServiceState().getDataNetworkType();
2202 } else {
2203 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2204 }
Wink Saville36469e72014-06-11 15:17:00 -07002205 }
2206
2207 /**
2208 * Returns the network type for a subId
2209 */
2210 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002211 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2212 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2213 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2214 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002215
Sanket Padawe356d7632015-06-22 14:03:32 -07002216 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002217 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002218 return phone.getServiceState().getDataNetworkType();
2219 } else {
2220 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2221 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002222 }
2223
2224 /**
2225 * Returns the data network type
2226 */
2227 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002228 public int getDataNetworkType(String callingPackage) {
2229 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002230 }
2231
2232 /**
2233 * Returns the data network type for a subId
2234 */
2235 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002236 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2237 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2238 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2239 }
2240
Sanket Padawe356d7632015-06-22 14:03:32 -07002241 final Phone phone = getPhone(subId);
2242 if (phone != null) {
2243 return phone.getServiceState().getDataNetworkType();
2244 } else {
2245 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2246 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002247 }
2248
2249 /**
Wink Saville36469e72014-06-11 15:17:00 -07002250 * Returns the Voice network type for a subId
2251 */
2252 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002253 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2254 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2255 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2256 }
2257
Sanket Padawe356d7632015-06-22 14:03:32 -07002258 final Phone phone = getPhone(subId);
2259 if (phone != null) {
2260 return phone.getServiceState().getVoiceNetworkType();
2261 } else {
2262 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2263 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002264 }
2265
2266 /**
2267 * @return true if a ICC card is present
2268 */
2269 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002270 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002271 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2272 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002273 }
2274
2275 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002276 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002277 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002278 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002279 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2280 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002281 final Phone phone = getPhone(subId[0]);
2282 if (subId != null && phone != null) {
2283 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002284 } else {
2285 return false;
2286 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002287 }
2288
2289 /**
2290 * Return if the current radio is LTE on CDMA. This
2291 * is a tri-state return value as for a period of time
2292 * the mode may be unknown.
2293 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002294 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002295 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002296 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002297 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002298 @Override
2299 public int getLteOnCdmaMode(String callingPackage) {
2300 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002301 }
2302
Sanket Padawe356d7632015-06-22 14:03:32 -07002303 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002304 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2305 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2306 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2307 }
2308
Sanket Padawe356d7632015-06-22 14:03:32 -07002309 final Phone phone = getPhone(subId);
2310 if (phone == null) {
2311 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2312 } else {
2313 return phone.getLteOnCdmaMode();
2314 }
Wink Saville36469e72014-06-11 15:17:00 -07002315 }
2316
2317 public void setPhone(Phone phone) {
2318 mPhone = phone;
2319 }
2320
2321 /**
2322 * {@hide}
2323 * Returns Default subId, 0 in the case of single standby.
2324 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002325 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002326 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002327 }
2328
Shishir Agrawala9f32182016-04-12 12:00:16 -07002329 private int getSlotForDefaultSubscription() {
2330 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2331 }
2332
Wink Savilleb564aae2014-10-23 10:18:09 -07002333 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002334 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002335 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002336
2337 /**
2338 * @see android.telephony.TelephonyManager.WifiCallingChoices
2339 */
2340 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002341 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2342 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002343 }
2344
2345 /**
2346 * @see android.telephony.TelephonyManager.WifiCallingChoices
2347 */
2348 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002349 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2350 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2351 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002352 }
2353
Sailesh Nepald1e68152013-12-12 19:08:02 -08002354 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002355 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002356 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002357 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002358
Shishir Agrawal566b7612013-10-28 14:41:00 -07002359 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002360 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002361 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002362
Ajay Nambiabd73502015-12-03 13:50:00 -08002363 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002364 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002365 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002366 if (DBG) log("iccOpenLogicalChannel: " + response);
2367 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002368 }
2369
2370 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002371 public boolean iccCloseLogicalChannel(int subId, int channel) {
2372 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002373
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002374 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002375 if (channel < 0) {
2376 return false;
2377 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002378 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002379 if (DBG) log("iccCloseLogicalChannel: " + success);
2380 return success;
2381 }
2382
2383 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002384 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002385 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002386 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002387
2388 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002389 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2390 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002391 " data=" + data);
2392 }
2393
2394 if (channel < 0) {
2395 return "";
2396 }
2397
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002398 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002399 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002400 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2401
Shishir Agrawal566b7612013-10-28 14:41:00 -07002402 // Append the returned status code to the end of the response payload.
2403 String s = Integer.toHexString(
2404 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002405 if (response.payload != null) {
2406 s = IccUtils.bytesToHexString(response.payload) + s;
2407 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002408 return s;
2409 }
Jake Hambye994d462014-02-03 13:10:13 -08002410
Evan Charltonc66da362014-05-16 14:06:40 -07002411 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002412 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002413 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002414 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002415
2416 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002417 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2418 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002419 }
2420
2421 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002422 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002423 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2424
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002425 // Append the returned status code to the end of the response payload.
2426 String s = Integer.toHexString(
2427 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002428 if (response.payload != null) {
2429 s = IccUtils.bytesToHexString(response.payload) + s;
2430 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002431 return s;
2432 }
2433
2434 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002435 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002436 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002437 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002438
2439 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002440 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002441 p1 + " " + p2 + " " + p3 + ":" + filePath);
2442 }
2443
2444 IccIoResult response =
2445 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002446 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2447 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002448
2449 if (DBG) {
2450 log("Exchange SIM_IO [R]" + response);
2451 }
2452
2453 byte[] result = null;
2454 int length = 2;
2455 if (response.payload != null) {
2456 length = 2 + response.payload.length;
2457 result = new byte[length];
2458 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2459 } else {
2460 result = new byte[length];
2461 }
2462
2463 result[length - 1] = (byte) response.sw2;
2464 result[length - 2] = (byte) response.sw1;
2465 return result;
2466 }
2467
Nathan Haroldb3014052017-01-25 15:57:32 -08002468 /**
2469 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2470 * on a particular subscription
2471 */
2472 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002473 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2474 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002475 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2476 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2477 return null;
2478 }
2479 Object response = sendRequest(
2480 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2481 if (response instanceof String[]) {
2482 return (String[]) response;
2483 }
2484 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2485 return null;
2486 }
2487
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002488 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002489 public String sendEnvelopeWithStatus(int subId, String content) {
2490 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002491
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002492 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002493 if (response.payload == null) {
2494 return "";
2495 }
2496
2497 // Append the returned status code to the end of the response payload.
2498 String s = Integer.toHexString(
2499 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2500 s = IccUtils.bytesToHexString(response.payload) + s;
2501 return s;
2502 }
2503
Jake Hambye994d462014-02-03 13:10:13 -08002504 /**
2505 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2506 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2507 *
2508 * @param itemID the ID of the item to read
2509 * @return the NV item as a String, or null on error.
2510 */
2511 @Override
2512 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002513 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002514 if (DBG) log("nvReadItem: item " + itemID);
2515 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2516 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2517 return value;
2518 }
2519
2520 /**
2521 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2522 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2523 *
2524 * @param itemID the ID of the item to read
2525 * @param itemValue the value to write, as a String
2526 * @return true on success; false on any failure
2527 */
2528 @Override
2529 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002530 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002531 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2532 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2533 new Pair<Integer, String>(itemID, itemValue));
2534 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2535 return success;
2536 }
2537
2538 /**
2539 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2540 * Used for device configuration by some CDMA operators.
2541 *
2542 * @param preferredRoamingList byte array containing the new PRL
2543 * @return true on success; false on any failure
2544 */
2545 @Override
2546 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002547 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002548 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2549 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2550 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2551 return success;
2552 }
2553
2554 /**
2555 * Perform the specified type of NV config reset.
2556 * Used for device configuration by some CDMA operators.
2557 *
2558 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2559 * @return true on success; false on any failure
2560 */
2561 @Override
2562 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002563 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002564 if (DBG) log("nvResetConfig: type " + resetType);
2565 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2566 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2567 return success;
2568 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002569
2570 /**
Wink Saville36469e72014-06-11 15:17:00 -07002571 * {@hide}
2572 * Returns Default sim, 0 in the case of single standby.
2573 */
2574 public int getDefaultSim() {
2575 //TODO Need to get it from Telephony Devcontroller
2576 return 0;
2577 }
2578
Svet Ganovb320e182015-04-16 12:30:10 -07002579 public String[] getPcscfAddress(String apnType, String callingPackage) {
2580 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2581 return new String[0];
2582 }
2583
2584
ram87fca6f2014-07-18 18:58:44 +05302585 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002586 }
2587
Brad Ebinger76681002017-01-23 13:50:20 -08002588 /**
2589 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2590 * feature or {@link null} if the service is not available. If an ImsServiceController is
2591 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2592 * feature updates.
2593 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002594 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002595 IImsServiceFeatureListener callback) {
2596 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002597 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002598 callback);
2599 }
2600
Wink Saville36469e72014-06-11 15:17:00 -07002601 public void setImsRegistrationState(boolean registered) {
2602 enforceModifyPermission();
2603 mPhone.setImsRegistrationState(registered);
2604 }
2605
2606 /**
Stuart Scott54788802015-03-30 13:18:01 -07002607 * Set the network selection mode to automatic.
2608 *
2609 */
2610 @Override
2611 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002612 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002613 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2614 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2615 }
2616
2617 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002618 * Set the network selection mode to manual with the selected carrier.
2619 */
2620 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002621 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2622 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002623 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002624 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002625 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2626 persistSelection);
2627 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002628 }
2629
2630 /**
2631 * Scans for available networks.
2632 */
2633 @Override
2634 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002635 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002636 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2637 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2638 CMD_PERFORM_NETWORK_SCAN, null, subId);
2639 return result;
2640 }
2641
2642 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002643 * Get the calculated preferred network type.
2644 * Used for debugging incorrect network type.
2645 *
2646 * @return the preferred network type, defined in RILConstants.java.
2647 */
2648 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002649 public int getCalculatedPreferredNetworkType(String callingPackage) {
2650 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2651 return RILConstants.PREFERRED_NETWORK_MODE;
2652 }
2653
Amit Mahajan43330e02014-11-18 11:54:45 -08002654 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002655 }
2656
2657 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002658 * Get the preferred network type.
2659 * Used for device configuration by some CDMA operators.
2660 *
2661 * @return the preferred network type, defined in RILConstants.java.
2662 */
2663 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002664 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002665 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002666 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002667 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002668 int networkType = (result != null ? result[0] : -1);
2669 if (DBG) log("getPreferredNetworkType: " + networkType);
2670 return networkType;
2671 }
2672
2673 /**
2674 * Set the preferred network type.
2675 * Used for device configuration by some CDMA operators.
2676 *
2677 * @param networkType the preferred network type, defined in RILConstants.java.
2678 * @return true on success; false on any failure.
2679 */
2680 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002681 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002682 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002683 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2684 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002685 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002686 if (success) {
2687 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002688 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002689 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002690 return success;
2691 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002692
2693 /**
Junda Liu475951f2014-11-07 16:45:03 -08002694 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2695 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2696 * tethering.
2697 *
2698 * @return 0: Not required. 1: required. 2: Not set.
2699 * @hide
2700 */
2701 @Override
2702 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002703 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002704 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2705 Settings.Global.TETHER_DUN_REQUIRED, 2);
2706 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2707 // config_tether_apndata.
2708 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2709 dunRequired = 1;
2710 }
2711 return dunRequired;
2712 }
2713
2714 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002715 * Set mobile data enabled
2716 * Used by the user through settings etc to turn on/off mobile data
2717 *
2718 * @param enable {@code true} turn turn data on, else {@code false}
2719 */
2720 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002721 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002722 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002723 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002724 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002725 Phone phone = PhoneFactory.getPhone(phoneId);
2726 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002727 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002728 phone.setDataEnabled(enable);
2729 } else {
2730 loge("setDataEnabled: no phone for subId=" + subId);
2731 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002732 }
2733
2734 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002735 * Get whether mobile data is enabled.
2736 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002737 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002738 *
2739 * @return {@code true} if data is enabled else {@code false}
2740 */
2741 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002742 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002743 try {
2744 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2745 null);
2746 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002747 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002748 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002749 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002750 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002751 Phone phone = PhoneFactory.getPhone(phoneId);
2752 if (phone != null) {
2753 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002754 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002755 return retVal;
2756 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002757 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002758 return false;
2759 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002760 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002761
2762 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002763 public int getCarrierPrivilegeStatus(int subId) {
2764 final Phone phone = getPhone(subId);
2765 if (phone == null) {
2766 loge("getCarrierPrivilegeStatus: Invalid subId");
2767 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2768 }
2769 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002770 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002771 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002772 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2773 }
2774 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002775 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002776 }
Junda Liu29340342014-07-10 15:23:27 -07002777
2778 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002779 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002780 if (TextUtils.isEmpty(pkgName))
2781 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002782 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002783 if (card == null) {
2784 loge("checkCarrierPrivilegesForPackage: No UICC");
2785 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2786 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002787 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2788 }
2789
2790 @Override
2791 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002792 if (TextUtils.isEmpty(pkgName))
2793 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002794 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2795 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2796 UiccCard card = UiccController.getInstance().getUiccCard(i);
2797 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002798 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002799 continue;
2800 }
2801
2802 result = card.getCarrierPrivilegeStatus(
2803 mPhone.getContext().getPackageManager(), pkgName);
2804 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2805 break;
2806 }
2807 }
2808
2809 return result;
Junda Liu29340342014-07-10 15:23:27 -07002810 }
Derek Tan89e89d42014-07-08 17:00:10 -07002811
2812 @Override
Junda Liue64de782015-04-16 17:19:16 -07002813 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2814 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2815 loge("phoneId " + phoneId + " is not valid.");
2816 return null;
2817 }
2818 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002819 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002820 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002821 return null ;
2822 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002823 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002824 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002825 }
2826
Amith Yamasani6e118872016-02-19 12:53:51 -08002827 @Override
2828 public List<String> getPackagesWithCarrierPrivileges() {
2829 PackageManager pm = mPhone.getContext().getPackageManager();
2830 List<String> privilegedPackages = new ArrayList<>();
2831 List<PackageInfo> packages = null;
2832 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2833 UiccCard card = UiccController.getInstance().getUiccCard(i);
2834 if (card == null) {
2835 // No UICC in that slot.
2836 continue;
2837 }
2838 if (card.hasCarrierPrivilegeRules()) {
2839 if (packages == null) {
2840 // Only check packages in user 0 for now
2841 packages = pm.getInstalledPackagesAsUser(
2842 PackageManager.MATCH_DISABLED_COMPONENTS
2843 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2844 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2845 }
2846 for (int p = packages.size() - 1; p >= 0; p--) {
2847 PackageInfo pkgInfo = packages.get(p);
2848 if (pkgInfo != null && pkgInfo.packageName != null
2849 && card.getCarrierPrivilegeStatus(pkgInfo)
2850 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2851 privilegedPackages.add(pkgInfo.packageName);
2852 }
2853 }
2854 }
2855 }
2856 return privilegedPackages;
2857 }
2858
Wink Savilleb564aae2014-10-23 10:18:09 -07002859 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002860 final Phone phone = getPhone(subId);
2861 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002862 if (card == null) {
2863 loge("getIccId: No UICC");
2864 return null;
2865 }
2866 String iccId = card.getIccId();
2867 if (TextUtils.isEmpty(iccId)) {
2868 loge("getIccId: ICC ID is null or empty.");
2869 return null;
2870 }
2871 return iccId;
2872 }
2873
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002874 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002875 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2876 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002877 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002878
Jeff Sharkey85190e62014-12-05 09:40:12 -08002879 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002880 final Phone phone = getPhone(subId);
2881 if (phone == null) {
2882 return false;
2883 }
2884 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002885
2886 if (DBG_MERGE) {
2887 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2888 + subscriberId + " to " + number);
2889 }
2890
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002891 if (TextUtils.isEmpty(iccId)) {
2892 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002893 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002894
Jeff Sharkey85190e62014-12-05 09:40:12 -08002895 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2896
2897 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002898 if (alphaTag == null) {
2899 editor.remove(alphaTagPrefKey);
2900 } else {
2901 editor.putString(alphaTagPrefKey, alphaTag);
2902 }
2903
Jeff Sharkey85190e62014-12-05 09:40:12 -08002904 // Record both the line number and IMSI for this ICCID, since we need to
2905 // track all merged IMSIs based on line number
2906 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2907 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002908 if (number == null) {
2909 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002910 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002911 } else {
2912 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002913 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002914 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002915
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002916 editor.commit();
2917 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002918 }
2919
2920 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002921 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002922 // This is open to apps with WRITE_SMS.
2923 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002924 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002925 return null;
2926 }
Derek Tan97ebb422014-09-05 16:55:38 -07002927
2928 String iccId = getIccId(subId);
2929 if (iccId != null) {
2930 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002931 if (DBG_MERGE) {
2932 log("getLine1NumberForDisplay returning " +
2933 mTelephonySharedPreferences.getString(numberPrefKey, null));
2934 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002935 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002936 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002937 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002938 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002939 }
2940
2941 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002942 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2943 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2944 return null;
2945 }
Derek Tan97ebb422014-09-05 16:55:38 -07002946
2947 String iccId = getIccId(subId);
2948 if (iccId != null) {
2949 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002950 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002951 }
Derek Tan97ebb422014-09-05 16:55:38 -07002952 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002953 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002954
2955 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002956 public String[] getMergedSubscriberIds(String callingPackage) {
2957 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2958 return null;
2959 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002960 final Context context = mPhone.getContext();
2961 final TelephonyManager tele = TelephonyManager.from(context);
2962 final SubscriptionManager sub = SubscriptionManager.from(context);
2963
2964 // Figure out what subscribers are currently active
2965 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002966 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2967 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2968 final long identity = Binder.clearCallingIdentity();
2969 try {
2970 final int[] subIds = sub.getActiveSubscriptionIdList();
2971 for (int subId : subIds) {
2972 activeSubscriberIds.add(tele.getSubscriberId(subId));
2973 }
2974 } finally {
2975 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002976 }
2977
2978 // First pass, find a number override for an active subscriber
2979 String mergeNumber = null;
2980 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2981 for (String key : prefs.keySet()) {
2982 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2983 final String subscriberId = (String) prefs.get(key);
2984 if (activeSubscriberIds.contains(subscriberId)) {
2985 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2986 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2987 mergeNumber = (String) prefs.get(numberKey);
2988 if (DBG_MERGE) {
2989 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2990 + " for active subscriber " + subscriberId);
2991 }
2992 if (!TextUtils.isEmpty(mergeNumber)) {
2993 break;
2994 }
2995 }
2996 }
2997 }
2998
2999 // Shortcut when no active merged subscribers
3000 if (TextUtils.isEmpty(mergeNumber)) {
3001 return null;
3002 }
3003
3004 // Second pass, find all subscribers under that line override
3005 final ArraySet<String> result = new ArraySet<>();
3006 for (String key : prefs.keySet()) {
3007 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3008 final String number = (String) prefs.get(key);
3009 if (mergeNumber.equals(number)) {
3010 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3011 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3012 final String subscriberId = (String) prefs.get(subscriberKey);
3013 if (!TextUtils.isEmpty(subscriberId)) {
3014 result.add(subscriberId);
3015 }
3016 }
3017 }
3018 }
3019
3020 final String[] resultArray = result.toArray(new String[result.size()]);
3021 Arrays.sort(resultArray);
3022 if (DBG_MERGE) {
3023 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3024 }
3025 return resultArray;
3026 }
3027
3028 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003029 public boolean setOperatorBrandOverride(int subId, String brand) {
3030 enforceCarrierPrivilege(subId);
3031 final Phone phone = getPhone(subId);
3032 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003033 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003034
3035 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003036 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003037 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3038 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003039 enforceCarrierPrivilege(subId);
3040 final Phone phone = getPhone(subId);
3041 if (phone == null) {
3042 return false;
3043 }
3044 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003045 cdmaNonRoamingList);
3046 }
3047
3048 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003049 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003050 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3051 enforceModifyPermission();
3052
3053 int returnValue = 0;
3054 try {
3055 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3056 if(result.exception == null) {
3057 if (result.result != null) {
3058 byte[] responseData = (byte[])(result.result);
3059 if(responseData.length > oemResp.length) {
3060 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3061 responseData.length + "bytes. Buffer Size is " +
3062 oemResp.length + "bytes.");
3063 }
3064 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3065 returnValue = responseData.length;
3066 }
3067 } else {
3068 CommandException ex = (CommandException) result.exception;
3069 returnValue = ex.getCommandError().ordinal();
3070 if(returnValue > 0) returnValue *= -1;
3071 }
3072 } catch (RuntimeException e) {
3073 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3074 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3075 if(returnValue > 0) returnValue *= -1;
3076 }
3077
3078 return returnValue;
3079 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003080
3081 @Override
3082 public void setRadioCapability(RadioAccessFamily[] rafs) {
3083 try {
3084 ProxyController.getInstance().setRadioCapability(rafs);
3085 } catch (RuntimeException e) {
3086 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3087 }
3088 }
3089
3090 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003091 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3092 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3093 return RadioAccessFamily.RAF_UNKNOWN;
3094 }
3095
Wink Saville5d475dd2014-10-17 15:00:58 -07003096 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3097 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003098
3099 @Override
3100 public void enableVideoCalling(boolean enable) {
3101 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003102 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003103 }
3104
3105 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003106 public boolean isVideoCallingEnabled(String callingPackage) {
3107 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3108 return false;
3109 }
3110
Andrew Lee77527ac2014-10-21 16:57:39 -07003111 // Check the user preference and the system-level IMS setting. Even if the user has
3112 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3113 // In the long run, we may instead need to check if there exists a connection service
3114 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003115 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3116 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003117 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003118 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003119
Andrew Leea1239f22015-03-02 17:44:07 -08003120 @Override
3121 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003122 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003123 }
3124
3125 @Override
3126 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003127 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003128 }
3129
Andrew Lee9431b832015-03-09 18:46:45 -07003130 @Override
3131 public boolean isTtyModeSupported() {
3132 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3133 TelephonyManager telephonyManager =
3134 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003135 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003136 }
3137
3138 @Override
3139 public boolean isHearingAidCompatibilitySupported() {
3140 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3141 }
3142
Sanket Padawe7310cc72015-01-14 09:53:20 -08003143 /**
3144 * Returns the unique device ID of phone, for example, the IMEI for
3145 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3146 *
3147 * <p>Requires Permission:
3148 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3149 */
3150 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003151 public String getDeviceId(String callingPackage) {
3152 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3153 return null;
3154 }
3155
Sanket Padawe7310cc72015-01-14 09:53:20 -08003156 final Phone phone = PhoneFactory.getPhone(0);
3157 if (phone != null) {
3158 return phone.getDeviceId();
3159 } else {
3160 return null;
3161 }
3162 }
3163
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003164 /*
3165 * {@hide}
3166 * Returns the IMS Registration Status
3167 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003168 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003169 public boolean isImsRegistered() {
3170 return mPhone.isImsRegistered();
3171 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003172
3173 @Override
3174 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3175 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3176 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003177
Nathan Haroldc55097a2015-03-11 18:14:50 -07003178 /*
3179 * {@hide}
3180 * Returns the IMS Registration Status
3181 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003182 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003183 return mPhone.isWifiCallingEnabled();
3184 }
3185
3186 /*
3187 * {@hide}
3188 * Returns the IMS Registration Status
3189 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003190 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003191 return mPhone.isVolteEnabled();
3192 }
Svet Ganovb320e182015-04-16 12:30:10 -07003193
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003194 /*
3195 * {@hide} Returns the IMS Registration Status
3196 */
3197 public boolean isVideoTelephonyAvailable() {
3198 return mPhone.isVideoEnabled();
3199 }
3200
Svet Ganovb320e182015-04-16 12:30:10 -07003201 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003202 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003203 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003204 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3205
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003206 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003207 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003208 } catch (SecurityException e) {
3209 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3210 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003211 }
Svet Ganovb320e182015-04-16 12:30:10 -07003212
3213 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3214 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3215 return false;
3216 }
3217
3218 return true;
3219 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003220
Makoto Onukifee69342015-06-29 14:44:50 -07003221 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003222 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003223 */
3224 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003225 // Default SMS app can always read it.
3226 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3227 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3228 return true;
3229 }
3230 try {
3231 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003232 } catch (SecurityException readPhoneStateSecurityException) {
3233 try {
3234 // Can be read with READ_SMS too.
3235 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3236 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3237 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3238 } catch (SecurityException readSmsSecurityException) {
3239 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3240 // permissions
3241 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3242 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3243 " or " + android.Manifest.permission.READ_SMS + ".");
3244 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003245 }
Makoto Onukifee69342015-06-29 14:44:50 -07003246 }
3247
Stuart Scott8eef64f2015-04-08 15:13:54 -07003248 @Override
3249 public void factoryReset(int subId) {
3250 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003251 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3252 return;
3253 }
3254
Svet Ganovcc087f82015-05-12 20:35:54 -07003255 final long identity = Binder.clearCallingIdentity();
3256 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003257 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3258 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003259 // Enable data
3260 setDataEnabled(subId, true);
3261 // Set network selection mode to automatic
3262 setNetworkSelectionModeAutomatic(subId);
3263 // Set preferred mobile network type to the best available
3264 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3265 // Turn off roaming
3266 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3267 }
3268 } finally {
3269 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003270 }
3271 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003272
3273 @Override
3274 public String getLocaleFromDefaultSim() {
3275 // We query all subscriptions instead of just the active ones, because
3276 // this might be called early on in the provisioning flow when the
3277 // subscriptions potentially aren't active yet.
3278 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3279 if (slist == null || slist.isEmpty()) {
3280 return null;
3281 }
3282
3283 // This function may be called very early, say, from the setup wizard, at
3284 // which point we won't have a default subscription set. If that's the case
3285 // we just choose the first, which will be valid in "most cases".
3286 final int defaultSubId = getDefaultSubscription();
3287 SubscriptionInfo info = null;
3288 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3289 info = slist.get(0);
3290 } else {
3291 for (SubscriptionInfo item : slist) {
3292 if (item.getSubscriptionId() == defaultSubId) {
3293 info = item;
3294 break;
3295 }
3296 }
3297
3298 if (info == null) {
3299 return null;
3300 }
3301 }
3302
3303 // Try and fetch the locale from the carrier properties or from the SIM language
3304 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003305 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003306 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003307 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003308 if (defaultPhone != null) {
3309 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3310 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003311 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003312 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3313 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003314 } else {
3315 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003316 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003317 }
3318 }
3319
Narayan Kamath011676f2015-07-29 12:04:08 +01003320 // The SIM language preferences only store a language (e.g. fr = French), not an
3321 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3322 // the SIM and carrier preferences does not include a country we add the country
3323 // determined from the SIM MCC to provide an exact locale.
3324 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003325 if (mccLocale != null) {
3326 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3327 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003328 }
3329
Tony Hill183b2de2015-06-24 14:53:58 +01003330 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003331 return null;
3332 }
3333
3334 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3335 final long identity = Binder.clearCallingIdentity();
3336 try {
3337 return mSubscriptionController.getAllSubInfoList(
3338 mPhone.getContext().getOpPackageName());
3339 } finally {
3340 Binder.restoreCallingIdentity(identity);
3341 }
3342 }
3343
3344 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3345 final long identity = Binder.clearCallingIdentity();
3346 try {
3347 return mSubscriptionController.getActiveSubscriptionInfoList(
3348 mPhone.getContext().getOpPackageName());
3349 } finally {
3350 Binder.restoreCallingIdentity(identity);
3351 }
3352 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003353
3354 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003355 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3356 * representing the state of the modem.
3357 *
3358 * NOTE: This clears the modem state, so there should only every be one caller.
3359 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003360 */
3361 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003362 public void requestModemActivityInfo(ResultReceiver result) {
3363 enforceModifyPermission();
3364
3365 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3366 Bundle bundle = new Bundle();
3367 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3368 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003369 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003370
3371 /**
3372 * {@hide}
3373 * Returns the service state information on specified subscription.
3374 */
3375 @Override
3376 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3377
3378 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3379 return null;
3380 }
3381
3382 final Phone phone = getPhone(subId);
3383 if (phone == null) {
3384 return null;
3385 }
3386
3387 return phone.getServiceState();
3388 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003389
3390 /**
3391 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3392 *
3393 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3394 * voicemail ringtone.
3395 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3396 * PhoneAccount.
3397 */
3398 @Override
3399 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3400 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3401 if (phone == null) {
3402 return null;
3403 }
3404
3405 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3406 }
3407
3408 /**
3409 * Returns whether vibration is set for voicemail notification in Phone settings.
3410 *
3411 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3412 * voicemail vibration setting.
3413 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3414 */
3415 @Override
3416 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3417 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3418 if (phone == null) {
3419 return false;
3420 }
3421
3422 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3423 }
3424
Youhan Wange64578a2016-05-02 15:32:42 -07003425 /**
3426 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3427 *
3428 * @throws SecurityException if the caller does not have the required permission
3429 */
3430 private void enforceReadPrivilegedPermission() {
3431 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3432 null);
3433 }
3434
3435 /**
Ta-wei Yen1b455992016-12-27 14:52:32 -08003436 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3437 * permission.
3438 *
3439 * @throws SecurityException if the caller does not have the required permission
3440 */
3441 private void enforceSendSmsPermission() {
3442 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3443 }
3444
3445 /**
Ta-wei Yen463efd12017-01-06 15:29:25 -08003446 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003447 *
Ta-wei Yen463efd12017-01-06 15:29:25 -08003448 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003449 */
Ta-wei Yen463efd12017-01-06 15:29:25 -08003450 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
3451 String vvmPackage = RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId)
3452 .getPackageName();
3453 if (!callingPackage.equals(vvmPackage)) {
3454 throw new SecurityException("Caller not current active visual voicemail package[" +
3455 vvmPackage + "]");
Ta-wei Yen1b455992016-12-27 14:52:32 -08003456 }
3457 }
3458
3459 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003460 * Return the application ID for the app type.
3461 *
3462 * @param subId the subscription ID that this request applies to.
3463 * @param appType the uicc app type.
3464 * @return Application ID for specificied app type, or null if no uicc.
3465 */
3466 @Override
3467 public String getAidForAppType(int subId, int appType) {
3468 enforceReadPrivilegedPermission();
3469 Phone phone = getPhone(subId);
3470 if (phone == null) {
3471 return null;
3472 }
3473 String aid = null;
3474 try {
3475 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3476 .getApplicationByType(appType).getAid();
3477 } catch (Exception e) {
3478 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3479 }
3480 return aid;
3481 }
3482
Youhan Wang4001d252016-05-11 10:29:41 -07003483 /**
3484 * Return the Electronic Serial Number.
3485 *
3486 * @param subId the subscription ID that this request applies to.
3487 * @return ESN or null if error.
3488 */
3489 @Override
3490 public String getEsn(int subId) {
3491 enforceReadPrivilegedPermission();
3492 Phone phone = getPhone(subId);
3493 if (phone == null) {
3494 return null;
3495 }
3496 String esn = null;
3497 try {
3498 esn = phone.getEsn();
3499 } catch (Exception e) {
3500 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3501 }
3502 return esn;
3503 }
3504
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003505 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003506 * Return the Preferred Roaming List Version.
3507 *
3508 * @param subId the subscription ID that this request applies to.
3509 * @return PRLVersion or null if error.
3510 */
3511 @Override
3512 public String getCdmaPrlVersion(int subId) {
3513 enforceReadPrivilegedPermission();
3514 Phone phone = getPhone(subId);
3515 if (phone == null) {
3516 return null;
3517 }
3518 String cdmaPrlVersion = null;
3519 try {
3520 cdmaPrlVersion = phone.getCdmaPrlVersion();
3521 } catch (Exception e) {
3522 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3523 }
3524 return cdmaPrlVersion;
3525 }
3526
3527 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003528 * Get snapshot of Telephony histograms
3529 * @return List of Telephony histograms
3530 * @hide
3531 */
3532 @Override
3533 public List<TelephonyHistogram> getTelephonyHistograms() {
3534 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3535 return RIL.getTelephonyRILTimingHistograms();
3536 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003537
3538 /**
3539 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003540 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003541 * Require system privileges. In the future we may add this to carrier APIs.
3542 *
3543 * @return The number of carriers set successfully, should match length of carriers
3544 */
3545 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003546 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003547 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003548 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003549 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3550 return retVal[0];
3551 }
3552
3553 /**
3554 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003555 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003556 * Require system privileges. In the future we may add this to carrier APIs.
3557 *
3558 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3559 * means all carriers are allowed.
3560 */
3561 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003562 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003563 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003564 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003565 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3566 }
3567
fionaxu59545b42016-05-25 15:53:37 -07003568 /**
3569 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3570 * @param subId the subscription ID that this action applies to.
3571 * @param enabled control enable or disable metered apns.
3572 * {@hide}
3573 */
3574 @Override
3575 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3576 enforceModifyPermission();
3577 final Phone phone = getPhone(subId);
3578 if (phone == null) {
3579 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3580 return;
3581 }
3582 try {
3583 phone.carrierActionSetMeteredApnsEnabled(enabled);
3584 } catch (Exception e) {
3585 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3586 }
3587 }
3588
3589 /**
3590 * Action set from carrier signalling broadcast receivers to enable/disable radio
3591 * @param subId the subscription ID that this action applies to.
3592 * @param enabled control enable or disable radio.
3593 * {@hide}
3594 */
3595 @Override
3596 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3597 enforceModifyPermission();
3598 final Phone phone = getPhone(subId);
3599 if (phone == null) {
3600 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3601 return;
3602 }
3603 try {
3604 phone.carrierActionSetRadioEnabled(enabled);
3605 } catch (Exception e) {
3606 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3607 }
3608 }
3609
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003610 /**
3611 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3612 * bug report is being generated.
3613 */
3614 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003615 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003616 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3617 != PackageManager.PERMISSION_GRANTED) {
3618 writer.println("Permission Denial: can't dump Phone from pid="
3619 + Binder.getCallingPid()
3620 + ", uid=" + Binder.getCallingUid()
3621 + "without permission "
3622 + android.Manifest.permission.DUMP);
3623 return;
3624 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003625 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003626 }
Jack Yueb89b242016-06-22 13:27:47 -07003627
3628 /**
3629 * Get aggregated video call data usage from all subscriptions since boot.
3630 * @return total data usage in bytes
3631 * {@hide}
3632 */
3633 @Override
3634 public long getVtDataUsage() {
3635 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3636 null);
3637
3638 // NetworkStatsService keeps tracking the active network interface and identity. It will
3639 // record the delta with the corresponding network identity. What we need to do here is
3640 // returning total video call data usage from all subscriptions since boot.
3641
3642 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3643 // simultaneous VT calls.
3644 final Phone[] phones = PhoneFactory.getPhones();
3645 long total = 0;
3646 for (Phone phone : phones) {
3647 total += phone.getVtDataUsage();
3648 }
3649 return total;
3650 }
Jack Yu75ab2952016-07-08 14:29:33 -07003651
3652 /**
3653 * Policy control of data connection. Usually used when data limit is passed.
3654 * @param enabled True if enabling the data, otherwise disabling.
3655 * @param subId Subscription index
3656 * {@hide}
3657 */
3658 @Override
3659 public void setPolicyDataEnabled(boolean enabled, int subId) {
3660 enforceModifyPermission();
3661 Phone phone = getPhone(subId);
3662 if (phone != null) {
3663 phone.setPolicyDataEnabled(enabled);
3664 }
3665 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003666
3667 /**
3668 * Get Client request stats
3669 * @return List of Client Request Stats
3670 * @hide
3671 */
3672 @Override
3673 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3674 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3675 return null;
3676 }
3677
3678 Phone phone = getPhone(subId);
3679 if (phone != null) {
3680 return phone.getClientRequestStats();
3681 }
3682
3683 return null;
3684 }
3685
3686 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3687 if (workSource != null) {
3688 return workSource;
3689 }
3690
3691 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3692 workSource = new WorkSource(uid, packageName);
3693 return workSource;
3694 }
Jack Yu346de222017-02-16 15:32:43 -08003695
3696 /**
3697 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3698 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003699 * @param slotIndex SIM slot id.
Jack Yu346de222017-02-16 15:32:43 -08003700 * @param powerUp True if powering up the SIM, otherwise powering down
3701 *
3702 **/
3703 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003704 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yu346de222017-02-16 15:32:43 -08003705 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003706 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003707 if (subId == null || subId.length == 0) {
3708 return;
3709 }
3710
3711 final Phone phone = getPhone(subId[0]);
3712 if (phone != null) {
3713 phone.setSimPowerState(powerUp);
3714 }
3715 }
shuoqfef36a32017-01-10 13:02:18 -08003716
3717 /**
3718 * Check if phone is in emergency callback mode
3719 * @return true if phone is in emergency callback mode
3720 * @param subId sub id
3721 */
3722 public boolean getEmergencyCallbackMode(int subId) {
3723 final Phone phone = getPhone(subId);
3724 if (phone != null) {
3725 return phone.isInEcm();
3726 } else {
3727 return false;
3728 }
3729 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08003730
3731 /**
3732 * Get the current signal strength information for the given subscription.
3733 * Because this information is not updated when the device is in a low power state
3734 * it should not be relied-upon to be current.
3735 * @param subId Subscription index
3736 * @return the most recent cached signal strength info from the modem
3737 */
3738 @Override
3739 public SignalStrength getSignalStrength(int subId) {
3740 Phone p = getPhone(subId);
3741 if (p == null) {
3742 return null;
3743 }
3744
3745 return p.getSignalStrength();
3746 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003747}