blob: 27e365f81b3724da0d3b20d0fd6c5b4c05deb279 [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;
Nathan Harolda667c152016-12-14 11:27:20 -080083import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070084import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070085import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070086import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070087import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070088import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080089import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070090import com.android.internal.telephony.uicc.IccIoResult;
91import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080092import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070093import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080094import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070095import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080096import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070097import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080098import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yen527a9c02017-01-06 15:29:25 -080099import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800100
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700101import java.io.FileDescriptor;
102import java.io.PrintWriter;
Ta-wei Yen30a69c82016-12-27 14:52:32 -0800103import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800105import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800106import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100107import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800108import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700109
110/**
111 * Implementation of the ITelephony interface.
112 */
Santos Cordon117fee72014-05-16 17:56:12 -0700113public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700114 private static final String LOG_TAG = "PhoneInterfaceManager";
115 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
116 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800117 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118
119 // Message codes used with mMainThreadHandler
120 private static final int CMD_HANDLE_PIN_MMI = 1;
121 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
122 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
123 private static final int CMD_ANSWER_RINGING_CALL = 4;
124 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700125 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
126 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700127 private static final int CMD_OPEN_CHANNEL = 9;
128 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
129 private static final int CMD_CLOSE_CHANNEL = 11;
130 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800131 private static final int CMD_NV_READ_ITEM = 13;
132 private static final int EVENT_NV_READ_ITEM_DONE = 14;
133 private static final int CMD_NV_WRITE_ITEM = 15;
134 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
135 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
136 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
137 private static final int CMD_NV_RESET_CONFIG = 19;
138 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800139 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
140 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
141 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
142 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800143 private static final int CMD_SEND_ENVELOPE = 25;
144 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700145 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
146 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
147 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
148 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
149 private static final int CMD_EXCHANGE_SIM_IO = 31;
150 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800151 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
152 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700153 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
154 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700155 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
156 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700157 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
158 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
159 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
160 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700161 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
162 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
163 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
164 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar32d516d2016-10-14 19:37:38 -0700165 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800166 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
167 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700168
169 /** The singleton instance. */
170 private static PhoneInterfaceManager sInstance;
171
Wink Saville3ab207e2014-11-20 13:07:20 -0800172 private PhoneGlobals mApp;
173 private Phone mPhone;
174 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700175 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800176 private AppOpsManager mAppOps;
177 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800178 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800179 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700180
Derek Tan97ebb422014-09-05 16:55:38 -0700181 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
182 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800183 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700184
185 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700186 * A request object to use for transmitting data to an ICC.
187 */
188 private static final class IccAPDUArgument {
189 public int channel, cla, command, p1, p2, p3;
190 public String data;
191
192 public IccAPDUArgument(int channel, int cla, int command,
193 int p1, int p2, int p3, String data) {
194 this.channel = channel;
195 this.cla = cla;
196 this.command = command;
197 this.p1 = p1;
198 this.p2 = p2;
199 this.p3 = p3;
200 this.data = data;
201 }
202 }
203
204 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700205 * A request object to use for transmitting data to an ICC.
206 */
207 private static final class ManualNetworkSelectionArgument {
208 public OperatorInfo operatorInfo;
209 public boolean persistSelection;
210
211 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
212 this.operatorInfo = operatorInfo;
213 this.persistSelection = persistSelection;
214 }
215 }
216
217 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700218 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
219 * request after sending. The main thread will notify the request when it is complete.
220 */
221 private static final class MainThreadRequest {
222 /** The argument to use for the request */
223 public Object argument;
224 /** The result of the request that is run on the main thread */
225 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800226 // The subscriber id that this request applies to. Defaults to
227 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
228 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700229
230 public MainThreadRequest(Object argument) {
231 this.argument = argument;
232 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800233
234 public MainThreadRequest(Object argument, Integer subId) {
235 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800236 if (subId != null) {
237 this.subId = subId;
238 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800239 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700240 }
241
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800242 private static final class IncomingThirdPartyCallArgs {
243 public final ComponentName component;
244 public final String callId;
245 public final String callerDisplayName;
246
247 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
248 String callerDisplayName) {
249 this.component = component;
250 this.callId = callId;
251 this.callerDisplayName = callerDisplayName;
252 }
253 }
254
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 /**
256 * A handler that processes messages on the main thread in the phone process. Since many
257 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
258 * inbound binder threads to the main thread in the phone process. The Binder thread
259 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
260 * on, which will be notified when the operation completes and will contain the result of the
261 * request.
262 *
263 * <p>If a MainThreadRequest object is provided in the msg.obj field,
264 * note that request.result must be set to something non-null for the calling thread to
265 * unblock.
266 */
267 private final class MainThreadHandler extends Handler {
268 @Override
269 public void handleMessage(Message msg) {
270 MainThreadRequest request;
271 Message onCompleted;
272 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800273 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700274 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700275
276 switch (msg.what) {
pkanwar32d516d2016-10-14 19:37:38 -0700277 case CMD_HANDLE_USSD_REQUEST: {
278 request = (MainThreadRequest) msg.obj;
279 final Phone phone = getPhoneFromRequest(request);
280 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
281 String ussdRequest = ussdObject.first;
282 ResultReceiver wrappedCallback = ussdObject.second;
283 request.result = phone != null ?
284 phone.handleUssdRequest(ussdRequest, wrappedCallback)
285 :false;
286 // Wake up the requesting thread
287 synchronized (request) {
288 request.notifyAll();
289 }
290 break;
291 }
292
Yorke Lee716f67e2015-06-17 15:39:16 -0700293 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700294 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700295 final Phone phone = getPhoneFromRequest(request);
296 request.result = phone != null ?
297 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
298 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 // Wake up the requesting thread
300 synchronized (request) {
301 request.notifyAll();
302 }
303 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700304 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305
306 case CMD_HANDLE_NEIGHBORING_CELL:
307 request = (MainThreadRequest) msg.obj;
308 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
309 request);
Sooraj Sasindran9a909312016-07-18 11:57:25 -0700310 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 break;
312
313 case EVENT_NEIGHBORING_CELL_DONE:
314 ar = (AsyncResult) msg.obj;
315 request = (MainThreadRequest) ar.userObj;
316 if (ar.exception == null && ar.result != null) {
317 request.result = ar.result;
318 } else {
319 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800320 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700321 }
322 // Wake up the requesting thread
323 synchronized (request) {
324 request.notifyAll();
325 }
326 break;
327
328 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700329 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800330 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700331 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700332 break;
333
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700334 case CMD_END_CALL:
335 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800336 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700337 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700338 Phone phone = getPhone(end_subId);
339 if (phone == null) {
340 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
341 break;
342 }
343 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700344 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
345 // CDMA: If the user presses the Power button we treat it as
346 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700347 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
349 // GSM: End the call as per the Phone state
350 hungUp = PhoneUtils.hangup(mCM);
351 } else {
352 throw new IllegalStateException("Unexpected phone type: " + phoneType);
353 }
354 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
355 request.result = hungUp;
356 // Wake up the requesting thread
357 synchronized (request) {
358 request.notifyAll();
359 }
360 break;
361
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700362 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700363 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700364 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800365 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700366 if (uiccCard == null) {
367 loge("iccTransmitApduLogicalChannel: No UICC");
368 request.result = new IccIoResult(0x6F, 0, (byte[])null);
369 synchronized (request) {
370 request.notifyAll();
371 }
372 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700373 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
374 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700375 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700376 iccArgument.channel, iccArgument.cla, iccArgument.command,
377 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700378 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700379 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700380 break;
381
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700382 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700383 ar = (AsyncResult) msg.obj;
384 request = (MainThreadRequest) ar.userObj;
385 if (ar.exception == null && ar.result != null) {
386 request.result = ar.result;
387 } else {
388 request.result = new IccIoResult(0x6F, 0, (byte[])null);
389 if (ar.result == null) {
390 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800391 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700392 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800393 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700394 } else {
395 loge("iccTransmitApduLogicalChannel: Unknown exception");
396 }
397 }
398 synchronized (request) {
399 request.notifyAll();
400 }
401 break;
402
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700403 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
404 request = (MainThreadRequest) msg.obj;
405 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800406 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700407 if (uiccCard == null) {
408 loge("iccTransmitApduBasicChannel: No UICC");
409 request.result = new IccIoResult(0x6F, 0, (byte[])null);
410 synchronized (request) {
411 request.notifyAll();
412 }
413 } else {
414 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
415 request);
416 uiccCard.iccTransmitApduBasicChannel(
417 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
418 iccArgument.p3, iccArgument.data, onCompleted);
419 }
420 break;
421
422 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
423 ar = (AsyncResult) msg.obj;
424 request = (MainThreadRequest) ar.userObj;
425 if (ar.exception == null && ar.result != null) {
426 request.result = ar.result;
427 } else {
428 request.result = new IccIoResult(0x6F, 0, (byte[])null);
429 if (ar.result == null) {
430 loge("iccTransmitApduBasicChannel: Empty response");
431 } else if (ar.exception instanceof CommandException) {
432 loge("iccTransmitApduBasicChannel: CommandException: " +
433 ar.exception);
434 } else {
435 loge("iccTransmitApduBasicChannel: Unknown exception");
436 }
437 }
438 synchronized (request) {
439 request.notifyAll();
440 }
441 break;
442
443 case CMD_EXCHANGE_SIM_IO:
444 request = (MainThreadRequest) msg.obj;
445 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800446 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700447 if (uiccCard == null) {
448 loge("iccExchangeSimIO: No UICC");
449 request.result = new IccIoResult(0x6F, 0, (byte[])null);
450 synchronized (request) {
451 request.notifyAll();
452 }
453 } else {
454 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
455 request);
456 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
457 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
458 iccArgument.data, onCompleted);
459 }
460 break;
461
462 case EVENT_EXCHANGE_SIM_IO_DONE:
463 ar = (AsyncResult) msg.obj;
464 request = (MainThreadRequest) ar.userObj;
465 if (ar.exception == null && ar.result != null) {
466 request.result = ar.result;
467 } else {
468 request.result = new IccIoResult(0x6f, 0, (byte[])null);
469 }
470 synchronized (request) {
471 request.notifyAll();
472 }
473 break;
474
Derek Tan4d5e5c12014-02-04 11:54:58 -0800475 case CMD_SEND_ENVELOPE:
476 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800477 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700478 if (uiccCard == null) {
479 loge("sendEnvelopeWithStatus: No UICC");
480 request.result = new IccIoResult(0x6F, 0, (byte[])null);
481 synchronized (request) {
482 request.notifyAll();
483 }
484 } else {
485 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
486 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
487 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800488 break;
489
490 case EVENT_SEND_ENVELOPE_DONE:
491 ar = (AsyncResult) msg.obj;
492 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700493 if (ar.exception == null && ar.result != null) {
494 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800495 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700496 request.result = new IccIoResult(0x6F, 0, (byte[])null);
497 if (ar.result == null) {
498 loge("sendEnvelopeWithStatus: Empty response");
499 } else if (ar.exception instanceof CommandException) {
500 loge("sendEnvelopeWithStatus: CommandException: " +
501 ar.exception);
502 } else {
503 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
504 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800505 }
506 synchronized (request) {
507 request.notifyAll();
508 }
509 break;
510
Shishir Agrawal566b7612013-10-28 14:41:00 -0700511 case CMD_OPEN_CHANNEL:
512 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800513 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700514 if (uiccCard == null) {
515 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800516 request.result = new IccOpenLogicalChannelResponse(-1,
517 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700518 synchronized (request) {
519 request.notifyAll();
520 }
521 } else {
522 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
523 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
524 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700525 break;
526
527 case EVENT_OPEN_CHANNEL_DONE:
528 ar = (AsyncResult) msg.obj;
529 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700530 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700531 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700532 int[] result = (int[]) ar.result;
533 int channelId = result[0];
534 byte[] selectResponse = null;
535 if (result.length > 1) {
536 selectResponse = new byte[result.length - 1];
537 for (int i = 1; i < result.length; ++i) {
538 selectResponse[i - 1] = (byte) result[i];
539 }
540 }
541 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700542 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700543 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700544 if (ar.result == null) {
545 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700546 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700547 if (ar.exception != null) {
548 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
549 }
550
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700551 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700552 if (ar.exception instanceof CommandException) {
553 CommandException.Error error =
554 ((CommandException) (ar.exception)).getCommandError();
555 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700556 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700557 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700558 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700559 }
560 }
561 openChannelResp = new IccOpenLogicalChannelResponse(
562 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700563 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700564 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700565 synchronized (request) {
566 request.notifyAll();
567 }
568 break;
569
570 case CMD_CLOSE_CHANNEL:
571 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800572 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700573 if (uiccCard == null) {
574 loge("iccCloseLogicalChannel: No UICC");
575 request.result = new IccIoResult(0x6F, 0, (byte[])null);
576 synchronized (request) {
577 request.notifyAll();
578 }
579 } else {
580 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
581 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
582 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700583 break;
584
585 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800586 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
587 break;
588
589 case CMD_NV_READ_ITEM:
590 request = (MainThreadRequest) msg.obj;
591 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
592 mPhone.nvReadItem((Integer) request.argument, onCompleted);
593 break;
594
595 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700596 ar = (AsyncResult) msg.obj;
597 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800598 if (ar.exception == null && ar.result != null) {
599 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700600 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800601 request.result = "";
602 if (ar.result == null) {
603 loge("nvReadItem: Empty response");
604 } else if (ar.exception instanceof CommandException) {
605 loge("nvReadItem: CommandException: " +
606 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700607 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800608 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700609 }
610 }
611 synchronized (request) {
612 request.notifyAll();
613 }
614 break;
615
Jake Hambye994d462014-02-03 13:10:13 -0800616 case CMD_NV_WRITE_ITEM:
617 request = (MainThreadRequest) msg.obj;
618 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
619 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
620 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
621 break;
622
623 case EVENT_NV_WRITE_ITEM_DONE:
624 handleNullReturnEvent(msg, "nvWriteItem");
625 break;
626
627 case CMD_NV_WRITE_CDMA_PRL:
628 request = (MainThreadRequest) msg.obj;
629 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
630 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
631 break;
632
633 case EVENT_NV_WRITE_CDMA_PRL_DONE:
634 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
635 break;
636
637 case CMD_NV_RESET_CONFIG:
638 request = (MainThreadRequest) msg.obj;
639 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
640 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
641 break;
642
643 case EVENT_NV_RESET_CONFIG_DONE:
644 handleNullReturnEvent(msg, "nvResetConfig");
645 break;
646
Jake Hamby7c27be32014-03-03 13:25:59 -0800647 case CMD_GET_PREFERRED_NETWORK_TYPE:
648 request = (MainThreadRequest) msg.obj;
649 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700650 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800651 break;
652
653 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
654 ar = (AsyncResult) msg.obj;
655 request = (MainThreadRequest) ar.userObj;
656 if (ar.exception == null && ar.result != null) {
657 request.result = ar.result; // Integer
658 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800659 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800660 if (ar.result == null) {
661 loge("getPreferredNetworkType: Empty response");
662 } else if (ar.exception instanceof CommandException) {
663 loge("getPreferredNetworkType: CommandException: " +
664 ar.exception);
665 } else {
666 loge("getPreferredNetworkType: Unknown exception");
667 }
668 }
669 synchronized (request) {
670 request.notifyAll();
671 }
672 break;
673
674 case CMD_SET_PREFERRED_NETWORK_TYPE:
675 request = (MainThreadRequest) msg.obj;
676 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
677 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700678 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800679 break;
680
681 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
682 handleNullReturnEvent(msg, "setPreferredNetworkType");
683 break;
684
Steven Liu4bf01bc2014-07-17 11:05:29 -0500685 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
686 request = (MainThreadRequest)msg.obj;
687 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
688 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
689 break;
690
691 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
692 ar = (AsyncResult)msg.obj;
693 request = (MainThreadRequest)ar.userObj;
694 request.result = ar;
695 synchronized (request) {
696 request.notifyAll();
697 }
698 break;
699
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800700 case CMD_SET_VOICEMAIL_NUMBER:
701 request = (MainThreadRequest) msg.obj;
702 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
703 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800704 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
705 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800706 break;
707
708 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
709 handleNullReturnEvent(msg, "setVoicemailNumber");
710 break;
711
Stuart Scott54788802015-03-30 13:18:01 -0700712 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
713 request = (MainThreadRequest) msg.obj;
714 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
715 request);
716 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
717 break;
718
719 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
720 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
721 break;
722
Shishir Agrawal302c8692015-06-19 13:49:39 -0700723 case CMD_PERFORM_NETWORK_SCAN:
724 request = (MainThreadRequest) msg.obj;
725 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
726 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
727 break;
728
729 case EVENT_PERFORM_NETWORK_SCAN_DONE:
730 ar = (AsyncResult) msg.obj;
731 request = (MainThreadRequest) ar.userObj;
732 CellNetworkScanResult cellScanResult;
733 if (ar.exception == null && ar.result != null) {
734 cellScanResult = new CellNetworkScanResult(
735 CellNetworkScanResult.STATUS_SUCCESS,
736 (List<OperatorInfo>) ar.result);
737 } else {
738 if (ar.result == null) {
739 loge("getCellNetworkScanResults: Empty response");
740 }
741 if (ar.exception != null) {
742 loge("getCellNetworkScanResults: Exception: " + ar.exception);
743 }
744 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
745 if (ar.exception instanceof CommandException) {
746 CommandException.Error error =
747 ((CommandException) (ar.exception)).getCommandError();
748 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
749 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
750 } else if (error == CommandException.Error.GENERIC_FAILURE) {
751 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
752 }
753 }
754 cellScanResult = new CellNetworkScanResult(errorCode, null);
755 }
756 request.result = cellScanResult;
757 synchronized (request) {
758 request.notifyAll();
759 }
760 break;
761
762 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
763 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700764 ManualNetworkSelectionArgument selArg =
765 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700766 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
767 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700768 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
769 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700770 break;
771
772 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
773 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
774 break;
775
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700776 case CMD_GET_MODEM_ACTIVITY_INFO:
777 request = (MainThreadRequest) msg.obj;
778 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700779 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700780 break;
781
782 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
783 ar = (AsyncResult) msg.obj;
784 request = (MainThreadRequest) ar.userObj;
785 if (ar.exception == null && ar.result != null) {
786 request.result = ar.result;
787 } else {
788 if (ar.result == null) {
789 loge("queryModemActivityInfo: Empty response");
790 } else if (ar.exception instanceof CommandException) {
791 loge("queryModemActivityInfo: CommandException: " +
792 ar.exception);
793 } else {
794 loge("queryModemActivityInfo: Unknown exception");
795 }
796 }
Amit Mahajand4766222016-01-28 15:28:28 -0800797 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
798 if (request.result == null) {
799 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
800 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700801 synchronized (request) {
802 request.notifyAll();
803 }
804 break;
805
Meng Wang1a7c35a2016-05-05 20:56:15 -0700806 case CMD_SET_ALLOWED_CARRIERS:
807 request = (MainThreadRequest) msg.obj;
808 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
809 mPhone.setAllowedCarriers(
810 (List<CarrierIdentifier>) request.argument,
811 onCompleted);
812 break;
813
814 case EVENT_SET_ALLOWED_CARRIERS_DONE:
815 ar = (AsyncResult) msg.obj;
816 request = (MainThreadRequest) ar.userObj;
817 if (ar.exception == null && ar.result != null) {
818 request.result = ar.result;
819 } else {
820 if (ar.result == null) {
821 loge("setAllowedCarriers: Empty response");
822 } else if (ar.exception instanceof CommandException) {
823 loge("setAllowedCarriers: CommandException: " +
824 ar.exception);
825 } else {
826 loge("setAllowedCarriers: Unknown exception");
827 }
828 }
829 // Result cannot be null. Return -1 on error.
830 if (request.result == null) {
831 request.result = new int[]{-1};
832 }
833 synchronized (request) {
834 request.notifyAll();
835 }
836 break;
837
838 case CMD_GET_ALLOWED_CARRIERS:
839 request = (MainThreadRequest) msg.obj;
840 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
841 mPhone.getAllowedCarriers(onCompleted);
842 break;
843
844 case EVENT_GET_ALLOWED_CARRIERS_DONE:
845 ar = (AsyncResult) msg.obj;
846 request = (MainThreadRequest) ar.userObj;
847 if (ar.exception == null && ar.result != null) {
848 request.result = ar.result;
849 } else {
850 if (ar.result == null) {
851 loge("getAllowedCarriers: Empty response");
852 } else if (ar.exception instanceof CommandException) {
853 loge("getAllowedCarriers: CommandException: " +
854 ar.exception);
855 } else {
856 loge("getAllowedCarriers: Unknown exception");
857 }
858 }
859 // Result cannot be null. Return empty list of CarrierIdentifier.
860 if (request.result == null) {
861 request.result = new ArrayList<CarrierIdentifier>(0);
862 }
863 synchronized (request) {
864 request.notifyAll();
865 }
866 break;
867
Nathan Haroldb3014052017-01-25 15:57:32 -0800868 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
869 ar = (AsyncResult) msg.obj;
870 request = (MainThreadRequest) ar.userObj;
871 if (ar.exception == null && ar.result != null) {
872 request.result = ar.result;
873 } else {
874 request.result = new IllegalArgumentException(
875 "Failed to retrieve Forbidden Plmns");
876 if (ar.result == null) {
877 loge("getForbiddenPlmns: Empty response");
878 } else {
879 loge("getForbiddenPlmns: Unknown exception");
880 }
881 }
882 synchronized (request) {
883 request.notifyAll();
884 }
885 break;
886
887 case CMD_GET_FORBIDDEN_PLMNS:
888 request = (MainThreadRequest) msg.obj;
889 uiccCard = getUiccCardFromRequest(request);
890 if (uiccCard == null) {
891 loge("getForbiddenPlmns() UiccCard is null");
892 request.result = new IllegalArgumentException(
893 "getForbiddenPlmns() UiccCard is null");
894 synchronized (request) {
895 request.notifyAll();
896 }
897 break;
898 }
899 Integer appType = (Integer) request.argument;
900 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
901 if (uiccApp == null) {
902 loge("getForbiddenPlmns() no app with specified type -- "
903 + appType);
904 request.result = new IllegalArgumentException("Failed to get UICC App");
905 synchronized (request) {
906 request.notifyAll();
907 }
908 break;
909 } else {
910 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
911 + " specified type -- " + appType);
912 }
913 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
914 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
915 onCompleted);
916 break;
917
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700918 default:
919 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
920 break;
921 }
922 }
Jake Hambye994d462014-02-03 13:10:13 -0800923
924 private void handleNullReturnEvent(Message msg, String command) {
925 AsyncResult ar = (AsyncResult) msg.obj;
926 MainThreadRequest request = (MainThreadRequest) ar.userObj;
927 if (ar.exception == null) {
928 request.result = true;
929 } else {
930 request.result = false;
931 if (ar.exception instanceof CommandException) {
932 loge(command + ": CommandException: " + ar.exception);
933 } else {
934 loge(command + ": Unknown exception");
935 }
936 }
937 synchronized (request) {
938 request.notifyAll();
939 }
940 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 }
942
943 /**
944 * Posts the specified command to be executed on the main thread,
945 * waits for the request to complete, and returns the result.
946 * @see #sendRequestAsync
947 */
948 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800949 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700950 }
951
952 /**
953 * Posts the specified command to be executed on the main thread,
954 * waits for the request to complete, and returns the result.
955 * @see #sendRequestAsync
956 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800957 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
959 throw new RuntimeException("This method will deadlock if called from the main thread.");
960 }
961
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800962 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 Message msg = mMainThreadHandler.obtainMessage(command, request);
964 msg.sendToTarget();
965
966 // Wait for the request to complete
967 synchronized (request) {
968 while (request.result == null) {
969 try {
970 request.wait();
971 } catch (InterruptedException e) {
972 // Do nothing, go back and wait until the request is complete
973 }
974 }
975 }
976 return request.result;
977 }
978
979 /**
980 * Asynchronous ("fire and forget") version of sendRequest():
981 * Posts the specified command to be executed on the main thread, and
982 * returns immediately.
983 * @see #sendRequest
984 */
985 private void sendRequestAsync(int command) {
986 mMainThreadHandler.sendEmptyMessage(command);
987 }
988
989 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700990 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
991 * @see {@link #sendRequest(int,Object)}
992 */
993 private void sendRequestAsync(int command, Object argument) {
994 MainThreadRequest request = new MainThreadRequest(argument);
995 Message msg = mMainThreadHandler.obtainMessage(command, request);
996 msg.sendToTarget();
997 }
998
999 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 * Initialize the singleton PhoneInterfaceManager instance.
1001 * This is only done once, at startup, from PhoneApp.onCreate().
1002 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001003 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001004 synchronized (PhoneInterfaceManager.class) {
1005 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001006 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001007 } else {
1008 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1009 }
1010 return sInstance;
1011 }
1012 }
1013
1014 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001015 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 mApp = app;
1017 mPhone = phone;
1018 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001019 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1021 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001022 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001023 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001024 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001025
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026 publish();
1027 }
1028
1029 private void publish() {
1030 if (DBG) log("publish: " + this);
1031
1032 ServiceManager.addService("phone", this);
1033 }
1034
Stuart Scott584921c2015-01-15 17:10:34 -08001035 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001036 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1037 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001038 }
1039
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001040 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1041 Phone phone = getPhoneFromRequest(request);
1042 return phone == null ? null :
1043 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1044 }
1045
Wink Saville36469e72014-06-11 15:17:00 -07001046 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001047 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001048 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001049 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001050 //
1051 // Implementation of the ITelephony interface.
1052 //
1053
1054 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001055 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001056 }
1057
Wink Savilleb564aae2014-10-23 10:18:09 -07001058 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 if (DBG) log("dial: " + number);
1060 // No permission check needed here: This is just a wrapper around the
1061 // ACTION_DIAL intent, which is available to any app since it puts up
1062 // the UI before it does anything.
1063
1064 String url = createTelUrl(number);
1065 if (url == null) {
1066 return;
1067 }
1068
1069 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001070 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001071 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1072 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1073 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1074 mApp.startActivity(intent);
1075 }
1076 }
1077
1078 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001079 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001080 }
1081
Wink Savilleb564aae2014-10-23 10:18:09 -07001082 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 if (DBG) log("call: " + number);
1084
1085 // This is just a wrapper around the ACTION_CALL intent, but we still
1086 // need to do a permission check since we're calling startActivity()
1087 // from the context of the phone app.
1088 enforceCallPermission();
1089
1090 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1091 != AppOpsManager.MODE_ALLOWED) {
1092 return;
1093 }
1094
1095 String url = createTelUrl(number);
1096 if (url == null) {
1097 return;
1098 }
1099
Wink Saville08874612014-08-31 19:19:58 -07001100 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001101 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001102 if (slist != null) {
1103 for (SubscriptionInfo subInfoRecord : slist) {
1104 if (subInfoRecord.getSubscriptionId() == subId) {
1105 isValid = true;
1106 break;
1107 }
Wink Saville08874612014-08-31 19:19:58 -07001108 }
1109 }
1110 if (isValid == false) {
1111 return;
1112 }
1113
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001114 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001115 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001116 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1117 mApp.startActivity(intent);
1118 }
1119
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001120 /**
1121 * End a call based on call state
1122 * @return true is a call was ended
1123 */
1124 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001125 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001126 }
1127
1128 /**
1129 * End a call based on the call state of the subId
1130 * @return true is a call was ended
1131 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001132 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001133 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001134 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001135 }
1136
1137 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001138 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001139 }
1140
Wink Savilleb564aae2014-10-23 10:18:09 -07001141 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001142 if (DBG) log("answerRingingCall...");
1143 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1144 // but that can probably wait till the big TelephonyManager API overhaul.
1145 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1146 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001147 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 }
1149
1150 /**
1151 * Make the actual telephony calls to implement answerRingingCall().
1152 * This should only be called from the main thread of the Phone app.
1153 * @see #answerRingingCall
1154 *
1155 * TODO: it would be nice to return true if we answered the call, or
1156 * false if there wasn't actually a ringing incoming call, or some
1157 * other error occurred. (In other words, pass back the return value
1158 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1159 * But that would require calling this method via sendRequest() rather
1160 * than sendRequestAsync(), and right now we don't actually *need* that
1161 * return value, so let's just return void for now.
1162 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001163 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001164 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001165 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001166 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1167 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001168 if (hasActiveCall && hasHoldingCall) {
1169 // Both lines are in use!
1170 // TODO: provide a flag to let the caller specify what
1171 // policy to use if both lines are in use. (The current
1172 // behavior is hardwired to "answer incoming, end ongoing",
1173 // which is how the CALL button is specced to behave.)
1174 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1175 return;
1176 } else {
1177 // answerCall() will automatically hold the current active
1178 // call, if there is one.
1179 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1180 return;
1181 }
1182 } else {
1183 // No call was ringing.
1184 return;
1185 }
1186 }
1187
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001188 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001189 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001190 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001191 public void silenceRinger() {
1192 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 }
1194
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001195 @Override
1196 public boolean isOffhook(String callingPackage) {
1197 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001198 }
1199
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001200 @Override
1201 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1202 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1203 return false;
1204 }
1205
Sanket Padawe356d7632015-06-22 14:03:32 -07001206 final Phone phone = getPhone(subId);
1207 if (phone != null) {
1208 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1209 } else {
1210 return false;
1211 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001212 }
1213
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001214 @Override
1215 public boolean isRinging(String callingPackage) {
1216 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001217 }
1218
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001219 @Override
1220 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1221 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1222 return false;
1223 }
1224
Sanket Padawe356d7632015-06-22 14:03:32 -07001225 final Phone phone = getPhone(subId);
1226 if (phone != null) {
1227 return (phone.getState() == PhoneConstants.State.RINGING);
1228 } else {
1229 return false;
1230 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001231 }
1232
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001233 @Override
1234 public boolean isIdle(String callingPackage) {
1235 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001236 }
1237
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001238 @Override
1239 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1240 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1241 return false;
1242 }
1243
Sanket Padawe356d7632015-06-22 14:03:32 -07001244 final Phone phone = getPhone(subId);
1245 if (phone != null) {
1246 return (phone.getState() == PhoneConstants.State.IDLE);
1247 } else {
1248 return false;
1249 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001250 }
1251
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001252 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001253 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001254 }
1255
Wink Savilleb564aae2014-10-23 10:18:09 -07001256 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001257 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001258 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1259 }
1260
1261 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001262 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001263 }
1264
Wink Savilleb564aae2014-10-23 10:18:09 -07001265 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001266 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001267 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1268 }
1269
1270 /** {@hide} */
1271 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001272 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001273 }
1274
Wink Savilleb564aae2014-10-23 10:18:09 -07001275 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001277 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001278 checkSimPin.start();
1279 return checkSimPin.unlockSim(null, pin);
1280 }
1281
Wink Saville9de0f752013-10-22 19:04:03 -07001282 /** {@hide} */
1283 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001284 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001285 }
1286
Wink Savilleb564aae2014-10-23 10:18:09 -07001287 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001289 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001290 checkSimPuk.start();
1291 return checkSimPuk.unlockSim(puk, pin);
1292 }
1293
1294 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001295 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 * a synchronous one.
1297 */
1298 private static class UnlockSim extends Thread {
1299
1300 private final IccCard mSimCard;
1301
1302 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001303 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1304 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305
1306 // For replies from SimCard interface
1307 private Handler mHandler;
1308
1309 // For async handler to identify request type
1310 private static final int SUPPLY_PIN_COMPLETE = 100;
1311
1312 public UnlockSim(IccCard simCard) {
1313 mSimCard = simCard;
1314 }
1315
1316 @Override
1317 public void run() {
1318 Looper.prepare();
1319 synchronized (UnlockSim.this) {
1320 mHandler = new Handler() {
1321 @Override
1322 public void handleMessage(Message msg) {
1323 AsyncResult ar = (AsyncResult) msg.obj;
1324 switch (msg.what) {
1325 case SUPPLY_PIN_COMPLETE:
1326 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1327 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001328 mRetryCount = msg.arg1;
1329 if (ar.exception != null) {
1330 if (ar.exception instanceof CommandException &&
1331 ((CommandException)(ar.exception)).getCommandError()
1332 == CommandException.Error.PASSWORD_INCORRECT) {
1333 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1334 } else {
1335 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1336 }
1337 } else {
1338 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1339 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001340 mDone = true;
1341 UnlockSim.this.notifyAll();
1342 }
1343 break;
1344 }
1345 }
1346 };
1347 UnlockSim.this.notifyAll();
1348 }
1349 Looper.loop();
1350 }
1351
1352 /*
1353 * Use PIN or PUK to unlock SIM card
1354 *
1355 * If PUK is null, unlock SIM card with PIN
1356 *
1357 * If PUK is not null, unlock SIM card with PUK and set PIN code
1358 */
Wink Saville9de0f752013-10-22 19:04:03 -07001359 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001360
1361 while (mHandler == null) {
1362 try {
1363 wait();
1364 } catch (InterruptedException e) {
1365 Thread.currentThread().interrupt();
1366 }
1367 }
1368 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1369
1370 if (puk == null) {
1371 mSimCard.supplyPin(pin, callback);
1372 } else {
1373 mSimCard.supplyPuk(puk, pin, callback);
1374 }
1375
1376 while (!mDone) {
1377 try {
1378 Log.d(LOG_TAG, "wait for done");
1379 wait();
1380 } catch (InterruptedException e) {
1381 // Restore the interrupted status
1382 Thread.currentThread().interrupt();
1383 }
1384 }
1385 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001386 int[] resultArray = new int[2];
1387 resultArray[0] = mResult;
1388 resultArray[1] = mRetryCount;
1389 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001390 }
1391 }
1392
1393 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001394 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001395
1396 }
1397
Wink Savilleb564aae2014-10-23 10:18:09 -07001398 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 // No permission check needed here: this call is harmless, and it's
1400 // needed for the ServiceState.requestStateUpdate() call (which is
1401 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001402 final Phone phone = getPhone(subId);
1403 if (phone != null) {
1404 phone.updateServiceLocation();
1405 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 }
1407
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001408 @Override
1409 public boolean isRadioOn(String callingPackage) {
1410 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001411 }
1412
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001413 @Override
1414 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1415 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1416 return false;
1417 }
1418 return isRadioOnForSubscriber(subId);
1419 }
1420
1421 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001422 final Phone phone = getPhone(subId);
1423 if (phone != null) {
1424 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1425 } else {
1426 return false;
1427 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001428 }
1429
1430 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001431 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001432
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 }
Wink Saville36469e72014-06-11 15:17:00 -07001434
Wink Savilleb564aae2014-10-23 10:18:09 -07001435 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001436 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001437 final Phone phone = getPhone(subId);
1438 if (phone != null) {
1439 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1440 }
Wink Saville36469e72014-06-11 15:17:00 -07001441 }
1442
1443 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001444 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001445 }
1446
Wink Savilleb564aae2014-10-23 10:18:09 -07001447 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001448 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001449 final Phone phone = getPhone(subId);
1450 if (phone == null) {
1451 return false;
1452 }
1453 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001454 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001455 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 }
1457 return true;
1458 }
Wink Saville36469e72014-06-11 15:17:00 -07001459
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001460 public boolean needMobileRadioShutdown() {
1461 /*
1462 * If any of the Radios are available, it will need to be
1463 * shutdown. So return true if any Radio is available.
1464 */
1465 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1466 Phone phone = PhoneFactory.getPhone(i);
1467 if (phone != null && phone.isRadioAvailable()) return true;
1468 }
1469 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1470 return false;
1471 }
1472
1473 public void shutdownMobileRadios() {
1474 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1475 logv("Shutting down Phone " + i);
1476 shutdownRadioUsingPhoneId(i);
1477 }
1478 }
1479
1480 private void shutdownRadioUsingPhoneId(int phoneId) {
1481 enforceModifyPermission();
1482 Phone phone = PhoneFactory.getPhone(phoneId);
1483 if (phone != null && phone.isRadioAvailable()) {
1484 phone.shutdownRadio();
1485 }
1486 }
1487
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001488 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001489 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1490 if (defaultPhone != null) {
1491 defaultPhone.setRadioPower(turnOn);
1492 return true;
1493 } else {
1494 loge("There's no default phone.");
1495 return false;
1496 }
Wink Saville36469e72014-06-11 15:17:00 -07001497 }
1498
Wink Savilleb564aae2014-10-23 10:18:09 -07001499 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001501 final Phone phone = getPhone(subId);
1502 if (phone != null) {
1503 phone.setRadioPower(turnOn);
1504 return true;
1505 } else {
1506 return false;
1507 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001508 }
1509
Wink Saville36469e72014-06-11 15:17:00 -07001510 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001511 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001512 public boolean enableDataConnectivity() {
1513 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001514 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001515 final Phone phone = getPhone(subId);
1516 if (phone != null) {
1517 phone.setDataEnabled(true);
1518 return true;
1519 } else {
1520 return false;
1521 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 }
1523
Wink Saville36469e72014-06-11 15:17:00 -07001524 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001525 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001526 public boolean disableDataConnectivity() {
1527 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001528 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001529 final Phone phone = getPhone(subId);
1530 if (phone != null) {
1531 phone.setDataEnabled(false);
1532 return true;
1533 } else {
1534 return false;
1535 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 }
1537
Wink Saville36469e72014-06-11 15:17:00 -07001538 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001539 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001540 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001541 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001542 final Phone phone = getPhone(subId);
1543 if (phone != null) {
1544 return phone.isDataConnectivityPossible();
1545 } else {
1546 return false;
1547 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001548 }
1549
1550 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001551 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001552 }
1553
pkanwarae03a6b2016-11-06 20:37:09 -08001554 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar32d516d2016-10-14 19:37:38 -07001555 enforceCallPermission();
pkanwar32d516d2016-10-14 19:37:38 -07001556 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1557 return;
1558 }
1559 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1560 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1561 };
1562
1563
Wink Savilleb564aae2014-10-23 10:18:09 -07001564 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001565 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001566 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1567 return false;
1568 }
Wink Saville36469e72014-06-11 15:17:00 -07001569 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001570 }
1571
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001572 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001573 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001574 }
1575
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001576 public int getCallStateForSlot(int slotIndex) {
1577 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001578 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harolda667c152016-12-14 11:27:20 -08001579 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001580 }
1581
Sanket Padawe356d7632015-06-22 14:03:32 -07001582 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001583 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001584 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001585 if (phone != null) {
Nathan Harolda667c152016-12-14 11:27:20 -08001586 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001587 } else {
Nathan Harolda667c152016-12-14 11:27:20 -08001588 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001589 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 }
1591
Sanket Padawe356d7632015-06-22 14:03:32 -07001592 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001593 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001594 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001595 if (phone != null) {
1596 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1597 } else {
1598 return TelephonyManager.DATA_ACTIVITY_NONE;
1599 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001600 }
1601
1602 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001603 public Bundle getCellLocation(String callingPackage) {
1604 enforceFineOrCoarseLocationPermission("getCellLocation");
1605
1606 // OP_COARSE_LOCATION controls both fine and coarse location.
1607 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1608 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001609 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001610 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001611 }
1612
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001613 if (checkIfCallerIsSelfOrForegroundUser() ||
1614 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001615 if (DBG_LOC) log("getCellLocation: is active user");
1616 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001617 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001618 if (phone == null) {
1619 return null;
1620 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001621
1622 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1623 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001624 return data;
1625 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001626 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001627 return null;
1628 }
1629 }
1630
Svetoslav64fad262015-04-14 14:35:21 -07001631 private void enforceFineOrCoarseLocationPermission(String message) {
1632 try {
1633 mApp.enforceCallingOrSelfPermission(
1634 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1635 } catch (SecurityException e) {
1636 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1637 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1638 // is the weaker precondition
1639 mApp.enforceCallingOrSelfPermission(
1640 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1641 }
1642 }
1643
1644
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001645 @Override
1646 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001647 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001648 }
1649
Sanket Padawe356d7632015-06-22 14:03:32 -07001650 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001651 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001652 mApp.enforceCallingOrSelfPermission(
1653 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001654 final Phone phone = getPhone(subId);
1655 if (phone != null) {
1656 phone.enableLocationUpdates();
1657 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001658 }
1659
1660 @Override
1661 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001662 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001663 }
1664
Sanket Padawe356d7632015-06-22 14:03:32 -07001665 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001666 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001667 mApp.enforceCallingOrSelfPermission(
1668 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001669 final Phone phone = getPhone(subId);
1670 if (phone != null) {
1671 phone.disableLocationUpdates();
1672 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001673 }
1674
1675 @Override
1676 @SuppressWarnings("unchecked")
1677 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001678 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1679
1680 // OP_COARSE_LOCATION controls both fine and coarse location.
1681 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1682 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1683 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001684 }
1685
1686 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1687 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1688 return null;
1689 }
Svetoslav64fad262015-04-14 14:35:21 -07001690
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001691 if (checkIfCallerIsSelfOrForegroundUser() ||
1692 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001693 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1694
1695 ArrayList<NeighboringCellInfo> cells = null;
1696
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001697 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001698 try {
1699 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001700 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001701 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001702 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001703 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001704 }
1705 return cells;
1706 } else {
1707 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1708 return null;
1709 }
1710 }
1711
1712
1713 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001714 public List<CellInfo> getAllCellInfo(String callingPackage) {
1715 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1716
1717 // OP_COARSE_LOCATION controls both fine and coarse location.
1718 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1719 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1720 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001721 }
1722
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001723 if (checkIfCallerIsSelfOrForegroundUser() ||
1724 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001725 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001726 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001727 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1728 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001729 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1730 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001731 }
1732 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001733 } else {
1734 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1735 return null;
1736 }
1737 }
1738
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001739 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001740 public void setCellInfoListRate(int rateInMillis) {
Jack Yua8d8cb82017-01-16 10:15:34 -08001741 enforceModifyPermission();
Sooraj Sasindran9a909312016-07-18 11:57:25 -07001742 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1743 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001744 }
1745
Shishir Agrawala9f32182016-04-12 12:00:16 -07001746 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001747 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001748 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1749 return null;
1750 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001751 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001752 return phone == null ? null : phone.getImei();
1753 }
1754
1755 @Override
Jack Yu2af8d712017-03-15 17:14:14 -07001756 public String getMeidForSlot(int slotIndex, String callingPackage) {
1757 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1758 return null;
1759 }
1760 Phone phone = PhoneFactory.getPhone(slotIndex);
1761 return phone == null ? null : phone.getMeid();
1762 }
1763
1764 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001765 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001766 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1767 return null;
1768 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001769 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001770 return phone == null ? null : phone.getDeviceSvn();
1771 }
1772
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001773 //
1774 // Internal helper methods.
1775 //
1776
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001777 /**
1778 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1779 */
1780 private boolean checkCallerInteractAcrossUsersFull() {
1781 return mPhone.getContext().checkCallingOrSelfPermission(
1782 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1783 == PackageManager.PERMISSION_GRANTED;
1784 }
1785
Jake Hambye994d462014-02-03 13:10:13 -08001786 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001787 boolean ok;
1788
1789 boolean self = Binder.getCallingUid() == Process.myUid();
1790 if (!self) {
1791 // Get the caller's user id then clear the calling identity
1792 // which will be restored in the finally clause.
1793 int callingUser = UserHandle.getCallingUserId();
1794 long ident = Binder.clearCallingIdentity();
1795
1796 try {
1797 // With calling identity cleared the current user is the foreground user.
1798 int foregroundUser = ActivityManager.getCurrentUser();
1799 ok = (foregroundUser == callingUser);
1800 if (DBG_LOC) {
1801 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1802 + " callingUser=" + callingUser + " ok=" + ok);
1803 }
1804 } catch (Exception ex) {
1805 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1806 ok = false;
1807 } finally {
1808 Binder.restoreCallingIdentity(ident);
1809 }
1810 } else {
1811 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1812 ok = true;
1813 }
1814 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1815 return ok;
1816 }
1817
1818 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001819 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1820 *
1821 * @throws SecurityException if the caller does not have the required permission
1822 */
1823 private void enforceModifyPermission() {
1824 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1825 }
1826
1827 /**
Junda Liua2e36012014-07-09 18:30:01 -07001828 * Make sure either system app or the caller has carrier privilege.
1829 *
1830 * @throws SecurityException if the caller does not have the required permission/privilege
1831 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001832 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001833 int permission = mApp.checkCallingOrSelfPermission(
1834 android.Manifest.permission.MODIFY_PHONE_STATE);
1835 if (permission == PackageManager.PERMISSION_GRANTED) {
1836 return;
1837 }
1838
1839 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001840 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001841 }
1842
1843 /**
1844 * Make sure the caller has carrier privilege.
1845 *
1846 * @throws SecurityException if the caller does not have the required permission
1847 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001848 private void enforceCarrierPrivilege(int subId) {
1849 if (getCarrierPrivilegeStatus(subId) !=
1850 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001851 loge("No Carrier Privilege.");
1852 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001853 }
1854 }
1855
1856 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001857 * Make sure the caller has the CALL_PHONE permission.
1858 *
1859 * @throws SecurityException if the caller does not have the required permission
1860 */
1861 private void enforceCallPermission() {
1862 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1863 }
1864
Stuart Scott8eef64f2015-04-08 15:13:54 -07001865 private void enforceConnectivityInternalPermission() {
1866 mApp.enforceCallingOrSelfPermission(
1867 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1868 "ConnectivityService");
1869 }
1870
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001871 private String createTelUrl(String number) {
1872 if (TextUtils.isEmpty(number)) {
1873 return null;
1874 }
1875
Jake Hambye994d462014-02-03 13:10:13 -08001876 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001877 }
1878
Ihab Awadf9e92732013-12-05 18:02:52 -08001879 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001880 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1881 }
1882
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001883 private static void logv(String msg) {
1884 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1885 }
1886
Ihab Awadf9e92732013-12-05 18:02:52 -08001887 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001888 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1889 }
1890
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001891 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001892 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001893 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001894 }
1895
Sanket Padawe356d7632015-06-22 14:03:32 -07001896 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07001897 public int getActivePhoneTypeForSlot(int slotIndex) {
1898 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001899 if (phone == null) {
1900 return PhoneConstants.PHONE_TYPE_NONE;
1901 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001902 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001903 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001904 }
1905
1906 /**
1907 * Returns the CDMA ERI icon index to display
1908 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001909 @Override
1910 public int getCdmaEriIconIndex(String callingPackage) {
1911 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001912 }
1913
Sanket Padawe356d7632015-06-22 14:03:32 -07001914 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001915 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1916 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1917 return -1;
1918 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001919 final Phone phone = getPhone(subId);
1920 if (phone != null) {
1921 return phone.getCdmaEriIconIndex();
1922 } else {
1923 return -1;
1924 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001925 }
1926
1927 /**
1928 * Returns the CDMA ERI icon mode,
1929 * 0 - ON
1930 * 1 - FLASHING
1931 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001932 @Override
1933 public int getCdmaEriIconMode(String callingPackage) {
1934 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001935 }
1936
Sanket Padawe356d7632015-06-22 14:03:32 -07001937 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001938 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1939 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1940 return -1;
1941 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001942 final Phone phone = getPhone(subId);
1943 if (phone != null) {
1944 return phone.getCdmaEriIconMode();
1945 } else {
1946 return -1;
1947 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001948 }
1949
1950 /**
1951 * Returns the CDMA ERI text,
1952 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001953 @Override
1954 public String getCdmaEriText(String callingPackage) {
1955 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001956 }
1957
Sanket Padawe356d7632015-06-22 14:03:32 -07001958 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001959 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1960 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1961 return null;
1962 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001963 final Phone phone = getPhone(subId);
1964 if (phone != null) {
1965 return phone.getCdmaEriText();
1966 } else {
1967 return null;
1968 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001969 }
1970
1971 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001972 * Returns the CDMA MDN.
1973 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001974 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001975 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001976 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001977 final Phone phone = getPhone(subId);
1978 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1979 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001980 } else {
1981 return null;
1982 }
1983 }
1984
1985 /**
1986 * Returns the CDMA MIN.
1987 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001988 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001989 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001990 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001991 final Phone phone = getPhone(subId);
1992 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1993 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001994 } else {
1995 return null;
1996 }
1997 }
1998
1999 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002000 * Returns true if CDMA provisioning needs to run.
2001 */
2002 public boolean needsOtaServiceProvisioning() {
2003 return mPhone.needsOtaServiceProvisioning();
2004 }
2005
2006 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002007 * Sets the voice mail number of a given subId.
2008 */
2009 @Override
2010 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002011 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002012 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2013 new Pair<String, String>(alphaTag, number), new Integer(subId));
2014 return success;
2015 }
2016
Ta-wei Yen87c49842016-05-13 21:19:52 -07002017 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07002018 public void setVisualVoicemailEnabled(String callingPackage,
2019 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
2020 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2021 if (!TextUtils.equals(callingPackage,
2022 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2023 enforceModifyPermissionOrCarrierPrivilege(
2024 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
2025 }
2026 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2027 }
2028
2029 @Override
2030 public boolean isVisualVoicemailEnabled(String callingPackage,
2031 PhoneAccountHandle phoneAccountHandle) {
2032 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2033 return false;
2034 }
2035 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2036 }
2037
2038 @Override
Ta-wei Yen409ac562017-03-06 16:00:44 -08002039 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yendca928f2017-01-10 16:17:08 -08002040 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2041 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2042 return null;
2043 }
Ta-wei Yendca928f2017-01-10 16:17:08 -08002044 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2045 }
2046
2047 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002048 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2049 VisualVoicemailSmsFilterSettings settings) {
2050 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002051 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002052 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2053 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002054 }
2055
2056 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002057 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2058 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002059 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002060 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002061 }
2062
2063 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002064 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2065 String callingPackage, int subId) {
2066 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002067 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002068 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002069 }
2070
2071 @Override
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002072 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002073 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002074 return VisualVoicemailSmsFilterConfig
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002075 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2076 }
2077
2078 @Override
2079 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2080 String number, int port, String text, PendingIntent sentIntent) {
2081 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen527a9c02017-01-06 15:29:25 -08002082 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen30a69c82016-12-27 14:52:32 -08002083 enforceSendSmsPermission();
2084 // Make the calls as the phone process.
2085 final long identity = Binder.clearCallingIdentity();
2086 try {
2087 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2088 if (port == 0) {
2089 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2090 sentIntent, null, false);
2091 } else {
2092 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2093 smsManager.sendDataMessageWithSelfPermissions(number, null,
2094 (short) port, data, sentIntent, null);
2095 }
2096 } finally {
2097 Binder.restoreCallingIdentity(identity);
2098 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002099 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002100 /**
fionaxu0152e512016-11-14 13:36:14 -08002101 * Sets the voice activation state of a given subId.
2102 */
2103 @Override
2104 public void setVoiceActivationState(int subId, int activationState) {
2105 enforceModifyPermissionOrCarrierPrivilege(subId);
2106 final Phone phone = getPhone(subId);
2107 if (phone != null) {
2108 phone.setVoiceActivationState(activationState);
2109 } else {
2110 loge("setVoiceActivationState fails with invalid subId: " + subId);
2111 }
2112 }
2113
2114 /**
2115 * Sets the data activation state of a given subId.
2116 */
2117 @Override
2118 public void setDataActivationState(int subId, int activationState) {
2119 enforceModifyPermissionOrCarrierPrivilege(subId);
2120 final Phone phone = getPhone(subId);
2121 if (phone != null) {
2122 phone.setDataActivationState(activationState);
2123 } else {
2124 loge("setVoiceActivationState fails with invalid subId: " + subId);
2125 }
2126 }
2127
2128 /**
2129 * Returns the voice activation state of a given subId.
2130 */
2131 @Override
2132 public int getVoiceActivationState(int subId, String callingPackage) {
2133 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2134 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2135 }
2136 final Phone phone = getPhone(subId);
2137 if (phone != null) {
2138 return phone.getVoiceActivationState();
2139 } else {
2140 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2141 }
2142 }
2143
2144 /**
2145 * Returns the data activation state of a given subId.
2146 */
2147 @Override
2148 public int getDataActivationState(int subId, String callingPackage) {
2149 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2150 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2151 }
2152 final Phone phone = getPhone(subId);
2153 if (phone != null) {
2154 return phone.getDataActivationState();
2155 } else {
2156 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2157 }
2158 }
2159
2160 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002161 * Returns the unread count of voicemails
2162 */
2163 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002164 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002165 }
2166
2167 /**
2168 * Returns the unread count of voicemails for a subId
2169 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002170 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002171 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002172 final Phone phone = getPhone(subId);
2173 if (phone != null) {
2174 return phone.getVoiceMessageCount();
2175 } else {
2176 return 0;
2177 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002178 }
2179
2180 /**
pkanwar8a4dcfb2017-01-19 13:43:16 -08002181 * returns true, if the device is in a state where both voice and data
2182 * are supported simultaneously. This can change based on location or network condition.
2183 */
2184 @Override
2185 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2186 final Phone phone = getPhone(subId);
2187 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2188 }
2189
2190 /**
fionaxue559f972017-01-24 22:31:12 -08002191 * Send the dialer code if called from the current default dialer and the input code follows the
2192 * format of *#*#<code>#*#*
2193 * <p>
2194 * Requires Permission:
2195 * {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
2196 *
2197 * @param inputCode The dialer code to send
2198 * @return true if successfully sent, false otherwise
2199 */
2200 @Override
2201 public boolean sendDialerCode(String callingPackage, String inputCode) {
2202 enforceModifyPermission();
2203 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2204 if (TextUtils.equals(callingPackage,
2205 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2206 final Phone phone = getPhone(getDefaultSubscription());
2207 if (phone != null) {
2208 return phone.sendDialerCode(inputCode);
2209 }
2210 }
2211 return false;
2212 }
2213
2214 /**
fionaxu235cc5e2017-03-06 22:25:57 -08002215 * Send the dialer code if called from the current default dialer or the caller has
2216 * carrier privilege.
2217 * @param inputCode The dialer code to send
2218 */
2219 @Override
2220 public void sendDialerSpecialCode(String callingPackage, String inputCode) {
2221 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2222 String defaultDialer = TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage();
2223 if (!TextUtils.equals(callingPackage, defaultDialer)) {
2224 enforceCarrierPrivilege(getDefaultSubscription());
2225 }
2226 mPhone.sendDialerSpecialCode(inputCode);
2227 }
2228
2229 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002230 * Returns the data network type.
2231 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002232 *
2233 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2234 */
2235 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002236 public int getNetworkType() {
2237 final Phone phone = getPhone(getDefaultSubscription());
2238 if (phone != null) {
2239 return phone.getServiceState().getDataNetworkType();
2240 } else {
2241 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2242 }
Wink Saville36469e72014-06-11 15:17:00 -07002243 }
2244
2245 /**
2246 * Returns the network type for a subId
2247 */
2248 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002249 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2250 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2251 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2252 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002253
Sanket Padawe356d7632015-06-22 14:03:32 -07002254 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002255 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002256 return phone.getServiceState().getDataNetworkType();
2257 } else {
2258 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002260 }
2261
2262 /**
2263 * Returns the data network type
2264 */
2265 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002266 public int getDataNetworkType(String callingPackage) {
2267 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002268 }
2269
2270 /**
2271 * Returns the data network type for a subId
2272 */
2273 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002274 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2275 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2276 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2277 }
2278
Sanket Padawe356d7632015-06-22 14:03:32 -07002279 final Phone phone = getPhone(subId);
2280 if (phone != null) {
2281 return phone.getServiceState().getDataNetworkType();
2282 } else {
2283 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2284 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002285 }
2286
2287 /**
Wink Saville36469e72014-06-11 15:17:00 -07002288 * Returns the Voice network type for a subId
2289 */
2290 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002291 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2292 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2293 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2294 }
2295
Sanket Padawe356d7632015-06-22 14:03:32 -07002296 final Phone phone = getPhone(subId);
2297 if (phone != null) {
2298 return phone.getServiceState().getVoiceNetworkType();
2299 } else {
2300 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2301 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002302 }
2303
2304 /**
2305 * @return true if a ICC card is present
2306 */
2307 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002308 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002309 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2310 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002311 }
2312
2313 /**
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002314 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002315 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002316 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002317 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2318 final Phone phone = PhoneFactory.getPhone(slotIndex);
fionaxu6e6c68b2016-06-23 13:31:32 -07002319 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002320 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002321 } else {
2322 return false;
2323 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002324 }
2325
2326 /**
2327 * Return if the current radio is LTE on CDMA. This
2328 * is a tri-state return value as for a period of time
2329 * the mode may be unknown.
2330 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002331 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002332 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002333 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002334 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002335 @Override
2336 public int getLteOnCdmaMode(String callingPackage) {
2337 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002338 }
2339
Sanket Padawe356d7632015-06-22 14:03:32 -07002340 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002341 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2342 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2343 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2344 }
2345
Sanket Padawe356d7632015-06-22 14:03:32 -07002346 final Phone phone = getPhone(subId);
2347 if (phone == null) {
2348 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2349 } else {
2350 return phone.getLteOnCdmaMode();
2351 }
Wink Saville36469e72014-06-11 15:17:00 -07002352 }
2353
2354 public void setPhone(Phone phone) {
2355 mPhone = phone;
2356 }
2357
2358 /**
2359 * {@hide}
2360 * Returns Default subId, 0 in the case of single standby.
2361 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002362 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002363 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002364 }
2365
Shishir Agrawala9f32182016-04-12 12:00:16 -07002366 private int getSlotForDefaultSubscription() {
2367 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2368 }
2369
Wink Savilleb564aae2014-10-23 10:18:09 -07002370 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002371 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002372 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002373
2374 /**
2375 * @see android.telephony.TelephonyManager.WifiCallingChoices
2376 */
2377 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002378 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2379 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002380 }
2381
2382 /**
2383 * @see android.telephony.TelephonyManager.WifiCallingChoices
2384 */
2385 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002386 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2387 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2388 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002389 }
2390
Sailesh Nepald1e68152013-12-12 19:08:02 -08002391 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002392 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002393 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002394 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002395
Shishir Agrawal566b7612013-10-28 14:41:00 -07002396 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002397 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2398 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002399
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002400 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002401 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002402 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002403 if (DBG) log("iccOpenLogicalChannel: " + response);
2404 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002405 }
2406
2407 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002408 public boolean iccCloseLogicalChannel(int subId, int channel) {
2409 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002410
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002411 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002412 if (channel < 0) {
2413 return false;
2414 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002415 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002416 if (DBG) log("iccCloseLogicalChannel: " + success);
2417 return success;
2418 }
2419
2420 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002421 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002422 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002423 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002424
2425 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002426 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2427 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002428 " data=" + data);
2429 }
2430
2431 if (channel < 0) {
2432 return "";
2433 }
2434
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002435 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002436 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002437 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2438
Shishir Agrawal566b7612013-10-28 14:41:00 -07002439 // Append the returned status code to the end of the response payload.
2440 String s = Integer.toHexString(
2441 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002442 if (response.payload != null) {
2443 s = IccUtils.bytesToHexString(response.payload) + s;
2444 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002445 return s;
2446 }
Jake Hambye994d462014-02-03 13:10:13 -08002447
Evan Charltonc66da362014-05-16 14:06:40 -07002448 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002449 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002450 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002451 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002452
2453 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002454 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2455 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002456 }
2457
2458 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002459 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002460 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2461
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002462 // Append the returned status code to the end of the response payload.
2463 String s = Integer.toHexString(
2464 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002465 if (response.payload != null) {
2466 s = IccUtils.bytesToHexString(response.payload) + s;
2467 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002468 return s;
2469 }
2470
2471 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002472 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002473 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002474 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002475
2476 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002477 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002478 p1 + " " + p2 + " " + p3 + ":" + filePath);
2479 }
2480
2481 IccIoResult response =
2482 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002483 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2484 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002485
2486 if (DBG) {
2487 log("Exchange SIM_IO [R]" + response);
2488 }
2489
2490 byte[] result = null;
2491 int length = 2;
2492 if (response.payload != null) {
2493 length = 2 + response.payload.length;
2494 result = new byte[length];
2495 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2496 } else {
2497 result = new byte[length];
2498 }
2499
2500 result[length - 1] = (byte) response.sw2;
2501 result[length - 2] = (byte) response.sw1;
2502 return result;
2503 }
2504
Nathan Haroldb3014052017-01-25 15:57:32 -08002505 /**
2506 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2507 * on a particular subscription
2508 */
2509 public String[] getForbiddenPlmns(int subId, int appType) {
2510 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
2511 "Requires READ_PRIVILEGED_PHONE_STATE");
2512 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2513 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2514 return null;
2515 }
2516 Object response = sendRequest(
2517 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2518 if (response instanceof String[]) {
2519 return (String[]) response;
2520 }
2521 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2522 return null;
2523 }
2524
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002525 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002526 public String sendEnvelopeWithStatus(int subId, String content) {
2527 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002528
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002529 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002530 if (response.payload == null) {
2531 return "";
2532 }
2533
2534 // Append the returned status code to the end of the response payload.
2535 String s = Integer.toHexString(
2536 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2537 s = IccUtils.bytesToHexString(response.payload) + s;
2538 return s;
2539 }
2540
Jake Hambye994d462014-02-03 13:10:13 -08002541 /**
2542 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2543 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2544 *
2545 * @param itemID the ID of the item to read
2546 * @return the NV item as a String, or null on error.
2547 */
2548 @Override
2549 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002550 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002551 if (DBG) log("nvReadItem: item " + itemID);
2552 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2553 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2554 return value;
2555 }
2556
2557 /**
2558 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2559 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2560 *
2561 * @param itemID the ID of the item to read
2562 * @param itemValue the value to write, as a String
2563 * @return true on success; false on any failure
2564 */
2565 @Override
2566 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002567 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002568 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2569 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2570 new Pair<Integer, String>(itemID, itemValue));
2571 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2572 return success;
2573 }
2574
2575 /**
2576 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2577 * Used for device configuration by some CDMA operators.
2578 *
2579 * @param preferredRoamingList byte array containing the new PRL
2580 * @return true on success; false on any failure
2581 */
2582 @Override
2583 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002584 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002585 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2586 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2587 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2588 return success;
2589 }
2590
2591 /**
2592 * Perform the specified type of NV config reset.
2593 * Used for device configuration by some CDMA operators.
2594 *
2595 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2596 * @return true on success; false on any failure
2597 */
2598 @Override
2599 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002600 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002601 if (DBG) log("nvResetConfig: type " + resetType);
2602 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2603 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2604 return success;
2605 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002606
2607 /**
Wink Saville36469e72014-06-11 15:17:00 -07002608 * {@hide}
2609 * Returns Default sim, 0 in the case of single standby.
2610 */
2611 public int getDefaultSim() {
2612 //TODO Need to get it from Telephony Devcontroller
2613 return 0;
2614 }
2615
Svet Ganovb320e182015-04-16 12:30:10 -07002616 public String[] getPcscfAddress(String apnType, String callingPackage) {
2617 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2618 return new String[0];
2619 }
2620
2621
ram87fca6f2014-07-18 18:58:44 +05302622 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002623 }
2624
Brad Ebinger51f743a2017-01-23 13:50:20 -08002625 /**
2626 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2627 * feature or {@link null} if the service is not available. If an ImsServiceController is
2628 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2629 * feature updates.
2630 */
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002631 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002632 IImsServiceFeatureListener callback) {
2633 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07002634 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger51f743a2017-01-23 13:50:20 -08002635 callback);
2636 }
2637
Wink Saville36469e72014-06-11 15:17:00 -07002638 public void setImsRegistrationState(boolean registered) {
2639 enforceModifyPermission();
2640 mPhone.setImsRegistrationState(registered);
2641 }
2642
2643 /**
Stuart Scott54788802015-03-30 13:18:01 -07002644 * Set the network selection mode to automatic.
2645 *
2646 */
2647 @Override
2648 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002649 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002650 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2651 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2652 }
2653
2654 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002655 * Set the network selection mode to manual with the selected carrier.
2656 */
2657 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002658 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2659 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002660 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002661 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002662 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2663 persistSelection);
2664 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002665 }
2666
2667 /**
2668 * Scans for available networks.
2669 */
2670 @Override
2671 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002672 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002673 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2674 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2675 CMD_PERFORM_NETWORK_SCAN, null, subId);
2676 return result;
2677 }
2678
2679 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002680 * Get the calculated preferred network type.
2681 * Used for debugging incorrect network type.
2682 *
2683 * @return the preferred network type, defined in RILConstants.java.
2684 */
2685 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002686 public int getCalculatedPreferredNetworkType(String callingPackage) {
2687 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2688 return RILConstants.PREFERRED_NETWORK_MODE;
2689 }
2690
Amit Mahajan43330e02014-11-18 11:54:45 -08002691 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002692 }
2693
2694 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002695 * Get the preferred network type.
2696 * Used for device configuration by some CDMA operators.
2697 *
2698 * @return the preferred network type, defined in RILConstants.java.
2699 */
2700 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002701 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002702 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002703 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002704 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002705 int networkType = (result != null ? result[0] : -1);
2706 if (DBG) log("getPreferredNetworkType: " + networkType);
2707 return networkType;
2708 }
2709
2710 /**
2711 * Set the preferred network type.
2712 * Used for device configuration by some CDMA operators.
2713 *
2714 * @param networkType the preferred network type, defined in RILConstants.java.
2715 * @return true on success; false on any failure.
2716 */
2717 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002718 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002719 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002720 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2721 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002722 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002723 if (success) {
2724 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002725 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002726 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002727 return success;
2728 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002729
2730 /**
Junda Liu475951f2014-11-07 16:45:03 -08002731 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2732 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2733 * tethering.
2734 *
2735 * @return 0: Not required. 1: required. 2: Not set.
2736 * @hide
2737 */
2738 @Override
2739 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002740 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002741 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2742 Settings.Global.TETHER_DUN_REQUIRED, 2);
2743 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2744 // config_tether_apndata.
2745 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2746 dunRequired = 1;
2747 }
2748 return dunRequired;
2749 }
2750
2751 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002752 * Set mobile data enabled
2753 * Used by the user through settings etc to turn on/off mobile data
2754 *
2755 * @param enable {@code true} turn turn data on, else {@code false}
2756 */
2757 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002758 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002759 enforceModifyPermissionOrCarrierPrivilege(subId);
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) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002764 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002765 phone.setDataEnabled(enable);
2766 } else {
2767 loge("setDataEnabled: no phone for subId=" + subId);
2768 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002769 }
2770
2771 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002772 * Get whether mobile data is enabled.
2773 *
Jeff Davidsona1920712016-11-18 17:05:56 -08002774 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002775 *
2776 * @return {@code true} if data is enabled else {@code false}
2777 */
2778 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002779 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002780 try {
2781 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2782 null);
2783 } catch (Exception e) {
Jeff Davidsona1920712016-11-18 17:05:56 -08002784 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002785 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002786 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002787 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002788 Phone phone = PhoneFactory.getPhone(phoneId);
2789 if (phone != null) {
2790 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002791 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002792 return retVal;
2793 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002794 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002795 return false;
2796 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002797 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002798
2799 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002800 public int getCarrierPrivilegeStatus(int subId) {
2801 final Phone phone = getPhone(subId);
2802 if (phone == null) {
2803 loge("getCarrierPrivilegeStatus: Invalid subId");
2804 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2805 }
2806 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002807 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002808 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002809 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2810 }
2811 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002812 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002813 }
Junda Liu29340342014-07-10 15:23:27 -07002814
2815 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002816 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002817 if (TextUtils.isEmpty(pkgName))
2818 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002819 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002820 if (card == null) {
2821 loge("checkCarrierPrivilegesForPackage: No UICC");
2822 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2823 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002824 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2825 }
2826
2827 @Override
2828 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002829 if (TextUtils.isEmpty(pkgName))
2830 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002831 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2832 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2833 UiccCard card = UiccController.getInstance().getUiccCard(i);
2834 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002835 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002836 continue;
2837 }
2838
2839 result = card.getCarrierPrivilegeStatus(
2840 mPhone.getContext().getPackageManager(), pkgName);
2841 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2842 break;
2843 }
2844 }
2845
2846 return result;
Junda Liu29340342014-07-10 15:23:27 -07002847 }
Derek Tan89e89d42014-07-08 17:00:10 -07002848
2849 @Override
Junda Liue64de782015-04-16 17:19:16 -07002850 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2851 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2852 loge("phoneId " + phoneId + " is not valid.");
2853 return null;
2854 }
2855 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002856 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002857 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002858 return null ;
2859 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002860 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002861 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002862 }
2863
Amith Yamasani6e118872016-02-19 12:53:51 -08002864 @Override
2865 public List<String> getPackagesWithCarrierPrivileges() {
2866 PackageManager pm = mPhone.getContext().getPackageManager();
2867 List<String> privilegedPackages = new ArrayList<>();
2868 List<PackageInfo> packages = null;
2869 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2870 UiccCard card = UiccController.getInstance().getUiccCard(i);
2871 if (card == null) {
2872 // No UICC in that slot.
2873 continue;
2874 }
2875 if (card.hasCarrierPrivilegeRules()) {
2876 if (packages == null) {
2877 // Only check packages in user 0 for now
2878 packages = pm.getInstalledPackagesAsUser(
2879 PackageManager.MATCH_DISABLED_COMPONENTS
2880 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2881 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2882 }
2883 for (int p = packages.size() - 1; p >= 0; p--) {
2884 PackageInfo pkgInfo = packages.get(p);
2885 if (pkgInfo != null && pkgInfo.packageName != null
2886 && card.getCarrierPrivilegeStatus(pkgInfo)
2887 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2888 privilegedPackages.add(pkgInfo.packageName);
2889 }
2890 }
2891 }
2892 }
2893 return privilegedPackages;
2894 }
2895
Wink Savilleb564aae2014-10-23 10:18:09 -07002896 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002897 final Phone phone = getPhone(subId);
2898 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002899 if (card == null) {
2900 loge("getIccId: No UICC");
2901 return null;
2902 }
2903 String iccId = card.getIccId();
2904 if (TextUtils.isEmpty(iccId)) {
2905 loge("getIccId: ICC ID is null or empty.");
2906 return null;
2907 }
2908 return iccId;
2909 }
2910
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002911 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002912 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2913 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002914 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002915
Jeff Sharkey85190e62014-12-05 09:40:12 -08002916 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002917 final Phone phone = getPhone(subId);
2918 if (phone == null) {
2919 return false;
2920 }
2921 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002922
2923 if (DBG_MERGE) {
2924 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2925 + subscriberId + " to " + number);
2926 }
2927
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002928 if (TextUtils.isEmpty(iccId)) {
2929 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002930 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002931
Jeff Sharkey85190e62014-12-05 09:40:12 -08002932 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2933
2934 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002935 if (alphaTag == null) {
2936 editor.remove(alphaTagPrefKey);
2937 } else {
2938 editor.putString(alphaTagPrefKey, alphaTag);
2939 }
2940
Jeff Sharkey85190e62014-12-05 09:40:12 -08002941 // Record both the line number and IMSI for this ICCID, since we need to
2942 // track all merged IMSIs based on line number
2943 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2944 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002945 if (number == null) {
2946 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002947 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002948 } else {
2949 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002950 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002951 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002952
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002953 editor.commit();
2954 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002955 }
2956
2957 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002958 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002959 // This is open to apps with WRITE_SMS.
2960 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002961 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002962 return null;
2963 }
Derek Tan97ebb422014-09-05 16:55:38 -07002964
2965 String iccId = getIccId(subId);
2966 if (iccId != null) {
2967 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002968 if (DBG_MERGE) {
2969 log("getLine1NumberForDisplay returning " +
2970 mTelephonySharedPreferences.getString(numberPrefKey, null));
2971 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002972 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002973 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002974 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002975 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002976 }
2977
2978 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002979 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2980 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2981 return null;
2982 }
Derek Tan97ebb422014-09-05 16:55:38 -07002983
2984 String iccId = getIccId(subId);
2985 if (iccId != null) {
2986 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002987 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002988 }
Derek Tan97ebb422014-09-05 16:55:38 -07002989 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002990 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002991
2992 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002993 public String[] getMergedSubscriberIds(String callingPackage) {
2994 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2995 return null;
2996 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002997 final Context context = mPhone.getContext();
2998 final TelephonyManager tele = TelephonyManager.from(context);
2999 final SubscriptionManager sub = SubscriptionManager.from(context);
3000
3001 // Figure out what subscribers are currently active
3002 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07003003 // Clear calling identity, when calling TelephonyManager, because callerUid must be
3004 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
3005 final long identity = Binder.clearCallingIdentity();
3006 try {
3007 final int[] subIds = sub.getActiveSubscriptionIdList();
3008 for (int subId : subIds) {
3009 activeSubscriberIds.add(tele.getSubscriberId(subId));
3010 }
3011 } finally {
3012 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08003013 }
3014
3015 // First pass, find a number override for an active subscriber
3016 String mergeNumber = null;
3017 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
3018 for (String key : prefs.keySet()) {
3019 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
3020 final String subscriberId = (String) prefs.get(key);
3021 if (activeSubscriberIds.contains(subscriberId)) {
3022 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
3023 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3024 mergeNumber = (String) prefs.get(numberKey);
3025 if (DBG_MERGE) {
3026 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3027 + " for active subscriber " + subscriberId);
3028 }
3029 if (!TextUtils.isEmpty(mergeNumber)) {
3030 break;
3031 }
3032 }
3033 }
3034 }
3035
3036 // Shortcut when no active merged subscribers
3037 if (TextUtils.isEmpty(mergeNumber)) {
3038 return null;
3039 }
3040
3041 // Second pass, find all subscribers under that line override
3042 final ArraySet<String> result = new ArraySet<>();
3043 for (String key : prefs.keySet()) {
3044 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3045 final String number = (String) prefs.get(key);
3046 if (mergeNumber.equals(number)) {
3047 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3048 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3049 final String subscriberId = (String) prefs.get(subscriberKey);
3050 if (!TextUtils.isEmpty(subscriberId)) {
3051 result.add(subscriberId);
3052 }
3053 }
3054 }
3055 }
3056
3057 final String[] resultArray = result.toArray(new String[result.size()]);
3058 Arrays.sort(resultArray);
3059 if (DBG_MERGE) {
3060 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3061 }
3062 return resultArray;
3063 }
3064
3065 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003066 public boolean setOperatorBrandOverride(int subId, String brand) {
3067 enforceCarrierPrivilege(subId);
3068 final Phone phone = getPhone(subId);
3069 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003070 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003071
3072 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003073 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003074 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3075 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003076 enforceCarrierPrivilege(subId);
3077 final Phone phone = getPhone(subId);
3078 if (phone == null) {
3079 return false;
3080 }
3081 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003082 cdmaNonRoamingList);
3083 }
3084
3085 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003086 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003087 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3088 enforceModifyPermission();
3089
3090 int returnValue = 0;
3091 try {
3092 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3093 if(result.exception == null) {
3094 if (result.result != null) {
3095 byte[] responseData = (byte[])(result.result);
3096 if(responseData.length > oemResp.length) {
3097 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3098 responseData.length + "bytes. Buffer Size is " +
3099 oemResp.length + "bytes.");
3100 }
3101 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3102 returnValue = responseData.length;
3103 }
3104 } else {
3105 CommandException ex = (CommandException) result.exception;
3106 returnValue = ex.getCommandError().ordinal();
3107 if(returnValue > 0) returnValue *= -1;
3108 }
3109 } catch (RuntimeException e) {
3110 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3111 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3112 if(returnValue > 0) returnValue *= -1;
3113 }
3114
3115 return returnValue;
3116 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003117
3118 @Override
3119 public void setRadioCapability(RadioAccessFamily[] rafs) {
3120 try {
3121 ProxyController.getInstance().setRadioCapability(rafs);
3122 } catch (RuntimeException e) {
3123 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3124 }
3125 }
3126
3127 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003128 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3129 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3130 return RadioAccessFamily.RAF_UNKNOWN;
3131 }
3132
Wink Saville5d475dd2014-10-17 15:00:58 -07003133 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3134 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003135
3136 @Override
3137 public void enableVideoCalling(boolean enable) {
3138 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003139 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003140 }
3141
3142 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003143 public boolean isVideoCallingEnabled(String callingPackage) {
3144 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3145 return false;
3146 }
3147
Andrew Lee77527ac2014-10-21 16:57:39 -07003148 // Check the user preference and the system-level IMS setting. Even if the user has
3149 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3150 // In the long run, we may instead need to check if there exists a connection service
3151 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003152 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3153 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003154 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003155 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003156
Andrew Leea1239f22015-03-02 17:44:07 -08003157 @Override
3158 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003159 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003160 }
3161
3162 @Override
3163 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003164 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003165 }
3166
Andrew Lee9431b832015-03-09 18:46:45 -07003167 @Override
3168 public boolean isTtyModeSupported() {
3169 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3170 TelephonyManager telephonyManager =
3171 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003172 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003173 }
3174
3175 @Override
3176 public boolean isHearingAidCompatibilitySupported() {
3177 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3178 }
3179
Sanket Padawe7310cc72015-01-14 09:53:20 -08003180 /**
3181 * Returns the unique device ID of phone, for example, the IMEI for
3182 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3183 *
3184 * <p>Requires Permission:
3185 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3186 */
3187 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003188 public String getDeviceId(String callingPackage) {
3189 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3190 return null;
3191 }
3192
Sanket Padawe7310cc72015-01-14 09:53:20 -08003193 final Phone phone = PhoneFactory.getPhone(0);
3194 if (phone != null) {
3195 return phone.getDeviceId();
3196 } else {
3197 return null;
3198 }
3199 }
3200
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003201 /*
3202 * {@hide}
3203 * Returns the IMS Registration Status
3204 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003205 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003206 public boolean isImsRegistered() {
3207 return mPhone.isImsRegistered();
3208 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003209
3210 @Override
3211 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3212 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3213 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003214
Nathan Haroldc55097a2015-03-11 18:14:50 -07003215 /*
3216 * {@hide}
3217 * Returns the IMS Registration Status
3218 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003219 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003220 return mPhone.isWifiCallingEnabled();
3221 }
3222
3223 /*
3224 * {@hide}
3225 * Returns the IMS Registration Status
3226 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003227 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003228 return mPhone.isVolteEnabled();
3229 }
Svet Ganovb320e182015-04-16 12:30:10 -07003230
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003231 /*
3232 * {@hide} Returns the IMS Registration Status
3233 */
3234 public boolean isVideoTelephonyAvailable() {
3235 return mPhone.isVideoEnabled();
3236 }
3237
Svet Ganovb320e182015-04-16 12:30:10 -07003238 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003239 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003240 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003241 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3242
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003243 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003244 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003245 } catch (SecurityException e) {
3246 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3247 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003248 }
Svet Ganovb320e182015-04-16 12:30:10 -07003249
3250 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3251 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3252 return false;
3253 }
3254
3255 return true;
3256 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003257
Makoto Onukifee69342015-06-29 14:44:50 -07003258 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003259 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003260 */
3261 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003262 // Default SMS app can always read it.
3263 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3264 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3265 return true;
3266 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003267
Makoto Onukie4072d12015-08-03 15:12:23 -07003268 try {
3269 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003270 } catch (SecurityException readPhoneStateSecurityException) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003271 }
Chad Brubaker7a8fba22016-11-10 11:23:18 -08003272 // Can be read with READ_SMS too.
3273 try {
3274 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3275 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_SMS);
3276 if (opCode != AppOpsManager.OP_NONE) {
3277 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3278 == AppOpsManager.MODE_ALLOWED;
3279 } else {
3280 return true;
3281 }
3282 } catch (SecurityException readSmsSecurityException) {
3283 }
3284 // Can be read with READ_PHONE_NUMBER too.
3285 try {
3286 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_NUMBER,
3287 message);
3288 int opCode = mAppOps.permissionToOpCode(android.Manifest.permission.READ_PHONE_NUMBER);
3289 if (opCode != AppOpsManager.OP_NONE) {
3290 return mAppOps.noteOp(opCode, Binder.getCallingUid(), callingPackage)
3291 == AppOpsManager.MODE_ALLOWED;
3292 } else {
3293 return true;
3294 }
3295 } catch (SecurityException readPhoneNumberSecurityException) {
3296 }
3297
3298 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3299 " nor current process has" + android.Manifest.permission.READ_PHONE_STATE +
3300 ", " + android.Manifest.permission.READ_SMS + ", or " +
3301 android.Manifest.permission.READ_PHONE_NUMBER);
Makoto Onukifee69342015-06-29 14:44:50 -07003302 }
3303
Stuart Scott8eef64f2015-04-08 15:13:54 -07003304 @Override
3305 public void factoryReset(int subId) {
3306 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003307 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3308 return;
3309 }
3310
Svet Ganovcc087f82015-05-12 20:35:54 -07003311 final long identity = Binder.clearCallingIdentity();
3312 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003313 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3314 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003315 // Enable data
3316 setDataEnabled(subId, true);
3317 // Set network selection mode to automatic
3318 setNetworkSelectionModeAutomatic(subId);
3319 // Set preferred mobile network type to the best available
3320 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3321 // Turn off roaming
3322 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3323 }
3324 } finally {
3325 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003326 }
3327 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003328
3329 @Override
3330 public String getLocaleFromDefaultSim() {
3331 // We query all subscriptions instead of just the active ones, because
3332 // this might be called early on in the provisioning flow when the
3333 // subscriptions potentially aren't active yet.
3334 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3335 if (slist == null || slist.isEmpty()) {
3336 return null;
3337 }
3338
3339 // This function may be called very early, say, from the setup wizard, at
3340 // which point we won't have a default subscription set. If that's the case
3341 // we just choose the first, which will be valid in "most cases".
3342 final int defaultSubId = getDefaultSubscription();
3343 SubscriptionInfo info = null;
3344 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3345 info = slist.get(0);
3346 } else {
3347 for (SubscriptionInfo item : slist) {
3348 if (item.getSubscriptionId() == defaultSubId) {
3349 info = item;
3350 break;
3351 }
3352 }
3353
3354 if (info == null) {
3355 return null;
3356 }
3357 }
3358
3359 // Try and fetch the locale from the carrier properties or from the SIM language
3360 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003361 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003362 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003363 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003364 if (defaultPhone != null) {
3365 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3366 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003367 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003368 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3369 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003370 } else {
3371 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003372 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003373 }
3374 }
3375
Narayan Kamath011676f2015-07-29 12:04:08 +01003376 // The SIM language preferences only store a language (e.g. fr = French), not an
3377 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3378 // the SIM and carrier preferences does not include a country we add the country
3379 // determined from the SIM MCC to provide an exact locale.
3380 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003381 if (mccLocale != null) {
3382 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3383 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003384 }
3385
Tony Hill183b2de2015-06-24 14:53:58 +01003386 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003387 return null;
3388 }
3389
3390 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3391 final long identity = Binder.clearCallingIdentity();
3392 try {
3393 return mSubscriptionController.getAllSubInfoList(
3394 mPhone.getContext().getOpPackageName());
3395 } finally {
3396 Binder.restoreCallingIdentity(identity);
3397 }
3398 }
3399
3400 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3401 final long identity = Binder.clearCallingIdentity();
3402 try {
3403 return mSubscriptionController.getActiveSubscriptionInfoList(
3404 mPhone.getContext().getOpPackageName());
3405 } finally {
3406 Binder.restoreCallingIdentity(identity);
3407 }
3408 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003409
3410 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003411 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3412 * representing the state of the modem.
3413 *
3414 * NOTE: This clears the modem state, so there should only every be one caller.
3415 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003416 */
3417 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003418 public void requestModemActivityInfo(ResultReceiver result) {
3419 enforceModifyPermission();
3420
3421 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3422 Bundle bundle = new Bundle();
3423 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3424 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003425 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003426
3427 /**
3428 * {@hide}
3429 * Returns the service state information on specified subscription.
3430 */
3431 @Override
3432 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3433
3434 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3435 return null;
3436 }
3437
3438 final Phone phone = getPhone(subId);
3439 if (phone == null) {
3440 return null;
3441 }
3442
3443 return phone.getServiceState();
3444 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003445
3446 /**
3447 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3448 *
3449 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3450 * voicemail ringtone.
3451 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3452 * PhoneAccount.
3453 */
3454 @Override
3455 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3456 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3457 if (phone == null) {
3458 return null;
3459 }
3460
3461 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3462 }
3463
3464 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003465 * Sets the per-account voicemail ringtone.
3466 *
3467 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3468 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3469 *
3470 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3471 * voicemail ringtone.
3472 * @param uri The URI for the ringtone to play when receiving a voicemail from a specific
3473 * PhoneAccount.
3474 */
3475 @Override
3476 public void setVoicemailRingtoneUri(String callingPackage,
3477 PhoneAccountHandle phoneAccountHandle, Uri uri) {
3478 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3479 if (!TextUtils.equals(callingPackage,
3480 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3481 enforceModifyPermissionOrCarrierPrivilege(
3482 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3483 }
3484 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3485 if (phone == null){
3486 throw new IllegalArgumentException("The phoneAccountHandle does not correspond to an "
3487 + "active phone account.");
3488 }
3489 VoicemailNotificationSettingsUtil.setRingtoneUri(phone, uri);
3490 }
3491
3492 /**
Nancy Chen31f9ba12016-01-06 11:42:12 -08003493 * Returns whether vibration is set for voicemail notification in Phone settings.
3494 *
3495 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3496 * voicemail vibration setting.
3497 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3498 */
3499 @Override
3500 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3501 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3502 if (phone == null) {
3503 return false;
3504 }
3505
3506 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3507 }
3508
Youhan Wange64578a2016-05-02 15:32:42 -07003509 /**
Ta-wei Yenc33877d2017-01-23 18:11:21 -08003510 * Sets the per-account voicemail vibration.
3511 *
3512 * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
3513 * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
3514 *
3515 * @param phoneAccountHandle The handle for the {@link PhoneAccount} for which to set the
3516 * voicemail vibration setting.
3517 * @param enabled Whether to enable or disable vibration for voicemail notifications from a
3518 * specific PhoneAccount.
3519 */
3520 @Override
3521 public void setVoicemailVibrationEnabled(String callingPackage,
3522 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
3523 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
3524 if (!TextUtils.equals(callingPackage,
3525 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
3526 enforceModifyPermissionOrCarrierPrivilege(
3527 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
3528 }
3529
3530 Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(phoneAccountHandle);
3531 if (phone == null){
3532 throw new IllegalArgumentException("The phoneAccountHandle does not correspond to an "
3533 + "active phone account.");
3534 }
3535 VoicemailNotificationSettingsUtil.setVibrationEnabled(phone, enabled);
3536 }
3537
3538 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003539 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3540 *
3541 * @throws SecurityException if the caller does not have the required permission
3542 */
3543 private void enforceReadPrivilegedPermission() {
3544 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3545 null);
3546 }
3547
3548 /**
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003549 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3550 * permission.
3551 *
3552 * @throws SecurityException if the caller does not have the required permission
3553 */
3554 private void enforceSendSmsPermission() {
3555 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3556 }
3557
3558 /**
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003559 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003560 *
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003561 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003562 */
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003563 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
Ta-wei Yen5d81b0c2017-03-03 16:32:26 -08003564 ComponentName componentName =
3565 RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId);
3566 if(componentName == null) {
3567 throw new SecurityException("Caller not current active visual voicemail package[null]");
3568 }
3569 String vvmPackage = componentName.getPackageName();
Ta-wei Yen527a9c02017-01-06 15:29:25 -08003570 if (!callingPackage.equals(vvmPackage)) {
3571 throw new SecurityException("Caller not current active visual voicemail package[" +
3572 vvmPackage + "]");
Ta-wei Yen30a69c82016-12-27 14:52:32 -08003573 }
3574 }
3575
3576 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003577 * Return the application ID for the app type.
3578 *
3579 * @param subId the subscription ID that this request applies to.
3580 * @param appType the uicc app type.
3581 * @return Application ID for specificied app type, or null if no uicc.
3582 */
3583 @Override
3584 public String getAidForAppType(int subId, int appType) {
3585 enforceReadPrivilegedPermission();
3586 Phone phone = getPhone(subId);
3587 if (phone == null) {
3588 return null;
3589 }
3590 String aid = null;
3591 try {
3592 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3593 .getApplicationByType(appType).getAid();
3594 } catch (Exception e) {
3595 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3596 }
3597 return aid;
3598 }
3599
Youhan Wang4001d252016-05-11 10:29:41 -07003600 /**
3601 * Return the Electronic Serial Number.
3602 *
3603 * @param subId the subscription ID that this request applies to.
3604 * @return ESN or null if error.
3605 */
3606 @Override
3607 public String getEsn(int subId) {
3608 enforceReadPrivilegedPermission();
3609 Phone phone = getPhone(subId);
3610 if (phone == null) {
3611 return null;
3612 }
3613 String esn = null;
3614 try {
3615 esn = phone.getEsn();
3616 } catch (Exception e) {
3617 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3618 }
3619 return esn;
3620 }
3621
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003622 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003623 * Return the Preferred Roaming List Version.
3624 *
3625 * @param subId the subscription ID that this request applies to.
3626 * @return PRLVersion or null if error.
3627 */
3628 @Override
3629 public String getCdmaPrlVersion(int subId) {
3630 enforceReadPrivilegedPermission();
3631 Phone phone = getPhone(subId);
3632 if (phone == null) {
3633 return null;
3634 }
3635 String cdmaPrlVersion = null;
3636 try {
3637 cdmaPrlVersion = phone.getCdmaPrlVersion();
3638 } catch (Exception e) {
3639 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3640 }
3641 return cdmaPrlVersion;
3642 }
3643
3644 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003645 * Get snapshot of Telephony histograms
3646 * @return List of Telephony histograms
3647 * @hide
3648 */
3649 @Override
3650 public List<TelephonyHistogram> getTelephonyHistograms() {
3651 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3652 return RIL.getTelephonyRILTimingHistograms();
3653 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003654
3655 /**
3656 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003657 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003658 * Require system privileges. In the future we may add this to carrier APIs.
3659 *
3660 * @return The number of carriers set successfully, should match length of carriers
3661 */
3662 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003663 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003664 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003665
Meng Wang9b7c4e92017-02-17 11:41:27 -08003666 if (carriers == null) {
3667 throw new NullPointerException("carriers cannot be null");
3668 }
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003669
3670 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003671 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3672 return retVal[0];
3673 }
3674
3675 /**
3676 * {@hide}
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003677 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003678 * Require system privileges. In the future we may add this to carrier APIs.
3679 *
3680 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3681 * means all carriers are allowed.
3682 */
3683 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003684 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003685 enforceReadPrivilegedPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003686 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003687 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3688 }
3689
fionaxu59545b42016-05-25 15:53:37 -07003690 /**
3691 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3692 * @param subId the subscription ID that this action applies to.
3693 * @param enabled control enable or disable metered apns.
3694 * {@hide}
3695 */
3696 @Override
3697 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3698 enforceModifyPermission();
3699 final Phone phone = getPhone(subId);
3700 if (phone == null) {
3701 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3702 return;
3703 }
3704 try {
3705 phone.carrierActionSetMeteredApnsEnabled(enabled);
3706 } catch (Exception e) {
3707 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3708 }
3709 }
3710
3711 /**
3712 * Action set from carrier signalling broadcast receivers to enable/disable radio
3713 * @param subId the subscription ID that this action applies to.
3714 * @param enabled control enable or disable radio.
3715 * {@hide}
3716 */
3717 @Override
3718 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3719 enforceModifyPermission();
3720 final Phone phone = getPhone(subId);
3721 if (phone == null) {
3722 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3723 return;
3724 }
3725 try {
3726 phone.carrierActionSetRadioEnabled(enabled);
3727 } catch (Exception e) {
3728 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3729 }
3730 }
3731
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003732 /**
3733 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3734 * bug report is being generated.
3735 */
3736 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003737 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003738 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3739 != PackageManager.PERMISSION_GRANTED) {
3740 writer.println("Permission Denial: can't dump Phone from pid="
3741 + Binder.getCallingPid()
3742 + ", uid=" + Binder.getCallingUid()
3743 + "without permission "
3744 + android.Manifest.permission.DUMP);
3745 return;
3746 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003747 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003748 }
Jack Yueb89b242016-06-22 13:27:47 -07003749
3750 /**
3751 * Get aggregated video call data usage from all subscriptions since boot.
3752 * @return total data usage in bytes
3753 * {@hide}
3754 */
3755 @Override
3756 public long getVtDataUsage() {
3757 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3758 null);
3759
3760 // NetworkStatsService keeps tracking the active network interface and identity. It will
3761 // record the delta with the corresponding network identity. What we need to do here is
3762 // returning total video call data usage from all subscriptions since boot.
3763
3764 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3765 // simultaneous VT calls.
3766 final Phone[] phones = PhoneFactory.getPhones();
3767 long total = 0;
3768 for (Phone phone : phones) {
3769 total += phone.getVtDataUsage();
3770 }
3771 return total;
3772 }
Jack Yu75ab2952016-07-08 14:29:33 -07003773
3774 /**
3775 * Policy control of data connection. Usually used when data limit is passed.
3776 * @param enabled True if enabling the data, otherwise disabling.
3777 * @param subId Subscription index
3778 * {@hide}
3779 */
3780 @Override
3781 public void setPolicyDataEnabled(boolean enabled, int subId) {
3782 enforceModifyPermission();
3783 Phone phone = getPhone(subId);
3784 if (phone != null) {
3785 phone.setPolicyDataEnabled(enabled);
3786 }
3787 }
Sooraj Sasindran9a909312016-07-18 11:57:25 -07003788
3789 /**
3790 * Get Client request stats
3791 * @return List of Client Request Stats
3792 * @hide
3793 */
3794 @Override
3795 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3796 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3797 return null;
3798 }
3799
3800 Phone phone = getPhone(subId);
3801 if (phone != null) {
3802 return phone.getClientRequestStats();
3803 }
3804
3805 return null;
3806 }
3807
3808 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3809 if (workSource != null) {
3810 return workSource;
3811 }
3812
3813 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3814 workSource = new WorkSource(uid, packageName);
3815 return workSource;
3816 }
Jack Yueb4124c2017-02-16 15:32:43 -08003817
3818 /**
3819 * Set SIM card power state. Request is equivalent to inserting or removing the card.
3820 *
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003821 * @param slotIndex SIM slot id.
Jack Yueb4124c2017-02-16 15:32:43 -08003822 * @param powerUp True if powering up the SIM, otherwise powering down
3823 *
3824 **/
3825 @Override
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003826 public void setSimPowerStateForSlot(int slotIndex, boolean powerUp) {
Jack Yueb4124c2017-02-16 15:32:43 -08003827 enforceModifyPermission();
Sanket Padawe13bac7b2017-03-20 15:04:47 -07003828 Phone phone = PhoneFactory.getPhone(slotIndex);
3829
Jack Yueb4124c2017-02-16 15:32:43 -08003830 if (phone != null) {
3831 phone.setSimPowerState(powerUp);
3832 }
3833 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003834}