blob: e3fd6f0eb68df1a1afc569a3ccc7f6f45aeb2c14 [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 Yen409ac562017-03-06 16:00:44 -08002029 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002030 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2031 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2032 return null;
2033 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002034 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2035 }
2036
2037 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002038 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2039 VisualVoicemailSmsFilterSettings settings) {
2040 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002041 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002042 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2043 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002044 }
2045
2046 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002047 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2048 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002049 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002050 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002051 }
2052
2053 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002054 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2055 String callingPackage, int subId) {
2056 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002057 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002058 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002059 }
2060
2061 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002062 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002063 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002064 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002065 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2066 }
2067
2068 @Override
2069 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2070 String number, int port, String text, PendingIntent sentIntent) {
2071 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002072 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002073 enforceSendSmsPermission();
2074 // Make the calls as the phone process.
2075 final long identity = Binder.clearCallingIdentity();
2076 try {
2077 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2078 if (port == 0) {
2079 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2080 sentIntent, null, false);
2081 } else {
2082 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2083 smsManager.sendDataMessageWithSelfPermissions(number, null,
2084 (short) port, data, sentIntent, null);
2085 }
2086 } finally {
2087 Binder.restoreCallingIdentity(identity);
2088 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002089 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002090 /**
fionaxu0152e512016-11-14 13:36:14 -08002091 * Sets the voice activation state of a given subId.
2092 */
2093 @Override
2094 public void setVoiceActivationState(int subId, int activationState) {
2095 enforceModifyPermissionOrCarrierPrivilege(subId);
2096 final Phone phone = getPhone(subId);
2097 if (phone != null) {
2098 phone.setVoiceActivationState(activationState);
2099 } else {
2100 loge("setVoiceActivationState fails with invalid subId: " + subId);
2101 }
2102 }
2103
2104 /**
2105 * Sets the data activation state of a given subId.
2106 */
2107 @Override
2108 public void setDataActivationState(int subId, int activationState) {
2109 enforceModifyPermissionOrCarrierPrivilege(subId);
2110 final Phone phone = getPhone(subId);
2111 if (phone != null) {
2112 phone.setDataActivationState(activationState);
2113 } else {
2114 loge("setVoiceActivationState fails with invalid subId: " + subId);
2115 }
2116 }
2117
2118 /**
2119 * Returns the voice activation state of a given subId.
2120 */
2121 @Override
2122 public int getVoiceActivationState(int subId, String callingPackage) {
2123 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2124 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2125 }
2126 final Phone phone = getPhone(subId);
2127 if (phone != null) {
2128 return phone.getVoiceActivationState();
2129 } else {
2130 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2131 }
2132 }
2133
2134 /**
2135 * Returns the data activation state of a given subId.
2136 */
2137 @Override
2138 public int getDataActivationState(int subId, String callingPackage) {
2139 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2140 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2141 }
2142 final Phone phone = getPhone(subId);
2143 if (phone != null) {
2144 return phone.getDataActivationState();
2145 } else {
2146 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2147 }
2148 }
2149
2150 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002151 * Returns the unread count of voicemails
2152 */
2153 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002154 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002155 }
2156
2157 /**
2158 * Returns the unread count of voicemails for a subId
2159 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002160 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002161 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002162 final Phone phone = getPhone(subId);
2163 if (phone != null) {
2164 return phone.getVoiceMessageCount();
2165 } else {
2166 return 0;
2167 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002168 }
2169
2170 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002171 * returns true, if the device is in a state where both voice and data
2172 * are supported simultaneously. This can change based on location or network condition.
2173 */
2174 @Override
2175 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2176 final Phone phone = getPhone(subId);
2177 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2178 }
2179
2180 /**
fionaxue559f972017-01-24 22:31:12 -08002181 * Send the dialer code if called from the current default dialer and the input code follows the
2182 * format of *#*#<code>#*#*
2183 * <p>
2184 * Requires Permission:
2185 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2186 *
2187 * @param inputCode The dialer code to send
2188 * @return true if successfully sent, false otherwise
2189 */
2190 @Override
2191 public boolean sendDialerCode(String callingPackage, String inputCode) {
2192 enforceModifyPermission();
2193 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2194 if (TextUtils.equals(callingPackage,
2195 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2196 final Phone phone = getPhone(getDefaultSubscription());
2197 if (phone != null) {
2198 return phone.sendDialerCode(inputCode);
2199 }
2200 }
2201 return false;
2202 }
2203
2204 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002205 * Returns the data network type.
2206 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002207 *
2208 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2209 */
2210 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002211 public int getNetworkType() {
2212 final Phone phone = getPhone(getDefaultSubscription());
2213 if (phone != null) {
2214 return phone.getServiceState().getDataNetworkType();
2215 } else {
2216 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2217 }
Wink Saville36469e72014-06-11 15:17:00 -07002218 }
2219
2220 /**
2221 * Returns the network type for a subId
2222 */
2223 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002224 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2225 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2226 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2227 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002228
Sanket Padawe356d7632015-06-22 14:03:32 -07002229 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002230 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002231 return phone.getServiceState().getDataNetworkType();
2232 } else {
2233 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2234 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002235 }
2236
2237 /**
2238 * Returns the data network type
2239 */
2240 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002241 public int getDataNetworkType(String callingPackage) {
2242 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002243 }
2244
2245 /**
2246 * Returns the data network type for a subId
2247 */
2248 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002249 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2250 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2251 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2252 }
2253
Sanket Padawe356d7632015-06-22 14:03:32 -07002254 final Phone phone = getPhone(subId);
2255 if (phone != null) {
2256 return phone.getServiceState().getDataNetworkType();
2257 } else {
2258 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002260 }
2261
2262 /**
Wink Saville36469e72014-06-11 15:17:00 -07002263 * Returns the Voice network type for a subId
2264 */
2265 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002266 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2267 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2268 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2269 }
2270
Sanket Padawe356d7632015-06-22 14:03:32 -07002271 final Phone phone = getPhone(subId);
2272 if (phone != null) {
2273 return phone.getServiceState().getVoiceNetworkType();
2274 } else {
2275 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2276 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002277 }
2278
2279 /**
2280 * @return true if a ICC card is present
2281 */
2282 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002283 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002284 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002285 }
2286
2287 /**
2288 * @return true if a ICC card is present for a slotId
2289 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002290 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002291 public boolean hasIccCardUsingSlotId(int slotId) {
fionaxu6e6c68b2016-06-23 13:31:32 -07002292 final Phone phone = PhoneFactory.getPhone(slotId);
2293 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002294 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002295 } else {
2296 return false;
2297 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002298 }
2299
2300 /**
2301 * Return if the current radio is LTE on CDMA. This
2302 * is a tri-state return value as for a period of time
2303 * the mode may be unknown.
2304 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002305 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002306 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002307 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002308 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002309 @Override
2310 public int getLteOnCdmaMode(String callingPackage) {
2311 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002312 }
2313
Sanket Padawe356d7632015-06-22 14:03:32 -07002314 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002315 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2316 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2317 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2318 }
2319
Sanket Padawe356d7632015-06-22 14:03:32 -07002320 final Phone phone = getPhone(subId);
2321 if (phone == null) {
2322 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2323 } else {
2324 return phone.getLteOnCdmaMode();
2325 }
Wink Saville36469e72014-06-11 15:17:00 -07002326 }
2327
2328 public void setPhone(Phone phone) {
2329 mPhone = phone;
2330 }
2331
2332 /**
2333 * {@hide}
2334 * Returns Default subId, 0 in the case of single standby.
2335 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002336 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002337 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002338 }
2339
Shishir Agrawala9f32182016-04-12 12:00:16 -07002340 private int getSlotForDefaultSubscription() {
2341 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2342 }
2343
Wink Savilleb564aae2014-10-23 10:18:09 -07002344 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002345 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002346 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002347
2348 /**
2349 * @see android.telephony.TelephonyManager.WifiCallingChoices
2350 */
2351 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002352 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2353 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002354 }
2355
2356 /**
2357 * @see android.telephony.TelephonyManager.WifiCallingChoices
2358 */
2359 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002360 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2361 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2362 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002363 }
2364
Sailesh Nepald1e68152013-12-12 19:08:02 -08002365 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002366 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002367 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002368 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002369
Shishir Agrawal566b7612013-10-28 14:41:00 -07002370 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002371 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2372 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002373
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002374 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002375 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002376 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002377 if (DBG) log("iccOpenLogicalChannel: " + response);
2378 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002379 }
2380
2381 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002382 public boolean iccCloseLogicalChannel(int subId, int channel) {
2383 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002384
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002385 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002386 if (channel < 0) {
2387 return false;
2388 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002389 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002390 if (DBG) log("iccCloseLogicalChannel: " + success);
2391 return success;
2392 }
2393
2394 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002395 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002396 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002397 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002398
2399 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002400 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2401 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002402 " data=" + data);
2403 }
2404
2405 if (channel < 0) {
2406 return "";
2407 }
2408
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002409 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002410 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002411 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2412
Shishir Agrawal566b7612013-10-28 14:41:00 -07002413 // Append the returned status code to the end of the response payload.
2414 String s = Integer.toHexString(
2415 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002416 if (response.payload != null) {
2417 s = IccUtils.bytesToHexString(response.payload) + s;
2418 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002419 return s;
2420 }
Jake Hambye994d462014-02-03 13:10:13 -08002421
Evan Charltonc66da362014-05-16 14:06:40 -07002422 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002423 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002424 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002425 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002426
2427 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002428 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2429 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002430 }
2431
2432 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002433 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002434 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2435
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002436 // Append the returned status code to the end of the response payload.
2437 String s = Integer.toHexString(
2438 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002439 if (response.payload != null) {
2440 s = IccUtils.bytesToHexString(response.payload) + s;
2441 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002442 return s;
2443 }
2444
2445 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002446 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002447 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002448 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002449
2450 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002451 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002452 p1 + " " + p2 + " " + p3 + ":" + filePath);
2453 }
2454
2455 IccIoResult response =
2456 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002457 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2458 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002459
2460 if (DBG) {
2461 log("Exchange SIM_IO [R]" + response);
2462 }
2463
2464 byte[] result = null;
2465 int length = 2;
2466 if (response.payload != null) {
2467 length = 2 + response.payload.length;
2468 result = new byte[length];
2469 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2470 } else {
2471 result = new byte[length];
2472 }
2473
2474 result[length - 1] = (byte) response.sw2;
2475 result[length - 2] = (byte) response.sw1;
2476 return result;
2477 }
2478
Nathan Haroldb3014052017-01-25 15:57:32 -08002479 /**
2480 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2481 * on a particular subscription
2482 */
2483 public String[] getForbiddenPlmns(int subId, int appType) {
2484 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
2485 "Requires READ_PRIVILEGED_PHONE_STATE");
2486 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2487 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2488 return null;
2489 }
2490 Object response = sendRequest(
2491 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2492 if (response instanceof String[]) {
2493 return (String[]) response;
2494 }
2495 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2496 return null;
2497 }
2498
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002499 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002500 public String sendEnvelopeWithStatus(int subId, String content) {
2501 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002502
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002503 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002504 if (response.payload == null) {
2505 return "";
2506 }
2507
2508 // Append the returned status code to the end of the response payload.
2509 String s = Integer.toHexString(
2510 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2511 s = IccUtils.bytesToHexString(response.payload) + s;
2512 return s;
2513 }
2514
Jake Hambye994d462014-02-03 13:10:13 -08002515 /**
2516 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2517 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2518 *
2519 * @param itemID the ID of the item to read
2520 * @return the NV item as a String, or null on error.
2521 */
2522 @Override
2523 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002524 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002525 if (DBG) log("nvReadItem: item " + itemID);
2526 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2527 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2528 return value;
2529 }
2530
2531 /**
2532 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2533 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2534 *
2535 * @param itemID the ID of the item to read
2536 * @param itemValue the value to write, as a String
2537 * @return true on success; false on any failure
2538 */
2539 @Override
2540 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002541 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002542 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2543 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2544 new Pair<Integer, String>(itemID, itemValue));
2545 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2546 return success;
2547 }
2548
2549 /**
2550 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2551 * Used for device configuration by some CDMA operators.
2552 *
2553 * @param preferredRoamingList byte array containing the new PRL
2554 * @return true on success; false on any failure
2555 */
2556 @Override
2557 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002558 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002559 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2560 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2561 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2562 return success;
2563 }
2564
2565 /**
2566 * Perform the specified type of NV config reset.
2567 * Used for device configuration by some CDMA operators.
2568 *
2569 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2570 * @return true on success; false on any failure
2571 */
2572 @Override
2573 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002574 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002575 if (DBG) log("nvResetConfig: type " + resetType);
2576 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2577 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2578 return success;
2579 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002580
2581 /**
Wink Saville36469e72014-06-11 15:17:00 -07002582 * {@hide}
2583 * Returns Default sim, 0 in the case of single standby.
2584 */
2585 public int getDefaultSim() {
2586 //TODO Need to get it from Telephony Devcontroller
2587 return 0;
2588 }
2589
Svet Ganovb320e182015-04-16 12:30:10 -07002590 public String[] getPcscfAddress(String apnType, String callingPackage) {
2591 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2592 return new String[0];
2593 }
2594
2595
ram87fca6f2014-07-18 18:58:44 +05302596 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002597 }
2598
Brad Ebinger51f743a2017-01-23 13:50:20 -08002599 /**
2600 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2601 * feature or {@link null} if the service is not available. If an ImsServiceController is
2602 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2603 * feature updates.
2604 */
2605 public IImsServiceController getImsServiceControllerAndListen(int slotId, int feature,
2606 IImsServiceFeatureListener callback) {
2607 enforceModifyPermission();
2608 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotId, feature,
2609 callback);
2610 }
2611
Wink Saville36469e72014-06-11 15:17:00 -07002612 public void setImsRegistrationState(boolean registered) {
2613 enforceModifyPermission();
2614 mPhone.setImsRegistrationState(registered);
2615 }
2616
2617 /**
Stuart Scott54788802015-03-30 13:18:01 -07002618 * Set the network selection mode to automatic.
2619 *
2620 */
2621 @Override
2622 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002623 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002624 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2625 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2626 }
2627
2628 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002629 * Set the network selection mode to manual with the selected carrier.
2630 */
2631 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002632 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2633 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002634 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002635 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002636 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2637 persistSelection);
2638 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002639 }
2640
2641 /**
2642 * Scans for available networks.
2643 */
2644 @Override
2645 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002646 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002647 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2648 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2649 CMD_PERFORM_NETWORK_SCAN, null, subId);
2650 return result;
2651 }
2652
2653 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002654 * Get the calculated preferred network type.
2655 * Used for debugging incorrect network type.
2656 *
2657 * @return the preferred network type, defined in RILConstants.java.
2658 */
2659 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002660 public int getCalculatedPreferredNetworkType(String callingPackage) {
2661 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2662 return RILConstants.PREFERRED_NETWORK_MODE;
2663 }
2664
Amit Mahajan43330e02014-11-18 11:54:45 -08002665 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002666 }
2667
2668 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002669 * Get the preferred network type.
2670 * Used for device configuration by some CDMA operators.
2671 *
2672 * @return the preferred network type, defined in RILConstants.java.
2673 */
2674 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002675 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002676 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002677 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002678 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002679 int networkType = (result != null ? result[0] : -1);
2680 if (DBG) log("getPreferredNetworkType: " + networkType);
2681 return networkType;
2682 }
2683
2684 /**
2685 * Set the preferred network type.
2686 * Used for device configuration by some CDMA operators.
2687 *
2688 * @param networkType the preferred network type, defined in RILConstants.java.
2689 * @return true on success; false on any failure.
2690 */
2691 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002692 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002693 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002694 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2695 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002696 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002697 if (success) {
2698 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002699 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002700 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002701 return success;
2702 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002703
2704 /**
Junda Liu475951f2014-11-07 16:45:03 -08002705 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2706 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2707 * tethering.
2708 *
2709 * @return 0: Not required. 1: required. 2: Not set.
2710 * @hide
2711 */
2712 @Override
2713 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002714 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002715 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2716 Settings.Global.TETHER_DUN_REQUIRED, 2);
2717 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2718 // config_tether_apndata.
2719 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2720 dunRequired = 1;
2721 }
2722 return dunRequired;
2723 }
2724
2725 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002726 * Set mobile data enabled
2727 * Used by the user through settings etc to turn on/off mobile data
2728 *
2729 * @param enable {@code true} turn turn data on, else {@code false}
2730 */
2731 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002732 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002733 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002734 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002735 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002736 Phone phone = PhoneFactory.getPhone(phoneId);
2737 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002738 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002739 phone.setDataEnabled(enable);
2740 } else {
2741 loge("setDataEnabled: no phone for subId=" + subId);
2742 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002743 }
2744
2745 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002746 * Get whether mobile data is enabled.
2747 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002748 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002749 *
2750 * @return {@code true} if data is enabled else {@code false}
2751 */
2752 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002753 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002754 try {
2755 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2756 null);
2757 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002758 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002759 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002760 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002761 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002762 Phone phone = PhoneFactory.getPhone(phoneId);
2763 if (phone != null) {
2764 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002765 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002766 return retVal;
2767 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002768 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002769 return false;
2770 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002771 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002772
2773 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002774 public int getCarrierPrivilegeStatus(int subId) {
2775 final Phone phone = getPhone(subId);
2776 if (phone == null) {
2777 loge("getCarrierPrivilegeStatus: Invalid subId");
2778 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2779 }
2780 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002781 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002782 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002783 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2784 }
2785 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002786 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002787 }
Junda Liu29340342014-07-10 15:23:27 -07002788
2789 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002790 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002791 if (TextUtils.isEmpty(pkgName))
2792 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002793 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002794 if (card == null) {
2795 loge("checkCarrierPrivilegesForPackage: No UICC");
2796 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2797 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002798 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2799 }
2800
2801 @Override
2802 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002803 if (TextUtils.isEmpty(pkgName))
2804 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002805 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2806 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2807 UiccCard card = UiccController.getInstance().getUiccCard(i);
2808 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002809 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002810 continue;
2811 }
2812
2813 result = card.getCarrierPrivilegeStatus(
2814 mPhone.getContext().getPackageManager(), pkgName);
2815 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2816 break;
2817 }
2818 }
2819
2820 return result;
Junda Liu29340342014-07-10 15:23:27 -07002821 }
Derek Tan89e89d42014-07-08 17:00:10 -07002822
2823 @Override
Junda Liue64de782015-04-16 17:19:16 -07002824 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2825 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2826 loge("phoneId " + phoneId + " is not valid.");
2827 return null;
2828 }
2829 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002830 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002831 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002832 return null ;
2833 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002834 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002835 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002836 }
2837
Amith Yamasani6e118872016-02-19 12:53:51 -08002838 @Override
2839 public List<String> getPackagesWithCarrierPrivileges() {
2840 PackageManager pm = mPhone.getContext().getPackageManager();
2841 List<String> privilegedPackages = new ArrayList<>();
2842 List<PackageInfo> packages = null;
2843 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2844 UiccCard card = UiccController.getInstance().getUiccCard(i);
2845 if (card == null) {
2846 // No UICC in that slot.
2847 continue;
2848 }
2849 if (card.hasCarrierPrivilegeRules()) {
2850 if (packages == null) {
2851 // Only check packages in user 0 for now
2852 packages = pm.getInstalledPackagesAsUser(
2853 PackageManager.MATCH_DISABLED_COMPONENTS
2854 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2855 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2856 }
2857 for (int p = packages.size() - 1; p >= 0; p--) {
2858 PackageInfo pkgInfo = packages.get(p);
2859 if (pkgInfo != null && pkgInfo.packageName != null
2860 && card.getCarrierPrivilegeStatus(pkgInfo)
2861 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2862 privilegedPackages.add(pkgInfo.packageName);
2863 }
2864 }
2865 }
2866 }
2867 return privilegedPackages;
2868 }
2869
Wink Savilleb564aae2014-10-23 10:18:09 -07002870 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002871 final Phone phone = getPhone(subId);
2872 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002873 if (card == null) {
2874 loge("getIccId: No UICC");
2875 return null;
2876 }
2877 String iccId = card.getIccId();
2878 if (TextUtils.isEmpty(iccId)) {
2879 loge("getIccId: ICC ID is null or empty.");
2880 return null;
2881 }
2882 return iccId;
2883 }
2884
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002885 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002886 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2887 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002888 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002889
Jeff Sharkey85190e62014-12-05 09:40:12 -08002890 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002891 final Phone phone = getPhone(subId);
2892 if (phone == null) {
2893 return false;
2894 }
2895 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002896
2897 if (DBG_MERGE) {
2898 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2899 + subscriberId + " to " + number);
2900 }
2901
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002902 if (TextUtils.isEmpty(iccId)) {
2903 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002904 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002905
Jeff Sharkey85190e62014-12-05 09:40:12 -08002906 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2907
2908 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002909 if (alphaTag == null) {
2910 editor.remove(alphaTagPrefKey);
2911 } else {
2912 editor.putString(alphaTagPrefKey, alphaTag);
2913 }
2914
Jeff Sharkey85190e62014-12-05 09:40:12 -08002915 // Record both the line number and IMSI for this ICCID, since we need to
2916 // track all merged IMSIs based on line number
2917 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2918 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002919 if (number == null) {
2920 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002921 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002922 } else {
2923 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002924 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002925 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002926
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002927 editor.commit();
2928 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002929 }
2930
2931 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002932 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002933 // This is open to apps with WRITE_SMS.
2934 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002935 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002936 return null;
2937 }
Derek Tan97ebb422014-09-05 16:55:38 -07002938
2939 String iccId = getIccId(subId);
2940 if (iccId != null) {
2941 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002942 if (DBG_MERGE) {
2943 log("getLine1NumberForDisplay returning " +
2944 mTelephonySharedPreferences.getString(numberPrefKey, null));
2945 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002946 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002947 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002948 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002949 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002950 }
2951
2952 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002953 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2954 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2955 return null;
2956 }
Derek Tan97ebb422014-09-05 16:55:38 -07002957
2958 String iccId = getIccId(subId);
2959 if (iccId != null) {
2960 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002961 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002962 }
Derek Tan97ebb422014-09-05 16:55:38 -07002963 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002964 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002965
2966 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002967 public String[] getMergedSubscriberIds(String callingPackage) {
2968 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2969 return null;
2970 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002971 final Context context = mPhone.getContext();
2972 final TelephonyManager tele = TelephonyManager.from(context);
2973 final SubscriptionManager sub = SubscriptionManager.from(context);
2974
2975 // Figure out what subscribers are currently active
2976 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002977 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2978 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2979 final long identity = Binder.clearCallingIdentity();
2980 try {
2981 final int[] subIds = sub.getActiveSubscriptionIdList();
2982 for (int subId : subIds) {
2983 activeSubscriberIds.add(tele.getSubscriberId(subId));
2984 }
2985 } finally {
2986 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002987 }
2988
2989 // First pass, find a number override for an active subscriber
2990 String mergeNumber = null;
2991 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2992 for (String key : prefs.keySet()) {
2993 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2994 final String subscriberId = (String) prefs.get(key);
2995 if (activeSubscriberIds.contains(subscriberId)) {
2996 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2997 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2998 mergeNumber = (String) prefs.get(numberKey);
2999 if (DBG_MERGE) {
3000 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3001 + " for active subscriber " + subscriberId);
3002 }
3003 if (!TextUtils.isEmpty(mergeNumber)) {
3004 break;
3005 }
3006 }
3007 }
3008 }
3009
3010 // Shortcut when no active merged subscribers
3011 if (TextUtils.isEmpty(mergeNumber)) {
3012 return null;
3013 }
3014
3015 // Second pass, find all subscribers under that line override
3016 final ArraySet<String> result = new ArraySet<>();
3017 for (String key : prefs.keySet()) {
3018 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3019 final String number = (String) prefs.get(key);
3020 if (mergeNumber.equals(number)) {
3021 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3022 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3023 final String subscriberId = (String) prefs.get(subscriberKey);
3024 if (!TextUtils.isEmpty(subscriberId)) {
3025 result.add(subscriberId);
3026 }
3027 }
3028 }
3029 }
3030
3031 final String[] resultArray = result.toArray(new String[result.size()]);
3032 Arrays.sort(resultArray);
3033 if (DBG_MERGE) {
3034 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3035 }
3036 return resultArray;
3037 }
3038
3039 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003040 public boolean setOperatorBrandOverride(int subId, String brand) {
3041 enforceCarrierPrivilege(subId);
3042 final Phone phone = getPhone(subId);
3043 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003044 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003045
3046 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003047 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003048 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3049 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003050 enforceCarrierPrivilege(subId);
3051 final Phone phone = getPhone(subId);
3052 if (phone == null) {
3053 return false;
3054 }
3055 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003056 cdmaNonRoamingList);
3057 }
3058
3059 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003060 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003061 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3062 enforceModifyPermission();
3063
3064 int returnValue = 0;
3065 try {
3066 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3067 if(result.exception == null) {
3068 if (result.result != null) {
3069 byte[] responseData = (byte[])(result.result);
3070 if(responseData.length > oemResp.length) {
3071 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3072 responseData.length + "bytes. Buffer Size is " +
3073 oemResp.length + "bytes.");
3074 }
3075 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3076 returnValue = responseData.length;
3077 }
3078 } else {
3079 CommandException ex = (CommandException) result.exception;
3080 returnValue = ex.getCommandError().ordinal();
3081 if(returnValue > 0) returnValue *= -1;
3082 }
3083 } catch (RuntimeException e) {
3084 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3085 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3086 if(returnValue > 0) returnValue *= -1;
3087 }
3088
3089 return returnValue;
3090 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003091
3092 @Override
3093 public void setRadioCapability(RadioAccessFamily[] rafs) {
3094 try {
3095 ProxyController.getInstance().setRadioCapability(rafs);
3096 } catch (RuntimeException e) {
3097 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3098 }
3099 }
3100
3101 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003102 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3103 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3104 return RadioAccessFamily.RAF_UNKNOWN;
3105 }
3106
Wink Saville5d475dd2014-10-17 15:00:58 -07003107 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3108 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003109
3110 @Override
3111 public void enableVideoCalling(boolean enable) {
3112 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003113 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003114 }
3115
3116 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003117 public boolean isVideoCallingEnabled(String callingPackage) {
3118 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3119 return false;
3120 }
3121
Andrew Lee77527ac2014-10-21 16:57:39 -07003122 // Check the user preference and the system-level IMS setting. Even if the user has
3123 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3124 // In the long run, we may instead need to check if there exists a connection service
3125 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003126 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3127 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003128 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003129 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003130
Andrew Leea1239f22015-03-02 17:44:07 -08003131 @Override
3132 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003133 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003134 }
3135
3136 @Override
3137 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003138 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003139 }
3140
Andrew Lee9431b832015-03-09 18:46:45 -07003141 @Override
3142 public boolean isTtyModeSupported() {
3143 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3144 TelephonyManager telephonyManager =
3145 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003146 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003147 }
3148
3149 @Override
3150 public boolean isHearingAidCompatibilitySupported() {
3151 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3152 }
3153
Sanket Padawe7310cc72015-01-14 09:53:20 -08003154 /**
3155 * Returns the unique device ID of phone, for example, the IMEI for
3156 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3157 *
3158 * <p>Requires Permission:
3159 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3160 */
3161 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003162 public String getDeviceId(String callingPackage) {
3163 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3164 return null;
3165 }
3166
Sanket Padawe7310cc72015-01-14 09:53:20 -08003167 final Phone phone = PhoneFactory.getPhone(0);
3168 if (phone != null) {
3169 return phone.getDeviceId();
3170 } else {
3171 return null;
3172 }
3173 }
3174
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003175 /*
3176 * {@hide}
3177 * Returns the IMS Registration Status
3178 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003179 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003180 public boolean isImsRegistered() {
3181 return mPhone.isImsRegistered();
3182 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003183
3184 @Override
3185 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3186 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3187 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003188
Nathan Haroldc55097a2015-03-11 18:14:50 -07003189 /*
3190 * {@hide}
3191 * Returns the IMS Registration Status
3192 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003193 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003194 return mPhone.isWifiCallingEnabled();
3195 }
3196
3197 /*
3198 * {@hide}
3199 * Returns the IMS Registration Status
3200 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003201 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003202 return mPhone.isVolteEnabled();
3203 }
Svet Ganovb320e182015-04-16 12:30:10 -07003204
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003205 /*
3206 * {@hide} Returns the IMS Registration Status
3207 */
3208 public boolean isVideoTelephonyAvailable() {
3209 return mPhone.isVideoEnabled();
3210 }
3211
Svet Ganovb320e182015-04-16 12:30:10 -07003212 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003213 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003214 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003215 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3216
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003217 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003218 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003219 } catch (SecurityException e) {
3220 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3221 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003222 }
Svet Ganovb320e182015-04-16 12:30:10 -07003223
3224 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3225 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3226 return false;
3227 }
3228
3229 return true;
3230 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003231
Makoto Onukifee69342015-06-29 14:44:50 -07003232 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003233 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003234 */
3235 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003236 // Default SMS app can always read it.
3237 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3238 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3239 return true;
3240 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003241
Makoto Onukie4072d12015-08-03 15:12:23 -07003242 try {
3243 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003244 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003245 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003246 // Can be read with READ_SMS too.
3247 try {
3248 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3249 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3250 if (opCode != AppOpsManager.OP_NONE) {
3251 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3252 == AppOpsManager.MODE_ALLOWED;
3253 } else {
3254 return true;
3255 }
3256 } catch (SecurityException readSmsSecurityException) {
3257 }
3258 // Can be read with READ_PHONE_NUMBER too.
3259 try {
3260 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBER,
3261 message);
3262 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBER);
3263 if (opCode != AppOpsManager.OP_NONE) {
3264 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3265 == AppOpsManager.MODE_ALLOWED;
3266 } else {
3267 return true;
3268 }
3269 } catch (SecurityException readPhoneNumberSecurityException) {
3270 }
3271
3272 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3273 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3274 ", " + android.Manifest.permission.READ_SMS + ", or " +
3275 android.Manifest.permission.READ_PHONE_NUMBER);
Makoto Onukifee69342015-06-29 14:44:50 -07003276 }
3277
Stuart Scott8eef64f2015-04-08 15:13:54 -07003278 @Override
3279 public void factoryReset(int subId) {
3280 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003281 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3282 return;
3283 }
3284
Svet Ganovcc087f82015-05-12 20:35:54 -07003285 final long identity = Binder.clearCallingIdentity();
3286 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003287 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3288 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003289 // Enable data
3290 setDataEnabled(subId, true);
3291 // Set network selection mode to automatic
3292 setNetworkSelectionModeAutomatic(subId);
3293 // Set preferred mobile network type to the best available
3294 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3295 // Turn off roaming
3296 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3297 }
3298 } finally {
3299 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003300 }
3301 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003302
3303 @Override
3304 public String getLocaleFromDefaultSim() {
3305 // We query all subscriptions instead of just the active ones, because
3306 // this might be called early on in the provisioning flow when the
3307 // subscriptions potentially aren't active yet.
3308 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3309 if (slist == null || slist.isEmpty()) {
3310 return null;
3311 }
3312
3313 // This function may be called very early, say, from the setup wizard, at
3314 // which point we won't have a default subscription set. If that's the case
3315 // we just choose the first, which will be valid in "most cases".
3316 final int defaultSubId = getDefaultSubscription();
3317 SubscriptionInfo info = null;
3318 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3319 info = slist.get(0);
3320 } else {
3321 for (SubscriptionInfo item : slist) {
3322 if (item.getSubscriptionId() == defaultSubId) {
3323 info = item;
3324 break;
3325 }
3326 }
3327
3328 if (info == null) {
3329 return null;
3330 }
3331 }
3332
3333 // Try and fetch the locale from the carrier properties or from the SIM language
3334 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003335 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003336 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003337 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003338 if (defaultPhone != null) {
3339 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3340 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003341 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003342 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3343 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003344 } else {
3345 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003346 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003347 }
3348 }
3349
Narayan Kamath011676f2015-07-29 12:04:08 +01003350 // The SIM language preferences only store a language (e.g. fr = French), not an
3351 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3352 // the SIM and carrier preferences does not include a country we add the country
3353 // determined from the SIM MCC to provide an exact locale.
3354 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003355 if (mccLocale != null) {
3356 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3357 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003358 }
3359
Tony Hill183b2de2015-06-24 14:53:58 +01003360 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003361 return null;
3362 }
3363
3364 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3365 final long identity = Binder.clearCallingIdentity();
3366 try {
3367 return mSubscriptionController.getAllSubInfoList(
3368 mPhone.getContext().getOpPackageName());
3369 } finally {
3370 Binder.restoreCallingIdentity(identity);
3371 }
3372 }
3373
3374 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3375 final long identity = Binder.clearCallingIdentity();
3376 try {
3377 return mSubscriptionController.getActiveSubscriptionInfoList(
3378 mPhone.getContext().getOpPackageName());
3379 } finally {
3380 Binder.restoreCallingIdentity(identity);
3381 }
3382 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003383
3384 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003385 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3386 * representing the state of the modem.
3387 *
3388 * NOTE: This clears the modem state, so there should only every be one caller.
3389 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003390 */
3391 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003392 public void requestModemActivityInfo(ResultReceiver result) {
3393 enforceModifyPermission();
3394
3395 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3396 Bundle bundle = new Bundle();
3397 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3398 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003399 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003400
3401 /**
3402 * {@hide}
3403 * Returns the service state information on specified subscription.
3404 */
3405 @Override
3406 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3407
3408 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3409 return null;
3410 }
3411
3412 final Phone phone = getPhone(subId);
3413 if (phone == null) {
3414 return null;
3415 }
3416
3417 return phone.getServiceState();
3418 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003419
3420 /**
3421 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3422 *
3423 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3424 * voicemail ringtone.
3425 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3426 * PhoneAccount.
3427 */
3428 @Override
3429 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3430 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3431 if (phone == null) {
3432 return null;
3433 }
3434
3435 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3436 }
3437
3438 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003439 * Sets the per-account voicemail ringtone.
3440 *
3441 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3442 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3443 *
3444 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3445 * voicemail ringtone.
3446 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3447 * PhoneAccount.
3448 */
3449 @Override
3450 public void setVoicemailRingtoneUri(String callingPackage,
3451 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3452 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3453 if (!TextUtils.equals(callingPackage,
3454 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3455 enforceModifyPermissionOrCarrierPrivilege(
3456 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3457 }
3458 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3459 if (phone == null){
3460 throw new IllegalArgumentException("The phoneAccountHandle does not correspond to an "
3461 + "active phone account.");
3462 }
3463 VoicemailNotificationSettingsUtil.setRingtoneUri(phone, uri);
3464 }
3465
3466 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003467 * Returns whether vibration is set for voicemail notification in Phone settings.
3468 *
3469 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3470 * voicemail vibration setting.
3471 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3472 */
3473 @Override
3474 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3475 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3476 if (phone == null) {
3477 return false;
3478 }
3479
3480 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3481 }
3482
Youhan Wange64578a2016-05-02 15:32:42 -07003483 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003484 * Sets the per-account voicemail vibration.
3485 *
3486 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3487 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3488 *
3489 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3490 * voicemail vibration setting.
3491 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3492 * specific PhoneAccount.
3493 */
3494 @Override
3495 public void setVoicemailVibrationEnabled(String callingPackage,
3496 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3497 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3498 if (!TextUtils.equals(callingPackage,
3499 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3500 enforceModifyPermissionOrCarrierPrivilege(
3501 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3502 }
3503
3504 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3505 if (phone == null){
3506 throw new IllegalArgumentException("The phoneAccountHandle does not correspond to an "
3507 + "active phone account.");
3508 }
3509 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone, enabled);
3510 }
3511
3512 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003513 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3514 *
3515 * @throws SecurityException if the caller does not have the required permission
3516 */
3517 private void enforceReadPrivilegedPermission() {
3518 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3519 null);
3520 }
3521
3522 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003523 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3524 * permission.
3525 *
3526 * @throws SecurityException if the caller does not have the required permission
3527 */
3528 private void enforceSendSmsPermission() {
3529 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3530 }
3531
3532 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003533 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003534 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003535 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003536 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003537 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
3538 String vvmPackage = RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId)
3539 .getPackageName();
3540 if (!callingPackage.equals(vvmPackage)) {
3541 throw new SecurityException("Caller not current active visual voicemail package[" +
3542 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003543 }
3544 }
3545
3546 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003547 * Return the application ID for the app type.
3548 *
3549 * @param subId the subscription ID that this request applies to.
3550 * @param appType the uicc app type.
3551 * @return Application ID for specificied app type, or null if no uicc.
3552 */
3553 @Override
3554 public String getAidForAppType(int subId, int appType) {
3555 enforceReadPrivilegedPermission();
3556 Phone phone = getPhone(subId);
3557 if (phone == null) {
3558 return null;
3559 }
3560 String aid = null;
3561 try {
3562 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3563 .getApplicationByType(appType).getAid();
3564 } catch (Exception e) {
3565 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3566 }
3567 return aid;
3568 }
3569
Youhan Wang4001d252016-05-11 10:29:41 -07003570 /**
3571 * Return the Electronic Serial Number.
3572 *
3573 * @param subId the subscription ID that this request applies to.
3574 * @return ESN or null if error.
3575 */
3576 @Override
3577 public String getEsn(int subId) {
3578 enforceReadPrivilegedPermission();
3579 Phone phone = getPhone(subId);
3580 if (phone == null) {
3581 return null;
3582 }
3583 String esn = null;
3584 try {
3585 esn = phone.getEsn();
3586 } catch (Exception e) {
3587 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3588 }
3589 return esn;
3590 }
3591
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003592 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003593 * Return the Preferred Roaming List Version.
3594 *
3595 * @param subId the subscription ID that this request applies to.
3596 * @return PRLVersion or null if error.
3597 */
3598 @Override
3599 public String getCdmaPrlVersion(int subId) {
3600 enforceReadPrivilegedPermission();
3601 Phone phone = getPhone(subId);
3602 if (phone == null) {
3603 return null;
3604 }
3605 String cdmaPrlVersion = null;
3606 try {
3607 cdmaPrlVersion = phone.getCdmaPrlVersion();
3608 } catch (Exception e) {
3609 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3610 }
3611 return cdmaPrlVersion;
3612 }
3613
3614 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003615 * Get snapshot of Telephony histograms
3616 * @return List of Telephony histograms
3617 * @hide
3618 */
3619 @Override
3620 public List<TelephonyHistogram> getTelephonyHistograms() {
3621 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3622 return RIL.getTelephonyRILTimingHistograms();
3623 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003624
3625 /**
3626 * {@hide}
3627 * Set the allowed carrier list for slotId
3628 * Require system privileges. In the future we may add this to carrier APIs.
3629 *
3630 * @return The number of carriers set successfully, should match length of carriers
3631 */
3632 @Override
3633 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3634 enforceModifyPermission();
3635 int subId = SubscriptionManager.getSubId(slotId)[0];
Meng Wang9b7c4e92017-02-17 11:41:27 -08003636 if (carriers == null) {
3637 throw new NullPointerException("carriers cannot be null");
3638 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003639 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3640 return retVal[0];
3641 }
3642
3643 /**
3644 * {@hide}
3645 * Get the allowed carrier list for slotId.
3646 * Require system privileges. In the future we may add this to carrier APIs.
3647 *
3648 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3649 * means all carriers are allowed.
3650 */
3651 @Override
3652 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3653 enforceReadPrivilegedPermission();
3654 int subId = SubscriptionManager.getSubId(slotId)[0];
3655 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3656 }
3657
fionaxu59545b42016-05-25 15:53:37 -07003658 /**
3659 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3660 * @param subId the subscription ID that this action applies to.
3661 * @param enabled control enable or disable metered apns.
3662 * {@hide}
3663 */
3664 @Override
3665 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3666 enforceModifyPermission();
3667 final Phone phone = getPhone(subId);
3668 if (phone == null) {
3669 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3670 return;
3671 }
3672 try {
3673 phone.carrierActionSetMeteredApnsEnabled(enabled);
3674 } catch (Exception e) {
3675 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3676 }
3677 }
3678
3679 /**
3680 * Action set from carrier signalling broadcast receivers to enable/disable radio
3681 * @param subId the subscription ID that this action applies to.
3682 * @param enabled control enable or disable radio.
3683 * {@hide}
3684 */
3685 @Override
3686 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3687 enforceModifyPermission();
3688 final Phone phone = getPhone(subId);
3689 if (phone == null) {
3690 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3691 return;
3692 }
3693 try {
3694 phone.carrierActionSetRadioEnabled(enabled);
3695 } catch (Exception e) {
3696 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3697 }
3698 }
3699
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003700 /**
3701 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3702 * bug report is being generated.
3703 */
3704 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003705 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003706 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3707 != PackageManager.PERMISSION_GRANTED) {
3708 writer.println("Permission Denial: can't dump Phone from pid="
3709 + Binder.getCallingPid()
3710 + ", uid=" + Binder.getCallingUid()
3711 + "without permission "
3712 + android.Manifest.permission.DUMP);
3713 return;
3714 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003715 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003716 }
Jack Yueb89b242016-06-22 13:27:47 -07003717
3718 /**
3719 * Get aggregated video call data usage from all subscriptions since boot.
3720 * @return total data usage in bytes
3721 * {@hide}
3722 */
3723 @Override
3724 public long getVtDataUsage() {
3725 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3726 null);
3727
3728 // NetworkStatsService keeps tracking the active network interface and identity. It will
3729 // record the delta with the corresponding network identity. What we need to do here is
3730 // returning total video call data usage from all subscriptions since boot.
3731
3732 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3733 // simultaneous VT calls.
3734 final Phone[] phones = PhoneFactory.getPhones();
3735 long total = 0;
3736 for (Phone phone : phones) {
3737 total += phone.getVtDataUsage();
3738 }
3739 return total;
3740 }
Jack Yu75ab2952016-07-08 14:29:33 -07003741
3742 /**
3743 * Policy control of data connection. Usually used when data limit is passed.
3744 * @param enabled True if enabling the data, otherwise disabling.
3745 * @param subId Subscription index
3746 * {@hide}
3747 */
3748 @Override
3749 public void setPolicyDataEnabled(boolean enabled, int subId) {
3750 enforceModifyPermission();
3751 Phone phone = getPhone(subId);
3752 if (phone != null) {
3753 phone.setPolicyDataEnabled(enabled);
3754 }
3755 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003756
3757 /**
3758 * Get Client request stats
3759 * @return List of Client Request Stats
3760 * @hide
3761 */
3762 @Override
3763 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3764 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3765 return null;
3766 }
3767
3768 Phone phone = getPhone(subId);
3769 if (phone != null) {
3770 return phone.getClientRequestStats();
3771 }
3772
3773 return null;
3774 }
3775
3776 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3777 if (workSource != null) {
3778 return workSource;
3779 }
3780
3781 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3782 workSource = new WorkSource(uid, packageName);
3783 return workSource;
3784 }
Jack Yueb4124c2017-02-16 15:32:43 -08003785
3786 /**
3787 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3788 *
3789 * @param slotId SIM slot id.
3790 * @param powerUp True if powering up the SIM, otherwise powering down
3791 *
3792 **/
3793 @Override
3794 public void setSimPowerStateForSlot(int slotId, boolean powerUp) {
3795 enforceModifyPermission();
3796 Phone phone = PhoneFactory.getPhone(slotId);
3797 if (phone != null) {
3798 phone.setSimPowerState(powerUp);
3799 }
3800 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003801}