blob: 4e41b1a7a7b0144ba8ab2ba74ad4f63f528a3bd3 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Ta-wei Yen1b455992016-12-27 14:52:32 -080021import android.Manifest.permission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.ActivityManager;
23import android.app.AppOpsManager;
Ta-wei Yen1b455992016-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;
Jack Yuc83dc1c2017-05-26 16:07:50 -070031import android.net.NetworkStats;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.net.Uri;
33import android.os.AsyncResult;
34import android.os.Binder;
35import android.os.Bundle;
36import android.os.Handler;
yinxu34eed212017-04-12 16:03:22 -070037import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Looper;
39import android.os.Message;
yinxu34eed212017-04-12 16:03:22 -070040import android.os.Messenger;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070042import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.os.ServiceManager;
44import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070045import android.os.UserManager;
Sooraj Sasindran22882212016-07-18 11:57:25 -070046import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070047import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080048import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070049import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080050import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080051import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070052import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070053import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.telephony.CellInfo;
Sooraj Sasindran22882212016-07-18 11:57:25 -070055import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070056import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070057import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080058import android.telephony.NeighboringCellInfo;
yinxu34eed212017-04-12 16:03:22 -070059import android.telephony.NetworkScanRequest;
Wink Saville5d475dd2014-10-17 15:00:58 -070060import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.telephony.ServiceState;
Nathan Harold46b42aa2017-03-10 19:38:22 -080062import android.telephony.SignalStrength;
Jack Yuc83dc1c2017-05-26 16:07:50 -070063import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080064import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080065import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070066import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070067import android.telephony.TelephonyManager;
68import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080070import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080072import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080073import android.util.Slog;
Ta-wei Yen1b455992016-12-27 14:52:32 -080074
Andrew Lee312e8172014-10-23 17:01:36 -070075import com.android.ims.ImsManager;
Brad Ebinger76681002017-01-23 13:50:20 -080076import com.android.ims.internal.IImsServiceController;
77import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070078import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070079import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070080import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080083import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010084import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070085import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086import com.android.internal.telephony.Phone;
Nathan Harold8f68faa2016-12-14 11:27:20 -080087import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070088import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070089import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070090import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070091import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070092import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080093import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070094import com.android.internal.telephony.uicc.IccIoResult;
95import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080096import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070097import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080098import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070099import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -0800100import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -0700101import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -0800102import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yen463efd12017-01-06 15:29:25 -0800103import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yen1b455992016-12-27 14:52:32 -0800104
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700105import java.io.FileDescriptor;
106import java.io.PrintWriter;
Ta-wei Yen1b455992016-12-27 14:52:32 -0800107import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800109import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800110import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100111import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800112import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113
114/**
115 * Implementation of the ITelephony interface.
116 */
Santos Cordon117fee72014-05-16 17:56:12 -0700117public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118 private static final String LOG_TAG = "PhoneInterfaceManager";
119 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
120 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800121 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700122
123 // Message codes used with mMainThreadHandler
124 private static final int CMD_HANDLE_PIN_MMI = 1;
125 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
126 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
127 private static final int CMD_ANSWER_RINGING_CALL = 4;
128 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700129 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
130 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700131 private static final int CMD_OPEN_CHANNEL = 9;
132 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
133 private static final int CMD_CLOSE_CHANNEL = 11;
134 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800135 private static final int CMD_NV_READ_ITEM = 13;
136 private static final int EVENT_NV_READ_ITEM_DONE = 14;
137 private static final int CMD_NV_WRITE_ITEM = 15;
138 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
139 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
140 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
141 private static final int CMD_NV_RESET_CONFIG = 19;
142 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800143 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
144 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
145 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
146 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800147 private static final int CMD_SEND_ENVELOPE = 25;
148 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700149 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
150 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
151 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
152 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
153 private static final int CMD_EXCHANGE_SIM_IO = 31;
154 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800155 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
156 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700157 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
158 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700159 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
160 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700161 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
162 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
163 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
164 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700165 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
166 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
167 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
168 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar7e856482016-10-14 19:37:38 -0700169 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800170 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
171 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172
173 /** The singleton instance. */
174 private static PhoneInterfaceManager sInstance;
175
Wink Saville3ab207e2014-11-20 13:07:20 -0800176 private PhoneGlobals mApp;
177 private Phone mPhone;
178 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700179 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800180 private AppOpsManager mAppOps;
181 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800182 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800183 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184
Derek Tan97ebb422014-09-05 16:55:38 -0700185 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
186 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800187 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700188
189 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700190 * A request object to use for transmitting data to an ICC.
191 */
192 private static final class IccAPDUArgument {
193 public int channel, cla, command, p1, p2, p3;
194 public String data;
195
196 public IccAPDUArgument(int channel, int cla, int command,
197 int p1, int p2, int p3, String data) {
198 this.channel = channel;
199 this.cla = cla;
200 this.command = command;
201 this.p1 = p1;
202 this.p2 = p2;
203 this.p3 = p3;
204 this.data = data;
205 }
206 }
207
208 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700209 * A request object to use for transmitting data to an ICC.
210 */
211 private static final class ManualNetworkSelectionArgument {
212 public OperatorInfo operatorInfo;
213 public boolean persistSelection;
214
215 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
216 this.operatorInfo = operatorInfo;
217 this.persistSelection = persistSelection;
218 }
219 }
220
221 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
223 * request after sending. The main thread will notify the request when it is complete.
224 */
225 private static final class MainThreadRequest {
226 /** The argument to use for the request */
227 public Object argument;
228 /** The result of the request that is run on the main thread */
229 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800230 // The subscriber id that this request applies to. Defaults to
231 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
232 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233
234 public MainThreadRequest(Object argument) {
235 this.argument = argument;
236 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800237
238 public MainThreadRequest(Object argument, Integer subId) {
239 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800240 if (subId != null) {
241 this.subId = subId;
242 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800243 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 }
245
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800246 private static final class IncomingThirdPartyCallArgs {
247 public final ComponentName component;
248 public final String callId;
249 public final String callerDisplayName;
250
251 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
252 String callerDisplayName) {
253 this.component = component;
254 this.callId = callId;
255 this.callerDisplayName = callerDisplayName;
256 }
257 }
258
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 /**
260 * A handler that processes messages on the main thread in the phone process. Since many
261 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
262 * inbound binder threads to the main thread in the phone process. The Binder thread
263 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
264 * on, which will be notified when the operation completes and will contain the result of the
265 * request.
266 *
267 * <p>If a MainThreadRequest object is provided in the msg.obj field,
268 * note that request.result must be set to something non-null for the calling thread to
269 * unblock.
270 */
271 private final class MainThreadHandler extends Handler {
272 @Override
273 public void handleMessage(Message msg) {
274 MainThreadRequest request;
275 Message onCompleted;
276 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800277 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700278 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279
280 switch (msg.what) {
pkanwar7e856482016-10-14 19:37:38 -0700281 case CMD_HANDLE_USSD_REQUEST: {
282 request = (MainThreadRequest) msg.obj;
283 final Phone phone = getPhoneFromRequest(request);
284 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
285 String ussdRequest = ussdObject.first;
286 ResultReceiver wrappedCallback = ussdObject.second;
287 request.result = phone != null ?
288 phone.handleUssdRequest(ussdRequest, wrappedCallback)
289 :false;
290 // Wake up the requesting thread
291 synchronized (request) {
292 request.notifyAll();
293 }
294 break;
295 }
296
Yorke Lee716f67e2015-06-17 15:39:16 -0700297 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700299 final Phone phone = getPhoneFromRequest(request);
300 request.result = phone != null ?
301 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
302 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303 // Wake up the requesting thread
304 synchronized (request) {
305 request.notifyAll();
306 }
307 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700308 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309
310 case CMD_HANDLE_NEIGHBORING_CELL:
311 request = (MainThreadRequest) msg.obj;
312 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
313 request);
Sooraj Sasindran22882212016-07-18 11:57:25 -0700314 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 break;
316
317 case EVENT_NEIGHBORING_CELL_DONE:
318 ar = (AsyncResult) msg.obj;
319 request = (MainThreadRequest) ar.userObj;
320 if (ar.exception == null && ar.result != null) {
321 request.result = ar.result;
322 } else {
323 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800324 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 }
326 // Wake up the requesting thread
327 synchronized (request) {
328 request.notifyAll();
329 }
330 break;
331
332 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700333 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800334 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700335 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 break;
337
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700338 case CMD_END_CALL:
339 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800340 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700341 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700342 Phone phone = getPhone(end_subId);
343 if (phone == null) {
344 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
345 break;
346 }
347 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
349 // CDMA: If the user presses the Power button we treat it as
350 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700351 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
353 // GSM: End the call as per the Phone state
354 hungUp = PhoneUtils.hangup(mCM);
355 } else {
356 throw new IllegalStateException("Unexpected phone type: " + phoneType);
357 }
358 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
359 request.result = hungUp;
360 // Wake up the requesting thread
361 synchronized (request) {
362 request.notifyAll();
363 }
364 break;
365
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700366 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700367 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700368 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800369 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700370 if (uiccCard == null) {
371 loge("iccTransmitApduLogicalChannel: No UICC");
372 request.result = new IccIoResult(0x6F, 0, (byte[])null);
373 synchronized (request) {
374 request.notifyAll();
375 }
376 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700377 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
378 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700379 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700380 iccArgument.channel, iccArgument.cla, iccArgument.command,
381 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700382 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700383 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700384 break;
385
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700386 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700387 ar = (AsyncResult) msg.obj;
388 request = (MainThreadRequest) ar.userObj;
389 if (ar.exception == null && ar.result != null) {
390 request.result = ar.result;
391 } else {
392 request.result = new IccIoResult(0x6F, 0, (byte[])null);
393 if (ar.result == null) {
394 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800395 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700396 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800397 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700398 } else {
399 loge("iccTransmitApduLogicalChannel: Unknown exception");
400 }
401 }
402 synchronized (request) {
403 request.notifyAll();
404 }
405 break;
406
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700407 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
408 request = (MainThreadRequest) msg.obj;
409 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800410 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700411 if (uiccCard == null) {
412 loge("iccTransmitApduBasicChannel: No UICC");
413 request.result = new IccIoResult(0x6F, 0, (byte[])null);
414 synchronized (request) {
415 request.notifyAll();
416 }
417 } else {
418 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
419 request);
420 uiccCard.iccTransmitApduBasicChannel(
421 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
422 iccArgument.p3, iccArgument.data, onCompleted);
423 }
424 break;
425
426 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
427 ar = (AsyncResult) msg.obj;
428 request = (MainThreadRequest) ar.userObj;
429 if (ar.exception == null && ar.result != null) {
430 request.result = ar.result;
431 } else {
432 request.result = new IccIoResult(0x6F, 0, (byte[])null);
433 if (ar.result == null) {
434 loge("iccTransmitApduBasicChannel: Empty response");
435 } else if (ar.exception instanceof CommandException) {
436 loge("iccTransmitApduBasicChannel: CommandException: " +
437 ar.exception);
438 } else {
439 loge("iccTransmitApduBasicChannel: Unknown exception");
440 }
441 }
442 synchronized (request) {
443 request.notifyAll();
444 }
445 break;
446
447 case CMD_EXCHANGE_SIM_IO:
448 request = (MainThreadRequest) msg.obj;
449 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800450 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700451 if (uiccCard == null) {
452 loge("iccExchangeSimIO: No UICC");
453 request.result = new IccIoResult(0x6F, 0, (byte[])null);
454 synchronized (request) {
455 request.notifyAll();
456 }
457 } else {
458 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
459 request);
460 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
461 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
462 iccArgument.data, onCompleted);
463 }
464 break;
465
466 case EVENT_EXCHANGE_SIM_IO_DONE:
467 ar = (AsyncResult) msg.obj;
468 request = (MainThreadRequest) ar.userObj;
469 if (ar.exception == null && ar.result != null) {
470 request.result = ar.result;
471 } else {
472 request.result = new IccIoResult(0x6f, 0, (byte[])null);
473 }
474 synchronized (request) {
475 request.notifyAll();
476 }
477 break;
478
Derek Tan4d5e5c12014-02-04 11:54:58 -0800479 case CMD_SEND_ENVELOPE:
480 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800481 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700482 if (uiccCard == null) {
483 loge("sendEnvelopeWithStatus: No UICC");
484 request.result = new IccIoResult(0x6F, 0, (byte[])null);
485 synchronized (request) {
486 request.notifyAll();
487 }
488 } else {
489 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
490 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
491 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800492 break;
493
494 case EVENT_SEND_ENVELOPE_DONE:
495 ar = (AsyncResult) msg.obj;
496 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700497 if (ar.exception == null && ar.result != null) {
498 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800499 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700500 request.result = new IccIoResult(0x6F, 0, (byte[])null);
501 if (ar.result == null) {
502 loge("sendEnvelopeWithStatus: Empty response");
503 } else if (ar.exception instanceof CommandException) {
504 loge("sendEnvelopeWithStatus: CommandException: " +
505 ar.exception);
506 } else {
507 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
508 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800509 }
510 synchronized (request) {
511 request.notifyAll();
512 }
513 break;
514
Shishir Agrawal566b7612013-10-28 14:41:00 -0700515 case CMD_OPEN_CHANNEL:
516 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800517 uiccCard = getUiccCardFromRequest(request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800518 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700519 if (uiccCard == null) {
520 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800521 request.result = new IccOpenLogicalChannelResponse(-1,
522 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700523 synchronized (request) {
524 request.notifyAll();
525 }
526 } else {
527 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800528 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
529 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700530 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700531 break;
532
533 case EVENT_OPEN_CHANNEL_DONE:
534 ar = (AsyncResult) msg.obj;
535 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700536 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700537 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700538 int[] result = (int[]) ar.result;
539 int channelId = result[0];
540 byte[] selectResponse = null;
541 if (result.length > 1) {
542 selectResponse = new byte[result.length - 1];
543 for (int i = 1; i < result.length; ++i) {
544 selectResponse[i - 1] = (byte) result[i];
545 }
546 }
547 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700548 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700549 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700550 if (ar.result == null) {
551 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700552 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700553 if (ar.exception != null) {
554 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
555 }
556
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700557 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700558 if (ar.exception instanceof CommandException) {
559 CommandException.Error error =
560 ((CommandException) (ar.exception)).getCommandError();
561 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700562 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700563 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700564 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700565 }
566 }
567 openChannelResp = new IccOpenLogicalChannelResponse(
568 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700569 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700570 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700571 synchronized (request) {
572 request.notifyAll();
573 }
574 break;
575
576 case CMD_CLOSE_CHANNEL:
577 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800578 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700579 if (uiccCard == null) {
580 loge("iccCloseLogicalChannel: No UICC");
Yoshiaki Naka2e29d822016-09-02 19:27:39 +0900581 request.result = false;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700582 synchronized (request) {
583 request.notifyAll();
584 }
585 } else {
586 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
587 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
588 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700589 break;
590
591 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800592 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
593 break;
594
595 case CMD_NV_READ_ITEM:
596 request = (MainThreadRequest) msg.obj;
597 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
598 mPhone.nvReadItem((Integer) request.argument, onCompleted);
599 break;
600
601 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700602 ar = (AsyncResult) msg.obj;
603 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800604 if (ar.exception == null && ar.result != null) {
605 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700606 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800607 request.result = "";
608 if (ar.result == null) {
609 loge("nvReadItem: Empty response");
610 } else if (ar.exception instanceof CommandException) {
611 loge("nvReadItem: CommandException: " +
612 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700613 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800614 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700615 }
616 }
617 synchronized (request) {
618 request.notifyAll();
619 }
620 break;
621
Jake Hambye994d462014-02-03 13:10:13 -0800622 case CMD_NV_WRITE_ITEM:
623 request = (MainThreadRequest) msg.obj;
624 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
625 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
626 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
627 break;
628
629 case EVENT_NV_WRITE_ITEM_DONE:
630 handleNullReturnEvent(msg, "nvWriteItem");
631 break;
632
633 case CMD_NV_WRITE_CDMA_PRL:
634 request = (MainThreadRequest) msg.obj;
635 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
636 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
637 break;
638
639 case EVENT_NV_WRITE_CDMA_PRL_DONE:
640 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
641 break;
642
643 case CMD_NV_RESET_CONFIG:
644 request = (MainThreadRequest) msg.obj;
645 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
646 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
647 break;
648
649 case EVENT_NV_RESET_CONFIG_DONE:
650 handleNullReturnEvent(msg, "nvResetConfig");
651 break;
652
Jake Hamby7c27be32014-03-03 13:25:59 -0800653 case CMD_GET_PREFERRED_NETWORK_TYPE:
654 request = (MainThreadRequest) msg.obj;
655 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700656 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800657 break;
658
659 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
660 ar = (AsyncResult) msg.obj;
661 request = (MainThreadRequest) ar.userObj;
662 if (ar.exception == null && ar.result != null) {
663 request.result = ar.result; // Integer
664 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800665 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800666 if (ar.result == null) {
667 loge("getPreferredNetworkType: Empty response");
668 } else if (ar.exception instanceof CommandException) {
669 loge("getPreferredNetworkType: CommandException: " +
670 ar.exception);
671 } else {
672 loge("getPreferredNetworkType: Unknown exception");
673 }
674 }
675 synchronized (request) {
676 request.notifyAll();
677 }
678 break;
679
680 case CMD_SET_PREFERRED_NETWORK_TYPE:
681 request = (MainThreadRequest) msg.obj;
682 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
683 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700684 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800685 break;
686
687 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
688 handleNullReturnEvent(msg, "setPreferredNetworkType");
689 break;
690
Steven Liu4bf01bc2014-07-17 11:05:29 -0500691 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
692 request = (MainThreadRequest)msg.obj;
693 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
694 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
695 break;
696
697 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
698 ar = (AsyncResult)msg.obj;
699 request = (MainThreadRequest)ar.userObj;
700 request.result = ar;
701 synchronized (request) {
702 request.notifyAll();
703 }
704 break;
705
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800706 case CMD_SET_VOICEMAIL_NUMBER:
707 request = (MainThreadRequest) msg.obj;
708 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
709 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800710 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
711 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800712 break;
713
714 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
715 handleNullReturnEvent(msg, "setVoicemailNumber");
716 break;
717
Stuart Scott54788802015-03-30 13:18:01 -0700718 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
719 request = (MainThreadRequest) msg.obj;
720 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
721 request);
722 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
723 break;
724
725 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
726 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
727 break;
728
Shishir Agrawal302c8692015-06-19 13:49:39 -0700729 case CMD_PERFORM_NETWORK_SCAN:
730 request = (MainThreadRequest) msg.obj;
731 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
732 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
733 break;
734
735 case EVENT_PERFORM_NETWORK_SCAN_DONE:
736 ar = (AsyncResult) msg.obj;
737 request = (MainThreadRequest) ar.userObj;
738 CellNetworkScanResult cellScanResult;
739 if (ar.exception == null && ar.result != null) {
740 cellScanResult = new CellNetworkScanResult(
741 CellNetworkScanResult.STATUS_SUCCESS,
742 (List<OperatorInfo>) ar.result);
743 } else {
744 if (ar.result == null) {
745 loge("getCellNetworkScanResults: Empty response");
746 }
747 if (ar.exception != null) {
748 loge("getCellNetworkScanResults: Exception: " + ar.exception);
749 }
750 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
751 if (ar.exception instanceof CommandException) {
752 CommandException.Error error =
753 ((CommandException) (ar.exception)).getCommandError();
754 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
755 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
756 } else if (error == CommandException.Error.GENERIC_FAILURE) {
757 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
758 }
759 }
760 cellScanResult = new CellNetworkScanResult(errorCode, null);
761 }
762 request.result = cellScanResult;
763 synchronized (request) {
764 request.notifyAll();
765 }
766 break;
767
768 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
769 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700770 ManualNetworkSelectionArgument selArg =
771 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700772 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
773 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700774 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
775 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700776 break;
777
778 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
779 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
780 break;
781
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700782 case CMD_GET_MODEM_ACTIVITY_INFO:
783 request = (MainThreadRequest) msg.obj;
784 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700785 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700786 break;
787
788 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
789 ar = (AsyncResult) msg.obj;
790 request = (MainThreadRequest) ar.userObj;
791 if (ar.exception == null && ar.result != null) {
792 request.result = ar.result;
793 } else {
794 if (ar.result == null) {
795 loge("queryModemActivityInfo: Empty response");
796 } else if (ar.exception instanceof CommandException) {
797 loge("queryModemActivityInfo: CommandException: " +
798 ar.exception);
799 } else {
800 loge("queryModemActivityInfo: Unknown exception");
801 }
802 }
Amit Mahajand4766222016-01-28 15:28:28 -0800803 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
804 if (request.result == null) {
805 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
806 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700807 synchronized (request) {
808 request.notifyAll();
809 }
810 break;
811
Meng Wang1a7c35a2016-05-05 20:56:15 -0700812 case CMD_SET_ALLOWED_CARRIERS:
813 request = (MainThreadRequest) msg.obj;
814 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
815 mPhone.setAllowedCarriers(
816 (List<CarrierIdentifier>) request.argument,
817 onCompleted);
818 break;
819
820 case EVENT_SET_ALLOWED_CARRIERS_DONE:
821 ar = (AsyncResult) msg.obj;
822 request = (MainThreadRequest) ar.userObj;
823 if (ar.exception == null && ar.result != null) {
824 request.result = ar.result;
825 } else {
826 if (ar.result == null) {
827 loge("setAllowedCarriers: Empty response");
828 } else if (ar.exception instanceof CommandException) {
829 loge("setAllowedCarriers: CommandException: " +
830 ar.exception);
831 } else {
832 loge("setAllowedCarriers: Unknown exception");
833 }
834 }
835 // Result cannot be null. Return -1 on error.
836 if (request.result == null) {
837 request.result = new int[]{-1};
838 }
839 synchronized (request) {
840 request.notifyAll();
841 }
842 break;
843
844 case CMD_GET_ALLOWED_CARRIERS:
845 request = (MainThreadRequest) msg.obj;
846 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
847 mPhone.getAllowedCarriers(onCompleted);
848 break;
849
850 case EVENT_GET_ALLOWED_CARRIERS_DONE:
851 ar = (AsyncResult) msg.obj;
852 request = (MainThreadRequest) ar.userObj;
853 if (ar.exception == null && ar.result != null) {
854 request.result = ar.result;
855 } else {
856 if (ar.result == null) {
857 loge("getAllowedCarriers: Empty response");
858 } else if (ar.exception instanceof CommandException) {
859 loge("getAllowedCarriers: CommandException: " +
860 ar.exception);
861 } else {
862 loge("getAllowedCarriers: Unknown exception");
863 }
864 }
865 // Result cannot be null. Return empty list of CarrierIdentifier.
866 if (request.result == null) {
867 request.result = new ArrayList<CarrierIdentifier>(0);
868 }
869 synchronized (request) {
870 request.notifyAll();
871 }
872 break;
873
Nathan Haroldb3014052017-01-25 15:57:32 -0800874 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
875 ar = (AsyncResult) msg.obj;
876 request = (MainThreadRequest) ar.userObj;
877 if (ar.exception == null && ar.result != null) {
878 request.result = ar.result;
879 } else {
880 request.result = new IllegalArgumentException(
881 "Failed to retrieve Forbidden Plmns");
882 if (ar.result == null) {
883 loge("getForbiddenPlmns: Empty response");
884 } else {
885 loge("getForbiddenPlmns: Unknown exception");
886 }
887 }
888 synchronized (request) {
889 request.notifyAll();
890 }
891 break;
892
893 case CMD_GET_FORBIDDEN_PLMNS:
894 request = (MainThreadRequest) msg.obj;
895 uiccCard = getUiccCardFromRequest(request);
896 if (uiccCard == null) {
897 loge("getForbiddenPlmns() UiccCard is null");
898 request.result = new IllegalArgumentException(
899 "getForbiddenPlmns() UiccCard is null");
900 synchronized (request) {
901 request.notifyAll();
902 }
903 break;
904 }
905 Integer appType = (Integer) request.argument;
906 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
907 if (uiccApp == null) {
908 loge("getForbiddenPlmns() no app with specified type -- "
909 + appType);
910 request.result = new IllegalArgumentException("Failed to get UICC App");
911 synchronized (request) {
912 request.notifyAll();
913 }
914 break;
915 } else {
916 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
917 + " specified type -- " + appType);
918 }
919 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
920 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
921 onCompleted);
922 break;
923
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 default:
925 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
926 break;
927 }
928 }
Jake Hambye994d462014-02-03 13:10:13 -0800929
930 private void handleNullReturnEvent(Message msg, String command) {
931 AsyncResult ar = (AsyncResult) msg.obj;
932 MainThreadRequest request = (MainThreadRequest) ar.userObj;
933 if (ar.exception == null) {
934 request.result = true;
935 } else {
936 request.result = false;
937 if (ar.exception instanceof CommandException) {
938 loge(command + ": CommandException: " + ar.exception);
939 } else {
940 loge(command + ": Unknown exception");
941 }
942 }
943 synchronized (request) {
944 request.notifyAll();
945 }
946 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700947 }
948
949 /**
950 * Posts the specified command to be executed on the main thread,
951 * waits for the request to complete, and returns the result.
952 * @see #sendRequestAsync
953 */
954 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800955 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700956 }
957
958 /**
959 * Posts the specified command to be executed on the main thread,
960 * waits for the request to complete, and returns the result.
961 * @see #sendRequestAsync
962 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800963 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
965 throw new RuntimeException("This method will deadlock if called from the main thread.");
966 }
967
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800968 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 Message msg = mMainThreadHandler.obtainMessage(command, request);
970 msg.sendToTarget();
971
972 // Wait for the request to complete
973 synchronized (request) {
974 while (request.result == null) {
975 try {
976 request.wait();
977 } catch (InterruptedException e) {
978 // Do nothing, go back and wait until the request is complete
979 }
980 }
981 }
982 return request.result;
983 }
984
985 /**
986 * Asynchronous ("fire and forget") version of sendRequest():
987 * Posts the specified command to be executed on the main thread, and
988 * returns immediately.
989 * @see #sendRequest
990 */
991 private void sendRequestAsync(int command) {
992 mMainThreadHandler.sendEmptyMessage(command);
993 }
994
995 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700996 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
997 * @see {@link #sendRequest(int,Object)}
998 */
999 private void sendRequestAsync(int command, Object argument) {
1000 MainThreadRequest request = new MainThreadRequest(argument);
1001 Message msg = mMainThreadHandler.obtainMessage(command, request);
1002 msg.sendToTarget();
1003 }
1004
1005 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001006 * Initialize the singleton PhoneInterfaceManager instance.
1007 * This is only done once, at startup, from PhoneApp.onCreate().
1008 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001009 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001010 synchronized (PhoneInterfaceManager.class) {
1011 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001012 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 } else {
1014 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1015 }
1016 return sInstance;
1017 }
1018 }
1019
1020 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001021 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 mApp = app;
1023 mPhone = phone;
1024 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001025 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1027 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001028 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001029 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001030 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001031
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 publish();
1033 }
1034
1035 private void publish() {
1036 if (DBG) log("publish: " + this);
1037
1038 ServiceManager.addService("phone", this);
1039 }
1040
Stuart Scott584921c2015-01-15 17:10:34 -08001041 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001042 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1043 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001044 }
1045
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001046 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1047 Phone phone = getPhoneFromRequest(request);
1048 return phone == null ? null :
1049 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1050 }
1051
Wink Saville36469e72014-06-11 15:17:00 -07001052 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001053 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001054 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001055 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001056 //
1057 // Implementation of the ITelephony interface.
1058 //
1059
1060 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001061 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001062 }
1063
Wink Savilleb564aae2014-10-23 10:18:09 -07001064 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001065 if (DBG) log("dial: " + number);
1066 // No permission check needed here: This is just a wrapper around the
1067 // ACTION_DIAL intent, which is available to any app since it puts up
1068 // the UI before it does anything.
1069
1070 String url = createTelUrl(number);
1071 if (url == null) {
1072 return;
1073 }
1074
1075 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001076 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001077 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1078 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1079 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1080 mApp.startActivity(intent);
1081 }
1082 }
1083
1084 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001085 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001086 }
1087
Wink Savilleb564aae2014-10-23 10:18:09 -07001088 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001089 if (DBG) log("call: " + number);
1090
1091 // This is just a wrapper around the ACTION_CALL intent, but we still
1092 // need to do a permission check since we're calling startActivity()
1093 // from the context of the phone app.
1094 enforceCallPermission();
1095
1096 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1097 != AppOpsManager.MODE_ALLOWED) {
1098 return;
1099 }
1100
1101 String url = createTelUrl(number);
1102 if (url == null) {
1103 return;
1104 }
1105
Wink Saville08874612014-08-31 19:19:58 -07001106 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001107 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001108 if (slist != null) {
1109 for (SubscriptionInfo subInfoRecord : slist) {
1110 if (subInfoRecord.getSubscriptionId() == subId) {
1111 isValid = true;
1112 break;
1113 }
Wink Saville08874612014-08-31 19:19:58 -07001114 }
1115 }
1116 if (isValid == false) {
1117 return;
1118 }
1119
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001120 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001121 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1123 mApp.startActivity(intent);
1124 }
1125
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 /**
1127 * End a call based on call state
1128 * @return true is a call was ended
1129 */
1130 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001131 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001132 }
1133
1134 /**
1135 * End a call based on the call state of the subId
1136 * @return true is a call was ended
1137 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001138 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001139 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001140 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001141 }
1142
1143 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001144 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001145 }
1146
Wink Savilleb564aae2014-10-23 10:18:09 -07001147 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 if (DBG) log("answerRingingCall...");
1149 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1150 // but that can probably wait till the big TelephonyManager API overhaul.
1151 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1152 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001153 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001154 }
1155
1156 /**
1157 * Make the actual telephony calls to implement answerRingingCall().
1158 * This should only be called from the main thread of the Phone app.
1159 * @see #answerRingingCall
1160 *
1161 * TODO: it would be nice to return true if we answered the call, or
1162 * false if there wasn't actually a ringing incoming call, or some
1163 * other error occurred. (In other words, pass back the return value
1164 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1165 * But that would require calling this method via sendRequest() rather
1166 * than sendRequestAsync(), and right now we don't actually *need* that
1167 * return value, so let's just return void for now.
1168 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001169 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001170 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001171 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001172 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1173 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001174 if (hasActiveCall && hasHoldingCall) {
1175 // Both lines are in use!
1176 // TODO: provide a flag to let the caller specify what
1177 // policy to use if both lines are in use. (The current
1178 // behavior is hardwired to "answer incoming, end ongoing",
1179 // which is how the CALL button is specced to behave.)
1180 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1181 return;
1182 } else {
1183 // answerCall() will automatically hold the current active
1184 // call, if there is one.
1185 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1186 return;
1187 }
1188 } else {
1189 // No call was ringing.
1190 return;
1191 }
1192 }
1193
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001195 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001197 public void silenceRinger() {
1198 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001199 }
1200
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001201 @Override
1202 public boolean isOffhook(String callingPackage) {
1203 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001204 }
1205
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001206 @Override
1207 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1208 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1209 return false;
1210 }
1211
Sanket Padawe356d7632015-06-22 14:03:32 -07001212 final Phone phone = getPhone(subId);
1213 if (phone != null) {
1214 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1215 } else {
1216 return false;
1217 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001218 }
1219
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001220 @Override
1221 public boolean isRinging(String callingPackage) {
1222 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001223 }
1224
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001225 @Override
1226 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1227 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1228 return false;
1229 }
1230
Sanket Padawe356d7632015-06-22 14:03:32 -07001231 final Phone phone = getPhone(subId);
1232 if (phone != null) {
1233 return (phone.getState() == PhoneConstants.State.RINGING);
1234 } else {
1235 return false;
1236 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001237 }
1238
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001239 @Override
1240 public boolean isIdle(String callingPackage) {
1241 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001242 }
1243
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001244 @Override
1245 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1246 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1247 return false;
1248 }
1249
Sanket Padawe356d7632015-06-22 14:03:32 -07001250 final Phone phone = getPhone(subId);
1251 if (phone != null) {
1252 return (phone.getState() == PhoneConstants.State.IDLE);
1253 } else {
1254 return false;
1255 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001256 }
1257
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001258 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001259 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001260 }
1261
Wink Savilleb564aae2014-10-23 10:18:09 -07001262 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001263 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001264 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1265 }
1266
1267 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001268 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001269 }
1270
Wink Savilleb564aae2014-10-23 10:18:09 -07001271 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001272 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001273 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1274 }
1275
1276 /** {@hide} */
1277 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001278 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001279 }
1280
Wink Savilleb564aae2014-10-23 10:18:09 -07001281 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001283 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001284 checkSimPin.start();
1285 return checkSimPin.unlockSim(null, pin);
1286 }
1287
Wink Saville9de0f752013-10-22 19:04:03 -07001288 /** {@hide} */
1289 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001290 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001291 }
1292
Wink Savilleb564aae2014-10-23 10:18:09 -07001293 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001294 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001295 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 checkSimPuk.start();
1297 return checkSimPuk.unlockSim(puk, pin);
1298 }
1299
1300 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001301 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001302 * a synchronous one.
1303 */
1304 private static class UnlockSim extends Thread {
1305
1306 private final IccCard mSimCard;
1307
1308 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001309 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1310 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001311
1312 // For replies from SimCard interface
1313 private Handler mHandler;
1314
1315 // For async handler to identify request type
1316 private static final int SUPPLY_PIN_COMPLETE = 100;
1317
1318 public UnlockSim(IccCard simCard) {
1319 mSimCard = simCard;
1320 }
1321
1322 @Override
1323 public void run() {
1324 Looper.prepare();
1325 synchronized (UnlockSim.this) {
1326 mHandler = new Handler() {
1327 @Override
1328 public void handleMessage(Message msg) {
1329 AsyncResult ar = (AsyncResult) msg.obj;
1330 switch (msg.what) {
1331 case SUPPLY_PIN_COMPLETE:
1332 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1333 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001334 mRetryCount = msg.arg1;
1335 if (ar.exception != null) {
1336 if (ar.exception instanceof CommandException &&
1337 ((CommandException)(ar.exception)).getCommandError()
1338 == CommandException.Error.PASSWORD_INCORRECT) {
1339 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1340 } else {
1341 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1342 }
1343 } else {
1344 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1345 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001346 mDone = true;
1347 UnlockSim.this.notifyAll();
1348 }
1349 break;
1350 }
1351 }
1352 };
1353 UnlockSim.this.notifyAll();
1354 }
1355 Looper.loop();
1356 }
1357
1358 /*
1359 * Use PIN or PUK to unlock SIM card
1360 *
1361 * If PUK is null, unlock SIM card with PIN
1362 *
1363 * If PUK is not null, unlock SIM card with PUK and set PIN code
1364 */
Wink Saville9de0f752013-10-22 19:04:03 -07001365 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001366
1367 while (mHandler == null) {
1368 try {
1369 wait();
1370 } catch (InterruptedException e) {
1371 Thread.currentThread().interrupt();
1372 }
1373 }
1374 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1375
1376 if (puk == null) {
1377 mSimCard.supplyPin(pin, callback);
1378 } else {
1379 mSimCard.supplyPuk(puk, pin, callback);
1380 }
1381
1382 while (!mDone) {
1383 try {
1384 Log.d(LOG_TAG, "wait for done");
1385 wait();
1386 } catch (InterruptedException e) {
1387 // Restore the interrupted status
1388 Thread.currentThread().interrupt();
1389 }
1390 }
1391 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001392 int[] resultArray = new int[2];
1393 resultArray[0] = mResult;
1394 resultArray[1] = mRetryCount;
1395 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001396 }
1397 }
1398
1399 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001400 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001401
1402 }
1403
Wink Savilleb564aae2014-10-23 10:18:09 -07001404 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001405 // No permission check needed here: this call is harmless, and it's
1406 // needed for the ServiceState.requestStateUpdate() call (which is
1407 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001408 final Phone phone = getPhone(subId);
1409 if (phone != null) {
1410 phone.updateServiceLocation();
1411 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 }
1413
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001414 @Override
1415 public boolean isRadioOn(String callingPackage) {
1416 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001417 }
1418
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001419 @Override
1420 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1421 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1422 return false;
1423 }
1424 return isRadioOnForSubscriber(subId);
1425 }
1426
1427 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001428 final Phone phone = getPhone(subId);
1429 if (phone != null) {
1430 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1431 } else {
1432 return false;
1433 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 }
1435
1436 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001437 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001438
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 }
Wink Saville36469e72014-06-11 15:17:00 -07001440
Wink Savilleb564aae2014-10-23 10:18:09 -07001441 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001443 final Phone phone = getPhone(subId);
1444 if (phone != null) {
1445 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1446 }
Wink Saville36469e72014-06-11 15:17:00 -07001447 }
1448
1449 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001450 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001451 }
1452
Wink Savilleb564aae2014-10-23 10:18:09 -07001453 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001454 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001455 final Phone phone = getPhone(subId);
1456 if (phone == null) {
1457 return false;
1458 }
1459 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001460 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001461 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001462 }
1463 return true;
1464 }
Wink Saville36469e72014-06-11 15:17:00 -07001465
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001466 public boolean needMobileRadioShutdown() {
1467 /*
1468 * If any of the Radios are available, it will need to be
1469 * shutdown. So return true if any Radio is available.
1470 */
1471 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1472 Phone phone = PhoneFactory.getPhone(i);
1473 if (phone != null && phone.isRadioAvailable()) return true;
1474 }
1475 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1476 return false;
1477 }
1478
1479 public void shutdownMobileRadios() {
1480 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1481 logv("Shutting down Phone " + i);
1482 shutdownRadioUsingPhoneId(i);
1483 }
1484 }
1485
1486 private void shutdownRadioUsingPhoneId(int phoneId) {
1487 enforceModifyPermission();
1488 Phone phone = PhoneFactory.getPhone(phoneId);
1489 if (phone != null && phone.isRadioAvailable()) {
1490 phone.shutdownRadio();
1491 }
1492 }
1493
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001494 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001495 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1496 if (defaultPhone != null) {
1497 defaultPhone.setRadioPower(turnOn);
1498 return true;
1499 } else {
1500 loge("There's no default phone.");
1501 return false;
1502 }
Wink Saville36469e72014-06-11 15:17:00 -07001503 }
1504
Wink Savilleb564aae2014-10-23 10:18:09 -07001505 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001506 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001507 final Phone phone = getPhone(subId);
1508 if (phone != null) {
1509 phone.setRadioPower(turnOn);
1510 return true;
1511 } else {
1512 return false;
1513 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001514 }
1515
Wink Saville36469e72014-06-11 15:17:00 -07001516 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001517 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001518 public boolean enableDataConnectivity() {
1519 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001520 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001521 final Phone phone = getPhone(subId);
1522 if (phone != null) {
1523 phone.setDataEnabled(true);
1524 return true;
1525 } else {
1526 return false;
1527 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001528 }
1529
Wink Saville36469e72014-06-11 15:17:00 -07001530 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001531 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 public boolean disableDataConnectivity() {
1533 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001534 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001535 final Phone phone = getPhone(subId);
1536 if (phone != null) {
1537 phone.setDataEnabled(false);
1538 return true;
1539 } else {
1540 return false;
1541 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001542 }
1543
Sanket Padawe356d7632015-06-22 14:03:32 -07001544 @Override
Jack Yu92586ef2017-05-01 17:00:48 -07001545 public boolean isDataConnectivityPossible(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001546 final Phone phone = getPhone(subId);
1547 if (phone != null) {
Jack Yu92586ef2017-05-01 17:00:48 -07001548 return phone.isDataAllowed();
Sanket Padawe356d7632015-06-22 14:03:32 -07001549 } else {
1550 return false;
1551 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001552 }
1553
1554 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001555 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001556 }
1557
pkanwarf8520782016-11-06 20:37:09 -08001558 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar7e856482016-10-14 19:37:38 -07001559 enforceCallPermission();
pkanwar7e856482016-10-14 19:37:38 -07001560 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1561 return;
1562 }
1563 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1564 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1565 };
1566
Wink Savilleb564aae2014-10-23 10:18:09 -07001567 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001569 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1570 return false;
1571 }
Wink Saville36469e72014-06-11 15:17:00 -07001572 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001573 }
1574
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001575 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001576 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001577 }
1578
Sanket Padawe5780e442017-03-20 15:04:47 -07001579 public int getCallStateForSlot(int slotIndex) {
1580 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001581 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harold8f68faa2016-12-14 11:27:20 -08001582 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583 }
1584
Sanket Padawe356d7632015-06-22 14:03:32 -07001585 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001586 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001587 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001588 if (phone != null) {
Nathan Harold8f68faa2016-12-14 11:27:20 -08001589 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001590 } else {
Nathan Harold8f68faa2016-12-14 11:27:20 -08001591 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001592 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001593 }
1594
Sanket Padawe356d7632015-06-22 14:03:32 -07001595 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001596 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001597 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001598 if (phone != null) {
1599 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1600 } else {
1601 return TelephonyManager.DATA_ACTIVITY_NONE;
1602 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001603 }
1604
1605 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001606 public Bundle getCellLocation(String callingPackage) {
1607 enforceFineOrCoarseLocationPermission("getCellLocation");
1608
1609 // OP_COARSE_LOCATION controls both fine and coarse location.
1610 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1611 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001612 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001613 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001614 }
1615
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001616 if (checkIfCallerIsSelfOrForegroundUser() ||
1617 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001618 if (DBG_LOC) log("getCellLocation: is active user");
1619 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001620 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001621 if (phone == null) {
1622 return null;
1623 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07001624
1625 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1626 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001627 return data;
1628 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001629 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001630 return null;
1631 }
1632 }
1633
Svetoslav64fad262015-04-14 14:35:21 -07001634 private void enforceFineOrCoarseLocationPermission(String message) {
1635 try {
1636 mApp.enforceCallingOrSelfPermission(
1637 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1638 } catch (SecurityException e) {
1639 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1640 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1641 // is the weaker precondition
1642 mApp.enforceCallingOrSelfPermission(
1643 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1644 }
1645 }
1646
1647
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001648 @Override
1649 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001650 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001651 }
1652
Sanket Padawe356d7632015-06-22 14:03:32 -07001653 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001654 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001655 mApp.enforceCallingOrSelfPermission(
1656 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001657 final Phone phone = getPhone(subId);
1658 if (phone != null) {
1659 phone.enableLocationUpdates();
1660 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001661 }
1662
1663 @Override
1664 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001665 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001666 }
1667
Sanket Padawe356d7632015-06-22 14:03:32 -07001668 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001669 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001670 mApp.enforceCallingOrSelfPermission(
1671 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001672 final Phone phone = getPhone(subId);
1673 if (phone != null) {
1674 phone.disableLocationUpdates();
1675 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001676 }
1677
1678 @Override
1679 @SuppressWarnings("unchecked")
1680 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001681 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1682
1683 // OP_COARSE_LOCATION controls both fine and coarse location.
1684 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1685 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1686 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001687 }
1688
1689 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1690 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1691 return null;
1692 }
Svetoslav64fad262015-04-14 14:35:21 -07001693
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001694 if (checkIfCallerIsSelfOrForegroundUser() ||
1695 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001696 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1697
1698 ArrayList<NeighboringCellInfo> cells = null;
1699
Sooraj Sasindran22882212016-07-18 11:57:25 -07001700 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001701 try {
1702 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran22882212016-07-18 11:57:25 -07001703 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001704 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001705 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001706 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001707 }
1708 return cells;
1709 } else {
1710 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1711 return null;
1712 }
1713 }
1714
1715
1716 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001717 public List<CellInfo> getAllCellInfo(String callingPackage) {
1718 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1719
1720 // OP_COARSE_LOCATION controls both fine and coarse location.
1721 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1722 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1723 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001724 }
1725
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001726 if (checkIfCallerIsSelfOrForegroundUser() ||
1727 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001728 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran22882212016-07-18 11:57:25 -07001729 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001730 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1731 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran22882212016-07-18 11:57:25 -07001732 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1733 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001734 }
1735 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001736 } else {
1737 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1738 return null;
1739 }
1740 }
1741
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001742 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001743 public void setCellInfoListRate(int rateInMillis) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001744 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001745 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1746 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001747 }
1748
Shishir Agrawala9f32182016-04-12 12:00:16 -07001749 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001750 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001751 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1752 return null;
1753 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001754 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001755 return phone == null ? null : phone.getImei();
1756 }
1757
1758 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001759 public String getMeidForSlot(int slotIndex, String callingPackage) {
1760 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1761 return null;
1762 }
1763 Phone phone = PhoneFactory.getPhone(slotIndex);
1764 return phone == null ? null : phone.getMeid();
1765 }
1766
1767 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001768 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001769 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1770 return null;
1771 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001772 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001773 return phone == null ? null : phone.getDeviceSvn();
1774 }
1775
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001776 //
1777 // Internal helper methods.
1778 //
1779
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001780 /**
1781 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1782 */
1783 private boolean checkCallerInteractAcrossUsersFull() {
1784 return mPhone.getContext().checkCallingOrSelfPermission(
1785 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1786 == PackageManager.PERMISSION_GRANTED;
1787 }
1788
Jake Hambye994d462014-02-03 13:10:13 -08001789 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001790 boolean ok;
1791
1792 boolean self = Binder.getCallingUid() == Process.myUid();
1793 if (!self) {
1794 // Get the caller's user id then clear the calling identity
1795 // which will be restored in the finally clause.
1796 int callingUser = UserHandle.getCallingUserId();
1797 long ident = Binder.clearCallingIdentity();
1798
1799 try {
1800 // With calling identity cleared the current user is the foreground user.
1801 int foregroundUser = ActivityManager.getCurrentUser();
1802 ok = (foregroundUser == callingUser);
1803 if (DBG_LOC) {
1804 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1805 + " callingUser=" + callingUser + " ok=" + ok);
1806 }
1807 } catch (Exception ex) {
1808 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1809 ok = false;
1810 } finally {
1811 Binder.restoreCallingIdentity(ident);
1812 }
1813 } else {
1814 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1815 ok = true;
1816 }
1817 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1818 return ok;
1819 }
1820
1821 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001822 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1823 *
1824 * @throws SecurityException if the caller does not have the required permission
1825 */
1826 private void enforceModifyPermission() {
1827 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1828 }
1829
1830 /**
Junda Liua2e36012014-07-09 18:30:01 -07001831 * Make sure either system app or the caller has carrier privilege.
1832 *
1833 * @throws SecurityException if the caller does not have the required permission/privilege
1834 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001835 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001836 int permission = mApp.checkCallingOrSelfPermission(
1837 android.Manifest.permission.MODIFY_PHONE_STATE);
1838 if (permission == PackageManager.PERMISSION_GRANTED) {
1839 return;
1840 }
1841
1842 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001843 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001844 }
1845
1846 /**
1847 * Make sure the caller has carrier privilege.
1848 *
1849 * @throws SecurityException if the caller does not have the required permission
1850 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001851 private void enforceCarrierPrivilege(int subId) {
1852 if (getCarrierPrivilegeStatus(subId) !=
1853 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001854 loge("No Carrier Privilege.");
1855 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001856 }
1857 }
1858
1859 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001860 * Make sure the caller has the CALL_PHONE permission.
1861 *
1862 * @throws SecurityException if the caller does not have the required permission
1863 */
1864 private void enforceCallPermission() {
1865 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1866 }
1867
Stuart Scott8eef64f2015-04-08 15:13:54 -07001868 private void enforceConnectivityInternalPermission() {
1869 mApp.enforceCallingOrSelfPermission(
1870 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1871 "ConnectivityService");
1872 }
1873
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001874 private String createTelUrl(String number) {
1875 if (TextUtils.isEmpty(number)) {
1876 return null;
1877 }
1878
Jake Hambye994d462014-02-03 13:10:13 -08001879 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001880 }
1881
Ihab Awadf9e92732013-12-05 18:02:52 -08001882 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001883 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1884 }
1885
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001886 private static void logv(String msg) {
1887 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1888 }
1889
Ihab Awadf9e92732013-12-05 18:02:52 -08001890 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001891 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1892 }
1893
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001894 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001895 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001896 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001897 }
1898
Sanket Padawe356d7632015-06-22 14:03:32 -07001899 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001900 public int getActivePhoneTypeForSlot(int slotIndex) {
1901 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001902 if (phone == null) {
1903 return PhoneConstants.PHONE_TYPE_NONE;
1904 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001905 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001906 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001907 }
1908
1909 /**
1910 * Returns the CDMA ERI icon index to display
1911 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001912 @Override
1913 public int getCdmaEriIconIndex(String callingPackage) {
1914 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001915 }
1916
Sanket Padawe356d7632015-06-22 14:03:32 -07001917 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001918 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1919 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1920 return -1;
1921 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001922 final Phone phone = getPhone(subId);
1923 if (phone != null) {
1924 return phone.getCdmaEriIconIndex();
1925 } else {
1926 return -1;
1927 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001928 }
1929
1930 /**
1931 * Returns the CDMA ERI icon mode,
1932 * 0 - ON
1933 * 1 - FLASHING
1934 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001935 @Override
1936 public int getCdmaEriIconMode(String callingPackage) {
1937 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001938 }
1939
Sanket Padawe356d7632015-06-22 14:03:32 -07001940 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001941 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1942 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1943 return -1;
1944 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001945 final Phone phone = getPhone(subId);
1946 if (phone != null) {
1947 return phone.getCdmaEriIconMode();
1948 } else {
1949 return -1;
1950 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001951 }
1952
1953 /**
1954 * Returns the CDMA ERI text,
1955 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001956 @Override
1957 public String getCdmaEriText(String callingPackage) {
1958 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001959 }
1960
Sanket Padawe356d7632015-06-22 14:03:32 -07001961 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001962 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1963 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1964 return null;
1965 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001966 final Phone phone = getPhone(subId);
1967 if (phone != null) {
1968 return phone.getCdmaEriText();
1969 } else {
1970 return null;
1971 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001972 }
1973
1974 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001975 * Returns the CDMA MDN.
1976 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001977 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001978 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001979 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001980 final Phone phone = getPhone(subId);
1981 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1982 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001983 } else {
1984 return null;
1985 }
1986 }
1987
1988 /**
1989 * Returns the CDMA MIN.
1990 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001991 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001992 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001993 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001994 final Phone phone = getPhone(subId);
1995 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1996 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001997 } else {
1998 return null;
1999 }
2000 }
2001
2002 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002003 * Returns true if CDMA provisioning needs to run.
2004 */
2005 public boolean needsOtaServiceProvisioning() {
2006 return mPhone.needsOtaServiceProvisioning();
2007 }
2008
2009 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002010 * Sets the voice mail number of a given subId.
2011 */
2012 @Override
2013 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002014 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002015 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2016 new Pair<String, String>(alphaTag, number), new Integer(subId));
2017 return success;
2018 }
2019
Ta-wei Yen87c49842016-05-13 21:19:52 -07002020 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07002021 public void setVisualVoicemailEnabled(String callingPackage,
2022 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
2023 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2024 if (!TextUtils.equals(callingPackage,
2025 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2026 enforceModifyPermissionOrCarrierPrivilege(
2027 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
2028 }
2029 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2030 }
2031
2032 @Override
2033 public boolean isVisualVoicemailEnabled(String callingPackage,
2034 PhoneAccountHandle phoneAccountHandle) {
2035 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2036 return false;
2037 }
2038 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2039 }
2040
2041 @Override
Ta-wei Yen12551102017-03-06 16:00:44 -08002042 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yen243b6372017-01-10 16:17:08 -08002043 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2044 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2045 return null;
2046 }
Ta-wei Yen243b6372017-01-10 16:17:08 -08002047 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2048 }
2049
2050 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002051 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2052 VisualVoicemailSmsFilterSettings settings) {
2053 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002054 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002055 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2056 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002057 }
2058
2059 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002060 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2061 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002062 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002063 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002064 }
2065
2066 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002067 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2068 String callingPackage, int subId) {
2069 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002070 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002071 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002072 }
2073
2074 @Override
Ta-wei Yen1b455992016-12-27 14:52:32 -08002075 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002076 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002077 return VisualVoicemailSmsFilterConfig
Ta-wei Yen1b455992016-12-27 14:52:32 -08002078 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2079 }
2080
2081 @Override
2082 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2083 String number, int port, String text, PendingIntent sentIntent) {
2084 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen463efd12017-01-06 15:29:25 -08002085 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen1b455992016-12-27 14:52:32 -08002086 enforceSendSmsPermission();
2087 // Make the calls as the phone process.
2088 final long identity = Binder.clearCallingIdentity();
2089 try {
2090 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2091 if (port == 0) {
2092 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2093 sentIntent, null, false);
2094 } else {
2095 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2096 smsManager.sendDataMessageWithSelfPermissions(number, null,
2097 (short) port, data, sentIntent, null);
2098 }
2099 } finally {
2100 Binder.restoreCallingIdentity(identity);
2101 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002102 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002103 /**
fionaxuef039d42016-11-14 13:36:14 -08002104 * Sets the voice activation state of a given subId.
2105 */
2106 @Override
2107 public void setVoiceActivationState(int subId, int activationState) {
2108 enforceModifyPermissionOrCarrierPrivilege(subId);
2109 final Phone phone = getPhone(subId);
2110 if (phone != null) {
2111 phone.setVoiceActivationState(activationState);
2112 } else {
2113 loge("setVoiceActivationState fails with invalid subId: " + subId);
2114 }
2115 }
2116
2117 /**
2118 * Sets the data activation state of a given subId.
2119 */
2120 @Override
2121 public void setDataActivationState(int subId, int activationState) {
2122 enforceModifyPermissionOrCarrierPrivilege(subId);
2123 final Phone phone = getPhone(subId);
2124 if (phone != null) {
2125 phone.setDataActivationState(activationState);
2126 } else {
2127 loge("setVoiceActivationState fails with invalid subId: " + subId);
2128 }
2129 }
2130
2131 /**
2132 * Returns the voice activation state of a given subId.
2133 */
2134 @Override
2135 public int getVoiceActivationState(int subId, String callingPackage) {
2136 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2137 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2138 }
2139 final Phone phone = getPhone(subId);
2140 if (phone != null) {
2141 return phone.getVoiceActivationState();
2142 } else {
2143 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2144 }
2145 }
2146
2147 /**
2148 * Returns the data activation state of a given subId.
2149 */
2150 @Override
2151 public int getDataActivationState(int subId, String callingPackage) {
2152 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2153 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2154 }
2155 final Phone phone = getPhone(subId);
2156 if (phone != null) {
2157 return phone.getDataActivationState();
2158 } else {
2159 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2160 }
2161 }
2162
2163 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002164 * Returns the unread count of voicemails
2165 */
2166 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002167 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002168 }
2169
2170 /**
2171 * Returns the unread count of voicemails for a subId
2172 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002173 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002174 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002175 final Phone phone = getPhone(subId);
2176 if (phone != null) {
2177 return phone.getVoiceMessageCount();
2178 } else {
2179 return 0;
2180 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002181 }
2182
2183 /**
pkanwar8efe6572017-01-19 13:43:16 -08002184 * returns true, if the device is in a state where both voice and data
2185 * are supported simultaneously. This can change based on location or network condition.
2186 */
2187 @Override
2188 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2189 final Phone phone = getPhone(subId);
2190 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2191 }
2192
2193 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002194 * Returns the data network type.
2195 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002196 *
2197 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2198 */
2199 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002200 public int getNetworkType() {
2201 final Phone phone = getPhone(getDefaultSubscription());
2202 if (phone != null) {
2203 return phone.getServiceState().getDataNetworkType();
2204 } else {
2205 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2206 }
Wink Saville36469e72014-06-11 15:17:00 -07002207 }
2208
2209 /**
2210 * Returns the network type for a subId
2211 */
2212 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002213 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2214 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2215 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2216 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002217
Sanket Padawe356d7632015-06-22 14:03:32 -07002218 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002219 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002220 return phone.getServiceState().getDataNetworkType();
2221 } else {
2222 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2223 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002224 }
2225
2226 /**
2227 * Returns the data network type
2228 */
2229 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002230 public int getDataNetworkType(String callingPackage) {
2231 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002232 }
2233
2234 /**
2235 * Returns the data network type for a subId
2236 */
2237 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002238 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2239 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2240 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2241 }
2242
Sanket Padawe356d7632015-06-22 14:03:32 -07002243 final Phone phone = getPhone(subId);
2244 if (phone != null) {
2245 return phone.getServiceState().getDataNetworkType();
2246 } else {
2247 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2248 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002249 }
2250
2251 /**
Wink Saville36469e72014-06-11 15:17:00 -07002252 * Returns the Voice network type for a subId
2253 */
2254 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002255 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2256 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2257 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2258 }
2259
Sanket Padawe356d7632015-06-22 14:03:32 -07002260 final Phone phone = getPhone(subId);
2261 if (phone != null) {
2262 return phone.getServiceState().getVoiceNetworkType();
2263 } else {
2264 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2265 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002266 }
2267
2268 /**
2269 * @return true if a ICC card is present
2270 */
2271 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002272 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002273 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2274 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002275 }
2276
2277 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002278 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002279 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002280 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002281 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2282 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002283 final Phone phone = getPhone(subId[0]);
2284 if (subId != null && phone != null) {
2285 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002286 } else {
2287 return false;
2288 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002289 }
2290
2291 /**
2292 * Return if the current radio is LTE on CDMA. This
2293 * is a tri-state return value as for a period of time
2294 * the mode may be unknown.
2295 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002296 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002297 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002298 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002299 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002300 @Override
2301 public int getLteOnCdmaMode(String callingPackage) {
2302 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002303 }
2304
Sanket Padawe356d7632015-06-22 14:03:32 -07002305 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002306 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2307 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2308 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2309 }
2310
Sanket Padawe356d7632015-06-22 14:03:32 -07002311 final Phone phone = getPhone(subId);
2312 if (phone == null) {
2313 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2314 } else {
2315 return phone.getLteOnCdmaMode();
2316 }
Wink Saville36469e72014-06-11 15:17:00 -07002317 }
2318
2319 public void setPhone(Phone phone) {
2320 mPhone = phone;
2321 }
2322
2323 /**
2324 * {@hide}
2325 * Returns Default subId, 0 in the case of single standby.
2326 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002327 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002328 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002329 }
2330
Shishir Agrawala9f32182016-04-12 12:00:16 -07002331 private int getSlotForDefaultSubscription() {
2332 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2333 }
2334
Wink Savilleb564aae2014-10-23 10:18:09 -07002335 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002336 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002337 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002338
2339 /**
2340 * @see android.telephony.TelephonyManager.WifiCallingChoices
2341 */
2342 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002343 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2344 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002345 }
2346
2347 /**
2348 * @see android.telephony.TelephonyManager.WifiCallingChoices
2349 */
2350 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002351 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2352 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2353 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002354 }
2355
Sailesh Nepald1e68152013-12-12 19:08:02 -08002356 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002357 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002358 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002359 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002360
Shishir Agrawal566b7612013-10-28 14:41:00 -07002361 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002362 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002363 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002364
Ajay Nambiabd73502015-12-03 13:50:00 -08002365 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002366 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002367 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002368 if (DBG) log("iccOpenLogicalChannel: " + response);
2369 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002370 }
2371
2372 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002373 public boolean iccCloseLogicalChannel(int subId, int channel) {
2374 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002375
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002376 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002377 if (channel < 0) {
2378 return false;
2379 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002380 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002381 if (DBG) log("iccCloseLogicalChannel: " + success);
2382 return success;
2383 }
2384
2385 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002386 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002387 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002388 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002389
2390 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002391 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2392 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002393 " data=" + data);
2394 }
2395
2396 if (channel < 0) {
2397 return "";
2398 }
2399
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002400 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002401 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002402 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2403
Shishir Agrawal566b7612013-10-28 14:41:00 -07002404 // Append the returned status code to the end of the response payload.
2405 String s = Integer.toHexString(
2406 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002407 if (response.payload != null) {
2408 s = IccUtils.bytesToHexString(response.payload) + s;
2409 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002410 return s;
2411 }
Jake Hambye994d462014-02-03 13:10:13 -08002412
Evan Charltonc66da362014-05-16 14:06:40 -07002413 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002414 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002415 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002416 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002417
2418 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002419 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2420 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002421 }
2422
2423 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002424 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002425 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2426
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002427 // Append the returned status code to the end of the response payload.
2428 String s = Integer.toHexString(
2429 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002430 if (response.payload != null) {
2431 s = IccUtils.bytesToHexString(response.payload) + s;
2432 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002433 return s;
2434 }
2435
2436 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002437 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002438 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002439 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002440
2441 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002442 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002443 p1 + " " + p2 + " " + p3 + ":" + filePath);
2444 }
2445
2446 IccIoResult response =
2447 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002448 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2449 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002450
2451 if (DBG) {
2452 log("Exchange SIM_IO [R]" + response);
2453 }
2454
2455 byte[] result = null;
2456 int length = 2;
2457 if (response.payload != null) {
2458 length = 2 + response.payload.length;
2459 result = new byte[length];
2460 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2461 } else {
2462 result = new byte[length];
2463 }
2464
2465 result[length - 1] = (byte) response.sw2;
2466 result[length - 2] = (byte) response.sw1;
2467 return result;
2468 }
2469
Nathan Haroldb3014052017-01-25 15:57:32 -08002470 /**
2471 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2472 * on a particular subscription
2473 */
2474 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002475 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2476 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002477 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2478 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2479 return null;
2480 }
2481 Object response = sendRequest(
2482 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2483 if (response instanceof String[]) {
2484 return (String[]) response;
2485 }
2486 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2487 return null;
2488 }
2489
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002490 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002491 public String sendEnvelopeWithStatus(int subId, String content) {
2492 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002493
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002494 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002495 if (response.payload == null) {
2496 return "";
2497 }
2498
2499 // Append the returned status code to the end of the response payload.
2500 String s = Integer.toHexString(
2501 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2502 s = IccUtils.bytesToHexString(response.payload) + s;
2503 return s;
2504 }
2505
Jake Hambye994d462014-02-03 13:10:13 -08002506 /**
2507 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2508 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2509 *
2510 * @param itemID the ID of the item to read
2511 * @return the NV item as a String, or null on error.
2512 */
2513 @Override
2514 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002515 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002516 if (DBG) log("nvReadItem: item " + itemID);
2517 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2518 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2519 return value;
2520 }
2521
2522 /**
2523 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2524 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2525 *
2526 * @param itemID the ID of the item to read
2527 * @param itemValue the value to write, as a String
2528 * @return true on success; false on any failure
2529 */
2530 @Override
2531 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002532 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002533 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2534 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2535 new Pair<Integer, String>(itemID, itemValue));
2536 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2537 return success;
2538 }
2539
2540 /**
2541 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2542 * Used for device configuration by some CDMA operators.
2543 *
2544 * @param preferredRoamingList byte array containing the new PRL
2545 * @return true on success; false on any failure
2546 */
2547 @Override
2548 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002549 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002550 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2551 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2552 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2553 return success;
2554 }
2555
2556 /**
2557 * Perform the specified type of NV config reset.
2558 * Used for device configuration by some CDMA operators.
2559 *
2560 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2561 * @return true on success; false on any failure
2562 */
2563 @Override
2564 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002565 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002566 if (DBG) log("nvResetConfig: type " + resetType);
2567 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2568 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2569 return success;
2570 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002571
2572 /**
Wink Saville36469e72014-06-11 15:17:00 -07002573 * {@hide}
2574 * Returns Default sim, 0 in the case of single standby.
2575 */
2576 public int getDefaultSim() {
2577 //TODO Need to get it from Telephony Devcontroller
2578 return 0;
2579 }
2580
Svet Ganovb320e182015-04-16 12:30:10 -07002581 public String[] getPcscfAddress(String apnType, String callingPackage) {
2582 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2583 return new String[0];
2584 }
2585
2586
ram87fca6f2014-07-18 18:58:44 +05302587 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002588 }
2589
Brad Ebinger76681002017-01-23 13:50:20 -08002590 /**
2591 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2592 * feature or {@link null} if the service is not available. If an ImsServiceController is
2593 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2594 * feature updates.
2595 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002596 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002597 IImsServiceFeatureListener callback) {
2598 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002599 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002600 callback);
2601 }
2602
Wink Saville36469e72014-06-11 15:17:00 -07002603 public void setImsRegistrationState(boolean registered) {
2604 enforceModifyPermission();
2605 mPhone.setImsRegistrationState(registered);
2606 }
2607
2608 /**
Stuart Scott54788802015-03-30 13:18:01 -07002609 * Set the network selection mode to automatic.
2610 *
2611 */
2612 @Override
2613 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002614 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002615 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2616 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2617 }
2618
2619 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002620 * Set the network selection mode to manual with the selected carrier.
2621 */
2622 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002623 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2624 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002625 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002626 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002627 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2628 persistSelection);
2629 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002630 }
2631
2632 /**
2633 * Scans for available networks.
2634 */
2635 @Override
2636 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002637 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002638 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2639 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2640 CMD_PERFORM_NETWORK_SCAN, null, subId);
2641 return result;
2642 }
2643
2644 /**
yinxu34eed212017-04-12 16:03:22 -07002645 * Performs a new network scan and returns the id of this scan.
2646 *
2647 * @return the id of the requested scan which can be used to stop the scan.
2648 */
2649 @Override
2650 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
2651 IBinder binder) {
2652 // TODO(yinxu): Implement this method.
2653 throw new UnsupportedOperationException("To be implemented...");
2654 }
2655
2656 /**
2657 * Stops an existing network scan with the given scanId.
2658 */
2659 @Override
2660 public void stopNetworkScan(int subId, int scanId) {
2661 // TODO(yinxu): Implement this method.
2662 throw new UnsupportedOperationException("To be implemented...");
2663 }
2664
2665 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002666 * Get the calculated preferred network type.
2667 * Used for debugging incorrect network type.
2668 *
2669 * @return the preferred network type, defined in RILConstants.java.
2670 */
2671 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002672 public int getCalculatedPreferredNetworkType(String callingPackage) {
2673 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2674 return RILConstants.PREFERRED_NETWORK_MODE;
2675 }
2676
Amit Mahajan43330e02014-11-18 11:54:45 -08002677 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002678 }
2679
2680 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002681 * Get the preferred network type.
2682 * Used for device configuration by some CDMA operators.
2683 *
2684 * @return the preferred network type, defined in RILConstants.java.
2685 */
2686 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002687 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002688 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002689 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002690 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002691 int networkType = (result != null ? result[0] : -1);
2692 if (DBG) log("getPreferredNetworkType: " + networkType);
2693 return networkType;
2694 }
2695
2696 /**
2697 * Set the preferred network type.
2698 * Used for device configuration by some CDMA operators.
2699 *
2700 * @param networkType the preferred network type, defined in RILConstants.java.
2701 * @return true on success; false on any failure.
2702 */
2703 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002704 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002705 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002706 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2707 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002708 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002709 if (success) {
2710 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002711 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002712 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002713 return success;
2714 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002715
2716 /**
Junda Liu475951f2014-11-07 16:45:03 -08002717 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2718 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2719 * tethering.
2720 *
2721 * @return 0: Not required. 1: required. 2: Not set.
2722 * @hide
2723 */
2724 @Override
2725 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002726 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002727 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2728 Settings.Global.TETHER_DUN_REQUIRED, 2);
2729 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2730 // config_tether_apndata.
2731 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2732 dunRequired = 1;
2733 }
2734 return dunRequired;
2735 }
2736
2737 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002738 * Set mobile data enabled
2739 * Used by the user through settings etc to turn on/off mobile data
2740 *
2741 * @param enable {@code true} turn turn data on, else {@code false}
2742 */
2743 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002744 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002745 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002746 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002747 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002748 Phone phone = PhoneFactory.getPhone(phoneId);
2749 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002750 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002751 phone.setDataEnabled(enable);
2752 } else {
2753 loge("setDataEnabled: no phone for subId=" + subId);
2754 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002755 }
2756
2757 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002758 * Get whether mobile data is enabled.
2759 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002760 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002761 *
2762 * @return {@code true} if data is enabled else {@code false}
2763 */
2764 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002765 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002766 try {
2767 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2768 null);
2769 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002770 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002771 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002772 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002773 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002774 Phone phone = PhoneFactory.getPhone(phoneId);
2775 if (phone != null) {
2776 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002777 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002778 return retVal;
2779 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002780 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002781 return false;
2782 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002783 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002784
2785 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002786 public int getCarrierPrivilegeStatus(int subId) {
2787 final Phone phone = getPhone(subId);
2788 if (phone == null) {
2789 loge("getCarrierPrivilegeStatus: Invalid subId");
2790 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2791 }
2792 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002793 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002794 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002795 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2796 }
2797 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002798 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002799 }
Junda Liu29340342014-07-10 15:23:27 -07002800
2801 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002802 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002803 if (TextUtils.isEmpty(pkgName))
2804 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002805 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002806 if (card == null) {
2807 loge("checkCarrierPrivilegesForPackage: No UICC");
2808 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2809 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002810 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2811 }
2812
2813 @Override
2814 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002815 if (TextUtils.isEmpty(pkgName))
2816 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002817 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2818 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2819 UiccCard card = UiccController.getInstance().getUiccCard(i);
2820 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002821 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002822 continue;
2823 }
2824
2825 result = card.getCarrierPrivilegeStatus(
2826 mPhone.getContext().getPackageManager(), pkgName);
2827 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2828 break;
2829 }
2830 }
2831
2832 return result;
Junda Liu29340342014-07-10 15:23:27 -07002833 }
Derek Tan89e89d42014-07-08 17:00:10 -07002834
2835 @Override
Junda Liue64de782015-04-16 17:19:16 -07002836 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2837 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2838 loge("phoneId " + phoneId + " is not valid.");
2839 return null;
2840 }
2841 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002842 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002843 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002844 return null ;
2845 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002846 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002847 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002848 }
2849
Amith Yamasani6e118872016-02-19 12:53:51 -08002850 @Override
2851 public List<String> getPackagesWithCarrierPrivileges() {
2852 PackageManager pm = mPhone.getContext().getPackageManager();
2853 List<String> privilegedPackages = new ArrayList<>();
2854 List<PackageInfo> packages = null;
2855 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2856 UiccCard card = UiccController.getInstance().getUiccCard(i);
2857 if (card == null) {
2858 // No UICC in that slot.
2859 continue;
2860 }
2861 if (card.hasCarrierPrivilegeRules()) {
2862 if (packages == null) {
2863 // Only check packages in user 0 for now
2864 packages = pm.getInstalledPackagesAsUser(
2865 PackageManager.MATCH_DISABLED_COMPONENTS
2866 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2867 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2868 }
2869 for (int p = packages.size() - 1; p >= 0; p--) {
2870 PackageInfo pkgInfo = packages.get(p);
2871 if (pkgInfo != null && pkgInfo.packageName != null
2872 && card.getCarrierPrivilegeStatus(pkgInfo)
2873 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2874 privilegedPackages.add(pkgInfo.packageName);
2875 }
2876 }
2877 }
2878 }
2879 return privilegedPackages;
2880 }
2881
Wink Savilleb564aae2014-10-23 10:18:09 -07002882 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002883 final Phone phone = getPhone(subId);
2884 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002885 if (card == null) {
2886 loge("getIccId: No UICC");
2887 return null;
2888 }
2889 String iccId = card.getIccId();
2890 if (TextUtils.isEmpty(iccId)) {
2891 loge("getIccId: ICC ID is null or empty.");
2892 return null;
2893 }
2894 return iccId;
2895 }
2896
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002897 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002898 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2899 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002900 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002901
Jeff Sharkey85190e62014-12-05 09:40:12 -08002902 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002903 final Phone phone = getPhone(subId);
2904 if (phone == null) {
2905 return false;
2906 }
2907 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002908
2909 if (DBG_MERGE) {
2910 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2911 + subscriberId + " to " + number);
2912 }
2913
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002914 if (TextUtils.isEmpty(iccId)) {
2915 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002916 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002917
Jeff Sharkey85190e62014-12-05 09:40:12 -08002918 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2919
2920 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002921 if (alphaTag == null) {
2922 editor.remove(alphaTagPrefKey);
2923 } else {
2924 editor.putString(alphaTagPrefKey, alphaTag);
2925 }
2926
Jeff Sharkey85190e62014-12-05 09:40:12 -08002927 // Record both the line number and IMSI for this ICCID, since we need to
2928 // track all merged IMSIs based on line number
2929 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2930 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002931 if (number == null) {
2932 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002933 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002934 } else {
2935 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002936 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002937 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002938
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002939 editor.commit();
2940 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002941 }
2942
2943 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002944 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002945 // This is open to apps with WRITE_SMS.
2946 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002947 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002948 return null;
2949 }
Derek Tan97ebb422014-09-05 16:55:38 -07002950
2951 String iccId = getIccId(subId);
2952 if (iccId != null) {
2953 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002954 if (DBG_MERGE) {
2955 log("getLine1NumberForDisplay returning " +
2956 mTelephonySharedPreferences.getString(numberPrefKey, null));
2957 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002958 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002959 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002960 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002961 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002962 }
2963
2964 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002965 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2966 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2967 return null;
2968 }
Derek Tan97ebb422014-09-05 16:55:38 -07002969
2970 String iccId = getIccId(subId);
2971 if (iccId != null) {
2972 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002973 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002974 }
Derek Tan97ebb422014-09-05 16:55:38 -07002975 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002976 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002977
2978 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002979 public String[] getMergedSubscriberIds(String callingPackage) {
2980 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2981 return null;
2982 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002983 final Context context = mPhone.getContext();
2984 final TelephonyManager tele = TelephonyManager.from(context);
2985 final SubscriptionManager sub = SubscriptionManager.from(context);
2986
2987 // Figure out what subscribers are currently active
2988 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002989 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2990 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2991 final long identity = Binder.clearCallingIdentity();
2992 try {
2993 final int[] subIds = sub.getActiveSubscriptionIdList();
2994 for (int subId : subIds) {
2995 activeSubscriberIds.add(tele.getSubscriberId(subId));
2996 }
2997 } finally {
2998 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002999 }
3000
3001 // First pass, find a number override for an active subscriber
3002 String mergeNumber = null;
3003 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
3004 for (String key : prefs.keySet()) {
3005 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
3006 final String subscriberId = (String) prefs.get(key);
3007 if (activeSubscriberIds.contains(subscriberId)) {
3008 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
3009 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3010 mergeNumber = (String) prefs.get(numberKey);
3011 if (DBG_MERGE) {
3012 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3013 + " for active subscriber " + subscriberId);
3014 }
3015 if (!TextUtils.isEmpty(mergeNumber)) {
3016 break;
3017 }
3018 }
3019 }
3020 }
3021
3022 // Shortcut when no active merged subscribers
3023 if (TextUtils.isEmpty(mergeNumber)) {
3024 return null;
3025 }
3026
3027 // Second pass, find all subscribers under that line override
3028 final ArraySet<String> result = new ArraySet<>();
3029 for (String key : prefs.keySet()) {
3030 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3031 final String number = (String) prefs.get(key);
3032 if (mergeNumber.equals(number)) {
3033 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3034 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3035 final String subscriberId = (String) prefs.get(subscriberKey);
3036 if (!TextUtils.isEmpty(subscriberId)) {
3037 result.add(subscriberId);
3038 }
3039 }
3040 }
3041 }
3042
3043 final String[] resultArray = result.toArray(new String[result.size()]);
3044 Arrays.sort(resultArray);
3045 if (DBG_MERGE) {
3046 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3047 }
3048 return resultArray;
3049 }
3050
3051 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003052 public boolean setOperatorBrandOverride(int subId, String brand) {
3053 enforceCarrierPrivilege(subId);
3054 final Phone phone = getPhone(subId);
3055 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003056 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003057
3058 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003059 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003060 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3061 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003062 enforceCarrierPrivilege(subId);
3063 final Phone phone = getPhone(subId);
3064 if (phone == null) {
3065 return false;
3066 }
3067 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003068 cdmaNonRoamingList);
3069 }
3070
3071 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003072 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003073 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3074 enforceModifyPermission();
3075
3076 int returnValue = 0;
3077 try {
3078 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3079 if(result.exception == null) {
3080 if (result.result != null) {
3081 byte[] responseData = (byte[])(result.result);
3082 if(responseData.length > oemResp.length) {
3083 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3084 responseData.length + "bytes. Buffer Size is " +
3085 oemResp.length + "bytes.");
3086 }
3087 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3088 returnValue = responseData.length;
3089 }
3090 } else {
3091 CommandException ex = (CommandException) result.exception;
3092 returnValue = ex.getCommandError().ordinal();
3093 if(returnValue > 0) returnValue *= -1;
3094 }
3095 } catch (RuntimeException e) {
3096 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3097 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3098 if(returnValue > 0) returnValue *= -1;
3099 }
3100
3101 return returnValue;
3102 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003103
3104 @Override
3105 public void setRadioCapability(RadioAccessFamily[] rafs) {
3106 try {
3107 ProxyController.getInstance().setRadioCapability(rafs);
3108 } catch (RuntimeException e) {
3109 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3110 }
3111 }
3112
3113 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003114 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3115 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3116 return RadioAccessFamily.RAF_UNKNOWN;
3117 }
3118
Wink Saville5d475dd2014-10-17 15:00:58 -07003119 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3120 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003121
3122 @Override
3123 public void enableVideoCalling(boolean enable) {
3124 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003125 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003126 }
3127
3128 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003129 public boolean isVideoCallingEnabled(String callingPackage) {
3130 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3131 return false;
3132 }
3133
Andrew Lee77527ac2014-10-21 16:57:39 -07003134 // Check the user preference and the system-level IMS setting. Even if the user has
3135 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3136 // In the long run, we may instead need to check if there exists a connection service
3137 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003138 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3139 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003140 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003141 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003142
Andrew Leea1239f22015-03-02 17:44:07 -08003143 @Override
3144 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003145 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003146 }
3147
3148 @Override
3149 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003150 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003151 }
3152
Andrew Lee9431b832015-03-09 18:46:45 -07003153 @Override
3154 public boolean isTtyModeSupported() {
3155 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3156 TelephonyManager telephonyManager =
3157 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003158 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003159 }
3160
3161 @Override
3162 public boolean isHearingAidCompatibilitySupported() {
3163 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3164 }
3165
Sanket Padawe7310cc72015-01-14 09:53:20 -08003166 /**
3167 * Returns the unique device ID of phone, for example, the IMEI for
3168 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3169 *
3170 * <p>Requires Permission:
3171 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3172 */
3173 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003174 public String getDeviceId(String callingPackage) {
3175 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3176 return null;
3177 }
3178
Sanket Padawe7310cc72015-01-14 09:53:20 -08003179 final Phone phone = PhoneFactory.getPhone(0);
3180 if (phone != null) {
3181 return phone.getDeviceId();
3182 } else {
3183 return null;
3184 }
3185 }
3186
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003187 /*
3188 * {@hide}
3189 * Returns the IMS Registration Status
3190 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003191 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003192 public boolean isImsRegistered() {
3193 return mPhone.isImsRegistered();
3194 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003195
3196 @Override
3197 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3198 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3199 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003200
Nathan Haroldc55097a2015-03-11 18:14:50 -07003201 /*
3202 * {@hide}
3203 * Returns the IMS Registration Status
3204 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003205 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003206 return mPhone.isWifiCallingEnabled();
3207 }
3208
3209 /*
3210 * {@hide}
3211 * Returns the IMS Registration Status
3212 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003213 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003214 return mPhone.isVolteEnabled();
3215 }
Svet Ganovb320e182015-04-16 12:30:10 -07003216
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003217 /*
3218 * {@hide} Returns the IMS Registration Status
3219 */
3220 public boolean isVideoTelephonyAvailable() {
3221 return mPhone.isVideoEnabled();
3222 }
3223
Svet Ganovb320e182015-04-16 12:30:10 -07003224 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003225 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003226 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003227 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3228
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003229 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003230 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003231 } catch (SecurityException e) {
3232 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3233 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003234 }
Svet Ganovb320e182015-04-16 12:30:10 -07003235
3236 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3237 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3238 return false;
3239 }
3240
3241 return true;
3242 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003243
Makoto Onukifee69342015-06-29 14:44:50 -07003244 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003245 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003246 */
3247 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003248 // Default SMS app can always read it.
3249 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3250 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3251 return true;
3252 }
3253 try {
3254 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003255 } catch (SecurityException readPhoneStateSecurityException) {
3256 try {
3257 // Can be read with READ_SMS too.
3258 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3259 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3260 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3261 } catch (SecurityException readSmsSecurityException) {
3262 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3263 // permissions
3264 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3265 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3266 " or " + android.Manifest.permission.READ_SMS + ".");
3267 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003268 }
Makoto Onukifee69342015-06-29 14:44:50 -07003269 }
3270
Stuart Scott8eef64f2015-04-08 15:13:54 -07003271 @Override
3272 public void factoryReset(int subId) {
3273 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003274 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3275 return;
3276 }
3277
Svet Ganovcc087f82015-05-12 20:35:54 -07003278 final long identity = Binder.clearCallingIdentity();
3279 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003280 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3281 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003282 // Enable data
3283 setDataEnabled(subId, true);
3284 // Set network selection mode to automatic
3285 setNetworkSelectionModeAutomatic(subId);
3286 // Set preferred mobile network type to the best available
3287 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3288 // Turn off roaming
3289 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3290 }
3291 } finally {
3292 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003293 }
3294 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003295
3296 @Override
3297 public String getLocaleFromDefaultSim() {
3298 // We query all subscriptions instead of just the active ones, because
3299 // this might be called early on in the provisioning flow when the
3300 // subscriptions potentially aren't active yet.
3301 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3302 if (slist == null || slist.isEmpty()) {
3303 return null;
3304 }
3305
3306 // This function may be called very early, say, from the setup wizard, at
3307 // which point we won't have a default subscription set. If that's the case
3308 // we just choose the first, which will be valid in "most cases".
3309 final int defaultSubId = getDefaultSubscription();
3310 SubscriptionInfo info = null;
3311 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3312 info = slist.get(0);
3313 } else {
3314 for (SubscriptionInfo item : slist) {
3315 if (item.getSubscriptionId() == defaultSubId) {
3316 info = item;
3317 break;
3318 }
3319 }
3320
3321 if (info == null) {
3322 return null;
3323 }
3324 }
3325
3326 // Try and fetch the locale from the carrier properties or from the SIM language
3327 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003328 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003329 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003330 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003331 if (defaultPhone != null) {
3332 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3333 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003334 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003335 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3336 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003337 } else {
3338 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003339 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003340 }
3341 }
3342
Narayan Kamath011676f2015-07-29 12:04:08 +01003343 // The SIM language preferences only store a language (e.g. fr = French), not an
3344 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3345 // the SIM and carrier preferences does not include a country we add the country
3346 // determined from the SIM MCC to provide an exact locale.
3347 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003348 if (mccLocale != null) {
3349 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3350 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003351 }
3352
Tony Hill183b2de2015-06-24 14:53:58 +01003353 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003354 return null;
3355 }
3356
3357 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3358 final long identity = Binder.clearCallingIdentity();
3359 try {
3360 return mSubscriptionController.getAllSubInfoList(
3361 mPhone.getContext().getOpPackageName());
3362 } finally {
3363 Binder.restoreCallingIdentity(identity);
3364 }
3365 }
3366
3367 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3368 final long identity = Binder.clearCallingIdentity();
3369 try {
3370 return mSubscriptionController.getActiveSubscriptionInfoList(
3371 mPhone.getContext().getOpPackageName());
3372 } finally {
3373 Binder.restoreCallingIdentity(identity);
3374 }
3375 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003376
3377 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003378 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3379 * representing the state of the modem.
3380 *
3381 * NOTE: This clears the modem state, so there should only every be one caller.
3382 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003383 */
3384 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003385 public void requestModemActivityInfo(ResultReceiver result) {
3386 enforceModifyPermission();
3387
3388 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3389 Bundle bundle = new Bundle();
3390 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3391 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003392 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003393
3394 /**
3395 * {@hide}
3396 * Returns the service state information on specified subscription.
3397 */
3398 @Override
3399 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3400
3401 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3402 return null;
3403 }
3404
3405 final Phone phone = getPhone(subId);
3406 if (phone == null) {
3407 return null;
3408 }
3409
3410 return phone.getServiceState();
3411 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003412
3413 /**
3414 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3415 *
3416 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3417 * voicemail ringtone.
3418 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3419 * PhoneAccount.
3420 */
3421 @Override
3422 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3423 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3424 if (phone == null) {
3425 return null;
3426 }
3427
3428 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3429 }
3430
3431 /**
3432 * Returns whether vibration is set for voicemail notification in Phone settings.
3433 *
3434 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3435 * voicemail vibration setting.
3436 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3437 */
3438 @Override
3439 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3440 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3441 if (phone == null) {
3442 return false;
3443 }
3444
3445 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3446 }
3447
Youhan Wange64578a2016-05-02 15:32:42 -07003448 /**
3449 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3450 *
3451 * @throws SecurityException if the caller does not have the required permission
3452 */
3453 private void enforceReadPrivilegedPermission() {
3454 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3455 null);
3456 }
3457
3458 /**
Ta-wei Yen1b455992016-12-27 14:52:32 -08003459 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3460 * permission.
3461 *
3462 * @throws SecurityException if the caller does not have the required permission
3463 */
3464 private void enforceSendSmsPermission() {
3465 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3466 }
3467
3468 /**
Ta-wei Yen463efd12017-01-06 15:29:25 -08003469 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003470 *
Ta-wei Yen463efd12017-01-06 15:29:25 -08003471 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003472 */
Ta-wei Yen463efd12017-01-06 15:29:25 -08003473 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
3474 String vvmPackage = RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId)
3475 .getPackageName();
3476 if (!callingPackage.equals(vvmPackage)) {
3477 throw new SecurityException("Caller not current active visual voicemail package[" +
3478 vvmPackage + "]");
Ta-wei Yen1b455992016-12-27 14:52:32 -08003479 }
3480 }
3481
3482 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003483 * Return the application ID for the app type.
3484 *
3485 * @param subId the subscription ID that this request applies to.
3486 * @param appType the uicc app type.
3487 * @return Application ID for specificied app type, or null if no uicc.
3488 */
3489 @Override
3490 public String getAidForAppType(int subId, int appType) {
3491 enforceReadPrivilegedPermission();
3492 Phone phone = getPhone(subId);
3493 if (phone == null) {
3494 return null;
3495 }
3496 String aid = null;
3497 try {
3498 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3499 .getApplicationByType(appType).getAid();
3500 } catch (Exception e) {
3501 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3502 }
3503 return aid;
3504 }
3505
Youhan Wang4001d252016-05-11 10:29:41 -07003506 /**
3507 * Return the Electronic Serial Number.
3508 *
3509 * @param subId the subscription ID that this request applies to.
3510 * @return ESN or null if error.
3511 */
3512 @Override
3513 public String getEsn(int subId) {
3514 enforceReadPrivilegedPermission();
3515 Phone phone = getPhone(subId);
3516 if (phone == null) {
3517 return null;
3518 }
3519 String esn = null;
3520 try {
3521 esn = phone.getEsn();
3522 } catch (Exception e) {
3523 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3524 }
3525 return esn;
3526 }
3527
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003528 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003529 * Return the Preferred Roaming List Version.
3530 *
3531 * @param subId the subscription ID that this request applies to.
3532 * @return PRLVersion or null if error.
3533 */
3534 @Override
3535 public String getCdmaPrlVersion(int subId) {
3536 enforceReadPrivilegedPermission();
3537 Phone phone = getPhone(subId);
3538 if (phone == null) {
3539 return null;
3540 }
3541 String cdmaPrlVersion = null;
3542 try {
3543 cdmaPrlVersion = phone.getCdmaPrlVersion();
3544 } catch (Exception e) {
3545 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3546 }
3547 return cdmaPrlVersion;
3548 }
3549
3550 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003551 * Get snapshot of Telephony histograms
3552 * @return List of Telephony histograms
3553 * @hide
3554 */
3555 @Override
3556 public List<TelephonyHistogram> getTelephonyHistograms() {
3557 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3558 return RIL.getTelephonyRILTimingHistograms();
3559 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003560
3561 /**
3562 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003563 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003564 * Require system privileges. In the future we may add this to carrier APIs.
3565 *
3566 * @return The number of carriers set successfully, should match length of carriers
3567 */
3568 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003569 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003570 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003571 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003572 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3573 return retVal[0];
3574 }
3575
3576 /**
3577 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003578 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003579 * Require system privileges. In the future we may add this to carrier APIs.
3580 *
3581 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3582 * means all carriers are allowed.
3583 */
3584 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003585 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003586 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003587 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003588 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3589 }
3590
fionaxu59545b42016-05-25 15:53:37 -07003591 /**
3592 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3593 * @param subId the subscription ID that this action applies to.
3594 * @param enabled control enable or disable metered apns.
3595 * {@hide}
3596 */
3597 @Override
3598 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3599 enforceModifyPermission();
3600 final Phone phone = getPhone(subId);
3601 if (phone == null) {
3602 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3603 return;
3604 }
3605 try {
3606 phone.carrierActionSetMeteredApnsEnabled(enabled);
3607 } catch (Exception e) {
3608 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3609 }
3610 }
3611
3612 /**
3613 * Action set from carrier signalling broadcast receivers to enable/disable radio
3614 * @param subId the subscription ID that this action applies to.
3615 * @param enabled control enable or disable radio.
3616 * {@hide}
3617 */
3618 @Override
3619 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3620 enforceModifyPermission();
3621 final Phone phone = getPhone(subId);
3622 if (phone == null) {
3623 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3624 return;
3625 }
3626 try {
3627 phone.carrierActionSetRadioEnabled(enabled);
3628 } catch (Exception e) {
3629 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3630 }
3631 }
3632
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003633 /**
3634 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3635 * bug report is being generated.
3636 */
3637 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003638 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003639 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3640 != PackageManager.PERMISSION_GRANTED) {
3641 writer.println("Permission Denial: can't dump Phone from pid="
3642 + Binder.getCallingPid()
3643 + ", uid=" + Binder.getCallingUid()
3644 + "without permission "
3645 + android.Manifest.permission.DUMP);
3646 return;
3647 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003648 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003649 }
Jack Yueb89b242016-06-22 13:27:47 -07003650
3651 /**
Jack Yuc83dc1c2017-05-26 16:07:50 -07003652 * Get aggregated video call data usage since boot.
3653 *
3654 * @param perUidStats True if requesting data usage per uid, otherwise overall usage.
3655 * @return Snapshot of video call data usage
Jack Yueb89b242016-06-22 13:27:47 -07003656 * {@hide}
3657 */
3658 @Override
Jack Yuc83dc1c2017-05-26 16:07:50 -07003659 public NetworkStats getVtDataUsage(int subId, boolean perUidStats) {
Jack Yueb89b242016-06-22 13:27:47 -07003660 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3661 null);
3662
Jack Yuc83dc1c2017-05-26 16:07:50 -07003663 // NetworkStatsService keeps tracking the active network interface and identity. It
3664 // records the delta with the corresponding network identity. We just return the total video
3665 // call data usage snapshot since boot.
3666 Phone phone = getPhone(subId);
3667 if (phone != null) {
3668 return phone.getVtDataUsage(perUidStats);
Jack Yueb89b242016-06-22 13:27:47 -07003669 }
Jack Yuc83dc1c2017-05-26 16:07:50 -07003670 return null;
Jack Yueb89b242016-06-22 13:27:47 -07003671 }
Jack Yu75ab2952016-07-08 14:29:33 -07003672
3673 /**
3674 * Policy control of data connection. Usually used when data limit is passed.
3675 * @param enabled True if enabling the data, otherwise disabling.
3676 * @param subId Subscription index
3677 * {@hide}
3678 */
3679 @Override
3680 public void setPolicyDataEnabled(boolean enabled, int subId) {
3681 enforceModifyPermission();
3682 Phone phone = getPhone(subId);
3683 if (phone != null) {
3684 phone.setPolicyDataEnabled(enabled);
3685 }
3686 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003687
3688 /**
3689 * Get Client request stats
3690 * @return List of Client Request Stats
3691 * @hide
3692 */
3693 @Override
3694 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3695 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3696 return null;
3697 }
3698
3699 Phone phone = getPhone(subId);
3700 if (phone != null) {
3701 return phone.getClientRequestStats();
3702 }
3703
3704 return null;
3705 }
3706
3707 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3708 if (workSource != null) {
3709 return workSource;
3710 }
3711
3712 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3713 workSource = new WorkSource(uid, packageName);
3714 return workSource;
3715 }
Jack Yu346de222017-02-16 15:32:43 -08003716
3717 /**
Grace Chenf4a72232017-03-24 17:21:30 -07003718 * Set SIM card power state.
Jack Yu346de222017-02-16 15:32:43 -08003719 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003720 * @param slotIndex SIM slot id.
Grace Chenf4a72232017-03-24 17:21:30 -07003721 * @param state State of SIM (power down, power up, pass through)
3722 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
3723 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
3724 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yu346de222017-02-16 15:32:43 -08003725 *
3726 **/
3727 @Override
Grace Chenf4a72232017-03-24 17:21:30 -07003728 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yu346de222017-02-16 15:32:43 -08003729 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003730 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003731 if (subId == null || subId.length == 0) {
3732 return;
3733 }
3734
3735 final Phone phone = getPhone(subId[0]);
3736 if (phone != null) {
Grace Chenf4a72232017-03-24 17:21:30 -07003737 phone.setSimPowerState(state);
Jack Yu346de222017-02-16 15:32:43 -08003738 }
3739 }
shuoqfef36a32017-01-10 13:02:18 -08003740
3741 /**
3742 * Check if phone is in emergency callback mode
3743 * @return true if phone is in emergency callback mode
3744 * @param subId sub id
3745 */
3746 public boolean getEmergencyCallbackMode(int subId) {
3747 final Phone phone = getPhone(subId);
3748 if (phone != null) {
3749 return phone.isInEcm();
3750 } else {
3751 return false;
3752 }
3753 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08003754
3755 /**
3756 * Get the current signal strength information for the given subscription.
3757 * Because this information is not updated when the device is in a low power state
3758 * it should not be relied-upon to be current.
3759 * @param subId Subscription index
3760 * @return the most recent cached signal strength info from the modem
3761 */
3762 @Override
3763 public SignalStrength getSignalStrength(int subId) {
3764 Phone p = getPhone(subId);
3765 if (p == null) {
3766 return null;
3767 }
3768
3769 return p.getSignalStrength();
3770 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003771}