blob: d2e635e592ebfeac7fbbe1a96f1dcafb07263b60 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Ta-wei Yen30a69c82016-12-27 14:52:32 -080021import android.Manifest.permission;
Ta-wei Yendca928f2017-01-10 16:17:08 -080022import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.ActivityManager;
24import android.app.AppOpsManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080025import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070026import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.Context;
28import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070029import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080030import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070031import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.net.Uri;
33import android.os.AsyncResult;
34import android.os.Binder;
35import android.os.Bundle;
36import android.os.Handler;
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 Sasindran9a909312016-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 Sasindran9a909312016-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 Yen30a69c82016-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 Yen30a69c82016-12-27 14:52:32 -080070
Andrew Lee312e8172014-10-23 17:01:36 -070071import com.android.ims.ImsManager;
Brad Ebinger51f743a2017-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 Yen527a9c02017-01-06 15:29:25 -080098import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -080099
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700100import java.io.FileDescriptor;
101import java.io.PrintWriter;
Ta-wei Yen30a69c82016-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;
pkanwar32d516d2016-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) {
pkanwar32d516d2016-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 Sasindran9a909312016-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);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700513 if (uiccCard == null) {
514 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800515 request.result = new IccOpenLogicalChannelResponse(-1,
516 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700517 synchronized (request) {
518 request.notifyAll();
519 }
520 } else {
521 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
522 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
523 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700524 break;
525
526 case EVENT_OPEN_CHANNEL_DONE:
527 ar = (AsyncResult) msg.obj;
528 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700529 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700530 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700531 int[] result = (int[]) ar.result;
532 int channelId = result[0];
533 byte[] selectResponse = null;
534 if (result.length > 1) {
535 selectResponse = new byte[result.length - 1];
536 for (int i = 1; i < result.length; ++i) {
537 selectResponse[i - 1] = (byte) result[i];
538 }
539 }
540 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700541 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700542 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700543 if (ar.result == null) {
544 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700545 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700546 if (ar.exception != null) {
547 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
548 }
549
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700550 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700551 if (ar.exception instanceof CommandException) {
552 CommandException.Error error =
553 ((CommandException) (ar.exception)).getCommandError();
554 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700555 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700556 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700557 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700558 }
559 }
560 openChannelResp = new IccOpenLogicalChannelResponse(
561 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700562 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700563 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700564 synchronized (request) {
565 request.notifyAll();
566 }
567 break;
568
569 case CMD_CLOSE_CHANNEL:
570 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800571 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700572 if (uiccCard == null) {
573 loge("iccCloseLogicalChannel: No UICC");
574 request.result = new IccIoResult(0x6F, 0, (byte[])null);
575 synchronized (request) {
576 request.notifyAll();
577 }
578 } else {
579 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
580 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
581 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700582 break;
583
584 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800585 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
586 break;
587
588 case CMD_NV_READ_ITEM:
589 request = (MainThreadRequest) msg.obj;
590 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
591 mPhone.nvReadItem((Integer) request.argument, onCompleted);
592 break;
593
594 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700595 ar = (AsyncResult) msg.obj;
596 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800597 if (ar.exception == null && ar.result != null) {
598 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700599 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800600 request.result = "";
601 if (ar.result == null) {
602 loge("nvReadItem: Empty response");
603 } else if (ar.exception instanceof CommandException) {
604 loge("nvReadItem: CommandException: " +
605 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700606 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800607 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700608 }
609 }
610 synchronized (request) {
611 request.notifyAll();
612 }
613 break;
614
Jake Hambye994d462014-02-03 13:10:13 -0800615 case CMD_NV_WRITE_ITEM:
616 request = (MainThreadRequest) msg.obj;
617 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
618 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
619 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
620 break;
621
622 case EVENT_NV_WRITE_ITEM_DONE:
623 handleNullReturnEvent(msg, "nvWriteItem");
624 break;
625
626 case CMD_NV_WRITE_CDMA_PRL:
627 request = (MainThreadRequest) msg.obj;
628 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
629 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
630 break;
631
632 case EVENT_NV_WRITE_CDMA_PRL_DONE:
633 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
634 break;
635
636 case CMD_NV_RESET_CONFIG:
637 request = (MainThreadRequest) msg.obj;
638 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
639 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
640 break;
641
642 case EVENT_NV_RESET_CONFIG_DONE:
643 handleNullReturnEvent(msg, "nvResetConfig");
644 break;
645
Jake Hamby7c27be32014-03-03 13:25:59 -0800646 case CMD_GET_PREFERRED_NETWORK_TYPE:
647 request = (MainThreadRequest) msg.obj;
648 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700649 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800650 break;
651
652 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
653 ar = (AsyncResult) msg.obj;
654 request = (MainThreadRequest) ar.userObj;
655 if (ar.exception == null && ar.result != null) {
656 request.result = ar.result; // Integer
657 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800658 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800659 if (ar.result == null) {
660 loge("getPreferredNetworkType: Empty response");
661 } else if (ar.exception instanceof CommandException) {
662 loge("getPreferredNetworkType: CommandException: " +
663 ar.exception);
664 } else {
665 loge("getPreferredNetworkType: Unknown exception");
666 }
667 }
668 synchronized (request) {
669 request.notifyAll();
670 }
671 break;
672
673 case CMD_SET_PREFERRED_NETWORK_TYPE:
674 request = (MainThreadRequest) msg.obj;
675 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
676 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700677 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800678 break;
679
680 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
681 handleNullReturnEvent(msg, "setPreferredNetworkType");
682 break;
683
Steven Liu4bf01bc2014-07-17 11:05:29 -0500684 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
685 request = (MainThreadRequest)msg.obj;
686 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
687 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
688 break;
689
690 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
691 ar = (AsyncResult)msg.obj;
692 request = (MainThreadRequest)ar.userObj;
693 request.result = ar;
694 synchronized (request) {
695 request.notifyAll();
696 }
697 break;
698
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800699 case CMD_SET_VOICEMAIL_NUMBER:
700 request = (MainThreadRequest) msg.obj;
701 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
702 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800703 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
704 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800705 break;
706
707 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
708 handleNullReturnEvent(msg, "setVoicemailNumber");
709 break;
710
Stuart Scott54788802015-03-30 13:18:01 -0700711 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
712 request = (MainThreadRequest) msg.obj;
713 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
714 request);
715 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
716 break;
717
718 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
719 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
720 break;
721
Shishir Agrawal302c8692015-06-19 13:49:39 -0700722 case CMD_PERFORM_NETWORK_SCAN:
723 request = (MainThreadRequest) msg.obj;
724 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
725 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
726 break;
727
728 case EVENT_PERFORM_NETWORK_SCAN_DONE:
729 ar = (AsyncResult) msg.obj;
730 request = (MainThreadRequest) ar.userObj;
731 CellNetworkScanResult cellScanResult;
732 if (ar.exception == null && ar.result != null) {
733 cellScanResult = new CellNetworkScanResult(
734 CellNetworkScanResult.STATUS_SUCCESS,
735 (List<OperatorInfo>) ar.result);
736 } else {
737 if (ar.result == null) {
738 loge("getCellNetworkScanResults: Empty response");
739 }
740 if (ar.exception != null) {
741 loge("getCellNetworkScanResults: Exception: " + ar.exception);
742 }
743 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
744 if (ar.exception instanceof CommandException) {
745 CommandException.Error error =
746 ((CommandException) (ar.exception)).getCommandError();
747 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
748 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
749 } else if (error == CommandException.Error.GENERIC_FAILURE) {
750 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
751 }
752 }
753 cellScanResult = new CellNetworkScanResult(errorCode, null);
754 }
755 request.result = cellScanResult;
756 synchronized (request) {
757 request.notifyAll();
758 }
759 break;
760
761 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
762 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700763 ManualNetworkSelectionArgument selArg =
764 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700765 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
766 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700767 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
768 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700769 break;
770
771 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
772 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
773 break;
774
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700775 case CMD_GET_MODEM_ACTIVITY_INFO:
776 request = (MainThreadRequest) msg.obj;
777 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700778 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700779 break;
780
781 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
782 ar = (AsyncResult) msg.obj;
783 request = (MainThreadRequest) ar.userObj;
784 if (ar.exception == null && ar.result != null) {
785 request.result = ar.result;
786 } else {
787 if (ar.result == null) {
788 loge("queryModemActivityInfo: Empty response");
789 } else if (ar.exception instanceof CommandException) {
790 loge("queryModemActivityInfo: CommandException: " +
791 ar.exception);
792 } else {
793 loge("queryModemActivityInfo: Unknown exception");
794 }
795 }
Amit Mahajand4766222016-01-28 15:28:28 -0800796 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
797 if (request.result == null) {
798 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
799 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700800 synchronized (request) {
801 request.notifyAll();
802 }
803 break;
804
Meng Wang1a7c35a2016-05-05 20:56:15 -0700805 case CMD_SET_ALLOWED_CARRIERS:
806 request = (MainThreadRequest) msg.obj;
807 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
808 mPhone.setAllowedCarriers(
809 (List<CarrierIdentifier>) request.argument,
810 onCompleted);
811 break;
812
813 case EVENT_SET_ALLOWED_CARRIERS_DONE:
814 ar = (AsyncResult) msg.obj;
815 request = (MainThreadRequest) ar.userObj;
816 if (ar.exception == null && ar.result != null) {
817 request.result = ar.result;
818 } else {
819 if (ar.result == null) {
820 loge("setAllowedCarriers: Empty response");
821 } else if (ar.exception instanceof CommandException) {
822 loge("setAllowedCarriers: CommandException: " +
823 ar.exception);
824 } else {
825 loge("setAllowedCarriers: Unknown exception");
826 }
827 }
828 // Result cannot be null. Return -1 on error.
829 if (request.result == null) {
830 request.result = new int[]{-1};
831 }
832 synchronized (request) {
833 request.notifyAll();
834 }
835 break;
836
837 case CMD_GET_ALLOWED_CARRIERS:
838 request = (MainThreadRequest) msg.obj;
839 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
840 mPhone.getAllowedCarriers(onCompleted);
841 break;
842
843 case EVENT_GET_ALLOWED_CARRIERS_DONE:
844 ar = (AsyncResult) msg.obj;
845 request = (MainThreadRequest) ar.userObj;
846 if (ar.exception == null && ar.result != null) {
847 request.result = ar.result;
848 } else {
849 if (ar.result == null) {
850 loge("getAllowedCarriers: Empty response");
851 } else if (ar.exception instanceof CommandException) {
852 loge("getAllowedCarriers: CommandException: " +
853 ar.exception);
854 } else {
855 loge("getAllowedCarriers: Unknown exception");
856 }
857 }
858 // Result cannot be null. Return empty list of CarrierIdentifier.
859 if (request.result == null) {
860 request.result = new ArrayList<CarrierIdentifier>(0);
861 }
862 synchronized (request) {
863 request.notifyAll();
864 }
865 break;
866
Nathan Haroldb3014052017-01-25 15:57:32 -0800867 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
868 ar = (AsyncResult) msg.obj;
869 request = (MainThreadRequest) ar.userObj;
870 if (ar.exception == null && ar.result != null) {
871 request.result = ar.result;
872 } else {
873 request.result = new IllegalArgumentException(
874 "Failed to retrieve Forbidden Plmns");
875 if (ar.result == null) {
876 loge("getForbiddenPlmns: Empty response");
877 } else {
878 loge("getForbiddenPlmns: Unknown exception");
879 }
880 }
881 synchronized (request) {
882 request.notifyAll();
883 }
884 break;
885
886 case CMD_GET_FORBIDDEN_PLMNS:
887 request = (MainThreadRequest) msg.obj;
888 uiccCard = getUiccCardFromRequest(request);
889 if (uiccCard == null) {
890 loge("getForbiddenPlmns() UiccCard is null");
891 request.result = new IllegalArgumentException(
892 "getForbiddenPlmns() UiccCard is null");
893 synchronized (request) {
894 request.notifyAll();
895 }
896 break;
897 }
898 Integer appType = (Integer) request.argument;
899 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
900 if (uiccApp == null) {
901 loge("getForbiddenPlmns() no app with specified type -- "
902 + appType);
903 request.result = new IllegalArgumentException("Failed to get UICC App");
904 synchronized (request) {
905 request.notifyAll();
906 }
907 break;
908 } else {
909 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
910 + " specified type -- " + appType);
911 }
912 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
913 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
914 onCompleted);
915 break;
916
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700917 default:
918 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
919 break;
920 }
921 }
Jake Hambye994d462014-02-03 13:10:13 -0800922
923 private void handleNullReturnEvent(Message msg, String command) {
924 AsyncResult ar = (AsyncResult) msg.obj;
925 MainThreadRequest request = (MainThreadRequest) ar.userObj;
926 if (ar.exception == null) {
927 request.result = true;
928 } else {
929 request.result = false;
930 if (ar.exception instanceof CommandException) {
931 loge(command + ": CommandException: " + ar.exception);
932 } else {
933 loge(command + ": Unknown exception");
934 }
935 }
936 synchronized (request) {
937 request.notifyAll();
938 }
939 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 }
941
942 /**
943 * Posts the specified command to be executed on the main thread,
944 * waits for the request to complete, and returns the result.
945 * @see #sendRequestAsync
946 */
947 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800948 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700949 }
950
951 /**
952 * Posts the specified command to be executed on the main thread,
953 * waits for the request to complete, and returns the result.
954 * @see #sendRequestAsync
955 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800956 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700957 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
958 throw new RuntimeException("This method will deadlock if called from the main thread.");
959 }
960
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800961 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700962 Message msg = mMainThreadHandler.obtainMessage(command, request);
963 msg.sendToTarget();
964
965 // Wait for the request to complete
966 synchronized (request) {
967 while (request.result == null) {
968 try {
969 request.wait();
970 } catch (InterruptedException e) {
971 // Do nothing, go back and wait until the request is complete
972 }
973 }
974 }
975 return request.result;
976 }
977
978 /**
979 * Asynchronous ("fire and forget") version of sendRequest():
980 * Posts the specified command to be executed on the main thread, and
981 * returns immediately.
982 * @see #sendRequest
983 */
984 private void sendRequestAsync(int command) {
985 mMainThreadHandler.sendEmptyMessage(command);
986 }
987
988 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700989 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
990 * @see {@link #sendRequest(int,Object)}
991 */
992 private void sendRequestAsync(int command, Object argument) {
993 MainThreadRequest request = new MainThreadRequest(argument);
994 Message msg = mMainThreadHandler.obtainMessage(command, request);
995 msg.sendToTarget();
996 }
997
998 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700999 * Initialize the singleton PhoneInterfaceManager instance.
1000 * This is only done once, at startup, from PhoneApp.onCreate().
1001 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001002 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001003 synchronized (PhoneInterfaceManager.class) {
1004 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001005 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001006 } else {
1007 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1008 }
1009 return sInstance;
1010 }
1011 }
1012
1013 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001014 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001015 mApp = app;
1016 mPhone = phone;
1017 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001018 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1020 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001021 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001022 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001023 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001024
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001025 publish();
1026 }
1027
1028 private void publish() {
1029 if (DBG) log("publish: " + this);
1030
1031 ServiceManager.addService("phone", this);
1032 }
1033
Stuart Scott584921c2015-01-15 17:10:34 -08001034 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001035 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1036 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001037 }
1038
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001039 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1040 Phone phone = getPhoneFromRequest(request);
1041 return phone == null ? null :
1042 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1043 }
1044
Wink Saville36469e72014-06-11 15:17:00 -07001045 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001046 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001047 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001048 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001049 //
1050 // Implementation of the ITelephony interface.
1051 //
1052
1053 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001054 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001055 }
1056
Wink Savilleb564aae2014-10-23 10:18:09 -07001057 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001058 if (DBG) log("dial: " + number);
1059 // No permission check needed here: This is just a wrapper around the
1060 // ACTION_DIAL intent, which is available to any app since it puts up
1061 // the UI before it does anything.
1062
1063 String url = createTelUrl(number);
1064 if (url == null) {
1065 return;
1066 }
1067
1068 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001069 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001070 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1071 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1072 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1073 mApp.startActivity(intent);
1074 }
1075 }
1076
1077 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001078 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001079 }
1080
Wink Savilleb564aae2014-10-23 10:18:09 -07001081 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001082 if (DBG) log("call: " + number);
1083
1084 // This is just a wrapper around the ACTION_CALL intent, but we still
1085 // need to do a permission check since we're calling startActivity()
1086 // from the context of the phone app.
1087 enforceCallPermission();
1088
1089 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1090 != AppOpsManager.MODE_ALLOWED) {
1091 return;
1092 }
1093
1094 String url = createTelUrl(number);
1095 if (url == null) {
1096 return;
1097 }
1098
Wink Saville08874612014-08-31 19:19:58 -07001099 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001100 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001101 if (slist != null) {
1102 for (SubscriptionInfo subInfoRecord : slist) {
1103 if (subInfoRecord.getSubscriptionId() == subId) {
1104 isValid = true;
1105 break;
1106 }
Wink Saville08874612014-08-31 19:19:58 -07001107 }
1108 }
1109 if (isValid == false) {
1110 return;
1111 }
1112
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001113 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001114 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001115 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1116 mApp.startActivity(intent);
1117 }
1118
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001119 /**
1120 * End a call based on call state
1121 * @return true is a call was ended
1122 */
1123 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001124 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001125 }
1126
1127 /**
1128 * End a call based on the call state of the subId
1129 * @return true is a call was ended
1130 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001131 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001132 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001133 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001134 }
1135
1136 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001137 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001138 }
1139
Wink Savilleb564aae2014-10-23 10:18:09 -07001140 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001141 if (DBG) log("answerRingingCall...");
1142 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1143 // but that can probably wait till the big TelephonyManager API overhaul.
1144 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1145 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001146 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001147 }
1148
1149 /**
1150 * Make the actual telephony calls to implement answerRingingCall().
1151 * This should only be called from the main thread of the Phone app.
1152 * @see #answerRingingCall
1153 *
1154 * TODO: it would be nice to return true if we answered the call, or
1155 * false if there wasn't actually a ringing incoming call, or some
1156 * other error occurred. (In other words, pass back the return value
1157 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1158 * But that would require calling this method via sendRequest() rather
1159 * than sendRequestAsync(), and right now we don't actually *need* that
1160 * return value, so let's just return void for now.
1161 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001162 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001163 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001164 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001165 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1166 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167 if (hasActiveCall && hasHoldingCall) {
1168 // Both lines are in use!
1169 // TODO: provide a flag to let the caller specify what
1170 // policy to use if both lines are in use. (The current
1171 // behavior is hardwired to "answer incoming, end ongoing",
1172 // which is how the CALL button is specced to behave.)
1173 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1174 return;
1175 } else {
1176 // answerCall() will automatically hold the current active
1177 // call, if there is one.
1178 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1179 return;
1180 }
1181 } else {
1182 // No call was ringing.
1183 return;
1184 }
1185 }
1186
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001187 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001188 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001189 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001190 public void silenceRinger() {
1191 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001192 }
1193
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001194 @Override
1195 public boolean isOffhook(String callingPackage) {
1196 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001197 }
1198
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001199 @Override
1200 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1201 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1202 return false;
1203 }
1204
Sanket Padawe356d7632015-06-22 14:03:32 -07001205 final Phone phone = getPhone(subId);
1206 if (phone != null) {
1207 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1208 } else {
1209 return false;
1210 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001211 }
1212
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001213 @Override
1214 public boolean isRinging(String callingPackage) {
1215 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001216 }
1217
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001218 @Override
1219 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1220 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1221 return false;
1222 }
1223
Sanket Padawe356d7632015-06-22 14:03:32 -07001224 final Phone phone = getPhone(subId);
1225 if (phone != null) {
1226 return (phone.getState() == PhoneConstants.State.RINGING);
1227 } else {
1228 return false;
1229 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001230 }
1231
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001232 @Override
1233 public boolean isIdle(String callingPackage) {
1234 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001235 }
1236
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001237 @Override
1238 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1239 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1240 return false;
1241 }
1242
Sanket Padawe356d7632015-06-22 14:03:32 -07001243 final Phone phone = getPhone(subId);
1244 if (phone != null) {
1245 return (phone.getState() == PhoneConstants.State.IDLE);
1246 } else {
1247 return false;
1248 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001249 }
1250
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001251 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001252 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001253 }
1254
Wink Savilleb564aae2014-10-23 10:18:09 -07001255 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001256 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001257 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1258 }
1259
1260 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001261 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001262 }
1263
Wink Savilleb564aae2014-10-23 10:18:09 -07001264 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001265 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001266 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1267 }
1268
1269 /** {@hide} */
1270 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001271 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001272 }
1273
Wink Savilleb564aae2014-10-23 10:18:09 -07001274 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001275 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001276 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277 checkSimPin.start();
1278 return checkSimPin.unlockSim(null, pin);
1279 }
1280
Wink Saville9de0f752013-10-22 19:04:03 -07001281 /** {@hide} */
1282 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001283 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001284 }
1285
Wink Savilleb564aae2014-10-23 10:18:09 -07001286 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001288 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001289 checkSimPuk.start();
1290 return checkSimPuk.unlockSim(puk, pin);
1291 }
1292
1293 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001294 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001295 * a synchronous one.
1296 */
1297 private static class UnlockSim extends Thread {
1298
1299 private final IccCard mSimCard;
1300
1301 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001302 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1303 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001304
1305 // For replies from SimCard interface
1306 private Handler mHandler;
1307
1308 // For async handler to identify request type
1309 private static final int SUPPLY_PIN_COMPLETE = 100;
1310
1311 public UnlockSim(IccCard simCard) {
1312 mSimCard = simCard;
1313 }
1314
1315 @Override
1316 public void run() {
1317 Looper.prepare();
1318 synchronized (UnlockSim.this) {
1319 mHandler = new Handler() {
1320 @Override
1321 public void handleMessage(Message msg) {
1322 AsyncResult ar = (AsyncResult) msg.obj;
1323 switch (msg.what) {
1324 case SUPPLY_PIN_COMPLETE:
1325 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1326 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001327 mRetryCount = msg.arg1;
1328 if (ar.exception != null) {
1329 if (ar.exception instanceof CommandException &&
1330 ((CommandException)(ar.exception)).getCommandError()
1331 == CommandException.Error.PASSWORD_INCORRECT) {
1332 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1333 } else {
1334 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1335 }
1336 } else {
1337 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1338 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 mDone = true;
1340 UnlockSim.this.notifyAll();
1341 }
1342 break;
1343 }
1344 }
1345 };
1346 UnlockSim.this.notifyAll();
1347 }
1348 Looper.loop();
1349 }
1350
1351 /*
1352 * Use PIN or PUK to unlock SIM card
1353 *
1354 * If PUK is null, unlock SIM card with PIN
1355 *
1356 * If PUK is not null, unlock SIM card with PUK and set PIN code
1357 */
Wink Saville9de0f752013-10-22 19:04:03 -07001358 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001359
1360 while (mHandler == null) {
1361 try {
1362 wait();
1363 } catch (InterruptedException e) {
1364 Thread.currentThread().interrupt();
1365 }
1366 }
1367 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1368
1369 if (puk == null) {
1370 mSimCard.supplyPin(pin, callback);
1371 } else {
1372 mSimCard.supplyPuk(puk, pin, callback);
1373 }
1374
1375 while (!mDone) {
1376 try {
1377 Log.d(LOG_TAG, "wait for done");
1378 wait();
1379 } catch (InterruptedException e) {
1380 // Restore the interrupted status
1381 Thread.currentThread().interrupt();
1382 }
1383 }
1384 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001385 int[] resultArray = new int[2];
1386 resultArray[0] = mResult;
1387 resultArray[1] = mRetryCount;
1388 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001389 }
1390 }
1391
1392 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001393 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001394
1395 }
1396
Wink Savilleb564aae2014-10-23 10:18:09 -07001397 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001398 // No permission check needed here: this call is harmless, and it's
1399 // needed for the ServiceState.requestStateUpdate() call (which is
1400 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001401 final Phone phone = getPhone(subId);
1402 if (phone != null) {
1403 phone.updateServiceLocation();
1404 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001405 }
1406
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001407 @Override
1408 public boolean isRadioOn(String callingPackage) {
1409 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001410 }
1411
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001412 @Override
1413 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1414 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1415 return false;
1416 }
1417 return isRadioOnForSubscriber(subId);
1418 }
1419
1420 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001421 final Phone phone = getPhone(subId);
1422 if (phone != null) {
1423 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1424 } else {
1425 return false;
1426 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001427 }
1428
1429 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001430 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001431
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001432 }
Wink Saville36469e72014-06-11 15:17:00 -07001433
Wink Savilleb564aae2014-10-23 10:18:09 -07001434 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001436 final Phone phone = getPhone(subId);
1437 if (phone != null) {
1438 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1439 }
Wink Saville36469e72014-06-11 15:17:00 -07001440 }
1441
1442 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001443 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001444 }
1445
Wink Savilleb564aae2014-10-23 10:18:09 -07001446 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001447 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001448 final Phone phone = getPhone(subId);
1449 if (phone == null) {
1450 return false;
1451 }
1452 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001453 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001454 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001455 }
1456 return true;
1457 }
Wink Saville36469e72014-06-11 15:17:00 -07001458
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001459 public boolean needMobileRadioShutdown() {
1460 /*
1461 * If any of the Radios are available, it will need to be
1462 * shutdown. So return true if any Radio is available.
1463 */
1464 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1465 Phone phone = PhoneFactory.getPhone(i);
1466 if (phone != null && phone.isRadioAvailable()) return true;
1467 }
1468 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1469 return false;
1470 }
1471
1472 public void shutdownMobileRadios() {
1473 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1474 logv("Shutting down Phone " + i);
1475 shutdownRadioUsingPhoneId(i);
1476 }
1477 }
1478
1479 private void shutdownRadioUsingPhoneId(int phoneId) {
1480 enforceModifyPermission();
1481 Phone phone = PhoneFactory.getPhone(phoneId);
1482 if (phone != null && phone.isRadioAvailable()) {
1483 phone.shutdownRadio();
1484 }
1485 }
1486
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001487 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001488 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1489 if (defaultPhone != null) {
1490 defaultPhone.setRadioPower(turnOn);
1491 return true;
1492 } else {
1493 loge("There's no default phone.");
1494 return false;
1495 }
Wink Saville36469e72014-06-11 15:17:00 -07001496 }
1497
Wink Savilleb564aae2014-10-23 10:18:09 -07001498 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001499 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001500 final Phone phone = getPhone(subId);
1501 if (phone != null) {
1502 phone.setRadioPower(turnOn);
1503 return true;
1504 } else {
1505 return false;
1506 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001507 }
1508
Wink Saville36469e72014-06-11 15:17:00 -07001509 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001510 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001511 public boolean enableDataConnectivity() {
1512 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001513 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001514 final Phone phone = getPhone(subId);
1515 if (phone != null) {
1516 phone.setDataEnabled(true);
1517 return true;
1518 } else {
1519 return false;
1520 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001521 }
1522
Wink Saville36469e72014-06-11 15:17:00 -07001523 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001524 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001525 public boolean disableDataConnectivity() {
1526 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001527 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001528 final Phone phone = getPhone(subId);
1529 if (phone != null) {
1530 phone.setDataEnabled(false);
1531 return true;
1532 } else {
1533 return false;
1534 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001535 }
1536
Wink Saville36469e72014-06-11 15:17:00 -07001537 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001538 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001539 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001540 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001541 final Phone phone = getPhone(subId);
1542 if (phone != null) {
1543 return phone.isDataConnectivityPossible();
1544 } else {
1545 return false;
1546 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001547 }
1548
1549 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001550 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001551 }
1552
pkanwarae03a6b2016-11-06 20:37:09 -08001553 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001554 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001555 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1556 return;
1557 }
1558 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1559 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1560 };
1561
1562
Wink Savilleb564aae2014-10-23 10:18:09 -07001563 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001564 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001565 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1566 return false;
1567 }
Wink Saville36469e72014-06-11 15:17:00 -07001568 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001569 }
1570
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001571 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001572 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001573 }
1574
Shishir Agrawala9f32182016-04-12 12:00:16 -07001575 public int getCallStateForSlot(int slotId) {
1576 Phone phone = PhoneFactory.getPhone(slotId);
1577 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1578 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001579 }
1580
Sanket Padawe356d7632015-06-22 14:03:32 -07001581 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001582 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001583 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001584 if (phone != null) {
1585 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1586 } else {
1587 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1588 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001589 }
1590
Sanket Padawe356d7632015-06-22 14:03:32 -07001591 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001592 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001593 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001594 if (phone != null) {
1595 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1596 } else {
1597 return TelephonyManager.DATA_ACTIVITY_NONE;
1598 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001599 }
1600
1601 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001602 public Bundle getCellLocation(String callingPackage) {
1603 enforceFineOrCoarseLocationPermission("getCellLocation");
1604
1605 // OP_COARSE_LOCATION controls both fine and coarse location.
1606 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1607 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001608 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001609 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001610 }
1611
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001612 if (checkIfCallerIsSelfOrForegroundUser() ||
1613 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001614 if (DBG_LOC) log("getCellLocation: is active user");
1615 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001616 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001617 if (phone == null) {
1618 return null;
1619 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001620
1621 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1622 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001623 return data;
1624 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001625 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001626 return null;
1627 }
1628 }
1629
Svetoslav64fad262015-04-14 14:35:21 -07001630 private void enforceFineOrCoarseLocationPermission(String message) {
1631 try {
1632 mApp.enforceCallingOrSelfPermission(
1633 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1634 } catch (SecurityException e) {
1635 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1636 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1637 // is the weaker precondition
1638 mApp.enforceCallingOrSelfPermission(
1639 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1640 }
1641 }
1642
1643
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001644 @Override
1645 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001646 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001647 }
1648
Sanket Padawe356d7632015-06-22 14:03:32 -07001649 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001650 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001651 mApp.enforceCallingOrSelfPermission(
1652 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001653 final Phone phone = getPhone(subId);
1654 if (phone != null) {
1655 phone.enableLocationUpdates();
1656 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 }
1658
1659 @Override
1660 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001661 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001662 }
1663
Sanket Padawe356d7632015-06-22 14:03:32 -07001664 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001665 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001666 mApp.enforceCallingOrSelfPermission(
1667 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001668 final Phone phone = getPhone(subId);
1669 if (phone != null) {
1670 phone.disableLocationUpdates();
1671 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001672 }
1673
1674 @Override
1675 @SuppressWarnings("unchecked")
1676 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001677 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1678
1679 // OP_COARSE_LOCATION controls both fine and coarse location.
1680 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1681 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1682 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001683 }
1684
1685 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1686 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1687 return null;
1688 }
Svetoslav64fad262015-04-14 14:35:21 -07001689
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001690 if (checkIfCallerIsSelfOrForegroundUser() ||
1691 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001692 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1693
1694 ArrayList<NeighboringCellInfo> cells = null;
1695
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001696 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001697 try {
1698 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001699 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001700 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001701 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001702 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001703 }
1704 return cells;
1705 } else {
1706 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1707 return null;
1708 }
1709 }
1710
1711
1712 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001713 public List<CellInfo> getAllCellInfo(String callingPackage) {
1714 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1715
1716 // OP_COARSE_LOCATION controls both fine and coarse location.
1717 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1718 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1719 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001720 }
1721
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001722 if (checkIfCallerIsSelfOrForegroundUser() ||
1723 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001724 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001725 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001726 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1727 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001728 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1729 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001730 }
1731 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001732 } else {
1733 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1734 return null;
1735 }
1736 }
1737
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001738 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001739 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08001740 enforceModifyPermission();
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001741 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1742 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001743 }
1744
Shishir Agrawala9f32182016-04-12 12:00:16 -07001745 @Override
1746 public String getImeiForSlot(int slotId, String callingPackage) {
1747 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1748 return null;
1749 }
1750 Phone phone = PhoneFactory.getPhone(slotId);
1751 return phone == null ? null : phone.getImei();
1752 }
1753
1754 @Override
1755 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1756 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1757 return null;
1758 }
1759 Phone phone = PhoneFactory.getPhone(slotId);
1760 return phone == null ? null : phone.getDeviceSvn();
1761 }
1762
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001763 //
1764 // Internal helper methods.
1765 //
1766
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001767 /**
1768 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1769 */
1770 private boolean checkCallerInteractAcrossUsersFull() {
1771 return mPhone.getContext().checkCallingOrSelfPermission(
1772 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1773 == PackageManager.PERMISSION_GRANTED;
1774 }
1775
Jake Hambye994d462014-02-03 13:10:13 -08001776 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001777 boolean ok;
1778
1779 boolean self = Binder.getCallingUid() == Process.myUid();
1780 if (!self) {
1781 // Get the caller's user id then clear the calling identity
1782 // which will be restored in the finally clause.
1783 int callingUser = UserHandle.getCallingUserId();
1784 long ident = Binder.clearCallingIdentity();
1785
1786 try {
1787 // With calling identity cleared the current user is the foreground user.
1788 int foregroundUser = ActivityManager.getCurrentUser();
1789 ok = (foregroundUser == callingUser);
1790 if (DBG_LOC) {
1791 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1792 + " callingUser=" + callingUser + " ok=" + ok);
1793 }
1794 } catch (Exception ex) {
1795 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1796 ok = false;
1797 } finally {
1798 Binder.restoreCallingIdentity(ident);
1799 }
1800 } else {
1801 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1802 ok = true;
1803 }
1804 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1805 return ok;
1806 }
1807
1808 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001809 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1810 *
1811 * @throws SecurityException if the caller does not have the required permission
1812 */
1813 private void enforceModifyPermission() {
1814 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1815 }
1816
1817 /**
Junda Liua2e36012014-07-09 18:30:01 -07001818 * Make sure either system app or the caller has carrier privilege.
1819 *
1820 * @throws SecurityException if the caller does not have the required permission/privilege
1821 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001822 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001823 int permission = mApp.checkCallingOrSelfPermission(
1824 android.Manifest.permission.MODIFY_PHONE_STATE);
1825 if (permission == PackageManager.PERMISSION_GRANTED) {
1826 return;
1827 }
1828
1829 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001830 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001831 }
1832
1833 /**
1834 * Make sure the caller has carrier privilege.
1835 *
1836 * @throws SecurityException if the caller does not have the required permission
1837 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001838 private void enforceCarrierPrivilege(int subId) {
1839 if (getCarrierPrivilegeStatus(subId) !=
1840 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001841 loge("No Carrier Privilege.");
1842 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001843 }
1844 }
1845
1846 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001847 * Make sure the caller has the CALL_PHONE permission.
1848 *
1849 * @throws SecurityException if the caller does not have the required permission
1850 */
1851 private void enforceCallPermission() {
1852 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1853 }
1854
Stuart Scott8eef64f2015-04-08 15:13:54 -07001855 private void enforceConnectivityInternalPermission() {
1856 mApp.enforceCallingOrSelfPermission(
1857 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1858 "ConnectivityService");
1859 }
1860
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001861 private String createTelUrl(String number) {
1862 if (TextUtils.isEmpty(number)) {
1863 return null;
1864 }
1865
Jake Hambye994d462014-02-03 13:10:13 -08001866 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001867 }
1868
Ihab Awadf9e92732013-12-05 18:02:52 -08001869 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001870 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1871 }
1872
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001873 private static void logv(String msg) {
1874 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1875 }
1876
Ihab Awadf9e92732013-12-05 18:02:52 -08001877 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001878 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1879 }
1880
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001881 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001882 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001883 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001884 }
1885
Sanket Padawe356d7632015-06-22 14:03:32 -07001886 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001887 public int getActivePhoneTypeForSlot(int slotId) {
1888 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001889 if (phone == null) {
1890 return PhoneConstants.PHONE_TYPE_NONE;
1891 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001892 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001893 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001894 }
1895
1896 /**
1897 * Returns the CDMA ERI icon index to display
1898 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001899 @Override
1900 public int getCdmaEriIconIndex(String callingPackage) {
1901 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001902 }
1903
Sanket Padawe356d7632015-06-22 14:03:32 -07001904 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001905 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1906 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1907 return -1;
1908 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001909 final Phone phone = getPhone(subId);
1910 if (phone != null) {
1911 return phone.getCdmaEriIconIndex();
1912 } else {
1913 return -1;
1914 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001915 }
1916
1917 /**
1918 * Returns the CDMA ERI icon mode,
1919 * 0 - ON
1920 * 1 - FLASHING
1921 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001922 @Override
1923 public int getCdmaEriIconMode(String callingPackage) {
1924 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001925 }
1926
Sanket Padawe356d7632015-06-22 14:03:32 -07001927 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001928 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1929 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1930 return -1;
1931 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001932 final Phone phone = getPhone(subId);
1933 if (phone != null) {
1934 return phone.getCdmaEriIconMode();
1935 } else {
1936 return -1;
1937 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001938 }
1939
1940 /**
1941 * Returns the CDMA ERI text,
1942 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001943 @Override
1944 public String getCdmaEriText(String callingPackage) {
1945 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001946 }
1947
Sanket Padawe356d7632015-06-22 14:03:32 -07001948 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001949 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1950 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1951 return null;
1952 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001953 final Phone phone = getPhone(subId);
1954 if (phone != null) {
1955 return phone.getCdmaEriText();
1956 } else {
1957 return null;
1958 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001959 }
1960
1961 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001962 * Returns the CDMA MDN.
1963 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001964 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001965 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001966 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001967 final Phone phone = getPhone(subId);
1968 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1969 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001970 } else {
1971 return null;
1972 }
1973 }
1974
1975 /**
1976 * Returns the CDMA MIN.
1977 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001978 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001979 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001980 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001981 final Phone phone = getPhone(subId);
1982 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1983 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001984 } else {
1985 return null;
1986 }
1987 }
1988
1989 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001990 * Returns true if CDMA provisioning needs to run.
1991 */
1992 public boolean needsOtaServiceProvisioning() {
1993 return mPhone.needsOtaServiceProvisioning();
1994 }
1995
1996 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001997 * Sets the voice mail number of a given subId.
1998 */
1999 @Override
2000 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002001 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002002 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2003 new Pair<String, String>(alphaTag, number), new Integer(subId));
2004 return success;
2005 }
2006
Ta-wei Yen87c49842016-05-13 21:19:52 -07002007 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07002008 public void setVisualVoicemailEnabled(String callingPackage,
2009 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
2010 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2011 if (!TextUtils.equals(callingPackage,
2012 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2013 enforceModifyPermissionOrCarrierPrivilege(
2014 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
2015 }
2016 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2017 }
2018
2019 @Override
2020 public boolean isVisualVoicemailEnabled(String callingPackage,
2021 PhoneAccountHandle phoneAccountHandle) {
2022 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2023 return false;
2024 }
2025 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2026 }
2027
2028 @Override
Ta-wei Yendca928f2017-01-10 16:17:08 -08002029 public String getVisualVoicemailPackageName(String callingPackage,
2030 @Nullable PhoneAccountHandle phoneAccountHandle) {
2031 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2032 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2033 return null;
2034 }
2035 int subId = PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle);
2036 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2037 }
2038
2039 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002040 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2041 VisualVoicemailSmsFilterSettings settings) {
2042 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002043 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002044 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2045 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002046 }
2047
2048 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002049 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
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 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002053 }
2054
2055 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002056 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2057 String callingPackage, int subId) {
2058 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002059 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002060 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002061 }
2062
2063 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002064 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002065 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002066 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002067 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2068 }
2069
2070 @Override
2071 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2072 String number, int port, String text, PendingIntent sentIntent) {
2073 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002074 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002075 enforceSendSmsPermission();
2076 // Make the calls as the phone process.
2077 final long identity = Binder.clearCallingIdentity();
2078 try {
2079 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2080 if (port == 0) {
2081 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2082 sentIntent, null, false);
2083 } else {
2084 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2085 smsManager.sendDataMessageWithSelfPermissions(number, null,
2086 (short) port, data, sentIntent, null);
2087 }
2088 } finally {
2089 Binder.restoreCallingIdentity(identity);
2090 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002091 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002092 /**
fionaxu0152e512016-11-14 13:36:14 -08002093 * Sets the voice activation state of a given subId.
2094 */
2095 @Override
2096 public void setVoiceActivationState(int subId, int activationState) {
2097 enforceModifyPermissionOrCarrierPrivilege(subId);
2098 final Phone phone = getPhone(subId);
2099 if (phone != null) {
2100 phone.setVoiceActivationState(activationState);
2101 } else {
2102 loge("setVoiceActivationState fails with invalid subId: " + subId);
2103 }
2104 }
2105
2106 /**
2107 * Sets the data activation state of a given subId.
2108 */
2109 @Override
2110 public void setDataActivationState(int subId, int activationState) {
2111 enforceModifyPermissionOrCarrierPrivilege(subId);
2112 final Phone phone = getPhone(subId);
2113 if (phone != null) {
2114 phone.setDataActivationState(activationState);
2115 } else {
2116 loge("setVoiceActivationState fails with invalid subId: " + subId);
2117 }
2118 }
2119
2120 /**
2121 * Returns the voice activation state of a given subId.
2122 */
2123 @Override
2124 public int getVoiceActivationState(int subId, String callingPackage) {
2125 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2126 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2127 }
2128 final Phone phone = getPhone(subId);
2129 if (phone != null) {
2130 return phone.getVoiceActivationState();
2131 } else {
2132 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2133 }
2134 }
2135
2136 /**
2137 * Returns the data activation state of a given subId.
2138 */
2139 @Override
2140 public int getDataActivationState(int subId, String callingPackage) {
2141 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2142 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2143 }
2144 final Phone phone = getPhone(subId);
2145 if (phone != null) {
2146 return phone.getDataActivationState();
2147 } else {
2148 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2149 }
2150 }
2151
2152 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002153 * Returns the unread count of voicemails
2154 */
2155 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002156 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002157 }
2158
2159 /**
2160 * Returns the unread count of voicemails for a subId
2161 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002162 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002163 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002164 final Phone phone = getPhone(subId);
2165 if (phone != null) {
2166 return phone.getVoiceMessageCount();
2167 } else {
2168 return 0;
2169 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002170 }
2171
2172 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002173 * returns true, if the device is in a state where both voice and data
2174 * are supported simultaneously. This can change based on location or network condition.
2175 */
2176 @Override
2177 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2178 final Phone phone = getPhone(subId);
2179 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2180 }
2181
2182 /**
fionaxue559f972017-01-24 22:31:12 -08002183 * Send the dialer code if called from the current default dialer and the input code follows the
2184 * format of *#*#<code>#*#*
2185 * <p>
2186 * Requires Permission:
2187 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2188 *
2189 * @param inputCode The dialer code to send
2190 * @return true if successfully sent, false otherwise
2191 */
2192 @Override
2193 public boolean sendDialerCode(String callingPackage, String inputCode) {
2194 enforceModifyPermission();
2195 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2196 if (TextUtils.equals(callingPackage,
2197 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2198 final Phone phone = getPhone(getDefaultSubscription());
2199 if (phone != null) {
2200 return phone.sendDialerCode(inputCode);
2201 }
2202 }
2203 return false;
2204 }
2205
2206 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002207 * Returns the data network type.
2208 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002209 *
2210 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2211 */
2212 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002213 public int getNetworkType() {
2214 final Phone phone = getPhone(getDefaultSubscription());
2215 if (phone != null) {
2216 return phone.getServiceState().getDataNetworkType();
2217 } else {
2218 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2219 }
Wink Saville36469e72014-06-11 15:17:00 -07002220 }
2221
2222 /**
2223 * Returns the network type for a subId
2224 */
2225 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002226 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2227 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2228 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2229 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002230
Sanket Padawe356d7632015-06-22 14:03:32 -07002231 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002232 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002233 return phone.getServiceState().getDataNetworkType();
2234 } else {
2235 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2236 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002237 }
2238
2239 /**
2240 * Returns the data network type
2241 */
2242 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002243 public int getDataNetworkType(String callingPackage) {
2244 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002245 }
2246
2247 /**
2248 * Returns the data network type for a subId
2249 */
2250 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002251 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2252 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2253 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2254 }
2255
Sanket Padawe356d7632015-06-22 14:03:32 -07002256 final Phone phone = getPhone(subId);
2257 if (phone != null) {
2258 return phone.getServiceState().getDataNetworkType();
2259 } else {
2260 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2261 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002262 }
2263
2264 /**
Wink Saville36469e72014-06-11 15:17:00 -07002265 * Returns the Voice network type for a subId
2266 */
2267 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002268 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2269 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2270 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2271 }
2272
Sanket Padawe356d7632015-06-22 14:03:32 -07002273 final Phone phone = getPhone(subId);
2274 if (phone != null) {
2275 return phone.getServiceState().getVoiceNetworkType();
2276 } else {
2277 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2278 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002279 }
2280
2281 /**
2282 * @return true if a ICC card is present
2283 */
2284 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002285 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002286 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002287 }
2288
2289 /**
2290 * @return true if a ICC card is present for a slotId
2291 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002292 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002293 public boolean hasIccCardUsingSlotId(int slotId) {
fionaxu6e6c68b2016-06-23 13:31:32 -07002294 final Phone phone = PhoneFactory.getPhone(slotId);
2295 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002296 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002297 } else {
2298 return false;
2299 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002300 }
2301
2302 /**
2303 * Return if the current radio is LTE on CDMA. This
2304 * is a tri-state return value as for a period of time
2305 * the mode may be unknown.
2306 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002307 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002308 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002309 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002310 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002311 @Override
2312 public int getLteOnCdmaMode(String callingPackage) {
2313 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002314 }
2315
Sanket Padawe356d7632015-06-22 14:03:32 -07002316 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002317 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2318 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2319 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2320 }
2321
Sanket Padawe356d7632015-06-22 14:03:32 -07002322 final Phone phone = getPhone(subId);
2323 if (phone == null) {
2324 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2325 } else {
2326 return phone.getLteOnCdmaMode();
2327 }
Wink Saville36469e72014-06-11 15:17:00 -07002328 }
2329
2330 public void setPhone(Phone phone) {
2331 mPhone = phone;
2332 }
2333
2334 /**
2335 * {@hide}
2336 * Returns Default subId, 0 in the case of single standby.
2337 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002338 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002339 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002340 }
2341
Shishir Agrawala9f32182016-04-12 12:00:16 -07002342 private int getSlotForDefaultSubscription() {
2343 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2344 }
2345
Wink Savilleb564aae2014-10-23 10:18:09 -07002346 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002347 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002348 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002349
2350 /**
2351 * @see android.telephony.TelephonyManager.WifiCallingChoices
2352 */
2353 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002354 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2355 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002356 }
2357
2358 /**
2359 * @see android.telephony.TelephonyManager.WifiCallingChoices
2360 */
2361 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002362 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2363 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2364 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002365 }
2366
Sailesh Nepald1e68152013-12-12 19:08:02 -08002367 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002368 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002369 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002370 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002371
Shishir Agrawal566b7612013-10-28 14:41:00 -07002372 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002373 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2374 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002375
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002376 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002377 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002378 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002379 if (DBG) log("iccOpenLogicalChannel: " + response);
2380 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002381 }
2382
2383 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002384 public boolean iccCloseLogicalChannel(int subId, int channel) {
2385 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002386
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002387 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002388 if (channel < 0) {
2389 return false;
2390 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002391 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002392 if (DBG) log("iccCloseLogicalChannel: " + success);
2393 return success;
2394 }
2395
2396 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002397 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002398 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002399 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002400
2401 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002402 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2403 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002404 " data=" + data);
2405 }
2406
2407 if (channel < 0) {
2408 return "";
2409 }
2410
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002411 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002412 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002413 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2414
Shishir Agrawal566b7612013-10-28 14:41:00 -07002415 // Append the returned status code to the end of the response payload.
2416 String s = Integer.toHexString(
2417 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002418 if (response.payload != null) {
2419 s = IccUtils.bytesToHexString(response.payload) + s;
2420 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002421 return s;
2422 }
Jake Hambye994d462014-02-03 13:10:13 -08002423
Evan Charltonc66da362014-05-16 14:06:40 -07002424 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002425 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002426 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002427 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002428
2429 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002430 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2431 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002432 }
2433
2434 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002435 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002436 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2437
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002438 // Append the returned status code to the end of the response payload.
2439 String s = Integer.toHexString(
2440 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002441 if (response.payload != null) {
2442 s = IccUtils.bytesToHexString(response.payload) + s;
2443 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002444 return s;
2445 }
2446
2447 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002448 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002449 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002450 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002451
2452 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002453 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002454 p1 + " " + p2 + " " + p3 + ":" + filePath);
2455 }
2456
2457 IccIoResult response =
2458 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002459 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2460 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002461
2462 if (DBG) {
2463 log("Exchange SIM_IO [R]" + response);
2464 }
2465
2466 byte[] result = null;
2467 int length = 2;
2468 if (response.payload != null) {
2469 length = 2 + response.payload.length;
2470 result = new byte[length];
2471 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2472 } else {
2473 result = new byte[length];
2474 }
2475
2476 result[length - 1] = (byte) response.sw2;
2477 result[length - 2] = (byte) response.sw1;
2478 return result;
2479 }
2480
Nathan Haroldb3014052017-01-25 15:57:32 -08002481 /**
2482 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2483 * on a particular subscription
2484 */
2485 public String[] getForbiddenPlmns(int subId, int appType) {
2486 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
2487 "Requires READ_PRIVILEGED_PHONE_STATE");
2488 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2489 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2490 return null;
2491 }
2492 Object response = sendRequest(
2493 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2494 if (response instanceof String[]) {
2495 return (String[]) response;
2496 }
2497 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2498 return null;
2499 }
2500
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002501 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002502 public String sendEnvelopeWithStatus(int subId, String content) {
2503 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002504
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002505 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002506 if (response.payload == null) {
2507 return "";
2508 }
2509
2510 // Append the returned status code to the end of the response payload.
2511 String s = Integer.toHexString(
2512 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2513 s = IccUtils.bytesToHexString(response.payload) + s;
2514 return s;
2515 }
2516
Jake Hambye994d462014-02-03 13:10:13 -08002517 /**
2518 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2519 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2520 *
2521 * @param itemID the ID of the item to read
2522 * @return the NV item as a String, or null on error.
2523 */
2524 @Override
2525 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002526 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002527 if (DBG) log("nvReadItem: item " + itemID);
2528 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2529 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2530 return value;
2531 }
2532
2533 /**
2534 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2535 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2536 *
2537 * @param itemID the ID of the item to read
2538 * @param itemValue the value to write, as a String
2539 * @return true on success; false on any failure
2540 */
2541 @Override
2542 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002543 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002544 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2545 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2546 new Pair<Integer, String>(itemID, itemValue));
2547 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2548 return success;
2549 }
2550
2551 /**
2552 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2553 * Used for device configuration by some CDMA operators.
2554 *
2555 * @param preferredRoamingList byte array containing the new PRL
2556 * @return true on success; false on any failure
2557 */
2558 @Override
2559 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002560 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002561 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2562 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2563 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2564 return success;
2565 }
2566
2567 /**
2568 * Perform the specified type of NV config reset.
2569 * Used for device configuration by some CDMA operators.
2570 *
2571 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2572 * @return true on success; false on any failure
2573 */
2574 @Override
2575 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002576 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002577 if (DBG) log("nvResetConfig: type " + resetType);
2578 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2579 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2580 return success;
2581 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002582
2583 /**
Wink Saville36469e72014-06-11 15:17:00 -07002584 * {@hide}
2585 * Returns Default sim, 0 in the case of single standby.
2586 */
2587 public int getDefaultSim() {
2588 //TODO Need to get it from Telephony Devcontroller
2589 return 0;
2590 }
2591
Svet Ganovb320e182015-04-16 12:30:10 -07002592 public String[] getPcscfAddress(String apnType, String callingPackage) {
2593 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2594 return new String[0];
2595 }
2596
2597
ram87fca6f2014-07-18 18:58:44 +05302598 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002599 }
2600
Brad Ebinger51f743a2017-01-23 13:50:20 -08002601 /**
2602 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2603 * feature or {@link null} if the service is not available. If an ImsServiceController is
2604 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2605 * feature updates.
2606 */
2607 public IImsServiceController getImsServiceControllerAndListen(int slotId, int feature,
2608 IImsServiceFeatureListener callback) {
2609 enforceModifyPermission();
2610 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotId, feature,
2611 callback);
2612 }
2613
Wink Saville36469e72014-06-11 15:17:00 -07002614 public void setImsRegistrationState(boolean registered) {
2615 enforceModifyPermission();
2616 mPhone.setImsRegistrationState(registered);
2617 }
2618
2619 /**
Stuart Scott54788802015-03-30 13:18:01 -07002620 * Set the network selection mode to automatic.
2621 *
2622 */
2623 @Override
2624 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002625 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002626 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2627 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2628 }
2629
2630 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002631 * Set the network selection mode to manual with the selected carrier.
2632 */
2633 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002634 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2635 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002636 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002637 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002638 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2639 persistSelection);
2640 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002641 }
2642
2643 /**
2644 * Scans for available networks.
2645 */
2646 @Override
2647 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002648 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002649 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2650 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2651 CMD_PERFORM_NETWORK_SCAN, null, subId);
2652 return result;
2653 }
2654
2655 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002656 * Get the calculated preferred network type.
2657 * Used for debugging incorrect network type.
2658 *
2659 * @return the preferred network type, defined in RILConstants.java.
2660 */
2661 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002662 public int getCalculatedPreferredNetworkType(String callingPackage) {
2663 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2664 return RILConstants.PREFERRED_NETWORK_MODE;
2665 }
2666
Amit Mahajan43330e02014-11-18 11:54:45 -08002667 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002668 }
2669
2670 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002671 * Get the preferred network type.
2672 * Used for device configuration by some CDMA operators.
2673 *
2674 * @return the preferred network type, defined in RILConstants.java.
2675 */
2676 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002677 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002678 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002679 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002680 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002681 int networkType = (result != null ? result[0] : -1);
2682 if (DBG) log("getPreferredNetworkType: " + networkType);
2683 return networkType;
2684 }
2685
2686 /**
2687 * Set the preferred network type.
2688 * Used for device configuration by some CDMA operators.
2689 *
2690 * @param networkType the preferred network type, defined in RILConstants.java.
2691 * @return true on success; false on any failure.
2692 */
2693 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002694 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002695 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002696 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2697 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002698 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002699 if (success) {
2700 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002701 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002702 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002703 return success;
2704 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002705
2706 /**
Junda Liu475951f2014-11-07 16:45:03 -08002707 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2708 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2709 * tethering.
2710 *
2711 * @return 0: Not required. 1: required. 2: Not set.
2712 * @hide
2713 */
2714 @Override
2715 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002716 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002717 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2718 Settings.Global.TETHER_DUN_REQUIRED, 2);
2719 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2720 // config_tether_apndata.
2721 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2722 dunRequired = 1;
2723 }
2724 return dunRequired;
2725 }
2726
2727 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002728 * Set mobile data enabled
2729 * Used by the user through settings etc to turn on/off mobile data
2730 *
2731 * @param enable {@code true} turn turn data on, else {@code false}
2732 */
2733 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002734 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002735 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002736 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002737 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002738 Phone phone = PhoneFactory.getPhone(phoneId);
2739 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002740 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002741 phone.setDataEnabled(enable);
2742 } else {
2743 loge("setDataEnabled: no phone for subId=" + subId);
2744 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002745 }
2746
2747 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002748 * Get whether mobile data is enabled.
2749 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002750 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002751 *
2752 * @return {@code true} if data is enabled else {@code false}
2753 */
2754 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002755 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002756 try {
2757 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2758 null);
2759 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002760 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002761 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002762 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002763 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002764 Phone phone = PhoneFactory.getPhone(phoneId);
2765 if (phone != null) {
2766 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002767 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002768 return retVal;
2769 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002770 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002771 return false;
2772 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002773 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002774
2775 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002776 public int getCarrierPrivilegeStatus(int subId) {
2777 final Phone phone = getPhone(subId);
2778 if (phone == null) {
2779 loge("getCarrierPrivilegeStatus: Invalid subId");
2780 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2781 }
2782 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002783 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002784 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002785 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2786 }
2787 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002788 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002789 }
Junda Liu29340342014-07-10 15:23:27 -07002790
2791 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002792 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002793 if (TextUtils.isEmpty(pkgName))
2794 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002795 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002796 if (card == null) {
2797 loge("checkCarrierPrivilegesForPackage: No UICC");
2798 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2799 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002800 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2801 }
2802
2803 @Override
2804 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002805 if (TextUtils.isEmpty(pkgName))
2806 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002807 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2808 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2809 UiccCard card = UiccController.getInstance().getUiccCard(i);
2810 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002811 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002812 continue;
2813 }
2814
2815 result = card.getCarrierPrivilegeStatus(
2816 mPhone.getContext().getPackageManager(), pkgName);
2817 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2818 break;
2819 }
2820 }
2821
2822 return result;
Junda Liu29340342014-07-10 15:23:27 -07002823 }
Derek Tan89e89d42014-07-08 17:00:10 -07002824
2825 @Override
Junda Liue64de782015-04-16 17:19:16 -07002826 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2827 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2828 loge("phoneId " + phoneId + " is not valid.");
2829 return null;
2830 }
2831 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002832 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002833 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002834 return null ;
2835 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002836 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002837 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002838 }
2839
Amith Yamasani6e118872016-02-19 12:53:51 -08002840 @Override
2841 public List<String> getPackagesWithCarrierPrivileges() {
2842 PackageManager pm = mPhone.getContext().getPackageManager();
2843 List<String> privilegedPackages = new ArrayList<>();
2844 List<PackageInfo> packages = null;
2845 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2846 UiccCard card = UiccController.getInstance().getUiccCard(i);
2847 if (card == null) {
2848 // No UICC in that slot.
2849 continue;
2850 }
2851 if (card.hasCarrierPrivilegeRules()) {
2852 if (packages == null) {
2853 // Only check packages in user 0 for now
2854 packages = pm.getInstalledPackagesAsUser(
2855 PackageManager.MATCH_DISABLED_COMPONENTS
2856 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2857 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2858 }
2859 for (int p = packages.size() - 1; p >= 0; p--) {
2860 PackageInfo pkgInfo = packages.get(p);
2861 if (pkgInfo != null && pkgInfo.packageName != null
2862 && card.getCarrierPrivilegeStatus(pkgInfo)
2863 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2864 privilegedPackages.add(pkgInfo.packageName);
2865 }
2866 }
2867 }
2868 }
2869 return privilegedPackages;
2870 }
2871
Wink Savilleb564aae2014-10-23 10:18:09 -07002872 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002873 final Phone phone = getPhone(subId);
2874 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002875 if (card == null) {
2876 loge("getIccId: No UICC");
2877 return null;
2878 }
2879 String iccId = card.getIccId();
2880 if (TextUtils.isEmpty(iccId)) {
2881 loge("getIccId: ICC ID is null or empty.");
2882 return null;
2883 }
2884 return iccId;
2885 }
2886
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002887 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002888 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2889 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002890 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002891
Jeff Sharkey85190e62014-12-05 09:40:12 -08002892 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002893 final Phone phone = getPhone(subId);
2894 if (phone == null) {
2895 return false;
2896 }
2897 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002898
2899 if (DBG_MERGE) {
2900 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2901 + subscriberId + " to " + number);
2902 }
2903
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002904 if (TextUtils.isEmpty(iccId)) {
2905 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002906 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002907
Jeff Sharkey85190e62014-12-05 09:40:12 -08002908 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2909
2910 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002911 if (alphaTag == null) {
2912 editor.remove(alphaTagPrefKey);
2913 } else {
2914 editor.putString(alphaTagPrefKey, alphaTag);
2915 }
2916
Jeff Sharkey85190e62014-12-05 09:40:12 -08002917 // Record both the line number and IMSI for this ICCID, since we need to
2918 // track all merged IMSIs based on line number
2919 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2920 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002921 if (number == null) {
2922 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002923 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002924 } else {
2925 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002926 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002927 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002928
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002929 editor.commit();
2930 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002931 }
2932
2933 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002934 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002935 // This is open to apps with WRITE_SMS.
2936 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002937 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002938 return null;
2939 }
Derek Tan97ebb422014-09-05 16:55:38 -07002940
2941 String iccId = getIccId(subId);
2942 if (iccId != null) {
2943 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002944 if (DBG_MERGE) {
2945 log("getLine1NumberForDisplay returning " +
2946 mTelephonySharedPreferences.getString(numberPrefKey, null));
2947 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002948 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002949 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002950 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002951 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002952 }
2953
2954 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002955 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2956 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2957 return null;
2958 }
Derek Tan97ebb422014-09-05 16:55:38 -07002959
2960 String iccId = getIccId(subId);
2961 if (iccId != null) {
2962 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002963 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002964 }
Derek Tan97ebb422014-09-05 16:55:38 -07002965 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002966 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002967
2968 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002969 public String[] getMergedSubscriberIds(String callingPackage) {
2970 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2971 return null;
2972 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002973 final Context context = mPhone.getContext();
2974 final TelephonyManager tele = TelephonyManager.from(context);
2975 final SubscriptionManager sub = SubscriptionManager.from(context);
2976
2977 // Figure out what subscribers are currently active
2978 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002979 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2980 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2981 final long identity = Binder.clearCallingIdentity();
2982 try {
2983 final int[] subIds = sub.getActiveSubscriptionIdList();
2984 for (int subId : subIds) {
2985 activeSubscriberIds.add(tele.getSubscriberId(subId));
2986 }
2987 } finally {
2988 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002989 }
2990
2991 // First pass, find a number override for an active subscriber
2992 String mergeNumber = null;
2993 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2994 for (String key : prefs.keySet()) {
2995 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2996 final String subscriberId = (String) prefs.get(key);
2997 if (activeSubscriberIds.contains(subscriberId)) {
2998 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2999 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3000 mergeNumber = (String) prefs.get(numberKey);
3001 if (DBG_MERGE) {
3002 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3003 + " for active subscriber " + subscriberId);
3004 }
3005 if (!TextUtils.isEmpty(mergeNumber)) {
3006 break;
3007 }
3008 }
3009 }
3010 }
3011
3012 // Shortcut when no active merged subscribers
3013 if (TextUtils.isEmpty(mergeNumber)) {
3014 return null;
3015 }
3016
3017 // Second pass, find all subscribers under that line override
3018 final ArraySet<String> result = new ArraySet<>();
3019 for (String key : prefs.keySet()) {
3020 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3021 final String number = (String) prefs.get(key);
3022 if (mergeNumber.equals(number)) {
3023 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3024 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3025 final String subscriberId = (String) prefs.get(subscriberKey);
3026 if (!TextUtils.isEmpty(subscriberId)) {
3027 result.add(subscriberId);
3028 }
3029 }
3030 }
3031 }
3032
3033 final String[] resultArray = result.toArray(new String[result.size()]);
3034 Arrays.sort(resultArray);
3035 if (DBG_MERGE) {
3036 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3037 }
3038 return resultArray;
3039 }
3040
3041 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003042 public boolean setOperatorBrandOverride(int subId, String brand) {
3043 enforceCarrierPrivilege(subId);
3044 final Phone phone = getPhone(subId);
3045 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003046 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003047
3048 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003049 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003050 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3051 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003052 enforceCarrierPrivilege(subId);
3053 final Phone phone = getPhone(subId);
3054 if (phone == null) {
3055 return false;
3056 }
3057 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003058 cdmaNonRoamingList);
3059 }
3060
3061 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003062 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003063 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3064 enforceModifyPermission();
3065
3066 int returnValue = 0;
3067 try {
3068 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3069 if(result.exception == null) {
3070 if (result.result != null) {
3071 byte[] responseData = (byte[])(result.result);
3072 if(responseData.length > oemResp.length) {
3073 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3074 responseData.length + "bytes. Buffer Size is " +
3075 oemResp.length + "bytes.");
3076 }
3077 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3078 returnValue = responseData.length;
3079 }
3080 } else {
3081 CommandException ex = (CommandException) result.exception;
3082 returnValue = ex.getCommandError().ordinal();
3083 if(returnValue > 0) returnValue *= -1;
3084 }
3085 } catch (RuntimeException e) {
3086 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3087 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3088 if(returnValue > 0) returnValue *= -1;
3089 }
3090
3091 return returnValue;
3092 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003093
3094 @Override
3095 public void setRadioCapability(RadioAccessFamily[] rafs) {
3096 try {
3097 ProxyController.getInstance().setRadioCapability(rafs);
3098 } catch (RuntimeException e) {
3099 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3100 }
3101 }
3102
3103 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003104 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3105 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3106 return RadioAccessFamily.RAF_UNKNOWN;
3107 }
3108
Wink Saville5d475dd2014-10-17 15:00:58 -07003109 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3110 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003111
3112 @Override
3113 public void enableVideoCalling(boolean enable) {
3114 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003115 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003116 }
3117
3118 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003119 public boolean isVideoCallingEnabled(String callingPackage) {
3120 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3121 return false;
3122 }
3123
Andrew Lee77527ac2014-10-21 16:57:39 -07003124 // Check the user preference and the system-level IMS setting. Even if the user has
3125 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3126 // In the long run, we may instead need to check if there exists a connection service
3127 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003128 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3129 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003130 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003131 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003132
Andrew Leea1239f22015-03-02 17:44:07 -08003133 @Override
3134 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003135 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003136 }
3137
3138 @Override
3139 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003140 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003141 }
3142
Andrew Lee9431b832015-03-09 18:46:45 -07003143 @Override
3144 public boolean isTtyModeSupported() {
3145 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3146 TelephonyManager telephonyManager =
3147 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003148 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003149 }
3150
3151 @Override
3152 public boolean isHearingAidCompatibilitySupported() {
3153 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3154 }
3155
Sanket Padawe7310cc72015-01-14 09:53:20 -08003156 /**
3157 * Returns the unique device ID of phone, for example, the IMEI for
3158 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3159 *
3160 * <p>Requires Permission:
3161 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3162 */
3163 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003164 public String getDeviceId(String callingPackage) {
3165 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3166 return null;
3167 }
3168
Sanket Padawe7310cc72015-01-14 09:53:20 -08003169 final Phone phone = PhoneFactory.getPhone(0);
3170 if (phone != null) {
3171 return phone.getDeviceId();
3172 } else {
3173 return null;
3174 }
3175 }
3176
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003177 /*
3178 * {@hide}
3179 * Returns the IMS Registration Status
3180 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003181 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003182 public boolean isImsRegistered() {
3183 return mPhone.isImsRegistered();
3184 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003185
3186 @Override
3187 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3188 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3189 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003190
Nathan Haroldc55097a2015-03-11 18:14:50 -07003191 /*
3192 * {@hide}
3193 * Returns the IMS Registration Status
3194 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003195 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003196 return mPhone.isWifiCallingEnabled();
3197 }
3198
3199 /*
3200 * {@hide}
3201 * Returns the IMS Registration Status
3202 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003203 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003204 return mPhone.isVolteEnabled();
3205 }
Svet Ganovb320e182015-04-16 12:30:10 -07003206
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003207 /*
3208 * {@hide} Returns the IMS Registration Status
3209 */
3210 public boolean isVideoTelephonyAvailable() {
3211 return mPhone.isVideoEnabled();
3212 }
3213
Svet Ganovb320e182015-04-16 12:30:10 -07003214 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003215 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003216 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003217 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3218
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003219 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003220 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003221 } catch (SecurityException e) {
3222 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3223 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003224 }
Svet Ganovb320e182015-04-16 12:30:10 -07003225
3226 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3227 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3228 return false;
3229 }
3230
3231 return true;
3232 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003233
Makoto Onukifee69342015-06-29 14:44:50 -07003234 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003235 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003236 */
3237 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003238 // Default SMS app can always read it.
3239 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3240 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3241 return true;
3242 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003243
Makoto Onukie4072d12015-08-03 15:12:23 -07003244 try {
3245 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003246 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003247 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003248 // Can be read with READ_SMS too.
3249 try {
3250 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3251 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3252 if (opCode != AppOpsManager.OP_NONE) {
3253 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3254 == AppOpsManager.MODE_ALLOWED;
3255 } else {
3256 return true;
3257 }
3258 } catch (SecurityException readSmsSecurityException) {
3259 }
3260 // Can be read with READ_PHONE_NUMBER too.
3261 try {
3262 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBER,
3263 message);
3264 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBER);
3265 if (opCode != AppOpsManager.OP_NONE) {
3266 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3267 == AppOpsManager.MODE_ALLOWED;
3268 } else {
3269 return true;
3270 }
3271 } catch (SecurityException readPhoneNumberSecurityException) {
3272 }
3273
3274 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3275 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3276 ", " + android.Manifest.permission.READ_SMS + ", or " +
3277 android.Manifest.permission.READ_PHONE_NUMBER);
Makoto Onukifee69342015-06-29 14:44:50 -07003278 }
3279
Stuart Scott8eef64f2015-04-08 15:13:54 -07003280 @Override
3281 public void factoryReset(int subId) {
3282 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003283 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3284 return;
3285 }
3286
Svet Ganovcc087f82015-05-12 20:35:54 -07003287 final long identity = Binder.clearCallingIdentity();
3288 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003289 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3290 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003291 // Enable data
3292 setDataEnabled(subId, true);
3293 // Set network selection mode to automatic
3294 setNetworkSelectionModeAutomatic(subId);
3295 // Set preferred mobile network type to the best available
3296 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3297 // Turn off roaming
3298 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3299 }
3300 } finally {
3301 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003302 }
3303 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003304
3305 @Override
3306 public String getLocaleFromDefaultSim() {
3307 // We query all subscriptions instead of just the active ones, because
3308 // this might be called early on in the provisioning flow when the
3309 // subscriptions potentially aren't active yet.
3310 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3311 if (slist == null || slist.isEmpty()) {
3312 return null;
3313 }
3314
3315 // This function may be called very early, say, from the setup wizard, at
3316 // which point we won't have a default subscription set. If that's the case
3317 // we just choose the first, which will be valid in "most cases".
3318 final int defaultSubId = getDefaultSubscription();
3319 SubscriptionInfo info = null;
3320 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3321 info = slist.get(0);
3322 } else {
3323 for (SubscriptionInfo item : slist) {
3324 if (item.getSubscriptionId() == defaultSubId) {
3325 info = item;
3326 break;
3327 }
3328 }
3329
3330 if (info == null) {
3331 return null;
3332 }
3333 }
3334
3335 // Try and fetch the locale from the carrier properties or from the SIM language
3336 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003337 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003338 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003339 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003340 if (defaultPhone != null) {
3341 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3342 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003343 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003344 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3345 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003346 } else {
3347 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003348 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003349 }
3350 }
3351
Narayan Kamath011676f2015-07-29 12:04:08 +01003352 // The SIM language preferences only store a language (e.g. fr = French), not an
3353 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3354 // the SIM and carrier preferences does not include a country we add the country
3355 // determined from the SIM MCC to provide an exact locale.
3356 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003357 if (mccLocale != null) {
3358 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3359 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003360 }
3361
Tony Hill183b2de2015-06-24 14:53:58 +01003362 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003363 return null;
3364 }
3365
3366 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3367 final long identity = Binder.clearCallingIdentity();
3368 try {
3369 return mSubscriptionController.getAllSubInfoList(
3370 mPhone.getContext().getOpPackageName());
3371 } finally {
3372 Binder.restoreCallingIdentity(identity);
3373 }
3374 }
3375
3376 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3377 final long identity = Binder.clearCallingIdentity();
3378 try {
3379 return mSubscriptionController.getActiveSubscriptionInfoList(
3380 mPhone.getContext().getOpPackageName());
3381 } finally {
3382 Binder.restoreCallingIdentity(identity);
3383 }
3384 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003385
3386 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003387 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3388 * representing the state of the modem.
3389 *
3390 * NOTE: This clears the modem state, so there should only every be one caller.
3391 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003392 */
3393 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003394 public void requestModemActivityInfo(ResultReceiver result) {
3395 enforceModifyPermission();
3396
3397 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3398 Bundle bundle = new Bundle();
3399 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3400 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003401 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003402
3403 /**
3404 * {@hide}
3405 * Returns the service state information on specified subscription.
3406 */
3407 @Override
3408 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3409
3410 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3411 return null;
3412 }
3413
3414 final Phone phone = getPhone(subId);
3415 if (phone == null) {
3416 return null;
3417 }
3418
3419 return phone.getServiceState();
3420 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003421
3422 /**
3423 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3424 *
3425 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3426 * voicemail ringtone.
3427 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3428 * PhoneAccount.
3429 */
3430 @Override
3431 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3432 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3433 if (phone == null) {
3434 return null;
3435 }
3436
3437 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3438 }
3439
3440 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003441 * Sets the per-account voicemail ringtone.
3442 *
3443 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3444 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3445 *
3446 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3447 * voicemail ringtone.
3448 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3449 * PhoneAccount.
3450 */
3451 @Override
3452 public void setVoicemailRingtoneUri(String callingPackage,
3453 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3454 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3455 if (!TextUtils.equals(callingPackage,
3456 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3457 enforceModifyPermissionOrCarrierPrivilege(
3458 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3459 }
3460 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3461 if (phone == null){
3462 throw new IllegalArgumentException("The phoneAccountHandle does not correspond to an "
3463 + "active phone account.");
3464 }
3465 VoicemailNotificationSettingsUtil.setRingtoneUri(phone, uri);
3466 }
3467
3468 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003469 * Returns whether vibration is set for voicemail notification in Phone settings.
3470 *
3471 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3472 * voicemail vibration setting.
3473 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3474 */
3475 @Override
3476 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3477 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3478 if (phone == null) {
3479 return false;
3480 }
3481
3482 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3483 }
3484
Youhan Wange64578a2016-05-02 15:32:42 -07003485 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003486 * Sets the per-account voicemail vibration.
3487 *
3488 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3489 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3490 *
3491 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3492 * voicemail vibration setting.
3493 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3494 * specific PhoneAccount.
3495 */
3496 @Override
3497 public void setVoicemailVibrationEnabled(String callingPackage,
3498 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3499 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3500 if (!TextUtils.equals(callingPackage,
3501 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3502 enforceModifyPermissionOrCarrierPrivilege(
3503 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3504 }
3505
3506 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3507 if (phone == null){
3508 throw new IllegalArgumentException("The phoneAccountHandle does not correspond to an "
3509 + "active phone account.");
3510 }
3511 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone, enabled);
3512 }
3513
3514 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003515 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3516 *
3517 * @throws SecurityException if the caller does not have the required permission
3518 */
3519 private void enforceReadPrivilegedPermission() {
3520 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3521 null);
3522 }
3523
3524 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003525 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3526 * permission.
3527 *
3528 * @throws SecurityException if the caller does not have the required permission
3529 */
3530 private void enforceSendSmsPermission() {
3531 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3532 }
3533
3534 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003535 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003536 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003537 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003538 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003539 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Ta-wei Yen5d81b0c2017-03-03 16:32:26 -08003540 ComponentName componentName =
3541 RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
3542 if(componentName == null) {
3543 throw new SecurityException("Caller not current active visual voicemail package[null]");
3544 }
3545 String vvmPackage = componentName.getPackageName();
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003546 if (!callingPackage.equals(vvmPackage)) {
3547 throw new SecurityException("Caller not current active visual voicemail package[" +
3548 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003549 }
3550 }
3551
3552 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003553 * Return the application ID for the app type.
3554 *
3555 * @param subId the subscription ID that this request applies to.
3556 * @param appType the uicc app type.
3557 * @return Application ID for specificied app type, or null if no uicc.
3558 */
3559 @Override
3560 public String getAidForAppType(int subId, int appType) {
3561 enforceReadPrivilegedPermission();
3562 Phone phone = getPhone(subId);
3563 if (phone == null) {
3564 return null;
3565 }
3566 String aid = null;
3567 try {
3568 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3569 .getApplicationByType(appType).getAid();
3570 } catch (Exception e) {
3571 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3572 }
3573 return aid;
3574 }
3575
Youhan Wang4001d252016-05-11 10:29:41 -07003576 /**
3577 * Return the Electronic Serial Number.
3578 *
3579 * @param subId the subscription ID that this request applies to.
3580 * @return ESN or null if error.
3581 */
3582 @Override
3583 public String getEsn(int subId) {
3584 enforceReadPrivilegedPermission();
3585 Phone phone = getPhone(subId);
3586 if (phone == null) {
3587 return null;
3588 }
3589 String esn = null;
3590 try {
3591 esn = phone.getEsn();
3592 } catch (Exception e) {
3593 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3594 }
3595 return esn;
3596 }
3597
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003598 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003599 * Return the Preferred Roaming List Version.
3600 *
3601 * @param subId the subscription ID that this request applies to.
3602 * @return PRLVersion or null if error.
3603 */
3604 @Override
3605 public String getCdmaPrlVersion(int subId) {
3606 enforceReadPrivilegedPermission();
3607 Phone phone = getPhone(subId);
3608 if (phone == null) {
3609 return null;
3610 }
3611 String cdmaPrlVersion = null;
3612 try {
3613 cdmaPrlVersion = phone.getCdmaPrlVersion();
3614 } catch (Exception e) {
3615 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3616 }
3617 return cdmaPrlVersion;
3618 }
3619
3620 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003621 * Get snapshot of Telephony histograms
3622 * @return List of Telephony histograms
3623 * @hide
3624 */
3625 @Override
3626 public List<TelephonyHistogram> getTelephonyHistograms() {
3627 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3628 return RIL.getTelephonyRILTimingHistograms();
3629 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003630
3631 /**
3632 * {@hide}
3633 * Set the allowed carrier list for slotId
3634 * Require system privileges. In the future we may add this to carrier APIs.
3635 *
3636 * @return The number of carriers set successfully, should match length of carriers
3637 */
3638 @Override
3639 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3640 enforceModifyPermission();
3641 int subId = SubscriptionManager.getSubId(slotId)[0];
Meng Wang9b7c4e92017-02-17 11:41:27 -08003642 if (carriers == null) {
3643 throw new NullPointerException("carriers cannot be null");
3644 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003645 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3646 return retVal[0];
3647 }
3648
3649 /**
3650 * {@hide}
3651 * Get the allowed carrier list for slotId.
3652 * Require system privileges. In the future we may add this to carrier APIs.
3653 *
3654 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3655 * means all carriers are allowed.
3656 */
3657 @Override
3658 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3659 enforceReadPrivilegedPermission();
3660 int subId = SubscriptionManager.getSubId(slotId)[0];
3661 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3662 }
3663
fionaxu59545b42016-05-25 15:53:37 -07003664 /**
3665 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3666 * @param subId the subscription ID that this action applies to.
3667 * @param enabled control enable or disable metered apns.
3668 * {@hide}
3669 */
3670 @Override
3671 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3672 enforceModifyPermission();
3673 final Phone phone = getPhone(subId);
3674 if (phone == null) {
3675 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3676 return;
3677 }
3678 try {
3679 phone.carrierActionSetMeteredApnsEnabled(enabled);
3680 } catch (Exception e) {
3681 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3682 }
3683 }
3684
3685 /**
3686 * Action set from carrier signalling broadcast receivers to enable/disable radio
3687 * @param subId the subscription ID that this action applies to.
3688 * @param enabled control enable or disable radio.
3689 * {@hide}
3690 */
3691 @Override
3692 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3693 enforceModifyPermission();
3694 final Phone phone = getPhone(subId);
3695 if (phone == null) {
3696 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3697 return;
3698 }
3699 try {
3700 phone.carrierActionSetRadioEnabled(enabled);
3701 } catch (Exception e) {
3702 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3703 }
3704 }
3705
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003706 /**
3707 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3708 * bug report is being generated.
3709 */
3710 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003711 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003712 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3713 != PackageManager.PERMISSION_GRANTED) {
3714 writer.println("Permission Denial: can't dump Phone from pid="
3715 + Binder.getCallingPid()
3716 + ", uid=" + Binder.getCallingUid()
3717 + "without permission "
3718 + android.Manifest.permission.DUMP);
3719 return;
3720 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003721 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003722 }
Jack Yueb89b242016-06-22 13:27:47 -07003723
3724 /**
3725 * Get aggregated video call data usage from all subscriptions since boot.
3726 * @return total data usage in bytes
3727 * {@hide}
3728 */
3729 @Override
3730 public long getVtDataUsage() {
3731 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3732 null);
3733
3734 // NetworkStatsService keeps tracking the active network interface and identity. It will
3735 // record the delta with the corresponding network identity. What we need to do here is
3736 // returning total video call data usage from all subscriptions since boot.
3737
3738 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3739 // simultaneous VT calls.
3740 final Phone[] phones = PhoneFactory.getPhones();
3741 long total = 0;
3742 for (Phone phone : phones) {
3743 total += phone.getVtDataUsage();
3744 }
3745 return total;
3746 }
Jack Yu75ab2952016-07-08 14:29:33 -07003747
3748 /**
3749 * Policy control of data connection. Usually used when data limit is passed.
3750 * @param enabled True if enabling the data, otherwise disabling.
3751 * @param subId Subscription index
3752 * {@hide}
3753 */
3754 @Override
3755 public void setPolicyDataEnabled(boolean enabled, int subId) {
3756 enforceModifyPermission();
3757 Phone phone = getPhone(subId);
3758 if (phone != null) {
3759 phone.setPolicyDataEnabled(enabled);
3760 }
3761 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003762
3763 /**
3764 * Get Client request stats
3765 * @return List of Client Request Stats
3766 * @hide
3767 */
3768 @Override
3769 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3770 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3771 return null;
3772 }
3773
3774 Phone phone = getPhone(subId);
3775 if (phone != null) {
3776 return phone.getClientRequestStats();
3777 }
3778
3779 return null;
3780 }
3781
3782 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3783 if (workSource != null) {
3784 return workSource;
3785 }
3786
3787 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3788 workSource = new WorkSource(uid, packageName);
3789 return workSource;
3790 }
Jack Yueb4124c2017-02-16 15:32:43 -08003791
3792 /**
3793 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3794 *
3795 * @param slotId SIM slot id.
3796 * @param powerUp True if powering up the SIM, otherwise powering down
3797 *
3798 **/
3799 @Override
3800 public void setSimPowerStateForSlot(int slotId, boolean powerUp) {
3801 enforceModifyPermission();
3802 Phone phone = PhoneFactory.getPhone(slotId);
3803 if (phone != null) {
3804 phone.setSimPowerState(powerUp);
3805 }
3806 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003807}