blob: b33931c7fcd3ba76228ec30b585faef894d2483c [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 Yen30a69c82016-12-27 14:52:32 -080021import android.Manifest.permission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.ActivityManager;
23import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080024import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070025import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.content.Context;
27import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070028import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080029import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070030import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.net.Uri;
32import android.os.AsyncResult;
33import android.os.Binder;
34import android.os.Bundle;
35import android.os.Handler;
36import android.os.Looper;
37import android.os.Message;
38import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070039import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.ServiceManager;
41import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070042import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070043import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070044import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080045import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070046import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080047import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080048import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070049import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070050import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.telephony.CellInfo;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070052import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070053import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070054import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080055import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070056import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.telephony.ServiceState;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080058import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080059import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080060import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070061import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070062import android.telephony.TelephonyManager;
63import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080065import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080067import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080068import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080069
Andrew Lee312e8172014-10-23 17:01:36 -070070import com.android.ims.ImsManager;
Brad Ebinger51f743a2017-01-23 13:50:20 -080071import com.android.ims.internal.IImsServiceController;
72import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070073import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070074import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070075import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070076import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080078import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010079import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070080import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import com.android.internal.telephony.Phone;
Nathan Harolda667c152016-12-14 11:27:20 -080082import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070083import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070084import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070085import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070086import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070087import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080088import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070089import com.android.internal.telephony.uicc.IccIoResult;
90import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080091import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070092import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080093import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070094import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080095import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080096import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc9df0432017-04-17 17:09:07 -070097import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -080098import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -070099import com.android.phone.vvm.VisualVoicemailPreferences;
100import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700101import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800102
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700103import java.io.FileDescriptor;
104import java.io.PrintWriter;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800105import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700106import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800107import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800108import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100109import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800110import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700111
112/**
113 * Implementation of the ITelephony interface.
114 */
Santos Cordon117fee72014-05-16 17:56:12 -0700115public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700116 private static final String LOG_TAG = "PhoneInterfaceManager";
117 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
118 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800119 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120
121 // Message codes used with mMainThreadHandler
122 private static final int CMD_HANDLE_PIN_MMI = 1;
123 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
124 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
125 private static final int CMD_ANSWER_RINGING_CALL = 4;
126 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700127 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
128 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700129 private static final int CMD_OPEN_CHANNEL = 9;
130 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
131 private static final int CMD_CLOSE_CHANNEL = 11;
132 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800133 private static final int CMD_NV_READ_ITEM = 13;
134 private static final int EVENT_NV_READ_ITEM_DONE = 14;
135 private static final int CMD_NV_WRITE_ITEM = 15;
136 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
137 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
138 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
139 private static final int CMD_NV_RESET_CONFIG = 19;
140 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800141 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
142 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
143 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
144 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800145 private static final int CMD_SEND_ENVELOPE = 25;
146 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700147 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
148 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
149 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
150 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
151 private static final int CMD_EXCHANGE_SIM_IO = 31;
152 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800153 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
154 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700155 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
156 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700157 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
158 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700159 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
160 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
161 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
162 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700163 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
164 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
165 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
166 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700167 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800168 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
169 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700170
171 /** The singleton instance. */
172 private static PhoneInterfaceManager sInstance;
173
Wink Saville3ab207e2014-11-20 13:07:20 -0800174 private PhoneGlobals mApp;
175 private Phone mPhone;
176 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700177 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800178 private AppOpsManager mAppOps;
179 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800180 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800181 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700182
Derek Tan97ebb422014-09-05 16:55:38 -0700183 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
184 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800185 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700186
187 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700188 * A request object to use for transmitting data to an ICC.
189 */
190 private static final class IccAPDUArgument {
191 public int channel, cla, command, p1, p2, p3;
192 public String data;
193
194 public IccAPDUArgument(int channel, int cla, int command,
195 int p1, int p2, int p3, String data) {
196 this.channel = channel;
197 this.cla = cla;
198 this.command = command;
199 this.p1 = p1;
200 this.p2 = p2;
201 this.p3 = p3;
202 this.data = data;
203 }
204 }
205
206 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700207 * A request object to use for transmitting data to an ICC.
208 */
209 private static final class ManualNetworkSelectionArgument {
210 public OperatorInfo operatorInfo;
211 public boolean persistSelection;
212
213 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
214 this.operatorInfo = operatorInfo;
215 this.persistSelection = persistSelection;
216 }
217 }
218
219 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700220 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
221 * request after sending. The main thread will notify the request when it is complete.
222 */
223 private static final class MainThreadRequest {
224 /** The argument to use for the request */
225 public Object argument;
226 /** The result of the request that is run on the main thread */
227 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800228 // The subscriber id that this request applies to. Defaults to
229 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
230 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700231
232 public MainThreadRequest(Object argument) {
233 this.argument = argument;
234 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800235
236 public MainThreadRequest(Object argument, Integer subId) {
237 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800238 if (subId != null) {
239 this.subId = subId;
240 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800241 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700242 }
243
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800244 private static final class IncomingThirdPartyCallArgs {
245 public final ComponentName component;
246 public final String callId;
247 public final String callerDisplayName;
248
249 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
250 String callerDisplayName) {
251 this.component = component;
252 this.callId = callId;
253 this.callerDisplayName = callerDisplayName;
254 }
255 }
256
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257 /**
258 * A handler that processes messages on the main thread in the phone process. Since many
259 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
260 * inbound binder threads to the main thread in the phone process. The Binder thread
261 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
262 * on, which will be notified when the operation completes and will contain the result of the
263 * request.
264 *
265 * <p>If a MainThreadRequest object is provided in the msg.obj field,
266 * note that request.result must be set to something non-null for the calling thread to
267 * unblock.
268 */
269 private final class MainThreadHandler extends Handler {
270 @Override
271 public void handleMessage(Message msg) {
272 MainThreadRequest request;
273 Message onCompleted;
274 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800275 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700276 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700277
278 switch (msg.what) {
pkanwar32d516d2016-10-14 19:37:38 -0700279 case CMD_HANDLE_USSD_REQUEST: {
280 request = (MainThreadRequest) msg.obj;
281 final Phone phone = getPhoneFromRequest(request);
282 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
283 String ussdRequest = ussdObject.first;
284 ResultReceiver wrappedCallback = ussdObject.second;
285 request.result = phone != null ?
286 phone.handleUssdRequest(ussdRequest, wrappedCallback)
287 :false;
288 // Wake up the requesting thread
289 synchronized (request) {
290 request.notifyAll();
291 }
292 break;
293 }
294
Yorke Lee716f67e2015-06-17 15:39:16 -0700295 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700297 final Phone phone = getPhoneFromRequest(request);
298 request.result = phone != null ?
299 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
300 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 // Wake up the requesting thread
302 synchronized (request) {
303 request.notifyAll();
304 }
305 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700306 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700307
308 case CMD_HANDLE_NEIGHBORING_CELL:
309 request = (MainThreadRequest) msg.obj;
310 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
311 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700312 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700313 break;
314
315 case EVENT_NEIGHBORING_CELL_DONE:
316 ar = (AsyncResult) msg.obj;
317 request = (MainThreadRequest) ar.userObj;
318 if (ar.exception == null && ar.result != null) {
319 request.result = ar.result;
320 } else {
321 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800322 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700323 }
324 // Wake up the requesting thread
325 synchronized (request) {
326 request.notifyAll();
327 }
328 break;
329
330 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700331 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800332 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700333 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334 break;
335
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 case CMD_END_CALL:
337 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800338 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700339 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700340 Phone phone = getPhone(end_subId);
341 if (phone == null) {
342 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
343 break;
344 }
345 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700346 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
347 // CDMA: If the user presses the Power button we treat it as
348 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700349 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700350 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
351 // GSM: End the call as per the Phone state
352 hungUp = PhoneUtils.hangup(mCM);
353 } else {
354 throw new IllegalStateException("Unexpected phone type: " + phoneType);
355 }
356 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
357 request.result = hungUp;
358 // Wake up the requesting thread
359 synchronized (request) {
360 request.notifyAll();
361 }
362 break;
363
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700364 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700365 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700366 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800367 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700368 if (uiccCard == null) {
369 loge("iccTransmitApduLogicalChannel: No UICC");
370 request.result = new IccIoResult(0x6F, 0, (byte[])null);
371 synchronized (request) {
372 request.notifyAll();
373 }
374 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700375 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
376 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700377 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700378 iccArgument.channel, iccArgument.cla, iccArgument.command,
379 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700380 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700381 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700382 break;
383
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700384 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700385 ar = (AsyncResult) msg.obj;
386 request = (MainThreadRequest) ar.userObj;
387 if (ar.exception == null && ar.result != null) {
388 request.result = ar.result;
389 } else {
390 request.result = new IccIoResult(0x6F, 0, (byte[])null);
391 if (ar.result == null) {
392 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800393 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700394 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800395 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700396 } else {
397 loge("iccTransmitApduLogicalChannel: Unknown exception");
398 }
399 }
400 synchronized (request) {
401 request.notifyAll();
402 }
403 break;
404
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700405 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
406 request = (MainThreadRequest) msg.obj;
407 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800408 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700409 if (uiccCard == null) {
410 loge("iccTransmitApduBasicChannel: No UICC");
411 request.result = new IccIoResult(0x6F, 0, (byte[])null);
412 synchronized (request) {
413 request.notifyAll();
414 }
415 } else {
416 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
417 request);
418 uiccCard.iccTransmitApduBasicChannel(
419 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
420 iccArgument.p3, iccArgument.data, onCompleted);
421 }
422 break;
423
424 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
425 ar = (AsyncResult) msg.obj;
426 request = (MainThreadRequest) ar.userObj;
427 if (ar.exception == null && ar.result != null) {
428 request.result = ar.result;
429 } else {
430 request.result = new IccIoResult(0x6F, 0, (byte[])null);
431 if (ar.result == null) {
432 loge("iccTransmitApduBasicChannel: Empty response");
433 } else if (ar.exception instanceof CommandException) {
434 loge("iccTransmitApduBasicChannel: CommandException: " +
435 ar.exception);
436 } else {
437 loge("iccTransmitApduBasicChannel: Unknown exception");
438 }
439 }
440 synchronized (request) {
441 request.notifyAll();
442 }
443 break;
444
445 case CMD_EXCHANGE_SIM_IO:
446 request = (MainThreadRequest) msg.obj;
447 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800448 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700449 if (uiccCard == null) {
450 loge("iccExchangeSimIO: No UICC");
451 request.result = new IccIoResult(0x6F, 0, (byte[])null);
452 synchronized (request) {
453 request.notifyAll();
454 }
455 } else {
456 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
457 request);
458 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
459 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
460 iccArgument.data, onCompleted);
461 }
462 break;
463
464 case EVENT_EXCHANGE_SIM_IO_DONE:
465 ar = (AsyncResult) msg.obj;
466 request = (MainThreadRequest) ar.userObj;
467 if (ar.exception == null && ar.result != null) {
468 request.result = ar.result;
469 } else {
470 request.result = new IccIoResult(0x6f, 0, (byte[])null);
471 }
472 synchronized (request) {
473 request.notifyAll();
474 }
475 break;
476
Derek Tan4d5e5c12014-02-04 11:54:58 -0800477 case CMD_SEND_ENVELOPE:
478 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800479 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700480 if (uiccCard == null) {
481 loge("sendEnvelopeWithStatus: No UICC");
482 request.result = new IccIoResult(0x6F, 0, (byte[])null);
483 synchronized (request) {
484 request.notifyAll();
485 }
486 } else {
487 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
488 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
489 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800490 break;
491
492 case EVENT_SEND_ENVELOPE_DONE:
493 ar = (AsyncResult) msg.obj;
494 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700495 if (ar.exception == null && ar.result != null) {
496 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800497 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700498 request.result = new IccIoResult(0x6F, 0, (byte[])null);
499 if (ar.result == null) {
500 loge("sendEnvelopeWithStatus: Empty response");
501 } else if (ar.exception instanceof CommandException) {
502 loge("sendEnvelopeWithStatus: CommandException: " +
503 ar.exception);
504 } else {
505 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
506 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800507 }
508 synchronized (request) {
509 request.notifyAll();
510 }
511 break;
512
Shishir Agrawal566b7612013-10-28 14:41:00 -0700513 case CMD_OPEN_CHANNEL:
514 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800515 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800516 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700517 if (uiccCard == null) {
518 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800519 request.result = new IccOpenLogicalChannelResponse(-1,
520 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700521 synchronized (request) {
522 request.notifyAll();
523 }
524 } else {
525 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800526 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
527 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700528 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700529 break;
530
531 case EVENT_OPEN_CHANNEL_DONE:
532 ar = (AsyncResult) msg.obj;
533 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700534 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700535 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700536 int[] result = (int[]) ar.result;
537 int channelId = result[0];
538 byte[] selectResponse = null;
539 if (result.length > 1) {
540 selectResponse = new byte[result.length - 1];
541 for (int i = 1; i < result.length; ++i) {
542 selectResponse[i - 1] = (byte) result[i];
543 }
544 }
545 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700546 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700547 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700548 if (ar.result == null) {
549 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700550 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700551 if (ar.exception != null) {
552 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
553 }
554
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700555 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700556 if (ar.exception instanceof CommandException) {
557 CommandException.Error error =
558 ((CommandException) (ar.exception)).getCommandError();
559 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700560 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700561 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700562 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700563 }
564 }
565 openChannelResp = new IccOpenLogicalChannelResponse(
566 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700567 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700568 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700569 synchronized (request) {
570 request.notifyAll();
571 }
572 break;
573
574 case CMD_CLOSE_CHANNEL:
575 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800576 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700577 if (uiccCard == null) {
578 loge("iccCloseLogicalChannel: No UICC");
579 request.result = new IccIoResult(0x6F, 0, (byte[])null);
580 synchronized (request) {
581 request.notifyAll();
582 }
583 } else {
584 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
585 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
586 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700587 break;
588
589 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800590 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
591 break;
592
593 case CMD_NV_READ_ITEM:
594 request = (MainThreadRequest) msg.obj;
595 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
596 mPhone.nvReadItem((Integer) request.argument, onCompleted);
597 break;
598
599 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700600 ar = (AsyncResult) msg.obj;
601 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800602 if (ar.exception == null && ar.result != null) {
603 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700604 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800605 request.result = "";
606 if (ar.result == null) {
607 loge("nvReadItem: Empty response");
608 } else if (ar.exception instanceof CommandException) {
609 loge("nvReadItem: CommandException: " +
610 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700611 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800612 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700613 }
614 }
615 synchronized (request) {
616 request.notifyAll();
617 }
618 break;
619
Jake Hambye994d462014-02-03 13:10:13 -0800620 case CMD_NV_WRITE_ITEM:
621 request = (MainThreadRequest) msg.obj;
622 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
623 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
624 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
625 break;
626
627 case EVENT_NV_WRITE_ITEM_DONE:
628 handleNullReturnEvent(msg, "nvWriteItem");
629 break;
630
631 case CMD_NV_WRITE_CDMA_PRL:
632 request = (MainThreadRequest) msg.obj;
633 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
634 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
635 break;
636
637 case EVENT_NV_WRITE_CDMA_PRL_DONE:
638 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
639 break;
640
641 case CMD_NV_RESET_CONFIG:
642 request = (MainThreadRequest) msg.obj;
643 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
644 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
645 break;
646
647 case EVENT_NV_RESET_CONFIG_DONE:
648 handleNullReturnEvent(msg, "nvResetConfig");
649 break;
650
Jake Hamby7c27be32014-03-03 13:25:59 -0800651 case CMD_GET_PREFERRED_NETWORK_TYPE:
652 request = (MainThreadRequest) msg.obj;
653 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700654 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800655 break;
656
657 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
658 ar = (AsyncResult) msg.obj;
659 request = (MainThreadRequest) ar.userObj;
660 if (ar.exception == null && ar.result != null) {
661 request.result = ar.result; // Integer
662 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800663 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800664 if (ar.result == null) {
665 loge("getPreferredNetworkType: Empty response");
666 } else if (ar.exception instanceof CommandException) {
667 loge("getPreferredNetworkType: CommandException: " +
668 ar.exception);
669 } else {
670 loge("getPreferredNetworkType: Unknown exception");
671 }
672 }
673 synchronized (request) {
674 request.notifyAll();
675 }
676 break;
677
678 case CMD_SET_PREFERRED_NETWORK_TYPE:
679 request = (MainThreadRequest) msg.obj;
680 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
681 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700682 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800683 break;
684
685 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
686 handleNullReturnEvent(msg, "setPreferredNetworkType");
687 break;
688
Steven Liu4bf01bc2014-07-17 11:05:29 -0500689 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
690 request = (MainThreadRequest)msg.obj;
691 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
692 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
693 break;
694
695 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
696 ar = (AsyncResult)msg.obj;
697 request = (MainThreadRequest)ar.userObj;
698 request.result = ar;
699 synchronized (request) {
700 request.notifyAll();
701 }
702 break;
703
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800704 case CMD_SET_VOICEMAIL_NUMBER:
705 request = (MainThreadRequest) msg.obj;
706 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
707 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800708 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
709 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800710 break;
711
712 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
713 handleNullReturnEvent(msg, "setVoicemailNumber");
714 break;
715
Stuart Scott54788802015-03-30 13:18:01 -0700716 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
717 request = (MainThreadRequest) msg.obj;
718 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
719 request);
720 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
721 break;
722
723 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
724 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
725 break;
726
Shishir Agrawal302c8692015-06-19 13:49:39 -0700727 case CMD_PERFORM_NETWORK_SCAN:
728 request = (MainThreadRequest) msg.obj;
729 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
730 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
731 break;
732
733 case EVENT_PERFORM_NETWORK_SCAN_DONE:
734 ar = (AsyncResult) msg.obj;
735 request = (MainThreadRequest) ar.userObj;
736 CellNetworkScanResult cellScanResult;
737 if (ar.exception == null && ar.result != null) {
738 cellScanResult = new CellNetworkScanResult(
739 CellNetworkScanResult.STATUS_SUCCESS,
740 (List<OperatorInfo>) ar.result);
741 } else {
742 if (ar.result == null) {
743 loge("getCellNetworkScanResults: Empty response");
744 }
745 if (ar.exception != null) {
746 loge("getCellNetworkScanResults: Exception: " + ar.exception);
747 }
748 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
749 if (ar.exception instanceof CommandException) {
750 CommandException.Error error =
751 ((CommandException) (ar.exception)).getCommandError();
752 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
753 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
754 } else if (error == CommandException.Error.GENERIC_FAILURE) {
755 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
756 }
757 }
758 cellScanResult = new CellNetworkScanResult(errorCode, null);
759 }
760 request.result = cellScanResult;
761 synchronized (request) {
762 request.notifyAll();
763 }
764 break;
765
766 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
767 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700768 ManualNetworkSelectionArgument selArg =
769 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700770 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
771 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700772 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
773 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700774 break;
775
776 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
777 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
778 break;
779
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700780 case CMD_GET_MODEM_ACTIVITY_INFO:
781 request = (MainThreadRequest) msg.obj;
782 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700783 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700784 break;
785
786 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
787 ar = (AsyncResult) msg.obj;
788 request = (MainThreadRequest) ar.userObj;
789 if (ar.exception == null && ar.result != null) {
790 request.result = ar.result;
791 } else {
792 if (ar.result == null) {
793 loge("queryModemActivityInfo: Empty response");
794 } else if (ar.exception instanceof CommandException) {
795 loge("queryModemActivityInfo: CommandException: " +
796 ar.exception);
797 } else {
798 loge("queryModemActivityInfo: Unknown exception");
799 }
800 }
Amit Mahajand4766222016-01-28 15:28:28 -0800801 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
802 if (request.result == null) {
803 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
804 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700805 synchronized (request) {
806 request.notifyAll();
807 }
808 break;
809
Meng Wang1a7c35a2016-05-05 20:56:15 -0700810 case CMD_SET_ALLOWED_CARRIERS:
811 request = (MainThreadRequest) msg.obj;
812 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
813 mPhone.setAllowedCarriers(
814 (List<CarrierIdentifier>) request.argument,
815 onCompleted);
816 break;
817
818 case EVENT_SET_ALLOWED_CARRIERS_DONE:
819 ar = (AsyncResult) msg.obj;
820 request = (MainThreadRequest) ar.userObj;
821 if (ar.exception == null && ar.result != null) {
822 request.result = ar.result;
823 } else {
824 if (ar.result == null) {
825 loge("setAllowedCarriers: Empty response");
826 } else if (ar.exception instanceof CommandException) {
827 loge("setAllowedCarriers: CommandException: " +
828 ar.exception);
829 } else {
830 loge("setAllowedCarriers: Unknown exception");
831 }
832 }
833 // Result cannot be null. Return -1 on error.
834 if (request.result == null) {
835 request.result = new int[]{-1};
836 }
837 synchronized (request) {
838 request.notifyAll();
839 }
840 break;
841
842 case CMD_GET_ALLOWED_CARRIERS:
843 request = (MainThreadRequest) msg.obj;
844 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
845 mPhone.getAllowedCarriers(onCompleted);
846 break;
847
848 case EVENT_GET_ALLOWED_CARRIERS_DONE:
849 ar = (AsyncResult) msg.obj;
850 request = (MainThreadRequest) ar.userObj;
851 if (ar.exception == null && ar.result != null) {
852 request.result = ar.result;
853 } else {
854 if (ar.result == null) {
855 loge("getAllowedCarriers: Empty response");
856 } else if (ar.exception instanceof CommandException) {
857 loge("getAllowedCarriers: CommandException: " +
858 ar.exception);
859 } else {
860 loge("getAllowedCarriers: Unknown exception");
861 }
862 }
863 // Result cannot be null. Return empty list of CarrierIdentifier.
864 if (request.result == null) {
865 request.result = new ArrayList<CarrierIdentifier>(0);
866 }
867 synchronized (request) {
868 request.notifyAll();
869 }
870 break;
871
Nathan Haroldb3014052017-01-25 15:57:32 -0800872 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
873 ar = (AsyncResult) msg.obj;
874 request = (MainThreadRequest) ar.userObj;
875 if (ar.exception == null && ar.result != null) {
876 request.result = ar.result;
877 } else {
878 request.result = new IllegalArgumentException(
879 "Failed to retrieve Forbidden Plmns");
880 if (ar.result == null) {
881 loge("getForbiddenPlmns: Empty response");
882 } else {
883 loge("getForbiddenPlmns: Unknown exception");
884 }
885 }
886 synchronized (request) {
887 request.notifyAll();
888 }
889 break;
890
891 case CMD_GET_FORBIDDEN_PLMNS:
892 request = (MainThreadRequest) msg.obj;
893 uiccCard = getUiccCardFromRequest(request);
894 if (uiccCard == null) {
895 loge("getForbiddenPlmns() UiccCard is null");
896 request.result = new IllegalArgumentException(
897 "getForbiddenPlmns() UiccCard is null");
898 synchronized (request) {
899 request.notifyAll();
900 }
901 break;
902 }
903 Integer appType = (Integer) request.argument;
904 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
905 if (uiccApp == null) {
906 loge("getForbiddenPlmns() no app with specified type -- "
907 + appType);
908 request.result = new IllegalArgumentException("Failed to get UICC App");
909 synchronized (request) {
910 request.notifyAll();
911 }
912 break;
913 } else {
914 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
915 + " specified type -- " + appType);
916 }
917 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
918 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
919 onCompleted);
920 break;
921
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700922 default:
923 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
924 break;
925 }
926 }
Jake Hambye994d462014-02-03 13:10:13 -0800927
928 private void handleNullReturnEvent(Message msg, String command) {
929 AsyncResult ar = (AsyncResult) msg.obj;
930 MainThreadRequest request = (MainThreadRequest) ar.userObj;
931 if (ar.exception == null) {
932 request.result = true;
933 } else {
934 request.result = false;
935 if (ar.exception instanceof CommandException) {
936 loge(command + ": CommandException: " + ar.exception);
937 } else {
938 loge(command + ": Unknown exception");
939 }
940 }
941 synchronized (request) {
942 request.notifyAll();
943 }
944 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 }
946
947 /**
948 * Posts the specified command to be executed on the main thread,
949 * waits for the request to complete, and returns the result.
950 * @see #sendRequestAsync
951 */
952 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800953 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700954 }
955
956 /**
957 * Posts the specified command to be executed on the main thread,
958 * waits for the request to complete, and returns the result.
959 * @see #sendRequestAsync
960 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800961 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700962 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
963 throw new RuntimeException("This method will deadlock if called from the main thread.");
964 }
965
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800966 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 Message msg = mMainThreadHandler.obtainMessage(command, request);
968 msg.sendToTarget();
969
970 // Wait for the request to complete
971 synchronized (request) {
972 while (request.result == null) {
973 try {
974 request.wait();
975 } catch (InterruptedException e) {
976 // Do nothing, go back and wait until the request is complete
977 }
978 }
979 }
980 return request.result;
981 }
982
983 /**
984 * Asynchronous ("fire and forget") version of sendRequest():
985 * Posts the specified command to be executed on the main thread, and
986 * returns immediately.
987 * @see #sendRequest
988 */
989 private void sendRequestAsync(int command) {
990 mMainThreadHandler.sendEmptyMessage(command);
991 }
992
993 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700994 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
995 * @see {@link #sendRequest(int,Object)}
996 */
997 private void sendRequestAsync(int command, Object argument) {
998 MainThreadRequest request = new MainThreadRequest(argument);
999 Message msg = mMainThreadHandler.obtainMessage(command, request);
1000 msg.sendToTarget();
1001 }
1002
1003 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001004 * Initialize the singleton PhoneInterfaceManager instance.
1005 * This is only done once, at startup, from PhoneApp.onCreate().
1006 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001007 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001008 synchronized (PhoneInterfaceManager.class) {
1009 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001010 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001011 } else {
1012 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1013 }
1014 return sInstance;
1015 }
1016 }
1017
1018 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001019 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 mApp = app;
1021 mPhone = phone;
1022 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001023 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001024 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1025 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001026 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001027 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001028 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001029
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001030 publish();
1031 }
1032
1033 private void publish() {
1034 if (DBG) log("publish: " + this);
1035
1036 ServiceManager.addService("phone", this);
1037 }
1038
Stuart Scott584921c2015-01-15 17:10:34 -08001039 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001040 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1041 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001042 }
1043
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001044 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1045 Phone phone = getPhoneFromRequest(request);
1046 return phone == null ? null :
1047 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1048 }
1049
Wink Saville36469e72014-06-11 15:17:00 -07001050 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001051 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001052 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001053 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 //
1055 // Implementation of the ITelephony interface.
1056 //
1057
1058 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001059 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001060 }
1061
Wink Savilleb564aae2014-10-23 10:18:09 -07001062 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001063 if (DBG) log("dial: " + number);
1064 // No permission check needed here: This is just a wrapper around the
1065 // ACTION_DIAL intent, which is available to any app since it puts up
1066 // the UI before it does anything.
1067
1068 String url = createTelUrl(number);
1069 if (url == null) {
1070 return;
1071 }
1072
1073 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001074 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001075 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1076 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1077 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1078 mApp.startActivity(intent);
1079 }
1080 }
1081
1082 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001083 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001084 }
1085
Wink Savilleb564aae2014-10-23 10:18:09 -07001086 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001087 if (DBG) log("call: " + number);
1088
1089 // This is just a wrapper around the ACTION_CALL intent, but we still
1090 // need to do a permission check since we're calling startActivity()
1091 // from the context of the phone app.
1092 enforceCallPermission();
1093
1094 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1095 != AppOpsManager.MODE_ALLOWED) {
1096 return;
1097 }
1098
1099 String url = createTelUrl(number);
1100 if (url == null) {
1101 return;
1102 }
1103
Wink Saville08874612014-08-31 19:19:58 -07001104 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001105 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001106 if (slist != null) {
1107 for (SubscriptionInfo subInfoRecord : slist) {
1108 if (subInfoRecord.getSubscriptionId() == subId) {
1109 isValid = true;
1110 break;
1111 }
Wink Saville08874612014-08-31 19:19:58 -07001112 }
1113 }
1114 if (isValid == false) {
1115 return;
1116 }
1117
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001119 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001120 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1121 mApp.startActivity(intent);
1122 }
1123
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001124 /**
1125 * End a call based on call state
1126 * @return true is a call was ended
1127 */
1128 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001129 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001130 }
1131
1132 /**
1133 * End a call based on the call state of the subId
1134 * @return true is a call was ended
1135 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001136 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001137 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001138 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001139 }
1140
1141 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001142 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001143 }
1144
Wink Savilleb564aae2014-10-23 10:18:09 -07001145 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001146 if (DBG) log("answerRingingCall...");
1147 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1148 // but that can probably wait till the big TelephonyManager API overhaul.
1149 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1150 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001151 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001152 }
1153
1154 /**
1155 * Make the actual telephony calls to implement answerRingingCall().
1156 * This should only be called from the main thread of the Phone app.
1157 * @see #answerRingingCall
1158 *
1159 * TODO: it would be nice to return true if we answered the call, or
1160 * false if there wasn't actually a ringing incoming call, or some
1161 * other error occurred. (In other words, pass back the return value
1162 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1163 * But that would require calling this method via sendRequest() rather
1164 * than sendRequestAsync(), and right now we don't actually *need* that
1165 * return value, so let's just return void for now.
1166 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001167 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001168 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001169 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001170 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1171 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001172 if (hasActiveCall && hasHoldingCall) {
1173 // Both lines are in use!
1174 // TODO: provide a flag to let the caller specify what
1175 // policy to use if both lines are in use. (The current
1176 // behavior is hardwired to "answer incoming, end ongoing",
1177 // which is how the CALL button is specced to behave.)
1178 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1179 return;
1180 } else {
1181 // answerCall() will automatically hold the current active
1182 // call, if there is one.
1183 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1184 return;
1185 }
1186 } else {
1187 // No call was ringing.
1188 return;
1189 }
1190 }
1191
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001192 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001193 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001195 public void silenceRinger() {
1196 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001197 }
1198
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001199 @Override
1200 public boolean isOffhook(String callingPackage) {
1201 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001202 }
1203
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001204 @Override
1205 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1206 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1207 return false;
1208 }
1209
Sanket Padawe356d7632015-06-22 14:03:32 -07001210 final Phone phone = getPhone(subId);
1211 if (phone != null) {
1212 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1213 } else {
1214 return false;
1215 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001216 }
1217
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001218 @Override
1219 public boolean isRinging(String callingPackage) {
1220 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001221 }
1222
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001223 @Override
1224 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1225 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1226 return false;
1227 }
1228
Sanket Padawe356d7632015-06-22 14:03:32 -07001229 final Phone phone = getPhone(subId);
1230 if (phone != null) {
1231 return (phone.getState() == PhoneConstants.State.RINGING);
1232 } else {
1233 return false;
1234 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001235 }
1236
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001237 @Override
1238 public boolean isIdle(String callingPackage) {
1239 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001240 }
1241
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001242 @Override
1243 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1244 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1245 return false;
1246 }
1247
Sanket Padawe356d7632015-06-22 14:03:32 -07001248 final Phone phone = getPhone(subId);
1249 if (phone != null) {
1250 return (phone.getState() == PhoneConstants.State.IDLE);
1251 } else {
1252 return false;
1253 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001254 }
1255
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001256 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001257 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001258 }
1259
Wink Savilleb564aae2014-10-23 10:18:09 -07001260 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001261 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001262 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1263 }
1264
1265 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001266 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001267 }
1268
Wink Savilleb564aae2014-10-23 10:18:09 -07001269 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001270 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001271 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1272 }
1273
1274 /** {@hide} */
1275 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001276 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001277 }
1278
Wink Savilleb564aae2014-10-23 10:18:09 -07001279 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001280 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001281 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 checkSimPin.start();
1283 return checkSimPin.unlockSim(null, pin);
1284 }
1285
Wink Saville9de0f752013-10-22 19:04:03 -07001286 /** {@hide} */
1287 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001288 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001289 }
1290
Wink Savilleb564aae2014-10-23 10:18:09 -07001291 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001292 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001293 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001294 checkSimPuk.start();
1295 return checkSimPuk.unlockSim(puk, pin);
1296 }
1297
1298 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001299 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001300 * a synchronous one.
1301 */
1302 private static class UnlockSim extends Thread {
1303
1304 private final IccCard mSimCard;
1305
1306 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001307 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1308 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001309
1310 // For replies from SimCard interface
1311 private Handler mHandler;
1312
1313 // For async handler to identify request type
1314 private static final int SUPPLY_PIN_COMPLETE = 100;
1315
1316 public UnlockSim(IccCard simCard) {
1317 mSimCard = simCard;
1318 }
1319
1320 @Override
1321 public void run() {
1322 Looper.prepare();
1323 synchronized (UnlockSim.this) {
1324 mHandler = new Handler() {
1325 @Override
1326 public void handleMessage(Message msg) {
1327 AsyncResult ar = (AsyncResult) msg.obj;
1328 switch (msg.what) {
1329 case SUPPLY_PIN_COMPLETE:
1330 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1331 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001332 mRetryCount = msg.arg1;
1333 if (ar.exception != null) {
1334 if (ar.exception instanceof CommandException &&
1335 ((CommandException)(ar.exception)).getCommandError()
1336 == CommandException.Error.PASSWORD_INCORRECT) {
1337 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1338 } else {
1339 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1340 }
1341 } else {
1342 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1343 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 mDone = true;
1345 UnlockSim.this.notifyAll();
1346 }
1347 break;
1348 }
1349 }
1350 };
1351 UnlockSim.this.notifyAll();
1352 }
1353 Looper.loop();
1354 }
1355
1356 /*
1357 * Use PIN or PUK to unlock SIM card
1358 *
1359 * If PUK is null, unlock SIM card with PIN
1360 *
1361 * If PUK is not null, unlock SIM card with PUK and set PIN code
1362 */
Wink Saville9de0f752013-10-22 19:04:03 -07001363 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001364
1365 while (mHandler == null) {
1366 try {
1367 wait();
1368 } catch (InterruptedException e) {
1369 Thread.currentThread().interrupt();
1370 }
1371 }
1372 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1373
1374 if (puk == null) {
1375 mSimCard.supplyPin(pin, callback);
1376 } else {
1377 mSimCard.supplyPuk(puk, pin, callback);
1378 }
1379
1380 while (!mDone) {
1381 try {
1382 Log.d(LOG_TAG, "wait for done");
1383 wait();
1384 } catch (InterruptedException e) {
1385 // Restore the interrupted status
1386 Thread.currentThread().interrupt();
1387 }
1388 }
1389 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001390 int[] resultArray = new int[2];
1391 resultArray[0] = mResult;
1392 resultArray[1] = mRetryCount;
1393 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001394 }
1395 }
1396
1397 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001398 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001399
1400 }
1401
Wink Savilleb564aae2014-10-23 10:18:09 -07001402 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 // No permission check needed here: this call is harmless, and it's
1404 // needed for the ServiceState.requestStateUpdate() call (which is
1405 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001406 final Phone phone = getPhone(subId);
1407 if (phone != null) {
1408 phone.updateServiceLocation();
1409 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001410 }
1411
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001412 @Override
1413 public boolean isRadioOn(String callingPackage) {
1414 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001415 }
1416
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001417 @Override
1418 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1419 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1420 return false;
1421 }
1422 return isRadioOnForSubscriber(subId);
1423 }
1424
1425 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001426 final Phone phone = getPhone(subId);
1427 if (phone != null) {
1428 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1429 } else {
1430 return false;
1431 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001432 }
1433
1434 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001435 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001436
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001437 }
Wink Saville36469e72014-06-11 15:17:00 -07001438
Wink Savilleb564aae2014-10-23 10:18:09 -07001439 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001440 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001441 final Phone phone = getPhone(subId);
1442 if (phone != null) {
1443 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1444 }
Wink Saville36469e72014-06-11 15:17:00 -07001445 }
1446
1447 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001448 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001449 }
1450
Wink Savilleb564aae2014-10-23 10:18:09 -07001451 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001452 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001453 final Phone phone = getPhone(subId);
1454 if (phone == null) {
1455 return false;
1456 }
1457 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001458 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001459 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001460 }
1461 return true;
1462 }
Wink Saville36469e72014-06-11 15:17:00 -07001463
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001464 public boolean needMobileRadioShutdown() {
1465 /*
1466 * If any of the Radios are available, it will need to be
1467 * shutdown. So return true if any Radio is available.
1468 */
1469 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1470 Phone phone = PhoneFactory.getPhone(i);
1471 if (phone != null && phone.isRadioAvailable()) return true;
1472 }
1473 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1474 return false;
1475 }
1476
1477 public void shutdownMobileRadios() {
1478 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1479 logv("Shutting down Phone " + i);
1480 shutdownRadioUsingPhoneId(i);
1481 }
1482 }
1483
1484 private void shutdownRadioUsingPhoneId(int phoneId) {
1485 enforceModifyPermission();
1486 Phone phone = PhoneFactory.getPhone(phoneId);
1487 if (phone != null && phone.isRadioAvailable()) {
1488 phone.shutdownRadio();
1489 }
1490 }
1491
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001492 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001493 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1494 if (defaultPhone != null) {
1495 defaultPhone.setRadioPower(turnOn);
1496 return true;
1497 } else {
1498 loge("There's no default phone.");
1499 return false;
1500 }
Wink Saville36469e72014-06-11 15:17:00 -07001501 }
1502
Wink Savilleb564aae2014-10-23 10:18:09 -07001503 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001504 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001505 final Phone phone = getPhone(subId);
1506 if (phone != null) {
1507 phone.setRadioPower(turnOn);
1508 return true;
1509 } else {
1510 return false;
1511 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001512 }
1513
Wink Saville36469e72014-06-11 15:17:00 -07001514 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001515 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001516 public boolean enableDataConnectivity() {
1517 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001518 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001519 final Phone phone = getPhone(subId);
1520 if (phone != null) {
1521 phone.setDataEnabled(true);
1522 return true;
1523 } else {
1524 return false;
1525 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001526 }
1527
Wink Saville36469e72014-06-11 15:17:00 -07001528 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001529 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001530 public boolean disableDataConnectivity() {
1531 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001532 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001533 final Phone phone = getPhone(subId);
1534 if (phone != null) {
1535 phone.setDataEnabled(false);
1536 return true;
1537 } else {
1538 return false;
1539 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001540 }
1541
Wink Saville36469e72014-06-11 15:17:00 -07001542 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001543 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001544 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001545 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001546 final Phone phone = getPhone(subId);
1547 if (phone != null) {
1548 return phone.isDataConnectivityPossible();
1549 } else {
1550 return false;
1551 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001552 }
1553
1554 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001555 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001556 }
1557
pkanwarae03a6b2016-11-06 20:37:09 -08001558 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001559 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001560 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1561 return;
1562 }
1563 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1564 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1565 };
1566
Wink Savilleb564aae2014-10-23 10:18:09 -07001567 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001569 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1570 return false;
1571 }
Wink Saville36469e72014-06-11 15:17:00 -07001572 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001573 }
1574
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001575 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001576 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001577 }
1578
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001579 public int getCallStateForSlot(int slotIndex) {
1580 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001581 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harolda667c152016-12-14 11:27:20 -08001582 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583 }
1584
Sanket Padawe356d7632015-06-22 14:03:32 -07001585 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001586 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001587 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001588 if (phone != null) {
Nathan Harolda667c152016-12-14 11:27:20 -08001589 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001590 } else {
Nathan Harolda667c152016-12-14 11:27:20 -08001591 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001592 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001593 }
1594
Sanket Padawe356d7632015-06-22 14:03:32 -07001595 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001596 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001597 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001598 if (phone != null) {
1599 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1600 } else {
1601 return TelephonyManager.DATA_ACTIVITY_NONE;
1602 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 }
1604
1605 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001606 public Bundle getCellLocation(String callingPackage) {
1607 enforceFineOrCoarseLocationPermission("getCellLocation");
1608
1609 // OP_COARSE_LOCATION controls both fine and coarse location.
1610 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1611 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001612 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001613 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001614 }
1615
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001616 if (checkIfCallerIsSelfOrForegroundUser() ||
1617 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001618 if (DBG_LOC) log("getCellLocation: is active user");
1619 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001620 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001621 if (phone == null) {
1622 return null;
1623 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001624
1625 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1626 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001627 return data;
1628 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001629 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001630 return null;
1631 }
1632 }
1633
Svetoslav64fad262015-04-14 14:35:21 -07001634 private void enforceFineOrCoarseLocationPermission(String message) {
1635 try {
1636 mApp.enforceCallingOrSelfPermission(
1637 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1638 } catch (SecurityException e) {
1639 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1640 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1641 // is the weaker precondition
1642 mApp.enforceCallingOrSelfPermission(
1643 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1644 }
1645 }
1646
1647
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001648 @Override
1649 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001650 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001651 }
1652
Sanket Padawe356d7632015-06-22 14:03:32 -07001653 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001654 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001655 mApp.enforceCallingOrSelfPermission(
1656 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001657 final Phone phone = getPhone(subId);
1658 if (phone != null) {
1659 phone.enableLocationUpdates();
1660 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001661 }
1662
1663 @Override
1664 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001665 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001666 }
1667
Sanket Padawe356d7632015-06-22 14:03:32 -07001668 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001669 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001670 mApp.enforceCallingOrSelfPermission(
1671 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001672 final Phone phone = getPhone(subId);
1673 if (phone != null) {
1674 phone.disableLocationUpdates();
1675 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001676 }
1677
1678 @Override
1679 @SuppressWarnings("unchecked")
1680 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001681 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1682
1683 // OP_COARSE_LOCATION controls both fine and coarse location.
1684 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1685 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1686 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001687 }
1688
1689 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1690 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1691 return null;
1692 }
Svetoslav64fad262015-04-14 14:35:21 -07001693
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001694 if (checkIfCallerIsSelfOrForegroundUser() ||
1695 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001696 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1697
1698 ArrayList<NeighboringCellInfo> cells = null;
1699
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001700 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001701 try {
1702 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001703 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001704 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001705 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001706 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001707 }
1708 return cells;
1709 } else {
1710 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1711 return null;
1712 }
1713 }
1714
1715
1716 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001717 public List<CellInfo> getAllCellInfo(String callingPackage) {
1718 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1719
1720 // OP_COARSE_LOCATION controls both fine and coarse location.
1721 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1722 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1723 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001724 }
1725
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001726 if (checkIfCallerIsSelfOrForegroundUser() ||
1727 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001728 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001729 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001730 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1731 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001732 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1733 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001734 }
1735 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001736 } else {
1737 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1738 return null;
1739 }
1740 }
1741
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001742 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001743 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08001744 enforceModifyPermission();
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001745 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1746 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001747 }
1748
Shishir Agrawala9f32182016-04-12 12:00:16 -07001749 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001750 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001751 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1752 return null;
1753 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001754 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001755 return phone == null ? null : phone.getImei();
1756 }
1757
1758 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07001759 public String getMeidForSlot(int slotIndex, String callingPackage) {
1760 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1761 return null;
1762 }
1763 Phone phone = PhoneFactory.getPhone(slotIndex);
1764 return phone == null ? null : phone.getMeid();
1765 }
1766
1767 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001768 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001769 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1770 return null;
1771 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001772 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001773 return phone == null ? null : phone.getDeviceSvn();
1774 }
1775
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001776 //
1777 // Internal helper methods.
1778 //
1779
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001780 /**
1781 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1782 */
1783 private boolean checkCallerInteractAcrossUsersFull() {
1784 return mPhone.getContext().checkCallingOrSelfPermission(
1785 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1786 == PackageManager.PERMISSION_GRANTED;
1787 }
1788
Jake Hambye994d462014-02-03 13:10:13 -08001789 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001790 boolean ok;
1791
1792 boolean self = Binder.getCallingUid() == Process.myUid();
1793 if (!self) {
1794 // Get the caller's user id then clear the calling identity
1795 // which will be restored in the finally clause.
1796 int callingUser = UserHandle.getCallingUserId();
1797 long ident = Binder.clearCallingIdentity();
1798
1799 try {
1800 // With calling identity cleared the current user is the foreground user.
1801 int foregroundUser = ActivityManager.getCurrentUser();
1802 ok = (foregroundUser == callingUser);
1803 if (DBG_LOC) {
1804 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1805 + " callingUser=" + callingUser + " ok=" + ok);
1806 }
1807 } catch (Exception ex) {
1808 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1809 ok = false;
1810 } finally {
1811 Binder.restoreCallingIdentity(ident);
1812 }
1813 } else {
1814 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1815 ok = true;
1816 }
1817 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1818 return ok;
1819 }
1820
1821 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001822 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1823 *
1824 * @throws SecurityException if the caller does not have the required permission
1825 */
1826 private void enforceModifyPermission() {
1827 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1828 }
1829
1830 /**
Junda Liua2e36012014-07-09 18:30:01 -07001831 * Make sure either system app or the caller has carrier privilege.
1832 *
1833 * @throws SecurityException if the caller does not have the required permission/privilege
1834 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001835 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001836 int permission = mApp.checkCallingOrSelfPermission(
1837 android.Manifest.permission.MODIFY_PHONE_STATE);
1838 if (permission == PackageManager.PERMISSION_GRANTED) {
1839 return;
1840 }
1841
1842 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001843 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001844 }
1845
1846 /**
1847 * Make sure the caller has carrier privilege.
1848 *
1849 * @throws SecurityException if the caller does not have the required permission
1850 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001851 private void enforceCarrierPrivilege(int subId) {
1852 if (getCarrierPrivilegeStatus(subId) !=
1853 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001854 loge("No Carrier Privilege.");
1855 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001856 }
1857 }
1858
1859 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001860 * Make sure the caller has the CALL_PHONE permission.
1861 *
1862 * @throws SecurityException if the caller does not have the required permission
1863 */
1864 private void enforceCallPermission() {
1865 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1866 }
1867
Stuart Scott8eef64f2015-04-08 15:13:54 -07001868 private void enforceConnectivityInternalPermission() {
1869 mApp.enforceCallingOrSelfPermission(
1870 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1871 "ConnectivityService");
1872 }
1873
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001874 private String createTelUrl(String number) {
1875 if (TextUtils.isEmpty(number)) {
1876 return null;
1877 }
1878
Jake Hambye994d462014-02-03 13:10:13 -08001879 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001880 }
1881
Ihab Awadf9e92732013-12-05 18:02:52 -08001882 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001883 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1884 }
1885
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001886 private static void logv(String msg) {
1887 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1888 }
1889
Ihab Awadf9e92732013-12-05 18:02:52 -08001890 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001891 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1892 }
1893
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001894 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001895 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001896 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001897 }
1898
Sanket Padawe356d7632015-06-22 14:03:32 -07001899 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001900 public int getActivePhoneTypeForSlot(int slotIndex) {
1901 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001902 if (phone == null) {
1903 return PhoneConstants.PHONE_TYPE_NONE;
1904 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001905 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001906 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001907 }
1908
1909 /**
1910 * Returns the CDMA ERI icon index to display
1911 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001912 @Override
1913 public int getCdmaEriIconIndex(String callingPackage) {
1914 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001915 }
1916
Sanket Padawe356d7632015-06-22 14:03:32 -07001917 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001918 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1919 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1920 return -1;
1921 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001922 final Phone phone = getPhone(subId);
1923 if (phone != null) {
1924 return phone.getCdmaEriIconIndex();
1925 } else {
1926 return -1;
1927 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001928 }
1929
1930 /**
1931 * Returns the CDMA ERI icon mode,
1932 * 0 - ON
1933 * 1 - FLASHING
1934 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001935 @Override
1936 public int getCdmaEriIconMode(String callingPackage) {
1937 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001938 }
1939
Sanket Padawe356d7632015-06-22 14:03:32 -07001940 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001941 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1942 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1943 return -1;
1944 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001945 final Phone phone = getPhone(subId);
1946 if (phone != null) {
1947 return phone.getCdmaEriIconMode();
1948 } else {
1949 return -1;
1950 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001951 }
1952
1953 /**
1954 * Returns the CDMA ERI text,
1955 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001956 @Override
1957 public String getCdmaEriText(String callingPackage) {
1958 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001959 }
1960
Sanket Padawe356d7632015-06-22 14:03:32 -07001961 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001962 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1963 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1964 return null;
1965 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001966 final Phone phone = getPhone(subId);
1967 if (phone != null) {
1968 return phone.getCdmaEriText();
1969 } else {
1970 return null;
1971 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001972 }
1973
1974 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001975 * Returns the CDMA MDN.
1976 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001977 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001978 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001979 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001980 final Phone phone = getPhone(subId);
1981 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1982 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001983 } else {
1984 return null;
1985 }
1986 }
1987
1988 /**
1989 * Returns the CDMA MIN.
1990 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001991 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001992 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001993 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001994 final Phone phone = getPhone(subId);
1995 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1996 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001997 } else {
1998 return null;
1999 }
2000 }
2001
2002 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002003 * Returns true if CDMA provisioning needs to run.
2004 */
2005 public boolean needsOtaServiceProvisioning() {
2006 return mPhone.needsOtaServiceProvisioning();
2007 }
2008
2009 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002010 * Sets the voice mail number of a given subId.
2011 */
2012 @Override
2013 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002014 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002015 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2016 new Pair<String, String>(alphaTag, number), new Integer(subId));
2017 return success;
2018 }
2019
Ta-wei Yen87c49842016-05-13 21:19:52 -07002020 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002021 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2022 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2023 String systemDialer = TelecomManager.from(mPhone.getContext()).getSystemDialerPackage();
2024 if (!TextUtils.equals(callingPackage, systemDialer)) {
2025 throw new SecurityException("caller must be system dialer");
2026 }
2027 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2028 if (phoneAccountHandle == null){
2029 return null;
2030 }
2031 return VisualVoicemailSettingsUtil.dump(mPhone.getContext(), phoneAccountHandle);
2032 }
2033
2034 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002035 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002036 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2037 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2038 return null;
2039 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002040 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2041 }
2042
2043 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002044 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2045 VisualVoicemailSmsFilterSettings settings) {
2046 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002047 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002048 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2049 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002050 }
2051
2052 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002053 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2054 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002055 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002056 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002057 }
2058
2059 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002060 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2061 String callingPackage, int subId) {
2062 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002063 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002064 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002065 }
2066
2067 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002068 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002069 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002070 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002071 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2072 }
2073
2074 @Override
2075 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2076 String number, int port, String text, PendingIntent sentIntent) {
2077 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002078 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002079 enforceSendSmsPermission();
2080 // Make the calls as the phone process.
2081 final long identity = Binder.clearCallingIdentity();
2082 try {
2083 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2084 if (port == 0) {
2085 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2086 sentIntent, null, false);
2087 } else {
2088 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2089 smsManager.sendDataMessageWithSelfPermissions(number, null,
2090 (short) port, data, sentIntent, null);
2091 }
2092 } finally {
2093 Binder.restoreCallingIdentity(identity);
2094 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002095 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002096 /**
fionaxu0152e512016-11-14 13:36:14 -08002097 * Sets the voice activation state of a given subId.
2098 */
2099 @Override
2100 public void setVoiceActivationState(int subId, int activationState) {
2101 enforceModifyPermissionOrCarrierPrivilege(subId);
2102 final Phone phone = getPhone(subId);
2103 if (phone != null) {
2104 phone.setVoiceActivationState(activationState);
2105 } else {
2106 loge("setVoiceActivationState fails with invalid subId: " + subId);
2107 }
2108 }
2109
2110 /**
2111 * Sets the data activation state of a given subId.
2112 */
2113 @Override
2114 public void setDataActivationState(int subId, int activationState) {
2115 enforceModifyPermissionOrCarrierPrivilege(subId);
2116 final Phone phone = getPhone(subId);
2117 if (phone != null) {
2118 phone.setDataActivationState(activationState);
2119 } else {
2120 loge("setVoiceActivationState fails with invalid subId: " + subId);
2121 }
2122 }
2123
2124 /**
2125 * Returns the voice activation state of a given subId.
2126 */
2127 @Override
2128 public int getVoiceActivationState(int subId, String callingPackage) {
2129 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2130 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2131 }
2132 final Phone phone = getPhone(subId);
2133 if (phone != null) {
2134 return phone.getVoiceActivationState();
2135 } else {
2136 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2137 }
2138 }
2139
2140 /**
2141 * Returns the data activation state of a given subId.
2142 */
2143 @Override
2144 public int getDataActivationState(int subId, String callingPackage) {
2145 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2146 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2147 }
2148 final Phone phone = getPhone(subId);
2149 if (phone != null) {
2150 return phone.getDataActivationState();
2151 } else {
2152 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2153 }
2154 }
2155
2156 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002157 * Returns the unread count of voicemails
2158 */
2159 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002160 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002161 }
2162
2163 /**
2164 * Returns the unread count of voicemails for a subId
2165 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002166 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002167 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002168 final Phone phone = getPhone(subId);
2169 if (phone != null) {
2170 return phone.getVoiceMessageCount();
2171 } else {
2172 return 0;
2173 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002174 }
2175
2176 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002177 * returns true, if the device is in a state where both voice and data
2178 * are supported simultaneously. This can change based on location or network condition.
2179 */
2180 @Override
2181 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2182 final Phone phone = getPhone(subId);
2183 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2184 }
2185
2186 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002187 * Send the dialer code if called from the current default dialer or the caller has
2188 * carrier privilege.
2189 * @param inputCode The dialer code to send
2190 */
2191 @Override
2192 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
2193 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2194 String defaultDialer = TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage();
2195 if (!TextUtils.equals(callingPackage, defaultDialer)) {
2196 enforceCarrierPrivilege(getDefaultSubscription());
2197 }
2198 mPhone.sendDialerSpecialCode(inputCode);
2199 }
2200
2201 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002202 * Returns the data network type.
2203 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002204 *
2205 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2206 */
2207 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002208 public int getNetworkType() {
2209 final Phone phone = getPhone(getDefaultSubscription());
2210 if (phone != null) {
2211 return phone.getServiceState().getDataNetworkType();
2212 } else {
2213 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2214 }
Wink Saville36469e72014-06-11 15:17:00 -07002215 }
2216
2217 /**
2218 * Returns the network type for a subId
2219 */
2220 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002221 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2222 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2223 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2224 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002225
Sanket Padawe356d7632015-06-22 14:03:32 -07002226 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002227 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002228 return phone.getServiceState().getDataNetworkType();
2229 } else {
2230 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2231 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002232 }
2233
2234 /**
2235 * Returns the data network type
2236 */
2237 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002238 public int getDataNetworkType(String callingPackage) {
2239 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002240 }
2241
2242 /**
2243 * Returns the data network type for a subId
2244 */
2245 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002246 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2247 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2248 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2249 }
2250
Sanket Padawe356d7632015-06-22 14:03:32 -07002251 final Phone phone = getPhone(subId);
2252 if (phone != null) {
2253 return phone.getServiceState().getDataNetworkType();
2254 } else {
2255 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2256 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002257 }
2258
2259 /**
Wink Saville36469e72014-06-11 15:17:00 -07002260 * Returns the Voice network type for a subId
2261 */
2262 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002263 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2264 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2265 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2266 }
2267
Sanket Padawe356d7632015-06-22 14:03:32 -07002268 final Phone phone = getPhone(subId);
2269 if (phone != null) {
2270 return phone.getServiceState().getVoiceNetworkType();
2271 } else {
2272 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2273 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002274 }
2275
2276 /**
2277 * @return true if a ICC card is present
2278 */
2279 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002280 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002281 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2282 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002283 }
2284
2285 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002286 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002287 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002288 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002289 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2290 final Phone phone = PhoneFactory.getPhone(slotIndex);
fionaxu6e6c68b2016-06-23 13:31:32 -07002291 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002292 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002293 } else {
2294 return false;
2295 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002296 }
2297
2298 /**
2299 * Return if the current radio is LTE on CDMA. This
2300 * is a tri-state return value as for a period of time
2301 * the mode may be unknown.
2302 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002303 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002304 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002305 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002306 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002307 @Override
2308 public int getLteOnCdmaMode(String callingPackage) {
2309 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002310 }
2311
Sanket Padawe356d7632015-06-22 14:03:32 -07002312 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002313 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2314 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2315 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2316 }
2317
Sanket Padawe356d7632015-06-22 14:03:32 -07002318 final Phone phone = getPhone(subId);
2319 if (phone == null) {
2320 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2321 } else {
2322 return phone.getLteOnCdmaMode();
2323 }
Wink Saville36469e72014-06-11 15:17:00 -07002324 }
2325
2326 public void setPhone(Phone phone) {
2327 mPhone = phone;
2328 }
2329
2330 /**
2331 * {@hide}
2332 * Returns Default subId, 0 in the case of single standby.
2333 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002334 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002335 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002336 }
2337
Shishir Agrawala9f32182016-04-12 12:00:16 -07002338 private int getSlotForDefaultSubscription() {
2339 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2340 }
2341
Wink Savilleb564aae2014-10-23 10:18:09 -07002342 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002343 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002344 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002345
2346 /**
2347 * @see android.telephony.TelephonyManager.WifiCallingChoices
2348 */
2349 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002350 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2351 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002352 }
2353
2354 /**
2355 * @see android.telephony.TelephonyManager.WifiCallingChoices
2356 */
2357 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002358 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2359 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2360 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002361 }
2362
Sailesh Nepald1e68152013-12-12 19:08:02 -08002363 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002364 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002365 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002366 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002367
Shishir Agrawal566b7612013-10-28 14:41:00 -07002368 @Override
Ajay Nambid7454d32015-12-03 13:50:00 -08002369 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002370 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002371
Ajay Nambid7454d32015-12-03 13:50:00 -08002372 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002373 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambid7454d32015-12-03 13:50:00 -08002374 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002375 if (DBG) log("iccOpenLogicalChannel: " + response);
2376 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002377 }
2378
2379 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002380 public boolean iccCloseLogicalChannel(int subId, int channel) {
2381 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002382
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002383 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002384 if (channel < 0) {
2385 return false;
2386 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002387 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002388 if (DBG) log("iccCloseLogicalChannel: " + success);
2389 return success;
2390 }
2391
2392 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002393 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002394 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002395 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002396
2397 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002398 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2399 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002400 " data=" + data);
2401 }
2402
2403 if (channel < 0) {
2404 return "";
2405 }
2406
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002407 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002408 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002409 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2410
Shishir Agrawal566b7612013-10-28 14:41:00 -07002411 // Append the returned status code to the end of the response payload.
2412 String s = Integer.toHexString(
2413 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002414 if (response.payload != null) {
2415 s = IccUtils.bytesToHexString(response.payload) + s;
2416 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002417 return s;
2418 }
Jake Hambye994d462014-02-03 13:10:13 -08002419
Evan Charltonc66da362014-05-16 14:06:40 -07002420 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002421 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002422 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002423 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002424
2425 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002426 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2427 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002428 }
2429
2430 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002431 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002432 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2433
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002434 // Append the returned status code to the end of the response payload.
2435 String s = Integer.toHexString(
2436 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002437 if (response.payload != null) {
2438 s = IccUtils.bytesToHexString(response.payload) + s;
2439 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002440 return s;
2441 }
2442
2443 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002444 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002445 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002446 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002447
2448 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002449 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002450 p1 + " " + p2 + " " + p3 + ":" + filePath);
2451 }
2452
2453 IccIoResult response =
2454 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002455 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2456 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002457
2458 if (DBG) {
2459 log("Exchange SIM_IO [R]" + response);
2460 }
2461
2462 byte[] result = null;
2463 int length = 2;
2464 if (response.payload != null) {
2465 length = 2 + response.payload.length;
2466 result = new byte[length];
2467 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2468 } else {
2469 result = new byte[length];
2470 }
2471
2472 result[length - 1] = (byte) response.sw2;
2473 result[length - 2] = (byte) response.sw1;
2474 return result;
2475 }
2476
Nathan Haroldb3014052017-01-25 15:57:32 -08002477 /**
2478 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2479 * on a particular subscription
2480 */
2481 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002482 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2483 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002484 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2485 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2486 return null;
2487 }
2488 Object response = sendRequest(
2489 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2490 if (response instanceof String[]) {
2491 return (String[]) response;
2492 }
2493 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2494 return null;
2495 }
2496
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002497 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002498 public String sendEnvelopeWithStatus(int subId, String content) {
2499 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002500
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002501 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002502 if (response.payload == null) {
2503 return "";
2504 }
2505
2506 // Append the returned status code to the end of the response payload.
2507 String s = Integer.toHexString(
2508 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2509 s = IccUtils.bytesToHexString(response.payload) + s;
2510 return s;
2511 }
2512
Jake Hambye994d462014-02-03 13:10:13 -08002513 /**
2514 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2515 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2516 *
2517 * @param itemID the ID of the item to read
2518 * @return the NV item as a String, or null on error.
2519 */
2520 @Override
2521 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002522 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002523 if (DBG) log("nvReadItem: item " + itemID);
2524 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2525 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2526 return value;
2527 }
2528
2529 /**
2530 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2531 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2532 *
2533 * @param itemID the ID of the item to read
2534 * @param itemValue the value to write, as a String
2535 * @return true on success; false on any failure
2536 */
2537 @Override
2538 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002539 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002540 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2541 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2542 new Pair<Integer, String>(itemID, itemValue));
2543 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2544 return success;
2545 }
2546
2547 /**
2548 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2549 * Used for device configuration by some CDMA operators.
2550 *
2551 * @param preferredRoamingList byte array containing the new PRL
2552 * @return true on success; false on any failure
2553 */
2554 @Override
2555 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002556 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002557 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2558 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2559 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2560 return success;
2561 }
2562
2563 /**
2564 * Perform the specified type of NV config reset.
2565 * Used for device configuration by some CDMA operators.
2566 *
2567 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2568 * @return true on success; false on any failure
2569 */
2570 @Override
2571 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002572 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002573 if (DBG) log("nvResetConfig: type " + resetType);
2574 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2575 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2576 return success;
2577 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002578
2579 /**
Wink Saville36469e72014-06-11 15:17:00 -07002580 * {@hide}
2581 * Returns Default sim, 0 in the case of single standby.
2582 */
2583 public int getDefaultSim() {
2584 //TODO Need to get it from Telephony Devcontroller
2585 return 0;
2586 }
2587
Svet Ganovb320e182015-04-16 12:30:10 -07002588 public String[] getPcscfAddress(String apnType, String callingPackage) {
2589 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2590 return new String[0];
2591 }
2592
2593
ram87fca6f2014-07-18 18:58:44 +05302594 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002595 }
2596
Brad Ebinger51f743a2017-01-23 13:50:20 -08002597 /**
2598 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2599 * feature or {@link null} if the service is not available. If an ImsServiceController is
2600 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2601 * feature updates.
2602 */
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002603 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002604 IImsServiceFeatureListener callback) {
2605 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002606 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002607 callback);
2608 }
2609
Wink Saville36469e72014-06-11 15:17:00 -07002610 public void setImsRegistrationState(boolean registered) {
2611 enforceModifyPermission();
2612 mPhone.setImsRegistrationState(registered);
2613 }
2614
2615 /**
Stuart Scott54788802015-03-30 13:18:01 -07002616 * Set the network selection mode to automatic.
2617 *
2618 */
2619 @Override
2620 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002621 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002622 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2623 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2624 }
2625
2626 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002627 * Set the network selection mode to manual with the selected carrier.
2628 */
2629 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002630 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2631 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002632 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002633 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002634 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2635 persistSelection);
2636 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002637 }
2638
2639 /**
2640 * Scans for available networks.
2641 */
2642 @Override
2643 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002644 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002645 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2646 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2647 CMD_PERFORM_NETWORK_SCAN, null, subId);
2648 return result;
2649 }
2650
2651 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002652 * Get the calculated preferred network type.
2653 * Used for debugging incorrect network type.
2654 *
2655 * @return the preferred network type, defined in RILConstants.java.
2656 */
2657 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002658 public int getCalculatedPreferredNetworkType(String callingPackage) {
2659 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2660 return RILConstants.PREFERRED_NETWORK_MODE;
2661 }
2662
Amit Mahajan43330e02014-11-18 11:54:45 -08002663 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002664 }
2665
2666 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002667 * Get the preferred network type.
2668 * Used for device configuration by some CDMA operators.
2669 *
2670 * @return the preferred network type, defined in RILConstants.java.
2671 */
2672 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002673 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002674 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002675 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002676 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002677 int networkType = (result != null ? result[0] : -1);
2678 if (DBG) log("getPreferredNetworkType: " + networkType);
2679 return networkType;
2680 }
2681
2682 /**
2683 * Set the preferred network type.
2684 * Used for device configuration by some CDMA operators.
2685 *
2686 * @param networkType the preferred network type, defined in RILConstants.java.
2687 * @return true on success; false on any failure.
2688 */
2689 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002690 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002691 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002692 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2693 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002694 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002695 if (success) {
2696 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002697 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002698 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002699 return success;
2700 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002701
2702 /**
Junda Liu475951f2014-11-07 16:45:03 -08002703 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2704 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2705 * tethering.
2706 *
2707 * @return 0: Not required. 1: required. 2: Not set.
2708 * @hide
2709 */
2710 @Override
2711 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002712 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002713 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2714 Settings.Global.TETHER_DUN_REQUIRED, 2);
2715 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2716 // config_tether_apndata.
2717 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2718 dunRequired = 1;
2719 }
2720 return dunRequired;
2721 }
2722
2723 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002724 * Set mobile data enabled
2725 * Used by the user through settings etc to turn on/off mobile data
2726 *
2727 * @param enable {@code true} turn turn data on, else {@code false}
2728 */
2729 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002730 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002731 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002732 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002733 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002734 Phone phone = PhoneFactory.getPhone(phoneId);
2735 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002736 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002737 phone.setDataEnabled(enable);
2738 } else {
2739 loge("setDataEnabled: no phone for subId=" + subId);
2740 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002741 }
2742
2743 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002744 * Get whether mobile data is enabled.
2745 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002746 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002747 *
2748 * @return {@code true} if data is enabled else {@code false}
2749 */
2750 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002751 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002752 try {
2753 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2754 null);
2755 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002756 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002757 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002758 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002759 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002760 Phone phone = PhoneFactory.getPhone(phoneId);
2761 if (phone != null) {
2762 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002763 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002764 return retVal;
2765 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002766 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002767 return false;
2768 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002769 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002770
2771 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002772 public int getCarrierPrivilegeStatus(int subId) {
2773 final Phone phone = getPhone(subId);
2774 if (phone == null) {
2775 loge("getCarrierPrivilegeStatus: Invalid subId");
2776 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2777 }
2778 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002779 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002780 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002781 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2782 }
2783 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002784 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002785 }
Junda Liu29340342014-07-10 15:23:27 -07002786
2787 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002788 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002789 if (TextUtils.isEmpty(pkgName))
2790 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002791 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002792 if (card == null) {
2793 loge("checkCarrierPrivilegesForPackage: No UICC");
2794 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2795 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002796 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2797 }
2798
2799 @Override
2800 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002801 if (TextUtils.isEmpty(pkgName))
2802 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002803 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2804 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2805 UiccCard card = UiccController.getInstance().getUiccCard(i);
2806 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002807 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002808 continue;
2809 }
2810
2811 result = card.getCarrierPrivilegeStatus(
2812 mPhone.getContext().getPackageManager(), pkgName);
2813 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2814 break;
2815 }
2816 }
2817
2818 return result;
Junda Liu29340342014-07-10 15:23:27 -07002819 }
Derek Tan89e89d42014-07-08 17:00:10 -07002820
2821 @Override
Junda Liue64de782015-04-16 17:19:16 -07002822 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2823 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2824 loge("phoneId " + phoneId + " is not valid.");
2825 return null;
2826 }
2827 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002828 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002829 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002830 return null ;
2831 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002832 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002833 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002834 }
2835
Amith Yamasani6e118872016-02-19 12:53:51 -08002836 @Override
2837 public List<String> getPackagesWithCarrierPrivileges() {
2838 PackageManager pm = mPhone.getContext().getPackageManager();
2839 List<String> privilegedPackages = new ArrayList<>();
2840 List<PackageInfo> packages = null;
2841 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2842 UiccCard card = UiccController.getInstance().getUiccCard(i);
2843 if (card == null) {
2844 // No UICC in that slot.
2845 continue;
2846 }
2847 if (card.hasCarrierPrivilegeRules()) {
2848 if (packages == null) {
2849 // Only check packages in user 0 for now
2850 packages = pm.getInstalledPackagesAsUser(
2851 PackageManager.MATCH_DISABLED_COMPONENTS
2852 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2853 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2854 }
2855 for (int p = packages.size() - 1; p >= 0; p--) {
2856 PackageInfo pkgInfo = packages.get(p);
2857 if (pkgInfo != null && pkgInfo.packageName != null
2858 && card.getCarrierPrivilegeStatus(pkgInfo)
2859 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2860 privilegedPackages.add(pkgInfo.packageName);
2861 }
2862 }
2863 }
2864 }
2865 return privilegedPackages;
2866 }
2867
Wink Savilleb564aae2014-10-23 10:18:09 -07002868 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002869 final Phone phone = getPhone(subId);
2870 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002871 if (card == null) {
2872 loge("getIccId: No UICC");
2873 return null;
2874 }
2875 String iccId = card.getIccId();
2876 if (TextUtils.isEmpty(iccId)) {
2877 loge("getIccId: ICC ID is null or empty.");
2878 return null;
2879 }
2880 return iccId;
2881 }
2882
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002883 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002884 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2885 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002886 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002887
Jeff Sharkey85190e62014-12-05 09:40:12 -08002888 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002889 final Phone phone = getPhone(subId);
2890 if (phone == null) {
2891 return false;
2892 }
2893 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002894
2895 if (DBG_MERGE) {
2896 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2897 + subscriberId + " to " + number);
2898 }
2899
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002900 if (TextUtils.isEmpty(iccId)) {
2901 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002902 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002903
Jeff Sharkey85190e62014-12-05 09:40:12 -08002904 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2905
2906 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002907 if (alphaTag == null) {
2908 editor.remove(alphaTagPrefKey);
2909 } else {
2910 editor.putString(alphaTagPrefKey, alphaTag);
2911 }
2912
Jeff Sharkey85190e62014-12-05 09:40:12 -08002913 // Record both the line number and IMSI for this ICCID, since we need to
2914 // track all merged IMSIs based on line number
2915 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2916 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002917 if (number == null) {
2918 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002919 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002920 } else {
2921 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002922 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002923 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002924
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002925 editor.commit();
2926 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002927 }
2928
2929 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002930 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002931 // This is open to apps with WRITE_SMS.
2932 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002933 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002934 return null;
2935 }
Derek Tan97ebb422014-09-05 16:55:38 -07002936
2937 String iccId = getIccId(subId);
2938 if (iccId != null) {
2939 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002940 if (DBG_MERGE) {
2941 log("getLine1NumberForDisplay returning " +
2942 mTelephonySharedPreferences.getString(numberPrefKey, null));
2943 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002944 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002945 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002946 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002947 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002948 }
2949
2950 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002951 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2952 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2953 return null;
2954 }
Derek Tan97ebb422014-09-05 16:55:38 -07002955
2956 String iccId = getIccId(subId);
2957 if (iccId != null) {
2958 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002959 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002960 }
Derek Tan97ebb422014-09-05 16:55:38 -07002961 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002962 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002963
2964 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002965 public String[] getMergedSubscriberIds(String callingPackage) {
2966 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2967 return null;
2968 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002969 final Context context = mPhone.getContext();
2970 final TelephonyManager tele = TelephonyManager.from(context);
2971 final SubscriptionManager sub = SubscriptionManager.from(context);
2972
2973 // Figure out what subscribers are currently active
2974 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002975 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2976 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2977 final long identity = Binder.clearCallingIdentity();
2978 try {
2979 final int[] subIds = sub.getActiveSubscriptionIdList();
2980 for (int subId : subIds) {
2981 activeSubscriberIds.add(tele.getSubscriberId(subId));
2982 }
2983 } finally {
2984 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002985 }
2986
2987 // First pass, find a number override for an active subscriber
2988 String mergeNumber = null;
2989 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2990 for (String key : prefs.keySet()) {
2991 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2992 final String subscriberId = (String) prefs.get(key);
2993 if (activeSubscriberIds.contains(subscriberId)) {
2994 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2995 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2996 mergeNumber = (String) prefs.get(numberKey);
2997 if (DBG_MERGE) {
2998 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2999 + " for active subscriber " + subscriberId);
3000 }
3001 if (!TextUtils.isEmpty(mergeNumber)) {
3002 break;
3003 }
3004 }
3005 }
3006 }
3007
3008 // Shortcut when no active merged subscribers
3009 if (TextUtils.isEmpty(mergeNumber)) {
3010 return null;
3011 }
3012
3013 // Second pass, find all subscribers under that line override
3014 final ArraySet<String> result = new ArraySet<>();
3015 for (String key : prefs.keySet()) {
3016 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3017 final String number = (String) prefs.get(key);
3018 if (mergeNumber.equals(number)) {
3019 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3020 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3021 final String subscriberId = (String) prefs.get(subscriberKey);
3022 if (!TextUtils.isEmpty(subscriberId)) {
3023 result.add(subscriberId);
3024 }
3025 }
3026 }
3027 }
3028
3029 final String[] resultArray = result.toArray(new String[result.size()]);
3030 Arrays.sort(resultArray);
3031 if (DBG_MERGE) {
3032 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3033 }
3034 return resultArray;
3035 }
3036
3037 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003038 public boolean setOperatorBrandOverride(int subId, String brand) {
3039 enforceCarrierPrivilege(subId);
3040 final Phone phone = getPhone(subId);
3041 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003042 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003043
3044 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003045 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003046 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3047 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003048 enforceCarrierPrivilege(subId);
3049 final Phone phone = getPhone(subId);
3050 if (phone == null) {
3051 return false;
3052 }
3053 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003054 cdmaNonRoamingList);
3055 }
3056
3057 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003058 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003059 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3060 enforceModifyPermission();
3061
3062 int returnValue = 0;
3063 try {
3064 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3065 if(result.exception == null) {
3066 if (result.result != null) {
3067 byte[] responseData = (byte[])(result.result);
3068 if(responseData.length > oemResp.length) {
3069 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3070 responseData.length + "bytes. Buffer Size is " +
3071 oemResp.length + "bytes.");
3072 }
3073 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3074 returnValue = responseData.length;
3075 }
3076 } else {
3077 CommandException ex = (CommandException) result.exception;
3078 returnValue = ex.getCommandError().ordinal();
3079 if(returnValue > 0) returnValue *= -1;
3080 }
3081 } catch (RuntimeException e) {
3082 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3083 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3084 if(returnValue > 0) returnValue *= -1;
3085 }
3086
3087 return returnValue;
3088 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003089
3090 @Override
3091 public void setRadioCapability(RadioAccessFamily[] rafs) {
3092 try {
3093 ProxyController.getInstance().setRadioCapability(rafs);
3094 } catch (RuntimeException e) {
3095 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3096 }
3097 }
3098
3099 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003100 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3101 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3102 return RadioAccessFamily.RAF_UNKNOWN;
3103 }
3104
Wink Saville5d475dd2014-10-17 15:00:58 -07003105 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3106 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003107
3108 @Override
3109 public void enableVideoCalling(boolean enable) {
3110 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003111 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003112 }
3113
3114 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003115 public boolean isVideoCallingEnabled(String callingPackage) {
3116 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3117 return false;
3118 }
3119
Andrew Lee77527ac2014-10-21 16:57:39 -07003120 // Check the user preference and the system-level IMS setting. Even if the user has
3121 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3122 // In the long run, we may instead need to check if there exists a connection service
3123 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003124 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3125 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003126 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003127 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003128
Andrew Leea1239f22015-03-02 17:44:07 -08003129 @Override
3130 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003131 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003132 }
3133
3134 @Override
3135 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003136 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003137 }
3138
Andrew Lee9431b832015-03-09 18:46:45 -07003139 @Override
3140 public boolean isTtyModeSupported() {
3141 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3142 TelephonyManager telephonyManager =
3143 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003144 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003145 }
3146
3147 @Override
3148 public boolean isHearingAidCompatibilitySupported() {
3149 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3150 }
3151
Sanket Padawe7310cc72015-01-14 09:53:20 -08003152 /**
3153 * Returns the unique device ID of phone, for example, the IMEI for
3154 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3155 *
3156 * <p>Requires Permission:
3157 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3158 */
3159 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003160 public String getDeviceId(String callingPackage) {
3161 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3162 return null;
3163 }
3164
Sanket Padawe7310cc72015-01-14 09:53:20 -08003165 final Phone phone = PhoneFactory.getPhone(0);
3166 if (phone != null) {
3167 return phone.getDeviceId();
3168 } else {
3169 return null;
3170 }
3171 }
3172
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003173 /*
3174 * {@hide}
3175 * Returns the IMS Registration Status
3176 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003177 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003178 public boolean isImsRegistered() {
3179 return mPhone.isImsRegistered();
3180 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003181
3182 @Override
3183 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3184 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3185 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003186
Nathan Haroldc55097a2015-03-11 18:14:50 -07003187 /*
3188 * {@hide}
3189 * Returns the IMS Registration Status
3190 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003191 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003192 return mPhone.isWifiCallingEnabled();
3193 }
3194
3195 /*
3196 * {@hide}
3197 * Returns the IMS Registration Status
3198 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003199 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003200 return mPhone.isVolteEnabled();
3201 }
Svet Ganovb320e182015-04-16 12:30:10 -07003202
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003203 /*
3204 * {@hide} Returns the IMS Registration Status
3205 */
3206 public boolean isVideoTelephonyAvailable() {
3207 return mPhone.isVideoEnabled();
3208 }
3209
Svet Ganovb320e182015-04-16 12:30:10 -07003210 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003211 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003212 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003213 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3214
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003215 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003216 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003217 } catch (SecurityException e) {
3218 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3219 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003220 }
Svet Ganovb320e182015-04-16 12:30:10 -07003221
3222 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3223 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3224 return false;
3225 }
3226
3227 return true;
3228 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003229
Makoto Onukifee69342015-06-29 14:44:50 -07003230 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003231 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003232 */
3233 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003234 // Default SMS app can always read it.
3235 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3236 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3237 return true;
3238 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003239
Makoto Onukie4072d12015-08-03 15:12:23 -07003240 try {
3241 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003242 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003243 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003244 // Can be read with READ_SMS too.
3245 try {
3246 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3247 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3248 if (opCode != AppOpsManager.OP_NONE) {
3249 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3250 == AppOpsManager.MODE_ALLOWED;
3251 } else {
3252 return true;
3253 }
3254 } catch (SecurityException readSmsSecurityException) {
3255 }
Chad Brubakerf342a982017-03-30 16:27:34 -07003256 // Can be read with READ_PHONE_NUMBERS too.
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003257 try {
Chad Brubakerf342a982017-03-30 16:27:34 -07003258 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBERS,
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003259 message);
Chad Brubakerf342a982017-03-30 16:27:34 -07003260 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBERS);
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003261 if (opCode != AppOpsManager.OP_NONE) {
3262 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3263 == AppOpsManager.MODE_ALLOWED;
3264 } else {
3265 return true;
3266 }
3267 } catch (SecurityException readPhoneNumberSecurityException) {
3268 }
3269
3270 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3271 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3272 ", " + android.Manifest.permission.READ_SMS + ", or " +
Chad Brubakerf342a982017-03-30 16:27:34 -07003273 android.Manifest.permission.READ_PHONE_NUMBERS);
Makoto Onukifee69342015-06-29 14:44:50 -07003274 }
3275
Stuart Scott8eef64f2015-04-08 15:13:54 -07003276 @Override
3277 public void factoryReset(int subId) {
3278 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003279 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3280 return;
3281 }
3282
Svet Ganovcc087f82015-05-12 20:35:54 -07003283 final long identity = Binder.clearCallingIdentity();
3284 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003285 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3286 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003287 // Enable data
3288 setDataEnabled(subId, true);
3289 // Set network selection mode to automatic
3290 setNetworkSelectionModeAutomatic(subId);
3291 // Set preferred mobile network type to the best available
3292 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3293 // Turn off roaming
Malcolm Chenf6b97f42017-04-19 16:03:24 -07003294 mPhone.setDataRoamingEnabled(false);
Svet Ganovcc087f82015-05-12 20:35:54 -07003295 }
3296 } finally {
3297 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003298 }
3299 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003300
3301 @Override
3302 public String getLocaleFromDefaultSim() {
3303 // We query all subscriptions instead of just the active ones, because
3304 // this might be called early on in the provisioning flow when the
3305 // subscriptions potentially aren't active yet.
3306 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3307 if (slist == null || slist.isEmpty()) {
3308 return null;
3309 }
3310
3311 // This function may be called very early, say, from the setup wizard, at
3312 // which point we won't have a default subscription set. If that's the case
3313 // we just choose the first, which will be valid in "most cases".
3314 final int defaultSubId = getDefaultSubscription();
3315 SubscriptionInfo info = null;
3316 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3317 info = slist.get(0);
3318 } else {
3319 for (SubscriptionInfo item : slist) {
3320 if (item.getSubscriptionId() == defaultSubId) {
3321 info = item;
3322 break;
3323 }
3324 }
3325
3326 if (info == null) {
3327 return null;
3328 }
3329 }
3330
3331 // Try and fetch the locale from the carrier properties or from the SIM language
3332 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003333 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003334 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003335 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003336 if (defaultPhone != null) {
3337 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3338 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003339 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003340 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3341 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003342 } else {
3343 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003344 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003345 }
3346 }
3347
Narayan Kamath011676f2015-07-29 12:04:08 +01003348 // The SIM language preferences only store a language (e.g. fr = French), not an
3349 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3350 // the SIM and carrier preferences does not include a country we add the country
3351 // determined from the SIM MCC to provide an exact locale.
3352 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003353 if (mccLocale != null) {
3354 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3355 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003356 }
3357
Tony Hill183b2de2015-06-24 14:53:58 +01003358 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003359 return null;
3360 }
3361
3362 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3363 final long identity = Binder.clearCallingIdentity();
3364 try {
3365 return mSubscriptionController.getAllSubInfoList(
3366 mPhone.getContext().getOpPackageName());
3367 } finally {
3368 Binder.restoreCallingIdentity(identity);
3369 }
3370 }
3371
3372 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3373 final long identity = Binder.clearCallingIdentity();
3374 try {
3375 return mSubscriptionController.getActiveSubscriptionInfoList(
3376 mPhone.getContext().getOpPackageName());
3377 } finally {
3378 Binder.restoreCallingIdentity(identity);
3379 }
3380 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003381
3382 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003383 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3384 * representing the state of the modem.
3385 *
3386 * NOTE: This clears the modem state, so there should only every be one caller.
3387 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003388 */
3389 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003390 public void requestModemActivityInfo(ResultReceiver result) {
3391 enforceModifyPermission();
3392
3393 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3394 Bundle bundle = new Bundle();
3395 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3396 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003397 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003398
3399 /**
3400 * {@hide}
3401 * Returns the service state information on specified subscription.
3402 */
3403 @Override
3404 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3405
3406 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3407 return null;
3408 }
3409
3410 final Phone phone = getPhone(subId);
3411 if (phone == null) {
3412 return null;
3413 }
3414
3415 return phone.getServiceState();
3416 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003417
3418 /**
3419 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3420 *
3421 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3422 * voicemail ringtone.
3423 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3424 * PhoneAccount.
3425 */
3426 @Override
3427 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003428 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003429 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003430 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003431 }
3432
3433 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3434 }
3435
3436 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003437 * Sets the per-account voicemail ringtone.
3438 *
3439 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3440 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3441 *
3442 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3443 * voicemail ringtone.
3444 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3445 * PhoneAccount.
3446 */
3447 @Override
3448 public void setVoicemailRingtoneUri(String callingPackage,
3449 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3450 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3451 if (!TextUtils.equals(callingPackage,
3452 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3453 enforceModifyPermissionOrCarrierPrivilege(
3454 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3455 }
3456 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3457 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003458 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003459 }
3460 VoicemailNotificationSettingsUtil.setRingtoneUri(phone, uri);
3461 }
3462
3463 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003464 * Returns whether vibration is set for voicemail notification in Phone settings.
3465 *
3466 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3467 * voicemail vibration setting.
3468 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3469 */
3470 @Override
3471 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003472 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003473 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003474 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003475 }
3476
3477 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3478 }
3479
Youhan Wange64578a2016-05-02 15:32:42 -07003480 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003481 * Sets the per-account voicemail vibration.
3482 *
3483 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3484 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3485 *
3486 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3487 * voicemail vibration setting.
3488 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3489 * specific PhoneAccount.
3490 */
3491 @Override
3492 public void setVoicemailVibrationEnabled(String callingPackage,
3493 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3494 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3495 if (!TextUtils.equals(callingPackage,
3496 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3497 enforceModifyPermissionOrCarrierPrivilege(
3498 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3499 }
3500
3501 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3502 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003503 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003504 }
3505 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone, enabled);
3506 }
3507
3508 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003509 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3510 *
3511 * @throws SecurityException if the caller does not have the required permission
3512 */
3513 private void enforceReadPrivilegedPermission() {
3514 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3515 null);
3516 }
3517
3518 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003519 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3520 * permission.
3521 *
3522 * @throws SecurityException if the caller does not have the required permission
3523 */
3524 private void enforceSendSmsPermission() {
3525 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3526 }
3527
3528 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003529 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003530 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003531 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003532 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003533 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Ta-wei Yen5d81b0c2017-03-03 16:32:26 -08003534 ComponentName componentName =
3535 RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
3536 if(componentName == null) {
3537 throw new SecurityException("Caller not current active visual voicemail package[null]");
3538 }
3539 String vvmPackage = componentName.getPackageName();
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003540 if (!callingPackage.equals(vvmPackage)) {
3541 throw new SecurityException("Caller not current active visual voicemail package[" +
3542 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003543 }
3544 }
3545
3546 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003547 * Return the application ID for the app type.
3548 *
3549 * @param subId the subscription ID that this request applies to.
3550 * @param appType the uicc app type.
3551 * @return Application ID for specificied app type, or null if no uicc.
3552 */
3553 @Override
3554 public String getAidForAppType(int subId, int appType) {
3555 enforceReadPrivilegedPermission();
3556 Phone phone = getPhone(subId);
3557 if (phone == null) {
3558 return null;
3559 }
3560 String aid = null;
3561 try {
3562 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3563 .getApplicationByType(appType).getAid();
3564 } catch (Exception e) {
3565 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3566 }
3567 return aid;
3568 }
3569
Youhan Wang4001d252016-05-11 10:29:41 -07003570 /**
3571 * Return the Electronic Serial Number.
3572 *
3573 * @param subId the subscription ID that this request applies to.
3574 * @return ESN or null if error.
3575 */
3576 @Override
3577 public String getEsn(int subId) {
3578 enforceReadPrivilegedPermission();
3579 Phone phone = getPhone(subId);
3580 if (phone == null) {
3581 return null;
3582 }
3583 String esn = null;
3584 try {
3585 esn = phone.getEsn();
3586 } catch (Exception e) {
3587 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3588 }
3589 return esn;
3590 }
3591
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003592 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003593 * Return the Preferred Roaming List Version.
3594 *
3595 * @param subId the subscription ID that this request applies to.
3596 * @return PRLVersion or null if error.
3597 */
3598 @Override
3599 public String getCdmaPrlVersion(int subId) {
3600 enforceReadPrivilegedPermission();
3601 Phone phone = getPhone(subId);
3602 if (phone == null) {
3603 return null;
3604 }
3605 String cdmaPrlVersion = null;
3606 try {
3607 cdmaPrlVersion = phone.getCdmaPrlVersion();
3608 } catch (Exception e) {
3609 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3610 }
3611 return cdmaPrlVersion;
3612 }
3613
3614 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003615 * Get snapshot of Telephony histograms
3616 * @return List of Telephony histograms
3617 * @hide
3618 */
3619 @Override
3620 public List<TelephonyHistogram> getTelephonyHistograms() {
3621 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3622 return RIL.getTelephonyRILTimingHistograms();
3623 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003624
3625 /**
3626 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003627 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003628 * Require system privileges. In the future we may add this to carrier APIs.
3629 *
3630 * @return The number of carriers set successfully, should match length of carriers
3631 */
3632 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003633 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003634 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003635
Meng Wang9b7c4e92017-02-17 11:41:27 -08003636 if (carriers == null) {
3637 throw new NullPointerException("carriers cannot be null");
3638 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003639
3640 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003641 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3642 return retVal[0];
3643 }
3644
3645 /**
3646 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003647 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003648 * Require system privileges. In the future we may add this to carrier APIs.
3649 *
3650 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3651 * means all carriers are allowed.
3652 */
3653 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003654 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003655 enforceReadPrivilegedPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003656 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003657 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3658 }
3659
fionaxu59545b42016-05-25 15:53:37 -07003660 /**
3661 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3662 * @param subId the subscription ID that this action applies to.
3663 * @param enabled control enable or disable metered apns.
3664 * {@hide}
3665 */
3666 @Override
3667 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3668 enforceModifyPermission();
3669 final Phone phone = getPhone(subId);
3670 if (phone == null) {
3671 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3672 return;
3673 }
3674 try {
3675 phone.carrierActionSetMeteredApnsEnabled(enabled);
3676 } catch (Exception e) {
3677 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3678 }
3679 }
3680
3681 /**
3682 * Action set from carrier signalling broadcast receivers to enable/disable radio
3683 * @param subId the subscription ID that this action applies to.
3684 * @param enabled control enable or disable radio.
3685 * {@hide}
3686 */
3687 @Override
3688 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3689 enforceModifyPermission();
3690 final Phone phone = getPhone(subId);
3691 if (phone == null) {
3692 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3693 return;
3694 }
3695 try {
3696 phone.carrierActionSetRadioEnabled(enabled);
3697 } catch (Exception e) {
3698 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3699 }
3700 }
3701
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003702 /**
3703 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3704 * bug report is being generated.
3705 */
3706 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003707 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003708 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3709 != PackageManager.PERMISSION_GRANTED) {
3710 writer.println("Permission Denial: can't dump Phone from pid="
3711 + Binder.getCallingPid()
3712 + ", uid=" + Binder.getCallingUid()
3713 + "without permission "
3714 + android.Manifest.permission.DUMP);
3715 return;
3716 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003717 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003718 }
Jack Yueb89b242016-06-22 13:27:47 -07003719
3720 /**
3721 * Get aggregated video call data usage from all subscriptions since boot.
3722 * @return total data usage in bytes
3723 * {@hide}
3724 */
3725 @Override
3726 public long getVtDataUsage() {
3727 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3728 null);
3729
3730 // NetworkStatsService keeps tracking the active network interface and identity. It will
3731 // record the delta with the corresponding network identity. What we need to do here is
3732 // returning total video call data usage from all subscriptions since boot.
3733
3734 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3735 // simultaneous VT calls.
3736 final Phone[] phones = PhoneFactory.getPhones();
3737 long total = 0;
3738 for (Phone phone : phones) {
3739 total += phone.getVtDataUsage();
3740 }
3741 return total;
3742 }
Jack Yu75ab2952016-07-08 14:29:33 -07003743
3744 /**
3745 * Policy control of data connection. Usually used when data limit is passed.
3746 * @param enabled True if enabling the data, otherwise disabling.
3747 * @param subId Subscription index
3748 * {@hide}
3749 */
3750 @Override
3751 public void setPolicyDataEnabled(boolean enabled, int subId) {
3752 enforceModifyPermission();
3753 Phone phone = getPhone(subId);
3754 if (phone != null) {
3755 phone.setPolicyDataEnabled(enabled);
3756 }
3757 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003758
3759 /**
3760 * Get Client request stats
3761 * @return List of Client Request Stats
3762 * @hide
3763 */
3764 @Override
3765 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3766 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3767 return null;
3768 }
3769
3770 Phone phone = getPhone(subId);
3771 if (phone != null) {
3772 return phone.getClientRequestStats();
3773 }
3774
3775 return null;
3776 }
3777
3778 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3779 if (workSource != null) {
3780 return workSource;
3781 }
3782
3783 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3784 workSource = new WorkSource(uid, packageName);
3785 return workSource;
3786 }
Jack Yueb4124c2017-02-16 15:32:43 -08003787
3788 /**
3789 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3790 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003791 * @param slotIndex SIM slot id.
Jack Yueb4124c2017-02-16 15:32:43 -08003792 * @param powerUp True if powering up the SIM, otherwise powering down
3793 *
3794 **/
3795 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003796 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yueb4124c2017-02-16 15:32:43 -08003797 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003798 Phone phone = PhoneFactory.getPhone(slotIndex);
3799
Jack Yueb4124c2017-02-16 15:32:43 -08003800 if (phone != null) {
3801 phone.setSimPowerState(powerUp);
3802 }
3803 }
Shuo Qiandd210312017-04-12 22:11:33 +00003804
3805 /**
3806 * Check if phone is in emergency callback mode
3807 * @return true if phone is in emergency callback mode
3808 * @param subId sub id
3809 */
3810 public boolean getEmergencyCallbackMode(int subId) {
3811 final Phone phone = getPhone(subId);
3812 if (phone != null) {
3813 return phone.isInEcm();
3814 } else {
3815 return false;
3816 }
3817 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003818}