blob: b70ef02da4653eb58575cc62498390f026980e6c [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;
Ta-wei Yendca928f2017-01-10 16:17:08 -080022import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.ActivityManager;
24import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080025import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070026import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.Context;
28import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070029import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080030import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070031import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.net.Uri;
33import android.os.AsyncResult;
34import android.os.Binder;
35import android.os.Bundle;
36import android.os.Handler;
yinxu504e1392017-04-12 16:03:22 -070037import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Looper;
39import android.os.Message;
yinxu504e1392017-04-12 16:03:22 -070040import android.os.Messenger;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070042import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.os.ServiceManager;
44import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070045import android.os.UserManager;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070046import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070047import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080048import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070049import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080050import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080051import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070052import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070053import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.telephony.CellInfo;
Sooraj Sasindran9a909312016-07-18 11:57:25 -070055import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070056import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070057import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080058import android.telephony.NeighboringCellInfo;
yinxu504e1392017-04-12 16:03:22 -070059import android.telephony.NetworkScanRequest;
Wink Saville5d475dd2014-10-17 15:00:58 -070060import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.telephony.ServiceState;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080062import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080063import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080064import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070065import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070066import android.telephony.TelephonyManager;
67import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080069import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080071import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080072import android.util.Slog;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080073
Andrew Lee312e8172014-10-23 17:01:36 -070074import com.android.ims.ImsManager;
Brad Ebinger51f743a2017-01-23 13:50:20 -080075import com.android.ims.internal.IImsServiceController;
76import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070077import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070078import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070079import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080082import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010083import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070084import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070085import com.android.internal.telephony.Phone;
Nathan Harolda667c152016-12-14 11:27:20 -080086import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070087import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070088import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070089import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070090import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070091import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080092import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070093import com.android.internal.telephony.uicc.IccIoResult;
94import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080095import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070096import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080097import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070098import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080099import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -0800100import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yen527a9c02017-01-06 15:29:25 -0800101import com.android.phone.vvm.RemoteVvmTaskManager;
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;
286 request.result = phone != null ?
287 phone.handleUssdRequest(ussdRequest, wrappedCallback)
288 :false;
289 // Wake up the requesting thread
290 synchronized (request) {
291 request.notifyAll();
292 }
293 break;
294 }
295
Yorke Lee716f67e2015-06-17 15:39:16 -0700296 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700297 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700298 final Phone phone = getPhoneFromRequest(request);
299 request.result = phone != null ?
300 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
301 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700302 // Wake up the requesting thread
303 synchronized (request) {
304 request.notifyAll();
305 }
306 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700307 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700308
309 case CMD_HANDLE_NEIGHBORING_CELL:
310 request = (MainThreadRequest) msg.obj;
311 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
312 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700313 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700314 break;
315
316 case EVENT_NEIGHBORING_CELL_DONE:
317 ar = (AsyncResult) msg.obj;
318 request = (MainThreadRequest) ar.userObj;
319 if (ar.exception == null && ar.result != null) {
320 request.result = ar.result;
321 } else {
322 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800323 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700324 }
325 // Wake up the requesting thread
326 synchronized (request) {
327 request.notifyAll();
328 }
329 break;
330
331 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700332 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800333 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700334 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700335 break;
336
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700337 case CMD_END_CALL:
338 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800339 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700340 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700341 Phone phone = getPhone(end_subId);
342 if (phone == null) {
343 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
344 break;
345 }
346 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
348 // CDMA: If the user presses the Power button we treat it as
349 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700350 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700351 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
352 // GSM: End the call as per the Phone state
353 hungUp = PhoneUtils.hangup(mCM);
354 } else {
355 throw new IllegalStateException("Unexpected phone type: " + phoneType);
356 }
357 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
358 request.result = hungUp;
359 // Wake up the requesting thread
360 synchronized (request) {
361 request.notifyAll();
362 }
363 break;
364
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700365 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700366 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700367 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800368 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700369 if (uiccCard == null) {
370 loge("iccTransmitApduLogicalChannel: No UICC");
371 request.result = new IccIoResult(0x6F, 0, (byte[])null);
372 synchronized (request) {
373 request.notifyAll();
374 }
375 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700376 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
377 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700378 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700379 iccArgument.channel, iccArgument.cla, iccArgument.command,
380 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700381 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700382 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700383 break;
384
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700385 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700386 ar = (AsyncResult) msg.obj;
387 request = (MainThreadRequest) ar.userObj;
388 if (ar.exception == null && ar.result != null) {
389 request.result = ar.result;
390 } else {
391 request.result = new IccIoResult(0x6F, 0, (byte[])null);
392 if (ar.result == null) {
393 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800394 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700395 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800396 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700397 } else {
398 loge("iccTransmitApduLogicalChannel: Unknown exception");
399 }
400 }
401 synchronized (request) {
402 request.notifyAll();
403 }
404 break;
405
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700406 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
407 request = (MainThreadRequest) msg.obj;
408 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800409 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700410 if (uiccCard == null) {
411 loge("iccTransmitApduBasicChannel: No UICC");
412 request.result = new IccIoResult(0x6F, 0, (byte[])null);
413 synchronized (request) {
414 request.notifyAll();
415 }
416 } else {
417 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
418 request);
419 uiccCard.iccTransmitApduBasicChannel(
420 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
421 iccArgument.p3, iccArgument.data, onCompleted);
422 }
423 break;
424
425 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
426 ar = (AsyncResult) msg.obj;
427 request = (MainThreadRequest) ar.userObj;
428 if (ar.exception == null && ar.result != null) {
429 request.result = ar.result;
430 } else {
431 request.result = new IccIoResult(0x6F, 0, (byte[])null);
432 if (ar.result == null) {
433 loge("iccTransmitApduBasicChannel: Empty response");
434 } else if (ar.exception instanceof CommandException) {
435 loge("iccTransmitApduBasicChannel: CommandException: " +
436 ar.exception);
437 } else {
438 loge("iccTransmitApduBasicChannel: Unknown exception");
439 }
440 }
441 synchronized (request) {
442 request.notifyAll();
443 }
444 break;
445
446 case CMD_EXCHANGE_SIM_IO:
447 request = (MainThreadRequest) msg.obj;
448 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800449 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700450 if (uiccCard == null) {
451 loge("iccExchangeSimIO: No UICC");
452 request.result = new IccIoResult(0x6F, 0, (byte[])null);
453 synchronized (request) {
454 request.notifyAll();
455 }
456 } else {
457 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
458 request);
459 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
460 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
461 iccArgument.data, onCompleted);
462 }
463 break;
464
465 case EVENT_EXCHANGE_SIM_IO_DONE:
466 ar = (AsyncResult) msg.obj;
467 request = (MainThreadRequest) ar.userObj;
468 if (ar.exception == null && ar.result != null) {
469 request.result = ar.result;
470 } else {
471 request.result = new IccIoResult(0x6f, 0, (byte[])null);
472 }
473 synchronized (request) {
474 request.notifyAll();
475 }
476 break;
477
Derek Tan4d5e5c12014-02-04 11:54:58 -0800478 case CMD_SEND_ENVELOPE:
479 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800480 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700481 if (uiccCard == null) {
482 loge("sendEnvelopeWithStatus: No UICC");
483 request.result = new IccIoResult(0x6F, 0, (byte[])null);
484 synchronized (request) {
485 request.notifyAll();
486 }
487 } else {
488 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
489 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
490 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800491 break;
492
493 case EVENT_SEND_ENVELOPE_DONE:
494 ar = (AsyncResult) msg.obj;
495 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700496 if (ar.exception == null && ar.result != null) {
497 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800498 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700499 request.result = new IccIoResult(0x6F, 0, (byte[])null);
500 if (ar.result == null) {
501 loge("sendEnvelopeWithStatus: Empty response");
502 } else if (ar.exception instanceof CommandException) {
503 loge("sendEnvelopeWithStatus: CommandException: " +
504 ar.exception);
505 } else {
506 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
507 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800508 }
509 synchronized (request) {
510 request.notifyAll();
511 }
512 break;
513
Shishir Agrawal566b7612013-10-28 14:41:00 -0700514 case CMD_OPEN_CHANNEL:
515 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800516 uiccCard = getUiccCardFromRequest(request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800517 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700518 if (uiccCard == null) {
519 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800520 request.result = new IccOpenLogicalChannelResponse(-1,
521 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700522 synchronized (request) {
523 request.notifyAll();
524 }
525 } else {
526 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambid7454d32015-12-03 13:50:00 -0800527 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
528 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700529 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700530 break;
531
532 case EVENT_OPEN_CHANNEL_DONE:
533 ar = (AsyncResult) msg.obj;
534 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700535 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700536 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700537 int[] result = (int[]) ar.result;
538 int channelId = result[0];
539 byte[] selectResponse = null;
540 if (result.length > 1) {
541 selectResponse = new byte[result.length - 1];
542 for (int i = 1; i < result.length; ++i) {
543 selectResponse[i - 1] = (byte) result[i];
544 }
545 }
546 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700547 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700548 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700549 if (ar.result == null) {
550 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700551 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700552 if (ar.exception != null) {
553 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
554 }
555
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700556 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700557 if (ar.exception instanceof CommandException) {
558 CommandException.Error error =
559 ((CommandException) (ar.exception)).getCommandError();
560 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700561 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700562 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700563 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700564 }
565 }
566 openChannelResp = new IccOpenLogicalChannelResponse(
567 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700568 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700569 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700570 synchronized (request) {
571 request.notifyAll();
572 }
573 break;
574
575 case CMD_CLOSE_CHANNEL:
576 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800577 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700578 if (uiccCard == null) {
579 loge("iccCloseLogicalChannel: No UICC");
580 request.result = new IccIoResult(0x6F, 0, (byte[])null);
581 synchronized (request) {
582 request.notifyAll();
583 }
584 } else {
585 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
586 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
587 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700588 break;
589
590 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800591 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
592 break;
593
594 case CMD_NV_READ_ITEM:
595 request = (MainThreadRequest) msg.obj;
596 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
597 mPhone.nvReadItem((Integer) request.argument, onCompleted);
598 break;
599
600 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700601 ar = (AsyncResult) msg.obj;
602 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800603 if (ar.exception == null && ar.result != null) {
604 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700605 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800606 request.result = "";
607 if (ar.result == null) {
608 loge("nvReadItem: Empty response");
609 } else if (ar.exception instanceof CommandException) {
610 loge("nvReadItem: CommandException: " +
611 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700612 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800613 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700614 }
615 }
616 synchronized (request) {
617 request.notifyAll();
618 }
619 break;
620
Jake Hambye994d462014-02-03 13:10:13 -0800621 case CMD_NV_WRITE_ITEM:
622 request = (MainThreadRequest) msg.obj;
623 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
624 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
625 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
626 break;
627
628 case EVENT_NV_WRITE_ITEM_DONE:
629 handleNullReturnEvent(msg, "nvWriteItem");
630 break;
631
632 case CMD_NV_WRITE_CDMA_PRL:
633 request = (MainThreadRequest) msg.obj;
634 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
635 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
636 break;
637
638 case EVENT_NV_WRITE_CDMA_PRL_DONE:
639 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
640 break;
641
642 case CMD_NV_RESET_CONFIG:
643 request = (MainThreadRequest) msg.obj;
644 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
645 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
646 break;
647
648 case EVENT_NV_RESET_CONFIG_DONE:
649 handleNullReturnEvent(msg, "nvResetConfig");
650 break;
651
Jake Hamby7c27be32014-03-03 13:25:59 -0800652 case CMD_GET_PREFERRED_NETWORK_TYPE:
653 request = (MainThreadRequest) msg.obj;
654 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700655 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800656 break;
657
658 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
659 ar = (AsyncResult) msg.obj;
660 request = (MainThreadRequest) ar.userObj;
661 if (ar.exception == null && ar.result != null) {
662 request.result = ar.result; // Integer
663 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800664 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800665 if (ar.result == null) {
666 loge("getPreferredNetworkType: Empty response");
667 } else if (ar.exception instanceof CommandException) {
668 loge("getPreferredNetworkType: CommandException: " +
669 ar.exception);
670 } else {
671 loge("getPreferredNetworkType: Unknown exception");
672 }
673 }
674 synchronized (request) {
675 request.notifyAll();
676 }
677 break;
678
679 case CMD_SET_PREFERRED_NETWORK_TYPE:
680 request = (MainThreadRequest) msg.obj;
681 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
682 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700683 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800684 break;
685
686 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
687 handleNullReturnEvent(msg, "setPreferredNetworkType");
688 break;
689
Steven Liu4bf01bc2014-07-17 11:05:29 -0500690 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
691 request = (MainThreadRequest)msg.obj;
692 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
693 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
694 break;
695
696 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
697 ar = (AsyncResult)msg.obj;
698 request = (MainThreadRequest)ar.userObj;
699 request.result = ar;
700 synchronized (request) {
701 request.notifyAll();
702 }
703 break;
704
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800705 case CMD_SET_VOICEMAIL_NUMBER:
706 request = (MainThreadRequest) msg.obj;
707 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
708 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800709 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
710 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800711 break;
712
713 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
714 handleNullReturnEvent(msg, "setVoicemailNumber");
715 break;
716
Stuart Scott54788802015-03-30 13:18:01 -0700717 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
718 request = (MainThreadRequest) msg.obj;
719 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
720 request);
721 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
722 break;
723
724 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
725 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
726 break;
727
Shishir Agrawal302c8692015-06-19 13:49:39 -0700728 case CMD_PERFORM_NETWORK_SCAN:
729 request = (MainThreadRequest) msg.obj;
730 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
731 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
732 break;
733
734 case EVENT_PERFORM_NETWORK_SCAN_DONE:
735 ar = (AsyncResult) msg.obj;
736 request = (MainThreadRequest) ar.userObj;
737 CellNetworkScanResult cellScanResult;
738 if (ar.exception == null && ar.result != null) {
739 cellScanResult = new CellNetworkScanResult(
740 CellNetworkScanResult.STATUS_SUCCESS,
741 (List<OperatorInfo>) ar.result);
742 } else {
743 if (ar.result == null) {
744 loge("getCellNetworkScanResults: Empty response");
745 }
746 if (ar.exception != null) {
747 loge("getCellNetworkScanResults: Exception: " + ar.exception);
748 }
749 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
750 if (ar.exception instanceof CommandException) {
751 CommandException.Error error =
752 ((CommandException) (ar.exception)).getCommandError();
753 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
754 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
755 } else if (error == CommandException.Error.GENERIC_FAILURE) {
756 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
757 }
758 }
759 cellScanResult = new CellNetworkScanResult(errorCode, null);
760 }
761 request.result = cellScanResult;
762 synchronized (request) {
763 request.notifyAll();
764 }
765 break;
766
767 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
768 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700769 ManualNetworkSelectionArgument selArg =
770 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700771 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
772 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700773 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
774 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700775 break;
776
777 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
778 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
779 break;
780
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700781 case CMD_GET_MODEM_ACTIVITY_INFO:
782 request = (MainThreadRequest) msg.obj;
783 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700784 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700785 break;
786
787 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
788 ar = (AsyncResult) msg.obj;
789 request = (MainThreadRequest) ar.userObj;
790 if (ar.exception == null && ar.result != null) {
791 request.result = ar.result;
792 } else {
793 if (ar.result == null) {
794 loge("queryModemActivityInfo: Empty response");
795 } else if (ar.exception instanceof CommandException) {
796 loge("queryModemActivityInfo: CommandException: " +
797 ar.exception);
798 } else {
799 loge("queryModemActivityInfo: Unknown exception");
800 }
801 }
Amit Mahajand4766222016-01-28 15:28:28 -0800802 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
803 if (request.result == null) {
804 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
805 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700806 synchronized (request) {
807 request.notifyAll();
808 }
809 break;
810
Meng Wang1a7c35a2016-05-05 20:56:15 -0700811 case CMD_SET_ALLOWED_CARRIERS:
812 request = (MainThreadRequest) msg.obj;
813 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
814 mPhone.setAllowedCarriers(
815 (List<CarrierIdentifier>) request.argument,
816 onCompleted);
817 break;
818
819 case EVENT_SET_ALLOWED_CARRIERS_DONE:
820 ar = (AsyncResult) msg.obj;
821 request = (MainThreadRequest) ar.userObj;
822 if (ar.exception == null && ar.result != null) {
823 request.result = ar.result;
824 } else {
825 if (ar.result == null) {
826 loge("setAllowedCarriers: Empty response");
827 } else if (ar.exception instanceof CommandException) {
828 loge("setAllowedCarriers: CommandException: " +
829 ar.exception);
830 } else {
831 loge("setAllowedCarriers: Unknown exception");
832 }
833 }
834 // Result cannot be null. Return -1 on error.
835 if (request.result == null) {
836 request.result = new int[]{-1};
837 }
838 synchronized (request) {
839 request.notifyAll();
840 }
841 break;
842
843 case CMD_GET_ALLOWED_CARRIERS:
844 request = (MainThreadRequest) msg.obj;
845 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
846 mPhone.getAllowedCarriers(onCompleted);
847 break;
848
849 case EVENT_GET_ALLOWED_CARRIERS_DONE:
850 ar = (AsyncResult) msg.obj;
851 request = (MainThreadRequest) ar.userObj;
852 if (ar.exception == null && ar.result != null) {
853 request.result = ar.result;
854 } else {
855 if (ar.result == null) {
856 loge("getAllowedCarriers: Empty response");
857 } else if (ar.exception instanceof CommandException) {
858 loge("getAllowedCarriers: CommandException: " +
859 ar.exception);
860 } else {
861 loge("getAllowedCarriers: Unknown exception");
862 }
863 }
864 // Result cannot be null. Return empty list of CarrierIdentifier.
865 if (request.result == null) {
866 request.result = new ArrayList<CarrierIdentifier>(0);
867 }
868 synchronized (request) {
869 request.notifyAll();
870 }
871 break;
872
Nathan Haroldb3014052017-01-25 15:57:32 -0800873 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
874 ar = (AsyncResult) msg.obj;
875 request = (MainThreadRequest) ar.userObj;
876 if (ar.exception == null && ar.result != null) {
877 request.result = ar.result;
878 } else {
879 request.result = new IllegalArgumentException(
880 "Failed to retrieve Forbidden Plmns");
881 if (ar.result == null) {
882 loge("getForbiddenPlmns: Empty response");
883 } else {
884 loge("getForbiddenPlmns: Unknown exception");
885 }
886 }
887 synchronized (request) {
888 request.notifyAll();
889 }
890 break;
891
892 case CMD_GET_FORBIDDEN_PLMNS:
893 request = (MainThreadRequest) msg.obj;
894 uiccCard = getUiccCardFromRequest(request);
895 if (uiccCard == null) {
896 loge("getForbiddenPlmns() UiccCard is null");
897 request.result = new IllegalArgumentException(
898 "getForbiddenPlmns() UiccCard is null");
899 synchronized (request) {
900 request.notifyAll();
901 }
902 break;
903 }
904 Integer appType = (Integer) request.argument;
905 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
906 if (uiccApp == null) {
907 loge("getForbiddenPlmns() no app with specified type -- "
908 + appType);
909 request.result = new IllegalArgumentException("Failed to get UICC App");
910 synchronized (request) {
911 request.notifyAll();
912 }
913 break;
914 } else {
915 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
916 + " specified type -- " + appType);
917 }
918 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
919 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
920 onCompleted);
921 break;
922
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700923 default:
924 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
925 break;
926 }
927 }
Jake Hambye994d462014-02-03 13:10:13 -0800928
929 private void handleNullReturnEvent(Message msg, String command) {
930 AsyncResult ar = (AsyncResult) msg.obj;
931 MainThreadRequest request = (MainThreadRequest) ar.userObj;
932 if (ar.exception == null) {
933 request.result = true;
934 } else {
935 request.result = false;
936 if (ar.exception instanceof CommandException) {
937 loge(command + ": CommandException: " + ar.exception);
938 } else {
939 loge(command + ": Unknown exception");
940 }
941 }
942 synchronized (request) {
943 request.notifyAll();
944 }
945 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 }
947
948 /**
949 * Posts the specified command to be executed on the main thread,
950 * waits for the request to complete, and returns the result.
951 * @see #sendRequestAsync
952 */
953 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800954 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700955 }
956
957 /**
958 * Posts the specified command to be executed on the main thread,
959 * waits for the request to complete, and returns the result.
960 * @see #sendRequestAsync
961 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800962 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
964 throw new RuntimeException("This method will deadlock if called from the main thread.");
965 }
966
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800967 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 Message msg = mMainThreadHandler.obtainMessage(command, request);
969 msg.sendToTarget();
970
971 // Wait for the request to complete
972 synchronized (request) {
973 while (request.result == null) {
974 try {
975 request.wait();
976 } catch (InterruptedException e) {
977 // Do nothing, go back and wait until the request is complete
978 }
979 }
980 }
981 return request.result;
982 }
983
984 /**
985 * Asynchronous ("fire and forget") version of sendRequest():
986 * Posts the specified command to be executed on the main thread, and
987 * returns immediately.
988 * @see #sendRequest
989 */
990 private void sendRequestAsync(int command) {
991 mMainThreadHandler.sendEmptyMessage(command);
992 }
993
994 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700995 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
996 * @see {@link #sendRequest(int,Object)}
997 */
998 private void sendRequestAsync(int command, Object argument) {
999 MainThreadRequest request = new MainThreadRequest(argument);
1000 Message msg = mMainThreadHandler.obtainMessage(command, request);
1001 msg.sendToTarget();
1002 }
1003
1004 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001005 * Initialize the singleton PhoneInterfaceManager instance.
1006 * This is only done once, at startup, from PhoneApp.onCreate().
1007 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001008 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001009 synchronized (PhoneInterfaceManager.class) {
1010 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001011 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001012 } else {
1013 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1014 }
1015 return sInstance;
1016 }
1017 }
1018
1019 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001020 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001021 mApp = app;
1022 mPhone = phone;
1023 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001024 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001025 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1026 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001027 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001028 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001029 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001030
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001031 publish();
1032 }
1033
1034 private void publish() {
1035 if (DBG) log("publish: " + this);
1036
1037 ServiceManager.addService("phone", this);
1038 }
1039
Stuart Scott584921c2015-01-15 17:10:34 -08001040 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001041 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1042 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001043 }
1044
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001045 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1046 Phone phone = getPhoneFromRequest(request);
1047 return phone == null ? null :
1048 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1049 }
1050
Wink Saville36469e72014-06-11 15:17:00 -07001051 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001052 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001053 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001054 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055 //
1056 // Implementation of the ITelephony interface.
1057 //
1058
1059 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001060 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001061 }
1062
Wink Savilleb564aae2014-10-23 10:18:09 -07001063 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001064 if (DBG) log("dial: " + number);
1065 // No permission check needed here: This is just a wrapper around the
1066 // ACTION_DIAL intent, which is available to any app since it puts up
1067 // the UI before it does anything.
1068
1069 String url = createTelUrl(number);
1070 if (url == null) {
1071 return;
1072 }
1073
1074 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001075 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001076 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1077 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1078 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1079 mApp.startActivity(intent);
1080 }
1081 }
1082
1083 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001084 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001085 }
1086
Wink Savilleb564aae2014-10-23 10:18:09 -07001087 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001088 if (DBG) log("call: " + number);
1089
1090 // This is just a wrapper around the ACTION_CALL intent, but we still
1091 // need to do a permission check since we're calling startActivity()
1092 // from the context of the phone app.
1093 enforceCallPermission();
1094
1095 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1096 != AppOpsManager.MODE_ALLOWED) {
1097 return;
1098 }
1099
1100 String url = createTelUrl(number);
1101 if (url == null) {
1102 return;
1103 }
1104
Wink Saville08874612014-08-31 19:19:58 -07001105 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001106 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001107 if (slist != null) {
1108 for (SubscriptionInfo subInfoRecord : slist) {
1109 if (subInfoRecord.getSubscriptionId() == subId) {
1110 isValid = true;
1111 break;
1112 }
Wink Saville08874612014-08-31 19:19:58 -07001113 }
1114 }
1115 if (isValid == false) {
1116 return;
1117 }
1118
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001119 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001120 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001121 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1122 mApp.startActivity(intent);
1123 }
1124
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 /**
1126 * End a call based on call state
1127 * @return true is a call was ended
1128 */
1129 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001130 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001131 }
1132
1133 /**
1134 * End a call based on the call state of the subId
1135 * @return true is a call was ended
1136 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001137 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001138 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001139 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001140 }
1141
1142 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001143 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001144 }
1145
Wink Savilleb564aae2014-10-23 10:18:09 -07001146 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001147 if (DBG) log("answerRingingCall...");
1148 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1149 // but that can probably wait till the big TelephonyManager API overhaul.
1150 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1151 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001152 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001153 }
1154
1155 /**
1156 * Make the actual telephony calls to implement answerRingingCall().
1157 * This should only be called from the main thread of the Phone app.
1158 * @see #answerRingingCall
1159 *
1160 * TODO: it would be nice to return true if we answered the call, or
1161 * false if there wasn't actually a ringing incoming call, or some
1162 * other error occurred. (In other words, pass back the return value
1163 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1164 * But that would require calling this method via sendRequest() rather
1165 * than sendRequestAsync(), and right now we don't actually *need* that
1166 * return value, so let's just return void for now.
1167 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001168 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001169 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001171 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1172 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001173 if (hasActiveCall && hasHoldingCall) {
1174 // Both lines are in use!
1175 // TODO: provide a flag to let the caller specify what
1176 // policy to use if both lines are in use. (The current
1177 // behavior is hardwired to "answer incoming, end ongoing",
1178 // which is how the CALL button is specced to behave.)
1179 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1180 return;
1181 } else {
1182 // answerCall() will automatically hold the current active
1183 // call, if there is one.
1184 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1185 return;
1186 }
1187 } else {
1188 // No call was ringing.
1189 return;
1190 }
1191 }
1192
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001194 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001196 public void silenceRinger() {
1197 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001198 }
1199
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001200 @Override
1201 public boolean isOffhook(String callingPackage) {
1202 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001203 }
1204
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001205 @Override
1206 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1207 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1208 return false;
1209 }
1210
Sanket Padawe356d7632015-06-22 14:03:32 -07001211 final Phone phone = getPhone(subId);
1212 if (phone != null) {
1213 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1214 } else {
1215 return false;
1216 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001217 }
1218
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001219 @Override
1220 public boolean isRinging(String callingPackage) {
1221 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001222 }
1223
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001224 @Override
1225 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1226 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1227 return false;
1228 }
1229
Sanket Padawe356d7632015-06-22 14:03:32 -07001230 final Phone phone = getPhone(subId);
1231 if (phone != null) {
1232 return (phone.getState() == PhoneConstants.State.RINGING);
1233 } else {
1234 return false;
1235 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001236 }
1237
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001238 @Override
1239 public boolean isIdle(String callingPackage) {
1240 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001241 }
1242
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001243 @Override
1244 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1245 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1246 return false;
1247 }
1248
Sanket Padawe356d7632015-06-22 14:03:32 -07001249 final Phone phone = getPhone(subId);
1250 if (phone != null) {
1251 return (phone.getState() == PhoneConstants.State.IDLE);
1252 } else {
1253 return false;
1254 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001255 }
1256
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001257 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001258 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001259 }
1260
Wink Savilleb564aae2014-10-23 10:18:09 -07001261 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001262 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001263 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1264 }
1265
1266 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001267 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001268 }
1269
Wink Savilleb564aae2014-10-23 10:18:09 -07001270 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001271 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001272 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1273 }
1274
1275 /** {@hide} */
1276 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001277 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001278 }
1279
Wink Savilleb564aae2014-10-23 10:18:09 -07001280 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001281 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001282 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001283 checkSimPin.start();
1284 return checkSimPin.unlockSim(null, pin);
1285 }
1286
Wink Saville9de0f752013-10-22 19:04:03 -07001287 /** {@hide} */
1288 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001289 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001290 }
1291
Wink Savilleb564aae2014-10-23 10:18:09 -07001292 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001293 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001294 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001295 checkSimPuk.start();
1296 return checkSimPuk.unlockSim(puk, pin);
1297 }
1298
1299 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001300 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001301 * a synchronous one.
1302 */
1303 private static class UnlockSim extends Thread {
1304
1305 private final IccCard mSimCard;
1306
1307 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001308 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1309 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001310
1311 // For replies from SimCard interface
1312 private Handler mHandler;
1313
1314 // For async handler to identify request type
1315 private static final int SUPPLY_PIN_COMPLETE = 100;
1316
1317 public UnlockSim(IccCard simCard) {
1318 mSimCard = simCard;
1319 }
1320
1321 @Override
1322 public void run() {
1323 Looper.prepare();
1324 synchronized (UnlockSim.this) {
1325 mHandler = new Handler() {
1326 @Override
1327 public void handleMessage(Message msg) {
1328 AsyncResult ar = (AsyncResult) msg.obj;
1329 switch (msg.what) {
1330 case SUPPLY_PIN_COMPLETE:
1331 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1332 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001333 mRetryCount = msg.arg1;
1334 if (ar.exception != null) {
1335 if (ar.exception instanceof CommandException &&
1336 ((CommandException)(ar.exception)).getCommandError()
1337 == CommandException.Error.PASSWORD_INCORRECT) {
1338 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1339 } else {
1340 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1341 }
1342 } else {
1343 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1344 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001345 mDone = true;
1346 UnlockSim.this.notifyAll();
1347 }
1348 break;
1349 }
1350 }
1351 };
1352 UnlockSim.this.notifyAll();
1353 }
1354 Looper.loop();
1355 }
1356
1357 /*
1358 * Use PIN or PUK to unlock SIM card
1359 *
1360 * If PUK is null, unlock SIM card with PIN
1361 *
1362 * If PUK is not null, unlock SIM card with PUK and set PIN code
1363 */
Wink Saville9de0f752013-10-22 19:04:03 -07001364 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001365
1366 while (mHandler == null) {
1367 try {
1368 wait();
1369 } catch (InterruptedException e) {
1370 Thread.currentThread().interrupt();
1371 }
1372 }
1373 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1374
1375 if (puk == null) {
1376 mSimCard.supplyPin(pin, callback);
1377 } else {
1378 mSimCard.supplyPuk(puk, pin, callback);
1379 }
1380
1381 while (!mDone) {
1382 try {
1383 Log.d(LOG_TAG, "wait for done");
1384 wait();
1385 } catch (InterruptedException e) {
1386 // Restore the interrupted status
1387 Thread.currentThread().interrupt();
1388 }
1389 }
1390 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001391 int[] resultArray = new int[2];
1392 resultArray[0] = mResult;
1393 resultArray[1] = mRetryCount;
1394 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001395 }
1396 }
1397
1398 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001399 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001400
1401 }
1402
Wink Savilleb564aae2014-10-23 10:18:09 -07001403 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 // No permission check needed here: this call is harmless, and it's
1405 // needed for the ServiceState.requestStateUpdate() call (which is
1406 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001407 final Phone phone = getPhone(subId);
1408 if (phone != null) {
1409 phone.updateServiceLocation();
1410 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001411 }
1412
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001413 @Override
1414 public boolean isRadioOn(String callingPackage) {
1415 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001416 }
1417
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001418 @Override
1419 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1420 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1421 return false;
1422 }
1423 return isRadioOnForSubscriber(subId);
1424 }
1425
1426 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001427 final Phone phone = getPhone(subId);
1428 if (phone != null) {
1429 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1430 } else {
1431 return false;
1432 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 }
1434
1435 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001436 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001437
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 }
Wink Saville36469e72014-06-11 15:17:00 -07001439
Wink Savilleb564aae2014-10-23 10:18:09 -07001440 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001441 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001442 final Phone phone = getPhone(subId);
1443 if (phone != null) {
1444 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1445 }
Wink Saville36469e72014-06-11 15:17:00 -07001446 }
1447
1448 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001449 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001450 }
1451
Wink Savilleb564aae2014-10-23 10:18:09 -07001452 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001453 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001454 final Phone phone = getPhone(subId);
1455 if (phone == null) {
1456 return false;
1457 }
1458 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001459 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001460 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001461 }
1462 return true;
1463 }
Wink Saville36469e72014-06-11 15:17:00 -07001464
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001465 public boolean needMobileRadioShutdown() {
1466 /*
1467 * If any of the Radios are available, it will need to be
1468 * shutdown. So return true if any Radio is available.
1469 */
1470 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1471 Phone phone = PhoneFactory.getPhone(i);
1472 if (phone != null && phone.isRadioAvailable()) return true;
1473 }
1474 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1475 return false;
1476 }
1477
1478 public void shutdownMobileRadios() {
1479 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1480 logv("Shutting down Phone " + i);
1481 shutdownRadioUsingPhoneId(i);
1482 }
1483 }
1484
1485 private void shutdownRadioUsingPhoneId(int phoneId) {
1486 enforceModifyPermission();
1487 Phone phone = PhoneFactory.getPhone(phoneId);
1488 if (phone != null && phone.isRadioAvailable()) {
1489 phone.shutdownRadio();
1490 }
1491 }
1492
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001493 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001494 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1495 if (defaultPhone != null) {
1496 defaultPhone.setRadioPower(turnOn);
1497 return true;
1498 } else {
1499 loge("There's no default phone.");
1500 return false;
1501 }
Wink Saville36469e72014-06-11 15:17:00 -07001502 }
1503
Wink Savilleb564aae2014-10-23 10:18:09 -07001504 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001505 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001506 final Phone phone = getPhone(subId);
1507 if (phone != null) {
1508 phone.setRadioPower(turnOn);
1509 return true;
1510 } else {
1511 return false;
1512 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001513 }
1514
Wink Saville36469e72014-06-11 15:17:00 -07001515 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001516 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001517 public boolean enableDataConnectivity() {
1518 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001519 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001520 final Phone phone = getPhone(subId);
1521 if (phone != null) {
1522 phone.setDataEnabled(true);
1523 return true;
1524 } else {
1525 return false;
1526 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001527 }
1528
Wink Saville36469e72014-06-11 15:17:00 -07001529 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001530 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001531 public boolean disableDataConnectivity() {
1532 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001533 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001534 final Phone phone = getPhone(subId);
1535 if (phone != null) {
1536 phone.setDataEnabled(false);
1537 return true;
1538 } else {
1539 return false;
1540 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001541 }
1542
Wink Saville36469e72014-06-11 15:17:00 -07001543 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001544 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001546 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001547 final Phone phone = getPhone(subId);
1548 if (phone != null) {
1549 return phone.isDataConnectivityPossible();
1550 } else {
1551 return false;
1552 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001553 }
1554
1555 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001556 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001557 }
1558
pkanwarae03a6b2016-11-06 20:37:09 -08001559 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001560 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001561 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1562 return;
1563 }
1564 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1565 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1566 };
1567
1568
Wink Savilleb564aae2014-10-23 10:18:09 -07001569 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001570 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001571 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1572 return false;
1573 }
Wink Saville36469e72014-06-11 15:17:00 -07001574 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001575 }
1576
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001577 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001578 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001579 }
1580
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001581 public int getCallStateForSlot(int slotIndex) {
1582 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001583 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harolda667c152016-12-14 11:27:20 -08001584 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001585 }
1586
Sanket Padawe356d7632015-06-22 14:03:32 -07001587 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001588 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001589 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001590 if (phone != null) {
Nathan Harolda667c152016-12-14 11:27:20 -08001591 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001592 } else {
Nathan Harolda667c152016-12-14 11:27:20 -08001593 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001594 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001595 }
1596
Sanket Padawe356d7632015-06-22 14:03:32 -07001597 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001598 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001599 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001600 if (phone != null) {
1601 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1602 } else {
1603 return TelephonyManager.DATA_ACTIVITY_NONE;
1604 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001605 }
1606
1607 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001608 public Bundle getCellLocation(String callingPackage) {
1609 enforceFineOrCoarseLocationPermission("getCellLocation");
1610
1611 // OP_COARSE_LOCATION controls both fine and coarse location.
1612 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1613 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001614 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001615 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001616 }
1617
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001618 if (checkIfCallerIsSelfOrForegroundUser() ||
1619 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001620 if (DBG_LOC) log("getCellLocation: is active user");
1621 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001622 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001623 if (phone == null) {
1624 return null;
1625 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001626
1627 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1628 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001629 return data;
1630 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001631 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001632 return null;
1633 }
1634 }
1635
Svetoslav64fad262015-04-14 14:35:21 -07001636 private void enforceFineOrCoarseLocationPermission(String message) {
1637 try {
1638 mApp.enforceCallingOrSelfPermission(
1639 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1640 } catch (SecurityException e) {
1641 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1642 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1643 // is the weaker precondition
1644 mApp.enforceCallingOrSelfPermission(
1645 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1646 }
1647 }
1648
1649
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001650 @Override
1651 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001652 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001653 }
1654
Sanket Padawe356d7632015-06-22 14:03:32 -07001655 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001656 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 mApp.enforceCallingOrSelfPermission(
1658 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001659 final Phone phone = getPhone(subId);
1660 if (phone != null) {
1661 phone.enableLocationUpdates();
1662 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001663 }
1664
1665 @Override
1666 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001667 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001668 }
1669
Sanket Padawe356d7632015-06-22 14:03:32 -07001670 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001671 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001672 mApp.enforceCallingOrSelfPermission(
1673 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001674 final Phone phone = getPhone(subId);
1675 if (phone != null) {
1676 phone.disableLocationUpdates();
1677 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001678 }
1679
1680 @Override
1681 @SuppressWarnings("unchecked")
1682 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001683 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1684
1685 // OP_COARSE_LOCATION controls both fine and coarse location.
1686 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1687 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1688 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001689 }
1690
1691 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1692 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1693 return null;
1694 }
Svetoslav64fad262015-04-14 14:35:21 -07001695
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001696 if (checkIfCallerIsSelfOrForegroundUser() ||
1697 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001698 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1699
1700 ArrayList<NeighboringCellInfo> cells = null;
1701
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001702 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001703 try {
1704 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001705 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001706 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001707 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001708 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001709 }
1710 return cells;
1711 } else {
1712 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1713 return null;
1714 }
1715 }
1716
1717
1718 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001719 public List<CellInfo> getAllCellInfo(String callingPackage) {
1720 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1721
1722 // OP_COARSE_LOCATION controls both fine and coarse location.
1723 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1724 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1725 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001726 }
1727
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001728 if (checkIfCallerIsSelfOrForegroundUser() ||
1729 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001730 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001731 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001732 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1733 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001734 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1735 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001736 }
1737 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001738 } else {
1739 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1740 return null;
1741 }
1742 }
1743
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001744 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001745 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08001746 enforceModifyPermission();
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001747 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1748 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001749 }
1750
Shishir Agrawala9f32182016-04-12 12:00:16 -07001751 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001752 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001753 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1754 return null;
1755 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001756 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001757 return phone == null ? null : phone.getImei();
1758 }
1759
1760 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07001761 public String getMeidForSlot(int slotIndex, String callingPackage) {
1762 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1763 return null;
1764 }
1765 Phone phone = PhoneFactory.getPhone(slotIndex);
1766 return phone == null ? null : phone.getMeid();
1767 }
1768
1769 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001770 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001771 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1772 return null;
1773 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001774 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001775 return phone == null ? null : phone.getDeviceSvn();
1776 }
1777
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001778 //
1779 // Internal helper methods.
1780 //
1781
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001782 /**
1783 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1784 */
1785 private boolean checkCallerInteractAcrossUsersFull() {
1786 return mPhone.getContext().checkCallingOrSelfPermission(
1787 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1788 == PackageManager.PERMISSION_GRANTED;
1789 }
1790
Jake Hambye994d462014-02-03 13:10:13 -08001791 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001792 boolean ok;
1793
1794 boolean self = Binder.getCallingUid() == Process.myUid();
1795 if (!self) {
1796 // Get the caller's user id then clear the calling identity
1797 // which will be restored in the finally clause.
1798 int callingUser = UserHandle.getCallingUserId();
1799 long ident = Binder.clearCallingIdentity();
1800
1801 try {
1802 // With calling identity cleared the current user is the foreground user.
1803 int foregroundUser = ActivityManager.getCurrentUser();
1804 ok = (foregroundUser == callingUser);
1805 if (DBG_LOC) {
1806 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1807 + " callingUser=" + callingUser + " ok=" + ok);
1808 }
1809 } catch (Exception ex) {
1810 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1811 ok = false;
1812 } finally {
1813 Binder.restoreCallingIdentity(ident);
1814 }
1815 } else {
1816 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1817 ok = true;
1818 }
1819 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1820 return ok;
1821 }
1822
1823 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001824 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1825 *
1826 * @throws SecurityException if the caller does not have the required permission
1827 */
1828 private void enforceModifyPermission() {
1829 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1830 }
1831
1832 /**
Junda Liua2e36012014-07-09 18:30:01 -07001833 * Make sure either system app or the caller has carrier privilege.
1834 *
1835 * @throws SecurityException if the caller does not have the required permission/privilege
1836 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001837 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001838 int permission = mApp.checkCallingOrSelfPermission(
1839 android.Manifest.permission.MODIFY_PHONE_STATE);
1840 if (permission == PackageManager.PERMISSION_GRANTED) {
1841 return;
1842 }
1843
1844 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001845 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001846 }
1847
1848 /**
1849 * Make sure the caller has carrier privilege.
1850 *
1851 * @throws SecurityException if the caller does not have the required permission
1852 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001853 private void enforceCarrierPrivilege(int subId) {
1854 if (getCarrierPrivilegeStatus(subId) !=
1855 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001856 loge("No Carrier Privilege.");
1857 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001858 }
1859 }
1860
1861 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001862 * Make sure the caller has the CALL_PHONE permission.
1863 *
1864 * @throws SecurityException if the caller does not have the required permission
1865 */
1866 private void enforceCallPermission() {
1867 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1868 }
1869
Stuart Scott8eef64f2015-04-08 15:13:54 -07001870 private void enforceConnectivityInternalPermission() {
1871 mApp.enforceCallingOrSelfPermission(
1872 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1873 "ConnectivityService");
1874 }
1875
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001876 private String createTelUrl(String number) {
1877 if (TextUtils.isEmpty(number)) {
1878 return null;
1879 }
1880
Jake Hambye994d462014-02-03 13:10:13 -08001881 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001882 }
1883
Ihab Awadf9e92732013-12-05 18:02:52 -08001884 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001885 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1886 }
1887
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001888 private static void logv(String msg) {
1889 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1890 }
1891
Ihab Awadf9e92732013-12-05 18:02:52 -08001892 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001893 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1894 }
1895
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001896 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001897 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001898 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001899 }
1900
Sanket Padawe356d7632015-06-22 14:03:32 -07001901 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001902 public int getActivePhoneTypeForSlot(int slotIndex) {
1903 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001904 if (phone == null) {
1905 return PhoneConstants.PHONE_TYPE_NONE;
1906 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001907 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001908 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001909 }
1910
1911 /**
1912 * Returns the CDMA ERI icon index to display
1913 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001914 @Override
1915 public int getCdmaEriIconIndex(String callingPackage) {
1916 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001917 }
1918
Sanket Padawe356d7632015-06-22 14:03:32 -07001919 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001920 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1921 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1922 return -1;
1923 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001924 final Phone phone = getPhone(subId);
1925 if (phone != null) {
1926 return phone.getCdmaEriIconIndex();
1927 } else {
1928 return -1;
1929 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001930 }
1931
1932 /**
1933 * Returns the CDMA ERI icon mode,
1934 * 0 - ON
1935 * 1 - FLASHING
1936 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001937 @Override
1938 public int getCdmaEriIconMode(String callingPackage) {
1939 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001940 }
1941
Sanket Padawe356d7632015-06-22 14:03:32 -07001942 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001943 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1944 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1945 return -1;
1946 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001947 final Phone phone = getPhone(subId);
1948 if (phone != null) {
1949 return phone.getCdmaEriIconMode();
1950 } else {
1951 return -1;
1952 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001953 }
1954
1955 /**
1956 * Returns the CDMA ERI text,
1957 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001958 @Override
1959 public String getCdmaEriText(String callingPackage) {
1960 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001961 }
1962
Sanket Padawe356d7632015-06-22 14:03:32 -07001963 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001964 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1965 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1966 return null;
1967 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001968 final Phone phone = getPhone(subId);
1969 if (phone != null) {
1970 return phone.getCdmaEriText();
1971 } else {
1972 return null;
1973 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001974 }
1975
1976 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001977 * Returns the CDMA MDN.
1978 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001979 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001980 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001981 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001982 final Phone phone = getPhone(subId);
1983 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1984 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001985 } else {
1986 return null;
1987 }
1988 }
1989
1990 /**
1991 * Returns the CDMA MIN.
1992 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001993 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001994 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001995 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001996 final Phone phone = getPhone(subId);
1997 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1998 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001999 } else {
2000 return null;
2001 }
2002 }
2003
2004 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002005 * Returns true if CDMA provisioning needs to run.
2006 */
2007 public boolean needsOtaServiceProvisioning() {
2008 return mPhone.needsOtaServiceProvisioning();
2009 }
2010
2011 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002012 * Sets the voice mail number of a given subId.
2013 */
2014 @Override
2015 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002016 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002017 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2018 new Pair<String, String>(alphaTag, number), new Integer(subId));
2019 return success;
2020 }
2021
Ta-wei Yen87c49842016-05-13 21:19:52 -07002022 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002023 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002024 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2025 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2026 return null;
2027 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002028 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2029 }
2030
2031 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002032 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2033 VisualVoicemailSmsFilterSettings settings) {
2034 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002035 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002036 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2037 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002038 }
2039
2040 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002041 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2042 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002043 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002044 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002045 }
2046
2047 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002048 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2049 String callingPackage, int subId) {
2050 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002051 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002052 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002053 }
2054
2055 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002056 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002057 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002058 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002059 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2060 }
2061
2062 @Override
2063 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2064 String number, int port, String text, PendingIntent sentIntent) {
2065 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002066 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002067 enforceSendSmsPermission();
2068 // Make the calls as the phone process.
2069 final long identity = Binder.clearCallingIdentity();
2070 try {
2071 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2072 if (port == 0) {
2073 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2074 sentIntent, null, false);
2075 } else {
2076 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2077 smsManager.sendDataMessageWithSelfPermissions(number, null,
2078 (short) port, data, sentIntent, null);
2079 }
2080 } finally {
2081 Binder.restoreCallingIdentity(identity);
2082 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002083 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002084 /**
fionaxu0152e512016-11-14 13:36:14 -08002085 * Sets the voice activation state of a given subId.
2086 */
2087 @Override
2088 public void setVoiceActivationState(int subId, int activationState) {
2089 enforceModifyPermissionOrCarrierPrivilege(subId);
2090 final Phone phone = getPhone(subId);
2091 if (phone != null) {
2092 phone.setVoiceActivationState(activationState);
2093 } else {
2094 loge("setVoiceActivationState fails with invalid subId: " + subId);
2095 }
2096 }
2097
2098 /**
2099 * Sets the data activation state of a given subId.
2100 */
2101 @Override
2102 public void setDataActivationState(int subId, int activationState) {
2103 enforceModifyPermissionOrCarrierPrivilege(subId);
2104 final Phone phone = getPhone(subId);
2105 if (phone != null) {
2106 phone.setDataActivationState(activationState);
2107 } else {
2108 loge("setVoiceActivationState fails with invalid subId: " + subId);
2109 }
2110 }
2111
2112 /**
2113 * Returns the voice activation state of a given subId.
2114 */
2115 @Override
2116 public int getVoiceActivationState(int subId, String callingPackage) {
2117 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2118 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2119 }
2120 final Phone phone = getPhone(subId);
2121 if (phone != null) {
2122 return phone.getVoiceActivationState();
2123 } else {
2124 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2125 }
2126 }
2127
2128 /**
2129 * Returns the data activation state of a given subId.
2130 */
2131 @Override
2132 public int getDataActivationState(int subId, String callingPackage) {
2133 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2134 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2135 }
2136 final Phone phone = getPhone(subId);
2137 if (phone != null) {
2138 return phone.getDataActivationState();
2139 } else {
2140 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2141 }
2142 }
2143
2144 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002145 * Returns the unread count of voicemails
2146 */
2147 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002148 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002149 }
2150
2151 /**
2152 * Returns the unread count of voicemails for a subId
2153 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002154 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002155 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002156 final Phone phone = getPhone(subId);
2157 if (phone != null) {
2158 return phone.getVoiceMessageCount();
2159 } else {
2160 return 0;
2161 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002162 }
2163
2164 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002165 * returns true, if the device is in a state where both voice and data
2166 * are supported simultaneously. This can change based on location or network condition.
2167 */
2168 @Override
2169 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2170 final Phone phone = getPhone(subId);
2171 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2172 }
2173
2174 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002175 * Send the dialer code if called from the current default dialer or the caller has
2176 * carrier privilege.
2177 * @param inputCode The dialer code to send
2178 */
2179 @Override
2180 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
2181 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2182 String defaultDialer = TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage();
2183 if (!TextUtils.equals(callingPackage, defaultDialer)) {
2184 enforceCarrierPrivilege(getDefaultSubscription());
2185 }
2186 mPhone.sendDialerSpecialCode(inputCode);
2187 }
2188
2189 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002190 * Returns the data network type.
2191 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002192 *
2193 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2194 */
2195 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002196 public int getNetworkType() {
2197 final Phone phone = getPhone(getDefaultSubscription());
2198 if (phone != null) {
2199 return phone.getServiceState().getDataNetworkType();
2200 } else {
2201 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2202 }
Wink Saville36469e72014-06-11 15:17:00 -07002203 }
2204
2205 /**
2206 * Returns the network type for a subId
2207 */
2208 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002209 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2210 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2211 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2212 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002213
Sanket Padawe356d7632015-06-22 14:03:32 -07002214 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002215 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002216 return phone.getServiceState().getDataNetworkType();
2217 } else {
2218 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2219 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002220 }
2221
2222 /**
2223 * Returns the data network type
2224 */
2225 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002226 public int getDataNetworkType(String callingPackage) {
2227 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002228 }
2229
2230 /**
2231 * Returns the data network type for a subId
2232 */
2233 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002234 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2235 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2236 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2237 }
2238
Sanket Padawe356d7632015-06-22 14:03:32 -07002239 final Phone phone = getPhone(subId);
2240 if (phone != null) {
2241 return phone.getServiceState().getDataNetworkType();
2242 } else {
2243 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2244 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002245 }
2246
2247 /**
Wink Saville36469e72014-06-11 15:17:00 -07002248 * Returns the Voice network type for a subId
2249 */
2250 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002251 public int getVoiceNetworkTypeForSubscriber(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().getVoiceNetworkType();
2259 } else {
2260 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2261 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002262 }
2263
2264 /**
2265 * @return true if a ICC card is present
2266 */
2267 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002268 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002269 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2270 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002271 }
2272
2273 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002274 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002275 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002276 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002277 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2278 final Phone phone = PhoneFactory.getPhone(slotIndex);
fionaxu6e6c68b2016-06-23 13:31:32 -07002279 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002280 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002281 } else {
2282 return false;
2283 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002284 }
2285
2286 /**
2287 * Return if the current radio is LTE on CDMA. This
2288 * is a tri-state return value as for a period of time
2289 * the mode may be unknown.
2290 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002291 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002292 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002293 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002294 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002295 @Override
2296 public int getLteOnCdmaMode(String callingPackage) {
2297 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002298 }
2299
Sanket Padawe356d7632015-06-22 14:03:32 -07002300 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002301 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2302 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2303 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2304 }
2305
Sanket Padawe356d7632015-06-22 14:03:32 -07002306 final Phone phone = getPhone(subId);
2307 if (phone == null) {
2308 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2309 } else {
2310 return phone.getLteOnCdmaMode();
2311 }
Wink Saville36469e72014-06-11 15:17:00 -07002312 }
2313
2314 public void setPhone(Phone phone) {
2315 mPhone = phone;
2316 }
2317
2318 /**
2319 * {@hide}
2320 * Returns Default subId, 0 in the case of single standby.
2321 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002322 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002323 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002324 }
2325
Shishir Agrawala9f32182016-04-12 12:00:16 -07002326 private int getSlotForDefaultSubscription() {
2327 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2328 }
2329
Wink Savilleb564aae2014-10-23 10:18:09 -07002330 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002331 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002332 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002333
2334 /**
2335 * @see android.telephony.TelephonyManager.WifiCallingChoices
2336 */
2337 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002338 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2339 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002340 }
2341
2342 /**
2343 * @see android.telephony.TelephonyManager.WifiCallingChoices
2344 */
2345 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002346 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2347 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2348 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002349 }
2350
Sailesh Nepald1e68152013-12-12 19:08:02 -08002351 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002352 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002353 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002354 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002355
Shishir Agrawal566b7612013-10-28 14:41:00 -07002356 @Override
Ajay Nambid7454d32015-12-03 13:50:00 -08002357 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002358 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002359
Ajay Nambid7454d32015-12-03 13:50:00 -08002360 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002361 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambid7454d32015-12-03 13:50:00 -08002362 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002363 if (DBG) log("iccOpenLogicalChannel: " + response);
2364 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002365 }
2366
2367 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002368 public boolean iccCloseLogicalChannel(int subId, int channel) {
2369 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002370
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002371 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002372 if (channel < 0) {
2373 return false;
2374 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002375 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002376 if (DBG) log("iccCloseLogicalChannel: " + success);
2377 return success;
2378 }
2379
2380 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002381 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002382 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002383 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002384
2385 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002386 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2387 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002388 " data=" + data);
2389 }
2390
2391 if (channel < 0) {
2392 return "";
2393 }
2394
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002395 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002396 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002397 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2398
Shishir Agrawal566b7612013-10-28 14:41:00 -07002399 // Append the returned status code to the end of the response payload.
2400 String s = Integer.toHexString(
2401 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002402 if (response.payload != null) {
2403 s = IccUtils.bytesToHexString(response.payload) + s;
2404 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002405 return s;
2406 }
Jake Hambye994d462014-02-03 13:10:13 -08002407
Evan Charltonc66da362014-05-16 14:06:40 -07002408 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002409 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002410 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002411 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002412
2413 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002414 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2415 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002416 }
2417
2418 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002419 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002420 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2421
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002422 // Append the returned status code to the end of the response payload.
2423 String s = Integer.toHexString(
2424 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002425 if (response.payload != null) {
2426 s = IccUtils.bytesToHexString(response.payload) + s;
2427 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002428 return s;
2429 }
2430
2431 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002432 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002433 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002434 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002435
2436 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002437 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002438 p1 + " " + p2 + " " + p3 + ":" + filePath);
2439 }
2440
2441 IccIoResult response =
2442 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002443 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2444 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002445
2446 if (DBG) {
2447 log("Exchange SIM_IO [R]" + response);
2448 }
2449
2450 byte[] result = null;
2451 int length = 2;
2452 if (response.payload != null) {
2453 length = 2 + response.payload.length;
2454 result = new byte[length];
2455 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2456 } else {
2457 result = new byte[length];
2458 }
2459
2460 result[length - 1] = (byte) response.sw2;
2461 result[length - 2] = (byte) response.sw1;
2462 return result;
2463 }
2464
Nathan Haroldb3014052017-01-25 15:57:32 -08002465 /**
2466 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2467 * on a particular subscription
2468 */
2469 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002470 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2471 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002472 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2473 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2474 return null;
2475 }
2476 Object response = sendRequest(
2477 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2478 if (response instanceof String[]) {
2479 return (String[]) response;
2480 }
2481 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2482 return null;
2483 }
2484
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002485 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002486 public String sendEnvelopeWithStatus(int subId, String content) {
2487 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002488
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002489 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002490 if (response.payload == null) {
2491 return "";
2492 }
2493
2494 // Append the returned status code to the end of the response payload.
2495 String s = Integer.toHexString(
2496 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2497 s = IccUtils.bytesToHexString(response.payload) + s;
2498 return s;
2499 }
2500
Jake Hambye994d462014-02-03 13:10:13 -08002501 /**
2502 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2503 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2504 *
2505 * @param itemID the ID of the item to read
2506 * @return the NV item as a String, or null on error.
2507 */
2508 @Override
2509 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002510 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002511 if (DBG) log("nvReadItem: item " + itemID);
2512 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2513 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2514 return value;
2515 }
2516
2517 /**
2518 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2519 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2520 *
2521 * @param itemID the ID of the item to read
2522 * @param itemValue the value to write, as a String
2523 * @return true on success; false on any failure
2524 */
2525 @Override
2526 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002527 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002528 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2529 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2530 new Pair<Integer, String>(itemID, itemValue));
2531 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2532 return success;
2533 }
2534
2535 /**
2536 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2537 * Used for device configuration by some CDMA operators.
2538 *
2539 * @param preferredRoamingList byte array containing the new PRL
2540 * @return true on success; false on any failure
2541 */
2542 @Override
2543 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002544 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002545 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2546 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2547 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2548 return success;
2549 }
2550
2551 /**
2552 * Perform the specified type of NV config reset.
2553 * Used for device configuration by some CDMA operators.
2554 *
2555 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2556 * @return true on success; false on any failure
2557 */
2558 @Override
2559 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002560 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002561 if (DBG) log("nvResetConfig: type " + resetType);
2562 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2563 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2564 return success;
2565 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002566
2567 /**
Wink Saville36469e72014-06-11 15:17:00 -07002568 * {@hide}
2569 * Returns Default sim, 0 in the case of single standby.
2570 */
2571 public int getDefaultSim() {
2572 //TODO Need to get it from Telephony Devcontroller
2573 return 0;
2574 }
2575
Svet Ganovb320e182015-04-16 12:30:10 -07002576 public String[] getPcscfAddress(String apnType, String callingPackage) {
2577 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2578 return new String[0];
2579 }
2580
2581
ram87fca6f2014-07-18 18:58:44 +05302582 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002583 }
2584
Brad Ebinger51f743a2017-01-23 13:50:20 -08002585 /**
2586 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2587 * feature or {@link null} if the service is not available. If an ImsServiceController is
2588 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2589 * feature updates.
2590 */
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002591 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002592 IImsServiceFeatureListener callback) {
2593 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002594 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002595 callback);
2596 }
2597
Wink Saville36469e72014-06-11 15:17:00 -07002598 public void setImsRegistrationState(boolean registered) {
2599 enforceModifyPermission();
2600 mPhone.setImsRegistrationState(registered);
2601 }
2602
2603 /**
Stuart Scott54788802015-03-30 13:18:01 -07002604 * Set the network selection mode to automatic.
2605 *
2606 */
2607 @Override
2608 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002609 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002610 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2611 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2612 }
2613
2614 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002615 * Set the network selection mode to manual with the selected carrier.
2616 */
2617 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002618 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2619 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002620 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002621 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002622 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2623 persistSelection);
2624 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002625 }
2626
2627 /**
2628 * Scans for available networks.
2629 */
2630 @Override
2631 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002632 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002633 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2634 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2635 CMD_PERFORM_NETWORK_SCAN, null, subId);
2636 return result;
2637 }
2638
2639 /**
yinxu504e1392017-04-12 16:03:22 -07002640 * Performs a new network scan and returns the id of this scan.
2641 *
2642 * @return the id of the requested scan which can be used to stop the scan.
2643 */
2644 @Override
2645 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
2646 IBinder binder) {
2647 // TODO(yinxu): Implement this method.
2648 throw new UnsupportedOperationException("To be implemented...");
2649 }
2650
2651 /**
2652 * Stops an existing network scan with the given scanId.
2653 */
2654 @Override
2655 public void stopNetworkScan(int subId, int scanId) {
2656 // TODO(yinxu): Implement this method.
2657 throw new UnsupportedOperationException("To be implemented...");
2658 }
2659
2660 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002661 * Get the calculated preferred network type.
2662 * Used for debugging incorrect network type.
2663 *
2664 * @return the preferred network type, defined in RILConstants.java.
2665 */
2666 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002667 public int getCalculatedPreferredNetworkType(String callingPackage) {
2668 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2669 return RILConstants.PREFERRED_NETWORK_MODE;
2670 }
2671
Amit Mahajan43330e02014-11-18 11:54:45 -08002672 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002673 }
2674
2675 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002676 * Get the preferred network type.
2677 * Used for device configuration by some CDMA operators.
2678 *
2679 * @return the preferred network type, defined in RILConstants.java.
2680 */
2681 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002682 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002683 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002684 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002685 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002686 int networkType = (result != null ? result[0] : -1);
2687 if (DBG) log("getPreferredNetworkType: " + networkType);
2688 return networkType;
2689 }
2690
2691 /**
2692 * Set the preferred network type.
2693 * Used for device configuration by some CDMA operators.
2694 *
2695 * @param networkType the preferred network type, defined in RILConstants.java.
2696 * @return true on success; false on any failure.
2697 */
2698 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002699 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002700 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002701 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2702 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002703 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002704 if (success) {
2705 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002706 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002707 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002708 return success;
2709 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002710
2711 /**
Junda Liu475951f2014-11-07 16:45:03 -08002712 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2713 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2714 * tethering.
2715 *
2716 * @return 0: Not required. 1: required. 2: Not set.
2717 * @hide
2718 */
2719 @Override
2720 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002721 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002722 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2723 Settings.Global.TETHER_DUN_REQUIRED, 2);
2724 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2725 // config_tether_apndata.
2726 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2727 dunRequired = 1;
2728 }
2729 return dunRequired;
2730 }
2731
2732 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002733 * Set mobile data enabled
2734 * Used by the user through settings etc to turn on/off mobile data
2735 *
2736 * @param enable {@code true} turn turn data on, else {@code false}
2737 */
2738 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002739 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002740 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002741 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002742 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002743 Phone phone = PhoneFactory.getPhone(phoneId);
2744 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002745 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002746 phone.setDataEnabled(enable);
2747 } else {
2748 loge("setDataEnabled: no phone for subId=" + subId);
2749 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002750 }
2751
2752 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002753 * Get whether mobile data is enabled.
2754 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002755 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002756 *
2757 * @return {@code true} if data is enabled else {@code false}
2758 */
2759 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002760 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002761 try {
2762 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2763 null);
2764 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002765 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002766 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002767 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002768 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002769 Phone phone = PhoneFactory.getPhone(phoneId);
2770 if (phone != null) {
2771 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002772 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002773 return retVal;
2774 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002775 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002776 return false;
2777 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002778 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002779
2780 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002781 public int getCarrierPrivilegeStatus(int subId) {
2782 final Phone phone = getPhone(subId);
2783 if (phone == null) {
2784 loge("getCarrierPrivilegeStatus: Invalid subId");
2785 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2786 }
2787 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002788 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002789 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002790 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2791 }
2792 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002793 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002794 }
Junda Liu29340342014-07-10 15:23:27 -07002795
2796 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002797 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002798 if (TextUtils.isEmpty(pkgName))
2799 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002800 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002801 if (card == null) {
2802 loge("checkCarrierPrivilegesForPackage: No UICC");
2803 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2804 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002805 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2806 }
2807
2808 @Override
2809 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002810 if (TextUtils.isEmpty(pkgName))
2811 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002812 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2813 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2814 UiccCard card = UiccController.getInstance().getUiccCard(i);
2815 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002816 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002817 continue;
2818 }
2819
2820 result = card.getCarrierPrivilegeStatus(
2821 mPhone.getContext().getPackageManager(), pkgName);
2822 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2823 break;
2824 }
2825 }
2826
2827 return result;
Junda Liu29340342014-07-10 15:23:27 -07002828 }
Derek Tan89e89d42014-07-08 17:00:10 -07002829
2830 @Override
Junda Liue64de782015-04-16 17:19:16 -07002831 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2832 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2833 loge("phoneId " + phoneId + " is not valid.");
2834 return null;
2835 }
2836 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002837 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002838 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002839 return null ;
2840 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002841 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002842 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002843 }
2844
Amith Yamasani6e118872016-02-19 12:53:51 -08002845 @Override
2846 public List<String> getPackagesWithCarrierPrivileges() {
2847 PackageManager pm = mPhone.getContext().getPackageManager();
2848 List<String> privilegedPackages = new ArrayList<>();
2849 List<PackageInfo> packages = null;
2850 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2851 UiccCard card = UiccController.getInstance().getUiccCard(i);
2852 if (card == null) {
2853 // No UICC in that slot.
2854 continue;
2855 }
2856 if (card.hasCarrierPrivilegeRules()) {
2857 if (packages == null) {
2858 // Only check packages in user 0 for now
2859 packages = pm.getInstalledPackagesAsUser(
2860 PackageManager.MATCH_DISABLED_COMPONENTS
2861 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2862 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2863 }
2864 for (int p = packages.size() - 1; p >= 0; p--) {
2865 PackageInfo pkgInfo = packages.get(p);
2866 if (pkgInfo != null && pkgInfo.packageName != null
2867 && card.getCarrierPrivilegeStatus(pkgInfo)
2868 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2869 privilegedPackages.add(pkgInfo.packageName);
2870 }
2871 }
2872 }
2873 }
2874 return privilegedPackages;
2875 }
2876
Wink Savilleb564aae2014-10-23 10:18:09 -07002877 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002878 final Phone phone = getPhone(subId);
2879 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002880 if (card == null) {
2881 loge("getIccId: No UICC");
2882 return null;
2883 }
2884 String iccId = card.getIccId();
2885 if (TextUtils.isEmpty(iccId)) {
2886 loge("getIccId: ICC ID is null or empty.");
2887 return null;
2888 }
2889 return iccId;
2890 }
2891
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002892 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002893 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2894 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002895 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002896
Jeff Sharkey85190e62014-12-05 09:40:12 -08002897 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002898 final Phone phone = getPhone(subId);
2899 if (phone == null) {
2900 return false;
2901 }
2902 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002903
2904 if (DBG_MERGE) {
2905 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2906 + subscriberId + " to " + number);
2907 }
2908
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002909 if (TextUtils.isEmpty(iccId)) {
2910 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002911 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002912
Jeff Sharkey85190e62014-12-05 09:40:12 -08002913 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2914
2915 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002916 if (alphaTag == null) {
2917 editor.remove(alphaTagPrefKey);
2918 } else {
2919 editor.putString(alphaTagPrefKey, alphaTag);
2920 }
2921
Jeff Sharkey85190e62014-12-05 09:40:12 -08002922 // Record both the line number and IMSI for this ICCID, since we need to
2923 // track all merged IMSIs based on line number
2924 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2925 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002926 if (number == null) {
2927 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002928 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002929 } else {
2930 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002931 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002932 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002933
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002934 editor.commit();
2935 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002936 }
2937
2938 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002939 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002940 // This is open to apps with WRITE_SMS.
2941 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002942 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002943 return null;
2944 }
Derek Tan97ebb422014-09-05 16:55:38 -07002945
2946 String iccId = getIccId(subId);
2947 if (iccId != null) {
2948 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002949 if (DBG_MERGE) {
2950 log("getLine1NumberForDisplay returning " +
2951 mTelephonySharedPreferences.getString(numberPrefKey, null));
2952 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002953 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002954 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002955 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002956 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002957 }
2958
2959 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002960 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2961 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2962 return null;
2963 }
Derek Tan97ebb422014-09-05 16:55:38 -07002964
2965 String iccId = getIccId(subId);
2966 if (iccId != null) {
2967 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002968 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002969 }
Derek Tan97ebb422014-09-05 16:55:38 -07002970 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002971 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002972
2973 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002974 public String[] getMergedSubscriberIds(String callingPackage) {
2975 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2976 return null;
2977 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002978 final Context context = mPhone.getContext();
2979 final TelephonyManager tele = TelephonyManager.from(context);
2980 final SubscriptionManager sub = SubscriptionManager.from(context);
2981
2982 // Figure out what subscribers are currently active
2983 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002984 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2985 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2986 final long identity = Binder.clearCallingIdentity();
2987 try {
2988 final int[] subIds = sub.getActiveSubscriptionIdList();
2989 for (int subId : subIds) {
2990 activeSubscriberIds.add(tele.getSubscriberId(subId));
2991 }
2992 } finally {
2993 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002994 }
2995
2996 // First pass, find a number override for an active subscriber
2997 String mergeNumber = null;
2998 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2999 for (String key : prefs.keySet()) {
3000 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
3001 final String subscriberId = (String) prefs.get(key);
3002 if (activeSubscriberIds.contains(subscriberId)) {
3003 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
3004 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3005 mergeNumber = (String) prefs.get(numberKey);
3006 if (DBG_MERGE) {
3007 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3008 + " for active subscriber " + subscriberId);
3009 }
3010 if (!TextUtils.isEmpty(mergeNumber)) {
3011 break;
3012 }
3013 }
3014 }
3015 }
3016
3017 // Shortcut when no active merged subscribers
3018 if (TextUtils.isEmpty(mergeNumber)) {
3019 return null;
3020 }
3021
3022 // Second pass, find all subscribers under that line override
3023 final ArraySet<String> result = new ArraySet<>();
3024 for (String key : prefs.keySet()) {
3025 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3026 final String number = (String) prefs.get(key);
3027 if (mergeNumber.equals(number)) {
3028 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3029 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3030 final String subscriberId = (String) prefs.get(subscriberKey);
3031 if (!TextUtils.isEmpty(subscriberId)) {
3032 result.add(subscriberId);
3033 }
3034 }
3035 }
3036 }
3037
3038 final String[] resultArray = result.toArray(new String[result.size()]);
3039 Arrays.sort(resultArray);
3040 if (DBG_MERGE) {
3041 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3042 }
3043 return resultArray;
3044 }
3045
3046 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003047 public boolean setOperatorBrandOverride(int subId, String brand) {
3048 enforceCarrierPrivilege(subId);
3049 final Phone phone = getPhone(subId);
3050 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003051 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003052
3053 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003054 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003055 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3056 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003057 enforceCarrierPrivilege(subId);
3058 final Phone phone = getPhone(subId);
3059 if (phone == null) {
3060 return false;
3061 }
3062 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003063 cdmaNonRoamingList);
3064 }
3065
3066 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003067 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003068 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3069 enforceModifyPermission();
3070
3071 int returnValue = 0;
3072 try {
3073 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3074 if(result.exception == null) {
3075 if (result.result != null) {
3076 byte[] responseData = (byte[])(result.result);
3077 if(responseData.length > oemResp.length) {
3078 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3079 responseData.length + "bytes. Buffer Size is " +
3080 oemResp.length + "bytes.");
3081 }
3082 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3083 returnValue = responseData.length;
3084 }
3085 } else {
3086 CommandException ex = (CommandException) result.exception;
3087 returnValue = ex.getCommandError().ordinal();
3088 if(returnValue > 0) returnValue *= -1;
3089 }
3090 } catch (RuntimeException e) {
3091 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3092 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3093 if(returnValue > 0) returnValue *= -1;
3094 }
3095
3096 return returnValue;
3097 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003098
3099 @Override
3100 public void setRadioCapability(RadioAccessFamily[] rafs) {
3101 try {
3102 ProxyController.getInstance().setRadioCapability(rafs);
3103 } catch (RuntimeException e) {
3104 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3105 }
3106 }
3107
3108 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003109 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3110 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3111 return RadioAccessFamily.RAF_UNKNOWN;
3112 }
3113
Wink Saville5d475dd2014-10-17 15:00:58 -07003114 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3115 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003116
3117 @Override
3118 public void enableVideoCalling(boolean enable) {
3119 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003120 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003121 }
3122
3123 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003124 public boolean isVideoCallingEnabled(String callingPackage) {
3125 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3126 return false;
3127 }
3128
Andrew Lee77527ac2014-10-21 16:57:39 -07003129 // Check the user preference and the system-level IMS setting. Even if the user has
3130 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3131 // In the long run, we may instead need to check if there exists a connection service
3132 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003133 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3134 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003135 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003136 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003137
Andrew Leea1239f22015-03-02 17:44:07 -08003138 @Override
3139 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003140 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003141 }
3142
3143 @Override
3144 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003145 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003146 }
3147
Andrew Lee9431b832015-03-09 18:46:45 -07003148 @Override
3149 public boolean isTtyModeSupported() {
3150 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3151 TelephonyManager telephonyManager =
3152 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003153 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003154 }
3155
3156 @Override
3157 public boolean isHearingAidCompatibilitySupported() {
3158 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3159 }
3160
Sanket Padawe7310cc72015-01-14 09:53:20 -08003161 /**
3162 * Returns the unique device ID of phone, for example, the IMEI for
3163 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3164 *
3165 * <p>Requires Permission:
3166 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3167 */
3168 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003169 public String getDeviceId(String callingPackage) {
3170 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3171 return null;
3172 }
3173
Sanket Padawe7310cc72015-01-14 09:53:20 -08003174 final Phone phone = PhoneFactory.getPhone(0);
3175 if (phone != null) {
3176 return phone.getDeviceId();
3177 } else {
3178 return null;
3179 }
3180 }
3181
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003182 /*
3183 * {@hide}
3184 * Returns the IMS Registration Status
3185 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003186 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003187 public boolean isImsRegistered() {
3188 return mPhone.isImsRegistered();
3189 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003190
3191 @Override
3192 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3193 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3194 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003195
Nathan Haroldc55097a2015-03-11 18:14:50 -07003196 /*
3197 * {@hide}
3198 * Returns the IMS Registration Status
3199 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003200 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003201 return mPhone.isWifiCallingEnabled();
3202 }
3203
3204 /*
3205 * {@hide}
3206 * Returns the IMS Registration Status
3207 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003208 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003209 return mPhone.isVolteEnabled();
3210 }
Svet Ganovb320e182015-04-16 12:30:10 -07003211
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003212 /*
3213 * {@hide} Returns the IMS Registration Status
3214 */
3215 public boolean isVideoTelephonyAvailable() {
3216 return mPhone.isVideoEnabled();
3217 }
3218
Svet Ganovb320e182015-04-16 12:30:10 -07003219 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003220 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003221 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003222 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3223
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003224 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003225 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003226 } catch (SecurityException e) {
3227 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3228 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003229 }
Svet Ganovb320e182015-04-16 12:30:10 -07003230
3231 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3232 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3233 return false;
3234 }
3235
3236 return true;
3237 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003238
Makoto Onukifee69342015-06-29 14:44:50 -07003239 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003240 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003241 */
3242 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003243 // Default SMS app can always read it.
3244 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3245 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3246 return true;
3247 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003248
Makoto Onukie4072d12015-08-03 15:12:23 -07003249 try {
3250 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003251 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003252 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003253 // Can be read with READ_SMS too.
3254 try {
3255 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3256 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3257 if (opCode != AppOpsManager.OP_NONE) {
3258 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3259 == AppOpsManager.MODE_ALLOWED;
3260 } else {
3261 return true;
3262 }
3263 } catch (SecurityException readSmsSecurityException) {
3264 }
Chad Brubakerf342a982017-03-30 16:27:34 -07003265 // Can be read with READ_PHONE_NUMBERS too.
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003266 try {
Chad Brubakerf342a982017-03-30 16:27:34 -07003267 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBERS,
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003268 message);
Chad Brubakerf342a982017-03-30 16:27:34 -07003269 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBERS);
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003270 if (opCode != AppOpsManager.OP_NONE) {
3271 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3272 == AppOpsManager.MODE_ALLOWED;
3273 } else {
3274 return true;
3275 }
3276 } catch (SecurityException readPhoneNumberSecurityException) {
3277 }
3278
3279 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3280 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3281 ", " + android.Manifest.permission.READ_SMS + ", or " +
Chad Brubakerf342a982017-03-30 16:27:34 -07003282 android.Manifest.permission.READ_PHONE_NUMBERS);
Makoto Onukifee69342015-06-29 14:44:50 -07003283 }
3284
Stuart Scott8eef64f2015-04-08 15:13:54 -07003285 @Override
3286 public void factoryReset(int subId) {
3287 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003288 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3289 return;
3290 }
3291
Svet Ganovcc087f82015-05-12 20:35:54 -07003292 final long identity = Binder.clearCallingIdentity();
3293 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003294 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3295 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003296 // Enable data
3297 setDataEnabled(subId, true);
3298 // Set network selection mode to automatic
3299 setNetworkSelectionModeAutomatic(subId);
3300 // Set preferred mobile network type to the best available
3301 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3302 // Turn off roaming
Malcolm Chenf6b97f42017-04-19 16:03:24 -07003303 mPhone.setDataRoamingEnabled(false);
Svet Ganovcc087f82015-05-12 20:35:54 -07003304 }
3305 } finally {
3306 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003307 }
3308 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003309
3310 @Override
3311 public String getLocaleFromDefaultSim() {
3312 // We query all subscriptions instead of just the active ones, because
3313 // this might be called early on in the provisioning flow when the
3314 // subscriptions potentially aren't active yet.
3315 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3316 if (slist == null || slist.isEmpty()) {
3317 return null;
3318 }
3319
3320 // This function may be called very early, say, from the setup wizard, at
3321 // which point we won't have a default subscription set. If that's the case
3322 // we just choose the first, which will be valid in "most cases".
3323 final int defaultSubId = getDefaultSubscription();
3324 SubscriptionInfo info = null;
3325 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3326 info = slist.get(0);
3327 } else {
3328 for (SubscriptionInfo item : slist) {
3329 if (item.getSubscriptionId() == defaultSubId) {
3330 info = item;
3331 break;
3332 }
3333 }
3334
3335 if (info == null) {
3336 return null;
3337 }
3338 }
3339
3340 // Try and fetch the locale from the carrier properties or from the SIM language
3341 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003342 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003343 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003344 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003345 if (defaultPhone != null) {
3346 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3347 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003348 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003349 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3350 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003351 } else {
3352 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003353 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003354 }
3355 }
3356
Narayan Kamath011676f2015-07-29 12:04:08 +01003357 // The SIM language preferences only store a language (e.g. fr = French), not an
3358 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3359 // the SIM and carrier preferences does not include a country we add the country
3360 // determined from the SIM MCC to provide an exact locale.
3361 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003362 if (mccLocale != null) {
3363 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3364 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003365 }
3366
Tony Hill183b2de2015-06-24 14:53:58 +01003367 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003368 return null;
3369 }
3370
3371 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3372 final long identity = Binder.clearCallingIdentity();
3373 try {
3374 return mSubscriptionController.getAllSubInfoList(
3375 mPhone.getContext().getOpPackageName());
3376 } finally {
3377 Binder.restoreCallingIdentity(identity);
3378 }
3379 }
3380
3381 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3382 final long identity = Binder.clearCallingIdentity();
3383 try {
3384 return mSubscriptionController.getActiveSubscriptionInfoList(
3385 mPhone.getContext().getOpPackageName());
3386 } finally {
3387 Binder.restoreCallingIdentity(identity);
3388 }
3389 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003390
3391 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003392 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3393 * representing the state of the modem.
3394 *
3395 * NOTE: This clears the modem state, so there should only every be one caller.
3396 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003397 */
3398 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003399 public void requestModemActivityInfo(ResultReceiver result) {
3400 enforceModifyPermission();
3401
3402 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3403 Bundle bundle = new Bundle();
3404 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3405 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003406 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003407
3408 /**
3409 * {@hide}
3410 * Returns the service state information on specified subscription.
3411 */
3412 @Override
3413 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3414
3415 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3416 return null;
3417 }
3418
3419 final Phone phone = getPhone(subId);
3420 if (phone == null) {
3421 return null;
3422 }
3423
3424 return phone.getServiceState();
3425 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003426
3427 /**
3428 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3429 *
3430 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3431 * voicemail ringtone.
3432 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3433 * PhoneAccount.
3434 */
3435 @Override
3436 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003437 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003438 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003439 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003440 }
3441
3442 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3443 }
3444
3445 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003446 * Sets the per-account voicemail ringtone.
3447 *
3448 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3449 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3450 *
3451 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3452 * voicemail ringtone.
3453 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3454 * PhoneAccount.
3455 */
3456 @Override
3457 public void setVoicemailRingtoneUri(String callingPackage,
3458 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3459 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3460 if (!TextUtils.equals(callingPackage,
3461 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3462 enforceModifyPermissionOrCarrierPrivilege(
3463 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3464 }
3465 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3466 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003467 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003468 }
3469 VoicemailNotificationSettingsUtil.setRingtoneUri(phone, uri);
3470 }
3471
3472 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003473 * Returns whether vibration is set for voicemail notification in Phone settings.
3474 *
3475 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3476 * voicemail vibration setting.
3477 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3478 */
3479 @Override
3480 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003481 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
Nancy Chen31f9ba12016-01-06 11:42:12 -08003482 if (phone == null) {
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003483 phone = mPhone;
Nancy Chen31f9ba12016-01-06 11:42:12 -08003484 }
3485
3486 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3487 }
3488
Youhan Wange64578a2016-05-02 15:32:42 -07003489 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003490 * Sets the per-account voicemail vibration.
3491 *
3492 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3493 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3494 *
3495 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3496 * voicemail vibration setting.
3497 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3498 * specific PhoneAccount.
3499 */
3500 @Override
3501 public void setVoicemailVibrationEnabled(String callingPackage,
3502 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3503 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3504 if (!TextUtils.equals(callingPackage,
3505 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3506 enforceModifyPermissionOrCarrierPrivilege(
3507 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3508 }
3509
3510 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3511 if (phone == null){
Ta-wei Yen00b4ab52017-04-11 13:24:26 -07003512 phone = mPhone;
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003513 }
3514 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone, enabled);
3515 }
3516
3517 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003518 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3519 *
3520 * @throws SecurityException if the caller does not have the required permission
3521 */
3522 private void enforceReadPrivilegedPermission() {
3523 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3524 null);
3525 }
3526
3527 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003528 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3529 * permission.
3530 *
3531 * @throws SecurityException if the caller does not have the required permission
3532 */
3533 private void enforceSendSmsPermission() {
3534 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3535 }
3536
3537 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003538 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003539 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003540 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003541 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003542 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Ta-wei Yen5d81b0c2017-03-03 16:32:26 -08003543 ComponentName componentName =
3544 RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
3545 if(componentName == null) {
3546 throw new SecurityException("Caller not current active visual voicemail package[null]");
3547 }
3548 String vvmPackage = componentName.getPackageName();
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003549 if (!callingPackage.equals(vvmPackage)) {
3550 throw new SecurityException("Caller not current active visual voicemail package[" +
3551 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003552 }
3553 }
3554
3555 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003556 * Return the application ID for the app type.
3557 *
3558 * @param subId the subscription ID that this request applies to.
3559 * @param appType the uicc app type.
3560 * @return Application ID for specificied app type, or null if no uicc.
3561 */
3562 @Override
3563 public String getAidForAppType(int subId, int appType) {
3564 enforceReadPrivilegedPermission();
3565 Phone phone = getPhone(subId);
3566 if (phone == null) {
3567 return null;
3568 }
3569 String aid = null;
3570 try {
3571 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3572 .getApplicationByType(appType).getAid();
3573 } catch (Exception e) {
3574 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3575 }
3576 return aid;
3577 }
3578
Youhan Wang4001d252016-05-11 10:29:41 -07003579 /**
3580 * Return the Electronic Serial Number.
3581 *
3582 * @param subId the subscription ID that this request applies to.
3583 * @return ESN or null if error.
3584 */
3585 @Override
3586 public String getEsn(int subId) {
3587 enforceReadPrivilegedPermission();
3588 Phone phone = getPhone(subId);
3589 if (phone == null) {
3590 return null;
3591 }
3592 String esn = null;
3593 try {
3594 esn = phone.getEsn();
3595 } catch (Exception e) {
3596 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3597 }
3598 return esn;
3599 }
3600
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003601 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003602 * Return the Preferred Roaming List Version.
3603 *
3604 * @param subId the subscription ID that this request applies to.
3605 * @return PRLVersion or null if error.
3606 */
3607 @Override
3608 public String getCdmaPrlVersion(int subId) {
3609 enforceReadPrivilegedPermission();
3610 Phone phone = getPhone(subId);
3611 if (phone == null) {
3612 return null;
3613 }
3614 String cdmaPrlVersion = null;
3615 try {
3616 cdmaPrlVersion = phone.getCdmaPrlVersion();
3617 } catch (Exception e) {
3618 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3619 }
3620 return cdmaPrlVersion;
3621 }
3622
3623 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003624 * Get snapshot of Telephony histograms
3625 * @return List of Telephony histograms
3626 * @hide
3627 */
3628 @Override
3629 public List<TelephonyHistogram> getTelephonyHistograms() {
3630 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3631 return RIL.getTelephonyRILTimingHistograms();
3632 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003633
3634 /**
3635 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003636 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003637 * Require system privileges. In the future we may add this to carrier APIs.
3638 *
3639 * @return The number of carriers set successfully, should match length of carriers
3640 */
3641 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003642 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003643 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003644
Meng Wang9b7c4e92017-02-17 11:41:27 -08003645 if (carriers == null) {
3646 throw new NullPointerException("carriers cannot be null");
3647 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003648
3649 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003650 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3651 return retVal[0];
3652 }
3653
3654 /**
3655 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003656 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003657 * Require system privileges. In the future we may add this to carrier APIs.
3658 *
3659 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3660 * means all carriers are allowed.
3661 */
3662 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003663 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003664 enforceReadPrivilegedPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003665 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003666 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3667 }
3668
fionaxu59545b42016-05-25 15:53:37 -07003669 /**
3670 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3671 * @param subId the subscription ID that this action applies to.
3672 * @param enabled control enable or disable metered apns.
3673 * {@hide}
3674 */
3675 @Override
3676 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3677 enforceModifyPermission();
3678 final Phone phone = getPhone(subId);
3679 if (phone == null) {
3680 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3681 return;
3682 }
3683 try {
3684 phone.carrierActionSetMeteredApnsEnabled(enabled);
3685 } catch (Exception e) {
3686 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3687 }
3688 }
3689
3690 /**
3691 * Action set from carrier signalling broadcast receivers to enable/disable radio
3692 * @param subId the subscription ID that this action applies to.
3693 * @param enabled control enable or disable radio.
3694 * {@hide}
3695 */
3696 @Override
3697 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3698 enforceModifyPermission();
3699 final Phone phone = getPhone(subId);
3700 if (phone == null) {
3701 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3702 return;
3703 }
3704 try {
3705 phone.carrierActionSetRadioEnabled(enabled);
3706 } catch (Exception e) {
3707 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3708 }
3709 }
3710
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003711 /**
3712 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3713 * bug report is being generated.
3714 */
3715 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003716 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003717 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3718 != PackageManager.PERMISSION_GRANTED) {
3719 writer.println("Permission Denial: can't dump Phone from pid="
3720 + Binder.getCallingPid()
3721 + ", uid=" + Binder.getCallingUid()
3722 + "without permission "
3723 + android.Manifest.permission.DUMP);
3724 return;
3725 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003726 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003727 }
Jack Yueb89b242016-06-22 13:27:47 -07003728
3729 /**
3730 * Get aggregated video call data usage from all subscriptions since boot.
3731 * @return total data usage in bytes
3732 * {@hide}
3733 */
3734 @Override
3735 public long getVtDataUsage() {
3736 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3737 null);
3738
3739 // NetworkStatsService keeps tracking the active network interface and identity. It will
3740 // record the delta with the corresponding network identity. What we need to do here is
3741 // returning total video call data usage from all subscriptions since boot.
3742
3743 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3744 // simultaneous VT calls.
3745 final Phone[] phones = PhoneFactory.getPhones();
3746 long total = 0;
3747 for (Phone phone : phones) {
3748 total += phone.getVtDataUsage();
3749 }
3750 return total;
3751 }
Jack Yu75ab2952016-07-08 14:29:33 -07003752
3753 /**
3754 * Policy control of data connection. Usually used when data limit is passed.
3755 * @param enabled True if enabling the data, otherwise disabling.
3756 * @param subId Subscription index
3757 * {@hide}
3758 */
3759 @Override
3760 public void setPolicyDataEnabled(boolean enabled, int subId) {
3761 enforceModifyPermission();
3762 Phone phone = getPhone(subId);
3763 if (phone != null) {
3764 phone.setPolicyDataEnabled(enabled);
3765 }
3766 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003767
3768 /**
3769 * Get Client request stats
3770 * @return List of Client Request Stats
3771 * @hide
3772 */
3773 @Override
3774 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3775 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3776 return null;
3777 }
3778
3779 Phone phone = getPhone(subId);
3780 if (phone != null) {
3781 return phone.getClientRequestStats();
3782 }
3783
3784 return null;
3785 }
3786
3787 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3788 if (workSource != null) {
3789 return workSource;
3790 }
3791
3792 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3793 workSource = new WorkSource(uid, packageName);
3794 return workSource;
3795 }
Jack Yueb4124c2017-02-16 15:32:43 -08003796
3797 /**
3798 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3799 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003800 * @param slotIndex SIM slot id.
Jack Yueb4124c2017-02-16 15:32:43 -08003801 * @param powerUp True if powering up the SIM, otherwise powering down
3802 *
3803 **/
3804 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003805 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yueb4124c2017-02-16 15:32:43 -08003806 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003807 Phone phone = PhoneFactory.getPhone(slotIndex);
3808
Jack Yueb4124c2017-02-16 15:32:43 -08003809 if (phone != null) {
3810 phone.setSimPowerState(powerUp);
3811 }
3812 }
Shuo Qiandd210312017-04-12 22:11:33 +00003813
3814 /**
3815 * Check if phone is in emergency callback mode
3816 * @return true if phone is in emergency callback mode
3817 * @param subId sub id
3818 */
3819 public boolean getEmergencyCallbackMode(int subId) {
3820 final Phone phone = getPhone(subId);
3821 if (phone != null) {
3822 return phone.isInEcm();
3823 } else {
3824 return false;
3825 }
3826 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003827}