blob: 9cd1f9d3ca6308d1d5cc7c1a908d015f19042c91 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Ta-wei Yen30a69c82016-12-27 14:52:32 -080021import android.Manifest.permission;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.app.ActivityManager;
23import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080024import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070025import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.content.Context;
27import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070028import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080029import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070030import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070031import android.net.Uri;
32import android.os.AsyncResult;
33import android.os.Binder;
34import android.os.Bundle;
35import android.os.Handler;
36import android.os.Looper;
37import android.os.Message;
38import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070039import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070040import android.os.ServiceManager;
41import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070042import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070043import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070044import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080045import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070046import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080047import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080048import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070049import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070050import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.telephony.CellInfo;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070052import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070053import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070054import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080055import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070056import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057import android.telephony.ServiceState;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080058import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080059import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080060import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070061import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070062import android.telephony.TelephonyManager;
63import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080065import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070066import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080067import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080068import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080069
Andrew Lee312e8172014-10-23 17:01:36 -070070import com.android.ims.ImsManager;
Brad Ebinger51f743a2017-01-23 13:50:20 -080071import com.android.ims.internal.IImsServiceController;
72import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070073import com.android.internal.telephony.CallManager;
Tyler Gunn52dcf772017-04-26 11:30:31 -070074import com.android.internal.telephony.CallStateException;
Shishir Agrawal302c8692015-06-19 13:49:39 -070075import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070076import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080079import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010080import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070081import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import com.android.internal.telephony.Phone;
Nathan Harolda667c152016-12-14 11:27:20 -080083import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070084import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070085import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070086import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070087import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070088import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080089import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070090import com.android.internal.telephony.uicc.IccIoResult;
91import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080092import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070093import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080094import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070095import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080096import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080097import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc9df0432017-04-17 17:09:07 -070098import com.android.phone.vvm.PhoneAccountHandleConverter;
Ta-wei Yen527a9c02017-01-06 15:29:25 -080099import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yenc9df0432017-04-17 17:09:07 -0700100import com.android.phone.vvm.VisualVoicemailPreferences;
101import com.android.phone.vvm.VisualVoicemailSettingsUtil;
Ta-wei Yenc8905312017-03-28 11:14:45 -0700102import com.android.phone.vvm.VisualVoicemailSmsFilterConfig;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800103
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700104import java.io.FileDescriptor;
105import java.io.PrintWriter;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800106import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700107import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800108import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800109import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100110import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800111import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700112
113/**
114 * Implementation of the ITelephony interface.
115 */
Santos Cordon117fee72014-05-16 17:56:12 -0700116public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700117 private static final String LOG_TAG = "PhoneInterfaceManager";
118 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
119 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800120 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700121
122 // Message codes used with mMainThreadHandler
123 private static final int CMD_HANDLE_PIN_MMI = 1;
124 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
125 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
126 private static final int CMD_ANSWER_RINGING_CALL = 4;
127 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700128 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
129 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700130 private static final int CMD_OPEN_CHANNEL = 9;
131 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
132 private static final int CMD_CLOSE_CHANNEL = 11;
133 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800134 private static final int CMD_NV_READ_ITEM = 13;
135 private static final int EVENT_NV_READ_ITEM_DONE = 14;
136 private static final int CMD_NV_WRITE_ITEM = 15;
137 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
138 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
139 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
140 private static final int CMD_NV_RESET_CONFIG = 19;
141 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800142 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
143 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
144 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
145 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800146 private static final int CMD_SEND_ENVELOPE = 25;
147 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700148 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
149 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
150 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
151 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
152 private static final int CMD_EXCHANGE_SIM_IO = 31;
153 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800154 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
155 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700156 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
157 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700158 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
159 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700160 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
161 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
162 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
163 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700164 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
165 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
166 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
167 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700168 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800169 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
170 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700171
172 /** The singleton instance. */
173 private static PhoneInterfaceManager sInstance;
174
Wink Saville3ab207e2014-11-20 13:07:20 -0800175 private PhoneGlobals mApp;
176 private Phone mPhone;
177 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700178 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800179 private AppOpsManager mAppOps;
180 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800181 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800182 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700183
Derek Tan97ebb422014-09-05 16:55:38 -0700184 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
185 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800186 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700187
188 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700189 * A request object to use for transmitting data to an ICC.
190 */
191 private static final class IccAPDUArgument {
192 public int channel, cla, command, p1, p2, p3;
193 public String data;
194
195 public IccAPDUArgument(int channel, int cla, int command,
196 int p1, int p2, int p3, String data) {
197 this.channel = channel;
198 this.cla = cla;
199 this.command = command;
200 this.p1 = p1;
201 this.p2 = p2;
202 this.p3 = p3;
203 this.data = data;
204 }
205 }
206
207 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700208 * A request object to use for transmitting data to an ICC.
209 */
210 private static final class ManualNetworkSelectionArgument {
211 public OperatorInfo operatorInfo;
212 public boolean persistSelection;
213
214 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
215 this.operatorInfo = operatorInfo;
216 this.persistSelection = persistSelection;
217 }
218 }
219
220 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700221 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
222 * request after sending. The main thread will notify the request when it is complete.
223 */
224 private static final class MainThreadRequest {
225 /** The argument to use for the request */
226 public Object argument;
227 /** The result of the request that is run on the main thread */
228 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800229 // The subscriber id that this request applies to. Defaults to
230 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
231 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700232
233 public MainThreadRequest(Object argument) {
234 this.argument = argument;
235 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800236
237 public MainThreadRequest(Object argument, Integer subId) {
238 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800239 if (subId != null) {
240 this.subId = subId;
241 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800242 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243 }
244
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800245 private static final class IncomingThirdPartyCallArgs {
246 public final ComponentName component;
247 public final String callId;
248 public final String callerDisplayName;
249
250 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
251 String callerDisplayName) {
252 this.component = component;
253 this.callId = callId;
254 this.callerDisplayName = callerDisplayName;
255 }
256 }
257
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258 /**
259 * A handler that processes messages on the main thread in the phone process. Since many
260 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
261 * inbound binder threads to the main thread in the phone process. The Binder thread
262 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
263 * on, which will be notified when the operation completes and will contain the result of the
264 * request.
265 *
266 * <p>If a MainThreadRequest object is provided in the msg.obj field,
267 * note that request.result must be set to something non-null for the calling thread to
268 * unblock.
269 */
270 private final class MainThreadHandler extends Handler {
271 @Override
272 public void handleMessage(Message msg) {
273 MainThreadRequest request;
274 Message onCompleted;
275 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800276 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700277 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700278
279 switch (msg.what) {
pkanwar32d516d2016-10-14 19:37:38 -0700280 case CMD_HANDLE_USSD_REQUEST: {
281 request = (MainThreadRequest) msg.obj;
282 final Phone phone = getPhoneFromRequest(request);
283 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
284 String ussdRequest = ussdObject.first;
285 ResultReceiver wrappedCallback = ussdObject.second;
Tyler Gunn52dcf772017-04-26 11:30:31 -0700286 try {
287 request.result = phone != null ?
288 phone.handleUssdRequest(ussdRequest, wrappedCallback)
289 : false;
290 } catch (CallStateException cse) {
291 request.result = false;
292 }
pkanwar32d516d2016-10-14 19:37:38 -0700293 // Wake up the requesting thread
294 synchronized (request) {
295 request.notifyAll();
296 }
297 break;
298 }
299
Yorke Lee716f67e2015-06-17 15:39:16 -0700300 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700302 final Phone phone = getPhoneFromRequest(request);
303 request.result = phone != null ?
304 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
305 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700306 // Wake up the requesting thread
307 synchronized (request) {
308 request.notifyAll();
309 }
310 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700311 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700312
313 case CMD_HANDLE_NEIGHBORING_CELL:
314 request = (MainThreadRequest) msg.obj;
315 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
316 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700317 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700318 break;
319
320 case EVENT_NEIGHBORING_CELL_DONE:
321 ar = (AsyncResult) msg.obj;
322 request = (MainThreadRequest) ar.userObj;
323 if (ar.exception == null && ar.result != null) {
324 request.result = ar.result;
325 } else {
326 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800327 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700328 }
329 // Wake up the requesting thread
330 synchronized (request) {
331 request.notifyAll();
332 }
333 break;
334
335 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700336 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800337 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700338 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700339 break;
340
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700341 case CMD_END_CALL:
342 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800343 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700344 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700345 Phone phone = getPhone(end_subId);
346 if (phone == null) {
347 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
348 break;
349 }
350 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
352 // CDMA: If the user presses the Power button we treat it as
353 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700354 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700355 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
356 // GSM: End the call as per the Phone state
357 hungUp = PhoneUtils.hangup(mCM);
358 } else {
359 throw new IllegalStateException("Unexpected phone type: " + phoneType);
360 }
361 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
362 request.result = hungUp;
363 // Wake up the requesting thread
364 synchronized (request) {
365 request.notifyAll();
366 }
367 break;
368
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700369 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700370 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700371 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800372 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700373 if (uiccCard == null) {
374 loge("iccTransmitApduLogicalChannel: No UICC");
375 request.result = new IccIoResult(0x6F, 0, (byte[])null);
376 synchronized (request) {
377 request.notifyAll();
378 }
379 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700380 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
381 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700382 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700383 iccArgument.channel, iccArgument.cla, iccArgument.command,
384 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700385 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700386 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700387 break;
388
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700389 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700390 ar = (AsyncResult) msg.obj;
391 request = (MainThreadRequest) ar.userObj;
392 if (ar.exception == null && ar.result != null) {
393 request.result = ar.result;
394 } else {
395 request.result = new IccIoResult(0x6F, 0, (byte[])null);
396 if (ar.result == null) {
397 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800398 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700399 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800400 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700401 } else {
402 loge("iccTransmitApduLogicalChannel: Unknown exception");
403 }
404 }
405 synchronized (request) {
406 request.notifyAll();
407 }
408 break;
409
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700410 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
411 request = (MainThreadRequest) msg.obj;
412 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800413 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700414 if (uiccCard == null) {
415 loge("iccTransmitApduBasicChannel: No UICC");
416 request.result = new IccIoResult(0x6F, 0, (byte[])null);
417 synchronized (request) {
418 request.notifyAll();
419 }
420 } else {
421 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
422 request);
423 uiccCard.iccTransmitApduBasicChannel(
424 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
425 iccArgument.p3, iccArgument.data, onCompleted);
426 }
427 break;
428
429 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
430 ar = (AsyncResult) msg.obj;
431 request = (MainThreadRequest) ar.userObj;
432 if (ar.exception == null && ar.result != null) {
433 request.result = ar.result;
434 } else {
435 request.result = new IccIoResult(0x6F, 0, (byte[])null);
436 if (ar.result == null) {
437 loge("iccTransmitApduBasicChannel: Empty response");
438 } else if (ar.exception instanceof CommandException) {
439 loge("iccTransmitApduBasicChannel: CommandException: " +
440 ar.exception);
441 } else {
442 loge("iccTransmitApduBasicChannel: Unknown exception");
443 }
444 }
445 synchronized (request) {
446 request.notifyAll();
447 }
448 break;
449
450 case CMD_EXCHANGE_SIM_IO:
451 request = (MainThreadRequest) msg.obj;
452 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800453 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700454 if (uiccCard == null) {
455 loge("iccExchangeSimIO: No UICC");
456 request.result = new IccIoResult(0x6F, 0, (byte[])null);
457 synchronized (request) {
458 request.notifyAll();
459 }
460 } else {
461 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
462 request);
463 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
464 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
465 iccArgument.data, onCompleted);
466 }
467 break;
468
469 case EVENT_EXCHANGE_SIM_IO_DONE:
470 ar = (AsyncResult) msg.obj;
471 request = (MainThreadRequest) ar.userObj;
472 if (ar.exception == null && ar.result != null) {
473 request.result = ar.result;
474 } else {
475 request.result = new IccIoResult(0x6f, 0, (byte[])null);
476 }
477 synchronized (request) {
478 request.notifyAll();
479 }
480 break;
481
Derek Tan4d5e5c12014-02-04 11:54:58 -0800482 case CMD_SEND_ENVELOPE:
483 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800484 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700485 if (uiccCard == null) {
486 loge("sendEnvelopeWithStatus: No UICC");
487 request.result = new IccIoResult(0x6F, 0, (byte[])null);
488 synchronized (request) {
489 request.notifyAll();
490 }
491 } else {
492 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
493 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
494 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800495 break;
496
497 case EVENT_SEND_ENVELOPE_DONE:
498 ar = (AsyncResult) msg.obj;
499 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700500 if (ar.exception == null && ar.result != null) {
501 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800502 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700503 request.result = new IccIoResult(0x6F, 0, (byte[])null);
504 if (ar.result == null) {
505 loge("sendEnvelopeWithStatus: Empty response");
506 } else if (ar.exception instanceof CommandException) {
507 loge("sendEnvelopeWithStatus: CommandException: " +
508 ar.exception);
509 } else {
510 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
511 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800512 }
513 synchronized (request) {
514 request.notifyAll();
515 }
516 break;
517
Shishir Agrawal566b7612013-10-28 14:41:00 -0700518 case CMD_OPEN_CHANNEL:
519 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800520 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800521 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700522 if (uiccCard == null) {
523 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800524 request.result = new IccOpenLogicalChannelResponse(-1,
525 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700526 synchronized (request) {
527 request.notifyAll();
528 }
529 } else {
530 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800531 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
532 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700533 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700534 break;
535
536 case EVENT_OPEN_CHANNEL_DONE:
537 ar = (AsyncResult) msg.obj;
538 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700539 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700540 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700541 int[] result = (int[]) ar.result;
542 int channelId = result[0];
543 byte[] selectResponse = null;
544 if (result.length > 1) {
545 selectResponse = new byte[result.length - 1];
546 for (int i = 1; i < result.length; ++i) {
547 selectResponse[i - 1] = (byte) result[i];
548 }
549 }
550 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700551 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700552 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700553 if (ar.result == null) {
554 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700555 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700556 if (ar.exception != null) {
557 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
558 }
559
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700560 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700561 if (ar.exception instanceof CommandException) {
562 CommandException.Error error =
563 ((CommandException) (ar.exception)).getCommandError();
564 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700565 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700566 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700567 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700568 }
569 }
570 openChannelResp = new IccOpenLogicalChannelResponse(
571 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700572 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700573 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700574 synchronized (request) {
575 request.notifyAll();
576 }
577 break;
578
579 case CMD_CLOSE_CHANNEL:
580 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800581 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700582 if (uiccCard == null) {
583 loge("iccCloseLogicalChannel: No UICC");
584 request.result = new IccIoResult(0x6F, 0, (byte[])null);
585 synchronized (request) {
586 request.notifyAll();
587 }
588 } else {
589 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
590 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
591 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700592 break;
593
594 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800595 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
596 break;
597
598 case CMD_NV_READ_ITEM:
599 request = (MainThreadRequest) msg.obj;
600 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
601 mPhone.nvReadItem((Integer) request.argument, onCompleted);
602 break;
603
604 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700605 ar = (AsyncResult) msg.obj;
606 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800607 if (ar.exception == null && ar.result != null) {
608 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700609 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800610 request.result = "";
611 if (ar.result == null) {
612 loge("nvReadItem: Empty response");
613 } else if (ar.exception instanceof CommandException) {
614 loge("nvReadItem: CommandException: " +
615 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700616 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800617 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700618 }
619 }
620 synchronized (request) {
621 request.notifyAll();
622 }
623 break;
624
Jake Hambye994d462014-02-03 13:10:13 -0800625 case CMD_NV_WRITE_ITEM:
626 request = (MainThreadRequest) msg.obj;
627 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
628 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
629 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
630 break;
631
632 case EVENT_NV_WRITE_ITEM_DONE:
633 handleNullReturnEvent(msg, "nvWriteItem");
634 break;
635
636 case CMD_NV_WRITE_CDMA_PRL:
637 request = (MainThreadRequest) msg.obj;
638 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
639 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
640 break;
641
642 case EVENT_NV_WRITE_CDMA_PRL_DONE:
643 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
644 break;
645
646 case CMD_NV_RESET_CONFIG:
647 request = (MainThreadRequest) msg.obj;
648 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
649 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
650 break;
651
652 case EVENT_NV_RESET_CONFIG_DONE:
653 handleNullReturnEvent(msg, "nvResetConfig");
654 break;
655
Jake Hamby7c27be32014-03-03 13:25:59 -0800656 case CMD_GET_PREFERRED_NETWORK_TYPE:
657 request = (MainThreadRequest) msg.obj;
658 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700659 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800660 break;
661
662 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
663 ar = (AsyncResult) msg.obj;
664 request = (MainThreadRequest) ar.userObj;
665 if (ar.exception == null && ar.result != null) {
666 request.result = ar.result; // Integer
667 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800668 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800669 if (ar.result == null) {
670 loge("getPreferredNetworkType: Empty response");
671 } else if (ar.exception instanceof CommandException) {
672 loge("getPreferredNetworkType: CommandException: " +
673 ar.exception);
674 } else {
675 loge("getPreferredNetworkType: Unknown exception");
676 }
677 }
678 synchronized (request) {
679 request.notifyAll();
680 }
681 break;
682
683 case CMD_SET_PREFERRED_NETWORK_TYPE:
684 request = (MainThreadRequest) msg.obj;
685 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
686 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700687 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800688 break;
689
690 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
691 handleNullReturnEvent(msg, "setPreferredNetworkType");
692 break;
693
Steven Liu4bf01bc2014-07-17 11:05:29 -0500694 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
695 request = (MainThreadRequest)msg.obj;
696 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
697 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
698 break;
699
700 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
701 ar = (AsyncResult)msg.obj;
702 request = (MainThreadRequest)ar.userObj;
703 request.result = ar;
704 synchronized (request) {
705 request.notifyAll();
706 }
707 break;
708
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800709 case CMD_SET_VOICEMAIL_NUMBER:
710 request = (MainThreadRequest) msg.obj;
711 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
712 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800713 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
714 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800715 break;
716
717 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
718 handleNullReturnEvent(msg, "setVoicemailNumber");
719 break;
720
Stuart Scott54788802015-03-30 13:18:01 -0700721 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
722 request = (MainThreadRequest) msg.obj;
723 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
724 request);
725 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
726 break;
727
728 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
729 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
730 break;
731
Shishir Agrawal302c8692015-06-19 13:49:39 -0700732 case CMD_PERFORM_NETWORK_SCAN:
733 request = (MainThreadRequest) msg.obj;
734 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
735 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
736 break;
737
738 case EVENT_PERFORM_NETWORK_SCAN_DONE:
739 ar = (AsyncResult) msg.obj;
740 request = (MainThreadRequest) ar.userObj;
741 CellNetworkScanResult cellScanResult;
742 if (ar.exception == null && ar.result != null) {
743 cellScanResult = new CellNetworkScanResult(
744 CellNetworkScanResult.STATUS_SUCCESS,
745 (List<OperatorInfo>) ar.result);
746 } else {
747 if (ar.result == null) {
748 loge("getCellNetworkScanResults: Empty response");
749 }
750 if (ar.exception != null) {
751 loge("getCellNetworkScanResults: Exception: " + ar.exception);
752 }
753 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
754 if (ar.exception instanceof CommandException) {
755 CommandException.Error error =
756 ((CommandException) (ar.exception)).getCommandError();
757 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
758 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
759 } else if (error == CommandException.Error.GENERIC_FAILURE) {
760 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
761 }
762 }
763 cellScanResult = new CellNetworkScanResult(errorCode, null);
764 }
765 request.result = cellScanResult;
766 synchronized (request) {
767 request.notifyAll();
768 }
769 break;
770
771 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
772 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700773 ManualNetworkSelectionArgument selArg =
774 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700775 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
776 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700777 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
778 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700779 break;
780
781 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
782 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
783 break;
784
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700785 case CMD_GET_MODEM_ACTIVITY_INFO:
786 request = (MainThreadRequest) msg.obj;
787 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700788 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700789 break;
790
791 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
792 ar = (AsyncResult) msg.obj;
793 request = (MainThreadRequest) ar.userObj;
794 if (ar.exception == null && ar.result != null) {
795 request.result = ar.result;
796 } else {
797 if (ar.result == null) {
798 loge("queryModemActivityInfo: Empty response");
799 } else if (ar.exception instanceof CommandException) {
800 loge("queryModemActivityInfo: CommandException: " +
801 ar.exception);
802 } else {
803 loge("queryModemActivityInfo: Unknown exception");
804 }
805 }
Amit Mahajand4766222016-01-28 15:28:28 -0800806 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
807 if (request.result == null) {
808 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
809 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700810 synchronized (request) {
811 request.notifyAll();
812 }
813 break;
814
Meng Wang1a7c35a2016-05-05 20:56:15 -0700815 case CMD_SET_ALLOWED_CARRIERS:
816 request = (MainThreadRequest) msg.obj;
817 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
818 mPhone.setAllowedCarriers(
819 (List<CarrierIdentifier>) request.argument,
820 onCompleted);
821 break;
822
823 case EVENT_SET_ALLOWED_CARRIERS_DONE:
824 ar = (AsyncResult) msg.obj;
825 request = (MainThreadRequest) ar.userObj;
826 if (ar.exception == null && ar.result != null) {
827 request.result = ar.result;
828 } else {
829 if (ar.result == null) {
830 loge("setAllowedCarriers: Empty response");
831 } else if (ar.exception instanceof CommandException) {
832 loge("setAllowedCarriers: CommandException: " +
833 ar.exception);
834 } else {
835 loge("setAllowedCarriers: Unknown exception");
836 }
837 }
838 // Result cannot be null. Return -1 on error.
839 if (request.result == null) {
840 request.result = new int[]{-1};
841 }
842 synchronized (request) {
843 request.notifyAll();
844 }
845 break;
846
847 case CMD_GET_ALLOWED_CARRIERS:
848 request = (MainThreadRequest) msg.obj;
849 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
850 mPhone.getAllowedCarriers(onCompleted);
851 break;
852
853 case EVENT_GET_ALLOWED_CARRIERS_DONE:
854 ar = (AsyncResult) msg.obj;
855 request = (MainThreadRequest) ar.userObj;
856 if (ar.exception == null && ar.result != null) {
857 request.result = ar.result;
858 } else {
859 if (ar.result == null) {
860 loge("getAllowedCarriers: Empty response");
861 } else if (ar.exception instanceof CommandException) {
862 loge("getAllowedCarriers: CommandException: " +
863 ar.exception);
864 } else {
865 loge("getAllowedCarriers: Unknown exception");
866 }
867 }
868 // Result cannot be null. Return empty list of CarrierIdentifier.
869 if (request.result == null) {
870 request.result = new ArrayList<CarrierIdentifier>(0);
871 }
872 synchronized (request) {
873 request.notifyAll();
874 }
875 break;
876
Nathan Haroldb3014052017-01-25 15:57:32 -0800877 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
878 ar = (AsyncResult) msg.obj;
879 request = (MainThreadRequest) ar.userObj;
880 if (ar.exception == null && ar.result != null) {
881 request.result = ar.result;
882 } else {
883 request.result = new IllegalArgumentException(
884 "Failed to retrieve Forbidden Plmns");
885 if (ar.result == null) {
886 loge("getForbiddenPlmns: Empty response");
887 } else {
888 loge("getForbiddenPlmns: Unknown exception");
889 }
890 }
891 synchronized (request) {
892 request.notifyAll();
893 }
894 break;
895
896 case CMD_GET_FORBIDDEN_PLMNS:
897 request = (MainThreadRequest) msg.obj;
898 uiccCard = getUiccCardFromRequest(request);
899 if (uiccCard == null) {
900 loge("getForbiddenPlmns() UiccCard is null");
901 request.result = new IllegalArgumentException(
902 "getForbiddenPlmns() UiccCard is null");
903 synchronized (request) {
904 request.notifyAll();
905 }
906 break;
907 }
908 Integer appType = (Integer) request.argument;
909 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
910 if (uiccApp == null) {
911 loge("getForbiddenPlmns() no app with specified type -- "
912 + appType);
913 request.result = new IllegalArgumentException("Failed to get UICC App");
914 synchronized (request) {
915 request.notifyAll();
916 }
917 break;
918 } else {
919 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
920 + " specified type -- " + appType);
921 }
922 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
923 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
924 onCompleted);
925 break;
926
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700927 default:
928 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
929 break;
930 }
931 }
Jake Hambye994d462014-02-03 13:10:13 -0800932
933 private void handleNullReturnEvent(Message msg, String command) {
934 AsyncResult ar = (AsyncResult) msg.obj;
935 MainThreadRequest request = (MainThreadRequest) ar.userObj;
936 if (ar.exception == null) {
937 request.result = true;
938 } else {
939 request.result = false;
940 if (ar.exception instanceof CommandException) {
941 loge(command + ": CommandException: " + ar.exception);
942 } else {
943 loge(command + ": Unknown exception");
944 }
945 }
946 synchronized (request) {
947 request.notifyAll();
948 }
949 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700950 }
951
952 /**
953 * Posts the specified command to be executed on the main thread,
954 * waits for the request to complete, and returns the result.
955 * @see #sendRequestAsync
956 */
957 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800958 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700959 }
960
961 /**
962 * Posts the specified command to be executed on the main thread,
963 * waits for the request to complete, and returns the result.
964 * @see #sendRequestAsync
965 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800966 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
968 throw new RuntimeException("This method will deadlock if called from the main thread.");
969 }
970
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800971 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700972 Message msg = mMainThreadHandler.obtainMessage(command, request);
973 msg.sendToTarget();
974
975 // Wait for the request to complete
976 synchronized (request) {
977 while (request.result == null) {
978 try {
979 request.wait();
980 } catch (InterruptedException e) {
981 // Do nothing, go back and wait until the request is complete
982 }
983 }
984 }
985 return request.result;
986 }
987
988 /**
989 * Asynchronous ("fire and forget") version of sendRequest():
990 * Posts the specified command to be executed on the main thread, and
991 * returns immediately.
992 * @see #sendRequest
993 */
994 private void sendRequestAsync(int command) {
995 mMainThreadHandler.sendEmptyMessage(command);
996 }
997
998 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700999 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
1000 * @see {@link #sendRequest(int,Object)}
1001 */
1002 private void sendRequestAsync(int command, Object argument) {
1003 MainThreadRequest request = new MainThreadRequest(argument);
1004 Message msg = mMainThreadHandler.obtainMessage(command, request);
1005 msg.sendToTarget();
1006 }
1007
1008 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 * Initialize the singleton PhoneInterfaceManager instance.
1010 * This is only done once, at startup, from PhoneApp.onCreate().
1011 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001012 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 synchronized (PhoneInterfaceManager.class) {
1014 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001015 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 } else {
1017 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1018 }
1019 return sInstance;
1020 }
1021 }
1022
1023 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001024 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001025 mApp = app;
1026 mPhone = phone;
1027 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001028 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001029 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1030 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001031 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001032 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001033 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001034
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 publish();
1036 }
1037
1038 private void publish() {
1039 if (DBG) log("publish: " + this);
1040
1041 ServiceManager.addService("phone", this);
1042 }
1043
Stuart Scott584921c2015-01-15 17:10:34 -08001044 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001045 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1046 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001047 }
1048
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001049 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1050 Phone phone = getPhoneFromRequest(request);
1051 return phone == null ? null :
1052 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1053 }
1054
Wink Saville36469e72014-06-11 15:17:00 -07001055 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001056 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001057 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001058 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 //
1060 // Implementation of the ITelephony interface.
1061 //
1062
1063 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001064 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001065 }
1066
Wink Savilleb564aae2014-10-23 10:18:09 -07001067 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001068 if (DBG) log("dial: " + number);
1069 // No permission check needed here: This is just a wrapper around the
1070 // ACTION_DIAL intent, which is available to any app since it puts up
1071 // the UI before it does anything.
1072
1073 String url = createTelUrl(number);
1074 if (url == null) {
1075 return;
1076 }
1077
1078 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001079 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001080 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1081 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1082 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1083 mApp.startActivity(intent);
1084 }
1085 }
1086
1087 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001088 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001089 }
1090
Wink Savilleb564aae2014-10-23 10:18:09 -07001091 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001092 if (DBG) log("call: " + number);
1093
1094 // This is just a wrapper around the ACTION_CALL intent, but we still
1095 // need to do a permission check since we're calling startActivity()
1096 // from the context of the phone app.
1097 enforceCallPermission();
1098
1099 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1100 != AppOpsManager.MODE_ALLOWED) {
1101 return;
1102 }
1103
1104 String url = createTelUrl(number);
1105 if (url == null) {
1106 return;
1107 }
1108
Wink Saville08874612014-08-31 19:19:58 -07001109 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001110 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001111 if (slist != null) {
1112 for (SubscriptionInfo subInfoRecord : slist) {
1113 if (subInfoRecord.getSubscriptionId() == subId) {
1114 isValid = true;
1115 break;
1116 }
Wink Saville08874612014-08-31 19:19:58 -07001117 }
1118 }
1119 if (isValid == false) {
1120 return;
1121 }
1122
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001123 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001124 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1126 mApp.startActivity(intent);
1127 }
1128
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 /**
1130 * End a call based on call state
1131 * @return true is a call was ended
1132 */
1133 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001134 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001135 }
1136
1137 /**
1138 * End a call based on the call state of the subId
1139 * @return true is a call was ended
1140 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001141 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001142 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001143 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001144 }
1145
1146 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001147 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001148 }
1149
Wink Savilleb564aae2014-10-23 10:18:09 -07001150 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001151 if (DBG) log("answerRingingCall...");
1152 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1153 // but that can probably wait till the big TelephonyManager API overhaul.
1154 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1155 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001156 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001157 }
1158
1159 /**
1160 * Make the actual telephony calls to implement answerRingingCall().
1161 * This should only be called from the main thread of the Phone app.
1162 * @see #answerRingingCall
1163 *
1164 * TODO: it would be nice to return true if we answered the call, or
1165 * false if there wasn't actually a ringing incoming call, or some
1166 * other error occurred. (In other words, pass back the return value
1167 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1168 * But that would require calling this method via sendRequest() rather
1169 * than sendRequestAsync(), and right now we don't actually *need* that
1170 * return value, so let's just return void for now.
1171 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001172 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001173 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001174 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001175 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1176 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001177 if (hasActiveCall && hasHoldingCall) {
1178 // Both lines are in use!
1179 // TODO: provide a flag to let the caller specify what
1180 // policy to use if both lines are in use. (The current
1181 // behavior is hardwired to "answer incoming, end ongoing",
1182 // which is how the CALL button is specced to behave.)
1183 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1184 return;
1185 } else {
1186 // answerCall() will automatically hold the current active
1187 // call, if there is one.
1188 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1189 return;
1190 }
1191 } else {
1192 // No call was ringing.
1193 return;
1194 }
1195 }
1196
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001197 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001198 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001199 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001200 public void silenceRinger() {
1201 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001202 }
1203
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001204 @Override
1205 public boolean isOffhook(String callingPackage) {
1206 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001207 }
1208
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001209 @Override
1210 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1211 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1212 return false;
1213 }
1214
Sanket Padawe356d7632015-06-22 14:03:32 -07001215 final Phone phone = getPhone(subId);
1216 if (phone != null) {
1217 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1218 } else {
1219 return false;
1220 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001221 }
1222
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001223 @Override
1224 public boolean isRinging(String callingPackage) {
1225 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001226 }
1227
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001228 @Override
1229 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1230 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1231 return false;
1232 }
1233
Sanket Padawe356d7632015-06-22 14:03:32 -07001234 final Phone phone = getPhone(subId);
1235 if (phone != null) {
1236 return (phone.getState() == PhoneConstants.State.RINGING);
1237 } else {
1238 return false;
1239 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001240 }
1241
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001242 @Override
1243 public boolean isIdle(String callingPackage) {
1244 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001245 }
1246
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001247 @Override
1248 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1249 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1250 return false;
1251 }
1252
Sanket Padawe356d7632015-06-22 14:03:32 -07001253 final Phone phone = getPhone(subId);
1254 if (phone != null) {
1255 return (phone.getState() == PhoneConstants.State.IDLE);
1256 } else {
1257 return false;
1258 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001259 }
1260
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001262 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001263 }
1264
Wink Savilleb564aae2014-10-23 10:18:09 -07001265 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001266 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001267 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1268 }
1269
1270 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001271 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001272 }
1273
Wink Savilleb564aae2014-10-23 10:18:09 -07001274 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001275 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001276 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1277 }
1278
1279 /** {@hide} */
1280 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001281 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001282 }
1283
Wink Savilleb564aae2014-10-23 10:18:09 -07001284 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001286 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287 checkSimPin.start();
1288 return checkSimPin.unlockSim(null, pin);
1289 }
1290
Wink Saville9de0f752013-10-22 19:04:03 -07001291 /** {@hide} */
1292 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001293 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001294 }
1295
Wink Savilleb564aae2014-10-23 10:18:09 -07001296 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001297 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001298 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001299 checkSimPuk.start();
1300 return checkSimPuk.unlockSim(puk, pin);
1301 }
1302
1303 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001304 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305 * a synchronous one.
1306 */
1307 private static class UnlockSim extends Thread {
1308
1309 private final IccCard mSimCard;
1310
1311 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001312 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1313 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001314
1315 // For replies from SimCard interface
1316 private Handler mHandler;
1317
1318 // For async handler to identify request type
1319 private static final int SUPPLY_PIN_COMPLETE = 100;
1320
1321 public UnlockSim(IccCard simCard) {
1322 mSimCard = simCard;
1323 }
1324
1325 @Override
1326 public void run() {
1327 Looper.prepare();
1328 synchronized (UnlockSim.this) {
1329 mHandler = new Handler() {
1330 @Override
1331 public void handleMessage(Message msg) {
1332 AsyncResult ar = (AsyncResult) msg.obj;
1333 switch (msg.what) {
1334 case SUPPLY_PIN_COMPLETE:
1335 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1336 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001337 mRetryCount = msg.arg1;
1338 if (ar.exception != null) {
1339 if (ar.exception instanceof CommandException &&
1340 ((CommandException)(ar.exception)).getCommandError()
1341 == CommandException.Error.PASSWORD_INCORRECT) {
1342 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1343 } else {
1344 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1345 }
1346 } else {
1347 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1348 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001349 mDone = true;
1350 UnlockSim.this.notifyAll();
1351 }
1352 break;
1353 }
1354 }
1355 };
1356 UnlockSim.this.notifyAll();
1357 }
1358 Looper.loop();
1359 }
1360
1361 /*
1362 * Use PIN or PUK to unlock SIM card
1363 *
1364 * If PUK is null, unlock SIM card with PIN
1365 *
1366 * If PUK is not null, unlock SIM card with PUK and set PIN code
1367 */
Wink Saville9de0f752013-10-22 19:04:03 -07001368 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001369
1370 while (mHandler == null) {
1371 try {
1372 wait();
1373 } catch (InterruptedException e) {
1374 Thread.currentThread().interrupt();
1375 }
1376 }
1377 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1378
1379 if (puk == null) {
1380 mSimCard.supplyPin(pin, callback);
1381 } else {
1382 mSimCard.supplyPuk(puk, pin, callback);
1383 }
1384
1385 while (!mDone) {
1386 try {
1387 Log.d(LOG_TAG, "wait for done");
1388 wait();
1389 } catch (InterruptedException e) {
1390 // Restore the interrupted status
1391 Thread.currentThread().interrupt();
1392 }
1393 }
1394 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001395 int[] resultArray = new int[2];
1396 resultArray[0] = mResult;
1397 resultArray[1] = mRetryCount;
1398 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 }
1400 }
1401
1402 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001403 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001404
1405 }
1406
Wink Savilleb564aae2014-10-23 10:18:09 -07001407 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 // No permission check needed here: this call is harmless, and it's
1409 // needed for the ServiceState.requestStateUpdate() call (which is
1410 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001411 final Phone phone = getPhone(subId);
1412 if (phone != null) {
1413 phone.updateServiceLocation();
1414 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001415 }
1416
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001417 @Override
1418 public boolean isRadioOn(String callingPackage) {
1419 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001420 }
1421
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001422 @Override
1423 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1424 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1425 return false;
1426 }
1427 return isRadioOnForSubscriber(subId);
1428 }
1429
1430 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001431 final Phone phone = getPhone(subId);
1432 if (phone != null) {
1433 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1434 } else {
1435 return false;
1436 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001437 }
1438
1439 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001440 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001441
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 }
Wink Saville36469e72014-06-11 15:17:00 -07001443
Wink Savilleb564aae2014-10-23 10:18:09 -07001444 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001445 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001446 final Phone phone = getPhone(subId);
1447 if (phone != null) {
1448 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1449 }
Wink Saville36469e72014-06-11 15:17:00 -07001450 }
1451
1452 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001453 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001454 }
1455
Wink Savilleb564aae2014-10-23 10:18:09 -07001456 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001457 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001458 final Phone phone = getPhone(subId);
1459 if (phone == null) {
1460 return false;
1461 }
1462 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001463 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001464 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001465 }
1466 return true;
1467 }
Wink Saville36469e72014-06-11 15:17:00 -07001468
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001469 public boolean needMobileRadioShutdown() {
1470 /*
1471 * If any of the Radios are available, it will need to be
1472 * shutdown. So return true if any Radio is available.
1473 */
1474 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1475 Phone phone = PhoneFactory.getPhone(i);
1476 if (phone != null && phone.isRadioAvailable()) return true;
1477 }
1478 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1479 return false;
1480 }
1481
1482 public void shutdownMobileRadios() {
1483 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1484 logv("Shutting down Phone " + i);
1485 shutdownRadioUsingPhoneId(i);
1486 }
1487 }
1488
1489 private void shutdownRadioUsingPhoneId(int phoneId) {
1490 enforceModifyPermission();
1491 Phone phone = PhoneFactory.getPhone(phoneId);
1492 if (phone != null && phone.isRadioAvailable()) {
1493 phone.shutdownRadio();
1494 }
1495 }
1496
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001497 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001498 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1499 if (defaultPhone != null) {
1500 defaultPhone.setRadioPower(turnOn);
1501 return true;
1502 } else {
1503 loge("There's no default phone.");
1504 return false;
1505 }
Wink Saville36469e72014-06-11 15:17:00 -07001506 }
1507
Wink Savilleb564aae2014-10-23 10:18:09 -07001508 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001509 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001510 final Phone phone = getPhone(subId);
1511 if (phone != null) {
1512 phone.setRadioPower(turnOn);
1513 return true;
1514 } else {
1515 return false;
1516 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001517 }
1518
Wink Saville36469e72014-06-11 15:17:00 -07001519 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001520 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001521 public boolean enableDataConnectivity() {
1522 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001523 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001524 final Phone phone = getPhone(subId);
1525 if (phone != null) {
1526 phone.setDataEnabled(true);
1527 return true;
1528 } else {
1529 return false;
1530 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001531 }
1532
Wink Saville36469e72014-06-11 15:17:00 -07001533 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001534 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001535 public boolean disableDataConnectivity() {
1536 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001537 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001538 final Phone phone = getPhone(subId);
1539 if (phone != null) {
1540 phone.setDataEnabled(false);
1541 return true;
1542 } else {
1543 return false;
1544 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545 }
1546
Wink Saville36469e72014-06-11 15:17:00 -07001547 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001548 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001549 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001550 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001551 final Phone phone = getPhone(subId);
1552 if (phone != null) {
1553 return phone.isDataConnectivityPossible();
1554 } else {
1555 return false;
1556 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001557 }
1558
1559 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001560 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001561 }
1562
pkanwarae03a6b2016-11-06 20:37:09 -08001563 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001564 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001565 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1566 return;
1567 }
1568 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1569 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1570 };
1571
Wink Savilleb564aae2014-10-23 10:18:09 -07001572 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001573 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001574 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1575 return false;
1576 }
Wink Saville36469e72014-06-11 15:17:00 -07001577 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001578 }
1579
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001580 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001581 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001582 }
1583
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001584 public int getCallStateForSlot(int slotIndex) {
1585 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001586 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harolda667c152016-12-14 11:27:20 -08001587 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001588 }
1589
Sanket Padawe356d7632015-06-22 14:03:32 -07001590 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001591 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001592 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001593 if (phone != null) {
Nathan Harolda667c152016-12-14 11:27:20 -08001594 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001595 } else {
Nathan Harolda667c152016-12-14 11:27:20 -08001596 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001597 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001598 }
1599
Sanket Padawe356d7632015-06-22 14:03:32 -07001600 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001601 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001602 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001603 if (phone != null) {
1604 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1605 } else {
1606 return TelephonyManager.DATA_ACTIVITY_NONE;
1607 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001608 }
1609
1610 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001611 public Bundle getCellLocation(String callingPackage) {
1612 enforceFineOrCoarseLocationPermission("getCellLocation");
1613
1614 // OP_COARSE_LOCATION controls both fine and coarse location.
1615 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1616 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001617 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001618 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001619 }
1620
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001621 if (checkIfCallerIsSelfOrForegroundUser() ||
1622 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001623 if (DBG_LOC) log("getCellLocation: is active user");
1624 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001625 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001626 if (phone == null) {
1627 return null;
1628 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001629
1630 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1631 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001632 return data;
1633 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001634 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001635 return null;
1636 }
1637 }
1638
Svetoslav64fad262015-04-14 14:35:21 -07001639 private void enforceFineOrCoarseLocationPermission(String message) {
1640 try {
1641 mApp.enforceCallingOrSelfPermission(
1642 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1643 } catch (SecurityException e) {
1644 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1645 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1646 // is the weaker precondition
1647 mApp.enforceCallingOrSelfPermission(
1648 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1649 }
1650 }
1651
1652
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001653 @Override
1654 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001655 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001656 }
1657
Sanket Padawe356d7632015-06-22 14:03:32 -07001658 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001659 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001660 mApp.enforceCallingOrSelfPermission(
1661 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001662 final Phone phone = getPhone(subId);
1663 if (phone != null) {
1664 phone.enableLocationUpdates();
1665 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001666 }
1667
1668 @Override
1669 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001670 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001671 }
1672
Sanket Padawe356d7632015-06-22 14:03:32 -07001673 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001674 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001675 mApp.enforceCallingOrSelfPermission(
1676 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001677 final Phone phone = getPhone(subId);
1678 if (phone != null) {
1679 phone.disableLocationUpdates();
1680 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001681 }
1682
1683 @Override
1684 @SuppressWarnings("unchecked")
1685 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001686 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1687
1688 // OP_COARSE_LOCATION controls both fine and coarse location.
1689 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1690 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1691 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001692 }
1693
1694 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1695 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1696 return null;
1697 }
Svetoslav64fad262015-04-14 14:35:21 -07001698
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001699 if (checkIfCallerIsSelfOrForegroundUser() ||
1700 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001701 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1702
1703 ArrayList<NeighboringCellInfo> cells = null;
1704
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001705 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001706 try {
1707 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001708 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001709 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001710 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001711 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001712 }
1713 return cells;
1714 } else {
1715 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1716 return null;
1717 }
1718 }
1719
1720
1721 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001722 public List<CellInfo> getAllCellInfo(String callingPackage) {
1723 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1724
1725 // OP_COARSE_LOCATION controls both fine and coarse location.
1726 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1727 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1728 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001729 }
1730
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001731 if (checkIfCallerIsSelfOrForegroundUser() ||
1732 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001733 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001734 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001735 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1736 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001737 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1738 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001739 }
1740 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001741 } else {
1742 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1743 return null;
1744 }
1745 }
1746
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001747 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001748 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08001749 enforceModifyPermission();
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001750 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1751 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001752 }
1753
Shishir Agrawala9f32182016-04-12 12:00:16 -07001754 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001755 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001756 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1757 return null;
1758 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001759 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001760 return phone == null ? null : phone.getImei();
1761 }
1762
1763 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07001764 public String getMeidForSlot(int slotIndex, String callingPackage) {
1765 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1766 return null;
1767 }
1768 Phone phone = PhoneFactory.getPhone(slotIndex);
1769 return phone == null ? null : phone.getMeid();
1770 }
1771
1772 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001773 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001774 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1775 return null;
1776 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001777 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001778 return phone == null ? null : phone.getDeviceSvn();
1779 }
1780
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001781 //
1782 // Internal helper methods.
1783 //
1784
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001785 /**
1786 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1787 */
1788 private boolean checkCallerInteractAcrossUsersFull() {
1789 return mPhone.getContext().checkCallingOrSelfPermission(
1790 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1791 == PackageManager.PERMISSION_GRANTED;
1792 }
1793
Jake Hambye994d462014-02-03 13:10:13 -08001794 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001795 boolean ok;
1796
1797 boolean self = Binder.getCallingUid() == Process.myUid();
1798 if (!self) {
1799 // Get the caller's user id then clear the calling identity
1800 // which will be restored in the finally clause.
1801 int callingUser = UserHandle.getCallingUserId();
1802 long ident = Binder.clearCallingIdentity();
1803
1804 try {
1805 // With calling identity cleared the current user is the foreground user.
1806 int foregroundUser = ActivityManager.getCurrentUser();
1807 ok = (foregroundUser == callingUser);
1808 if (DBG_LOC) {
1809 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1810 + " callingUser=" + callingUser + " ok=" + ok);
1811 }
1812 } catch (Exception ex) {
1813 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1814 ok = false;
1815 } finally {
1816 Binder.restoreCallingIdentity(ident);
1817 }
1818 } else {
1819 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1820 ok = true;
1821 }
1822 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1823 return ok;
1824 }
1825
1826 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001827 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1828 *
1829 * @throws SecurityException if the caller does not have the required permission
1830 */
1831 private void enforceModifyPermission() {
1832 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1833 }
1834
1835 /**
Junda Liua2e36012014-07-09 18:30:01 -07001836 * Make sure either system app or the caller has carrier privilege.
1837 *
1838 * @throws SecurityException if the caller does not have the required permission/privilege
1839 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001840 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001841 int permission = mApp.checkCallingOrSelfPermission(
1842 android.Manifest.permission.MODIFY_PHONE_STATE);
1843 if (permission == PackageManager.PERMISSION_GRANTED) {
1844 return;
1845 }
1846
1847 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001848 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001849 }
1850
1851 /**
1852 * Make sure the caller has carrier privilege.
1853 *
1854 * @throws SecurityException if the caller does not have the required permission
1855 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001856 private void enforceCarrierPrivilege(int subId) {
1857 if (getCarrierPrivilegeStatus(subId) !=
1858 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001859 loge("No Carrier Privilege.");
1860 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001861 }
1862 }
1863
1864 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001865 * Make sure the caller has the CALL_PHONE permission.
1866 *
1867 * @throws SecurityException if the caller does not have the required permission
1868 */
1869 private void enforceCallPermission() {
1870 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1871 }
1872
Stuart Scott8eef64f2015-04-08 15:13:54 -07001873 private void enforceConnectivityInternalPermission() {
1874 mApp.enforceCallingOrSelfPermission(
1875 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1876 "ConnectivityService");
1877 }
1878
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001879 private String createTelUrl(String number) {
1880 if (TextUtils.isEmpty(number)) {
1881 return null;
1882 }
1883
Jake Hambye994d462014-02-03 13:10:13 -08001884 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001885 }
1886
Ihab Awadf9e92732013-12-05 18:02:52 -08001887 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001888 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1889 }
1890
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001891 private static void logv(String msg) {
1892 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1893 }
1894
Ihab Awadf9e92732013-12-05 18:02:52 -08001895 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001896 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1897 }
1898
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001899 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001900 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001901 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001902 }
1903
Sanket Padawe356d7632015-06-22 14:03:32 -07001904 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001905 public int getActivePhoneTypeForSlot(int slotIndex) {
1906 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001907 if (phone == null) {
1908 return PhoneConstants.PHONE_TYPE_NONE;
1909 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001910 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001911 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001912 }
1913
1914 /**
1915 * Returns the CDMA ERI icon index to display
1916 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001917 @Override
1918 public int getCdmaEriIconIndex(String callingPackage) {
1919 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001920 }
1921
Sanket Padawe356d7632015-06-22 14:03:32 -07001922 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001923 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1924 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1925 return -1;
1926 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001927 final Phone phone = getPhone(subId);
1928 if (phone != null) {
1929 return phone.getCdmaEriIconIndex();
1930 } else {
1931 return -1;
1932 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001933 }
1934
1935 /**
1936 * Returns the CDMA ERI icon mode,
1937 * 0 - ON
1938 * 1 - FLASHING
1939 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001940 @Override
1941 public int getCdmaEriIconMode(String callingPackage) {
1942 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001943 }
1944
Sanket Padawe356d7632015-06-22 14:03:32 -07001945 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001946 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1947 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1948 return -1;
1949 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001950 final Phone phone = getPhone(subId);
1951 if (phone != null) {
1952 return phone.getCdmaEriIconMode();
1953 } else {
1954 return -1;
1955 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001956 }
1957
1958 /**
1959 * Returns the CDMA ERI text,
1960 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001961 @Override
1962 public String getCdmaEriText(String callingPackage) {
1963 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001964 }
1965
Sanket Padawe356d7632015-06-22 14:03:32 -07001966 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001967 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1968 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1969 return null;
1970 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001971 final Phone phone = getPhone(subId);
1972 if (phone != null) {
1973 return phone.getCdmaEriText();
1974 } else {
1975 return null;
1976 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001977 }
1978
1979 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001980 * Returns the CDMA MDN.
1981 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001982 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001983 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001984 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001985 final Phone phone = getPhone(subId);
1986 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1987 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001988 } else {
1989 return null;
1990 }
1991 }
1992
1993 /**
1994 * Returns the CDMA MIN.
1995 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001996 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001997 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001998 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001999 final Phone phone = getPhone(subId);
2000 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
2001 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07002002 } else {
2003 return null;
2004 }
2005 }
2006
2007 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002008 * Returns true if CDMA provisioning needs to run.
2009 */
2010 public boolean needsOtaServiceProvisioning() {
2011 return mPhone.needsOtaServiceProvisioning();
2012 }
2013
2014 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002015 * Sets the voice mail number of a given subId.
2016 */
2017 @Override
2018 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002019 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002020 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2021 new Pair<String, String>(alphaTag, number), new Integer(subId));
2022 return success;
2023 }
2024
Ta-wei Yen87c49842016-05-13 21:19:52 -07002025 @Override
Ta-wei Yenc9df0432017-04-17 17:09:07 -07002026 public Bundle getVisualVoicemailSettings(String callingPackage, int subId) {
2027 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2028 String systemDialer = TelecomManager.from(mPhone.getContext()).getSystemDialerPackage();
2029 if (!TextUtils.equals(callingPackage, systemDialer)) {
2030 throw new SecurityException("caller must be system dialer");
2031 }
2032 PhoneAccountHandle phoneAccountHandle = PhoneAccountHandleConverter.fromSubId(subId);
2033 if (phoneAccountHandle == null){
2034 return null;
2035 }
2036 return VisualVoicemailSettingsUtil.dump(mPhone.getContext(), phoneAccountHandle);
2037 }
2038
2039 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002040 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002041 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2042 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2043 return null;
2044 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002045 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2046 }
2047
2048 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002049 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2050 VisualVoicemailSmsFilterSettings settings) {
2051 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002052 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002053 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2054 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002055 }
2056
2057 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002058 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2059 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002060 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002061 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002062 }
2063
2064 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002065 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2066 String callingPackage, int subId) {
2067 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002068 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002069 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002070 }
2071
2072 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002073 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002074 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002075 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002076 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2077 }
2078
2079 @Override
2080 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2081 String number, int port, String text, PendingIntent sentIntent) {
2082 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002083 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002084 enforceSendSmsPermission();
2085 // Make the calls as the phone process.
2086 final long identity = Binder.clearCallingIdentity();
2087 try {
2088 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2089 if (port == 0) {
2090 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2091 sentIntent, null, false);
2092 } else {
2093 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2094 smsManager.sendDataMessageWithSelfPermissions(number, null,
2095 (short) port, data, sentIntent, null);
2096 }
2097 } finally {
2098 Binder.restoreCallingIdentity(identity);
2099 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002100 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002101 /**
fionaxu0152e512016-11-14 13:36:14 -08002102 * Sets the voice activation state of a given subId.
2103 */
2104 @Override
2105 public void setVoiceActivationState(int subId, int activationState) {
2106 enforceModifyPermissionOrCarrierPrivilege(subId);
2107 final Phone phone = getPhone(subId);
2108 if (phone != null) {
2109 phone.setVoiceActivationState(activationState);
2110 } else {
2111 loge("setVoiceActivationState fails with invalid subId: " + subId);
2112 }
2113 }
2114
2115 /**
2116 * Sets the data activation state of a given subId.
2117 */
2118 @Override
2119 public void setDataActivationState(int subId, int activationState) {
2120 enforceModifyPermissionOrCarrierPrivilege(subId);
2121 final Phone phone = getPhone(subId);
2122 if (phone != null) {
2123 phone.setDataActivationState(activationState);
2124 } else {
2125 loge("setVoiceActivationState fails with invalid subId: " + subId);
2126 }
2127 }
2128
2129 /**
2130 * Returns the voice activation state of a given subId.
2131 */
2132 @Override
2133 public int getVoiceActivationState(int subId, String callingPackage) {
2134 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2135 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2136 }
2137 final Phone phone = getPhone(subId);
2138 if (phone != null) {
2139 return phone.getVoiceActivationState();
2140 } else {
2141 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2142 }
2143 }
2144
2145 /**
2146 * Returns the data activation state of a given subId.
2147 */
2148 @Override
2149 public int getDataActivationState(int subId, String callingPackage) {
2150 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2151 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2152 }
2153 final Phone phone = getPhone(subId);
2154 if (phone != null) {
2155 return phone.getDataActivationState();
2156 } else {
2157 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2158 }
2159 }
2160
2161 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002162 * Returns the unread count of voicemails
2163 */
2164 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002165 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002166 }
2167
2168 /**
2169 * Returns the unread count of voicemails for a subId
2170 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002171 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002172 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002173 final Phone phone = getPhone(subId);
2174 if (phone != null) {
2175 return phone.getVoiceMessageCount();
2176 } else {
2177 return 0;
2178 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002179 }
2180
2181 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002182 * returns true, if the device is in a state where both voice and data
2183 * are supported simultaneously. This can change based on location or network condition.
2184 */
2185 @Override
2186 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2187 final Phone phone = getPhone(subId);
2188 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2189 }
2190
2191 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002192 * Send the dialer code if called from the current default dialer or the caller has
2193 * carrier privilege.
2194 * @param inputCode The dialer code to send
2195 */
2196 @Override
2197 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
2198 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2199 String defaultDialer = TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage();
2200 if (!TextUtils.equals(callingPackage, defaultDialer)) {
2201 enforceCarrierPrivilege(getDefaultSubscription());
2202 }
2203 mPhone.sendDialerSpecialCode(inputCode);
2204 }
2205
2206 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002207 * Returns the data network type.
2208 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002209 *
2210 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2211 */
2212 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002213 public int getNetworkType() {
2214 final Phone phone = getPhone(getDefaultSubscription());
2215 if (phone != null) {
2216 return phone.getServiceState().getDataNetworkType();
2217 } else {
2218 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2219 }
Wink Saville36469e72014-06-11 15:17:00 -07002220 }
2221
2222 /**
2223 * Returns the network type for a subId
2224 */
2225 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002226 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2227 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2228 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2229 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002230
Sanket Padawe356d7632015-06-22 14:03:32 -07002231 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002232 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002233 return phone.getServiceState().getDataNetworkType();
2234 } else {
2235 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2236 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002237 }
2238
2239 /**
2240 * Returns the data network type
2241 */
2242 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002243 public int getDataNetworkType(String callingPackage) {
2244 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002245 }
2246
2247 /**
2248 * Returns the data network type for a subId
2249 */
2250 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002251 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2252 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2253 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2254 }
2255
Sanket Padawe356d7632015-06-22 14:03:32 -07002256 final Phone phone = getPhone(subId);
2257 if (phone != null) {
2258 return phone.getServiceState().getDataNetworkType();
2259 } else {
2260 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2261 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002262 }
2263
2264 /**
Wink Saville36469e72014-06-11 15:17:00 -07002265 * Returns the Voice network type for a subId
2266 */
2267 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002268 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2269 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2270 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2271 }
2272
Sanket Padawe356d7632015-06-22 14:03:32 -07002273 final Phone phone = getPhone(subId);
2274 if (phone != null) {
2275 return phone.getServiceState().getVoiceNetworkType();
2276 } else {
2277 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2278 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002279 }
2280
2281 /**
2282 * @return true if a ICC card is present
2283 */
2284 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002285 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002286 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2287 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002288 }
2289
2290 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002291 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002292 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002293 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002294 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2295 final Phone phone = PhoneFactory.getPhone(slotIndex);
fionaxu6e6c68b2016-06-23 13:31:32 -07002296 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002297 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002298 } else {
2299 return false;
2300 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002301 }
2302
2303 /**
2304 * Return if the current radio is LTE on CDMA. This
2305 * is a tri-state return value as for a period of time
2306 * the mode may be unknown.
2307 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002308 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002309 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002310 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002311 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002312 @Override
2313 public int getLteOnCdmaMode(String callingPackage) {
2314 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002315 }
2316
Sanket Padawe356d7632015-06-22 14:03:32 -07002317 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002318 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2319 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2320 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2321 }
2322
Sanket Padawe356d7632015-06-22 14:03:32 -07002323 final Phone phone = getPhone(subId);
2324 if (phone == null) {
2325 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2326 } else {
2327 return phone.getLteOnCdmaMode();
2328 }
Wink Saville36469e72014-06-11 15:17:00 -07002329 }
2330
2331 public void setPhone(Phone phone) {
2332 mPhone = phone;
2333 }
2334
2335 /**
2336 * {@hide}
2337 * Returns Default subId, 0 in the case of single standby.
2338 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002339 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002340 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002341 }
2342
Shishir Agrawala9f32182016-04-12 12:00:16 -07002343 private int getSlotForDefaultSubscription() {
2344 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2345 }
2346
Wink Savilleb564aae2014-10-23 10:18:09 -07002347 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002348 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002349 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002350
2351 /**
2352 * @see android.telephony.TelephonyManager.WifiCallingChoices
2353 */
2354 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002355 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2356 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002357 }
2358
2359 /**
2360 * @see android.telephony.TelephonyManager.WifiCallingChoices
2361 */
2362 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002363 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2364 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2365 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002366 }
2367
Sailesh Nepald1e68152013-12-12 19:08:02 -08002368 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002369 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002370 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002371 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002372
Shishir Agrawal566b7612013-10-28 14:41:00 -07002373 @Override
Ajay Nambid7454d32015-12-03 13:50:00 -08002374 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002375 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002376
Ajay Nambid7454d32015-12-03 13:50:00 -08002377 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002378 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambid7454d32015-12-03 13:50:00 -08002379 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002380 if (DBG) log("iccOpenLogicalChannel: " + response);
2381 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002382 }
2383
2384 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002385 public boolean iccCloseLogicalChannel(int subId, int channel) {
2386 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002387
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002388 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002389 if (channel < 0) {
2390 return false;
2391 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002392 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002393 if (DBG) log("iccCloseLogicalChannel: " + success);
2394 return success;
2395 }
2396
2397 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002398 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002399 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002400 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002401
2402 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002403 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2404 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002405 " data=" + data);
2406 }
2407
2408 if (channel < 0) {
2409 return "";
2410 }
2411
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002412 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002413 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002414 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2415
Shishir Agrawal566b7612013-10-28 14:41:00 -07002416 // Append the returned status code to the end of the response payload.
2417 String s = Integer.toHexString(
2418 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002419 if (response.payload != null) {
2420 s = IccUtils.bytesToHexString(response.payload) + s;
2421 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002422 return s;
2423 }
Jake Hambye994d462014-02-03 13:10:13 -08002424
Evan Charltonc66da362014-05-16 14:06:40 -07002425 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002426 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002427 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002428 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002429
2430 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002431 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2432 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002433 }
2434
2435 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002436 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002437 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2438
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002439 // Append the returned status code to the end of the response payload.
2440 String s = Integer.toHexString(
2441 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002442 if (response.payload != null) {
2443 s = IccUtils.bytesToHexString(response.payload) + s;
2444 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002445 return s;
2446 }
2447
2448 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002449 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002450 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002451 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002452
2453 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002454 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002455 p1 + " " + p2 + " " + p3 + ":" + filePath);
2456 }
2457
2458 IccIoResult response =
2459 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002460 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2461 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002462
2463 if (DBG) {
2464 log("Exchange SIM_IO [R]" + response);
2465 }
2466
2467 byte[] result = null;
2468 int length = 2;
2469 if (response.payload != null) {
2470 length = 2 + response.payload.length;
2471 result = new byte[length];
2472 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2473 } else {
2474 result = new byte[length];
2475 }
2476
2477 result[length - 1] = (byte) response.sw2;
2478 result[length - 2] = (byte) response.sw1;
2479 return result;
2480 }
2481
Nathan Haroldb3014052017-01-25 15:57:32 -08002482 /**
2483 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2484 * on a particular subscription
2485 */
2486 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002487 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2488 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002489 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2490 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2491 return null;
2492 }
2493 Object response = sendRequest(
2494 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2495 if (response instanceof String[]) {
2496 return (String[]) response;
2497 }
2498 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2499 return null;
2500 }
2501
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002502 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002503 public String sendEnvelopeWithStatus(int subId, String content) {
2504 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002505
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002506 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002507 if (response.payload == null) {
2508 return "";
2509 }
2510
2511 // Append the returned status code to the end of the response payload.
2512 String s = Integer.toHexString(
2513 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2514 s = IccUtils.bytesToHexString(response.payload) + s;
2515 return s;
2516 }
2517
Jake Hambye994d462014-02-03 13:10:13 -08002518 /**
2519 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2520 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2521 *
2522 * @param itemID the ID of the item to read
2523 * @return the NV item as a String, or null on error.
2524 */
2525 @Override
2526 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002527 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002528 if (DBG) log("nvReadItem: item " + itemID);
2529 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2530 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2531 return value;
2532 }
2533
2534 /**
2535 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2536 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2537 *
2538 * @param itemID the ID of the item to read
2539 * @param itemValue the value to write, as a String
2540 * @return true on success; false on any failure
2541 */
2542 @Override
2543 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002544 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002545 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2546 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2547 new Pair<Integer, String>(itemID, itemValue));
2548 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2549 return success;
2550 }
2551
2552 /**
2553 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2554 * Used for device configuration by some CDMA operators.
2555 *
2556 * @param preferredRoamingList byte array containing the new PRL
2557 * @return true on success; false on any failure
2558 */
2559 @Override
2560 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002561 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002562 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2563 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2564 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2565 return success;
2566 }
2567
2568 /**
2569 * Perform the specified type of NV config reset.
2570 * Used for device configuration by some CDMA operators.
2571 *
2572 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2573 * @return true on success; false on any failure
2574 */
2575 @Override
2576 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002577 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002578 if (DBG) log("nvResetConfig: type " + resetType);
2579 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2580 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2581 return success;
2582 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002583
2584 /**
Wink Saville36469e72014-06-11 15:17:00 -07002585 * {@hide}
2586 * Returns Default sim, 0 in the case of single standby.
2587 */
2588 public int getDefaultSim() {
2589 //TODO Need to get it from Telephony Devcontroller
2590 return 0;
2591 }
2592
Svet Ganovb320e182015-04-16 12:30:10 -07002593 public String[] getPcscfAddress(String apnType, String callingPackage) {
2594 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2595 return new String[0];
2596 }
2597
2598
ram87fca6f2014-07-18 18:58:44 +05302599 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002600 }
2601
Brad Ebinger51f743a2017-01-23 13:50:20 -08002602 /**
2603 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2604 * feature or {@link null} if the service is not available. If an ImsServiceController is
2605 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2606 * feature updates.
2607 */
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002608 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002609 IImsServiceFeatureListener callback) {
2610 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002611 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002612 callback);
2613 }
2614
Wink Saville36469e72014-06-11 15:17:00 -07002615 public void setImsRegistrationState(boolean registered) {
2616 enforceModifyPermission();
2617 mPhone.setImsRegistrationState(registered);
2618 }
2619
2620 /**
Stuart Scott54788802015-03-30 13:18:01 -07002621 * Set the network selection mode to automatic.
2622 *
2623 */
2624 @Override
2625 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002626 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002627 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2628 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2629 }
2630
2631 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002632 * Set the network selection mode to manual with the selected carrier.
2633 */
2634 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002635 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2636 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002637 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002638 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002639 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2640 persistSelection);
2641 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002642 }
2643
2644 /**
2645 * Scans for available networks.
2646 */
2647 @Override
2648 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002649 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002650 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2651 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2652 CMD_PERFORM_NETWORK_SCAN, null, subId);
2653 return result;
2654 }
2655
2656 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002657 * Get the calculated preferred network type.
2658 * Used for debugging incorrect network type.
2659 *
2660 * @return the preferred network type, defined in RILConstants.java.
2661 */
2662 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002663 public int getCalculatedPreferredNetworkType(String callingPackage) {
2664 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2665 return RILConstants.PREFERRED_NETWORK_MODE;
2666 }
2667
Amit Mahajan43330e02014-11-18 11:54:45 -08002668 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002669 }
2670
2671 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002672 * Get the preferred network type.
2673 * Used for device configuration by some CDMA operators.
2674 *
2675 * @return the preferred network type, defined in RILConstants.java.
2676 */
2677 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002678 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002679 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002680 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002681 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002682 int networkType = (result != null ? result[0] : -1);
2683 if (DBG) log("getPreferredNetworkType: " + networkType);
2684 return networkType;
2685 }
2686
2687 /**
2688 * Set the preferred network type.
2689 * Used for device configuration by some CDMA operators.
2690 *
2691 * @param networkType the preferred network type, defined in RILConstants.java.
2692 * @return true on success; false on any failure.
2693 */
2694 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002695 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002696 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002697 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2698 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002699 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002700 if (success) {
2701 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002702 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002703 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002704 return success;
2705 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002706
2707 /**
Junda Liu475951f2014-11-07 16:45:03 -08002708 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2709 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2710 * tethering.
2711 *
2712 * @return 0: Not required. 1: required. 2: Not set.
2713 * @hide
2714 */
2715 @Override
2716 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002717 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002718 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2719 Settings.Global.TETHER_DUN_REQUIRED, 2);
2720 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2721 // config_tether_apndata.
2722 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2723 dunRequired = 1;
2724 }
2725 return dunRequired;
2726 }
2727
2728 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002729 * Set mobile data enabled
2730 * Used by the user through settings etc to turn on/off mobile data
2731 *
2732 * @param enable {@code true} turn turn data on, else {@code false}
2733 */
2734 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002735 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002736 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002737 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002738 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002739 Phone phone = PhoneFactory.getPhone(phoneId);
2740 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002741 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002742 phone.setDataEnabled(enable);
2743 } else {
2744 loge("setDataEnabled: no phone for subId=" + subId);
2745 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002746 }
2747
2748 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002749 * Get whether mobile data is enabled.
2750 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002751 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002752 *
2753 * @return {@code true} if data is enabled else {@code false}
2754 */
2755 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002756 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002757 try {
2758 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2759 null);
2760 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002761 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002762 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002763 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002764 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002765 Phone phone = PhoneFactory.getPhone(phoneId);
2766 if (phone != null) {
2767 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002768 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002769 return retVal;
2770 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002771 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002772 return false;
2773 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002774 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002775
2776 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002777 public int getCarrierPrivilegeStatus(int subId) {
2778 final Phone phone = getPhone(subId);
2779 if (phone == null) {
2780 loge("getCarrierPrivilegeStatus: Invalid subId");
2781 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2782 }
2783 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002784 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002785 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002786 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2787 }
2788 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002789 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002790 }
Junda Liu29340342014-07-10 15:23:27 -07002791
2792 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002793 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002794 if (TextUtils.isEmpty(pkgName))
2795 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002796 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002797 if (card == null) {
2798 loge("checkCarrierPrivilegesForPackage: No UICC");
2799 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2800 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002801 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2802 }
2803
2804 @Override
2805 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002806 if (TextUtils.isEmpty(pkgName))
2807 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002808 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2809 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2810 UiccCard card = UiccController.getInstance().getUiccCard(i);
2811 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002812 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002813 continue;
2814 }
2815
2816 result = card.getCarrierPrivilegeStatus(
2817 mPhone.getContext().getPackageManager(), pkgName);
2818 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2819 break;
2820 }
2821 }
2822
2823 return result;
Junda Liu29340342014-07-10 15:23:27 -07002824 }
Derek Tan89e89d42014-07-08 17:00:10 -07002825
2826 @Override
Junda Liue64de782015-04-16 17:19:16 -07002827 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2828 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2829 loge("phoneId " + phoneId + " is not valid.");
2830 return null;
2831 }
2832 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002833 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002834 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002835 return null ;
2836 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002837 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002838 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002839 }
2840
Amith Yamasani6e118872016-02-19 12:53:51 -08002841 @Override
2842 public List<String> getPackagesWithCarrierPrivileges() {
2843 PackageManager pm = mPhone.getContext().getPackageManager();
2844 List<String> privilegedPackages = new ArrayList<>();
2845 List<PackageInfo> packages = null;
2846 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2847 UiccCard card = UiccController.getInstance().getUiccCard(i);
2848 if (card == null) {
2849 // No UICC in that slot.
2850 continue;
2851 }
2852 if (card.hasCarrierPrivilegeRules()) {
2853 if (packages == null) {
2854 // Only check packages in user 0 for now
2855 packages = pm.getInstalledPackagesAsUser(
2856 PackageManager.MATCH_DISABLED_COMPONENTS
2857 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2858 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2859 }
2860 for (int p = packages.size() - 1; p >= 0; p--) {
2861 PackageInfo pkgInfo = packages.get(p);
2862 if (pkgInfo != null && pkgInfo.packageName != null
2863 && card.getCarrierPrivilegeStatus(pkgInfo)
2864 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2865 privilegedPackages.add(pkgInfo.packageName);
2866 }
2867 }
2868 }
2869 }
2870 return privilegedPackages;
2871 }
2872
Wink Savilleb564aae2014-10-23 10:18:09 -07002873 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002874 final Phone phone = getPhone(subId);
2875 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002876 if (card == null) {
2877 loge("getIccId: No UICC");
2878 return null;
2879 }
2880 String iccId = card.getIccId();
2881 if (TextUtils.isEmpty(iccId)) {
2882 loge("getIccId: ICC ID is null or empty.");
2883 return null;
2884 }
2885 return iccId;
2886 }
2887
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002888 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002889 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2890 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002891 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002892
Jeff Sharkey85190e62014-12-05 09:40:12 -08002893 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002894 final Phone phone = getPhone(subId);
2895 if (phone == null) {
2896 return false;
2897 }
2898 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002899
2900 if (DBG_MERGE) {
2901 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2902 + subscriberId + " to " + number);
2903 }
2904
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002905 if (TextUtils.isEmpty(iccId)) {
2906 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002907 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002908
Jeff Sharkey85190e62014-12-05 09:40:12 -08002909 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2910
2911 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002912 if (alphaTag == null) {
2913 editor.remove(alphaTagPrefKey);
2914 } else {
2915 editor.putString(alphaTagPrefKey, alphaTag);
2916 }
2917
Jeff Sharkey85190e62014-12-05 09:40:12 -08002918 // Record both the line number and IMSI for this ICCID, since we need to
2919 // track all merged IMSIs based on line number
2920 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2921 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002922 if (number == null) {
2923 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002924 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002925 } else {
2926 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002927 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002928 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002929
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002930 editor.commit();
2931 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002932 }
2933
2934 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002935 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002936 // This is open to apps with WRITE_SMS.
2937 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002938 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002939 return null;
2940 }
Derek Tan97ebb422014-09-05 16:55:38 -07002941
2942 String iccId = getIccId(subId);
2943 if (iccId != null) {
2944 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002945 if (DBG_MERGE) {
2946 log("getLine1NumberForDisplay returning " +
2947 mTelephonySharedPreferences.getString(numberPrefKey, null));
2948 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002949 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002950 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002951 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002952 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002953 }
2954
2955 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002956 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2957 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2958 return null;
2959 }
Derek Tan97ebb422014-09-05 16:55:38 -07002960
2961 String iccId = getIccId(subId);
2962 if (iccId != null) {
2963 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002964 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002965 }
Derek Tan97ebb422014-09-05 16:55:38 -07002966 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002967 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002968
2969 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002970 public String[] getMergedSubscriberIds(String callingPackage) {
2971 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2972 return null;
2973 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002974 final Context context = mPhone.getContext();
2975 final TelephonyManager tele = TelephonyManager.from(context);
2976 final SubscriptionManager sub = SubscriptionManager.from(context);
2977
2978 // Figure out what subscribers are currently active
2979 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002980 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2981 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2982 final long identity = Binder.clearCallingIdentity();
2983 try {
2984 final int[] subIds = sub.getActiveSubscriptionIdList();
2985 for (int subId : subIds) {
2986 activeSubscriberIds.add(tele.getSubscriberId(subId));
2987 }
2988 } finally {
2989 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002990 }
2991
2992 // First pass, find a number override for an active subscriber
2993 String mergeNumber = null;
2994 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2995 for (String key : prefs.keySet()) {
2996 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2997 final String subscriberId = (String) prefs.get(key);
2998 if (activeSubscriberIds.contains(subscriberId)) {
2999 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
3000 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3001 mergeNumber = (String) prefs.get(numberKey);
3002 if (DBG_MERGE) {
3003 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3004 + " for active subscriber " + subscriberId);
3005 }
3006 if (!TextUtils.isEmpty(mergeNumber)) {
3007 break;
3008 }
3009 }
3010 }
3011 }
3012
3013 // Shortcut when no active merged subscribers
3014 if (TextUtils.isEmpty(mergeNumber)) {
3015 return null;
3016 }
3017
3018 // Second pass, find all subscribers under that line override
3019 final ArraySet<String> result = new ArraySet<>();
3020 for (String key : prefs.keySet()) {
3021 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3022 final String number = (String) prefs.get(key);
3023 if (mergeNumber.equals(number)) {
3024 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3025 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3026 final String subscriberId = (String) prefs.get(subscriberKey);
3027 if (!TextUtils.isEmpty(subscriberId)) {
3028 result.add(subscriberId);
3029 }
3030 }
3031 }
3032 }
3033
3034 final String[] resultArray = result.toArray(new String[result.size()]);
3035 Arrays.sort(resultArray);
3036 if (DBG_MERGE) {
3037 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3038 }
3039 return resultArray;
3040 }
3041
3042 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003043 public boolean setOperatorBrandOverride(int subId, String brand) {
3044 enforceCarrierPrivilege(subId);
3045 final Phone phone = getPhone(subId);
3046 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003047 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003048
3049 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003050 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003051 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3052 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003053 enforceCarrierPrivilege(subId);
3054 final Phone phone = getPhone(subId);
3055 if (phone == null) {
3056 return false;
3057 }
3058 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003059 cdmaNonRoamingList);
3060 }
3061
3062 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003063 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003064 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3065 enforceModifyPermission();
3066
3067 int returnValue = 0;
3068 try {
3069 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3070 if(result.exception == null) {
3071 if (result.result != null) {
3072 byte[] responseData = (byte[])(result.result);
3073 if(responseData.length > oemResp.length) {
3074 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3075 responseData.length + "bytes. Buffer Size is " +
3076 oemResp.length + "bytes.");
3077 }
3078 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3079 returnValue = responseData.length;
3080 }
3081 } else {
3082 CommandException ex = (CommandException) result.exception;
3083 returnValue = ex.getCommandError().ordinal();
3084 if(returnValue > 0) returnValue *= -1;
3085 }
3086 } catch (RuntimeException e) {
3087 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3088 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3089 if(returnValue > 0) returnValue *= -1;
3090 }
3091
3092 return returnValue;
3093 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003094
3095 @Override
3096 public void setRadioCapability(RadioAccessFamily[] rafs) {
3097 try {
3098 ProxyController.getInstance().setRadioCapability(rafs);
3099 } catch (RuntimeException e) {
3100 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3101 }
3102 }
3103
3104 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003105 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3106 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3107 return RadioAccessFamily.RAF_UNKNOWN;
3108 }
3109
Wink Saville5d475dd2014-10-17 15:00:58 -07003110 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3111 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003112
3113 @Override
3114 public void enableVideoCalling(boolean enable) {
3115 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003116 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003117 }
3118
3119 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003120 public boolean isVideoCallingEnabled(String callingPackage) {
3121 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3122 return false;
3123 }
3124
Andrew Lee77527ac2014-10-21 16:57:39 -07003125 // Check the user preference and the system-level IMS setting. Even if the user has
3126 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3127 // In the long run, we may instead need to check if there exists a connection service
3128 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003129 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3130 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003131 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003132 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003133
Andrew Leea1239f22015-03-02 17:44:07 -08003134 @Override
3135 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003136 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003137 }
3138
3139 @Override
3140 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003141 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003142 }
3143
Andrew Lee9431b832015-03-09 18:46:45 -07003144 @Override
3145 public boolean isTtyModeSupported() {
3146 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3147 TelephonyManager telephonyManager =
3148 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003149 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003150 }
3151
3152 @Override
3153 public boolean isHearingAidCompatibilitySupported() {
3154 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3155 }
3156
Sanket Padawe7310cc72015-01-14 09:53:20 -08003157 /**
3158 * Returns the unique device ID of phone, for example, the IMEI for
3159 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3160 *
3161 * <p>Requires Permission:
3162 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3163 */
3164 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003165 public String getDeviceId(String callingPackage) {
3166 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3167 return null;
3168 }
3169
Sanket Padawe7310cc72015-01-14 09:53:20 -08003170 final Phone phone = PhoneFactory.getPhone(0);
3171 if (phone != null) {
3172 return phone.getDeviceId();
3173 } else {
3174 return null;
3175 }
3176 }
3177
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003178 /*
3179 * {@hide}
3180 * Returns the IMS Registration Status
3181 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003182 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003183 public boolean isImsRegistered() {
3184 return mPhone.isImsRegistered();
3185 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003186
3187 @Override
3188 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3189 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3190 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003191
Nathan Haroldc55097a2015-03-11 18:14:50 -07003192 /*
3193 * {@hide}
3194 * Returns the IMS Registration Status
3195 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003196 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003197 return mPhone.isWifiCallingEnabled();
3198 }
3199
3200 /*
3201 * {@hide}
3202 * Returns the IMS Registration Status
3203 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003204 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003205 return mPhone.isVolteEnabled();
3206 }
Svet Ganovb320e182015-04-16 12:30:10 -07003207
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003208 /*
3209 * {@hide} Returns the IMS Registration Status
3210 */
3211 public boolean isVideoTelephonyAvailable() {
3212 return mPhone.isVideoEnabled();
3213 }
3214
Svet Ganovb320e182015-04-16 12:30:10 -07003215 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003216 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003217 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003218 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3219
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003220 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003221 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003222 } catch (SecurityException e) {
3223 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3224 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003225 }
Svet Ganovb320e182015-04-16 12:30:10 -07003226
3227 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3228 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3229 return false;
3230 }
3231
3232 return true;
3233 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003234
Makoto Onukifee69342015-06-29 14:44:50 -07003235 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003236 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003237 */
3238 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003239 // Default SMS app can always read it.
3240 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3241 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3242 return true;
3243 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003244
Makoto Onukie4072d12015-08-03 15:12:23 -07003245 try {
3246 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003247 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003248 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003249 // Can be read with READ_SMS too.
3250 try {
3251 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3252 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3253 if (opCode != AppOpsManager.OP_NONE) {
3254 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3255 == AppOpsManager.MODE_ALLOWED;
3256 } else {
3257 return true;
3258 }
3259 } catch (SecurityException readSmsSecurityException) {
3260 }
Chad Brubakerf342a982017-03-30 16:27:34 -07003261 // Can be read with READ_PHONE_NUMBERS too.
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003262 try {
Chad Brubakerf342a982017-03-30 16:27:34 -07003263 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBERS,
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003264 message);
Chad Brubakerf342a982017-03-30 16:27:34 -07003265 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBERS);
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003266 if (opCode != AppOpsManager.OP_NONE) {
3267 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3268 == AppOpsManager.MODE_ALLOWED;
3269 } else {
3270 return true;
3271 }
3272 } catch (SecurityException readPhoneNumberSecurityException) {
3273 }
3274
3275 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3276 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3277 ", " + android.Manifest.permission.READ_SMS + ", or " +
Chad Brubakerf342a982017-03-30 16:27:34 -07003278 android.Manifest.permission.READ_PHONE_NUMBERS);
Makoto Onukifee69342015-06-29 14:44:50 -07003279 }
3280
Stuart Scott8eef64f2015-04-08 15:13:54 -07003281 @Override
3282 public void factoryReset(int subId) {
3283 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003284 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3285 return;
3286 }
3287
Svet Ganovcc087f82015-05-12 20:35:54 -07003288 final long identity = Binder.clearCallingIdentity();
3289 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003290 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3291 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003292 // Enable data
3293 setDataEnabled(subId, true);
3294 // Set network selection mode to automatic
3295 setNetworkSelectionModeAutomatic(subId);
3296 // Set preferred mobile network type to the best available
3297 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3298 // Turn off roaming
Malcolm Chenf6b97f42017-04-19 16:03:24 -07003299 mPhone.setDataRoamingEnabled(false);
Svet Ganovcc087f82015-05-12 20:35:54 -07003300 }
3301 } finally {
3302 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003303 }
3304 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003305
3306 @Override
3307 public String getLocaleFromDefaultSim() {
3308 // We query all subscriptions instead of just the active ones, because
3309 // this might be called early on in the provisioning flow when the
3310 // subscriptions potentially aren't active yet.
3311 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3312 if (slist == null || slist.isEmpty()) {
3313 return null;
3314 }
3315
3316 // This function may be called very early, say, from the setup wizard, at
3317 // which point we won't have a default subscription set. If that's the case
3318 // we just choose the first, which will be valid in "most cases".
3319 final int defaultSubId = getDefaultSubscription();
3320 SubscriptionInfo info = null;
3321 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3322 info = slist.get(0);
3323 } else {
3324 for (SubscriptionInfo item : slist) {
3325 if (item.getSubscriptionId() == defaultSubId) {
3326 info = item;
3327 break;
3328 }
3329 }
3330
3331 if (info == null) {
3332 return null;
3333 }
3334 }
3335
3336 // Try and fetch the locale from the carrier properties or from the SIM language
3337 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003338 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003339 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003340 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003341 if (defaultPhone != null) {
3342 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3343 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003344 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003345 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3346 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003347 } else {
3348 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003349 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003350 }
3351 }
3352
Narayan Kamath011676f2015-07-29 12:04:08 +01003353 // The SIM language preferences only store a language (e.g. fr = French), not an
3354 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3355 // the SIM and carrier preferences does not include a country we add the country
3356 // determined from the SIM MCC to provide an exact locale.
3357 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003358 if (mccLocale != null) {
3359 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3360 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003361 }
3362
Tony Hill183b2de2015-06-24 14:53:58 +01003363 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003364 return null;
3365 }
3366
3367 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3368 final long identity = Binder.clearCallingIdentity();
3369 try {
3370 return mSubscriptionController.getAllSubInfoList(
3371 mPhone.getContext().getOpPackageName());
3372 } finally {
3373 Binder.restoreCallingIdentity(identity);
3374 }
3375 }
3376
3377 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3378 final long identity = Binder.clearCallingIdentity();
3379 try {
3380 return mSubscriptionController.getActiveSubscriptionInfoList(
3381 mPhone.getContext().getOpPackageName());
3382 } finally {
3383 Binder.restoreCallingIdentity(identity);
3384 }
3385 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003386
3387 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003388 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3389 * representing the state of the modem.
3390 *
3391 * NOTE: This clears the modem state, so there should only every be one caller.
3392 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003393 */
3394 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003395 public void requestModemActivityInfo(ResultReceiver result) {
3396 enforceModifyPermission();
3397
3398 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3399 Bundle bundle = new Bundle();
3400 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3401 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003402 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003403
3404 /**
3405 * {@hide}
3406 * Returns the service state information on specified subscription.
3407 */
3408 @Override
3409 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3410
3411 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3412 return null;
3413 }
3414
3415 final Phone phone = getPhone(subId);
3416 if (phone == null) {
3417 return null;
3418 }
3419
3420 return phone.getServiceState();
3421 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003422
3423 /**
3424 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3425 *
3426 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3427 * voicemail ringtone.
3428 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3429 * PhoneAccount.
3430 */
3431 @Override
3432 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003433 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003434 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003435 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003436 }
3437
3438 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3439 }
3440
3441 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003442 * Sets the per-account voicemail ringtone.
3443 *
3444 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3445 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3446 *
3447 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3448 * voicemail ringtone.
3449 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3450 * PhoneAccount.
3451 */
3452 @Override
3453 public void setVoicemailRingtoneUri(String callingPackage,
3454 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3455 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3456 if (!TextUtils.equals(callingPackage,
3457 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3458 enforceModifyPermissionOrCarrierPrivilege(
3459 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3460 }
3461 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3462 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003463 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003464 }
3465 VoicemailNotificationSettingsUtil.setRingtoneUri(phone, uri);
3466 }
3467
3468 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003469 * Returns whether vibration is set for voicemail notification in Phone settings.
3470 *
3471 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3472 * voicemail vibration setting.
3473 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3474 */
3475 @Override
3476 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003477 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003478 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003479 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003480 }
3481
3482 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3483 }
3484
Youhan Wange64578a2016-05-02 15:32:42 -07003485 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003486 * Sets the per-account voicemail vibration.
3487 *
3488 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3489 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3490 *
3491 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3492 * voicemail vibration setting.
3493 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3494 * specific PhoneAccount.
3495 */
3496 @Override
3497 public void setVoicemailVibrationEnabled(String callingPackage,
3498 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3499 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3500 if (!TextUtils.equals(callingPackage,
3501 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3502 enforceModifyPermissionOrCarrierPrivilege(
3503 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3504 }
3505
3506 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3507 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003508 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003509 }
3510 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone, enabled);
3511 }
3512
3513 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003514 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3515 *
3516 * @throws SecurityException if the caller does not have the required permission
3517 */
3518 private void enforceReadPrivilegedPermission() {
3519 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3520 null);
3521 }
3522
3523 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003524 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3525 * permission.
3526 *
3527 * @throws SecurityException if the caller does not have the required permission
3528 */
3529 private void enforceSendSmsPermission() {
3530 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3531 }
3532
3533 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003534 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003535 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003536 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003537 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003538 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Ta-wei Yen5d81b0c2017-03-03 16:32:26 -08003539 ComponentName componentName =
3540 RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
3541 if(componentName == null) {
3542 throw new SecurityException("Caller not current active visual voicemail package[null]");
3543 }
3544 String vvmPackage = componentName.getPackageName();
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003545 if (!callingPackage.equals(vvmPackage)) {
3546 throw new SecurityException("Caller not current active visual voicemail package[" +
3547 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003548 }
3549 }
3550
3551 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003552 * Return the application ID for the app type.
3553 *
3554 * @param subId the subscription ID that this request applies to.
3555 * @param appType the uicc app type.
3556 * @return Application ID for specificied app type, or null if no uicc.
3557 */
3558 @Override
3559 public String getAidForAppType(int subId, int appType) {
3560 enforceReadPrivilegedPermission();
3561 Phone phone = getPhone(subId);
3562 if (phone == null) {
3563 return null;
3564 }
3565 String aid = null;
3566 try {
3567 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3568 .getApplicationByType(appType).getAid();
3569 } catch (Exception e) {
3570 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3571 }
3572 return aid;
3573 }
3574
Youhan Wang4001d252016-05-11 10:29:41 -07003575 /**
3576 * Return the Electronic Serial Number.
3577 *
3578 * @param subId the subscription ID that this request applies to.
3579 * @return ESN or null if error.
3580 */
3581 @Override
3582 public String getEsn(int subId) {
3583 enforceReadPrivilegedPermission();
3584 Phone phone = getPhone(subId);
3585 if (phone == null) {
3586 return null;
3587 }
3588 String esn = null;
3589 try {
3590 esn = phone.getEsn();
3591 } catch (Exception e) {
3592 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3593 }
3594 return esn;
3595 }
3596
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003597 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003598 * Return the Preferred Roaming List Version.
3599 *
3600 * @param subId the subscription ID that this request applies to.
3601 * @return PRLVersion or null if error.
3602 */
3603 @Override
3604 public String getCdmaPrlVersion(int subId) {
3605 enforceReadPrivilegedPermission();
3606 Phone phone = getPhone(subId);
3607 if (phone == null) {
3608 return null;
3609 }
3610 String cdmaPrlVersion = null;
3611 try {
3612 cdmaPrlVersion = phone.getCdmaPrlVersion();
3613 } catch (Exception e) {
3614 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3615 }
3616 return cdmaPrlVersion;
3617 }
3618
3619 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003620 * Get snapshot of Telephony histograms
3621 * @return List of Telephony histograms
3622 * @hide
3623 */
3624 @Override
3625 public List<TelephonyHistogram> getTelephonyHistograms() {
3626 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3627 return RIL.getTelephonyRILTimingHistograms();
3628 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003629
3630 /**
3631 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003632 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003633 * Require system privileges. In the future we may add this to carrier APIs.
3634 *
3635 * @return The number of carriers set successfully, should match length of carriers
3636 */
3637 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003638 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003639 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003640
Meng Wang9b7c4e92017-02-17 11:41:27 -08003641 if (carriers == null) {
3642 throw new NullPointerException("carriers cannot be null");
3643 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003644
3645 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003646 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3647 return retVal[0];
3648 }
3649
3650 /**
3651 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003652 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003653 * Require system privileges. In the future we may add this to carrier APIs.
3654 *
3655 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3656 * means all carriers are allowed.
3657 */
3658 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003659 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003660 enforceReadPrivilegedPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003661 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003662 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3663 }
3664
fionaxu59545b42016-05-25 15:53:37 -07003665 /**
3666 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3667 * @param subId the subscription ID that this action applies to.
3668 * @param enabled control enable or disable metered apns.
3669 * {@hide}
3670 */
3671 @Override
3672 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3673 enforceModifyPermission();
3674 final Phone phone = getPhone(subId);
3675 if (phone == null) {
3676 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3677 return;
3678 }
3679 try {
3680 phone.carrierActionSetMeteredApnsEnabled(enabled);
3681 } catch (Exception e) {
3682 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3683 }
3684 }
3685
3686 /**
3687 * Action set from carrier signalling broadcast receivers to enable/disable radio
3688 * @param subId the subscription ID that this action applies to.
3689 * @param enabled control enable or disable radio.
3690 * {@hide}
3691 */
3692 @Override
3693 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3694 enforceModifyPermission();
3695 final Phone phone = getPhone(subId);
3696 if (phone == null) {
3697 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3698 return;
3699 }
3700 try {
3701 phone.carrierActionSetRadioEnabled(enabled);
3702 } catch (Exception e) {
3703 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3704 }
3705 }
3706
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003707 /**
3708 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3709 * bug report is being generated.
3710 */
3711 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003712 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003713 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3714 != PackageManager.PERMISSION_GRANTED) {
3715 writer.println("Permission Denial: can't dump Phone from pid="
3716 + Binder.getCallingPid()
3717 + ", uid=" + Binder.getCallingUid()
3718 + "without permission "
3719 + android.Manifest.permission.DUMP);
3720 return;
3721 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003722 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003723 }
Jack Yueb89b242016-06-22 13:27:47 -07003724
3725 /**
3726 * Get aggregated video call data usage from all subscriptions since boot.
3727 * @return total data usage in bytes
3728 * {@hide}
3729 */
3730 @Override
3731 public long getVtDataUsage() {
3732 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3733 null);
3734
3735 // NetworkStatsService keeps tracking the active network interface and identity. It will
3736 // record the delta with the corresponding network identity. What we need to do here is
3737 // returning total video call data usage from all subscriptions since boot.
3738
3739 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3740 // simultaneous VT calls.
3741 final Phone[] phones = PhoneFactory.getPhones();
3742 long total = 0;
3743 for (Phone phone : phones) {
3744 total += phone.getVtDataUsage();
3745 }
3746 return total;
3747 }
Jack Yu75ab2952016-07-08 14:29:33 -07003748
3749 /**
3750 * Policy control of data connection. Usually used when data limit is passed.
3751 * @param enabled True if enabling the data, otherwise disabling.
3752 * @param subId Subscription index
3753 * {@hide}
3754 */
3755 @Override
3756 public void setPolicyDataEnabled(boolean enabled, int subId) {
3757 enforceModifyPermission();
3758 Phone phone = getPhone(subId);
3759 if (phone != null) {
3760 phone.setPolicyDataEnabled(enabled);
3761 }
3762 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003763
3764 /**
3765 * Get Client request stats
3766 * @return List of Client Request Stats
3767 * @hide
3768 */
3769 @Override
3770 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3771 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3772 return null;
3773 }
3774
3775 Phone phone = getPhone(subId);
3776 if (phone != null) {
3777 return phone.getClientRequestStats();
3778 }
3779
3780 return null;
3781 }
3782
3783 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3784 if (workSource != null) {
3785 return workSource;
3786 }
3787
3788 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3789 workSource = new WorkSource(uid, packageName);
3790 return workSource;
3791 }
Jack Yueb4124c2017-02-16 15:32:43 -08003792
3793 /**
3794 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3795 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003796 * @param slotIndex SIM slot id.
Jack Yueb4124c2017-02-16 15:32:43 -08003797 * @param powerUp True if powering up the SIM, otherwise powering down
3798 *
3799 **/
3800 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003801 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yueb4124c2017-02-16 15:32:43 -08003802 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003803 Phone phone = PhoneFactory.getPhone(slotIndex);
3804
Jack Yueb4124c2017-02-16 15:32:43 -08003805 if (phone != null) {
3806 phone.setSimPowerState(powerUp);
3807 }
3808 }
Shuo Qiandd210312017-04-12 22:11:33 +00003809
3810 /**
3811 * Check if phone is in emergency callback mode
3812 * @return true if phone is in emergency callback mode
3813 * @param subId sub id
3814 */
3815 public boolean getEmergencyCallbackMode(int subId) {
3816 final Phone phone = getPhone(subId);
3817 if (phone != null) {
3818 return phone.isInEcm();
3819 } else {
3820 return false;
3821 }
3822 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003823}