blob: 3fb8544b66e841cbc9708f91b9a691751c1e7e31 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Ta-wei Yen1b455992016-12-27 14:52:32 -080021import android.Manifest.permission;
Ta-wei Yen243b6372017-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 Yen1b455992016-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;
37import android.os.Looper;
38import android.os.Message;
39import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070040import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.ServiceManager;
42import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070043import android.os.UserManager;
Sooraj Sasindran22882212016-07-18 11:57:25 -070044import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070045import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080046import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070047import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080048import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080049import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070050import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070051import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.CellInfo;
Sooraj Sasindran22882212016-07-18 11:57:25 -070053import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070054import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070055import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080056import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070057import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.telephony.ServiceState;
Ta-wei Yen1b455992016-12-27 14:52:32 -080059import android.telephony.SmsManager;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080060import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080061import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070062import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070063import android.telephony.TelephonyManager;
64import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070065import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080066import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080068import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080069import android.util.Slog;
Ta-wei Yen1b455992016-12-27 14:52:32 -080070
Andrew Lee312e8172014-10-23 17:01:36 -070071import com.android.ims.ImsManager;
Brad Ebinger76681002017-01-23 13:50:20 -080072import com.android.ims.internal.IImsServiceController;
73import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070074import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070075import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070076import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070077import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080079import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010080import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070081import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070083import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070084import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070085import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070086import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070087import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080088import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070089import com.android.internal.telephony.uicc.IccIoResult;
90import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080091import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070092import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080093import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070094import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080095import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070096import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080097import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yen463efd12017-01-06 15:29:25 -080098import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yen1b455992016-12-27 14:52:32 -080099
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700100import java.io.FileDescriptor;
101import java.io.PrintWriter;
Ta-wei Yen1b455992016-12-27 14:52:32 -0800102import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800104import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800105import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100106import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800107import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108
109/**
110 * Implementation of the ITelephony interface.
111 */
Santos Cordon117fee72014-05-16 17:56:12 -0700112public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113 private static final String LOG_TAG = "PhoneInterfaceManager";
114 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
115 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800116 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700117
118 // Message codes used with mMainThreadHandler
119 private static final int CMD_HANDLE_PIN_MMI = 1;
120 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
121 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
122 private static final int CMD_ANSWER_RINGING_CALL = 4;
123 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700124 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
125 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700126 private static final int CMD_OPEN_CHANNEL = 9;
127 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
128 private static final int CMD_CLOSE_CHANNEL = 11;
129 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800130 private static final int CMD_NV_READ_ITEM = 13;
131 private static final int EVENT_NV_READ_ITEM_DONE = 14;
132 private static final int CMD_NV_WRITE_ITEM = 15;
133 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
134 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
135 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
136 private static final int CMD_NV_RESET_CONFIG = 19;
137 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800138 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
139 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
140 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
141 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800142 private static final int CMD_SEND_ENVELOPE = 25;
143 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700144 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
145 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
146 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
147 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
148 private static final int CMD_EXCHANGE_SIM_IO = 31;
149 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800150 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
151 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700152 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
153 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700154 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
155 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700156 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
157 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
158 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
159 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700160 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
161 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
162 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
163 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar7e856482016-10-14 19:37:38 -0700164 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800165 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
166 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700167
168 /** The singleton instance. */
169 private static PhoneInterfaceManager sInstance;
170
Wink Saville3ab207e2014-11-20 13:07:20 -0800171 private PhoneGlobals mApp;
172 private Phone mPhone;
173 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700174 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800175 private AppOpsManager mAppOps;
176 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800177 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800178 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700179
Derek Tan97ebb422014-09-05 16:55:38 -0700180 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
181 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800182 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700183
184 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700185 * A request object to use for transmitting data to an ICC.
186 */
187 private static final class IccAPDUArgument {
188 public int channel, cla, command, p1, p2, p3;
189 public String data;
190
191 public IccAPDUArgument(int channel, int cla, int command,
192 int p1, int p2, int p3, String data) {
193 this.channel = channel;
194 this.cla = cla;
195 this.command = command;
196 this.p1 = p1;
197 this.p2 = p2;
198 this.p3 = p3;
199 this.data = data;
200 }
201 }
202
203 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700204 * A request object to use for transmitting data to an ICC.
205 */
206 private static final class ManualNetworkSelectionArgument {
207 public OperatorInfo operatorInfo;
208 public boolean persistSelection;
209
210 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
211 this.operatorInfo = operatorInfo;
212 this.persistSelection = persistSelection;
213 }
214 }
215
216 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700217 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
218 * request after sending. The main thread will notify the request when it is complete.
219 */
220 private static final class MainThreadRequest {
221 /** The argument to use for the request */
222 public Object argument;
223 /** The result of the request that is run on the main thread */
224 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800225 // The subscriber id that this request applies to. Defaults to
226 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
227 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700228
229 public MainThreadRequest(Object argument) {
230 this.argument = argument;
231 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800232
233 public MainThreadRequest(Object argument, Integer subId) {
234 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800235 if (subId != null) {
236 this.subId = subId;
237 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800238 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700239 }
240
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800241 private static final class IncomingThirdPartyCallArgs {
242 public final ComponentName component;
243 public final String callId;
244 public final String callerDisplayName;
245
246 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
247 String callerDisplayName) {
248 this.component = component;
249 this.callId = callId;
250 this.callerDisplayName = callerDisplayName;
251 }
252 }
253
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 /**
255 * A handler that processes messages on the main thread in the phone process. Since many
256 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
257 * inbound binder threads to the main thread in the phone process. The Binder thread
258 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
259 * on, which will be notified when the operation completes and will contain the result of the
260 * request.
261 *
262 * <p>If a MainThreadRequest object is provided in the msg.obj field,
263 * note that request.result must be set to something non-null for the calling thread to
264 * unblock.
265 */
266 private final class MainThreadHandler extends Handler {
267 @Override
268 public void handleMessage(Message msg) {
269 MainThreadRequest request;
270 Message onCompleted;
271 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800272 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700273 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274
275 switch (msg.what) {
pkanwar7e856482016-10-14 19:37:38 -0700276 case CMD_HANDLE_USSD_REQUEST: {
277 request = (MainThreadRequest) msg.obj;
278 final Phone phone = getPhoneFromRequest(request);
279 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
280 String ussdRequest = ussdObject.first;
281 ResultReceiver wrappedCallback = ussdObject.second;
282 request.result = phone != null ?
283 phone.handleUssdRequest(ussdRequest, wrappedCallback)
284 :false;
285 // Wake up the requesting thread
286 synchronized (request) {
287 request.notifyAll();
288 }
289 break;
290 }
291
Yorke Lee716f67e2015-06-17 15:39:16 -0700292 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700293 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700294 final Phone phone = getPhoneFromRequest(request);
295 request.result = phone != null ?
296 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
297 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298 // Wake up the requesting thread
299 synchronized (request) {
300 request.notifyAll();
301 }
302 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700303 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304
305 case CMD_HANDLE_NEIGHBORING_CELL:
306 request = (MainThreadRequest) msg.obj;
307 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
308 request);
Sooraj Sasindran22882212016-07-18 11:57:25 -0700309 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310 break;
311
312 case EVENT_NEIGHBORING_CELL_DONE:
313 ar = (AsyncResult) msg.obj;
314 request = (MainThreadRequest) ar.userObj;
315 if (ar.exception == null && ar.result != null) {
316 request.result = ar.result;
317 } else {
318 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800319 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700320 }
321 // Wake up the requesting thread
322 synchronized (request) {
323 request.notifyAll();
324 }
325 break;
326
327 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700328 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800329 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700330 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700331 break;
332
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700333 case CMD_END_CALL:
334 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800335 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700336 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700337 Phone phone = getPhone(end_subId);
338 if (phone == null) {
339 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
340 break;
341 }
342 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700343 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
344 // CDMA: If the user presses the Power button we treat it as
345 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700346 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700347 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
348 // GSM: End the call as per the Phone state
349 hungUp = PhoneUtils.hangup(mCM);
350 } else {
351 throw new IllegalStateException("Unexpected phone type: " + phoneType);
352 }
353 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
354 request.result = hungUp;
355 // Wake up the requesting thread
356 synchronized (request) {
357 request.notifyAll();
358 }
359 break;
360
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700361 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700362 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700363 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800364 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700365 if (uiccCard == null) {
366 loge("iccTransmitApduLogicalChannel: No UICC");
367 request.result = new IccIoResult(0x6F, 0, (byte[])null);
368 synchronized (request) {
369 request.notifyAll();
370 }
371 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700372 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
373 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700374 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700375 iccArgument.channel, iccArgument.cla, iccArgument.command,
376 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700377 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700378 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700379 break;
380
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700381 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700382 ar = (AsyncResult) msg.obj;
383 request = (MainThreadRequest) ar.userObj;
384 if (ar.exception == null && ar.result != null) {
385 request.result = ar.result;
386 } else {
387 request.result = new IccIoResult(0x6F, 0, (byte[])null);
388 if (ar.result == null) {
389 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800390 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700391 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800392 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700393 } else {
394 loge("iccTransmitApduLogicalChannel: Unknown exception");
395 }
396 }
397 synchronized (request) {
398 request.notifyAll();
399 }
400 break;
401
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700402 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
403 request = (MainThreadRequest) msg.obj;
404 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800405 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700406 if (uiccCard == null) {
407 loge("iccTransmitApduBasicChannel: No UICC");
408 request.result = new IccIoResult(0x6F, 0, (byte[])null);
409 synchronized (request) {
410 request.notifyAll();
411 }
412 } else {
413 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
414 request);
415 uiccCard.iccTransmitApduBasicChannel(
416 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
417 iccArgument.p3, iccArgument.data, onCompleted);
418 }
419 break;
420
421 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
422 ar = (AsyncResult) msg.obj;
423 request = (MainThreadRequest) ar.userObj;
424 if (ar.exception == null && ar.result != null) {
425 request.result = ar.result;
426 } else {
427 request.result = new IccIoResult(0x6F, 0, (byte[])null);
428 if (ar.result == null) {
429 loge("iccTransmitApduBasicChannel: Empty response");
430 } else if (ar.exception instanceof CommandException) {
431 loge("iccTransmitApduBasicChannel: CommandException: " +
432 ar.exception);
433 } else {
434 loge("iccTransmitApduBasicChannel: Unknown exception");
435 }
436 }
437 synchronized (request) {
438 request.notifyAll();
439 }
440 break;
441
442 case CMD_EXCHANGE_SIM_IO:
443 request = (MainThreadRequest) msg.obj;
444 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800445 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700446 if (uiccCard == null) {
447 loge("iccExchangeSimIO: No UICC");
448 request.result = new IccIoResult(0x6F, 0, (byte[])null);
449 synchronized (request) {
450 request.notifyAll();
451 }
452 } else {
453 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
454 request);
455 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
456 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
457 iccArgument.data, onCompleted);
458 }
459 break;
460
461 case EVENT_EXCHANGE_SIM_IO_DONE:
462 ar = (AsyncResult) msg.obj;
463 request = (MainThreadRequest) ar.userObj;
464 if (ar.exception == null && ar.result != null) {
465 request.result = ar.result;
466 } else {
467 request.result = new IccIoResult(0x6f, 0, (byte[])null);
468 }
469 synchronized (request) {
470 request.notifyAll();
471 }
472 break;
473
Derek Tan4d5e5c12014-02-04 11:54:58 -0800474 case CMD_SEND_ENVELOPE:
475 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800476 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700477 if (uiccCard == null) {
478 loge("sendEnvelopeWithStatus: No UICC");
479 request.result = new IccIoResult(0x6F, 0, (byte[])null);
480 synchronized (request) {
481 request.notifyAll();
482 }
483 } else {
484 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
485 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
486 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800487 break;
488
489 case EVENT_SEND_ENVELOPE_DONE:
490 ar = (AsyncResult) msg.obj;
491 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700492 if (ar.exception == null && ar.result != null) {
493 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800494 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700495 request.result = new IccIoResult(0x6F, 0, (byte[])null);
496 if (ar.result == null) {
497 loge("sendEnvelopeWithStatus: Empty response");
498 } else if (ar.exception instanceof CommandException) {
499 loge("sendEnvelopeWithStatus: CommandException: " +
500 ar.exception);
501 } else {
502 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
503 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800504 }
505 synchronized (request) {
506 request.notifyAll();
507 }
508 break;
509
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 case CMD_OPEN_CHANNEL:
511 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800512 uiccCard = getUiccCardFromRequest(request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800513 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700514 if (uiccCard == null) {
515 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800516 request.result = new IccOpenLogicalChannelResponse(-1,
517 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700518 synchronized (request) {
519 request.notifyAll();
520 }
521 } else {
522 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800523 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
524 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700525 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700526 break;
527
528 case EVENT_OPEN_CHANNEL_DONE:
529 ar = (AsyncResult) msg.obj;
530 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700531 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700532 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700533 int[] result = (int[]) ar.result;
534 int channelId = result[0];
535 byte[] selectResponse = null;
536 if (result.length > 1) {
537 selectResponse = new byte[result.length - 1];
538 for (int i = 1; i < result.length; ++i) {
539 selectResponse[i - 1] = (byte) result[i];
540 }
541 }
542 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700543 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700544 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700545 if (ar.result == null) {
546 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700547 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700548 if (ar.exception != null) {
549 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
550 }
551
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700552 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700553 if (ar.exception instanceof CommandException) {
554 CommandException.Error error =
555 ((CommandException) (ar.exception)).getCommandError();
556 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700557 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700558 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700559 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700560 }
561 }
562 openChannelResp = new IccOpenLogicalChannelResponse(
563 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700564 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700565 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700566 synchronized (request) {
567 request.notifyAll();
568 }
569 break;
570
571 case CMD_CLOSE_CHANNEL:
572 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800573 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700574 if (uiccCard == null) {
575 loge("iccCloseLogicalChannel: No UICC");
576 request.result = new IccIoResult(0x6F, 0, (byte[])null);
577 synchronized (request) {
578 request.notifyAll();
579 }
580 } else {
581 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
582 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
583 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700584 break;
585
586 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800587 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
588 break;
589
590 case CMD_NV_READ_ITEM:
591 request = (MainThreadRequest) msg.obj;
592 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
593 mPhone.nvReadItem((Integer) request.argument, onCompleted);
594 break;
595
596 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700597 ar = (AsyncResult) msg.obj;
598 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800599 if (ar.exception == null && ar.result != null) {
600 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700601 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800602 request.result = "";
603 if (ar.result == null) {
604 loge("nvReadItem: Empty response");
605 } else if (ar.exception instanceof CommandException) {
606 loge("nvReadItem: CommandException: " +
607 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700608 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800609 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700610 }
611 }
612 synchronized (request) {
613 request.notifyAll();
614 }
615 break;
616
Jake Hambye994d462014-02-03 13:10:13 -0800617 case CMD_NV_WRITE_ITEM:
618 request = (MainThreadRequest) msg.obj;
619 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
620 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
621 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
622 break;
623
624 case EVENT_NV_WRITE_ITEM_DONE:
625 handleNullReturnEvent(msg, "nvWriteItem");
626 break;
627
628 case CMD_NV_WRITE_CDMA_PRL:
629 request = (MainThreadRequest) msg.obj;
630 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
631 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
632 break;
633
634 case EVENT_NV_WRITE_CDMA_PRL_DONE:
635 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
636 break;
637
638 case CMD_NV_RESET_CONFIG:
639 request = (MainThreadRequest) msg.obj;
640 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
641 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
642 break;
643
644 case EVENT_NV_RESET_CONFIG_DONE:
645 handleNullReturnEvent(msg, "nvResetConfig");
646 break;
647
Jake Hamby7c27be32014-03-03 13:25:59 -0800648 case CMD_GET_PREFERRED_NETWORK_TYPE:
649 request = (MainThreadRequest) msg.obj;
650 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700651 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800652 break;
653
654 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
655 ar = (AsyncResult) msg.obj;
656 request = (MainThreadRequest) ar.userObj;
657 if (ar.exception == null && ar.result != null) {
658 request.result = ar.result; // Integer
659 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800660 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800661 if (ar.result == null) {
662 loge("getPreferredNetworkType: Empty response");
663 } else if (ar.exception instanceof CommandException) {
664 loge("getPreferredNetworkType: CommandException: " +
665 ar.exception);
666 } else {
667 loge("getPreferredNetworkType: Unknown exception");
668 }
669 }
670 synchronized (request) {
671 request.notifyAll();
672 }
673 break;
674
675 case CMD_SET_PREFERRED_NETWORK_TYPE:
676 request = (MainThreadRequest) msg.obj;
677 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
678 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700679 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800680 break;
681
682 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
683 handleNullReturnEvent(msg, "setPreferredNetworkType");
684 break;
685
Steven Liu4bf01bc2014-07-17 11:05:29 -0500686 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
687 request = (MainThreadRequest)msg.obj;
688 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
689 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
690 break;
691
692 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
693 ar = (AsyncResult)msg.obj;
694 request = (MainThreadRequest)ar.userObj;
695 request.result = ar;
696 synchronized (request) {
697 request.notifyAll();
698 }
699 break;
700
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800701 case CMD_SET_VOICEMAIL_NUMBER:
702 request = (MainThreadRequest) msg.obj;
703 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
704 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800705 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
706 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800707 break;
708
709 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
710 handleNullReturnEvent(msg, "setVoicemailNumber");
711 break;
712
Stuart Scott54788802015-03-30 13:18:01 -0700713 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
714 request = (MainThreadRequest) msg.obj;
715 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
716 request);
717 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
718 break;
719
720 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
721 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
722 break;
723
Shishir Agrawal302c8692015-06-19 13:49:39 -0700724 case CMD_PERFORM_NETWORK_SCAN:
725 request = (MainThreadRequest) msg.obj;
726 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
727 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
728 break;
729
730 case EVENT_PERFORM_NETWORK_SCAN_DONE:
731 ar = (AsyncResult) msg.obj;
732 request = (MainThreadRequest) ar.userObj;
733 CellNetworkScanResult cellScanResult;
734 if (ar.exception == null && ar.result != null) {
735 cellScanResult = new CellNetworkScanResult(
736 CellNetworkScanResult.STATUS_SUCCESS,
737 (List<OperatorInfo>) ar.result);
738 } else {
739 if (ar.result == null) {
740 loge("getCellNetworkScanResults: Empty response");
741 }
742 if (ar.exception != null) {
743 loge("getCellNetworkScanResults: Exception: " + ar.exception);
744 }
745 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
746 if (ar.exception instanceof CommandException) {
747 CommandException.Error error =
748 ((CommandException) (ar.exception)).getCommandError();
749 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
750 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
751 } else if (error == CommandException.Error.GENERIC_FAILURE) {
752 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
753 }
754 }
755 cellScanResult = new CellNetworkScanResult(errorCode, null);
756 }
757 request.result = cellScanResult;
758 synchronized (request) {
759 request.notifyAll();
760 }
761 break;
762
763 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
764 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700765 ManualNetworkSelectionArgument selArg =
766 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700767 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
768 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700769 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
770 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700771 break;
772
773 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
774 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
775 break;
776
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700777 case CMD_GET_MODEM_ACTIVITY_INFO:
778 request = (MainThreadRequest) msg.obj;
779 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700780 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700781 break;
782
783 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
784 ar = (AsyncResult) msg.obj;
785 request = (MainThreadRequest) ar.userObj;
786 if (ar.exception == null && ar.result != null) {
787 request.result = ar.result;
788 } else {
789 if (ar.result == null) {
790 loge("queryModemActivityInfo: Empty response");
791 } else if (ar.exception instanceof CommandException) {
792 loge("queryModemActivityInfo: CommandException: " +
793 ar.exception);
794 } else {
795 loge("queryModemActivityInfo: Unknown exception");
796 }
797 }
Amit Mahajand4766222016-01-28 15:28:28 -0800798 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
799 if (request.result == null) {
800 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
801 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700802 synchronized (request) {
803 request.notifyAll();
804 }
805 break;
806
Meng Wang1a7c35a2016-05-05 20:56:15 -0700807 case CMD_SET_ALLOWED_CARRIERS:
808 request = (MainThreadRequest) msg.obj;
809 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
810 mPhone.setAllowedCarriers(
811 (List<CarrierIdentifier>) request.argument,
812 onCompleted);
813 break;
814
815 case EVENT_SET_ALLOWED_CARRIERS_DONE:
816 ar = (AsyncResult) msg.obj;
817 request = (MainThreadRequest) ar.userObj;
818 if (ar.exception == null && ar.result != null) {
819 request.result = ar.result;
820 } else {
821 if (ar.result == null) {
822 loge("setAllowedCarriers: Empty response");
823 } else if (ar.exception instanceof CommandException) {
824 loge("setAllowedCarriers: CommandException: " +
825 ar.exception);
826 } else {
827 loge("setAllowedCarriers: Unknown exception");
828 }
829 }
830 // Result cannot be null. Return -1 on error.
831 if (request.result == null) {
832 request.result = new int[]{-1};
833 }
834 synchronized (request) {
835 request.notifyAll();
836 }
837 break;
838
839 case CMD_GET_ALLOWED_CARRIERS:
840 request = (MainThreadRequest) msg.obj;
841 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
842 mPhone.getAllowedCarriers(onCompleted);
843 break;
844
845 case EVENT_GET_ALLOWED_CARRIERS_DONE:
846 ar = (AsyncResult) msg.obj;
847 request = (MainThreadRequest) ar.userObj;
848 if (ar.exception == null && ar.result != null) {
849 request.result = ar.result;
850 } else {
851 if (ar.result == null) {
852 loge("getAllowedCarriers: Empty response");
853 } else if (ar.exception instanceof CommandException) {
854 loge("getAllowedCarriers: CommandException: " +
855 ar.exception);
856 } else {
857 loge("getAllowedCarriers: Unknown exception");
858 }
859 }
860 // Result cannot be null. Return empty list of CarrierIdentifier.
861 if (request.result == null) {
862 request.result = new ArrayList<CarrierIdentifier>(0);
863 }
864 synchronized (request) {
865 request.notifyAll();
866 }
867 break;
868
Nathan Haroldb3014052017-01-25 15:57:32 -0800869 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
870 ar = (AsyncResult) msg.obj;
871 request = (MainThreadRequest) ar.userObj;
872 if (ar.exception == null && ar.result != null) {
873 request.result = ar.result;
874 } else {
875 request.result = new IllegalArgumentException(
876 "Failed to retrieve Forbidden Plmns");
877 if (ar.result == null) {
878 loge("getForbiddenPlmns: Empty response");
879 } else {
880 loge("getForbiddenPlmns: Unknown exception");
881 }
882 }
883 synchronized (request) {
884 request.notifyAll();
885 }
886 break;
887
888 case CMD_GET_FORBIDDEN_PLMNS:
889 request = (MainThreadRequest) msg.obj;
890 uiccCard = getUiccCardFromRequest(request);
891 if (uiccCard == null) {
892 loge("getForbiddenPlmns() UiccCard is null");
893 request.result = new IllegalArgumentException(
894 "getForbiddenPlmns() UiccCard is null");
895 synchronized (request) {
896 request.notifyAll();
897 }
898 break;
899 }
900 Integer appType = (Integer) request.argument;
901 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
902 if (uiccApp == null) {
903 loge("getForbiddenPlmns() no app with specified type -- "
904 + appType);
905 request.result = new IllegalArgumentException("Failed to get UICC App");
906 synchronized (request) {
907 request.notifyAll();
908 }
909 break;
910 } else {
911 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
912 + " specified type -- " + appType);
913 }
914 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
915 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
916 onCompleted);
917 break;
918
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700919 default:
920 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
921 break;
922 }
923 }
Jake Hambye994d462014-02-03 13:10:13 -0800924
925 private void handleNullReturnEvent(Message msg, String command) {
926 AsyncResult ar = (AsyncResult) msg.obj;
927 MainThreadRequest request = (MainThreadRequest) ar.userObj;
928 if (ar.exception == null) {
929 request.result = true;
930 } else {
931 request.result = false;
932 if (ar.exception instanceof CommandException) {
933 loge(command + ": CommandException: " + ar.exception);
934 } else {
935 loge(command + ": Unknown exception");
936 }
937 }
938 synchronized (request) {
939 request.notifyAll();
940 }
941 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 }
943
944 /**
945 * Posts the specified command to be executed on the main thread,
946 * waits for the request to complete, and returns the result.
947 * @see #sendRequestAsync
948 */
949 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800950 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700951 }
952
953 /**
954 * Posts the specified command to be executed on the main thread,
955 * waits for the request to complete, and returns the result.
956 * @see #sendRequestAsync
957 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800958 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700959 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
960 throw new RuntimeException("This method will deadlock if called from the main thread.");
961 }
962
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800963 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 Message msg = mMainThreadHandler.obtainMessage(command, request);
965 msg.sendToTarget();
966
967 // Wait for the request to complete
968 synchronized (request) {
969 while (request.result == null) {
970 try {
971 request.wait();
972 } catch (InterruptedException e) {
973 // Do nothing, go back and wait until the request is complete
974 }
975 }
976 }
977 return request.result;
978 }
979
980 /**
981 * Asynchronous ("fire and forget") version of sendRequest():
982 * Posts the specified command to be executed on the main thread, and
983 * returns immediately.
984 * @see #sendRequest
985 */
986 private void sendRequestAsync(int command) {
987 mMainThreadHandler.sendEmptyMessage(command);
988 }
989
990 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700991 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
992 * @see {@link #sendRequest(int,Object)}
993 */
994 private void sendRequestAsync(int command, Object argument) {
995 MainThreadRequest request = new MainThreadRequest(argument);
996 Message msg = mMainThreadHandler.obtainMessage(command, request);
997 msg.sendToTarget();
998 }
999
1000 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001001 * Initialize the singleton PhoneInterfaceManager instance.
1002 * This is only done once, at startup, from PhoneApp.onCreate().
1003 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001004 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001005 synchronized (PhoneInterfaceManager.class) {
1006 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001007 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001008 } else {
1009 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1010 }
1011 return sInstance;
1012 }
1013 }
1014
1015 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001016 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001017 mApp = app;
1018 mPhone = phone;
1019 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001020 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001021 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1022 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001023 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001024 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001025 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001026
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001027 publish();
1028 }
1029
1030 private void publish() {
1031 if (DBG) log("publish: " + this);
1032
1033 ServiceManager.addService("phone", this);
1034 }
1035
Stuart Scott584921c2015-01-15 17:10:34 -08001036 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001037 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1038 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001039 }
1040
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001041 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1042 Phone phone = getPhoneFromRequest(request);
1043 return phone == null ? null :
1044 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1045 }
1046
Wink Saville36469e72014-06-11 15:17:00 -07001047 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001048 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001049 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001050 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001051 //
1052 // Implementation of the ITelephony interface.
1053 //
1054
1055 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001056 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001057 }
1058
Wink Savilleb564aae2014-10-23 10:18:09 -07001059 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001060 if (DBG) log("dial: " + number);
1061 // No permission check needed here: This is just a wrapper around the
1062 // ACTION_DIAL intent, which is available to any app since it puts up
1063 // the UI before it does anything.
1064
1065 String url = createTelUrl(number);
1066 if (url == null) {
1067 return;
1068 }
1069
1070 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001071 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001072 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1073 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1074 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1075 mApp.startActivity(intent);
1076 }
1077 }
1078
1079 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001080 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001081 }
1082
Wink Savilleb564aae2014-10-23 10:18:09 -07001083 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001084 if (DBG) log("call: " + number);
1085
1086 // This is just a wrapper around the ACTION_CALL intent, but we still
1087 // need to do a permission check since we're calling startActivity()
1088 // from the context of the phone app.
1089 enforceCallPermission();
1090
1091 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1092 != AppOpsManager.MODE_ALLOWED) {
1093 return;
1094 }
1095
1096 String url = createTelUrl(number);
1097 if (url == null) {
1098 return;
1099 }
1100
Wink Saville08874612014-08-31 19:19:58 -07001101 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001102 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001103 if (slist != null) {
1104 for (SubscriptionInfo subInfoRecord : slist) {
1105 if (subInfoRecord.getSubscriptionId() == subId) {
1106 isValid = true;
1107 break;
1108 }
Wink Saville08874612014-08-31 19:19:58 -07001109 }
1110 }
1111 if (isValid == false) {
1112 return;
1113 }
1114
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001115 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001116 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001117 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1118 mApp.startActivity(intent);
1119 }
1120
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001121 /**
1122 * End a call based on call state
1123 * @return true is a call was ended
1124 */
1125 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001126 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001127 }
1128
1129 /**
1130 * End a call based on the call state of the subId
1131 * @return true is a call was ended
1132 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001133 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001134 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001135 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001136 }
1137
1138 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001139 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001140 }
1141
Wink Savilleb564aae2014-10-23 10:18:09 -07001142 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001143 if (DBG) log("answerRingingCall...");
1144 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1145 // but that can probably wait till the big TelephonyManager API overhaul.
1146 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1147 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001148 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001149 }
1150
1151 /**
1152 * Make the actual telephony calls to implement answerRingingCall().
1153 * This should only be called from the main thread of the Phone app.
1154 * @see #answerRingingCall
1155 *
1156 * TODO: it would be nice to return true if we answered the call, or
1157 * false if there wasn't actually a ringing incoming call, or some
1158 * other error occurred. (In other words, pass back the return value
1159 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1160 * But that would require calling this method via sendRequest() rather
1161 * than sendRequestAsync(), and right now we don't actually *need* that
1162 * return value, so let's just return void for now.
1163 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001164 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001165 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001166 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001167 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1168 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001169 if (hasActiveCall && hasHoldingCall) {
1170 // Both lines are in use!
1171 // TODO: provide a flag to let the caller specify what
1172 // policy to use if both lines are in use. (The current
1173 // behavior is hardwired to "answer incoming, end ongoing",
1174 // which is how the CALL button is specced to behave.)
1175 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1176 return;
1177 } else {
1178 // answerCall() will automatically hold the current active
1179 // call, if there is one.
1180 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1181 return;
1182 }
1183 } else {
1184 // No call was ringing.
1185 return;
1186 }
1187 }
1188
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001189 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001190 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001191 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001192 public void silenceRinger() {
1193 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 }
1195
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001196 @Override
1197 public boolean isOffhook(String callingPackage) {
1198 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001199 }
1200
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001201 @Override
1202 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1203 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1204 return false;
1205 }
1206
Sanket Padawe356d7632015-06-22 14:03:32 -07001207 final Phone phone = getPhone(subId);
1208 if (phone != null) {
1209 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1210 } else {
1211 return false;
1212 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 }
1214
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001215 @Override
1216 public boolean isRinging(String callingPackage) {
1217 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001218 }
1219
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001220 @Override
1221 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1222 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1223 return false;
1224 }
1225
Sanket Padawe356d7632015-06-22 14:03:32 -07001226 final Phone phone = getPhone(subId);
1227 if (phone != null) {
1228 return (phone.getState() == PhoneConstants.State.RINGING);
1229 } else {
1230 return false;
1231 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001232 }
1233
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001234 @Override
1235 public boolean isIdle(String callingPackage) {
1236 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001237 }
1238
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001239 @Override
1240 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1241 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1242 return false;
1243 }
1244
Sanket Padawe356d7632015-06-22 14:03:32 -07001245 final Phone phone = getPhone(subId);
1246 if (phone != null) {
1247 return (phone.getState() == PhoneConstants.State.IDLE);
1248 } else {
1249 return false;
1250 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001251 }
1252
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001253 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001254 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001255 }
1256
Wink Savilleb564aae2014-10-23 10:18:09 -07001257 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001258 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001259 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1260 }
1261
1262 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001263 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001264 }
1265
Wink Savilleb564aae2014-10-23 10:18:09 -07001266 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001267 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001268 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1269 }
1270
1271 /** {@hide} */
1272 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001273 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001274 }
1275
Wink Savilleb564aae2014-10-23 10:18:09 -07001276 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001278 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001279 checkSimPin.start();
1280 return checkSimPin.unlockSim(null, pin);
1281 }
1282
Wink Saville9de0f752013-10-22 19:04:03 -07001283 /** {@hide} */
1284 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001285 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001286 }
1287
Wink Savilleb564aae2014-10-23 10:18:09 -07001288 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001289 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001290 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001291 checkSimPuk.start();
1292 return checkSimPuk.unlockSim(puk, pin);
1293 }
1294
1295 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001296 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001297 * a synchronous one.
1298 */
1299 private static class UnlockSim extends Thread {
1300
1301 private final IccCard mSimCard;
1302
1303 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001304 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1305 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306
1307 // For replies from SimCard interface
1308 private Handler mHandler;
1309
1310 // For async handler to identify request type
1311 private static final int SUPPLY_PIN_COMPLETE = 100;
1312
1313 public UnlockSim(IccCard simCard) {
1314 mSimCard = simCard;
1315 }
1316
1317 @Override
1318 public void run() {
1319 Looper.prepare();
1320 synchronized (UnlockSim.this) {
1321 mHandler = new Handler() {
1322 @Override
1323 public void handleMessage(Message msg) {
1324 AsyncResult ar = (AsyncResult) msg.obj;
1325 switch (msg.what) {
1326 case SUPPLY_PIN_COMPLETE:
1327 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1328 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001329 mRetryCount = msg.arg1;
1330 if (ar.exception != null) {
1331 if (ar.exception instanceof CommandException &&
1332 ((CommandException)(ar.exception)).getCommandError()
1333 == CommandException.Error.PASSWORD_INCORRECT) {
1334 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1335 } else {
1336 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1337 }
1338 } else {
1339 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1340 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001341 mDone = true;
1342 UnlockSim.this.notifyAll();
1343 }
1344 break;
1345 }
1346 }
1347 };
1348 UnlockSim.this.notifyAll();
1349 }
1350 Looper.loop();
1351 }
1352
1353 /*
1354 * Use PIN or PUK to unlock SIM card
1355 *
1356 * If PUK is null, unlock SIM card with PIN
1357 *
1358 * If PUK is not null, unlock SIM card with PUK and set PIN code
1359 */
Wink Saville9de0f752013-10-22 19:04:03 -07001360 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001361
1362 while (mHandler == null) {
1363 try {
1364 wait();
1365 } catch (InterruptedException e) {
1366 Thread.currentThread().interrupt();
1367 }
1368 }
1369 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1370
1371 if (puk == null) {
1372 mSimCard.supplyPin(pin, callback);
1373 } else {
1374 mSimCard.supplyPuk(puk, pin, callback);
1375 }
1376
1377 while (!mDone) {
1378 try {
1379 Log.d(LOG_TAG, "wait for done");
1380 wait();
1381 } catch (InterruptedException e) {
1382 // Restore the interrupted status
1383 Thread.currentThread().interrupt();
1384 }
1385 }
1386 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001387 int[] resultArray = new int[2];
1388 resultArray[0] = mResult;
1389 resultArray[1] = mRetryCount;
1390 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001391 }
1392 }
1393
1394 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001395 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001396
1397 }
1398
Wink Savilleb564aae2014-10-23 10:18:09 -07001399 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001400 // No permission check needed here: this call is harmless, and it's
1401 // needed for the ServiceState.requestStateUpdate() call (which is
1402 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001403 final Phone phone = getPhone(subId);
1404 if (phone != null) {
1405 phone.updateServiceLocation();
1406 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001407 }
1408
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001409 @Override
1410 public boolean isRadioOn(String callingPackage) {
1411 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001412 }
1413
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001414 @Override
1415 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1416 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1417 return false;
1418 }
1419 return isRadioOnForSubscriber(subId);
1420 }
1421
1422 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001423 final Phone phone = getPhone(subId);
1424 if (phone != null) {
1425 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1426 } else {
1427 return false;
1428 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001429 }
1430
1431 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001432 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001433
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 }
Wink Saville36469e72014-06-11 15:17:00 -07001435
Wink Savilleb564aae2014-10-23 10:18:09 -07001436 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001437 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001438 final Phone phone = getPhone(subId);
1439 if (phone != null) {
1440 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1441 }
Wink Saville36469e72014-06-11 15:17:00 -07001442 }
1443
1444 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001445 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001446 }
1447
Wink Savilleb564aae2014-10-23 10:18:09 -07001448 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001449 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001450 final Phone phone = getPhone(subId);
1451 if (phone == null) {
1452 return false;
1453 }
1454 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001455 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001456 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 }
1458 return true;
1459 }
Wink Saville36469e72014-06-11 15:17:00 -07001460
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001461 public boolean needMobileRadioShutdown() {
1462 /*
1463 * If any of the Radios are available, it will need to be
1464 * shutdown. So return true if any Radio is available.
1465 */
1466 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1467 Phone phone = PhoneFactory.getPhone(i);
1468 if (phone != null && phone.isRadioAvailable()) return true;
1469 }
1470 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1471 return false;
1472 }
1473
1474 public void shutdownMobileRadios() {
1475 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1476 logv("Shutting down Phone " + i);
1477 shutdownRadioUsingPhoneId(i);
1478 }
1479 }
1480
1481 private void shutdownRadioUsingPhoneId(int phoneId) {
1482 enforceModifyPermission();
1483 Phone phone = PhoneFactory.getPhone(phoneId);
1484 if (phone != null && phone.isRadioAvailable()) {
1485 phone.shutdownRadio();
1486 }
1487 }
1488
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001489 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001490 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1491 if (defaultPhone != null) {
1492 defaultPhone.setRadioPower(turnOn);
1493 return true;
1494 } else {
1495 loge("There's no default phone.");
1496 return false;
1497 }
Wink Saville36469e72014-06-11 15:17:00 -07001498 }
1499
Wink Savilleb564aae2014-10-23 10:18:09 -07001500 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001501 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001502 final Phone phone = getPhone(subId);
1503 if (phone != null) {
1504 phone.setRadioPower(turnOn);
1505 return true;
1506 } else {
1507 return false;
1508 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001509 }
1510
Wink Saville36469e72014-06-11 15:17:00 -07001511 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001512 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001513 public boolean enableDataConnectivity() {
1514 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001515 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001516 final Phone phone = getPhone(subId);
1517 if (phone != null) {
1518 phone.setDataEnabled(true);
1519 return true;
1520 } else {
1521 return false;
1522 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001523 }
1524
Wink Saville36469e72014-06-11 15:17:00 -07001525 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001526 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001527 public boolean disableDataConnectivity() {
1528 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001529 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001530 final Phone phone = getPhone(subId);
1531 if (phone != null) {
1532 phone.setDataEnabled(false);
1533 return true;
1534 } else {
1535 return false;
1536 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001537 }
1538
Wink Saville36469e72014-06-11 15:17:00 -07001539 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001540 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001541 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001542 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001543 final Phone phone = getPhone(subId);
1544 if (phone != null) {
1545 return phone.isDataConnectivityPossible();
1546 } else {
1547 return false;
1548 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001549 }
1550
1551 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001552 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001553 }
1554
pkanwarf8520782016-11-06 20:37:09 -08001555 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar7e856482016-10-14 19:37:38 -07001556 enforceCallPermission();
pkanwar7e856482016-10-14 19:37:38 -07001557 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1558 return;
1559 }
1560 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1561 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1562 };
1563
Wink Savilleb564aae2014-10-23 10:18:09 -07001564 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001565 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001566 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1567 return false;
1568 }
Wink Saville36469e72014-06-11 15:17:00 -07001569 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001570 }
1571
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001572 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001573 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001574 }
1575
Sanket Padawe5780e442017-03-20 15:04:47 -07001576 public int getCallStateForSlot(int slotIndex) {
1577 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001578 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1579 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001580 }
1581
Sanket Padawe356d7632015-06-22 14:03:32 -07001582 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001584 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001585 if (phone != null) {
1586 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1587 } else {
1588 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1589 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 }
1591
Sanket Padawe356d7632015-06-22 14:03:32 -07001592 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001593 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001594 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001595 if (phone != null) {
1596 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1597 } else {
1598 return TelephonyManager.DATA_ACTIVITY_NONE;
1599 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001600 }
1601
1602 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001603 public Bundle getCellLocation(String callingPackage) {
1604 enforceFineOrCoarseLocationPermission("getCellLocation");
1605
1606 // OP_COARSE_LOCATION controls both fine and coarse location.
1607 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1608 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001609 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001610 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001611 }
1612
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001613 if (checkIfCallerIsSelfOrForegroundUser() ||
1614 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001615 if (DBG_LOC) log("getCellLocation: is active user");
1616 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001617 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001618 if (phone == null) {
1619 return null;
1620 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07001621
1622 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1623 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001624 return data;
1625 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001626 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001627 return null;
1628 }
1629 }
1630
Svetoslav64fad262015-04-14 14:35:21 -07001631 private void enforceFineOrCoarseLocationPermission(String message) {
1632 try {
1633 mApp.enforceCallingOrSelfPermission(
1634 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1635 } catch (SecurityException e) {
1636 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1637 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1638 // is the weaker precondition
1639 mApp.enforceCallingOrSelfPermission(
1640 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1641 }
1642 }
1643
1644
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001645 @Override
1646 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001647 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001648 }
1649
Sanket Padawe356d7632015-06-22 14:03:32 -07001650 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001651 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001652 mApp.enforceCallingOrSelfPermission(
1653 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001654 final Phone phone = getPhone(subId);
1655 if (phone != null) {
1656 phone.enableLocationUpdates();
1657 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001658 }
1659
1660 @Override
1661 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001662 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001663 }
1664
Sanket Padawe356d7632015-06-22 14:03:32 -07001665 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001666 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001667 mApp.enforceCallingOrSelfPermission(
1668 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001669 final Phone phone = getPhone(subId);
1670 if (phone != null) {
1671 phone.disableLocationUpdates();
1672 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001673 }
1674
1675 @Override
1676 @SuppressWarnings("unchecked")
1677 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001678 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1679
1680 // OP_COARSE_LOCATION controls both fine and coarse location.
1681 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1682 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1683 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001684 }
1685
1686 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1687 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1688 return null;
1689 }
Svetoslav64fad262015-04-14 14:35:21 -07001690
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001691 if (checkIfCallerIsSelfOrForegroundUser() ||
1692 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001693 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1694
1695 ArrayList<NeighboringCellInfo> cells = null;
1696
Sooraj Sasindran22882212016-07-18 11:57:25 -07001697 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001698 try {
1699 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran22882212016-07-18 11:57:25 -07001700 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001701 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001702 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001703 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001704 }
1705 return cells;
1706 } else {
1707 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1708 return null;
1709 }
1710 }
1711
1712
1713 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001714 public List<CellInfo> getAllCellInfo(String callingPackage) {
1715 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1716
1717 // OP_COARSE_LOCATION controls both fine and coarse location.
1718 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1719 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1720 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001721 }
1722
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001723 if (checkIfCallerIsSelfOrForegroundUser() ||
1724 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001725 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran22882212016-07-18 11:57:25 -07001726 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001727 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1728 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran22882212016-07-18 11:57:25 -07001729 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1730 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001731 }
1732 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001733 } else {
1734 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1735 return null;
1736 }
1737 }
1738
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001739 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001740 public void setCellInfoListRate(int rateInMillis) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001741 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001742 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1743 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001744 }
1745
Shishir Agrawala9f32182016-04-12 12:00:16 -07001746 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001747 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001748 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1749 return null;
1750 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001751 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001752 return phone == null ? null : phone.getImei();
1753 }
1754
1755 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001756 public String getMeidForSlot(int slotIndex, String callingPackage) {
1757 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1758 return null;
1759 }
1760 Phone phone = PhoneFactory.getPhone(slotIndex);
1761 return phone == null ? null : phone.getMeid();
1762 }
1763
1764 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001765 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001766 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1767 return null;
1768 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001769 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001770 return phone == null ? null : phone.getDeviceSvn();
1771 }
1772
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001773 //
1774 // Internal helper methods.
1775 //
1776
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001777 /**
1778 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1779 */
1780 private boolean checkCallerInteractAcrossUsersFull() {
1781 return mPhone.getContext().checkCallingOrSelfPermission(
1782 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1783 == PackageManager.PERMISSION_GRANTED;
1784 }
1785
Jake Hambye994d462014-02-03 13:10:13 -08001786 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001787 boolean ok;
1788
1789 boolean self = Binder.getCallingUid() == Process.myUid();
1790 if (!self) {
1791 // Get the caller's user id then clear the calling identity
1792 // which will be restored in the finally clause.
1793 int callingUser = UserHandle.getCallingUserId();
1794 long ident = Binder.clearCallingIdentity();
1795
1796 try {
1797 // With calling identity cleared the current user is the foreground user.
1798 int foregroundUser = ActivityManager.getCurrentUser();
1799 ok = (foregroundUser == callingUser);
1800 if (DBG_LOC) {
1801 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1802 + " callingUser=" + callingUser + " ok=" + ok);
1803 }
1804 } catch (Exception ex) {
1805 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1806 ok = false;
1807 } finally {
1808 Binder.restoreCallingIdentity(ident);
1809 }
1810 } else {
1811 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1812 ok = true;
1813 }
1814 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1815 return ok;
1816 }
1817
1818 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001819 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1820 *
1821 * @throws SecurityException if the caller does not have the required permission
1822 */
1823 private void enforceModifyPermission() {
1824 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1825 }
1826
1827 /**
Junda Liua2e36012014-07-09 18:30:01 -07001828 * Make sure either system app or the caller has carrier privilege.
1829 *
1830 * @throws SecurityException if the caller does not have the required permission/privilege
1831 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001832 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001833 int permission = mApp.checkCallingOrSelfPermission(
1834 android.Manifest.permission.MODIFY_PHONE_STATE);
1835 if (permission == PackageManager.PERMISSION_GRANTED) {
1836 return;
1837 }
1838
1839 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001840 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001841 }
1842
1843 /**
1844 * Make sure the caller has carrier privilege.
1845 *
1846 * @throws SecurityException if the caller does not have the required permission
1847 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001848 private void enforceCarrierPrivilege(int subId) {
1849 if (getCarrierPrivilegeStatus(subId) !=
1850 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001851 loge("No Carrier Privilege.");
1852 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001853 }
1854 }
1855
1856 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001857 * Make sure the caller has the CALL_PHONE permission.
1858 *
1859 * @throws SecurityException if the caller does not have the required permission
1860 */
1861 private void enforceCallPermission() {
1862 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1863 }
1864
Stuart Scott8eef64f2015-04-08 15:13:54 -07001865 private void enforceConnectivityInternalPermission() {
1866 mApp.enforceCallingOrSelfPermission(
1867 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1868 "ConnectivityService");
1869 }
1870
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001871 private String createTelUrl(String number) {
1872 if (TextUtils.isEmpty(number)) {
1873 return null;
1874 }
1875
Jake Hambye994d462014-02-03 13:10:13 -08001876 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001877 }
1878
Ihab Awadf9e92732013-12-05 18:02:52 -08001879 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001880 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1881 }
1882
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001883 private static void logv(String msg) {
1884 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1885 }
1886
Ihab Awadf9e92732013-12-05 18:02:52 -08001887 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001888 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1889 }
1890
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001891 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001892 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001893 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001894 }
1895
Sanket Padawe356d7632015-06-22 14:03:32 -07001896 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001897 public int getActivePhoneTypeForSlot(int slotIndex) {
1898 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001899 if (phone == null) {
1900 return PhoneConstants.PHONE_TYPE_NONE;
1901 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001902 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001903 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001904 }
1905
1906 /**
1907 * Returns the CDMA ERI icon index to display
1908 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001909 @Override
1910 public int getCdmaEriIconIndex(String callingPackage) {
1911 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001912 }
1913
Sanket Padawe356d7632015-06-22 14:03:32 -07001914 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001915 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1916 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1917 return -1;
1918 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001919 final Phone phone = getPhone(subId);
1920 if (phone != null) {
1921 return phone.getCdmaEriIconIndex();
1922 } else {
1923 return -1;
1924 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001925 }
1926
1927 /**
1928 * Returns the CDMA ERI icon mode,
1929 * 0 - ON
1930 * 1 - FLASHING
1931 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001932 @Override
1933 public int getCdmaEriIconMode(String callingPackage) {
1934 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001935 }
1936
Sanket Padawe356d7632015-06-22 14:03:32 -07001937 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001938 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1939 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1940 return -1;
1941 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001942 final Phone phone = getPhone(subId);
1943 if (phone != null) {
1944 return phone.getCdmaEriIconMode();
1945 } else {
1946 return -1;
1947 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001948 }
1949
1950 /**
1951 * Returns the CDMA ERI text,
1952 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001953 @Override
1954 public String getCdmaEriText(String callingPackage) {
1955 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001956 }
1957
Sanket Padawe356d7632015-06-22 14:03:32 -07001958 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001959 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1960 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1961 return null;
1962 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001963 final Phone phone = getPhone(subId);
1964 if (phone != null) {
1965 return phone.getCdmaEriText();
1966 } else {
1967 return null;
1968 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001969 }
1970
1971 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001972 * Returns the CDMA MDN.
1973 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001974 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001975 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001976 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001977 final Phone phone = getPhone(subId);
1978 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1979 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001980 } else {
1981 return null;
1982 }
1983 }
1984
1985 /**
1986 * Returns the CDMA MIN.
1987 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001988 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001989 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001990 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001991 final Phone phone = getPhone(subId);
1992 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1993 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001994 } else {
1995 return null;
1996 }
1997 }
1998
1999 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002000 * Returns true if CDMA provisioning needs to run.
2001 */
2002 public boolean needsOtaServiceProvisioning() {
2003 return mPhone.needsOtaServiceProvisioning();
2004 }
2005
2006 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002007 * Sets the voice mail number of a given subId.
2008 */
2009 @Override
2010 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002011 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002012 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2013 new Pair<String, String>(alphaTag, number), new Integer(subId));
2014 return success;
2015 }
2016
Ta-wei Yen87c49842016-05-13 21:19:52 -07002017 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07002018 public void setVisualVoicemailEnabled(String callingPackage,
2019 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
2020 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2021 if (!TextUtils.equals(callingPackage,
2022 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2023 enforceModifyPermissionOrCarrierPrivilege(
2024 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
2025 }
2026 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2027 }
2028
2029 @Override
2030 public boolean isVisualVoicemailEnabled(String callingPackage,
2031 PhoneAccountHandle phoneAccountHandle) {
2032 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2033 return false;
2034 }
2035 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2036 }
2037
2038 @Override
Ta-wei Yen243b6372017-01-10 16:17:08 -08002039 public String getVisualVoicemailPackageName(String callingPackage,
2040 @Nullable PhoneAccountHandle phoneAccountHandle) {
2041 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2042 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2043 return null;
2044 }
2045 int subId = PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
2046 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2047 }
2048
2049 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002050 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2051 VisualVoicemailSmsFilterSettings settings) {
2052 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002053 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002054 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2055 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002056 }
2057
2058 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002059 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2060 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002061 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002062 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002063 }
2064
2065 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002066 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2067 String callingPackage, int subId) {
2068 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002069 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002070 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002071 }
2072
2073 @Override
Ta-wei Yen1b455992016-12-27 14:52:32 -08002074 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002075 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002076 return VisualVoicemailSmsFilterConfig
Ta-wei Yen1b455992016-12-27 14:52:32 -08002077 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2078 }
2079
2080 @Override
2081 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2082 String number, int port, String text, PendingIntent sentIntent) {
2083 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen463efd12017-01-06 15:29:25 -08002084 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen1b455992016-12-27 14:52:32 -08002085 enforceSendSmsPermission();
2086 // Make the calls as the phone process.
2087 final long identity = Binder.clearCallingIdentity();
2088 try {
2089 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2090 if (port == 0) {
2091 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2092 sentIntent, null, false);
2093 } else {
2094 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2095 smsManager.sendDataMessageWithSelfPermissions(number, null,
2096 (short) port, data, sentIntent, null);
2097 }
2098 } finally {
2099 Binder.restoreCallingIdentity(identity);
2100 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002101 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002102 /**
fionaxuef039d42016-11-14 13:36:14 -08002103 * Sets the voice activation state of a given subId.
2104 */
2105 @Override
2106 public void setVoiceActivationState(int subId, int activationState) {
2107 enforceModifyPermissionOrCarrierPrivilege(subId);
2108 final Phone phone = getPhone(subId);
2109 if (phone != null) {
2110 phone.setVoiceActivationState(activationState);
2111 } else {
2112 loge("setVoiceActivationState fails with invalid subId: " + subId);
2113 }
2114 }
2115
2116 /**
2117 * Sets the data activation state of a given subId.
2118 */
2119 @Override
2120 public void setDataActivationState(int subId, int activationState) {
2121 enforceModifyPermissionOrCarrierPrivilege(subId);
2122 final Phone phone = getPhone(subId);
2123 if (phone != null) {
2124 phone.setDataActivationState(activationState);
2125 } else {
2126 loge("setVoiceActivationState fails with invalid subId: " + subId);
2127 }
2128 }
2129
2130 /**
2131 * Returns the voice activation state of a given subId.
2132 */
2133 @Override
2134 public int getVoiceActivationState(int subId, String callingPackage) {
2135 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2136 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2137 }
2138 final Phone phone = getPhone(subId);
2139 if (phone != null) {
2140 return phone.getVoiceActivationState();
2141 } else {
2142 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2143 }
2144 }
2145
2146 /**
2147 * Returns the data activation state of a given subId.
2148 */
2149 @Override
2150 public int getDataActivationState(int subId, String callingPackage) {
2151 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2152 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2153 }
2154 final Phone phone = getPhone(subId);
2155 if (phone != null) {
2156 return phone.getDataActivationState();
2157 } else {
2158 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2159 }
2160 }
2161
2162 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002163 * Returns the unread count of voicemails
2164 */
2165 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002166 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002167 }
2168
2169 /**
2170 * Returns the unread count of voicemails for a subId
2171 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002172 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002173 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002174 final Phone phone = getPhone(subId);
2175 if (phone != null) {
2176 return phone.getVoiceMessageCount();
2177 } else {
2178 return 0;
2179 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002180 }
2181
2182 /**
pkanwar8efe6572017-01-19 13:43:16 -08002183 * returns true, if the device is in a state where both voice and data
2184 * are supported simultaneously. This can change based on location or network condition.
2185 */
2186 @Override
2187 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2188 final Phone phone = getPhone(subId);
2189 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2190 }
2191
2192 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002193 * Returns the data network type.
2194 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002195 *
2196 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2197 */
2198 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002199 public int getNetworkType() {
2200 final Phone phone = getPhone(getDefaultSubscription());
2201 if (phone != null) {
2202 return phone.getServiceState().getDataNetworkType();
2203 } else {
2204 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2205 }
Wink Saville36469e72014-06-11 15:17:00 -07002206 }
2207
2208 /**
2209 * Returns the network type for a subId
2210 */
2211 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002212 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2213 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2214 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2215 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002216
Sanket Padawe356d7632015-06-22 14:03:32 -07002217 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002218 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002219 return phone.getServiceState().getDataNetworkType();
2220 } else {
2221 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2222 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002223 }
2224
2225 /**
2226 * Returns the data network type
2227 */
2228 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002229 public int getDataNetworkType(String callingPackage) {
2230 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002231 }
2232
2233 /**
2234 * Returns the data network type for a subId
2235 */
2236 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002237 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2238 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2239 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2240 }
2241
Sanket Padawe356d7632015-06-22 14:03:32 -07002242 final Phone phone = getPhone(subId);
2243 if (phone != null) {
2244 return phone.getServiceState().getDataNetworkType();
2245 } else {
2246 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2247 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002248 }
2249
2250 /**
Wink Saville36469e72014-06-11 15:17:00 -07002251 * Returns the Voice network type for a subId
2252 */
2253 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002254 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2255 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2256 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2257 }
2258
Sanket Padawe356d7632015-06-22 14:03:32 -07002259 final Phone phone = getPhone(subId);
2260 if (phone != null) {
2261 return phone.getServiceState().getVoiceNetworkType();
2262 } else {
2263 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2264 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002265 }
2266
2267 /**
2268 * @return true if a ICC card is present
2269 */
2270 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002271 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002272 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2273 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002274 }
2275
2276 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002277 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002278 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002279 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002280 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2281 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002282 final Phone phone = getPhone(subId[0]);
2283 if (subId != null && phone != null) {
2284 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002285 } else {
2286 return false;
2287 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002288 }
2289
2290 /**
2291 * Return if the current radio is LTE on CDMA. This
2292 * is a tri-state return value as for a period of time
2293 * the mode may be unknown.
2294 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002295 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002296 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002297 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002298 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002299 @Override
2300 public int getLteOnCdmaMode(String callingPackage) {
2301 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002302 }
2303
Sanket Padawe356d7632015-06-22 14:03:32 -07002304 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002305 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2306 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2307 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2308 }
2309
Sanket Padawe356d7632015-06-22 14:03:32 -07002310 final Phone phone = getPhone(subId);
2311 if (phone == null) {
2312 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2313 } else {
2314 return phone.getLteOnCdmaMode();
2315 }
Wink Saville36469e72014-06-11 15:17:00 -07002316 }
2317
2318 public void setPhone(Phone phone) {
2319 mPhone = phone;
2320 }
2321
2322 /**
2323 * {@hide}
2324 * Returns Default subId, 0 in the case of single standby.
2325 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002326 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002327 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002328 }
2329
Shishir Agrawala9f32182016-04-12 12:00:16 -07002330 private int getSlotForDefaultSubscription() {
2331 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2332 }
2333
Wink Savilleb564aae2014-10-23 10:18:09 -07002334 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002335 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002336 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002337
2338 /**
2339 * @see android.telephony.TelephonyManager.WifiCallingChoices
2340 */
2341 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002342 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2343 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002344 }
2345
2346 /**
2347 * @see android.telephony.TelephonyManager.WifiCallingChoices
2348 */
2349 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002350 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2351 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2352 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002353 }
2354
Sailesh Nepald1e68152013-12-12 19:08:02 -08002355 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002356 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002357 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002358 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002359
Shishir Agrawal566b7612013-10-28 14:41:00 -07002360 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002361 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002362 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002363
Ajay Nambiabd73502015-12-03 13:50:00 -08002364 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002365 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002366 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002367 if (DBG) log("iccOpenLogicalChannel: " + response);
2368 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002369 }
2370
2371 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002372 public boolean iccCloseLogicalChannel(int subId, int channel) {
2373 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002374
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002375 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002376 if (channel < 0) {
2377 return false;
2378 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002379 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002380 if (DBG) log("iccCloseLogicalChannel: " + success);
2381 return success;
2382 }
2383
2384 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002385 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002386 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002387 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002388
2389 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002390 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2391 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002392 " data=" + data);
2393 }
2394
2395 if (channel < 0) {
2396 return "";
2397 }
2398
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002399 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002400 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002401 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2402
Shishir Agrawal566b7612013-10-28 14:41:00 -07002403 // Append the returned status code to the end of the response payload.
2404 String s = Integer.toHexString(
2405 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002406 if (response.payload != null) {
2407 s = IccUtils.bytesToHexString(response.payload) + s;
2408 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002409 return s;
2410 }
Jake Hambye994d462014-02-03 13:10:13 -08002411
Evan Charltonc66da362014-05-16 14:06:40 -07002412 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002413 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002414 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002415 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002416
2417 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002418 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2419 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002420 }
2421
2422 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002423 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002424 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2425
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002426 // Append the returned status code to the end of the response payload.
2427 String s = Integer.toHexString(
2428 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002429 if (response.payload != null) {
2430 s = IccUtils.bytesToHexString(response.payload) + s;
2431 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002432 return s;
2433 }
2434
2435 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002436 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002437 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002438 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002439
2440 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002441 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002442 p1 + " " + p2 + " " + p3 + ":" + filePath);
2443 }
2444
2445 IccIoResult response =
2446 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002447 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2448 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002449
2450 if (DBG) {
2451 log("Exchange SIM_IO [R]" + response);
2452 }
2453
2454 byte[] result = null;
2455 int length = 2;
2456 if (response.payload != null) {
2457 length = 2 + response.payload.length;
2458 result = new byte[length];
2459 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2460 } else {
2461 result = new byte[length];
2462 }
2463
2464 result[length - 1] = (byte) response.sw2;
2465 result[length - 2] = (byte) response.sw1;
2466 return result;
2467 }
2468
Nathan Haroldb3014052017-01-25 15:57:32 -08002469 /**
2470 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2471 * on a particular subscription
2472 */
2473 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002474 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2475 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002476 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2477 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2478 return null;
2479 }
2480 Object response = sendRequest(
2481 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2482 if (response instanceof String[]) {
2483 return (String[]) response;
2484 }
2485 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2486 return null;
2487 }
2488
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002489 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002490 public String sendEnvelopeWithStatus(int subId, String content) {
2491 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002492
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002493 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002494 if (response.payload == null) {
2495 return "";
2496 }
2497
2498 // Append the returned status code to the end of the response payload.
2499 String s = Integer.toHexString(
2500 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2501 s = IccUtils.bytesToHexString(response.payload) + s;
2502 return s;
2503 }
2504
Jake Hambye994d462014-02-03 13:10:13 -08002505 /**
2506 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2507 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2508 *
2509 * @param itemID the ID of the item to read
2510 * @return the NV item as a String, or null on error.
2511 */
2512 @Override
2513 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002514 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002515 if (DBG) log("nvReadItem: item " + itemID);
2516 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2517 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2518 return value;
2519 }
2520
2521 /**
2522 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2523 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2524 *
2525 * @param itemID the ID of the item to read
2526 * @param itemValue the value to write, as a String
2527 * @return true on success; false on any failure
2528 */
2529 @Override
2530 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002531 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002532 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2533 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2534 new Pair<Integer, String>(itemID, itemValue));
2535 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2536 return success;
2537 }
2538
2539 /**
2540 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2541 * Used for device configuration by some CDMA operators.
2542 *
2543 * @param preferredRoamingList byte array containing the new PRL
2544 * @return true on success; false on any failure
2545 */
2546 @Override
2547 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002548 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002549 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2550 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2551 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2552 return success;
2553 }
2554
2555 /**
2556 * Perform the specified type of NV config reset.
2557 * Used for device configuration by some CDMA operators.
2558 *
2559 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2560 * @return true on success; false on any failure
2561 */
2562 @Override
2563 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002564 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002565 if (DBG) log("nvResetConfig: type " + resetType);
2566 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2567 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2568 return success;
2569 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002570
2571 /**
Wink Saville36469e72014-06-11 15:17:00 -07002572 * {@hide}
2573 * Returns Default sim, 0 in the case of single standby.
2574 */
2575 public int getDefaultSim() {
2576 //TODO Need to get it from Telephony Devcontroller
2577 return 0;
2578 }
2579
Svet Ganovb320e182015-04-16 12:30:10 -07002580 public String[] getPcscfAddress(String apnType, String callingPackage) {
2581 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2582 return new String[0];
2583 }
2584
2585
ram87fca6f2014-07-18 18:58:44 +05302586 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002587 }
2588
Brad Ebinger76681002017-01-23 13:50:20 -08002589 /**
2590 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2591 * feature or {@link null} if the service is not available. If an ImsServiceController is
2592 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2593 * feature updates.
2594 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002595 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002596 IImsServiceFeatureListener callback) {
2597 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002598 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002599 callback);
2600 }
2601
Wink Saville36469e72014-06-11 15:17:00 -07002602 public void setImsRegistrationState(boolean registered) {
2603 enforceModifyPermission();
2604 mPhone.setImsRegistrationState(registered);
2605 }
2606
2607 /**
Stuart Scott54788802015-03-30 13:18:01 -07002608 * Set the network selection mode to automatic.
2609 *
2610 */
2611 @Override
2612 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002613 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002614 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2615 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2616 }
2617
2618 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002619 * Set the network selection mode to manual with the selected carrier.
2620 */
2621 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002622 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2623 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002624 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002625 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002626 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2627 persistSelection);
2628 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002629 }
2630
2631 /**
2632 * Scans for available networks.
2633 */
2634 @Override
2635 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002636 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002637 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2638 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2639 CMD_PERFORM_NETWORK_SCAN, null, subId);
2640 return result;
2641 }
2642
2643 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002644 * Get the calculated preferred network type.
2645 * Used for debugging incorrect network type.
2646 *
2647 * @return the preferred network type, defined in RILConstants.java.
2648 */
2649 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002650 public int getCalculatedPreferredNetworkType(String callingPackage) {
2651 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2652 return RILConstants.PREFERRED_NETWORK_MODE;
2653 }
2654
Amit Mahajan43330e02014-11-18 11:54:45 -08002655 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002656 }
2657
2658 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002659 * Get the preferred network type.
2660 * Used for device configuration by some CDMA operators.
2661 *
2662 * @return the preferred network type, defined in RILConstants.java.
2663 */
2664 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002665 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002666 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002667 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002668 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002669 int networkType = (result != null ? result[0] : -1);
2670 if (DBG) log("getPreferredNetworkType: " + networkType);
2671 return networkType;
2672 }
2673
2674 /**
2675 * Set the preferred network type.
2676 * Used for device configuration by some CDMA operators.
2677 *
2678 * @param networkType the preferred network type, defined in RILConstants.java.
2679 * @return true on success; false on any failure.
2680 */
2681 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002682 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002683 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002684 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2685 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002686 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002687 if (success) {
2688 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002689 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002690 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002691 return success;
2692 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002693
2694 /**
Junda Liu475951f2014-11-07 16:45:03 -08002695 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2696 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2697 * tethering.
2698 *
2699 * @return 0: Not required. 1: required. 2: Not set.
2700 * @hide
2701 */
2702 @Override
2703 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002704 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002705 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2706 Settings.Global.TETHER_DUN_REQUIRED, 2);
2707 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2708 // config_tether_apndata.
2709 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2710 dunRequired = 1;
2711 }
2712 return dunRequired;
2713 }
2714
2715 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002716 * Set mobile data enabled
2717 * Used by the user through settings etc to turn on/off mobile data
2718 *
2719 * @param enable {@code true} turn turn data on, else {@code false}
2720 */
2721 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002722 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002723 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002724 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002725 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002726 Phone phone = PhoneFactory.getPhone(phoneId);
2727 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002728 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002729 phone.setDataEnabled(enable);
2730 } else {
2731 loge("setDataEnabled: no phone for subId=" + subId);
2732 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002733 }
2734
2735 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002736 * Get whether mobile data is enabled.
2737 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002738 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002739 *
2740 * @return {@code true} if data is enabled else {@code false}
2741 */
2742 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002743 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002744 try {
2745 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2746 null);
2747 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002748 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002749 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002750 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002751 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002752 Phone phone = PhoneFactory.getPhone(phoneId);
2753 if (phone != null) {
2754 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002755 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002756 return retVal;
2757 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002758 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002759 return false;
2760 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002761 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002762
2763 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002764 public int getCarrierPrivilegeStatus(int subId) {
2765 final Phone phone = getPhone(subId);
2766 if (phone == null) {
2767 loge("getCarrierPrivilegeStatus: Invalid subId");
2768 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2769 }
2770 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002771 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002772 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002773 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2774 }
2775 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002776 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002777 }
Junda Liu29340342014-07-10 15:23:27 -07002778
2779 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002780 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002781 if (TextUtils.isEmpty(pkgName))
2782 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002783 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002784 if (card == null) {
2785 loge("checkCarrierPrivilegesForPackage: No UICC");
2786 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2787 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002788 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2789 }
2790
2791 @Override
2792 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002793 if (TextUtils.isEmpty(pkgName))
2794 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002795 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2796 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2797 UiccCard card = UiccController.getInstance().getUiccCard(i);
2798 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002799 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002800 continue;
2801 }
2802
2803 result = card.getCarrierPrivilegeStatus(
2804 mPhone.getContext().getPackageManager(), pkgName);
2805 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2806 break;
2807 }
2808 }
2809
2810 return result;
Junda Liu29340342014-07-10 15:23:27 -07002811 }
Derek Tan89e89d42014-07-08 17:00:10 -07002812
2813 @Override
Junda Liue64de782015-04-16 17:19:16 -07002814 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2815 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2816 loge("phoneId " + phoneId + " is not valid.");
2817 return null;
2818 }
2819 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002820 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002821 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002822 return null ;
2823 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002824 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002825 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002826 }
2827
Amith Yamasani6e118872016-02-19 12:53:51 -08002828 @Override
2829 public List<String> getPackagesWithCarrierPrivileges() {
2830 PackageManager pm = mPhone.getContext().getPackageManager();
2831 List<String> privilegedPackages = new ArrayList<>();
2832 List<PackageInfo> packages = null;
2833 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2834 UiccCard card = UiccController.getInstance().getUiccCard(i);
2835 if (card == null) {
2836 // No UICC in that slot.
2837 continue;
2838 }
2839 if (card.hasCarrierPrivilegeRules()) {
2840 if (packages == null) {
2841 // Only check packages in user 0 for now
2842 packages = pm.getInstalledPackagesAsUser(
2843 PackageManager.MATCH_DISABLED_COMPONENTS
2844 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2845 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2846 }
2847 for (int p = packages.size() - 1; p >= 0; p--) {
2848 PackageInfo pkgInfo = packages.get(p);
2849 if (pkgInfo != null && pkgInfo.packageName != null
2850 && card.getCarrierPrivilegeStatus(pkgInfo)
2851 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2852 privilegedPackages.add(pkgInfo.packageName);
2853 }
2854 }
2855 }
2856 }
2857 return privilegedPackages;
2858 }
2859
Wink Savilleb564aae2014-10-23 10:18:09 -07002860 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002861 final Phone phone = getPhone(subId);
2862 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002863 if (card == null) {
2864 loge("getIccId: No UICC");
2865 return null;
2866 }
2867 String iccId = card.getIccId();
2868 if (TextUtils.isEmpty(iccId)) {
2869 loge("getIccId: ICC ID is null or empty.");
2870 return null;
2871 }
2872 return iccId;
2873 }
2874
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002875 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002876 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2877 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002878 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002879
Jeff Sharkey85190e62014-12-05 09:40:12 -08002880 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002881 final Phone phone = getPhone(subId);
2882 if (phone == null) {
2883 return false;
2884 }
2885 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002886
2887 if (DBG_MERGE) {
2888 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2889 + subscriberId + " to " + number);
2890 }
2891
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002892 if (TextUtils.isEmpty(iccId)) {
2893 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002894 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002895
Jeff Sharkey85190e62014-12-05 09:40:12 -08002896 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2897
2898 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002899 if (alphaTag == null) {
2900 editor.remove(alphaTagPrefKey);
2901 } else {
2902 editor.putString(alphaTagPrefKey, alphaTag);
2903 }
2904
Jeff Sharkey85190e62014-12-05 09:40:12 -08002905 // Record both the line number and IMSI for this ICCID, since we need to
2906 // track all merged IMSIs based on line number
2907 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2908 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002909 if (number == null) {
2910 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002911 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002912 } else {
2913 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002914 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002915 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002916
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002917 editor.commit();
2918 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002919 }
2920
2921 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002922 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002923 // This is open to apps with WRITE_SMS.
2924 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002925 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002926 return null;
2927 }
Derek Tan97ebb422014-09-05 16:55:38 -07002928
2929 String iccId = getIccId(subId);
2930 if (iccId != null) {
2931 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002932 if (DBG_MERGE) {
2933 log("getLine1NumberForDisplay returning " +
2934 mTelephonySharedPreferences.getString(numberPrefKey, null));
2935 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002936 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002937 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002938 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002939 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002940 }
2941
2942 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002943 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2944 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2945 return null;
2946 }
Derek Tan97ebb422014-09-05 16:55:38 -07002947
2948 String iccId = getIccId(subId);
2949 if (iccId != null) {
2950 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002951 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002952 }
Derek Tan97ebb422014-09-05 16:55:38 -07002953 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002954 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002955
2956 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002957 public String[] getMergedSubscriberIds(String callingPackage) {
2958 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2959 return null;
2960 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002961 final Context context = mPhone.getContext();
2962 final TelephonyManager tele = TelephonyManager.from(context);
2963 final SubscriptionManager sub = SubscriptionManager.from(context);
2964
2965 // Figure out what subscribers are currently active
2966 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002967 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2968 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2969 final long identity = Binder.clearCallingIdentity();
2970 try {
2971 final int[] subIds = sub.getActiveSubscriptionIdList();
2972 for (int subId : subIds) {
2973 activeSubscriberIds.add(tele.getSubscriberId(subId));
2974 }
2975 } finally {
2976 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002977 }
2978
2979 // First pass, find a number override for an active subscriber
2980 String mergeNumber = null;
2981 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2982 for (String key : prefs.keySet()) {
2983 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2984 final String subscriberId = (String) prefs.get(key);
2985 if (activeSubscriberIds.contains(subscriberId)) {
2986 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2987 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2988 mergeNumber = (String) prefs.get(numberKey);
2989 if (DBG_MERGE) {
2990 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2991 + " for active subscriber " + subscriberId);
2992 }
2993 if (!TextUtils.isEmpty(mergeNumber)) {
2994 break;
2995 }
2996 }
2997 }
2998 }
2999
3000 // Shortcut when no active merged subscribers
3001 if (TextUtils.isEmpty(mergeNumber)) {
3002 return null;
3003 }
3004
3005 // Second pass, find all subscribers under that line override
3006 final ArraySet<String> result = new ArraySet<>();
3007 for (String key : prefs.keySet()) {
3008 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3009 final String number = (String) prefs.get(key);
3010 if (mergeNumber.equals(number)) {
3011 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3012 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3013 final String subscriberId = (String) prefs.get(subscriberKey);
3014 if (!TextUtils.isEmpty(subscriberId)) {
3015 result.add(subscriberId);
3016 }
3017 }
3018 }
3019 }
3020
3021 final String[] resultArray = result.toArray(new String[result.size()]);
3022 Arrays.sort(resultArray);
3023 if (DBG_MERGE) {
3024 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3025 }
3026 return resultArray;
3027 }
3028
3029 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003030 public boolean setOperatorBrandOverride(int subId, String brand) {
3031 enforceCarrierPrivilege(subId);
3032 final Phone phone = getPhone(subId);
3033 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003034 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003035
3036 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003037 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003038 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3039 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003040 enforceCarrierPrivilege(subId);
3041 final Phone phone = getPhone(subId);
3042 if (phone == null) {
3043 return false;
3044 }
3045 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003046 cdmaNonRoamingList);
3047 }
3048
3049 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003050 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003051 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3052 enforceModifyPermission();
3053
3054 int returnValue = 0;
3055 try {
3056 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3057 if(result.exception == null) {
3058 if (result.result != null) {
3059 byte[] responseData = (byte[])(result.result);
3060 if(responseData.length > oemResp.length) {
3061 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3062 responseData.length + "bytes. Buffer Size is " +
3063 oemResp.length + "bytes.");
3064 }
3065 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3066 returnValue = responseData.length;
3067 }
3068 } else {
3069 CommandException ex = (CommandException) result.exception;
3070 returnValue = ex.getCommandError().ordinal();
3071 if(returnValue > 0) returnValue *= -1;
3072 }
3073 } catch (RuntimeException e) {
3074 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3075 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3076 if(returnValue > 0) returnValue *= -1;
3077 }
3078
3079 return returnValue;
3080 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003081
3082 @Override
3083 public void setRadioCapability(RadioAccessFamily[] rafs) {
3084 try {
3085 ProxyController.getInstance().setRadioCapability(rafs);
3086 } catch (RuntimeException e) {
3087 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3088 }
3089 }
3090
3091 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003092 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3093 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3094 return RadioAccessFamily.RAF_UNKNOWN;
3095 }
3096
Wink Saville5d475dd2014-10-17 15:00:58 -07003097 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3098 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003099
3100 @Override
3101 public void enableVideoCalling(boolean enable) {
3102 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003103 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003104 }
3105
3106 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003107 public boolean isVideoCallingEnabled(String callingPackage) {
3108 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3109 return false;
3110 }
3111
Andrew Lee77527ac2014-10-21 16:57:39 -07003112 // Check the user preference and the system-level IMS setting. Even if the user has
3113 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3114 // In the long run, we may instead need to check if there exists a connection service
3115 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003116 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3117 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003118 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003119 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003120
Andrew Leea1239f22015-03-02 17:44:07 -08003121 @Override
3122 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003123 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003124 }
3125
3126 @Override
3127 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003128 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003129 }
3130
Andrew Lee9431b832015-03-09 18:46:45 -07003131 @Override
3132 public boolean isTtyModeSupported() {
3133 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3134 TelephonyManager telephonyManager =
3135 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003136 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003137 }
3138
3139 @Override
3140 public boolean isHearingAidCompatibilitySupported() {
3141 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3142 }
3143
Sanket Padawe7310cc72015-01-14 09:53:20 -08003144 /**
3145 * Returns the unique device ID of phone, for example, the IMEI for
3146 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3147 *
3148 * <p>Requires Permission:
3149 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3150 */
3151 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003152 public String getDeviceId(String callingPackage) {
3153 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3154 return null;
3155 }
3156
Sanket Padawe7310cc72015-01-14 09:53:20 -08003157 final Phone phone = PhoneFactory.getPhone(0);
3158 if (phone != null) {
3159 return phone.getDeviceId();
3160 } else {
3161 return null;
3162 }
3163 }
3164
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003165 /*
3166 * {@hide}
3167 * Returns the IMS Registration Status
3168 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003169 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003170 public boolean isImsRegistered() {
3171 return mPhone.isImsRegistered();
3172 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003173
3174 @Override
3175 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3176 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3177 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003178
Nathan Haroldc55097a2015-03-11 18:14:50 -07003179 /*
3180 * {@hide}
3181 * Returns the IMS Registration Status
3182 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003183 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003184 return mPhone.isWifiCallingEnabled();
3185 }
3186
3187 /*
3188 * {@hide}
3189 * Returns the IMS Registration Status
3190 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003191 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003192 return mPhone.isVolteEnabled();
3193 }
Svet Ganovb320e182015-04-16 12:30:10 -07003194
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003195 /*
3196 * {@hide} Returns the IMS Registration Status
3197 */
3198 public boolean isVideoTelephonyAvailable() {
3199 return mPhone.isVideoEnabled();
3200 }
3201
Svet Ganovb320e182015-04-16 12:30:10 -07003202 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003203 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003204 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003205 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3206
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003207 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003208 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003209 } catch (SecurityException e) {
3210 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3211 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003212 }
Svet Ganovb320e182015-04-16 12:30:10 -07003213
3214 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3215 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3216 return false;
3217 }
3218
3219 return true;
3220 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003221
Makoto Onukifee69342015-06-29 14:44:50 -07003222 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003223 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003224 */
3225 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003226 // Default SMS app can always read it.
3227 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3228 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3229 return true;
3230 }
3231 try {
3232 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003233 } catch (SecurityException readPhoneStateSecurityException) {
3234 try {
3235 // Can be read with READ_SMS too.
3236 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3237 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3238 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3239 } catch (SecurityException readSmsSecurityException) {
3240 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3241 // permissions
3242 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3243 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3244 " or " + android.Manifest.permission.READ_SMS + ".");
3245 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003246 }
Makoto Onukifee69342015-06-29 14:44:50 -07003247 }
3248
Stuart Scott8eef64f2015-04-08 15:13:54 -07003249 @Override
3250 public void factoryReset(int subId) {
3251 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003252 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3253 return;
3254 }
3255
Svet Ganovcc087f82015-05-12 20:35:54 -07003256 final long identity = Binder.clearCallingIdentity();
3257 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003258 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3259 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003260 // Enable data
3261 setDataEnabled(subId, true);
3262 // Set network selection mode to automatic
3263 setNetworkSelectionModeAutomatic(subId);
3264 // Set preferred mobile network type to the best available
3265 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3266 // Turn off roaming
3267 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3268 }
3269 } finally {
3270 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003271 }
3272 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003273
3274 @Override
3275 public String getLocaleFromDefaultSim() {
3276 // We query all subscriptions instead of just the active ones, because
3277 // this might be called early on in the provisioning flow when the
3278 // subscriptions potentially aren't active yet.
3279 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3280 if (slist == null || slist.isEmpty()) {
3281 return null;
3282 }
3283
3284 // This function may be called very early, say, from the setup wizard, at
3285 // which point we won't have a default subscription set. If that's the case
3286 // we just choose the first, which will be valid in "most cases".
3287 final int defaultSubId = getDefaultSubscription();
3288 SubscriptionInfo info = null;
3289 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3290 info = slist.get(0);
3291 } else {
3292 for (SubscriptionInfo item : slist) {
3293 if (item.getSubscriptionId() == defaultSubId) {
3294 info = item;
3295 break;
3296 }
3297 }
3298
3299 if (info == null) {
3300 return null;
3301 }
3302 }
3303
3304 // Try and fetch the locale from the carrier properties or from the SIM language
3305 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003306 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003307 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003308 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003309 if (defaultPhone != null) {
3310 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3311 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003312 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003313 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3314 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003315 } else {
3316 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003317 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003318 }
3319 }
3320
Narayan Kamath011676f2015-07-29 12:04:08 +01003321 // The SIM language preferences only store a language (e.g. fr = French), not an
3322 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3323 // the SIM and carrier preferences does not include a country we add the country
3324 // determined from the SIM MCC to provide an exact locale.
3325 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003326 if (mccLocale != null) {
3327 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3328 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003329 }
3330
Tony Hill183b2de2015-06-24 14:53:58 +01003331 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003332 return null;
3333 }
3334
3335 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3336 final long identity = Binder.clearCallingIdentity();
3337 try {
3338 return mSubscriptionController.getAllSubInfoList(
3339 mPhone.getContext().getOpPackageName());
3340 } finally {
3341 Binder.restoreCallingIdentity(identity);
3342 }
3343 }
3344
3345 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3346 final long identity = Binder.clearCallingIdentity();
3347 try {
3348 return mSubscriptionController.getActiveSubscriptionInfoList(
3349 mPhone.getContext().getOpPackageName());
3350 } finally {
3351 Binder.restoreCallingIdentity(identity);
3352 }
3353 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003354
3355 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003356 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3357 * representing the state of the modem.
3358 *
3359 * NOTE: This clears the modem state, so there should only every be one caller.
3360 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003361 */
3362 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003363 public void requestModemActivityInfo(ResultReceiver result) {
3364 enforceModifyPermission();
3365
3366 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3367 Bundle bundle = new Bundle();
3368 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3369 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003370 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003371
3372 /**
3373 * {@hide}
3374 * Returns the service state information on specified subscription.
3375 */
3376 @Override
3377 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3378
3379 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3380 return null;
3381 }
3382
3383 final Phone phone = getPhone(subId);
3384 if (phone == null) {
3385 return null;
3386 }
3387
3388 return phone.getServiceState();
3389 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003390
3391 /**
3392 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3393 *
3394 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3395 * voicemail ringtone.
3396 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3397 * PhoneAccount.
3398 */
3399 @Override
3400 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3401 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3402 if (phone == null) {
3403 return null;
3404 }
3405
3406 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3407 }
3408
3409 /**
3410 * Returns whether vibration is set for voicemail notification in Phone settings.
3411 *
3412 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3413 * voicemail vibration setting.
3414 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3415 */
3416 @Override
3417 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3418 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3419 if (phone == null) {
3420 return false;
3421 }
3422
3423 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3424 }
3425
Youhan Wange64578a2016-05-02 15:32:42 -07003426 /**
3427 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3428 *
3429 * @throws SecurityException if the caller does not have the required permission
3430 */
3431 private void enforceReadPrivilegedPermission() {
3432 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3433 null);
3434 }
3435
3436 /**
Ta-wei Yen1b455992016-12-27 14:52:32 -08003437 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3438 * permission.
3439 *
3440 * @throws SecurityException if the caller does not have the required permission
3441 */
3442 private void enforceSendSmsPermission() {
3443 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3444 }
3445
3446 /**
Ta-wei Yen463efd12017-01-06 15:29:25 -08003447 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003448 *
Ta-wei Yen463efd12017-01-06 15:29:25 -08003449 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003450 */
Ta-wei Yen463efd12017-01-06 15:29:25 -08003451 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
3452 String vvmPackage = RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId)
3453 .getPackageName();
3454 if (!callingPackage.equals(vvmPackage)) {
3455 throw new SecurityException("Caller not current active visual voicemail package[" +
3456 vvmPackage + "]");
Ta-wei Yen1b455992016-12-27 14:52:32 -08003457 }
3458 }
3459
3460 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003461 * Return the application ID for the app type.
3462 *
3463 * @param subId the subscription ID that this request applies to.
3464 * @param appType the uicc app type.
3465 * @return Application ID for specificied app type, or null if no uicc.
3466 */
3467 @Override
3468 public String getAidForAppType(int subId, int appType) {
3469 enforceReadPrivilegedPermission();
3470 Phone phone = getPhone(subId);
3471 if (phone == null) {
3472 return null;
3473 }
3474 String aid = null;
3475 try {
3476 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3477 .getApplicationByType(appType).getAid();
3478 } catch (Exception e) {
3479 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3480 }
3481 return aid;
3482 }
3483
Youhan Wang4001d252016-05-11 10:29:41 -07003484 /**
3485 * Return the Electronic Serial Number.
3486 *
3487 * @param subId the subscription ID that this request applies to.
3488 * @return ESN or null if error.
3489 */
3490 @Override
3491 public String getEsn(int subId) {
3492 enforceReadPrivilegedPermission();
3493 Phone phone = getPhone(subId);
3494 if (phone == null) {
3495 return null;
3496 }
3497 String esn = null;
3498 try {
3499 esn = phone.getEsn();
3500 } catch (Exception e) {
3501 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3502 }
3503 return esn;
3504 }
3505
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003506 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003507 * Return the Preferred Roaming List Version.
3508 *
3509 * @param subId the subscription ID that this request applies to.
3510 * @return PRLVersion or null if error.
3511 */
3512 @Override
3513 public String getCdmaPrlVersion(int subId) {
3514 enforceReadPrivilegedPermission();
3515 Phone phone = getPhone(subId);
3516 if (phone == null) {
3517 return null;
3518 }
3519 String cdmaPrlVersion = null;
3520 try {
3521 cdmaPrlVersion = phone.getCdmaPrlVersion();
3522 } catch (Exception e) {
3523 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3524 }
3525 return cdmaPrlVersion;
3526 }
3527
3528 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003529 * Get snapshot of Telephony histograms
3530 * @return List of Telephony histograms
3531 * @hide
3532 */
3533 @Override
3534 public List<TelephonyHistogram> getTelephonyHistograms() {
3535 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3536 return RIL.getTelephonyRILTimingHistograms();
3537 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003538
3539 /**
3540 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003541 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003542 * Require system privileges. In the future we may add this to carrier APIs.
3543 *
3544 * @return The number of carriers set successfully, should match length of carriers
3545 */
3546 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003547 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003548 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003549 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003550 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3551 return retVal[0];
3552 }
3553
3554 /**
3555 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003556 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003557 * Require system privileges. In the future we may add this to carrier APIs.
3558 *
3559 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3560 * means all carriers are allowed.
3561 */
3562 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003563 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003564 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003565 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003566 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3567 }
3568
fionaxu59545b42016-05-25 15:53:37 -07003569 /**
3570 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3571 * @param subId the subscription ID that this action applies to.
3572 * @param enabled control enable or disable metered apns.
3573 * {@hide}
3574 */
3575 @Override
3576 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3577 enforceModifyPermission();
3578 final Phone phone = getPhone(subId);
3579 if (phone == null) {
3580 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3581 return;
3582 }
3583 try {
3584 phone.carrierActionSetMeteredApnsEnabled(enabled);
3585 } catch (Exception e) {
3586 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3587 }
3588 }
3589
3590 /**
3591 * Action set from carrier signalling broadcast receivers to enable/disable radio
3592 * @param subId the subscription ID that this action applies to.
3593 * @param enabled control enable or disable radio.
3594 * {@hide}
3595 */
3596 @Override
3597 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3598 enforceModifyPermission();
3599 final Phone phone = getPhone(subId);
3600 if (phone == null) {
3601 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3602 return;
3603 }
3604 try {
3605 phone.carrierActionSetRadioEnabled(enabled);
3606 } catch (Exception e) {
3607 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3608 }
3609 }
3610
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003611 /**
3612 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3613 * bug report is being generated.
3614 */
3615 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003616 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003617 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3618 != PackageManager.PERMISSION_GRANTED) {
3619 writer.println("Permission Denial: can't dump Phone from pid="
3620 + Binder.getCallingPid()
3621 + ", uid=" + Binder.getCallingUid()
3622 + "without permission "
3623 + android.Manifest.permission.DUMP);
3624 return;
3625 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003626 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003627 }
Jack Yueb89b242016-06-22 13:27:47 -07003628
3629 /**
3630 * Get aggregated video call data usage from all subscriptions since boot.
3631 * @return total data usage in bytes
3632 * {@hide}
3633 */
3634 @Override
3635 public long getVtDataUsage() {
3636 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3637 null);
3638
3639 // NetworkStatsService keeps tracking the active network interface and identity. It will
3640 // record the delta with the corresponding network identity. What we need to do here is
3641 // returning total video call data usage from all subscriptions since boot.
3642
3643 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3644 // simultaneous VT calls.
3645 final Phone[] phones = PhoneFactory.getPhones();
3646 long total = 0;
3647 for (Phone phone : phones) {
3648 total += phone.getVtDataUsage();
3649 }
3650 return total;
3651 }
Jack Yu75ab2952016-07-08 14:29:33 -07003652
3653 /**
3654 * Policy control of data connection. Usually used when data limit is passed.
3655 * @param enabled True if enabling the data, otherwise disabling.
3656 * @param subId Subscription index
3657 * {@hide}
3658 */
3659 @Override
3660 public void setPolicyDataEnabled(boolean enabled, int subId) {
3661 enforceModifyPermission();
3662 Phone phone = getPhone(subId);
3663 if (phone != null) {
3664 phone.setPolicyDataEnabled(enabled);
3665 }
3666 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003667
3668 /**
3669 * Get Client request stats
3670 * @return List of Client Request Stats
3671 * @hide
3672 */
3673 @Override
3674 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3675 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3676 return null;
3677 }
3678
3679 Phone phone = getPhone(subId);
3680 if (phone != null) {
3681 return phone.getClientRequestStats();
3682 }
3683
3684 return null;
3685 }
3686
3687 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3688 if (workSource != null) {
3689 return workSource;
3690 }
3691
3692 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3693 workSource = new WorkSource(uid, packageName);
3694 return workSource;
3695 }
Jack Yu346de222017-02-16 15:32:43 -08003696
3697 /**
3698 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3699 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003700 * @param slotIndex SIM slot id.
Jack Yu346de222017-02-16 15:32:43 -08003701 * @param powerUp True if powering up the SIM, otherwise powering down
3702 *
3703 **/
3704 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003705 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yu346de222017-02-16 15:32:43 -08003706 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003707 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003708 if (subId == null || subId.length == 0) {
3709 return;
3710 }
3711
3712 final Phone phone = getPhone(subId[0]);
3713 if (phone != null) {
3714 phone.setSimPowerState(powerUp);
3715 }
3716 }
shuoqfef36a32017-01-10 13:02:18 -08003717
3718 /**
3719 * Check if phone is in emergency callback mode
3720 * @return true if phone is in emergency callback mode
3721 * @param subId sub id
3722 */
3723 public boolean getEmergencyCallbackMode(int subId) {
3724 final Phone phone = getPhone(subId);
3725 if (phone != null) {
3726 return phone.isInEcm();
3727 } else {
3728 return false;
3729 }
3730 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003731}