blob: f59e97f8e8fb50e5ceb52f1809e7847e9b03b376 [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 Yen12551102017-03-06 16:00:44 -08002039 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yen243b6372017-01-10 16:17:08 -08002040 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2041 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2042 return null;
2043 }
Ta-wei Yen243b6372017-01-10 16:17:08 -08002044 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2045 }
2046
2047 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002048 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2049 VisualVoicemailSmsFilterSettings settings) {
2050 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002051 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002052 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2053 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002054 }
2055
2056 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002057 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2058 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002059 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002060 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002061 }
2062
2063 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002064 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2065 String callingPackage, int subId) {
2066 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002067 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002068 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002069 }
2070
2071 @Override
Ta-wei Yen1b455992016-12-27 14:52:32 -08002072 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002073 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002074 return VisualVoicemailSmsFilterConfig
Ta-wei Yen1b455992016-12-27 14:52:32 -08002075 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2076 }
2077
2078 @Override
2079 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2080 String number, int port, String text, PendingIntent sentIntent) {
2081 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen463efd12017-01-06 15:29:25 -08002082 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen1b455992016-12-27 14:52:32 -08002083 enforceSendSmsPermission();
2084 // Make the calls as the phone process.
2085 final long identity = Binder.clearCallingIdentity();
2086 try {
2087 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2088 if (port == 0) {
2089 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2090 sentIntent, null, false);
2091 } else {
2092 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2093 smsManager.sendDataMessageWithSelfPermissions(number, null,
2094 (short) port, data, sentIntent, null);
2095 }
2096 } finally {
2097 Binder.restoreCallingIdentity(identity);
2098 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002099 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002100 /**
fionaxuef039d42016-11-14 13:36:14 -08002101 * Sets the voice activation state of a given subId.
2102 */
2103 @Override
2104 public void setVoiceActivationState(int subId, int activationState) {
2105 enforceModifyPermissionOrCarrierPrivilege(subId);
2106 final Phone phone = getPhone(subId);
2107 if (phone != null) {
2108 phone.setVoiceActivationState(activationState);
2109 } else {
2110 loge("setVoiceActivationState fails with invalid subId: " + subId);
2111 }
2112 }
2113
2114 /**
2115 * Sets the data activation state of a given subId.
2116 */
2117 @Override
2118 public void setDataActivationState(int subId, int activationState) {
2119 enforceModifyPermissionOrCarrierPrivilege(subId);
2120 final Phone phone = getPhone(subId);
2121 if (phone != null) {
2122 phone.setDataActivationState(activationState);
2123 } else {
2124 loge("setVoiceActivationState fails with invalid subId: " + subId);
2125 }
2126 }
2127
2128 /**
2129 * Returns the voice activation state of a given subId.
2130 */
2131 @Override
2132 public int getVoiceActivationState(int subId, String callingPackage) {
2133 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2134 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2135 }
2136 final Phone phone = getPhone(subId);
2137 if (phone != null) {
2138 return phone.getVoiceActivationState();
2139 } else {
2140 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2141 }
2142 }
2143
2144 /**
2145 * Returns the data activation state of a given subId.
2146 */
2147 @Override
2148 public int getDataActivationState(int subId, String callingPackage) {
2149 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2150 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2151 }
2152 final Phone phone = getPhone(subId);
2153 if (phone != null) {
2154 return phone.getDataActivationState();
2155 } else {
2156 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2157 }
2158 }
2159
2160 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002161 * Returns the unread count of voicemails
2162 */
2163 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002164 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002165 }
2166
2167 /**
2168 * Returns the unread count of voicemails for a subId
2169 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002170 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002171 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002172 final Phone phone = getPhone(subId);
2173 if (phone != null) {
2174 return phone.getVoiceMessageCount();
2175 } else {
2176 return 0;
2177 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002178 }
2179
2180 /**
pkanwar8efe6572017-01-19 13:43:16 -08002181 * returns true, if the device is in a state where both voice and data
2182 * are supported simultaneously. This can change based on location or network condition.
2183 */
2184 @Override
2185 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2186 final Phone phone = getPhone(subId);
2187 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2188 }
2189
2190 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002191 * Returns the data network type.
2192 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002193 *
2194 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2195 */
2196 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002197 public int getNetworkType() {
2198 final Phone phone = getPhone(getDefaultSubscription());
2199 if (phone != null) {
2200 return phone.getServiceState().getDataNetworkType();
2201 } else {
2202 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2203 }
Wink Saville36469e72014-06-11 15:17:00 -07002204 }
2205
2206 /**
2207 * Returns the network type for a subId
2208 */
2209 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002210 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2211 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2212 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2213 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002214
Sanket Padawe356d7632015-06-22 14:03:32 -07002215 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002216 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002217 return phone.getServiceState().getDataNetworkType();
2218 } else {
2219 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2220 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002221 }
2222
2223 /**
2224 * Returns the data network type
2225 */
2226 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002227 public int getDataNetworkType(String callingPackage) {
2228 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002229 }
2230
2231 /**
2232 * Returns the data network type for a subId
2233 */
2234 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002235 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2236 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2237 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2238 }
2239
Sanket Padawe356d7632015-06-22 14:03:32 -07002240 final Phone phone = getPhone(subId);
2241 if (phone != null) {
2242 return phone.getServiceState().getDataNetworkType();
2243 } else {
2244 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2245 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002246 }
2247
2248 /**
Wink Saville36469e72014-06-11 15:17:00 -07002249 * Returns the Voice network type for a subId
2250 */
2251 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002252 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2253 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2254 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2255 }
2256
Sanket Padawe356d7632015-06-22 14:03:32 -07002257 final Phone phone = getPhone(subId);
2258 if (phone != null) {
2259 return phone.getServiceState().getVoiceNetworkType();
2260 } else {
2261 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2262 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002263 }
2264
2265 /**
2266 * @return true if a ICC card is present
2267 */
2268 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002269 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002270 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2271 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002272 }
2273
2274 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002275 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002276 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002277 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002278 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2279 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002280 final Phone phone = getPhone(subId[0]);
2281 if (subId != null && phone != null) {
2282 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002283 } else {
2284 return false;
2285 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002286 }
2287
2288 /**
2289 * Return if the current radio is LTE on CDMA. This
2290 * is a tri-state return value as for a period of time
2291 * the mode may be unknown.
2292 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002293 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002294 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002295 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002296 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002297 @Override
2298 public int getLteOnCdmaMode(String callingPackage) {
2299 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002300 }
2301
Sanket Padawe356d7632015-06-22 14:03:32 -07002302 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002303 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2304 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2305 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2306 }
2307
Sanket Padawe356d7632015-06-22 14:03:32 -07002308 final Phone phone = getPhone(subId);
2309 if (phone == null) {
2310 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2311 } else {
2312 return phone.getLteOnCdmaMode();
2313 }
Wink Saville36469e72014-06-11 15:17:00 -07002314 }
2315
2316 public void setPhone(Phone phone) {
2317 mPhone = phone;
2318 }
2319
2320 /**
2321 * {@hide}
2322 * Returns Default subId, 0 in the case of single standby.
2323 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002324 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002325 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002326 }
2327
Shishir Agrawala9f32182016-04-12 12:00:16 -07002328 private int getSlotForDefaultSubscription() {
2329 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2330 }
2331
Wink Savilleb564aae2014-10-23 10:18:09 -07002332 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002333 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002334 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002335
2336 /**
2337 * @see android.telephony.TelephonyManager.WifiCallingChoices
2338 */
2339 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002340 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2341 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002342 }
2343
2344 /**
2345 * @see android.telephony.TelephonyManager.WifiCallingChoices
2346 */
2347 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002348 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2349 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2350 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002351 }
2352
Sailesh Nepald1e68152013-12-12 19:08:02 -08002353 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002354 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002355 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002356 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002357
Shishir Agrawal566b7612013-10-28 14:41:00 -07002358 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002359 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002360 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002361
Ajay Nambiabd73502015-12-03 13:50:00 -08002362 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002363 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002364 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002365 if (DBG) log("iccOpenLogicalChannel: " + response);
2366 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002367 }
2368
2369 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002370 public boolean iccCloseLogicalChannel(int subId, int channel) {
2371 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002372
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002373 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002374 if (channel < 0) {
2375 return false;
2376 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002377 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002378 if (DBG) log("iccCloseLogicalChannel: " + success);
2379 return success;
2380 }
2381
2382 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002383 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002384 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002385 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002386
2387 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002388 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2389 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002390 " data=" + data);
2391 }
2392
2393 if (channel < 0) {
2394 return "";
2395 }
2396
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002397 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002398 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002399 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2400
Shishir Agrawal566b7612013-10-28 14:41:00 -07002401 // Append the returned status code to the end of the response payload.
2402 String s = Integer.toHexString(
2403 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002404 if (response.payload != null) {
2405 s = IccUtils.bytesToHexString(response.payload) + s;
2406 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002407 return s;
2408 }
Jake Hambye994d462014-02-03 13:10:13 -08002409
Evan Charltonc66da362014-05-16 14:06:40 -07002410 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002411 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002412 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002413 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002414
2415 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002416 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2417 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002418 }
2419
2420 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002421 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002422 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2423
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002424 // Append the returned status code to the end of the response payload.
2425 String s = Integer.toHexString(
2426 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002427 if (response.payload != null) {
2428 s = IccUtils.bytesToHexString(response.payload) + s;
2429 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002430 return s;
2431 }
2432
2433 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002434 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002435 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002436 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002437
2438 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002439 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002440 p1 + " " + p2 + " " + p3 + ":" + filePath);
2441 }
2442
2443 IccIoResult response =
2444 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002445 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2446 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002447
2448 if (DBG) {
2449 log("Exchange SIM_IO [R]" + response);
2450 }
2451
2452 byte[] result = null;
2453 int length = 2;
2454 if (response.payload != null) {
2455 length = 2 + response.payload.length;
2456 result = new byte[length];
2457 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2458 } else {
2459 result = new byte[length];
2460 }
2461
2462 result[length - 1] = (byte) response.sw2;
2463 result[length - 2] = (byte) response.sw1;
2464 return result;
2465 }
2466
Nathan Haroldb3014052017-01-25 15:57:32 -08002467 /**
2468 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2469 * on a particular subscription
2470 */
2471 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002472 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2473 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002474 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2475 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2476 return null;
2477 }
2478 Object response = sendRequest(
2479 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2480 if (response instanceof String[]) {
2481 return (String[]) response;
2482 }
2483 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2484 return null;
2485 }
2486
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002487 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002488 public String sendEnvelopeWithStatus(int subId, String content) {
2489 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002490
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002491 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002492 if (response.payload == null) {
2493 return "";
2494 }
2495
2496 // Append the returned status code to the end of the response payload.
2497 String s = Integer.toHexString(
2498 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2499 s = IccUtils.bytesToHexString(response.payload) + s;
2500 return s;
2501 }
2502
Jake Hambye994d462014-02-03 13:10:13 -08002503 /**
2504 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2505 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2506 *
2507 * @param itemID the ID of the item to read
2508 * @return the NV item as a String, or null on error.
2509 */
2510 @Override
2511 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002512 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002513 if (DBG) log("nvReadItem: item " + itemID);
2514 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2515 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2516 return value;
2517 }
2518
2519 /**
2520 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2521 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2522 *
2523 * @param itemID the ID of the item to read
2524 * @param itemValue the value to write, as a String
2525 * @return true on success; false on any failure
2526 */
2527 @Override
2528 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002529 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002530 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2531 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2532 new Pair<Integer, String>(itemID, itemValue));
2533 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2534 return success;
2535 }
2536
2537 /**
2538 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2539 * Used for device configuration by some CDMA operators.
2540 *
2541 * @param preferredRoamingList byte array containing the new PRL
2542 * @return true on success; false on any failure
2543 */
2544 @Override
2545 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002546 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002547 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2548 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2549 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2550 return success;
2551 }
2552
2553 /**
2554 * Perform the specified type of NV config reset.
2555 * Used for device configuration by some CDMA operators.
2556 *
2557 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2558 * @return true on success; false on any failure
2559 */
2560 @Override
2561 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002562 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002563 if (DBG) log("nvResetConfig: type " + resetType);
2564 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2565 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2566 return success;
2567 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002568
2569 /**
Wink Saville36469e72014-06-11 15:17:00 -07002570 * {@hide}
2571 * Returns Default sim, 0 in the case of single standby.
2572 */
2573 public int getDefaultSim() {
2574 //TODO Need to get it from Telephony Devcontroller
2575 return 0;
2576 }
2577
Svet Ganovb320e182015-04-16 12:30:10 -07002578 public String[] getPcscfAddress(String apnType, String callingPackage) {
2579 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2580 return new String[0];
2581 }
2582
2583
ram87fca6f2014-07-18 18:58:44 +05302584 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002585 }
2586
Brad Ebinger76681002017-01-23 13:50:20 -08002587 /**
2588 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2589 * feature or {@link null} if the service is not available. If an ImsServiceController is
2590 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2591 * feature updates.
2592 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002593 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002594 IImsServiceFeatureListener callback) {
2595 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002596 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002597 callback);
2598 }
2599
Wink Saville36469e72014-06-11 15:17:00 -07002600 public void setImsRegistrationState(boolean registered) {
2601 enforceModifyPermission();
2602 mPhone.setImsRegistrationState(registered);
2603 }
2604
2605 /**
Stuart Scott54788802015-03-30 13:18:01 -07002606 * Set the network selection mode to automatic.
2607 *
2608 */
2609 @Override
2610 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002611 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002612 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2613 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2614 }
2615
2616 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002617 * Set the network selection mode to manual with the selected carrier.
2618 */
2619 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002620 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2621 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002622 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002623 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002624 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2625 persistSelection);
2626 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002627 }
2628
2629 /**
2630 * Scans for available networks.
2631 */
2632 @Override
2633 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002634 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002635 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2636 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2637 CMD_PERFORM_NETWORK_SCAN, null, subId);
2638 return result;
2639 }
2640
2641 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002642 * Get the calculated preferred network type.
2643 * Used for debugging incorrect network type.
2644 *
2645 * @return the preferred network type, defined in RILConstants.java.
2646 */
2647 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002648 public int getCalculatedPreferredNetworkType(String callingPackage) {
2649 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2650 return RILConstants.PREFERRED_NETWORK_MODE;
2651 }
2652
Amit Mahajan43330e02014-11-18 11:54:45 -08002653 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002654 }
2655
2656 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002657 * Get the preferred network type.
2658 * Used for device configuration by some CDMA operators.
2659 *
2660 * @return the preferred network type, defined in RILConstants.java.
2661 */
2662 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002663 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002664 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002665 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002666 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002667 int networkType = (result != null ? result[0] : -1);
2668 if (DBG) log("getPreferredNetworkType: " + networkType);
2669 return networkType;
2670 }
2671
2672 /**
2673 * Set the preferred network type.
2674 * Used for device configuration by some CDMA operators.
2675 *
2676 * @param networkType the preferred network type, defined in RILConstants.java.
2677 * @return true on success; false on any failure.
2678 */
2679 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002680 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002681 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002682 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2683 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002684 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002685 if (success) {
2686 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002687 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002688 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002689 return success;
2690 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002691
2692 /**
Junda Liu475951f2014-11-07 16:45:03 -08002693 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2694 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2695 * tethering.
2696 *
2697 * @return 0: Not required. 1: required. 2: Not set.
2698 * @hide
2699 */
2700 @Override
2701 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002702 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002703 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2704 Settings.Global.TETHER_DUN_REQUIRED, 2);
2705 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2706 // config_tether_apndata.
2707 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2708 dunRequired = 1;
2709 }
2710 return dunRequired;
2711 }
2712
2713 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002714 * Set mobile data enabled
2715 * Used by the user through settings etc to turn on/off mobile data
2716 *
2717 * @param enable {@code true} turn turn data on, else {@code false}
2718 */
2719 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002720 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002721 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002722 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002723 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002724 Phone phone = PhoneFactory.getPhone(phoneId);
2725 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002726 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002727 phone.setDataEnabled(enable);
2728 } else {
2729 loge("setDataEnabled: no phone for subId=" + subId);
2730 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002731 }
2732
2733 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002734 * Get whether mobile data is enabled.
2735 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002736 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002737 *
2738 * @return {@code true} if data is enabled else {@code false}
2739 */
2740 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002741 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002742 try {
2743 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2744 null);
2745 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002746 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002747 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002748 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002749 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002750 Phone phone = PhoneFactory.getPhone(phoneId);
2751 if (phone != null) {
2752 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002753 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002754 return retVal;
2755 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002756 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002757 return false;
2758 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002759 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002760
2761 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002762 public int getCarrierPrivilegeStatus(int subId) {
2763 final Phone phone = getPhone(subId);
2764 if (phone == null) {
2765 loge("getCarrierPrivilegeStatus: Invalid subId");
2766 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2767 }
2768 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002769 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002770 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002771 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2772 }
2773 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002774 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002775 }
Junda Liu29340342014-07-10 15:23:27 -07002776
2777 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002778 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002779 if (TextUtils.isEmpty(pkgName))
2780 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002781 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002782 if (card == null) {
2783 loge("checkCarrierPrivilegesForPackage: No UICC");
2784 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2785 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002786 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2787 }
2788
2789 @Override
2790 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002791 if (TextUtils.isEmpty(pkgName))
2792 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002793 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2794 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2795 UiccCard card = UiccController.getInstance().getUiccCard(i);
2796 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002797 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002798 continue;
2799 }
2800
2801 result = card.getCarrierPrivilegeStatus(
2802 mPhone.getContext().getPackageManager(), pkgName);
2803 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2804 break;
2805 }
2806 }
2807
2808 return result;
Junda Liu29340342014-07-10 15:23:27 -07002809 }
Derek Tan89e89d42014-07-08 17:00:10 -07002810
2811 @Override
Junda Liue64de782015-04-16 17:19:16 -07002812 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2813 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2814 loge("phoneId " + phoneId + " is not valid.");
2815 return null;
2816 }
2817 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002818 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002819 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002820 return null ;
2821 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002822 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002823 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002824 }
2825
Amith Yamasani6e118872016-02-19 12:53:51 -08002826 @Override
2827 public List<String> getPackagesWithCarrierPrivileges() {
2828 PackageManager pm = mPhone.getContext().getPackageManager();
2829 List<String> privilegedPackages = new ArrayList<>();
2830 List<PackageInfo> packages = null;
2831 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2832 UiccCard card = UiccController.getInstance().getUiccCard(i);
2833 if (card == null) {
2834 // No UICC in that slot.
2835 continue;
2836 }
2837 if (card.hasCarrierPrivilegeRules()) {
2838 if (packages == null) {
2839 // Only check packages in user 0 for now
2840 packages = pm.getInstalledPackagesAsUser(
2841 PackageManager.MATCH_DISABLED_COMPONENTS
2842 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2843 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2844 }
2845 for (int p = packages.size() - 1; p >= 0; p--) {
2846 PackageInfo pkgInfo = packages.get(p);
2847 if (pkgInfo != null && pkgInfo.packageName != null
2848 && card.getCarrierPrivilegeStatus(pkgInfo)
2849 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2850 privilegedPackages.add(pkgInfo.packageName);
2851 }
2852 }
2853 }
2854 }
2855 return privilegedPackages;
2856 }
2857
Wink Savilleb564aae2014-10-23 10:18:09 -07002858 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002859 final Phone phone = getPhone(subId);
2860 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002861 if (card == null) {
2862 loge("getIccId: No UICC");
2863 return null;
2864 }
2865 String iccId = card.getIccId();
2866 if (TextUtils.isEmpty(iccId)) {
2867 loge("getIccId: ICC ID is null or empty.");
2868 return null;
2869 }
2870 return iccId;
2871 }
2872
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002873 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002874 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2875 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002876 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002877
Jeff Sharkey85190e62014-12-05 09:40:12 -08002878 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002879 final Phone phone = getPhone(subId);
2880 if (phone == null) {
2881 return false;
2882 }
2883 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002884
2885 if (DBG_MERGE) {
2886 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2887 + subscriberId + " to " + number);
2888 }
2889
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002890 if (TextUtils.isEmpty(iccId)) {
2891 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002892 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002893
Jeff Sharkey85190e62014-12-05 09:40:12 -08002894 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2895
2896 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002897 if (alphaTag == null) {
2898 editor.remove(alphaTagPrefKey);
2899 } else {
2900 editor.putString(alphaTagPrefKey, alphaTag);
2901 }
2902
Jeff Sharkey85190e62014-12-05 09:40:12 -08002903 // Record both the line number and IMSI for this ICCID, since we need to
2904 // track all merged IMSIs based on line number
2905 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2906 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002907 if (number == null) {
2908 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002909 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002910 } else {
2911 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002912 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002913 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002914
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002915 editor.commit();
2916 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002917 }
2918
2919 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002920 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002921 // This is open to apps with WRITE_SMS.
2922 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002923 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002924 return null;
2925 }
Derek Tan97ebb422014-09-05 16:55:38 -07002926
2927 String iccId = getIccId(subId);
2928 if (iccId != null) {
2929 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002930 if (DBG_MERGE) {
2931 log("getLine1NumberForDisplay returning " +
2932 mTelephonySharedPreferences.getString(numberPrefKey, null));
2933 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002934 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002935 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002936 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002937 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002938 }
2939
2940 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002941 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2942 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2943 return null;
2944 }
Derek Tan97ebb422014-09-05 16:55:38 -07002945
2946 String iccId = getIccId(subId);
2947 if (iccId != null) {
2948 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002949 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002950 }
Derek Tan97ebb422014-09-05 16:55:38 -07002951 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002952 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002953
2954 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002955 public String[] getMergedSubscriberIds(String callingPackage) {
2956 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2957 return null;
2958 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002959 final Context context = mPhone.getContext();
2960 final TelephonyManager tele = TelephonyManager.from(context);
2961 final SubscriptionManager sub = SubscriptionManager.from(context);
2962
2963 // Figure out what subscribers are currently active
2964 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002965 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2966 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2967 final long identity = Binder.clearCallingIdentity();
2968 try {
2969 final int[] subIds = sub.getActiveSubscriptionIdList();
2970 for (int subId : subIds) {
2971 activeSubscriberIds.add(tele.getSubscriberId(subId));
2972 }
2973 } finally {
2974 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002975 }
2976
2977 // First pass, find a number override for an active subscriber
2978 String mergeNumber = null;
2979 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2980 for (String key : prefs.keySet()) {
2981 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2982 final String subscriberId = (String) prefs.get(key);
2983 if (activeSubscriberIds.contains(subscriberId)) {
2984 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2985 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2986 mergeNumber = (String) prefs.get(numberKey);
2987 if (DBG_MERGE) {
2988 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2989 + " for active subscriber " + subscriberId);
2990 }
2991 if (!TextUtils.isEmpty(mergeNumber)) {
2992 break;
2993 }
2994 }
2995 }
2996 }
2997
2998 // Shortcut when no active merged subscribers
2999 if (TextUtils.isEmpty(mergeNumber)) {
3000 return null;
3001 }
3002
3003 // Second pass, find all subscribers under that line override
3004 final ArraySet<String> result = new ArraySet<>();
3005 for (String key : prefs.keySet()) {
3006 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3007 final String number = (String) prefs.get(key);
3008 if (mergeNumber.equals(number)) {
3009 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3010 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3011 final String subscriberId = (String) prefs.get(subscriberKey);
3012 if (!TextUtils.isEmpty(subscriberId)) {
3013 result.add(subscriberId);
3014 }
3015 }
3016 }
3017 }
3018
3019 final String[] resultArray = result.toArray(new String[result.size()]);
3020 Arrays.sort(resultArray);
3021 if (DBG_MERGE) {
3022 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3023 }
3024 return resultArray;
3025 }
3026
3027 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003028 public boolean setOperatorBrandOverride(int subId, String brand) {
3029 enforceCarrierPrivilege(subId);
3030 final Phone phone = getPhone(subId);
3031 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003032 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003033
3034 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003035 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003036 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3037 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003038 enforceCarrierPrivilege(subId);
3039 final Phone phone = getPhone(subId);
3040 if (phone == null) {
3041 return false;
3042 }
3043 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003044 cdmaNonRoamingList);
3045 }
3046
3047 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003048 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003049 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3050 enforceModifyPermission();
3051
3052 int returnValue = 0;
3053 try {
3054 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3055 if(result.exception == null) {
3056 if (result.result != null) {
3057 byte[] responseData = (byte[])(result.result);
3058 if(responseData.length > oemResp.length) {
3059 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3060 responseData.length + "bytes. Buffer Size is " +
3061 oemResp.length + "bytes.");
3062 }
3063 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3064 returnValue = responseData.length;
3065 }
3066 } else {
3067 CommandException ex = (CommandException) result.exception;
3068 returnValue = ex.getCommandError().ordinal();
3069 if(returnValue > 0) returnValue *= -1;
3070 }
3071 } catch (RuntimeException e) {
3072 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3073 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3074 if(returnValue > 0) returnValue *= -1;
3075 }
3076
3077 return returnValue;
3078 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003079
3080 @Override
3081 public void setRadioCapability(RadioAccessFamily[] rafs) {
3082 try {
3083 ProxyController.getInstance().setRadioCapability(rafs);
3084 } catch (RuntimeException e) {
3085 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3086 }
3087 }
3088
3089 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003090 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3091 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3092 return RadioAccessFamily.RAF_UNKNOWN;
3093 }
3094
Wink Saville5d475dd2014-10-17 15:00:58 -07003095 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3096 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003097
3098 @Override
3099 public void enableVideoCalling(boolean enable) {
3100 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003101 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003102 }
3103
3104 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003105 public boolean isVideoCallingEnabled(String callingPackage) {
3106 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3107 return false;
3108 }
3109
Andrew Lee77527ac2014-10-21 16:57:39 -07003110 // Check the user preference and the system-level IMS setting. Even if the user has
3111 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3112 // In the long run, we may instead need to check if there exists a connection service
3113 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003114 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3115 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003116 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003117 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003118
Andrew Leea1239f22015-03-02 17:44:07 -08003119 @Override
3120 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003121 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003122 }
3123
3124 @Override
3125 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003126 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003127 }
3128
Andrew Lee9431b832015-03-09 18:46:45 -07003129 @Override
3130 public boolean isTtyModeSupported() {
3131 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3132 TelephonyManager telephonyManager =
3133 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003134 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003135 }
3136
3137 @Override
3138 public boolean isHearingAidCompatibilitySupported() {
3139 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3140 }
3141
Sanket Padawe7310cc72015-01-14 09:53:20 -08003142 /**
3143 * Returns the unique device ID of phone, for example, the IMEI for
3144 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3145 *
3146 * <p>Requires Permission:
3147 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3148 */
3149 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003150 public String getDeviceId(String callingPackage) {
3151 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3152 return null;
3153 }
3154
Sanket Padawe7310cc72015-01-14 09:53:20 -08003155 final Phone phone = PhoneFactory.getPhone(0);
3156 if (phone != null) {
3157 return phone.getDeviceId();
3158 } else {
3159 return null;
3160 }
3161 }
3162
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003163 /*
3164 * {@hide}
3165 * Returns the IMS Registration Status
3166 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003167 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003168 public boolean isImsRegistered() {
3169 return mPhone.isImsRegistered();
3170 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003171
3172 @Override
3173 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3174 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3175 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003176
Nathan Haroldc55097a2015-03-11 18:14:50 -07003177 /*
3178 * {@hide}
3179 * Returns the IMS Registration Status
3180 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003181 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003182 return mPhone.isWifiCallingEnabled();
3183 }
3184
3185 /*
3186 * {@hide}
3187 * Returns the IMS Registration Status
3188 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003189 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003190 return mPhone.isVolteEnabled();
3191 }
Svet Ganovb320e182015-04-16 12:30:10 -07003192
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003193 /*
3194 * {@hide} Returns the IMS Registration Status
3195 */
3196 public boolean isVideoTelephonyAvailable() {
3197 return mPhone.isVideoEnabled();
3198 }
3199
Svet Ganovb320e182015-04-16 12:30:10 -07003200 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003201 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003202 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003203 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3204
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003205 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003206 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003207 } catch (SecurityException e) {
3208 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3209 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003210 }
Svet Ganovb320e182015-04-16 12:30:10 -07003211
3212 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3213 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3214 return false;
3215 }
3216
3217 return true;
3218 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003219
Makoto Onukifee69342015-06-29 14:44:50 -07003220 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003221 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003222 */
3223 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003224 // Default SMS app can always read it.
3225 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3226 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3227 return true;
3228 }
3229 try {
3230 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003231 } catch (SecurityException readPhoneStateSecurityException) {
3232 try {
3233 // Can be read with READ_SMS too.
3234 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3235 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3236 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3237 } catch (SecurityException readSmsSecurityException) {
3238 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3239 // permissions
3240 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3241 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3242 " or " + android.Manifest.permission.READ_SMS + ".");
3243 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003244 }
Makoto Onukifee69342015-06-29 14:44:50 -07003245 }
3246
Stuart Scott8eef64f2015-04-08 15:13:54 -07003247 @Override
3248 public void factoryReset(int subId) {
3249 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003250 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3251 return;
3252 }
3253
Svet Ganovcc087f82015-05-12 20:35:54 -07003254 final long identity = Binder.clearCallingIdentity();
3255 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003256 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3257 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003258 // Enable data
3259 setDataEnabled(subId, true);
3260 // Set network selection mode to automatic
3261 setNetworkSelectionModeAutomatic(subId);
3262 // Set preferred mobile network type to the best available
3263 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3264 // Turn off roaming
3265 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3266 }
3267 } finally {
3268 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003269 }
3270 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003271
3272 @Override
3273 public String getLocaleFromDefaultSim() {
3274 // We query all subscriptions instead of just the active ones, because
3275 // this might be called early on in the provisioning flow when the
3276 // subscriptions potentially aren't active yet.
3277 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3278 if (slist == null || slist.isEmpty()) {
3279 return null;
3280 }
3281
3282 // This function may be called very early, say, from the setup wizard, at
3283 // which point we won't have a default subscription set. If that's the case
3284 // we just choose the first, which will be valid in "most cases".
3285 final int defaultSubId = getDefaultSubscription();
3286 SubscriptionInfo info = null;
3287 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3288 info = slist.get(0);
3289 } else {
3290 for (SubscriptionInfo item : slist) {
3291 if (item.getSubscriptionId() == defaultSubId) {
3292 info = item;
3293 break;
3294 }
3295 }
3296
3297 if (info == null) {
3298 return null;
3299 }
3300 }
3301
3302 // Try and fetch the locale from the carrier properties or from the SIM language
3303 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003304 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003305 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003306 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003307 if (defaultPhone != null) {
3308 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3309 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003310 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003311 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3312 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003313 } else {
3314 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003315 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003316 }
3317 }
3318
Narayan Kamath011676f2015-07-29 12:04:08 +01003319 // The SIM language preferences only store a language (e.g. fr = French), not an
3320 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3321 // the SIM and carrier preferences does not include a country we add the country
3322 // determined from the SIM MCC to provide an exact locale.
3323 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003324 if (mccLocale != null) {
3325 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3326 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003327 }
3328
Tony Hill183b2de2015-06-24 14:53:58 +01003329 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003330 return null;
3331 }
3332
3333 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3334 final long identity = Binder.clearCallingIdentity();
3335 try {
3336 return mSubscriptionController.getAllSubInfoList(
3337 mPhone.getContext().getOpPackageName());
3338 } finally {
3339 Binder.restoreCallingIdentity(identity);
3340 }
3341 }
3342
3343 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3344 final long identity = Binder.clearCallingIdentity();
3345 try {
3346 return mSubscriptionController.getActiveSubscriptionInfoList(
3347 mPhone.getContext().getOpPackageName());
3348 } finally {
3349 Binder.restoreCallingIdentity(identity);
3350 }
3351 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003352
3353 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003354 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3355 * representing the state of the modem.
3356 *
3357 * NOTE: This clears the modem state, so there should only every be one caller.
3358 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003359 */
3360 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003361 public void requestModemActivityInfo(ResultReceiver result) {
3362 enforceModifyPermission();
3363
3364 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3365 Bundle bundle = new Bundle();
3366 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3367 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003368 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003369
3370 /**
3371 * {@hide}
3372 * Returns the service state information on specified subscription.
3373 */
3374 @Override
3375 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3376
3377 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3378 return null;
3379 }
3380
3381 final Phone phone = getPhone(subId);
3382 if (phone == null) {
3383 return null;
3384 }
3385
3386 return phone.getServiceState();
3387 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003388
3389 /**
3390 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3391 *
3392 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3393 * voicemail ringtone.
3394 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3395 * PhoneAccount.
3396 */
3397 @Override
3398 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3399 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3400 if (phone == null) {
3401 return null;
3402 }
3403
3404 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3405 }
3406
3407 /**
3408 * Returns whether vibration is set for voicemail notification in Phone settings.
3409 *
3410 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3411 * voicemail vibration setting.
3412 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3413 */
3414 @Override
3415 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3416 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3417 if (phone == null) {
3418 return false;
3419 }
3420
3421 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3422 }
3423
Youhan Wange64578a2016-05-02 15:32:42 -07003424 /**
3425 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3426 *
3427 * @throws SecurityException if the caller does not have the required permission
3428 */
3429 private void enforceReadPrivilegedPermission() {
3430 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3431 null);
3432 }
3433
3434 /**
Ta-wei Yen1b455992016-12-27 14:52:32 -08003435 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3436 * permission.
3437 *
3438 * @throws SecurityException if the caller does not have the required permission
3439 */
3440 private void enforceSendSmsPermission() {
3441 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3442 }
3443
3444 /**
Ta-wei Yen463efd12017-01-06 15:29:25 -08003445 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003446 *
Ta-wei Yen463efd12017-01-06 15:29:25 -08003447 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003448 */
Ta-wei Yen463efd12017-01-06 15:29:25 -08003449 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
3450 String vvmPackage = RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId)
3451 .getPackageName();
3452 if (!callingPackage.equals(vvmPackage)) {
3453 throw new SecurityException("Caller not current active visual voicemail package[" +
3454 vvmPackage + "]");
Ta-wei Yen1b455992016-12-27 14:52:32 -08003455 }
3456 }
3457
3458 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003459 * Return the application ID for the app type.
3460 *
3461 * @param subId the subscription ID that this request applies to.
3462 * @param appType the uicc app type.
3463 * @return Application ID for specificied app type, or null if no uicc.
3464 */
3465 @Override
3466 public String getAidForAppType(int subId, int appType) {
3467 enforceReadPrivilegedPermission();
3468 Phone phone = getPhone(subId);
3469 if (phone == null) {
3470 return null;
3471 }
3472 String aid = null;
3473 try {
3474 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3475 .getApplicationByType(appType).getAid();
3476 } catch (Exception e) {
3477 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3478 }
3479 return aid;
3480 }
3481
Youhan Wang4001d252016-05-11 10:29:41 -07003482 /**
3483 * Return the Electronic Serial Number.
3484 *
3485 * @param subId the subscription ID that this request applies to.
3486 * @return ESN or null if error.
3487 */
3488 @Override
3489 public String getEsn(int subId) {
3490 enforceReadPrivilegedPermission();
3491 Phone phone = getPhone(subId);
3492 if (phone == null) {
3493 return null;
3494 }
3495 String esn = null;
3496 try {
3497 esn = phone.getEsn();
3498 } catch (Exception e) {
3499 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3500 }
3501 return esn;
3502 }
3503
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003504 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003505 * Return the Preferred Roaming List Version.
3506 *
3507 * @param subId the subscription ID that this request applies to.
3508 * @return PRLVersion or null if error.
3509 */
3510 @Override
3511 public String getCdmaPrlVersion(int subId) {
3512 enforceReadPrivilegedPermission();
3513 Phone phone = getPhone(subId);
3514 if (phone == null) {
3515 return null;
3516 }
3517 String cdmaPrlVersion = null;
3518 try {
3519 cdmaPrlVersion = phone.getCdmaPrlVersion();
3520 } catch (Exception e) {
3521 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3522 }
3523 return cdmaPrlVersion;
3524 }
3525
3526 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003527 * Get snapshot of Telephony histograms
3528 * @return List of Telephony histograms
3529 * @hide
3530 */
3531 @Override
3532 public List<TelephonyHistogram> getTelephonyHistograms() {
3533 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3534 return RIL.getTelephonyRILTimingHistograms();
3535 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003536
3537 /**
3538 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003539 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003540 * Require system privileges. In the future we may add this to carrier APIs.
3541 *
3542 * @return The number of carriers set successfully, should match length of carriers
3543 */
3544 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003545 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003546 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003547 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003548 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3549 return retVal[0];
3550 }
3551
3552 /**
3553 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003554 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003555 * Require system privileges. In the future we may add this to carrier APIs.
3556 *
3557 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3558 * means all carriers are allowed.
3559 */
3560 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003561 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003562 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003563 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003564 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3565 }
3566
fionaxu59545b42016-05-25 15:53:37 -07003567 /**
3568 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3569 * @param subId the subscription ID that this action applies to.
3570 * @param enabled control enable or disable metered apns.
3571 * {@hide}
3572 */
3573 @Override
3574 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3575 enforceModifyPermission();
3576 final Phone phone = getPhone(subId);
3577 if (phone == null) {
3578 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3579 return;
3580 }
3581 try {
3582 phone.carrierActionSetMeteredApnsEnabled(enabled);
3583 } catch (Exception e) {
3584 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3585 }
3586 }
3587
3588 /**
3589 * Action set from carrier signalling broadcast receivers to enable/disable radio
3590 * @param subId the subscription ID that this action applies to.
3591 * @param enabled control enable or disable radio.
3592 * {@hide}
3593 */
3594 @Override
3595 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3596 enforceModifyPermission();
3597 final Phone phone = getPhone(subId);
3598 if (phone == null) {
3599 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3600 return;
3601 }
3602 try {
3603 phone.carrierActionSetRadioEnabled(enabled);
3604 } catch (Exception e) {
3605 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3606 }
3607 }
3608
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003609 /**
3610 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3611 * bug report is being generated.
3612 */
3613 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003614 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003615 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3616 != PackageManager.PERMISSION_GRANTED) {
3617 writer.println("Permission Denial: can't dump Phone from pid="
3618 + Binder.getCallingPid()
3619 + ", uid=" + Binder.getCallingUid()
3620 + "without permission "
3621 + android.Manifest.permission.DUMP);
3622 return;
3623 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003624 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003625 }
Jack Yueb89b242016-06-22 13:27:47 -07003626
3627 /**
3628 * Get aggregated video call data usage from all subscriptions since boot.
3629 * @return total data usage in bytes
3630 * {@hide}
3631 */
3632 @Override
3633 public long getVtDataUsage() {
3634 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3635 null);
3636
3637 // NetworkStatsService keeps tracking the active network interface and identity. It will
3638 // record the delta with the corresponding network identity. What we need to do here is
3639 // returning total video call data usage from all subscriptions since boot.
3640
3641 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3642 // simultaneous VT calls.
3643 final Phone[] phones = PhoneFactory.getPhones();
3644 long total = 0;
3645 for (Phone phone : phones) {
3646 total += phone.getVtDataUsage();
3647 }
3648 return total;
3649 }
Jack Yu75ab2952016-07-08 14:29:33 -07003650
3651 /**
3652 * Policy control of data connection. Usually used when data limit is passed.
3653 * @param enabled True if enabling the data, otherwise disabling.
3654 * @param subId Subscription index
3655 * {@hide}
3656 */
3657 @Override
3658 public void setPolicyDataEnabled(boolean enabled, int subId) {
3659 enforceModifyPermission();
3660 Phone phone = getPhone(subId);
3661 if (phone != null) {
3662 phone.setPolicyDataEnabled(enabled);
3663 }
3664 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003665
3666 /**
3667 * Get Client request stats
3668 * @return List of Client Request Stats
3669 * @hide
3670 */
3671 @Override
3672 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3673 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3674 return null;
3675 }
3676
3677 Phone phone = getPhone(subId);
3678 if (phone != null) {
3679 return phone.getClientRequestStats();
3680 }
3681
3682 return null;
3683 }
3684
3685 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3686 if (workSource != null) {
3687 return workSource;
3688 }
3689
3690 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3691 workSource = new WorkSource(uid, packageName);
3692 return workSource;
3693 }
Jack Yu346de222017-02-16 15:32:43 -08003694
3695 /**
3696 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3697 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003698 * @param slotIndex SIM slot id.
Jack Yu346de222017-02-16 15:32:43 -08003699 * @param powerUp True if powering up the SIM, otherwise powering down
3700 *
3701 **/
3702 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003703 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yu346de222017-02-16 15:32:43 -08003704 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003705 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003706 if (subId == null || subId.length == 0) {
3707 return;
3708 }
3709
3710 final Phone phone = getPhone(subId[0]);
3711 if (phone != null) {
3712 phone.setSimPowerState(powerUp);
3713 }
3714 }
shuoqfef36a32017-01-10 13:02:18 -08003715
3716 /**
3717 * Check if phone is in emergency callback mode
3718 * @return true if phone is in emergency callback mode
3719 * @param subId sub id
3720 */
3721 public boolean getEmergencyCallbackMode(int subId) {
3722 final Phone phone = getPhone(subId);
3723 if (phone != null) {
3724 return phone.isInEcm();
3725 } else {
3726 return false;
3727 }
3728 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003729}