blob: cf80a11d9a451c236979b3c438394ef659e15649 [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;
Tyler Gunn65d45c22017-06-05 11:22:26 -070039import android.os.PersistableBundle;
Adam Lesinski903a54c2016-04-11 14:49:52 -070040import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.ServiceManager;
42import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070043import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070044import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070045import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080046import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070047import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080048import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080049import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070050import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070051import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.CellInfo;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070053import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070054import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070055import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080056import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070057import android.telephony.RadioAccessFamily;
Tyler Gunn65d45c22017-06-05 11:22:26 -070058import android.telephony.Rlog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.telephony.ServiceState;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080060import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080061import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080062import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070063import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070064import android.telephony.TelephonyManager;
Tyler Gunn65d45c22017-06-05 11:22:26 -070065import android.telephony.UssdResponse;
Ta-wei Yenb6929602016-05-24 15:48:27 -070066import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080068import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080070import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080071import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080072
Andrew Lee312e8172014-10-23 17:01:36 -070073import com.android.ims.ImsManager;
Brad Ebinger51f743a2017-01-23 13:50:20 -080074import com.android.ims.internal.IImsServiceController;
75import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070076import com.android.internal.telephony.CallManager;
Tyler Gunn10d19bf2017-04-26 11:30:31 -070077import com.android.internal.telephony.CallStateException;
Shishir Agrawal302c8692015-06-19 13:49:39 -070078import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070079import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080082import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010083import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070084import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085import com.android.internal.telephony.Phone;
Nathan Harolda667c152016-12-14 11:27:20 -080086import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070087import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070088import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070089import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070090import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070091import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080092import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070093import com.android.internal.telephony.uicc.IccIoResult;
94import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080095import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070096import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080097import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070098import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080099import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -0800100import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700101import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800102import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700103import com.android.phone.vvm.VisualVoicemailPreferences;
104import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700105import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800106
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700107import java.io.FileDescriptor;
108import java.io.PrintWriter;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800109import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700110import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800111import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800112import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100113import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800114import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700115
116/**
117 * Implementation of the ITelephony interface.
118 */
Santos Cordon117fee72014-05-16 17:56:12 -0700119public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700120 private static final String LOG_TAG = "PhoneInterfaceManager";
121 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
122 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800123 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700124
125 // Message codes used with mMainThreadHandler
126 private static final int CMD_HANDLE_PIN_MMI = 1;
127 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
128 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
129 private static final int CMD_ANSWER_RINGING_CALL = 4;
130 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700131 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
132 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700133 private static final int CMD_OPEN_CHANNEL = 9;
134 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
135 private static final int CMD_CLOSE_CHANNEL = 11;
136 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800137 private static final int CMD_NV_READ_ITEM = 13;
138 private static final int EVENT_NV_READ_ITEM_DONE = 14;
139 private static final int CMD_NV_WRITE_ITEM = 15;
140 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
141 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
142 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
143 private static final int CMD_NV_RESET_CONFIG = 19;
144 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800145 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
146 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
147 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
148 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800149 private static final int CMD_SEND_ENVELOPE = 25;
150 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700151 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
152 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
153 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
154 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
155 private static final int CMD_EXCHANGE_SIM_IO = 31;
156 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800157 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
158 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700159 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
160 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700161 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
162 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700163 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
164 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
165 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
166 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700167 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
168 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
169 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
170 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700171 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800172 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
173 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700174
175 /** The singleton instance. */
176 private static PhoneInterfaceManager sInstance;
177
Wink Saville3ab207e2014-11-20 13:07:20 -0800178 private PhoneGlobals mApp;
179 private Phone mPhone;
180 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700181 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800182 private AppOpsManager mAppOps;
183 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800184 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800185 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700186
Derek Tan97ebb422014-09-05 16:55:38 -0700187 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
188 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800189 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700190
191 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700192 * A request object to use for transmitting data to an ICC.
193 */
194 private static final class IccAPDUArgument {
195 public int channel, cla, command, p1, p2, p3;
196 public String data;
197
198 public IccAPDUArgument(int channel, int cla, int command,
199 int p1, int p2, int p3, String data) {
200 this.channel = channel;
201 this.cla = cla;
202 this.command = command;
203 this.p1 = p1;
204 this.p2 = p2;
205 this.p3 = p3;
206 this.data = data;
207 }
208 }
209
210 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700211 * A request object to use for transmitting data to an ICC.
212 */
213 private static final class ManualNetworkSelectionArgument {
214 public OperatorInfo operatorInfo;
215 public boolean persistSelection;
216
217 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
218 this.operatorInfo = operatorInfo;
219 this.persistSelection = persistSelection;
220 }
221 }
222
223 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
225 * request after sending. The main thread will notify the request when it is complete.
226 */
227 private static final class MainThreadRequest {
228 /** The argument to use for the request */
229 public Object argument;
230 /** The result of the request that is run on the main thread */
231 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800232 // The subscriber id that this request applies to. Defaults to
233 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
234 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700235
236 public MainThreadRequest(Object argument) {
237 this.argument = argument;
238 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800239
240 public MainThreadRequest(Object argument, Integer subId) {
241 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800242 if (subId != null) {
243 this.subId = subId;
244 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800245 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 }
247
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800248 private static final class IncomingThirdPartyCallArgs {
249 public final ComponentName component;
250 public final String callId;
251 public final String callerDisplayName;
252
253 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
254 String callerDisplayName) {
255 this.component = component;
256 this.callId = callId;
257 this.callerDisplayName = callerDisplayName;
258 }
259 }
260
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261 /**
262 * A handler that processes messages on the main thread in the phone process. Since many
263 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
264 * inbound binder threads to the main thread in the phone process. The Binder thread
265 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
266 * on, which will be notified when the operation completes and will contain the result of the
267 * request.
268 *
269 * <p>If a MainThreadRequest object is provided in the msg.obj field,
270 * note that request.result must be set to something non-null for the calling thread to
271 * unblock.
272 */
273 private final class MainThreadHandler extends Handler {
274 @Override
275 public void handleMessage(Message msg) {
276 MainThreadRequest request;
277 Message onCompleted;
278 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800279 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700280 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700281
282 switch (msg.what) {
pkanwar32d516d2016-10-14 19:37:38 -0700283 case CMD_HANDLE_USSD_REQUEST: {
284 request = (MainThreadRequest) msg.obj;
285 final Phone phone = getPhoneFromRequest(request);
286 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
287 String ussdRequest = ussdObject.first;
288 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn65d45c22017-06-05 11:22:26 -0700289
290 if (!isUssdApiAllowed(request.subId)) {
291 // Carrier does not support use of this API, return failure.
292 Rlog.w(LOG_TAG, "handleUssdRequest: carrier does not support USSD apis.");
293 UssdResponse response = new UssdResponse(ussdRequest, null);
294 Bundle returnData = new Bundle();
295 returnData.putParcelable(TelephonyManager.USSD_RESPONSE, response);
296 wrappedCallback.send(TelephonyManager.USSD_RETURN_FAILURE, returnData);
297
298 request.result = true;
299 synchronized (request) {
300 request.notifyAll();
301 }
302 return;
303 }
304
Tyler Gunn10d19bf2017-04-26 11:30:31 -0700305 try {
306 request.result = phone != null ?
307 phone.handleUssdRequest(ussdRequest, wrappedCallback)
308 : false;
309 } catch (CallStateException cse) {
310 request.result = false;
311 }
pkanwar32d516d2016-10-14 19:37:38 -0700312 // Wake up the requesting thread
313 synchronized (request) {
314 request.notifyAll();
315 }
316 break;
317 }
318
Yorke Lee716f67e2015-06-17 15:39:16 -0700319 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700321 final Phone phone = getPhoneFromRequest(request);
322 request.result = phone != null ?
323 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
324 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 // Wake up the requesting thread
326 synchronized (request) {
327 request.notifyAll();
328 }
329 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700330 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331
332 case CMD_HANDLE_NEIGHBORING_CELL:
333 request = (MainThreadRequest) msg.obj;
334 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
335 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700336 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700337 break;
338
339 case EVENT_NEIGHBORING_CELL_DONE:
340 ar = (AsyncResult) msg.obj;
341 request = (MainThreadRequest) ar.userObj;
342 if (ar.exception == null && ar.result != null) {
343 request.result = ar.result;
344 } else {
345 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800346 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347 }
348 // Wake up the requesting thread
349 synchronized (request) {
350 request.notifyAll();
351 }
352 break;
353
354 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700355 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800356 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700357 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700358 break;
359
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700360 case CMD_END_CALL:
361 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800362 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700363 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700364 Phone phone = getPhone(end_subId);
365 if (phone == null) {
366 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
367 break;
368 }
369 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700370 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
371 // CDMA: If the user presses the Power button we treat it as
372 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700373 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700374 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
375 // GSM: End the call as per the Phone state
376 hungUp = PhoneUtils.hangup(mCM);
377 } else {
378 throw new IllegalStateException("Unexpected phone type: " + phoneType);
379 }
380 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
381 request.result = hungUp;
382 // Wake up the requesting thread
383 synchronized (request) {
384 request.notifyAll();
385 }
386 break;
387
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700388 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700389 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700390 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800391 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700392 if (uiccCard == null) {
393 loge("iccTransmitApduLogicalChannel: No UICC");
394 request.result = new IccIoResult(0x6F, 0, (byte[])null);
395 synchronized (request) {
396 request.notifyAll();
397 }
398 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700399 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
400 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700401 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700402 iccArgument.channel, iccArgument.cla, iccArgument.command,
403 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700404 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700405 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700406 break;
407
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700408 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700409 ar = (AsyncResult) msg.obj;
410 request = (MainThreadRequest) ar.userObj;
411 if (ar.exception == null && ar.result != null) {
412 request.result = ar.result;
413 } else {
414 request.result = new IccIoResult(0x6F, 0, (byte[])null);
415 if (ar.result == null) {
416 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800417 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700418 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800419 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700420 } else {
421 loge("iccTransmitApduLogicalChannel: Unknown exception");
422 }
423 }
424 synchronized (request) {
425 request.notifyAll();
426 }
427 break;
428
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700429 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
430 request = (MainThreadRequest) msg.obj;
431 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800432 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700433 if (uiccCard == null) {
434 loge("iccTransmitApduBasicChannel: No UICC");
435 request.result = new IccIoResult(0x6F, 0, (byte[])null);
436 synchronized (request) {
437 request.notifyAll();
438 }
439 } else {
440 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
441 request);
442 uiccCard.iccTransmitApduBasicChannel(
443 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
444 iccArgument.p3, iccArgument.data, onCompleted);
445 }
446 break;
447
448 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
449 ar = (AsyncResult) msg.obj;
450 request = (MainThreadRequest) ar.userObj;
451 if (ar.exception == null && ar.result != null) {
452 request.result = ar.result;
453 } else {
454 request.result = new IccIoResult(0x6F, 0, (byte[])null);
455 if (ar.result == null) {
456 loge("iccTransmitApduBasicChannel: Empty response");
457 } else if (ar.exception instanceof CommandException) {
458 loge("iccTransmitApduBasicChannel: CommandException: " +
459 ar.exception);
460 } else {
461 loge("iccTransmitApduBasicChannel: Unknown exception");
462 }
463 }
464 synchronized (request) {
465 request.notifyAll();
466 }
467 break;
468
469 case CMD_EXCHANGE_SIM_IO:
470 request = (MainThreadRequest) msg.obj;
471 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800472 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700473 if (uiccCard == null) {
474 loge("iccExchangeSimIO: No UICC");
475 request.result = new IccIoResult(0x6F, 0, (byte[])null);
476 synchronized (request) {
477 request.notifyAll();
478 }
479 } else {
480 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
481 request);
482 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
483 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
484 iccArgument.data, onCompleted);
485 }
486 break;
487
488 case EVENT_EXCHANGE_SIM_IO_DONE:
489 ar = (AsyncResult) msg.obj;
490 request = (MainThreadRequest) ar.userObj;
491 if (ar.exception == null && ar.result != null) {
492 request.result = ar.result;
493 } else {
494 request.result = new IccIoResult(0x6f, 0, (byte[])null);
495 }
496 synchronized (request) {
497 request.notifyAll();
498 }
499 break;
500
Derek Tan4d5e5c12014-02-04 11:54:58 -0800501 case CMD_SEND_ENVELOPE:
502 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800503 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700504 if (uiccCard == null) {
505 loge("sendEnvelopeWithStatus: No UICC");
506 request.result = new IccIoResult(0x6F, 0, (byte[])null);
507 synchronized (request) {
508 request.notifyAll();
509 }
510 } else {
511 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
512 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
513 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800514 break;
515
516 case EVENT_SEND_ENVELOPE_DONE:
517 ar = (AsyncResult) msg.obj;
518 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700519 if (ar.exception == null && ar.result != null) {
520 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800521 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700522 request.result = new IccIoResult(0x6F, 0, (byte[])null);
523 if (ar.result == null) {
524 loge("sendEnvelopeWithStatus: Empty response");
525 } else if (ar.exception instanceof CommandException) {
526 loge("sendEnvelopeWithStatus: CommandException: " +
527 ar.exception);
528 } else {
529 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
530 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800531 }
532 synchronized (request) {
533 request.notifyAll();
534 }
535 break;
536
Shishir Agrawal566b7612013-10-28 14:41:00 -0700537 case CMD_OPEN_CHANNEL:
538 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800539 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800540 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700541 if (uiccCard == null) {
542 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800543 request.result = new IccOpenLogicalChannelResponse(-1,
544 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700545 synchronized (request) {
546 request.notifyAll();
547 }
548 } else {
549 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800550 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
551 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700552 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700553 break;
554
555 case EVENT_OPEN_CHANNEL_DONE:
556 ar = (AsyncResult) msg.obj;
557 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700558 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700559 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700560 int[] result = (int[]) ar.result;
561 int channelId = result[0];
562 byte[] selectResponse = null;
563 if (result.length > 1) {
564 selectResponse = new byte[result.length - 1];
565 for (int i = 1; i < result.length; ++i) {
566 selectResponse[i - 1] = (byte) result[i];
567 }
568 }
569 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700570 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700571 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700572 if (ar.result == null) {
573 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700574 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700575 if (ar.exception != null) {
576 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
577 }
578
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700579 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700580 if (ar.exception instanceof CommandException) {
581 CommandException.Error error =
582 ((CommandException) (ar.exception)).getCommandError();
583 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700584 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700585 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700586 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700587 }
588 }
589 openChannelResp = new IccOpenLogicalChannelResponse(
590 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700591 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700592 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700593 synchronized (request) {
594 request.notifyAll();
595 }
596 break;
597
598 case CMD_CLOSE_CHANNEL:
599 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800600 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700601 if (uiccCard == null) {
602 loge("iccCloseLogicalChannel: No UICC");
603 request.result = new IccIoResult(0x6F, 0, (byte[])null);
604 synchronized (request) {
605 request.notifyAll();
606 }
607 } else {
608 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
609 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
610 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700611 break;
612
613 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800614 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
615 break;
616
617 case CMD_NV_READ_ITEM:
618 request = (MainThreadRequest) msg.obj;
619 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
620 mPhone.nvReadItem((Integer) request.argument, onCompleted);
621 break;
622
623 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700624 ar = (AsyncResult) msg.obj;
625 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800626 if (ar.exception == null && ar.result != null) {
627 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700628 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800629 request.result = "";
630 if (ar.result == null) {
631 loge("nvReadItem: Empty response");
632 } else if (ar.exception instanceof CommandException) {
633 loge("nvReadItem: CommandException: " +
634 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700635 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800636 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700637 }
638 }
639 synchronized (request) {
640 request.notifyAll();
641 }
642 break;
643
Jake Hambye994d462014-02-03 13:10:13 -0800644 case CMD_NV_WRITE_ITEM:
645 request = (MainThreadRequest) msg.obj;
646 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
647 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
648 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
649 break;
650
651 case EVENT_NV_WRITE_ITEM_DONE:
652 handleNullReturnEvent(msg, "nvWriteItem");
653 break;
654
655 case CMD_NV_WRITE_CDMA_PRL:
656 request = (MainThreadRequest) msg.obj;
657 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
658 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
659 break;
660
661 case EVENT_NV_WRITE_CDMA_PRL_DONE:
662 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
663 break;
664
665 case CMD_NV_RESET_CONFIG:
666 request = (MainThreadRequest) msg.obj;
667 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
668 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
669 break;
670
671 case EVENT_NV_RESET_CONFIG_DONE:
672 handleNullReturnEvent(msg, "nvResetConfig");
673 break;
674
Jake Hamby7c27be32014-03-03 13:25:59 -0800675 case CMD_GET_PREFERRED_NETWORK_TYPE:
676 request = (MainThreadRequest) msg.obj;
677 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700678 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800679 break;
680
681 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
682 ar = (AsyncResult) msg.obj;
683 request = (MainThreadRequest) ar.userObj;
684 if (ar.exception == null && ar.result != null) {
685 request.result = ar.result; // Integer
686 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800687 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800688 if (ar.result == null) {
689 loge("getPreferredNetworkType: Empty response");
690 } else if (ar.exception instanceof CommandException) {
691 loge("getPreferredNetworkType: CommandException: " +
692 ar.exception);
693 } else {
694 loge("getPreferredNetworkType: Unknown exception");
695 }
696 }
697 synchronized (request) {
698 request.notifyAll();
699 }
700 break;
701
702 case CMD_SET_PREFERRED_NETWORK_TYPE:
703 request = (MainThreadRequest) msg.obj;
704 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
705 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700706 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800707 break;
708
709 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
710 handleNullReturnEvent(msg, "setPreferredNetworkType");
711 break;
712
Steven Liu4bf01bc2014-07-17 11:05:29 -0500713 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
714 request = (MainThreadRequest)msg.obj;
715 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
716 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
717 break;
718
719 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
720 ar = (AsyncResult)msg.obj;
721 request = (MainThreadRequest)ar.userObj;
722 request.result = ar;
723 synchronized (request) {
724 request.notifyAll();
725 }
726 break;
727
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800728 case CMD_SET_VOICEMAIL_NUMBER:
729 request = (MainThreadRequest) msg.obj;
730 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
731 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800732 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
733 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800734 break;
735
736 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
737 handleNullReturnEvent(msg, "setVoicemailNumber");
738 break;
739
Stuart Scott54788802015-03-30 13:18:01 -0700740 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
741 request = (MainThreadRequest) msg.obj;
742 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
743 request);
744 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
745 break;
746
747 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
748 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
749 break;
750
Shishir Agrawal302c8692015-06-19 13:49:39 -0700751 case CMD_PERFORM_NETWORK_SCAN:
752 request = (MainThreadRequest) msg.obj;
753 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
754 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
755 break;
756
757 case EVENT_PERFORM_NETWORK_SCAN_DONE:
758 ar = (AsyncResult) msg.obj;
759 request = (MainThreadRequest) ar.userObj;
760 CellNetworkScanResult cellScanResult;
761 if (ar.exception == null && ar.result != null) {
762 cellScanResult = new CellNetworkScanResult(
763 CellNetworkScanResult.STATUS_SUCCESS,
764 (List<OperatorInfo>) ar.result);
765 } else {
766 if (ar.result == null) {
767 loge("getCellNetworkScanResults: Empty response");
768 }
769 if (ar.exception != null) {
770 loge("getCellNetworkScanResults: Exception: " + ar.exception);
771 }
772 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
773 if (ar.exception instanceof CommandException) {
774 CommandException.Error error =
775 ((CommandException) (ar.exception)).getCommandError();
776 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
777 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
778 } else if (error == CommandException.Error.GENERIC_FAILURE) {
779 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
780 }
781 }
782 cellScanResult = new CellNetworkScanResult(errorCode, null);
783 }
784 request.result = cellScanResult;
785 synchronized (request) {
786 request.notifyAll();
787 }
788 break;
789
790 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
791 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700792 ManualNetworkSelectionArgument selArg =
793 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700794 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
795 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700796 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
797 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700798 break;
799
800 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
801 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
802 break;
803
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700804 case CMD_GET_MODEM_ACTIVITY_INFO:
805 request = (MainThreadRequest) msg.obj;
806 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700807 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700808 break;
809
810 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
811 ar = (AsyncResult) msg.obj;
812 request = (MainThreadRequest) ar.userObj;
813 if (ar.exception == null && ar.result != null) {
814 request.result = ar.result;
815 } else {
816 if (ar.result == null) {
817 loge("queryModemActivityInfo: Empty response");
818 } else if (ar.exception instanceof CommandException) {
819 loge("queryModemActivityInfo: CommandException: " +
820 ar.exception);
821 } else {
822 loge("queryModemActivityInfo: Unknown exception");
823 }
824 }
Amit Mahajand4766222016-01-28 15:28:28 -0800825 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
826 if (request.result == null) {
827 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
828 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700829 synchronized (request) {
830 request.notifyAll();
831 }
832 break;
833
Meng Wang1a7c35a2016-05-05 20:56:15 -0700834 case CMD_SET_ALLOWED_CARRIERS:
835 request = (MainThreadRequest) msg.obj;
836 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
837 mPhone.setAllowedCarriers(
838 (List<CarrierIdentifier>) request.argument,
839 onCompleted);
840 break;
841
842 case EVENT_SET_ALLOWED_CARRIERS_DONE:
843 ar = (AsyncResult) msg.obj;
844 request = (MainThreadRequest) ar.userObj;
845 if (ar.exception == null && ar.result != null) {
846 request.result = ar.result;
847 } else {
848 if (ar.result == null) {
849 loge("setAllowedCarriers: Empty response");
850 } else if (ar.exception instanceof CommandException) {
851 loge("setAllowedCarriers: CommandException: " +
852 ar.exception);
853 } else {
854 loge("setAllowedCarriers: Unknown exception");
855 }
856 }
857 // Result cannot be null. Return -1 on error.
858 if (request.result == null) {
859 request.result = new int[]{-1};
860 }
861 synchronized (request) {
862 request.notifyAll();
863 }
864 break;
865
866 case CMD_GET_ALLOWED_CARRIERS:
867 request = (MainThreadRequest) msg.obj;
868 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
869 mPhone.getAllowedCarriers(onCompleted);
870 break;
871
872 case EVENT_GET_ALLOWED_CARRIERS_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 if (ar.result == null) {
879 loge("getAllowedCarriers: Empty response");
880 } else if (ar.exception instanceof CommandException) {
881 loge("getAllowedCarriers: CommandException: " +
882 ar.exception);
883 } else {
884 loge("getAllowedCarriers: Unknown exception");
885 }
886 }
887 // Result cannot be null. Return empty list of CarrierIdentifier.
888 if (request.result == null) {
889 request.result = new ArrayList<CarrierIdentifier>(0);
890 }
891 synchronized (request) {
892 request.notifyAll();
893 }
894 break;
895
Nathan Haroldb3014052017-01-25 15:57:32 -0800896 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
897 ar = (AsyncResult) msg.obj;
898 request = (MainThreadRequest) ar.userObj;
899 if (ar.exception == null && ar.result != null) {
900 request.result = ar.result;
901 } else {
902 request.result = new IllegalArgumentException(
903 "Failed to retrieve Forbidden Plmns");
904 if (ar.result == null) {
905 loge("getForbiddenPlmns: Empty response");
906 } else {
907 loge("getForbiddenPlmns: Unknown exception");
908 }
909 }
910 synchronized (request) {
911 request.notifyAll();
912 }
913 break;
914
915 case CMD_GET_FORBIDDEN_PLMNS:
916 request = (MainThreadRequest) msg.obj;
917 uiccCard = getUiccCardFromRequest(request);
918 if (uiccCard == null) {
919 loge("getForbiddenPlmns() UiccCard is null");
920 request.result = new IllegalArgumentException(
921 "getForbiddenPlmns() UiccCard is null");
922 synchronized (request) {
923 request.notifyAll();
924 }
925 break;
926 }
927 Integer appType = (Integer) request.argument;
928 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
929 if (uiccApp == null) {
930 loge("getForbiddenPlmns() no app with specified type -- "
931 + appType);
932 request.result = new IllegalArgumentException("Failed to get UICC App");
933 synchronized (request) {
934 request.notifyAll();
935 }
936 break;
937 } else {
938 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
939 + " specified type -- " + appType);
940 }
941 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
942 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
943 onCompleted);
944 break;
945
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 default:
947 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
948 break;
949 }
950 }
Jake Hambye994d462014-02-03 13:10:13 -0800951
952 private void handleNullReturnEvent(Message msg, String command) {
953 AsyncResult ar = (AsyncResult) msg.obj;
954 MainThreadRequest request = (MainThreadRequest) ar.userObj;
955 if (ar.exception == null) {
956 request.result = true;
957 } else {
958 request.result = false;
959 if (ar.exception instanceof CommandException) {
960 loge(command + ": CommandException: " + ar.exception);
961 } else {
962 loge(command + ": Unknown exception");
963 }
964 }
965 synchronized (request) {
966 request.notifyAll();
967 }
968 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 }
970
971 /**
972 * Posts the specified command to be executed on the main thread,
973 * waits for the request to complete, and returns the result.
974 * @see #sendRequestAsync
975 */
976 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800977 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700978 }
979
980 /**
981 * Posts the specified command to be executed on the main thread,
982 * waits for the request to complete, and returns the result.
983 * @see #sendRequestAsync
984 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800985 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700986 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
987 throw new RuntimeException("This method will deadlock if called from the main thread.");
988 }
989
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800990 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 Message msg = mMainThreadHandler.obtainMessage(command, request);
992 msg.sendToTarget();
993
994 // Wait for the request to complete
995 synchronized (request) {
996 while (request.result == null) {
997 try {
998 request.wait();
999 } catch (InterruptedException e) {
1000 // Do nothing, go back and wait until the request is complete
1001 }
1002 }
1003 }
1004 return request.result;
1005 }
1006
1007 /**
1008 * Asynchronous ("fire and forget") version of sendRequest():
1009 * Posts the specified command to be executed on the main thread, and
1010 * returns immediately.
1011 * @see #sendRequest
1012 */
1013 private void sendRequestAsync(int command) {
1014 mMainThreadHandler.sendEmptyMessage(command);
1015 }
1016
1017 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001018 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
1019 * @see {@link #sendRequest(int,Object)}
1020 */
1021 private void sendRequestAsync(int command, Object argument) {
1022 MainThreadRequest request = new MainThreadRequest(argument);
1023 Message msg = mMainThreadHandler.obtainMessage(command, request);
1024 msg.sendToTarget();
1025 }
1026
1027 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001028 * Initialize the singleton PhoneInterfaceManager instance.
1029 * This is only done once, at startup, from PhoneApp.onCreate().
1030 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001031 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 synchronized (PhoneInterfaceManager.class) {
1033 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001034 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 } else {
1036 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1037 }
1038 return sInstance;
1039 }
1040 }
1041
1042 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001043 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001044 mApp = app;
1045 mPhone = phone;
1046 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001047 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001048 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1049 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001050 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001051 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001052 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001053
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 publish();
1055 }
1056
1057 private void publish() {
1058 if (DBG) log("publish: " + this);
1059
1060 ServiceManager.addService("phone", this);
1061 }
1062
Stuart Scott584921c2015-01-15 17:10:34 -08001063 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001064 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1065 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001066 }
1067
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001068 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1069 Phone phone = getPhoneFromRequest(request);
1070 return phone == null ? null :
1071 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1072 }
1073
Wink Saville36469e72014-06-11 15:17:00 -07001074 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001075 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001076 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001077 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001078 //
1079 // Implementation of the ITelephony interface.
1080 //
1081
1082 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001083 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001084 }
1085
Wink Savilleb564aae2014-10-23 10:18:09 -07001086 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001087 if (DBG) log("dial: " + number);
1088 // No permission check needed here: This is just a wrapper around the
1089 // ACTION_DIAL intent, which is available to any app since it puts up
1090 // the UI before it does anything.
1091
1092 String url = createTelUrl(number);
1093 if (url == null) {
1094 return;
1095 }
1096
1097 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001098 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1100 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1101 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1102 mApp.startActivity(intent);
1103 }
1104 }
1105
1106 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001107 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001108 }
1109
Wink Savilleb564aae2014-10-23 10:18:09 -07001110 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 if (DBG) log("call: " + number);
1112
1113 // This is just a wrapper around the ACTION_CALL intent, but we still
1114 // need to do a permission check since we're calling startActivity()
1115 // from the context of the phone app.
1116 enforceCallPermission();
1117
1118 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1119 != AppOpsManager.MODE_ALLOWED) {
1120 return;
1121 }
1122
1123 String url = createTelUrl(number);
1124 if (url == null) {
1125 return;
1126 }
1127
Wink Saville08874612014-08-31 19:19:58 -07001128 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001129 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001130 if (slist != null) {
1131 for (SubscriptionInfo subInfoRecord : slist) {
1132 if (subInfoRecord.getSubscriptionId() == subId) {
1133 isValid = true;
1134 break;
1135 }
Wink Saville08874612014-08-31 19:19:58 -07001136 }
1137 }
1138 if (isValid == false) {
1139 return;
1140 }
1141
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001142 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001143 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001144 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1145 mApp.startActivity(intent);
1146 }
1147
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 /**
1149 * End a call based on call state
1150 * @return true is a call was ended
1151 */
1152 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001153 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001154 }
1155
1156 /**
1157 * End a call based on the call state of the subId
1158 * @return true is a call was ended
1159 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001160 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001161 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001162 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001163 }
1164
1165 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001166 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001167 }
1168
Wink Savilleb564aae2014-10-23 10:18:09 -07001169 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 if (DBG) log("answerRingingCall...");
1171 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1172 // but that can probably wait till the big TelephonyManager API overhaul.
1173 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1174 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001175 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001176 }
1177
1178 /**
1179 * Make the actual telephony calls to implement answerRingingCall().
1180 * This should only be called from the main thread of the Phone app.
1181 * @see #answerRingingCall
1182 *
1183 * TODO: it would be nice to return true if we answered the call, or
1184 * false if there wasn't actually a ringing incoming call, or some
1185 * other error occurred. (In other words, pass back the return value
1186 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1187 * But that would require calling this method via sendRequest() rather
1188 * than sendRequestAsync(), and right now we don't actually *need* that
1189 * return value, so let's just return void for now.
1190 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001191 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001192 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001194 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1195 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 if (hasActiveCall && hasHoldingCall) {
1197 // Both lines are in use!
1198 // TODO: provide a flag to let the caller specify what
1199 // policy to use if both lines are in use. (The current
1200 // behavior is hardwired to "answer incoming, end ongoing",
1201 // which is how the CALL button is specced to behave.)
1202 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1203 return;
1204 } else {
1205 // answerCall() will automatically hold the current active
1206 // call, if there is one.
1207 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1208 return;
1209 }
1210 } else {
1211 // No call was ringing.
1212 return;
1213 }
1214 }
1215
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001216 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001217 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001218 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001219 public void silenceRinger() {
1220 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001221 }
1222
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001223 @Override
1224 public boolean isOffhook(String callingPackage) {
1225 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001226 }
1227
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001228 @Override
1229 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1230 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1231 return false;
1232 }
1233
Sanket Padawe356d7632015-06-22 14:03:32 -07001234 final Phone phone = getPhone(subId);
1235 if (phone != null) {
1236 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1237 } else {
1238 return false;
1239 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001240 }
1241
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001242 @Override
1243 public boolean isRinging(String callingPackage) {
1244 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001245 }
1246
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001247 @Override
1248 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1249 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1250 return false;
1251 }
1252
Sanket Padawe356d7632015-06-22 14:03:32 -07001253 final Phone phone = getPhone(subId);
1254 if (phone != null) {
1255 return (phone.getState() == PhoneConstants.State.RINGING);
1256 } else {
1257 return false;
1258 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001259 }
1260
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001261 @Override
1262 public boolean isIdle(String callingPackage) {
1263 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001264 }
1265
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001266 @Override
1267 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1268 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1269 return false;
1270 }
1271
Sanket Padawe356d7632015-06-22 14:03:32 -07001272 final Phone phone = getPhone(subId);
1273 if (phone != null) {
1274 return (phone.getState() == PhoneConstants.State.IDLE);
1275 } else {
1276 return false;
1277 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001278 }
1279
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001280 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001281 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001282 }
1283
Wink Savilleb564aae2014-10-23 10:18:09 -07001284 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001285 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001286 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1287 }
1288
1289 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001290 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001291 }
1292
Wink Savilleb564aae2014-10-23 10:18:09 -07001293 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001294 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001295 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1296 }
1297
1298 /** {@hide} */
1299 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001300 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001301 }
1302
Wink Savilleb564aae2014-10-23 10:18:09 -07001303 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001304 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001305 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306 checkSimPin.start();
1307 return checkSimPin.unlockSim(null, pin);
1308 }
1309
Wink Saville9de0f752013-10-22 19:04:03 -07001310 /** {@hide} */
1311 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001312 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001313 }
1314
Wink Savilleb564aae2014-10-23 10:18:09 -07001315 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001316 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001317 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001318 checkSimPuk.start();
1319 return checkSimPuk.unlockSim(puk, pin);
1320 }
1321
1322 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001323 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001324 * a synchronous one.
1325 */
1326 private static class UnlockSim extends Thread {
1327
1328 private final IccCard mSimCard;
1329
1330 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001331 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1332 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333
1334 // For replies from SimCard interface
1335 private Handler mHandler;
1336
1337 // For async handler to identify request type
1338 private static final int SUPPLY_PIN_COMPLETE = 100;
1339
1340 public UnlockSim(IccCard simCard) {
1341 mSimCard = simCard;
1342 }
1343
1344 @Override
1345 public void run() {
1346 Looper.prepare();
1347 synchronized (UnlockSim.this) {
1348 mHandler = new Handler() {
1349 @Override
1350 public void handleMessage(Message msg) {
1351 AsyncResult ar = (AsyncResult) msg.obj;
1352 switch (msg.what) {
1353 case SUPPLY_PIN_COMPLETE:
1354 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1355 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001356 mRetryCount = msg.arg1;
1357 if (ar.exception != null) {
1358 if (ar.exception instanceof CommandException &&
1359 ((CommandException)(ar.exception)).getCommandError()
1360 == CommandException.Error.PASSWORD_INCORRECT) {
1361 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1362 } else {
1363 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1364 }
1365 } else {
1366 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1367 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001368 mDone = true;
1369 UnlockSim.this.notifyAll();
1370 }
1371 break;
1372 }
1373 }
1374 };
1375 UnlockSim.this.notifyAll();
1376 }
1377 Looper.loop();
1378 }
1379
1380 /*
1381 * Use PIN or PUK to unlock SIM card
1382 *
1383 * If PUK is null, unlock SIM card with PIN
1384 *
1385 * If PUK is not null, unlock SIM card with PUK and set PIN code
1386 */
Wink Saville9de0f752013-10-22 19:04:03 -07001387 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388
1389 while (mHandler == null) {
1390 try {
1391 wait();
1392 } catch (InterruptedException e) {
1393 Thread.currentThread().interrupt();
1394 }
1395 }
1396 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1397
1398 if (puk == null) {
1399 mSimCard.supplyPin(pin, callback);
1400 } else {
1401 mSimCard.supplyPuk(puk, pin, callback);
1402 }
1403
1404 while (!mDone) {
1405 try {
1406 Log.d(LOG_TAG, "wait for done");
1407 wait();
1408 } catch (InterruptedException e) {
1409 // Restore the interrupted status
1410 Thread.currentThread().interrupt();
1411 }
1412 }
1413 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001414 int[] resultArray = new int[2];
1415 resultArray[0] = mResult;
1416 resultArray[1] = mRetryCount;
1417 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418 }
1419 }
1420
1421 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001422 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001423
1424 }
1425
Wink Savilleb564aae2014-10-23 10:18:09 -07001426 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001427 // No permission check needed here: this call is harmless, and it's
1428 // needed for the ServiceState.requestStateUpdate() call (which is
1429 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001430 final Phone phone = getPhone(subId);
1431 if (phone != null) {
1432 phone.updateServiceLocation();
1433 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 }
1435
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001436 @Override
1437 public boolean isRadioOn(String callingPackage) {
1438 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001439 }
1440
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001441 @Override
1442 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1443 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1444 return false;
1445 }
1446 return isRadioOnForSubscriber(subId);
1447 }
1448
1449 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001450 final Phone phone = getPhone(subId);
1451 if (phone != null) {
1452 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1453 } else {
1454 return false;
1455 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 }
1457
1458 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001459 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001460
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001461 }
Wink Saville36469e72014-06-11 15:17:00 -07001462
Wink Savilleb564aae2014-10-23 10:18:09 -07001463 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001464 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001465 final Phone phone = getPhone(subId);
1466 if (phone != null) {
1467 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1468 }
Wink Saville36469e72014-06-11 15:17:00 -07001469 }
1470
1471 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001472 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001473 }
1474
Wink Savilleb564aae2014-10-23 10:18:09 -07001475 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001476 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001477 final Phone phone = getPhone(subId);
1478 if (phone == null) {
1479 return false;
1480 }
1481 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001482 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001483 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001484 }
1485 return true;
1486 }
Wink Saville36469e72014-06-11 15:17:00 -07001487
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001488 public boolean needMobileRadioShutdown() {
1489 /*
1490 * If any of the Radios are available, it will need to be
1491 * shutdown. So return true if any Radio is available.
1492 */
1493 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1494 Phone phone = PhoneFactory.getPhone(i);
1495 if (phone != null && phone.isRadioAvailable()) return true;
1496 }
1497 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1498 return false;
1499 }
1500
1501 public void shutdownMobileRadios() {
1502 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1503 logv("Shutting down Phone " + i);
1504 shutdownRadioUsingPhoneId(i);
1505 }
1506 }
1507
1508 private void shutdownRadioUsingPhoneId(int phoneId) {
1509 enforceModifyPermission();
1510 Phone phone = PhoneFactory.getPhone(phoneId);
1511 if (phone != null && phone.isRadioAvailable()) {
1512 phone.shutdownRadio();
1513 }
1514 }
1515
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001516 public boolean setRadioPower(boolean turnOn) {
Jack Yub4e16162017-05-15 12:48:40 -07001517 enforceModifyPermission();
Wei Liu9ae2a062016-08-08 11:09:34 -07001518 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1519 if (defaultPhone != null) {
1520 defaultPhone.setRadioPower(turnOn);
1521 return true;
1522 } else {
1523 loge("There's no default phone.");
1524 return false;
1525 }
Wink Saville36469e72014-06-11 15:17:00 -07001526 }
1527
Wink Savilleb564aae2014-10-23 10:18:09 -07001528 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001529 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001530 final Phone phone = getPhone(subId);
1531 if (phone != null) {
1532 phone.setRadioPower(turnOn);
1533 return true;
1534 } else {
1535 return false;
1536 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001537 }
1538
Wink Saville36469e72014-06-11 15:17:00 -07001539 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001540 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001541 public boolean enableDataConnectivity() {
1542 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001543 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001544 final Phone phone = getPhone(subId);
1545 if (phone != null) {
1546 phone.setDataEnabled(true);
1547 return true;
1548 } else {
1549 return false;
1550 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001551 }
1552
Wink Saville36469e72014-06-11 15:17:00 -07001553 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001554 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001555 public boolean disableDataConnectivity() {
1556 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001557 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001558 final Phone phone = getPhone(subId);
1559 if (phone != null) {
1560 phone.setDataEnabled(false);
1561 return true;
1562 } else {
1563 return false;
1564 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001565 }
1566
Wink Saville36469e72014-06-11 15:17:00 -07001567 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001568 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001569 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001570 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001571 final Phone phone = getPhone(subId);
1572 if (phone != null) {
1573 return phone.isDataConnectivityPossible();
1574 } else {
1575 return false;
1576 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001577 }
1578
1579 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001580 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001581 }
1582
pkanwarae03a6b2016-11-06 20:37:09 -08001583 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001584 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001585 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1586 return;
1587 }
1588 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1589 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1590 };
1591
1592
Wink Savilleb564aae2014-10-23 10:18:09 -07001593 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001594 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001595 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1596 return false;
1597 }
Wink Saville36469e72014-06-11 15:17:00 -07001598 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001599 }
1600
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001601 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001602 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001603 }
1604
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001605 public int getCallStateForSlot(int slotIndex) {
1606 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001607 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harolda667c152016-12-14 11:27:20 -08001608 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001609 }
1610
Sanket Padawe356d7632015-06-22 14:03:32 -07001611 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001612 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001613 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001614 if (phone != null) {
Nathan Harolda667c152016-12-14 11:27:20 -08001615 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001616 } else {
Nathan Harolda667c152016-12-14 11:27:20 -08001617 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001618 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001619 }
1620
Sanket Padawe356d7632015-06-22 14:03:32 -07001621 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001622 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001623 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001624 if (phone != null) {
1625 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1626 } else {
1627 return TelephonyManager.DATA_ACTIVITY_NONE;
1628 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001629 }
1630
1631 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001632 public Bundle getCellLocation(String callingPackage) {
1633 enforceFineOrCoarseLocationPermission("getCellLocation");
1634
1635 // OP_COARSE_LOCATION controls both fine and coarse location.
1636 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1637 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001638 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001639 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001640 }
1641
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001642 if (checkIfCallerIsSelfOrForegroundUser() ||
1643 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001644 if (DBG_LOC) log("getCellLocation: is active user");
1645 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001646 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001647 if (phone == null) {
1648 return null;
1649 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001650
1651 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1652 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001653 return data;
1654 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001655 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001656 return null;
1657 }
1658 }
1659
Svetoslav64fad262015-04-14 14:35:21 -07001660 private void enforceFineOrCoarseLocationPermission(String message) {
1661 try {
1662 mApp.enforceCallingOrSelfPermission(
1663 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1664 } catch (SecurityException e) {
1665 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1666 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1667 // is the weaker precondition
1668 mApp.enforceCallingOrSelfPermission(
1669 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1670 }
1671 }
1672
1673
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001674 @Override
1675 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001676 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001677 }
1678
Sanket Padawe356d7632015-06-22 14:03:32 -07001679 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001680 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001681 mApp.enforceCallingOrSelfPermission(
1682 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001683 final Phone phone = getPhone(subId);
1684 if (phone != null) {
1685 phone.enableLocationUpdates();
1686 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001687 }
1688
1689 @Override
1690 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001691 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001692 }
1693
Sanket Padawe356d7632015-06-22 14:03:32 -07001694 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001695 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001696 mApp.enforceCallingOrSelfPermission(
1697 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001698 final Phone phone = getPhone(subId);
1699 if (phone != null) {
1700 phone.disableLocationUpdates();
1701 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001702 }
1703
1704 @Override
1705 @SuppressWarnings("unchecked")
1706 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001707 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1708
1709 // OP_COARSE_LOCATION controls both fine and coarse location.
1710 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1711 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1712 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001713 }
1714
1715 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1716 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1717 return null;
1718 }
Svetoslav64fad262015-04-14 14:35:21 -07001719
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001720 if (checkIfCallerIsSelfOrForegroundUser() ||
1721 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001722 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1723
1724 ArrayList<NeighboringCellInfo> cells = null;
1725
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001726 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001727 try {
1728 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001729 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001730 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001731 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001732 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001733 }
1734 return cells;
1735 } else {
1736 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1737 return null;
1738 }
1739 }
1740
1741
1742 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001743 public List<CellInfo> getAllCellInfo(String callingPackage) {
1744 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1745
1746 // OP_COARSE_LOCATION controls both fine and coarse location.
1747 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1748 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1749 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001750 }
1751
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001752 if (checkIfCallerIsSelfOrForegroundUser() ||
1753 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001754 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001755 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001756 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1757 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001758 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1759 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001760 }
1761 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001762 } else {
1763 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1764 return null;
1765 }
1766 }
1767
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001768 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001769 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08001770 enforceModifyPermission();
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001771 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1772 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001773 }
1774
Shishir Agrawala9f32182016-04-12 12:00:16 -07001775 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001776 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001777 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1778 return null;
1779 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001780 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001781 return phone == null ? null : phone.getImei();
1782 }
1783
1784 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07001785 public String getMeidForSlot(int slotIndex, String callingPackage) {
1786 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1787 return null;
1788 }
1789 Phone phone = PhoneFactory.getPhone(slotIndex);
1790 return phone == null ? null : phone.getMeid();
1791 }
1792
1793 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001794 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001795 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1796 return null;
1797 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001798 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001799 return phone == null ? null : phone.getDeviceSvn();
1800 }
1801
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001802 //
1803 // Internal helper methods.
1804 //
1805
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001806 /**
1807 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1808 */
1809 private boolean checkCallerInteractAcrossUsersFull() {
1810 return mPhone.getContext().checkCallingOrSelfPermission(
1811 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1812 == PackageManager.PERMISSION_GRANTED;
1813 }
1814
Jake Hambye994d462014-02-03 13:10:13 -08001815 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001816 boolean ok;
1817
1818 boolean self = Binder.getCallingUid() == Process.myUid();
1819 if (!self) {
1820 // Get the caller's user id then clear the calling identity
1821 // which will be restored in the finally clause.
1822 int callingUser = UserHandle.getCallingUserId();
1823 long ident = Binder.clearCallingIdentity();
1824
1825 try {
1826 // With calling identity cleared the current user is the foreground user.
1827 int foregroundUser = ActivityManager.getCurrentUser();
1828 ok = (foregroundUser == callingUser);
1829 if (DBG_LOC) {
1830 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1831 + " callingUser=" + callingUser + " ok=" + ok);
1832 }
1833 } catch (Exception ex) {
1834 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1835 ok = false;
1836 } finally {
1837 Binder.restoreCallingIdentity(ident);
1838 }
1839 } else {
1840 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1841 ok = true;
1842 }
1843 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1844 return ok;
1845 }
1846
1847 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001848 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1849 *
1850 * @throws SecurityException if the caller does not have the required permission
1851 */
1852 private void enforceModifyPermission() {
1853 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1854 }
1855
1856 /**
Junda Liua2e36012014-07-09 18:30:01 -07001857 * Make sure either system app or the caller has carrier privilege.
1858 *
1859 * @throws SecurityException if the caller does not have the required permission/privilege
1860 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001861 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001862 int permission = mApp.checkCallingOrSelfPermission(
1863 android.Manifest.permission.MODIFY_PHONE_STATE);
1864 if (permission == PackageManager.PERMISSION_GRANTED) {
1865 return;
1866 }
1867
1868 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001869 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001870 }
1871
1872 /**
1873 * Make sure the caller has carrier privilege.
1874 *
1875 * @throws SecurityException if the caller does not have the required permission
1876 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001877 private void enforceCarrierPrivilege(int subId) {
1878 if (getCarrierPrivilegeStatus(subId) !=
1879 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001880 loge("No Carrier Privilege.");
1881 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001882 }
1883 }
1884
1885 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001886 * Make sure the caller has the CALL_PHONE permission.
1887 *
1888 * @throws SecurityException if the caller does not have the required permission
1889 */
1890 private void enforceCallPermission() {
1891 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1892 }
1893
Stuart Scott8eef64f2015-04-08 15:13:54 -07001894 private void enforceConnectivityInternalPermission() {
1895 mApp.enforceCallingOrSelfPermission(
1896 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1897 "ConnectivityService");
1898 }
1899
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001900 private String createTelUrl(String number) {
1901 if (TextUtils.isEmpty(number)) {
1902 return null;
1903 }
1904
Jake Hambye994d462014-02-03 13:10:13 -08001905 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001906 }
1907
Ihab Awadf9e92732013-12-05 18:02:52 -08001908 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001909 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1910 }
1911
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001912 private static void logv(String msg) {
1913 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1914 }
1915
Ihab Awadf9e92732013-12-05 18:02:52 -08001916 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001917 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1918 }
1919
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001920 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001921 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001922 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001923 }
1924
Sanket Padawe356d7632015-06-22 14:03:32 -07001925 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001926 public int getActivePhoneTypeForSlot(int slotIndex) {
1927 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001928 if (phone == null) {
1929 return PhoneConstants.PHONE_TYPE_NONE;
1930 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001931 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001932 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001933 }
1934
1935 /**
1936 * Returns the CDMA ERI icon index to display
1937 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001938 @Override
1939 public int getCdmaEriIconIndex(String callingPackage) {
1940 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001941 }
1942
Sanket Padawe356d7632015-06-22 14:03:32 -07001943 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001944 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1945 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1946 return -1;
1947 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001948 final Phone phone = getPhone(subId);
1949 if (phone != null) {
1950 return phone.getCdmaEriIconIndex();
1951 } else {
1952 return -1;
1953 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001954 }
1955
1956 /**
1957 * Returns the CDMA ERI icon mode,
1958 * 0 - ON
1959 * 1 - FLASHING
1960 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001961 @Override
1962 public int getCdmaEriIconMode(String callingPackage) {
1963 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001964 }
1965
Sanket Padawe356d7632015-06-22 14:03:32 -07001966 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001967 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1968 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1969 return -1;
1970 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001971 final Phone phone = getPhone(subId);
1972 if (phone != null) {
1973 return phone.getCdmaEriIconMode();
1974 } else {
1975 return -1;
1976 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001977 }
1978
1979 /**
1980 * Returns the CDMA ERI text,
1981 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001982 @Override
1983 public String getCdmaEriText(String callingPackage) {
1984 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001985 }
1986
Sanket Padawe356d7632015-06-22 14:03:32 -07001987 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001988 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1989 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1990 return null;
1991 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001992 final Phone phone = getPhone(subId);
1993 if (phone != null) {
1994 return phone.getCdmaEriText();
1995 } else {
1996 return null;
1997 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001998 }
1999
2000 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07002001 * Returns the CDMA MDN.
2002 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002003 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002004 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002005 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002006 final Phone phone = getPhone(subId);
2007 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
2008 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07002009 } else {
2010 return null;
2011 }
2012 }
2013
2014 /**
2015 * Returns the CDMA MIN.
2016 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002017 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002018 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002019 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002020 final Phone phone = getPhone(subId);
2021 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2022 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07002023 } else {
2024 return null;
2025 }
2026 }
2027
2028 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002029 * Returns true if CDMA provisioning needs to run.
2030 */
2031 public boolean needsOtaServiceProvisioning() {
2032 return mPhone.needsOtaServiceProvisioning();
2033 }
2034
2035 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002036 * Sets the voice mail number of a given subId.
2037 */
2038 @Override
2039 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002040 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002041 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2042 new Pair<String, String>(alphaTag, number), new Integer(subId));
2043 return success;
2044 }
2045
Ta-wei Yen87c49842016-05-13 21:19:52 -07002046 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002047 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2048 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2049 String systemDialer = TelecomManager.from(mPhone.getContext()).getSystemDialerPackage();
2050 if (!TextUtils.equals(callingPackage, systemDialer)) {
2051 throw new SecurityException("caller must be system dialer");
2052 }
2053 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2054 if (phoneAccountHandle == null){
2055 return null;
2056 }
2057 return VisualVoicemailSettingsUtil.dump(mPhone.getContext(), phoneAccountHandle);
2058 }
2059
2060 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002061 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002062 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2063 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2064 return null;
2065 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002066 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2067 }
2068
2069 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002070 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2071 VisualVoicemailSmsFilterSettings settings) {
2072 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002073 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002074 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2075 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002076 }
2077
2078 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002079 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2080 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002081 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002082 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002083 }
2084
2085 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002086 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2087 String callingPackage, int subId) {
2088 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002089 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002090 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002091 }
2092
2093 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002094 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002095 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002096 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002097 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2098 }
2099
2100 @Override
2101 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2102 String number, int port, String text, PendingIntent sentIntent) {
2103 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002104 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002105 enforceSendSmsPermission();
2106 // Make the calls as the phone process.
2107 final long identity = Binder.clearCallingIdentity();
2108 try {
2109 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2110 if (port == 0) {
2111 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2112 sentIntent, null, false);
2113 } else {
2114 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2115 smsManager.sendDataMessageWithSelfPermissions(number, null,
2116 (short) port, data, sentIntent, null);
2117 }
2118 } finally {
2119 Binder.restoreCallingIdentity(identity);
2120 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002121 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002122 /**
fionaxu0152e512016-11-14 13:36:14 -08002123 * Sets the voice activation state of a given subId.
2124 */
2125 @Override
2126 public void setVoiceActivationState(int subId, int activationState) {
2127 enforceModifyPermissionOrCarrierPrivilege(subId);
2128 final Phone phone = getPhone(subId);
2129 if (phone != null) {
2130 phone.setVoiceActivationState(activationState);
2131 } else {
2132 loge("setVoiceActivationState fails with invalid subId: " + subId);
2133 }
2134 }
2135
2136 /**
2137 * Sets the data activation state of a given subId.
2138 */
2139 @Override
2140 public void setDataActivationState(int subId, int activationState) {
2141 enforceModifyPermissionOrCarrierPrivilege(subId);
2142 final Phone phone = getPhone(subId);
2143 if (phone != null) {
2144 phone.setDataActivationState(activationState);
2145 } else {
2146 loge("setVoiceActivationState fails with invalid subId: " + subId);
2147 }
2148 }
2149
2150 /**
2151 * Returns the voice activation state of a given subId.
2152 */
2153 @Override
2154 public int getVoiceActivationState(int subId, String callingPackage) {
2155 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2156 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2157 }
2158 final Phone phone = getPhone(subId);
2159 if (phone != null) {
2160 return phone.getVoiceActivationState();
2161 } else {
2162 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2163 }
2164 }
2165
2166 /**
2167 * Returns the data activation state of a given subId.
2168 */
2169 @Override
2170 public int getDataActivationState(int subId, String callingPackage) {
2171 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2172 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2173 }
2174 final Phone phone = getPhone(subId);
2175 if (phone != null) {
2176 return phone.getDataActivationState();
2177 } else {
2178 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2179 }
2180 }
2181
2182 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002183 * Returns the unread count of voicemails
2184 */
2185 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002186 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002187 }
2188
2189 /**
2190 * Returns the unread count of voicemails for a subId
2191 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002192 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002193 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002194 final Phone phone = getPhone(subId);
2195 if (phone != null) {
2196 return phone.getVoiceMessageCount();
2197 } else {
2198 return 0;
2199 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002200 }
2201
2202 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002203 * returns true, if the device is in a state where both voice and data
2204 * are supported simultaneously. This can change based on location or network condition.
2205 */
2206 @Override
2207 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2208 final Phone phone = getPhone(subId);
2209 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2210 }
2211
2212 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002213 * Send the dialer code if called from the current default dialer or the caller has
2214 * carrier privilege.
2215 * @param inputCode The dialer code to send
2216 */
2217 @Override
2218 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
2219 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2220 String defaultDialer = TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage();
2221 if (!TextUtils.equals(callingPackage, defaultDialer)) {
2222 enforceCarrierPrivilege(getDefaultSubscription());
2223 }
2224 mPhone.sendDialerSpecialCode(inputCode);
2225 }
2226
2227 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002228 * Returns the data network type.
2229 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002230 *
2231 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2232 */
2233 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002234 public int getNetworkType() {
2235 final Phone phone = getPhone(getDefaultSubscription());
2236 if (phone != null) {
2237 return phone.getServiceState().getDataNetworkType();
2238 } else {
2239 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2240 }
Wink Saville36469e72014-06-11 15:17:00 -07002241 }
2242
2243 /**
2244 * Returns the network type for a subId
2245 */
2246 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002247 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2248 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2249 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2250 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002251
Sanket Padawe356d7632015-06-22 14:03:32 -07002252 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002253 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002254 return phone.getServiceState().getDataNetworkType();
2255 } else {
2256 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2257 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002258 }
2259
2260 /**
2261 * Returns the data network type
2262 */
2263 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002264 public int getDataNetworkType(String callingPackage) {
2265 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002266 }
2267
2268 /**
2269 * Returns the data network type for a subId
2270 */
2271 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002272 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2273 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2274 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2275 }
2276
Sanket Padawe356d7632015-06-22 14:03:32 -07002277 final Phone phone = getPhone(subId);
2278 if (phone != null) {
2279 return phone.getServiceState().getDataNetworkType();
2280 } else {
2281 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2282 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002283 }
2284
2285 /**
Wink Saville36469e72014-06-11 15:17:00 -07002286 * Returns the Voice network type for a subId
2287 */
2288 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002289 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2290 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2291 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2292 }
2293
Sanket Padawe356d7632015-06-22 14:03:32 -07002294 final Phone phone = getPhone(subId);
2295 if (phone != null) {
2296 return phone.getServiceState().getVoiceNetworkType();
2297 } else {
2298 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2299 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002300 }
2301
2302 /**
2303 * @return true if a ICC card is present
2304 */
2305 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002306 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002307 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2308 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002309 }
2310
2311 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002312 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002313 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002314 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002315 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2316 final Phone phone = PhoneFactory.getPhone(slotIndex);
fionaxu6e6c68b2016-06-23 13:31:32 -07002317 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002318 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002319 } else {
2320 return false;
2321 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002322 }
2323
2324 /**
2325 * Return if the current radio is LTE on CDMA. This
2326 * is a tri-state return value as for a period of time
2327 * the mode may be unknown.
2328 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002329 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002330 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002331 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002332 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002333 @Override
2334 public int getLteOnCdmaMode(String callingPackage) {
2335 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002336 }
2337
Sanket Padawe356d7632015-06-22 14:03:32 -07002338 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002339 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2340 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2341 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2342 }
2343
Sanket Padawe356d7632015-06-22 14:03:32 -07002344 final Phone phone = getPhone(subId);
2345 if (phone == null) {
2346 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2347 } else {
2348 return phone.getLteOnCdmaMode();
2349 }
Wink Saville36469e72014-06-11 15:17:00 -07002350 }
2351
2352 public void setPhone(Phone phone) {
2353 mPhone = phone;
2354 }
2355
2356 /**
2357 * {@hide}
2358 * Returns Default subId, 0 in the case of single standby.
2359 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002360 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002361 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002362 }
2363
Shishir Agrawala9f32182016-04-12 12:00:16 -07002364 private int getSlotForDefaultSubscription() {
2365 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2366 }
2367
Wink Savilleb564aae2014-10-23 10:18:09 -07002368 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002369 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002370 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002371
2372 /**
2373 * @see android.telephony.TelephonyManager.WifiCallingChoices
2374 */
2375 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002376 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2377 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002378 }
2379
2380 /**
2381 * @see android.telephony.TelephonyManager.WifiCallingChoices
2382 */
2383 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002384 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2385 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2386 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002387 }
2388
Sailesh Nepald1e68152013-12-12 19:08:02 -08002389 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002390 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002391 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002392 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002393
Shishir Agrawal566b7612013-10-28 14:41:00 -07002394 @Override
Ajay Nambid7454d32015-12-03 13:50:00 -08002395 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002396 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002397
Ajay Nambid7454d32015-12-03 13:50:00 -08002398 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002399 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambid7454d32015-12-03 13:50:00 -08002400 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002401 if (DBG) log("iccOpenLogicalChannel: " + response);
2402 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002403 }
2404
2405 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002406 public boolean iccCloseLogicalChannel(int subId, int channel) {
2407 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002408
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002409 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002410 if (channel < 0) {
2411 return false;
2412 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002413 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002414 if (DBG) log("iccCloseLogicalChannel: " + success);
2415 return success;
2416 }
2417
2418 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002419 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002420 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002421 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002422
2423 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002424 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2425 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002426 " data=" + data);
2427 }
2428
2429 if (channel < 0) {
2430 return "";
2431 }
2432
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002433 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002434 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002435 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2436
Shishir Agrawal566b7612013-10-28 14:41:00 -07002437 // Append the returned status code to the end of the response payload.
2438 String s = Integer.toHexString(
2439 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002440 if (response.payload != null) {
2441 s = IccUtils.bytesToHexString(response.payload) + s;
2442 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002443 return s;
2444 }
Jake Hambye994d462014-02-03 13:10:13 -08002445
Evan Charltonc66da362014-05-16 14:06:40 -07002446 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002447 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002448 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002449 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002450
2451 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002452 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2453 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002454 }
2455
2456 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002457 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002458 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2459
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002460 // Append the returned status code to the end of the response payload.
2461 String s = Integer.toHexString(
2462 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002463 if (response.payload != null) {
2464 s = IccUtils.bytesToHexString(response.payload) + s;
2465 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002466 return s;
2467 }
2468
2469 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002470 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002471 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002472 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002473
2474 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002475 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002476 p1 + " " + p2 + " " + p3 + ":" + filePath);
2477 }
2478
2479 IccIoResult response =
2480 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002481 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2482 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002483
2484 if (DBG) {
2485 log("Exchange SIM_IO [R]" + response);
2486 }
2487
2488 byte[] result = null;
2489 int length = 2;
2490 if (response.payload != null) {
2491 length = 2 + response.payload.length;
2492 result = new byte[length];
2493 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2494 } else {
2495 result = new byte[length];
2496 }
2497
2498 result[length - 1] = (byte) response.sw2;
2499 result[length - 2] = (byte) response.sw1;
2500 return result;
2501 }
2502
Nathan Haroldb3014052017-01-25 15:57:32 -08002503 /**
2504 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2505 * on a particular subscription
2506 */
2507 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold8c92f382017-04-13 18:19:05 -07002508 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2509 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002510 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2511 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2512 return null;
2513 }
2514 Object response = sendRequest(
2515 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2516 if (response instanceof String[]) {
2517 return (String[]) response;
2518 }
2519 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2520 return null;
2521 }
2522
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002523 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002524 public String sendEnvelopeWithStatus(int subId, String content) {
2525 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002526
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002527 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002528 if (response.payload == null) {
2529 return "";
2530 }
2531
2532 // Append the returned status code to the end of the response payload.
2533 String s = Integer.toHexString(
2534 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2535 s = IccUtils.bytesToHexString(response.payload) + s;
2536 return s;
2537 }
2538
Jake Hambye994d462014-02-03 13:10:13 -08002539 /**
2540 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2541 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2542 *
2543 * @param itemID the ID of the item to read
2544 * @return the NV item as a String, or null on error.
2545 */
2546 @Override
2547 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002548 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002549 if (DBG) log("nvReadItem: item " + itemID);
2550 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2551 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2552 return value;
2553 }
2554
2555 /**
2556 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2557 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2558 *
2559 * @param itemID the ID of the item to read
2560 * @param itemValue the value to write, as a String
2561 * @return true on success; false on any failure
2562 */
2563 @Override
2564 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002565 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002566 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2567 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2568 new Pair<Integer, String>(itemID, itemValue));
2569 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2570 return success;
2571 }
2572
2573 /**
2574 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2575 * Used for device configuration by some CDMA operators.
2576 *
2577 * @param preferredRoamingList byte array containing the new PRL
2578 * @return true on success; false on any failure
2579 */
2580 @Override
2581 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002582 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002583 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2584 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2585 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2586 return success;
2587 }
2588
2589 /**
2590 * Perform the specified type of NV config reset.
2591 * Used for device configuration by some CDMA operators.
2592 *
2593 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2594 * @return true on success; false on any failure
2595 */
2596 @Override
2597 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002598 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002599 if (DBG) log("nvResetConfig: type " + resetType);
2600 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2601 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2602 return success;
2603 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002604
2605 /**
Wink Saville36469e72014-06-11 15:17:00 -07002606 * {@hide}
2607 * Returns Default sim, 0 in the case of single standby.
2608 */
2609 public int getDefaultSim() {
2610 //TODO Need to get it from Telephony Devcontroller
2611 return 0;
2612 }
2613
Svet Ganovb320e182015-04-16 12:30:10 -07002614 public String[] getPcscfAddress(String apnType, String callingPackage) {
2615 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2616 return new String[0];
2617 }
2618
2619
ram87fca6f2014-07-18 18:58:44 +05302620 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002621 }
2622
Brad Ebinger51f743a2017-01-23 13:50:20 -08002623 /**
2624 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2625 * feature or {@link null} if the service is not available. If an ImsServiceController is
2626 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2627 * feature updates.
2628 */
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002629 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002630 IImsServiceFeatureListener callback) {
2631 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002632 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002633 callback);
2634 }
2635
Wink Saville36469e72014-06-11 15:17:00 -07002636 public void setImsRegistrationState(boolean registered) {
2637 enforceModifyPermission();
2638 mPhone.setImsRegistrationState(registered);
2639 }
2640
2641 /**
Stuart Scott54788802015-03-30 13:18:01 -07002642 * Set the network selection mode to automatic.
2643 *
2644 */
2645 @Override
2646 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002647 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002648 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2649 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2650 }
2651
2652 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002653 * Set the network selection mode to manual with the selected carrier.
2654 */
2655 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002656 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2657 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002658 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002659 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002660 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2661 persistSelection);
2662 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002663 }
2664
2665 /**
2666 * Scans for available networks.
2667 */
2668 @Override
2669 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002670 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002671 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2672 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2673 CMD_PERFORM_NETWORK_SCAN, null, subId);
2674 return result;
2675 }
2676
2677 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002678 * Get the calculated preferred network type.
2679 * Used for debugging incorrect network type.
2680 *
2681 * @return the preferred network type, defined in RILConstants.java.
2682 */
2683 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002684 public int getCalculatedPreferredNetworkType(String callingPackage) {
2685 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2686 return RILConstants.PREFERRED_NETWORK_MODE;
2687 }
2688
Amit Mahajan43330e02014-11-18 11:54:45 -08002689 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002690 }
2691
2692 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002693 * Get the preferred network type.
2694 * Used for device configuration by some CDMA operators.
2695 *
2696 * @return the preferred network type, defined in RILConstants.java.
2697 */
2698 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002699 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002700 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002701 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002702 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002703 int networkType = (result != null ? result[0] : -1);
2704 if (DBG) log("getPreferredNetworkType: " + networkType);
2705 return networkType;
2706 }
2707
2708 /**
2709 * Set the preferred network type.
2710 * Used for device configuration by some CDMA operators.
2711 *
2712 * @param networkType the preferred network type, defined in RILConstants.java.
2713 * @return true on success; false on any failure.
2714 */
2715 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002716 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002717 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002718 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2719 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002720 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002721 if (success) {
2722 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002723 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002724 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002725 return success;
2726 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002727
2728 /**
Junda Liu475951f2014-11-07 16:45:03 -08002729 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2730 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2731 * tethering.
2732 *
2733 * @return 0: Not required. 1: required. 2: Not set.
2734 * @hide
2735 */
2736 @Override
2737 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002738 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002739 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2740 Settings.Global.TETHER_DUN_REQUIRED, 2);
2741 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2742 // config_tether_apndata.
2743 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2744 dunRequired = 1;
2745 }
2746 return dunRequired;
2747 }
2748
2749 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002750 * Set mobile data enabled
2751 * Used by the user through settings etc to turn on/off mobile data
2752 *
2753 * @param enable {@code true} turn turn data on, else {@code false}
2754 */
2755 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002756 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002757 enforceModifyPermissionOrCarrierPrivilege(subId);
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) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002762 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002763 phone.setDataEnabled(enable);
2764 } else {
2765 loge("setDataEnabled: no phone for subId=" + subId);
2766 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002767 }
2768
2769 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002770 * Get whether mobile data is enabled.
2771 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002772 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002773 *
2774 * @return {@code true} if data is enabled else {@code false}
2775 */
2776 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002777 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002778 try {
2779 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2780 null);
2781 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002782 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002783 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002784 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002785 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002786 Phone phone = PhoneFactory.getPhone(phoneId);
2787 if (phone != null) {
2788 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002789 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002790 return retVal;
2791 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002792 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002793 return false;
2794 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002795 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002796
2797 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002798 public int getCarrierPrivilegeStatus(int subId) {
2799 final Phone phone = getPhone(subId);
2800 if (phone == null) {
2801 loge("getCarrierPrivilegeStatus: Invalid subId");
2802 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2803 }
2804 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002805 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002806 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002807 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2808 }
2809 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002810 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002811 }
Junda Liu29340342014-07-10 15:23:27 -07002812
2813 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002814 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002815 if (TextUtils.isEmpty(pkgName))
2816 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002817 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002818 if (card == null) {
2819 loge("checkCarrierPrivilegesForPackage: No UICC");
2820 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2821 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002822 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2823 }
2824
2825 @Override
2826 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002827 if (TextUtils.isEmpty(pkgName))
2828 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002829 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2830 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2831 UiccCard card = UiccController.getInstance().getUiccCard(i);
2832 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002833 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002834 continue;
2835 }
2836
2837 result = card.getCarrierPrivilegeStatus(
2838 mPhone.getContext().getPackageManager(), pkgName);
2839 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2840 break;
2841 }
2842 }
2843
2844 return result;
Junda Liu29340342014-07-10 15:23:27 -07002845 }
Derek Tan89e89d42014-07-08 17:00:10 -07002846
2847 @Override
Junda Liue64de782015-04-16 17:19:16 -07002848 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2849 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2850 loge("phoneId " + phoneId + " is not valid.");
2851 return null;
2852 }
2853 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002854 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002855 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002856 return null ;
2857 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002858 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002859 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002860 }
2861
Amith Yamasani6e118872016-02-19 12:53:51 -08002862 @Override
2863 public List<String> getPackagesWithCarrierPrivileges() {
2864 PackageManager pm = mPhone.getContext().getPackageManager();
2865 List<String> privilegedPackages = new ArrayList<>();
2866 List<PackageInfo> packages = null;
2867 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2868 UiccCard card = UiccController.getInstance().getUiccCard(i);
2869 if (card == null) {
2870 // No UICC in that slot.
2871 continue;
2872 }
2873 if (card.hasCarrierPrivilegeRules()) {
2874 if (packages == null) {
2875 // Only check packages in user 0 for now
2876 packages = pm.getInstalledPackagesAsUser(
2877 PackageManager.MATCH_DISABLED_COMPONENTS
2878 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2879 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2880 }
2881 for (int p = packages.size() - 1; p >= 0; p--) {
2882 PackageInfo pkgInfo = packages.get(p);
2883 if (pkgInfo != null && pkgInfo.packageName != null
2884 && card.getCarrierPrivilegeStatus(pkgInfo)
2885 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2886 privilegedPackages.add(pkgInfo.packageName);
2887 }
2888 }
2889 }
2890 }
2891 return privilegedPackages;
2892 }
2893
Wink Savilleb564aae2014-10-23 10:18:09 -07002894 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002895 final Phone phone = getPhone(subId);
2896 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002897 if (card == null) {
2898 loge("getIccId: No UICC");
2899 return null;
2900 }
2901 String iccId = card.getIccId();
2902 if (TextUtils.isEmpty(iccId)) {
2903 loge("getIccId: ICC ID is null or empty.");
2904 return null;
2905 }
2906 return iccId;
2907 }
2908
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002909 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002910 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2911 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002912 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002913
Jeff Sharkey85190e62014-12-05 09:40:12 -08002914 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002915 final Phone phone = getPhone(subId);
2916 if (phone == null) {
2917 return false;
2918 }
2919 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002920
2921 if (DBG_MERGE) {
2922 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2923 + subscriberId + " to " + number);
2924 }
2925
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002926 if (TextUtils.isEmpty(iccId)) {
2927 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002928 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002929
Jeff Sharkey85190e62014-12-05 09:40:12 -08002930 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2931
2932 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002933 if (alphaTag == null) {
2934 editor.remove(alphaTagPrefKey);
2935 } else {
2936 editor.putString(alphaTagPrefKey, alphaTag);
2937 }
2938
Jeff Sharkey85190e62014-12-05 09:40:12 -08002939 // Record both the line number and IMSI for this ICCID, since we need to
2940 // track all merged IMSIs based on line number
2941 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2942 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002943 if (number == null) {
2944 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002945 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002946 } else {
2947 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002948 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002949 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002950
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002951 editor.commit();
2952 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002953 }
2954
2955 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002956 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002957 // This is open to apps with WRITE_SMS.
2958 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002959 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002960 return null;
2961 }
Derek Tan97ebb422014-09-05 16:55:38 -07002962
2963 String iccId = getIccId(subId);
2964 if (iccId != null) {
2965 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002966 if (DBG_MERGE) {
2967 log("getLine1NumberForDisplay returning " +
2968 mTelephonySharedPreferences.getString(numberPrefKey, null));
2969 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002970 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002971 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002972 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002973 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002974 }
2975
2976 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002977 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2978 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2979 return null;
2980 }
Derek Tan97ebb422014-09-05 16:55:38 -07002981
2982 String iccId = getIccId(subId);
2983 if (iccId != null) {
2984 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002985 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002986 }
Derek Tan97ebb422014-09-05 16:55:38 -07002987 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002988 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002989
2990 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002991 public String[] getMergedSubscriberIds(String callingPackage) {
2992 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2993 return null;
2994 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002995 final Context context = mPhone.getContext();
2996 final TelephonyManager tele = TelephonyManager.from(context);
2997 final SubscriptionManager sub = SubscriptionManager.from(context);
2998
2999 // Figure out what subscribers are currently active
3000 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07003001 // Clear calling identity, when calling TelephonyManager, because callerUid must be
3002 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
3003 final long identity = Binder.clearCallingIdentity();
3004 try {
3005 final int[] subIds = sub.getActiveSubscriptionIdList();
3006 for (int subId : subIds) {
3007 activeSubscriberIds.add(tele.getSubscriberId(subId));
3008 }
3009 } finally {
3010 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08003011 }
3012
3013 // First pass, find a number override for an active subscriber
3014 String mergeNumber = null;
3015 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
3016 for (String key : prefs.keySet()) {
3017 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
3018 final String subscriberId = (String) prefs.get(key);
3019 if (activeSubscriberIds.contains(subscriberId)) {
3020 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
3021 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3022 mergeNumber = (String) prefs.get(numberKey);
3023 if (DBG_MERGE) {
3024 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3025 + " for active subscriber " + subscriberId);
3026 }
3027 if (!TextUtils.isEmpty(mergeNumber)) {
3028 break;
3029 }
3030 }
3031 }
3032 }
3033
3034 // Shortcut when no active merged subscribers
3035 if (TextUtils.isEmpty(mergeNumber)) {
3036 return null;
3037 }
3038
3039 // Second pass, find all subscribers under that line override
3040 final ArraySet<String> result = new ArraySet<>();
3041 for (String key : prefs.keySet()) {
3042 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3043 final String number = (String) prefs.get(key);
3044 if (mergeNumber.equals(number)) {
3045 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3046 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3047 final String subscriberId = (String) prefs.get(subscriberKey);
3048 if (!TextUtils.isEmpty(subscriberId)) {
3049 result.add(subscriberId);
3050 }
3051 }
3052 }
3053 }
3054
3055 final String[] resultArray = result.toArray(new String[result.size()]);
3056 Arrays.sort(resultArray);
3057 if (DBG_MERGE) {
3058 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3059 }
3060 return resultArray;
3061 }
3062
3063 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003064 public boolean setOperatorBrandOverride(int subId, String brand) {
3065 enforceCarrierPrivilege(subId);
3066 final Phone phone = getPhone(subId);
3067 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003068 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003069
3070 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003071 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003072 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3073 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003074 enforceCarrierPrivilege(subId);
3075 final Phone phone = getPhone(subId);
3076 if (phone == null) {
3077 return false;
3078 }
3079 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003080 cdmaNonRoamingList);
3081 }
3082
3083 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003084 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003085 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3086 enforceModifyPermission();
3087
3088 int returnValue = 0;
3089 try {
3090 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3091 if(result.exception == null) {
3092 if (result.result != null) {
3093 byte[] responseData = (byte[])(result.result);
3094 if(responseData.length > oemResp.length) {
3095 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3096 responseData.length + "bytes. Buffer Size is " +
3097 oemResp.length + "bytes.");
3098 }
3099 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3100 returnValue = responseData.length;
3101 }
3102 } else {
3103 CommandException ex = (CommandException) result.exception;
3104 returnValue = ex.getCommandError().ordinal();
3105 if(returnValue > 0) returnValue *= -1;
3106 }
3107 } catch (RuntimeException e) {
3108 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3109 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3110 if(returnValue > 0) returnValue *= -1;
3111 }
3112
3113 return returnValue;
3114 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003115
3116 @Override
3117 public void setRadioCapability(RadioAccessFamily[] rafs) {
3118 try {
3119 ProxyController.getInstance().setRadioCapability(rafs);
3120 } catch (RuntimeException e) {
3121 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3122 }
3123 }
3124
3125 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003126 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3127 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3128 return RadioAccessFamily.RAF_UNKNOWN;
3129 }
3130
Wink Saville5d475dd2014-10-17 15:00:58 -07003131 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3132 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003133
3134 @Override
3135 public void enableVideoCalling(boolean enable) {
3136 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003137 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003138 }
3139
3140 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003141 public boolean isVideoCallingEnabled(String callingPackage) {
3142 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3143 return false;
3144 }
3145
Andrew Lee77527ac2014-10-21 16:57:39 -07003146 // Check the user preference and the system-level IMS setting. Even if the user has
3147 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3148 // In the long run, we may instead need to check if there exists a connection service
3149 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003150 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3151 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003152 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003153 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003154
Andrew Leea1239f22015-03-02 17:44:07 -08003155 @Override
3156 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003157 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003158 }
3159
3160 @Override
3161 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003162 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003163 }
3164
Andrew Lee9431b832015-03-09 18:46:45 -07003165 @Override
3166 public boolean isTtyModeSupported() {
3167 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3168 TelephonyManager telephonyManager =
3169 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003170 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003171 }
3172
3173 @Override
3174 public boolean isHearingAidCompatibilitySupported() {
3175 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3176 }
3177
Sanket Padawe7310cc72015-01-14 09:53:20 -08003178 /**
3179 * Returns the unique device ID of phone, for example, the IMEI for
3180 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3181 *
3182 * <p>Requires Permission:
3183 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3184 */
3185 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003186 public String getDeviceId(String callingPackage) {
3187 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3188 return null;
3189 }
3190
Sanket Padawe7310cc72015-01-14 09:53:20 -08003191 final Phone phone = PhoneFactory.getPhone(0);
3192 if (phone != null) {
3193 return phone.getDeviceId();
3194 } else {
3195 return null;
3196 }
3197 }
3198
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003199 /*
3200 * {@hide}
3201 * Returns the IMS Registration Status
3202 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003203 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003204 public boolean isImsRegistered() {
3205 return mPhone.isImsRegistered();
3206 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003207
3208 @Override
3209 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3210 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3211 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003212
Nathan Haroldc55097a2015-03-11 18:14:50 -07003213 /*
3214 * {@hide}
3215 * Returns the IMS Registration Status
3216 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003217 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003218 return mPhone.isWifiCallingEnabled();
3219 }
3220
3221 /*
3222 * {@hide}
3223 * Returns the IMS Registration Status
3224 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003225 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003226 return mPhone.isVolteEnabled();
3227 }
Svet Ganovb320e182015-04-16 12:30:10 -07003228
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003229 /*
3230 * {@hide} Returns the IMS Registration Status
3231 */
3232 public boolean isVideoTelephonyAvailable() {
3233 return mPhone.isVideoEnabled();
3234 }
3235
Svet Ganovb320e182015-04-16 12:30:10 -07003236 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003237 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003238 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003239 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3240
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003241 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003242 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003243 } catch (SecurityException e) {
3244 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3245 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003246 }
Svet Ganovb320e182015-04-16 12:30:10 -07003247
3248 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3249 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3250 return false;
3251 }
3252
3253 return true;
3254 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003255
Makoto Onukifee69342015-06-29 14:44:50 -07003256 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003257 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003258 */
3259 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003260 // Default SMS app can always read it.
3261 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3262 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3263 return true;
3264 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003265
Makoto Onukie4072d12015-08-03 15:12:23 -07003266 try {
3267 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003268 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003269 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003270 // Can be read with READ_SMS too.
3271 try {
3272 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3273 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3274 if (opCode != AppOpsManager.OP_NONE) {
3275 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3276 == AppOpsManager.MODE_ALLOWED;
3277 } else {
3278 return true;
3279 }
3280 } catch (SecurityException readSmsSecurityException) {
3281 }
Chad Brubakerf342a982017-03-30 16:27:34 -07003282 // Can be read with READ_PHONE_NUMBERS too.
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003283 try {
Chad Brubakerf342a982017-03-30 16:27:34 -07003284 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBERS,
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003285 message);
Chad Brubakerf342a982017-03-30 16:27:34 -07003286 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBERS);
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003287 if (opCode != AppOpsManager.OP_NONE) {
3288 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3289 == AppOpsManager.MODE_ALLOWED;
3290 } else {
3291 return true;
3292 }
3293 } catch (SecurityException readPhoneNumberSecurityException) {
3294 }
3295
3296 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3297 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3298 ", " + android.Manifest.permission.READ_SMS + ", or " +
Chad Brubakerf342a982017-03-30 16:27:34 -07003299 android.Manifest.permission.READ_PHONE_NUMBERS);
Makoto Onukifee69342015-06-29 14:44:50 -07003300 }
3301
Stuart Scott8eef64f2015-04-08 15:13:54 -07003302 @Override
3303 public void factoryReset(int subId) {
3304 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003305 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3306 return;
3307 }
3308
Svet Ganovcc087f82015-05-12 20:35:54 -07003309 final long identity = Binder.clearCallingIdentity();
3310 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003311 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3312 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003313 // Enable data
3314 setDataEnabled(subId, true);
3315 // Set network selection mode to automatic
3316 setNetworkSelectionModeAutomatic(subId);
3317 // Set preferred mobile network type to the best available
3318 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3319 // Turn off roaming
3320 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3321 }
3322 } finally {
3323 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003324 }
3325 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003326
3327 @Override
3328 public String getLocaleFromDefaultSim() {
3329 // We query all subscriptions instead of just the active ones, because
3330 // this might be called early on in the provisioning flow when the
3331 // subscriptions potentially aren't active yet.
3332 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3333 if (slist == null || slist.isEmpty()) {
3334 return null;
3335 }
3336
3337 // This function may be called very early, say, from the setup wizard, at
3338 // which point we won't have a default subscription set. If that's the case
3339 // we just choose the first, which will be valid in "most cases".
3340 final int defaultSubId = getDefaultSubscription();
3341 SubscriptionInfo info = null;
3342 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3343 info = slist.get(0);
3344 } else {
3345 for (SubscriptionInfo item : slist) {
3346 if (item.getSubscriptionId() == defaultSubId) {
3347 info = item;
3348 break;
3349 }
3350 }
3351
3352 if (info == null) {
3353 return null;
3354 }
3355 }
3356
3357 // Try and fetch the locale from the carrier properties or from the SIM language
3358 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003359 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003360 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003361 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003362 if (defaultPhone != null) {
3363 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3364 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003365 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003366 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3367 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003368 } else {
3369 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003370 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003371 }
3372 }
3373
Narayan Kamath011676f2015-07-29 12:04:08 +01003374 // The SIM language preferences only store a language (e.g. fr = French), not an
3375 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3376 // the SIM and carrier preferences does not include a country we add the country
3377 // determined from the SIM MCC to provide an exact locale.
3378 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003379 if (mccLocale != null) {
3380 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3381 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003382 }
3383
Tony Hill183b2de2015-06-24 14:53:58 +01003384 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003385 return null;
3386 }
3387
3388 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3389 final long identity = Binder.clearCallingIdentity();
3390 try {
3391 return mSubscriptionController.getAllSubInfoList(
3392 mPhone.getContext().getOpPackageName());
3393 } finally {
3394 Binder.restoreCallingIdentity(identity);
3395 }
3396 }
3397
3398 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3399 final long identity = Binder.clearCallingIdentity();
3400 try {
3401 return mSubscriptionController.getActiveSubscriptionInfoList(
3402 mPhone.getContext().getOpPackageName());
3403 } finally {
3404 Binder.restoreCallingIdentity(identity);
3405 }
3406 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003407
3408 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003409 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3410 * representing the state of the modem.
3411 *
3412 * NOTE: This clears the modem state, so there should only every be one caller.
3413 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003414 */
3415 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003416 public void requestModemActivityInfo(ResultReceiver result) {
3417 enforceModifyPermission();
3418
3419 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3420 Bundle bundle = new Bundle();
3421 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3422 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003423 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003424
3425 /**
3426 * {@hide}
3427 * Returns the service state information on specified subscription.
3428 */
3429 @Override
3430 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3431
3432 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3433 return null;
3434 }
3435
3436 final Phone phone = getPhone(subId);
3437 if (phone == null) {
3438 return null;
3439 }
3440
3441 return phone.getServiceState();
3442 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003443
3444 /**
3445 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3446 *
3447 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3448 * voicemail ringtone.
3449 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3450 * PhoneAccount.
3451 */
3452 @Override
3453 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003454 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003455 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003456 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003457 }
3458
3459 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3460 }
3461
3462 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003463 * Sets the per-account voicemail ringtone.
3464 *
3465 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3466 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3467 *
3468 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3469 * voicemail ringtone.
3470 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3471 * PhoneAccount.
3472 */
3473 @Override
3474 public void setVoicemailRingtoneUri(String callingPackage,
3475 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3476 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3477 if (!TextUtils.equals(callingPackage,
3478 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3479 enforceModifyPermissionOrCarrierPrivilege(
3480 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3481 }
3482 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3483 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003484 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003485 }
3486 VoicemailNotificationSettingsUtil.setRingtoneUri(phone, uri);
3487 }
3488
3489 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003490 * Returns whether vibration is set for voicemail notification in Phone settings.
3491 *
3492 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3493 * voicemail vibration setting.
3494 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3495 */
3496 @Override
3497 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003498 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003499 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003500 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003501 }
3502
3503 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3504 }
3505
Youhan Wange64578a2016-05-02 15:32:42 -07003506 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003507 * Sets the per-account voicemail vibration.
3508 *
3509 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3510 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3511 *
3512 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3513 * voicemail vibration setting.
3514 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3515 * specific PhoneAccount.
3516 */
3517 @Override
3518 public void setVoicemailVibrationEnabled(String callingPackage,
3519 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3520 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3521 if (!TextUtils.equals(callingPackage,
3522 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3523 enforceModifyPermissionOrCarrierPrivilege(
3524 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3525 }
3526
3527 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3528 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003529 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003530 }
3531 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone, enabled);
3532 }
3533
3534 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003535 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3536 *
3537 * @throws SecurityException if the caller does not have the required permission
3538 */
3539 private void enforceReadPrivilegedPermission() {
3540 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3541 null);
3542 }
3543
3544 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003545 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3546 * permission.
3547 *
3548 * @throws SecurityException if the caller does not have the required permission
3549 */
3550 private void enforceSendSmsPermission() {
3551 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3552 }
3553
3554 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003555 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003556 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003557 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003558 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003559 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Ta-wei Yen5d81b0c2017-03-03 16:32:26 -08003560 ComponentName componentName =
3561 RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
3562 if(componentName == null) {
3563 throw new SecurityException("Caller not current active visual voicemail package[null]");
3564 }
3565 String vvmPackage = componentName.getPackageName();
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003566 if (!callingPackage.equals(vvmPackage)) {
3567 throw new SecurityException("Caller not current active visual voicemail package[" +
3568 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003569 }
3570 }
3571
3572 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003573 * Return the application ID for the app type.
3574 *
3575 * @param subId the subscription ID that this request applies to.
3576 * @param appType the uicc app type.
3577 * @return Application ID for specificied app type, or null if no uicc.
3578 */
3579 @Override
3580 public String getAidForAppType(int subId, int appType) {
3581 enforceReadPrivilegedPermission();
3582 Phone phone = getPhone(subId);
3583 if (phone == null) {
3584 return null;
3585 }
3586 String aid = null;
3587 try {
3588 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3589 .getApplicationByType(appType).getAid();
3590 } catch (Exception e) {
3591 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3592 }
3593 return aid;
3594 }
3595
Youhan Wang4001d252016-05-11 10:29:41 -07003596 /**
3597 * Return the Electronic Serial Number.
3598 *
3599 * @param subId the subscription ID that this request applies to.
3600 * @return ESN or null if error.
3601 */
3602 @Override
3603 public String getEsn(int subId) {
3604 enforceReadPrivilegedPermission();
3605 Phone phone = getPhone(subId);
3606 if (phone == null) {
3607 return null;
3608 }
3609 String esn = null;
3610 try {
3611 esn = phone.getEsn();
3612 } catch (Exception e) {
3613 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3614 }
3615 return esn;
3616 }
3617
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003618 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003619 * Return the Preferred Roaming List Version.
3620 *
3621 * @param subId the subscription ID that this request applies to.
3622 * @return PRLVersion or null if error.
3623 */
3624 @Override
3625 public String getCdmaPrlVersion(int subId) {
3626 enforceReadPrivilegedPermission();
3627 Phone phone = getPhone(subId);
3628 if (phone == null) {
3629 return null;
3630 }
3631 String cdmaPrlVersion = null;
3632 try {
3633 cdmaPrlVersion = phone.getCdmaPrlVersion();
3634 } catch (Exception e) {
3635 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3636 }
3637 return cdmaPrlVersion;
3638 }
3639
3640 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003641 * Get snapshot of Telephony histograms
3642 * @return List of Telephony histograms
3643 * @hide
3644 */
3645 @Override
3646 public List<TelephonyHistogram> getTelephonyHistograms() {
3647 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3648 return RIL.getTelephonyRILTimingHistograms();
3649 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003650
3651 /**
3652 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003653 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003654 * Require system privileges. In the future we may add this to carrier APIs.
3655 *
3656 * @return The number of carriers set successfully, should match length of carriers
3657 */
3658 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003659 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003660 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003661
Meng Wang9b7c4e92017-02-17 11:41:27 -08003662 if (carriers == null) {
3663 throw new NullPointerException("carriers cannot be null");
3664 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003665
3666 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003667 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3668 return retVal[0];
3669 }
3670
3671 /**
3672 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003673 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003674 * Require system privileges. In the future we may add this to carrier APIs.
3675 *
3676 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3677 * means all carriers are allowed.
3678 */
3679 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003680 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003681 enforceReadPrivilegedPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003682 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003683 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3684 }
3685
fionaxu59545b42016-05-25 15:53:37 -07003686 /**
3687 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3688 * @param subId the subscription ID that this action applies to.
3689 * @param enabled control enable or disable metered apns.
3690 * {@hide}
3691 */
3692 @Override
3693 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3694 enforceModifyPermission();
3695 final Phone phone = getPhone(subId);
3696 if (phone == null) {
3697 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3698 return;
3699 }
3700 try {
3701 phone.carrierActionSetMeteredApnsEnabled(enabled);
3702 } catch (Exception e) {
3703 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3704 }
3705 }
3706
3707 /**
3708 * Action set from carrier signalling broadcast receivers to enable/disable radio
3709 * @param subId the subscription ID that this action applies to.
3710 * @param enabled control enable or disable radio.
3711 * {@hide}
3712 */
3713 @Override
3714 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3715 enforceModifyPermission();
3716 final Phone phone = getPhone(subId);
3717 if (phone == null) {
3718 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3719 return;
3720 }
3721 try {
3722 phone.carrierActionSetRadioEnabled(enabled);
3723 } catch (Exception e) {
3724 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3725 }
3726 }
3727
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003728 /**
3729 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3730 * bug report is being generated.
3731 */
3732 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003733 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003734 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3735 != PackageManager.PERMISSION_GRANTED) {
3736 writer.println("Permission Denial: can't dump Phone from pid="
3737 + Binder.getCallingPid()
3738 + ", uid=" + Binder.getCallingUid()
3739 + "without permission "
3740 + android.Manifest.permission.DUMP);
3741 return;
3742 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003743 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003744 }
Jack Yueb89b242016-06-22 13:27:47 -07003745
3746 /**
3747 * Get aggregated video call data usage from all subscriptions since boot.
3748 * @return total data usage in bytes
3749 * {@hide}
3750 */
3751 @Override
3752 public long getVtDataUsage() {
3753 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3754 null);
3755
3756 // NetworkStatsService keeps tracking the active network interface and identity. It will
3757 // record the delta with the corresponding network identity. What we need to do here is
3758 // returning total video call data usage from all subscriptions since boot.
3759
3760 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3761 // simultaneous VT calls.
3762 final Phone[] phones = PhoneFactory.getPhones();
3763 long total = 0;
3764 for (Phone phone : phones) {
3765 total += phone.getVtDataUsage();
3766 }
3767 return total;
3768 }
Jack Yu75ab2952016-07-08 14:29:33 -07003769
3770 /**
3771 * Policy control of data connection. Usually used when data limit is passed.
3772 * @param enabled True if enabling the data, otherwise disabling.
3773 * @param subId Subscription index
3774 * {@hide}
3775 */
3776 @Override
3777 public void setPolicyDataEnabled(boolean enabled, int subId) {
3778 enforceModifyPermission();
3779 Phone phone = getPhone(subId);
3780 if (phone != null) {
3781 phone.setPolicyDataEnabled(enabled);
3782 }
3783 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003784
3785 /**
3786 * Get Client request stats
3787 * @return List of Client Request Stats
3788 * @hide
3789 */
3790 @Override
3791 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3792 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3793 return null;
3794 }
3795
3796 Phone phone = getPhone(subId);
3797 if (phone != null) {
3798 return phone.getClientRequestStats();
3799 }
3800
3801 return null;
3802 }
3803
3804 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3805 if (workSource != null) {
3806 return workSource;
3807 }
3808
3809 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3810 workSource = new WorkSource(uid, packageName);
3811 return workSource;
3812 }
Jack Yueb4124c2017-02-16 15:32:43 -08003813
3814 /**
3815 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3816 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003817 * @param slotIndex SIM slot id.
Jack Yueb4124c2017-02-16 15:32:43 -08003818 * @param powerUp True if powering up the SIM, otherwise powering down
3819 *
3820 **/
3821 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003822 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yueb4124c2017-02-16 15:32:43 -08003823 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003824 Phone phone = PhoneFactory.getPhone(slotIndex);
3825
Jack Yueb4124c2017-02-16 15:32:43 -08003826 if (phone != null) {
3827 phone.setSimPowerState(powerUp);
3828 }
3829 }
Shuo Qiandd210312017-04-12 22:11:33 +00003830
Tyler Gunn65d45c22017-06-05 11:22:26 -07003831 private boolean isUssdApiAllowed(int subId) {
3832 CarrierConfigManager configManager =
3833 (CarrierConfigManager) mPhone.getContext().getSystemService(
3834 Context.CARRIER_CONFIG_SERVICE);
3835 if (configManager == null) {
3836 return false;
3837 }
3838 PersistableBundle pb = configManager.getConfigForSubId(subId);
3839 if (pb == null) {
3840 return false;
3841 }
3842 return pb.getBoolean(
3843 CarrierConfigManager.KEY_ALLOW_USSD_REQUESTS_VIA_TELEPHONY_MANAGER_BOOL);
3844 }
3845
Shuo Qiandd210312017-04-12 22:11:33 +00003846 /**
3847 * Check if phone is in emergency callback mode
3848 * @return true if phone is in emergency callback mode
3849 * @param subId sub id
3850 */
3851 public boolean getEmergencyCallbackMode(int subId) {
3852 final Phone phone = getPhone(subId);
3853 if (phone != null) {
3854 return phone.isInEcm();
3855 } else {
3856 return false;
3857 }
3858 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003859}