blob: d857087561b3dd2f48ad971933dd604225241033 [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
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.ActivityManager;
22import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070023import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.Context;
25import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070026import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080027import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070028import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Looper;
35import android.os.Message;
36import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070037import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.ServiceManager;
39import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070040import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070041import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080042import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070043import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080044import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080045import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070046import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070047import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070049import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070050import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080051import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070052import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080054import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080055import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070056import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070057import android.telephony.TelephonyManager;
58import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080060import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080062import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080063import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064
Andrew Lee312e8172014-10-23 17:01:36 -070065import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070066import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070067import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070068import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080071import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010072import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070073import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070075import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070076import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070077import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070078import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070079import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080080import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070081import com.android.internal.telephony.uicc.IccIoResult;
82import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070083import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070084import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080085import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080086import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087
Ta-wei Yenc236d6b2016-06-21 13:33:12 -070088import java.io.FileDescriptor;
89import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080091import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080092import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010093import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080094import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095
96/**
97 * Implementation of the ITelephony interface.
98 */
Santos Cordon117fee72014-05-16 17:56:12 -070099public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100 private static final String LOG_TAG = "PhoneInterfaceManager";
101 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
102 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800103 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104
105 // Message codes used with mMainThreadHandler
106 private static final int CMD_HANDLE_PIN_MMI = 1;
107 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
108 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
109 private static final int CMD_ANSWER_RINGING_CALL = 4;
110 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700111 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
112 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700113 private static final int CMD_OPEN_CHANNEL = 9;
114 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
115 private static final int CMD_CLOSE_CHANNEL = 11;
116 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800117 private static final int CMD_NV_READ_ITEM = 13;
118 private static final int EVENT_NV_READ_ITEM_DONE = 14;
119 private static final int CMD_NV_WRITE_ITEM = 15;
120 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
121 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
122 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
123 private static final int CMD_NV_RESET_CONFIG = 19;
124 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800125 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
126 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
127 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
128 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800129 private static final int CMD_SEND_ENVELOPE = 25;
130 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700131 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
132 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
133 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
134 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
135 private static final int CMD_EXCHANGE_SIM_IO = 31;
136 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800137 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
138 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700139 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
140 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700141 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
142 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700143 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
144 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
145 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
146 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700147 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
148 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
149 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
150 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151
152 /** The singleton instance. */
153 private static PhoneInterfaceManager sInstance;
154
Wink Saville3ab207e2014-11-20 13:07:20 -0800155 private PhoneGlobals mApp;
156 private Phone mPhone;
157 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700158 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800159 private AppOpsManager mAppOps;
160 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800161 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800162 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163
Derek Tan97ebb422014-09-05 16:55:38 -0700164 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
165 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800166 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700167
168 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700169 * A request object to use for transmitting data to an ICC.
170 */
171 private static final class IccAPDUArgument {
172 public int channel, cla, command, p1, p2, p3;
173 public String data;
174
175 public IccAPDUArgument(int channel, int cla, int command,
176 int p1, int p2, int p3, String data) {
177 this.channel = channel;
178 this.cla = cla;
179 this.command = command;
180 this.p1 = p1;
181 this.p2 = p2;
182 this.p3 = p3;
183 this.data = data;
184 }
185 }
186
187 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700188 * A request object to use for transmitting data to an ICC.
189 */
190 private static final class ManualNetworkSelectionArgument {
191 public OperatorInfo operatorInfo;
192 public boolean persistSelection;
193
194 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
195 this.operatorInfo = operatorInfo;
196 this.persistSelection = persistSelection;
197 }
198 }
199
200 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
202 * request after sending. The main thread will notify the request when it is complete.
203 */
204 private static final class MainThreadRequest {
205 /** The argument to use for the request */
206 public Object argument;
207 /** The result of the request that is run on the main thread */
208 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800209 // The subscriber id that this request applies to. Defaults to
210 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
211 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212
213 public MainThreadRequest(Object argument) {
214 this.argument = argument;
215 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800216
217 public MainThreadRequest(Object argument, Integer subId) {
218 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800219 if (subId != null) {
220 this.subId = subId;
221 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800222 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700223 }
224
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800225 private static final class IncomingThirdPartyCallArgs {
226 public final ComponentName component;
227 public final String callId;
228 public final String callerDisplayName;
229
230 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
231 String callerDisplayName) {
232 this.component = component;
233 this.callId = callId;
234 this.callerDisplayName = callerDisplayName;
235 }
236 }
237
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238 /**
239 * A handler that processes messages on the main thread in the phone process. Since many
240 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
241 * inbound binder threads to the main thread in the phone process. The Binder thread
242 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
243 * on, which will be notified when the operation completes and will contain the result of the
244 * request.
245 *
246 * <p>If a MainThreadRequest object is provided in the msg.obj field,
247 * note that request.result must be set to something non-null for the calling thread to
248 * unblock.
249 */
250 private final class MainThreadHandler extends Handler {
251 @Override
252 public void handleMessage(Message msg) {
253 MainThreadRequest request;
254 Message onCompleted;
255 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800256 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700257 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258
259 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700260 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700262 final Phone phone = getPhoneFromRequest(request);
263 request.result = phone != null ?
264 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
265 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 // Wake up the requesting thread
267 synchronized (request) {
268 request.notifyAll();
269 }
270 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700271 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272
273 case CMD_HANDLE_NEIGHBORING_CELL:
274 request = (MainThreadRequest) msg.obj;
275 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
276 request);
277 mPhone.getNeighboringCids(onCompleted);
278 break;
279
280 case EVENT_NEIGHBORING_CELL_DONE:
281 ar = (AsyncResult) msg.obj;
282 request = (MainThreadRequest) ar.userObj;
283 if (ar.exception == null && ar.result != null) {
284 request.result = ar.result;
285 } else {
286 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800287 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 }
289 // Wake up the requesting thread
290 synchronized (request) {
291 request.notifyAll();
292 }
293 break;
294
295 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700296 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800297 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700298 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 break;
300
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 case CMD_END_CALL:
302 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800303 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700304 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700305 Phone phone = getPhone(end_subId);
306 if (phone == null) {
307 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
308 break;
309 }
310 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
312 // CDMA: If the user presses the Power button we treat it as
313 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700314 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
316 // GSM: End the call as per the Phone state
317 hungUp = PhoneUtils.hangup(mCM);
318 } else {
319 throw new IllegalStateException("Unexpected phone type: " + phoneType);
320 }
321 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
322 request.result = hungUp;
323 // Wake up the requesting thread
324 synchronized (request) {
325 request.notifyAll();
326 }
327 break;
328
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700329 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700330 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700331 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800332 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700333 if (uiccCard == null) {
334 loge("iccTransmitApduLogicalChannel: No UICC");
335 request.result = new IccIoResult(0x6F, 0, (byte[])null);
336 synchronized (request) {
337 request.notifyAll();
338 }
339 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700340 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
341 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700342 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700343 iccArgument.channel, iccArgument.cla, iccArgument.command,
344 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700345 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700346 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700347 break;
348
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700349 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700350 ar = (AsyncResult) msg.obj;
351 request = (MainThreadRequest) ar.userObj;
352 if (ar.exception == null && ar.result != null) {
353 request.result = ar.result;
354 } else {
355 request.result = new IccIoResult(0x6F, 0, (byte[])null);
356 if (ar.result == null) {
357 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800358 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700359 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800360 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700361 } else {
362 loge("iccTransmitApduLogicalChannel: Unknown exception");
363 }
364 }
365 synchronized (request) {
366 request.notifyAll();
367 }
368 break;
369
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700370 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
371 request = (MainThreadRequest) msg.obj;
372 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800373 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700374 if (uiccCard == null) {
375 loge("iccTransmitApduBasicChannel: No UICC");
376 request.result = new IccIoResult(0x6F, 0, (byte[])null);
377 synchronized (request) {
378 request.notifyAll();
379 }
380 } else {
381 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
382 request);
383 uiccCard.iccTransmitApduBasicChannel(
384 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
385 iccArgument.p3, iccArgument.data, onCompleted);
386 }
387 break;
388
389 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
390 ar = (AsyncResult) msg.obj;
391 request = (MainThreadRequest) ar.userObj;
392 if (ar.exception == null && ar.result != null) {
393 request.result = ar.result;
394 } else {
395 request.result = new IccIoResult(0x6F, 0, (byte[])null);
396 if (ar.result == null) {
397 loge("iccTransmitApduBasicChannel: Empty response");
398 } else if (ar.exception instanceof CommandException) {
399 loge("iccTransmitApduBasicChannel: CommandException: " +
400 ar.exception);
401 } else {
402 loge("iccTransmitApduBasicChannel: Unknown exception");
403 }
404 }
405 synchronized (request) {
406 request.notifyAll();
407 }
408 break;
409
410 case CMD_EXCHANGE_SIM_IO:
411 request = (MainThreadRequest) msg.obj;
412 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800413 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700414 if (uiccCard == null) {
415 loge("iccExchangeSimIO: No UICC");
416 request.result = new IccIoResult(0x6F, 0, (byte[])null);
417 synchronized (request) {
418 request.notifyAll();
419 }
420 } else {
421 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
422 request);
423 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
424 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
425 iccArgument.data, onCompleted);
426 }
427 break;
428
429 case EVENT_EXCHANGE_SIM_IO_DONE:
430 ar = (AsyncResult) msg.obj;
431 request = (MainThreadRequest) ar.userObj;
432 if (ar.exception == null && ar.result != null) {
433 request.result = ar.result;
434 } else {
435 request.result = new IccIoResult(0x6f, 0, (byte[])null);
436 }
437 synchronized (request) {
438 request.notifyAll();
439 }
440 break;
441
Derek Tan4d5e5c12014-02-04 11:54:58 -0800442 case CMD_SEND_ENVELOPE:
443 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800444 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700445 if (uiccCard == null) {
446 loge("sendEnvelopeWithStatus: No UICC");
447 request.result = new IccIoResult(0x6F, 0, (byte[])null);
448 synchronized (request) {
449 request.notifyAll();
450 }
451 } else {
452 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
453 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
454 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800455 break;
456
457 case EVENT_SEND_ENVELOPE_DONE:
458 ar = (AsyncResult) msg.obj;
459 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700460 if (ar.exception == null && ar.result != null) {
461 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800462 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700463 request.result = new IccIoResult(0x6F, 0, (byte[])null);
464 if (ar.result == null) {
465 loge("sendEnvelopeWithStatus: Empty response");
466 } else if (ar.exception instanceof CommandException) {
467 loge("sendEnvelopeWithStatus: CommandException: " +
468 ar.exception);
469 } else {
470 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
471 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800472 }
473 synchronized (request) {
474 request.notifyAll();
475 }
476 break;
477
Shishir Agrawal566b7612013-10-28 14:41:00 -0700478 case CMD_OPEN_CHANNEL:
479 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800480 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700481 if (uiccCard == null) {
482 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800483 request.result = new IccOpenLogicalChannelResponse(-1,
484 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700485 synchronized (request) {
486 request.notifyAll();
487 }
488 } else {
489 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
490 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
491 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700492 break;
493
494 case EVENT_OPEN_CHANNEL_DONE:
495 ar = (AsyncResult) msg.obj;
496 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700497 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700498 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700499 int[] result = (int[]) ar.result;
500 int channelId = result[0];
501 byte[] selectResponse = null;
502 if (result.length > 1) {
503 selectResponse = new byte[result.length - 1];
504 for (int i = 1; i < result.length; ++i) {
505 selectResponse[i - 1] = (byte) result[i];
506 }
507 }
508 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700509 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700511 if (ar.result == null) {
512 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700513 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700514 if (ar.exception != null) {
515 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
516 }
517
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700518 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700519 if (ar.exception instanceof CommandException) {
520 CommandException.Error error =
521 ((CommandException) (ar.exception)).getCommandError();
522 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700523 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700524 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700525 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700526 }
527 }
528 openChannelResp = new IccOpenLogicalChannelResponse(
529 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700530 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700531 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700532 synchronized (request) {
533 request.notifyAll();
534 }
535 break;
536
537 case CMD_CLOSE_CHANNEL:
538 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800539 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700540 if (uiccCard == null) {
541 loge("iccCloseLogicalChannel: No UICC");
542 request.result = new IccIoResult(0x6F, 0, (byte[])null);
543 synchronized (request) {
544 request.notifyAll();
545 }
546 } else {
547 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
548 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
549 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700550 break;
551
552 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800553 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
554 break;
555
556 case CMD_NV_READ_ITEM:
557 request = (MainThreadRequest) msg.obj;
558 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
559 mPhone.nvReadItem((Integer) request.argument, onCompleted);
560 break;
561
562 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700563 ar = (AsyncResult) msg.obj;
564 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800565 if (ar.exception == null && ar.result != null) {
566 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700567 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800568 request.result = "";
569 if (ar.result == null) {
570 loge("nvReadItem: Empty response");
571 } else if (ar.exception instanceof CommandException) {
572 loge("nvReadItem: CommandException: " +
573 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700574 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800575 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700576 }
577 }
578 synchronized (request) {
579 request.notifyAll();
580 }
581 break;
582
Jake Hambye994d462014-02-03 13:10:13 -0800583 case CMD_NV_WRITE_ITEM:
584 request = (MainThreadRequest) msg.obj;
585 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
586 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
587 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
588 break;
589
590 case EVENT_NV_WRITE_ITEM_DONE:
591 handleNullReturnEvent(msg, "nvWriteItem");
592 break;
593
594 case CMD_NV_WRITE_CDMA_PRL:
595 request = (MainThreadRequest) msg.obj;
596 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
597 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
598 break;
599
600 case EVENT_NV_WRITE_CDMA_PRL_DONE:
601 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
602 break;
603
604 case CMD_NV_RESET_CONFIG:
605 request = (MainThreadRequest) msg.obj;
606 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
607 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
608 break;
609
610 case EVENT_NV_RESET_CONFIG_DONE:
611 handleNullReturnEvent(msg, "nvResetConfig");
612 break;
613
Jake Hamby7c27be32014-03-03 13:25:59 -0800614 case CMD_GET_PREFERRED_NETWORK_TYPE:
615 request = (MainThreadRequest) msg.obj;
616 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700617 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800618 break;
619
620 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
621 ar = (AsyncResult) msg.obj;
622 request = (MainThreadRequest) ar.userObj;
623 if (ar.exception == null && ar.result != null) {
624 request.result = ar.result; // Integer
625 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800626 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800627 if (ar.result == null) {
628 loge("getPreferredNetworkType: Empty response");
629 } else if (ar.exception instanceof CommandException) {
630 loge("getPreferredNetworkType: CommandException: " +
631 ar.exception);
632 } else {
633 loge("getPreferredNetworkType: Unknown exception");
634 }
635 }
636 synchronized (request) {
637 request.notifyAll();
638 }
639 break;
640
641 case CMD_SET_PREFERRED_NETWORK_TYPE:
642 request = (MainThreadRequest) msg.obj;
643 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
644 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700645 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800646 break;
647
648 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
649 handleNullReturnEvent(msg, "setPreferredNetworkType");
650 break;
651
Steven Liu4bf01bc2014-07-17 11:05:29 -0500652 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
653 request = (MainThreadRequest)msg.obj;
654 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
655 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
656 break;
657
658 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
659 ar = (AsyncResult)msg.obj;
660 request = (MainThreadRequest)ar.userObj;
661 request.result = ar;
662 synchronized (request) {
663 request.notifyAll();
664 }
665 break;
666
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800667 case CMD_SET_VOICEMAIL_NUMBER:
668 request = (MainThreadRequest) msg.obj;
669 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
670 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800671 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
672 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800673 break;
674
675 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
676 handleNullReturnEvent(msg, "setVoicemailNumber");
677 break;
678
Stuart Scott54788802015-03-30 13:18:01 -0700679 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
680 request = (MainThreadRequest) msg.obj;
681 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
682 request);
683 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
684 break;
685
686 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
687 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
688 break;
689
Shishir Agrawal302c8692015-06-19 13:49:39 -0700690 case CMD_PERFORM_NETWORK_SCAN:
691 request = (MainThreadRequest) msg.obj;
692 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
693 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
694 break;
695
696 case EVENT_PERFORM_NETWORK_SCAN_DONE:
697 ar = (AsyncResult) msg.obj;
698 request = (MainThreadRequest) ar.userObj;
699 CellNetworkScanResult cellScanResult;
700 if (ar.exception == null && ar.result != null) {
701 cellScanResult = new CellNetworkScanResult(
702 CellNetworkScanResult.STATUS_SUCCESS,
703 (List<OperatorInfo>) ar.result);
704 } else {
705 if (ar.result == null) {
706 loge("getCellNetworkScanResults: Empty response");
707 }
708 if (ar.exception != null) {
709 loge("getCellNetworkScanResults: Exception: " + ar.exception);
710 }
711 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
712 if (ar.exception instanceof CommandException) {
713 CommandException.Error error =
714 ((CommandException) (ar.exception)).getCommandError();
715 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
716 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
717 } else if (error == CommandException.Error.GENERIC_FAILURE) {
718 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
719 }
720 }
721 cellScanResult = new CellNetworkScanResult(errorCode, null);
722 }
723 request.result = cellScanResult;
724 synchronized (request) {
725 request.notifyAll();
726 }
727 break;
728
729 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
730 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700731 ManualNetworkSelectionArgument selArg =
732 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700733 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
734 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700735 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
736 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700737 break;
738
739 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
740 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
741 break;
742
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700743 case CMD_GET_MODEM_ACTIVITY_INFO:
744 request = (MainThreadRequest) msg.obj;
745 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700746 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700747 break;
748
749 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
750 ar = (AsyncResult) msg.obj;
751 request = (MainThreadRequest) ar.userObj;
752 if (ar.exception == null && ar.result != null) {
753 request.result = ar.result;
754 } else {
755 if (ar.result == null) {
756 loge("queryModemActivityInfo: Empty response");
757 } else if (ar.exception instanceof CommandException) {
758 loge("queryModemActivityInfo: CommandException: " +
759 ar.exception);
760 } else {
761 loge("queryModemActivityInfo: Unknown exception");
762 }
763 }
Amit Mahajand4766222016-01-28 15:28:28 -0800764 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
765 if (request.result == null) {
766 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
767 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700768 synchronized (request) {
769 request.notifyAll();
770 }
771 break;
772
Meng Wang1a7c35a2016-05-05 20:56:15 -0700773 case CMD_SET_ALLOWED_CARRIERS:
774 request = (MainThreadRequest) msg.obj;
775 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
776 mPhone.setAllowedCarriers(
777 (List<CarrierIdentifier>) request.argument,
778 onCompleted);
779 break;
780
781 case EVENT_SET_ALLOWED_CARRIERS_DONE:
782 ar = (AsyncResult) msg.obj;
783 request = (MainThreadRequest) ar.userObj;
784 if (ar.exception == null && ar.result != null) {
785 request.result = ar.result;
786 } else {
787 if (ar.result == null) {
788 loge("setAllowedCarriers: Empty response");
789 } else if (ar.exception instanceof CommandException) {
790 loge("setAllowedCarriers: CommandException: " +
791 ar.exception);
792 } else {
793 loge("setAllowedCarriers: Unknown exception");
794 }
795 }
796 // Result cannot be null. Return -1 on error.
797 if (request.result == null) {
798 request.result = new int[]{-1};
799 }
800 synchronized (request) {
801 request.notifyAll();
802 }
803 break;
804
805 case CMD_GET_ALLOWED_CARRIERS:
806 request = (MainThreadRequest) msg.obj;
807 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
808 mPhone.getAllowedCarriers(onCompleted);
809 break;
810
811 case EVENT_GET_ALLOWED_CARRIERS_DONE:
812 ar = (AsyncResult) msg.obj;
813 request = (MainThreadRequest) ar.userObj;
814 if (ar.exception == null && ar.result != null) {
815 request.result = ar.result;
816 } else {
817 if (ar.result == null) {
818 loge("getAllowedCarriers: Empty response");
819 } else if (ar.exception instanceof CommandException) {
820 loge("getAllowedCarriers: CommandException: " +
821 ar.exception);
822 } else {
823 loge("getAllowedCarriers: Unknown exception");
824 }
825 }
826 // Result cannot be null. Return empty list of CarrierIdentifier.
827 if (request.result == null) {
828 request.result = new ArrayList<CarrierIdentifier>(0);
829 }
830 synchronized (request) {
831 request.notifyAll();
832 }
833 break;
834
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 default:
836 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
837 break;
838 }
839 }
Jake Hambye994d462014-02-03 13:10:13 -0800840
841 private void handleNullReturnEvent(Message msg, String command) {
842 AsyncResult ar = (AsyncResult) msg.obj;
843 MainThreadRequest request = (MainThreadRequest) ar.userObj;
844 if (ar.exception == null) {
845 request.result = true;
846 } else {
847 request.result = false;
848 if (ar.exception instanceof CommandException) {
849 loge(command + ": CommandException: " + ar.exception);
850 } else {
851 loge(command + ": Unknown exception");
852 }
853 }
854 synchronized (request) {
855 request.notifyAll();
856 }
857 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 }
859
860 /**
861 * Posts the specified command to be executed on the main thread,
862 * waits for the request to complete, and returns the result.
863 * @see #sendRequestAsync
864 */
865 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800866 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700867 }
868
869 /**
870 * Posts the specified command to be executed on the main thread,
871 * waits for the request to complete, and returns the result.
872 * @see #sendRequestAsync
873 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800874 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
876 throw new RuntimeException("This method will deadlock if called from the main thread.");
877 }
878
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800879 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700880 Message msg = mMainThreadHandler.obtainMessage(command, request);
881 msg.sendToTarget();
882
883 // Wait for the request to complete
884 synchronized (request) {
885 while (request.result == null) {
886 try {
887 request.wait();
888 } catch (InterruptedException e) {
889 // Do nothing, go back and wait until the request is complete
890 }
891 }
892 }
893 return request.result;
894 }
895
896 /**
897 * Asynchronous ("fire and forget") version of sendRequest():
898 * Posts the specified command to be executed on the main thread, and
899 * returns immediately.
900 * @see #sendRequest
901 */
902 private void sendRequestAsync(int command) {
903 mMainThreadHandler.sendEmptyMessage(command);
904 }
905
906 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700907 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
908 * @see {@link #sendRequest(int,Object)}
909 */
910 private void sendRequestAsync(int command, Object argument) {
911 MainThreadRequest request = new MainThreadRequest(argument);
912 Message msg = mMainThreadHandler.obtainMessage(command, request);
913 msg.sendToTarget();
914 }
915
916 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700917 * Initialize the singleton PhoneInterfaceManager instance.
918 * This is only done once, at startup, from PhoneApp.onCreate().
919 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700920 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700921 synchronized (PhoneInterfaceManager.class) {
922 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700923 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 } else {
925 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
926 }
927 return sInstance;
928 }
929 }
930
931 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700932 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700933 mApp = app;
934 mPhone = phone;
935 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700936 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
938 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700939 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700940 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800941 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800942
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943 publish();
944 }
945
946 private void publish() {
947 if (DBG) log("publish: " + this);
948
949 ServiceManager.addService("phone", this);
950 }
951
Stuart Scott584921c2015-01-15 17:10:34 -0800952 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800953 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
954 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800955 }
956
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800957 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
958 Phone phone = getPhoneFromRequest(request);
959 return phone == null ? null :
960 UiccController.getInstance().getUiccCard(phone.getPhoneId());
961 }
962
Wink Saville36469e72014-06-11 15:17:00 -0700963 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700964 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800965 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700966 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 //
968 // Implementation of the ITelephony interface.
969 //
970
971 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700972 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700973 }
974
Wink Savilleb564aae2014-10-23 10:18:09 -0700975 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 if (DBG) log("dial: " + number);
977 // No permission check needed here: This is just a wrapper around the
978 // ACTION_DIAL intent, which is available to any app since it puts up
979 // the UI before it does anything.
980
981 String url = createTelUrl(number);
982 if (url == null) {
983 return;
984 }
985
986 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700987 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
989 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
990 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
991 mApp.startActivity(intent);
992 }
993 }
994
995 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700996 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700997 }
998
Wink Savilleb564aae2014-10-23 10:18:09 -0700999 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 if (DBG) log("call: " + number);
1001
1002 // This is just a wrapper around the ACTION_CALL intent, but we still
1003 // need to do a permission check since we're calling startActivity()
1004 // from the context of the phone app.
1005 enforceCallPermission();
1006
1007 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1008 != AppOpsManager.MODE_ALLOWED) {
1009 return;
1010 }
1011
1012 String url = createTelUrl(number);
1013 if (url == null) {
1014 return;
1015 }
1016
Wink Saville08874612014-08-31 19:19:58 -07001017 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001018 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001019 if (slist != null) {
1020 for (SubscriptionInfo subInfoRecord : slist) {
1021 if (subInfoRecord.getSubscriptionId() == subId) {
1022 isValid = true;
1023 break;
1024 }
Wink Saville08874612014-08-31 19:19:58 -07001025 }
1026 }
1027 if (isValid == false) {
1028 return;
1029 }
1030
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001031 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001032 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1034 mApp.startActivity(intent);
1035 }
1036
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001037 /**
1038 * End a call based on call state
1039 * @return true is a call was ended
1040 */
1041 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001042 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001043 }
1044
1045 /**
1046 * End a call based on the call state of the subId
1047 * @return true is a call was ended
1048 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001049 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001050 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001051 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001052 }
1053
1054 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001055 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001056 }
1057
Wink Savilleb564aae2014-10-23 10:18:09 -07001058 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 if (DBG) log("answerRingingCall...");
1060 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1061 // but that can probably wait till the big TelephonyManager API overhaul.
1062 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1063 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001064 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001065 }
1066
1067 /**
1068 * Make the actual telephony calls to implement answerRingingCall().
1069 * This should only be called from the main thread of the Phone app.
1070 * @see #answerRingingCall
1071 *
1072 * TODO: it would be nice to return true if we answered the call, or
1073 * false if there wasn't actually a ringing incoming call, or some
1074 * other error occurred. (In other words, pass back the return value
1075 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1076 * But that would require calling this method via sendRequest() rather
1077 * than sendRequestAsync(), and right now we don't actually *need* that
1078 * return value, so let's just return void for now.
1079 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001080 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001081 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001082 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001083 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1084 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001085 if (hasActiveCall && hasHoldingCall) {
1086 // Both lines are in use!
1087 // TODO: provide a flag to let the caller specify what
1088 // policy to use if both lines are in use. (The current
1089 // behavior is hardwired to "answer incoming, end ongoing",
1090 // which is how the CALL button is specced to behave.)
1091 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1092 return;
1093 } else {
1094 // answerCall() will automatically hold the current active
1095 // call, if there is one.
1096 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1097 return;
1098 }
1099 } else {
1100 // No call was ringing.
1101 return;
1102 }
1103 }
1104
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001105 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001106 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001107 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001108 public void silenceRinger() {
1109 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001110 }
1111
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001112 @Override
1113 public boolean isOffhook(String callingPackage) {
1114 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001115 }
1116
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001117 @Override
1118 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1119 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1120 return false;
1121 }
1122
Sanket Padawe356d7632015-06-22 14:03:32 -07001123 final Phone phone = getPhone(subId);
1124 if (phone != null) {
1125 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1126 } else {
1127 return false;
1128 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 }
1130
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001131 @Override
1132 public boolean isRinging(String callingPackage) {
1133 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001134 }
1135
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001136 @Override
1137 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1138 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1139 return false;
1140 }
1141
Sanket Padawe356d7632015-06-22 14:03:32 -07001142 final Phone phone = getPhone(subId);
1143 if (phone != null) {
1144 return (phone.getState() == PhoneConstants.State.RINGING);
1145 } else {
1146 return false;
1147 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 }
1149
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001150 @Override
1151 public boolean isIdle(String callingPackage) {
1152 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001153 }
1154
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001155 @Override
1156 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1157 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1158 return false;
1159 }
1160
Sanket Padawe356d7632015-06-22 14:03:32 -07001161 final Phone phone = getPhone(subId);
1162 if (phone != null) {
1163 return (phone.getState() == PhoneConstants.State.IDLE);
1164 } else {
1165 return false;
1166 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167 }
1168
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001169 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001170 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001171 }
1172
Wink Savilleb564aae2014-10-23 10:18:09 -07001173 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001174 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001175 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1176 }
1177
1178 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001179 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001180 }
1181
Wink Savilleb564aae2014-10-23 10:18:09 -07001182 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001183 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001184 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1185 }
1186
1187 /** {@hide} */
1188 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001189 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001190 }
1191
Wink Savilleb564aae2014-10-23 10:18:09 -07001192 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001194 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 checkSimPin.start();
1196 return checkSimPin.unlockSim(null, pin);
1197 }
1198
Wink Saville9de0f752013-10-22 19:04:03 -07001199 /** {@hide} */
1200 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001201 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001202 }
1203
Wink Savilleb564aae2014-10-23 10:18:09 -07001204 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001206 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001207 checkSimPuk.start();
1208 return checkSimPuk.unlockSim(puk, pin);
1209 }
1210
1211 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001212 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 * a synchronous one.
1214 */
1215 private static class UnlockSim extends Thread {
1216
1217 private final IccCard mSimCard;
1218
1219 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001220 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1221 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001222
1223 // For replies from SimCard interface
1224 private Handler mHandler;
1225
1226 // For async handler to identify request type
1227 private static final int SUPPLY_PIN_COMPLETE = 100;
1228
1229 public UnlockSim(IccCard simCard) {
1230 mSimCard = simCard;
1231 }
1232
1233 @Override
1234 public void run() {
1235 Looper.prepare();
1236 synchronized (UnlockSim.this) {
1237 mHandler = new Handler() {
1238 @Override
1239 public void handleMessage(Message msg) {
1240 AsyncResult ar = (AsyncResult) msg.obj;
1241 switch (msg.what) {
1242 case SUPPLY_PIN_COMPLETE:
1243 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1244 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001245 mRetryCount = msg.arg1;
1246 if (ar.exception != null) {
1247 if (ar.exception instanceof CommandException &&
1248 ((CommandException)(ar.exception)).getCommandError()
1249 == CommandException.Error.PASSWORD_INCORRECT) {
1250 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1251 } else {
1252 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1253 }
1254 } else {
1255 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1256 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001257 mDone = true;
1258 UnlockSim.this.notifyAll();
1259 }
1260 break;
1261 }
1262 }
1263 };
1264 UnlockSim.this.notifyAll();
1265 }
1266 Looper.loop();
1267 }
1268
1269 /*
1270 * Use PIN or PUK to unlock SIM card
1271 *
1272 * If PUK is null, unlock SIM card with PIN
1273 *
1274 * If PUK is not null, unlock SIM card with PUK and set PIN code
1275 */
Wink Saville9de0f752013-10-22 19:04:03 -07001276 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277
1278 while (mHandler == null) {
1279 try {
1280 wait();
1281 } catch (InterruptedException e) {
1282 Thread.currentThread().interrupt();
1283 }
1284 }
1285 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1286
1287 if (puk == null) {
1288 mSimCard.supplyPin(pin, callback);
1289 } else {
1290 mSimCard.supplyPuk(puk, pin, callback);
1291 }
1292
1293 while (!mDone) {
1294 try {
1295 Log.d(LOG_TAG, "wait for done");
1296 wait();
1297 } catch (InterruptedException e) {
1298 // Restore the interrupted status
1299 Thread.currentThread().interrupt();
1300 }
1301 }
1302 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001303 int[] resultArray = new int[2];
1304 resultArray[0] = mResult;
1305 resultArray[1] = mRetryCount;
1306 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001307 }
1308 }
1309
1310 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001311 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001312
1313 }
1314
Wink Savilleb564aae2014-10-23 10:18:09 -07001315 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001316 // No permission check needed here: this call is harmless, and it's
1317 // needed for the ServiceState.requestStateUpdate() call (which is
1318 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001319 final Phone phone = getPhone(subId);
1320 if (phone != null) {
1321 phone.updateServiceLocation();
1322 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001323 }
1324
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001325 @Override
1326 public boolean isRadioOn(String callingPackage) {
1327 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001328 }
1329
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001330 @Override
1331 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1332 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1333 return false;
1334 }
1335 return isRadioOnForSubscriber(subId);
1336 }
1337
1338 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001339 final Phone phone = getPhone(subId);
1340 if (phone != null) {
1341 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1342 } else {
1343 return false;
1344 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001345 }
1346
1347 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001348 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001349
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001350 }
Wink Saville36469e72014-06-11 15:17:00 -07001351
Wink Savilleb564aae2014-10-23 10:18:09 -07001352 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001354 final Phone phone = getPhone(subId);
1355 if (phone != null) {
1356 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1357 }
Wink Saville36469e72014-06-11 15:17:00 -07001358 }
1359
1360 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001361 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001362 }
1363
Wink Savilleb564aae2014-10-23 10:18:09 -07001364 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001365 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001366 final Phone phone = getPhone(subId);
1367 if (phone == null) {
1368 return false;
1369 }
1370 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001371 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001372 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 }
1374 return true;
1375 }
Wink Saville36469e72014-06-11 15:17:00 -07001376
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001377 public boolean needMobileRadioShutdown() {
1378 /*
1379 * If any of the Radios are available, it will need to be
1380 * shutdown. So return true if any Radio is available.
1381 */
1382 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1383 Phone phone = PhoneFactory.getPhone(i);
1384 if (phone != null && phone.isRadioAvailable()) return true;
1385 }
1386 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1387 return false;
1388 }
1389
1390 public void shutdownMobileRadios() {
1391 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1392 logv("Shutting down Phone " + i);
1393 shutdownRadioUsingPhoneId(i);
1394 }
1395 }
1396
1397 private void shutdownRadioUsingPhoneId(int phoneId) {
1398 enforceModifyPermission();
1399 Phone phone = PhoneFactory.getPhone(phoneId);
1400 if (phone != null && phone.isRadioAvailable()) {
1401 phone.shutdownRadio();
1402 }
1403 }
1404
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001405 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001406 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1407 if (defaultPhone != null) {
1408 defaultPhone.setRadioPower(turnOn);
1409 return true;
1410 } else {
1411 loge("There's no default phone.");
1412 return false;
1413 }
Wink Saville36469e72014-06-11 15:17:00 -07001414 }
1415
Wink Savilleb564aae2014-10-23 10:18:09 -07001416 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001417 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001418 final Phone phone = getPhone(subId);
1419 if (phone != null) {
1420 phone.setRadioPower(turnOn);
1421 return true;
1422 } else {
1423 return false;
1424 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 }
1426
Wink Saville36469e72014-06-11 15:17:00 -07001427 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001428 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001429 public boolean enableDataConnectivity() {
1430 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001431 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001432 final Phone phone = getPhone(subId);
1433 if (phone != null) {
1434 phone.setDataEnabled(true);
1435 return true;
1436 } else {
1437 return false;
1438 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 }
1440
Wink Saville36469e72014-06-11 15:17:00 -07001441 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001442 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 public boolean disableDataConnectivity() {
1444 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001445 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001446 final Phone phone = getPhone(subId);
1447 if (phone != null) {
1448 phone.setDataEnabled(false);
1449 return true;
1450 } else {
1451 return false;
1452 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001453 }
1454
Wink Saville36469e72014-06-11 15:17:00 -07001455 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001456 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001458 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001459 final Phone phone = getPhone(subId);
1460 if (phone != null) {
1461 return phone.isDataConnectivityPossible();
1462 } else {
1463 return false;
1464 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001465 }
1466
1467 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001468 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001469 }
1470
Wink Savilleb564aae2014-10-23 10:18:09 -07001471 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001472 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001473 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1474 return false;
1475 }
Wink Saville36469e72014-06-11 15:17:00 -07001476 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001477 }
1478
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001479 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001480 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001481 }
1482
Shishir Agrawala9f32182016-04-12 12:00:16 -07001483 public int getCallStateForSlot(int slotId) {
1484 Phone phone = PhoneFactory.getPhone(slotId);
1485 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1486 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001487 }
1488
Sanket Padawe356d7632015-06-22 14:03:32 -07001489 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001490 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001491 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001492 if (phone != null) {
1493 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1494 } else {
1495 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1496 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001497 }
1498
Sanket Padawe356d7632015-06-22 14:03:32 -07001499 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001501 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001502 if (phone != null) {
1503 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1504 } else {
1505 return TelephonyManager.DATA_ACTIVITY_NONE;
1506 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001507 }
1508
1509 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001510 public Bundle getCellLocation(String callingPackage) {
1511 enforceFineOrCoarseLocationPermission("getCellLocation");
1512
1513 // OP_COARSE_LOCATION controls both fine and coarse location.
1514 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1515 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001516 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001517 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001518 }
1519
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001520 if (checkIfCallerIsSelfOrForegroundUser() ||
1521 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 if (DBG_LOC) log("getCellLocation: is active user");
1523 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001524 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001525 if (phone == null) {
1526 return null;
1527 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001528 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001529 return data;
1530 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001531 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 return null;
1533 }
1534 }
1535
Svetoslav64fad262015-04-14 14:35:21 -07001536 private void enforceFineOrCoarseLocationPermission(String message) {
1537 try {
1538 mApp.enforceCallingOrSelfPermission(
1539 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1540 } catch (SecurityException e) {
1541 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1542 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1543 // is the weaker precondition
1544 mApp.enforceCallingOrSelfPermission(
1545 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1546 }
1547 }
1548
1549
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 @Override
1551 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001552 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001553 }
1554
Sanket Padawe356d7632015-06-22 14:03:32 -07001555 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001556 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001557 mApp.enforceCallingOrSelfPermission(
1558 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001559 final Phone phone = getPhone(subId);
1560 if (phone != null) {
1561 phone.enableLocationUpdates();
1562 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001563 }
1564
1565 @Override
1566 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001567 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001568 }
1569
Sanket Padawe356d7632015-06-22 14:03:32 -07001570 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001571 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001572 mApp.enforceCallingOrSelfPermission(
1573 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001574 final Phone phone = getPhone(subId);
1575 if (phone != null) {
1576 phone.disableLocationUpdates();
1577 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001578 }
1579
1580 @Override
1581 @SuppressWarnings("unchecked")
1582 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001583 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1584
1585 // OP_COARSE_LOCATION controls both fine and coarse location.
1586 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1587 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1588 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001589 }
1590
1591 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1592 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1593 return null;
1594 }
Svetoslav64fad262015-04-14 14:35:21 -07001595
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001596 if (checkIfCallerIsSelfOrForegroundUser() ||
1597 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001598 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1599
1600 ArrayList<NeighboringCellInfo> cells = null;
1601
1602 try {
1603 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001604 CMD_HANDLE_NEIGHBORING_CELL, null,
1605 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001606 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001607 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001608 }
1609 return cells;
1610 } else {
1611 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1612 return null;
1613 }
1614 }
1615
1616
1617 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001618 public List<CellInfo> getAllCellInfo(String callingPackage) {
1619 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1620
1621 // OP_COARSE_LOCATION controls both fine and coarse location.
1622 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1623 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1624 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001625 }
1626
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001627 if (checkIfCallerIsSelfOrForegroundUser() ||
1628 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001629 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001630 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1631 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001632 final List<CellInfo> info = phone.getAllCellInfo();
1633 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001634 }
1635 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001636 } else {
1637 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1638 return null;
1639 }
1640 }
1641
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001642 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001643 public void setCellInfoListRate(int rateInMillis) {
1644 mPhone.setCellInfoListRate(rateInMillis);
1645 }
1646
Shishir Agrawala9f32182016-04-12 12:00:16 -07001647 @Override
1648 public String getImeiForSlot(int slotId, String callingPackage) {
1649 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1650 return null;
1651 }
1652 Phone phone = PhoneFactory.getPhone(slotId);
1653 return phone == null ? null : phone.getImei();
1654 }
1655
1656 @Override
1657 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1658 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1659 return null;
1660 }
1661 Phone phone = PhoneFactory.getPhone(slotId);
1662 return phone == null ? null : phone.getDeviceSvn();
1663 }
1664
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001665 //
1666 // Internal helper methods.
1667 //
1668
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001669 /**
1670 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1671 */
1672 private boolean checkCallerInteractAcrossUsersFull() {
1673 return mPhone.getContext().checkCallingOrSelfPermission(
1674 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1675 == PackageManager.PERMISSION_GRANTED;
1676 }
1677
Jake Hambye994d462014-02-03 13:10:13 -08001678 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001679 boolean ok;
1680
1681 boolean self = Binder.getCallingUid() == Process.myUid();
1682 if (!self) {
1683 // Get the caller's user id then clear the calling identity
1684 // which will be restored in the finally clause.
1685 int callingUser = UserHandle.getCallingUserId();
1686 long ident = Binder.clearCallingIdentity();
1687
1688 try {
1689 // With calling identity cleared the current user is the foreground user.
1690 int foregroundUser = ActivityManager.getCurrentUser();
1691 ok = (foregroundUser == callingUser);
1692 if (DBG_LOC) {
1693 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1694 + " callingUser=" + callingUser + " ok=" + ok);
1695 }
1696 } catch (Exception ex) {
1697 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1698 ok = false;
1699 } finally {
1700 Binder.restoreCallingIdentity(ident);
1701 }
1702 } else {
1703 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1704 ok = true;
1705 }
1706 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1707 return ok;
1708 }
1709
1710 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001711 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1712 *
1713 * @throws SecurityException if the caller does not have the required permission
1714 */
1715 private void enforceModifyPermission() {
1716 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1717 }
1718
1719 /**
Junda Liua2e36012014-07-09 18:30:01 -07001720 * Make sure either system app or the caller has carrier privilege.
1721 *
1722 * @throws SecurityException if the caller does not have the required permission/privilege
1723 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001724 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001725 int permission = mApp.checkCallingOrSelfPermission(
1726 android.Manifest.permission.MODIFY_PHONE_STATE);
1727 if (permission == PackageManager.PERMISSION_GRANTED) {
1728 return;
1729 }
1730
1731 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001732 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001733 }
1734
1735 /**
1736 * Make sure the caller has carrier privilege.
1737 *
1738 * @throws SecurityException if the caller does not have the required permission
1739 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001740 private void enforceCarrierPrivilege(int subId) {
1741 if (getCarrierPrivilegeStatus(subId) !=
1742 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001743 loge("No Carrier Privilege.");
1744 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001745 }
1746 }
1747
1748 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001749 * Make sure the caller has the CALL_PHONE permission.
1750 *
1751 * @throws SecurityException if the caller does not have the required permission
1752 */
1753 private void enforceCallPermission() {
1754 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1755 }
1756
Stuart Scott8eef64f2015-04-08 15:13:54 -07001757 private void enforceConnectivityInternalPermission() {
1758 mApp.enforceCallingOrSelfPermission(
1759 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1760 "ConnectivityService");
1761 }
1762
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001763 private String createTelUrl(String number) {
1764 if (TextUtils.isEmpty(number)) {
1765 return null;
1766 }
1767
Jake Hambye994d462014-02-03 13:10:13 -08001768 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001769 }
1770
Ihab Awadf9e92732013-12-05 18:02:52 -08001771 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001772 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1773 }
1774
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001775 private static void logv(String msg) {
1776 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1777 }
1778
Ihab Awadf9e92732013-12-05 18:02:52 -08001779 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001780 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1781 }
1782
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001783 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001784 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001785 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001786 }
1787
Sanket Padawe356d7632015-06-22 14:03:32 -07001788 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001789 public int getActivePhoneTypeForSlot(int slotId) {
1790 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001791 if (phone == null) {
1792 return PhoneConstants.PHONE_TYPE_NONE;
1793 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001794 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001795 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001796 }
1797
1798 /**
1799 * Returns the CDMA ERI icon index to display
1800 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001801 @Override
1802 public int getCdmaEriIconIndex(String callingPackage) {
1803 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001804 }
1805
Sanket Padawe356d7632015-06-22 14:03:32 -07001806 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001807 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1808 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1809 return -1;
1810 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001811 final Phone phone = getPhone(subId);
1812 if (phone != null) {
1813 return phone.getCdmaEriIconIndex();
1814 } else {
1815 return -1;
1816 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001817 }
1818
1819 /**
1820 * Returns the CDMA ERI icon mode,
1821 * 0 - ON
1822 * 1 - FLASHING
1823 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001824 @Override
1825 public int getCdmaEriIconMode(String callingPackage) {
1826 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001827 }
1828
Sanket Padawe356d7632015-06-22 14:03:32 -07001829 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001830 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1831 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1832 return -1;
1833 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001834 final Phone phone = getPhone(subId);
1835 if (phone != null) {
1836 return phone.getCdmaEriIconMode();
1837 } else {
1838 return -1;
1839 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001840 }
1841
1842 /**
1843 * Returns the CDMA ERI text,
1844 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001845 @Override
1846 public String getCdmaEriText(String callingPackage) {
1847 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001848 }
1849
Sanket Padawe356d7632015-06-22 14:03:32 -07001850 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001851 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1852 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1853 return null;
1854 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001855 final Phone phone = getPhone(subId);
1856 if (phone != null) {
1857 return phone.getCdmaEriText();
1858 } else {
1859 return null;
1860 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001861 }
1862
1863 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001864 * Returns the CDMA MDN.
1865 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001866 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001867 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001868 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001869 final Phone phone = getPhone(subId);
1870 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1871 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001872 } else {
1873 return null;
1874 }
1875 }
1876
1877 /**
1878 * Returns the CDMA MIN.
1879 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001880 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001881 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001882 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001883 final Phone phone = getPhone(subId);
1884 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1885 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001886 } else {
1887 return null;
1888 }
1889 }
1890
1891 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001892 * Returns true if CDMA provisioning needs to run.
1893 */
1894 public boolean needsOtaServiceProvisioning() {
1895 return mPhone.needsOtaServiceProvisioning();
1896 }
1897
1898 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001899 * Sets the voice mail number of a given subId.
1900 */
1901 @Override
1902 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001903 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001904 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1905 new Pair<String, String>(alphaTag, number), new Integer(subId));
1906 return success;
1907 }
1908
Ta-wei Yen87c49842016-05-13 21:19:52 -07001909 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001910 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1911 VisualVoicemailSmsFilterSettings settings) {
1912 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001913 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001914 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1915 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001916 }
1917
1918 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001919 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1920 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001921 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001922 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001923 }
1924
1925 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001926 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
1927 String callingPackage, int subId) {
1928 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001929 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001930 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001931 }
1932
1933 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001934 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
1935 String packageName, int subId) {
1936 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07001937 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001938 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001939 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001940 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001941 * Returns the unread count of voicemails
1942 */
1943 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001944 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001945 }
1946
1947 /**
1948 * Returns the unread count of voicemails for a subId
1949 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001950 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001951 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001952 final Phone phone = getPhone(subId);
1953 if (phone != null) {
1954 return phone.getVoiceMessageCount();
1955 } else {
1956 return 0;
1957 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001958 }
1959
1960 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001961 * Returns the data network type.
1962 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001963 *
1964 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1965 */
1966 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001967 public int getNetworkType() {
1968 final Phone phone = getPhone(getDefaultSubscription());
1969 if (phone != null) {
1970 return phone.getServiceState().getDataNetworkType();
1971 } else {
1972 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1973 }
Wink Saville36469e72014-06-11 15:17:00 -07001974 }
1975
1976 /**
1977 * Returns the network type for a subId
1978 */
1979 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001980 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1981 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1982 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1983 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001984
Sanket Padawe356d7632015-06-22 14:03:32 -07001985 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001986 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001987 return phone.getServiceState().getDataNetworkType();
1988 } else {
1989 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1990 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001991 }
1992
1993 /**
1994 * Returns the data network type
1995 */
1996 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001997 public int getDataNetworkType(String callingPackage) {
1998 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001999 }
2000
2001 /**
2002 * Returns the data network type for a subId
2003 */
2004 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002005 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2006 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2007 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2008 }
2009
Sanket Padawe356d7632015-06-22 14:03:32 -07002010 final Phone phone = getPhone(subId);
2011 if (phone != null) {
2012 return phone.getServiceState().getDataNetworkType();
2013 } else {
2014 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2015 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002016 }
2017
2018 /**
Wink Saville36469e72014-06-11 15:17:00 -07002019 * Returns the Voice network type for a subId
2020 */
2021 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002022 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2023 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2024 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2025 }
2026
Sanket Padawe356d7632015-06-22 14:03:32 -07002027 final Phone phone = getPhone(subId);
2028 if (phone != null) {
2029 return phone.getServiceState().getVoiceNetworkType();
2030 } else {
2031 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2032 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002033 }
2034
2035 /**
2036 * @return true if a ICC card is present
2037 */
2038 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002039 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002040 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002041 }
2042
2043 /**
2044 * @return true if a ICC card is present for a slotId
2045 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002046 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002047 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002048 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002049 final Phone phone = getPhone(subId[0]);
2050 if (subId != null && phone != null) {
2051 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002052 } else {
2053 return false;
2054 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002055 }
2056
2057 /**
2058 * Return if the current radio is LTE on CDMA. This
2059 * is a tri-state return value as for a period of time
2060 * the mode may be unknown.
2061 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002062 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002063 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002064 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002065 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002066 @Override
2067 public int getLteOnCdmaMode(String callingPackage) {
2068 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002069 }
2070
Sanket Padawe356d7632015-06-22 14:03:32 -07002071 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002072 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2073 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2074 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2075 }
2076
Sanket Padawe356d7632015-06-22 14:03:32 -07002077 final Phone phone = getPhone(subId);
2078 if (phone == null) {
2079 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2080 } else {
2081 return phone.getLteOnCdmaMode();
2082 }
Wink Saville36469e72014-06-11 15:17:00 -07002083 }
2084
2085 public void setPhone(Phone phone) {
2086 mPhone = phone;
2087 }
2088
2089 /**
2090 * {@hide}
2091 * Returns Default subId, 0 in the case of single standby.
2092 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002093 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002094 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002095 }
2096
Shishir Agrawala9f32182016-04-12 12:00:16 -07002097 private int getSlotForDefaultSubscription() {
2098 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2099 }
2100
Wink Savilleb564aae2014-10-23 10:18:09 -07002101 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002102 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002103 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002104
2105 /**
2106 * @see android.telephony.TelephonyManager.WifiCallingChoices
2107 */
2108 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002109 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2110 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002111 }
2112
2113 /**
2114 * @see android.telephony.TelephonyManager.WifiCallingChoices
2115 */
2116 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002117 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2118 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2119 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002120 }
2121
Sailesh Nepald1e68152013-12-12 19:08:02 -08002122 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002123 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002124 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002125 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002126
Shishir Agrawal566b7612013-10-28 14:41:00 -07002127 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002128 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2129 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002130
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002131 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002132 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002133 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002134 if (DBG) log("iccOpenLogicalChannel: " + response);
2135 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002136 }
2137
2138 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002139 public boolean iccCloseLogicalChannel(int subId, int channel) {
2140 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002141
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002142 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002143 if (channel < 0) {
2144 return false;
2145 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002146 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002147 if (DBG) log("iccCloseLogicalChannel: " + success);
2148 return success;
2149 }
2150
2151 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002152 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002153 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002154 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002155
2156 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002157 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2158 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002159 " data=" + data);
2160 }
2161
2162 if (channel < 0) {
2163 return "";
2164 }
2165
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002166 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002167 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002168 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2169
Shishir Agrawal566b7612013-10-28 14:41:00 -07002170 // Append the returned status code to the end of the response payload.
2171 String s = Integer.toHexString(
2172 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002173 if (response.payload != null) {
2174 s = IccUtils.bytesToHexString(response.payload) + s;
2175 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002176 return s;
2177 }
Jake Hambye994d462014-02-03 13:10:13 -08002178
Evan Charltonc66da362014-05-16 14:06:40 -07002179 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002180 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002181 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002182 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002183
2184 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002185 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2186 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002187 }
2188
2189 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002190 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002191 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2192
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002193 // Append the returned status code to the end of the response payload.
2194 String s = Integer.toHexString(
2195 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002196 if (response.payload != null) {
2197 s = IccUtils.bytesToHexString(response.payload) + s;
2198 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002199 return s;
2200 }
2201
2202 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002203 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002204 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002205 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002206
2207 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002208 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002209 p1 + " " + p2 + " " + p3 + ":" + filePath);
2210 }
2211
2212 IccIoResult response =
2213 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002214 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2215 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002216
2217 if (DBG) {
2218 log("Exchange SIM_IO [R]" + response);
2219 }
2220
2221 byte[] result = null;
2222 int length = 2;
2223 if (response.payload != null) {
2224 length = 2 + response.payload.length;
2225 result = new byte[length];
2226 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2227 } else {
2228 result = new byte[length];
2229 }
2230
2231 result[length - 1] = (byte) response.sw2;
2232 result[length - 2] = (byte) response.sw1;
2233 return result;
2234 }
2235
2236 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002237 public String sendEnvelopeWithStatus(int subId, String content) {
2238 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002239
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002240 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002241 if (response.payload == null) {
2242 return "";
2243 }
2244
2245 // Append the returned status code to the end of the response payload.
2246 String s = Integer.toHexString(
2247 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2248 s = IccUtils.bytesToHexString(response.payload) + s;
2249 return s;
2250 }
2251
Jake Hambye994d462014-02-03 13:10:13 -08002252 /**
2253 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2254 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2255 *
2256 * @param itemID the ID of the item to read
2257 * @return the NV item as a String, or null on error.
2258 */
2259 @Override
2260 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002261 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002262 if (DBG) log("nvReadItem: item " + itemID);
2263 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2264 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2265 return value;
2266 }
2267
2268 /**
2269 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2270 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2271 *
2272 * @param itemID the ID of the item to read
2273 * @param itemValue the value to write, as a String
2274 * @return true on success; false on any failure
2275 */
2276 @Override
2277 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002278 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002279 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2280 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2281 new Pair<Integer, String>(itemID, itemValue));
2282 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2283 return success;
2284 }
2285
2286 /**
2287 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2288 * Used for device configuration by some CDMA operators.
2289 *
2290 * @param preferredRoamingList byte array containing the new PRL
2291 * @return true on success; false on any failure
2292 */
2293 @Override
2294 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002295 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002296 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2297 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2298 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2299 return success;
2300 }
2301
2302 /**
2303 * Perform the specified type of NV config reset.
2304 * Used for device configuration by some CDMA operators.
2305 *
2306 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2307 * @return true on success; false on any failure
2308 */
2309 @Override
2310 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002311 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002312 if (DBG) log("nvResetConfig: type " + resetType);
2313 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2314 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2315 return success;
2316 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002317
2318 /**
Wink Saville36469e72014-06-11 15:17:00 -07002319 * {@hide}
2320 * Returns Default sim, 0 in the case of single standby.
2321 */
2322 public int getDefaultSim() {
2323 //TODO Need to get it from Telephony Devcontroller
2324 return 0;
2325 }
2326
Svet Ganovb320e182015-04-16 12:30:10 -07002327 public String[] getPcscfAddress(String apnType, String callingPackage) {
2328 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2329 return new String[0];
2330 }
2331
2332
ram87fca6f2014-07-18 18:58:44 +05302333 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002334 }
2335
2336 public void setImsRegistrationState(boolean registered) {
2337 enforceModifyPermission();
2338 mPhone.setImsRegistrationState(registered);
2339 }
2340
2341 /**
Stuart Scott54788802015-03-30 13:18:01 -07002342 * Set the network selection mode to automatic.
2343 *
2344 */
2345 @Override
2346 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002347 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002348 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2349 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2350 }
2351
2352 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002353 * Set the network selection mode to manual with the selected carrier.
2354 */
2355 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002356 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2357 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002358 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002359 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002360 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2361 persistSelection);
2362 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002363 }
2364
2365 /**
2366 * Scans for available networks.
2367 */
2368 @Override
2369 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002370 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002371 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2372 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2373 CMD_PERFORM_NETWORK_SCAN, null, subId);
2374 return result;
2375 }
2376
2377 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002378 * Get the calculated preferred network type.
2379 * Used for debugging incorrect network type.
2380 *
2381 * @return the preferred network type, defined in RILConstants.java.
2382 */
2383 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002384 public int getCalculatedPreferredNetworkType(String callingPackage) {
2385 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2386 return RILConstants.PREFERRED_NETWORK_MODE;
2387 }
2388
Amit Mahajan43330e02014-11-18 11:54:45 -08002389 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002390 }
2391
2392 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002393 * Get the preferred network type.
2394 * Used for device configuration by some CDMA operators.
2395 *
2396 * @return the preferred network type, defined in RILConstants.java.
2397 */
2398 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002399 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002400 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002401 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002402 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002403 int networkType = (result != null ? result[0] : -1);
2404 if (DBG) log("getPreferredNetworkType: " + networkType);
2405 return networkType;
2406 }
2407
2408 /**
2409 * Set the preferred network type.
2410 * Used for device configuration by some CDMA operators.
2411 *
2412 * @param networkType the preferred network type, defined in RILConstants.java.
2413 * @return true on success; false on any failure.
2414 */
2415 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002416 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002417 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002418 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2419 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002420 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002421 if (success) {
2422 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002423 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002424 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002425 return success;
2426 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002427
2428 /**
Junda Liu475951f2014-11-07 16:45:03 -08002429 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2430 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2431 * tethering.
2432 *
2433 * @return 0: Not required. 1: required. 2: Not set.
2434 * @hide
2435 */
2436 @Override
2437 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002438 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002439 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2440 Settings.Global.TETHER_DUN_REQUIRED, 2);
2441 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2442 // config_tether_apndata.
2443 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2444 dunRequired = 1;
2445 }
2446 return dunRequired;
2447 }
2448
2449 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002450 * Set mobile data enabled
2451 * Used by the user through settings etc to turn on/off mobile data
2452 *
2453 * @param enable {@code true} turn turn data on, else {@code false}
2454 */
2455 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002456 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002457 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002458 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002459 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002460 Phone phone = PhoneFactory.getPhone(phoneId);
2461 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002462 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002463 phone.setDataEnabled(enable);
2464 } else {
2465 loge("setDataEnabled: no phone for subId=" + subId);
2466 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002467 }
2468
2469 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002470 * Get whether mobile data is enabled.
2471 *
2472 * Note that this used to be available from ConnectivityService, gated by
2473 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2474 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002475 *
2476 * @return {@code true} if data is enabled else {@code false}
2477 */
2478 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002479 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002480 try {
2481 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2482 null);
2483 } catch (Exception e) {
2484 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2485 null);
2486 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002487 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002488 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002489 Phone phone = PhoneFactory.getPhone(phoneId);
2490 if (phone != null) {
2491 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002492 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002493 return retVal;
2494 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002495 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002496 return false;
2497 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002498 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002499
2500 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002501 public int getCarrierPrivilegeStatus(int subId) {
2502 final Phone phone = getPhone(subId);
2503 if (phone == null) {
2504 loge("getCarrierPrivilegeStatus: Invalid subId");
2505 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2506 }
2507 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002508 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002509 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002510 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2511 }
2512 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002513 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002514 }
Junda Liu29340342014-07-10 15:23:27 -07002515
2516 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002517 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002518 if (TextUtils.isEmpty(pkgName))
2519 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002520 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002521 if (card == null) {
2522 loge("checkCarrierPrivilegesForPackage: No UICC");
2523 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2524 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002525 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2526 }
2527
2528 @Override
2529 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002530 if (TextUtils.isEmpty(pkgName))
2531 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002532 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2533 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2534 UiccCard card = UiccController.getInstance().getUiccCard(i);
2535 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002536 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002537 continue;
2538 }
2539
2540 result = card.getCarrierPrivilegeStatus(
2541 mPhone.getContext().getPackageManager(), pkgName);
2542 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2543 break;
2544 }
2545 }
2546
2547 return result;
Junda Liu29340342014-07-10 15:23:27 -07002548 }
Derek Tan89e89d42014-07-08 17:00:10 -07002549
2550 @Override
Junda Liue64de782015-04-16 17:19:16 -07002551 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2552 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2553 loge("phoneId " + phoneId + " is not valid.");
2554 return null;
2555 }
2556 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002557 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002558 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002559 return null ;
2560 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002561 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002562 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002563 }
2564
Amith Yamasani6e118872016-02-19 12:53:51 -08002565 @Override
2566 public List<String> getPackagesWithCarrierPrivileges() {
2567 PackageManager pm = mPhone.getContext().getPackageManager();
2568 List<String> privilegedPackages = new ArrayList<>();
2569 List<PackageInfo> packages = null;
2570 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2571 UiccCard card = UiccController.getInstance().getUiccCard(i);
2572 if (card == null) {
2573 // No UICC in that slot.
2574 continue;
2575 }
2576 if (card.hasCarrierPrivilegeRules()) {
2577 if (packages == null) {
2578 // Only check packages in user 0 for now
2579 packages = pm.getInstalledPackagesAsUser(
2580 PackageManager.MATCH_DISABLED_COMPONENTS
2581 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2582 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2583 }
2584 for (int p = packages.size() - 1; p >= 0; p--) {
2585 PackageInfo pkgInfo = packages.get(p);
2586 if (pkgInfo != null && pkgInfo.packageName != null
2587 && card.getCarrierPrivilegeStatus(pkgInfo)
2588 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2589 privilegedPackages.add(pkgInfo.packageName);
2590 }
2591 }
2592 }
2593 }
2594 return privilegedPackages;
2595 }
2596
Wink Savilleb564aae2014-10-23 10:18:09 -07002597 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002598 final Phone phone = getPhone(subId);
2599 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002600 if (card == null) {
2601 loge("getIccId: No UICC");
2602 return null;
2603 }
2604 String iccId = card.getIccId();
2605 if (TextUtils.isEmpty(iccId)) {
2606 loge("getIccId: ICC ID is null or empty.");
2607 return null;
2608 }
2609 return iccId;
2610 }
2611
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002612 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002613 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2614 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002615 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002616
Jeff Sharkey85190e62014-12-05 09:40:12 -08002617 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002618 final Phone phone = getPhone(subId);
2619 if (phone == null) {
2620 return false;
2621 }
2622 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002623
2624 if (DBG_MERGE) {
2625 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2626 + subscriberId + " to " + number);
2627 }
2628
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002629 if (TextUtils.isEmpty(iccId)) {
2630 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002631 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002632
Jeff Sharkey85190e62014-12-05 09:40:12 -08002633 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2634
2635 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002636 if (alphaTag == null) {
2637 editor.remove(alphaTagPrefKey);
2638 } else {
2639 editor.putString(alphaTagPrefKey, alphaTag);
2640 }
2641
Jeff Sharkey85190e62014-12-05 09:40:12 -08002642 // Record both the line number and IMSI for this ICCID, since we need to
2643 // track all merged IMSIs based on line number
2644 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2645 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002646 if (number == null) {
2647 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002648 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002649 } else {
2650 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002651 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002652 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002653
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002654 editor.commit();
2655 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002656 }
2657
2658 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002659 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002660 // This is open to apps with WRITE_SMS.
2661 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002662 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002663 return null;
2664 }
Derek Tan97ebb422014-09-05 16:55:38 -07002665
2666 String iccId = getIccId(subId);
2667 if (iccId != null) {
2668 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002669 if (DBG_MERGE) {
2670 log("getLine1NumberForDisplay returning " +
2671 mTelephonySharedPreferences.getString(numberPrefKey, null));
2672 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002673 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002674 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002675 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002676 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002677 }
2678
2679 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002680 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2681 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2682 return null;
2683 }
Derek Tan97ebb422014-09-05 16:55:38 -07002684
2685 String iccId = getIccId(subId);
2686 if (iccId != null) {
2687 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002688 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002689 }
Derek Tan97ebb422014-09-05 16:55:38 -07002690 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002691 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002692
2693 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002694 public String[] getMergedSubscriberIds(String callingPackage) {
2695 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2696 return null;
2697 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002698 final Context context = mPhone.getContext();
2699 final TelephonyManager tele = TelephonyManager.from(context);
2700 final SubscriptionManager sub = SubscriptionManager.from(context);
2701
2702 // Figure out what subscribers are currently active
2703 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002704 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2705 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2706 final long identity = Binder.clearCallingIdentity();
2707 try {
2708 final int[] subIds = sub.getActiveSubscriptionIdList();
2709 for (int subId : subIds) {
2710 activeSubscriberIds.add(tele.getSubscriberId(subId));
2711 }
2712 } finally {
2713 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002714 }
2715
2716 // First pass, find a number override for an active subscriber
2717 String mergeNumber = null;
2718 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2719 for (String key : prefs.keySet()) {
2720 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2721 final String subscriberId = (String) prefs.get(key);
2722 if (activeSubscriberIds.contains(subscriberId)) {
2723 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2724 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2725 mergeNumber = (String) prefs.get(numberKey);
2726 if (DBG_MERGE) {
2727 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2728 + " for active subscriber " + subscriberId);
2729 }
2730 if (!TextUtils.isEmpty(mergeNumber)) {
2731 break;
2732 }
2733 }
2734 }
2735 }
2736
2737 // Shortcut when no active merged subscribers
2738 if (TextUtils.isEmpty(mergeNumber)) {
2739 return null;
2740 }
2741
2742 // Second pass, find all subscribers under that line override
2743 final ArraySet<String> result = new ArraySet<>();
2744 for (String key : prefs.keySet()) {
2745 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2746 final String number = (String) prefs.get(key);
2747 if (mergeNumber.equals(number)) {
2748 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2749 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2750 final String subscriberId = (String) prefs.get(subscriberKey);
2751 if (!TextUtils.isEmpty(subscriberId)) {
2752 result.add(subscriberId);
2753 }
2754 }
2755 }
2756 }
2757
2758 final String[] resultArray = result.toArray(new String[result.size()]);
2759 Arrays.sort(resultArray);
2760 if (DBG_MERGE) {
2761 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2762 }
2763 return resultArray;
2764 }
2765
2766 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002767 public boolean setOperatorBrandOverride(int subId, String brand) {
2768 enforceCarrierPrivilege(subId);
2769 final Phone phone = getPhone(subId);
2770 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002771 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002772
2773 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002774 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002775 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2776 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002777 enforceCarrierPrivilege(subId);
2778 final Phone phone = getPhone(subId);
2779 if (phone == null) {
2780 return false;
2781 }
2782 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002783 cdmaNonRoamingList);
2784 }
2785
2786 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002787 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2788 enforceModifyPermission();
2789
2790 int returnValue = 0;
2791 try {
2792 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2793 if(result.exception == null) {
2794 if (result.result != null) {
2795 byte[] responseData = (byte[])(result.result);
2796 if(responseData.length > oemResp.length) {
2797 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2798 responseData.length + "bytes. Buffer Size is " +
2799 oemResp.length + "bytes.");
2800 }
2801 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2802 returnValue = responseData.length;
2803 }
2804 } else {
2805 CommandException ex = (CommandException) result.exception;
2806 returnValue = ex.getCommandError().ordinal();
2807 if(returnValue > 0) returnValue *= -1;
2808 }
2809 } catch (RuntimeException e) {
2810 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2811 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2812 if(returnValue > 0) returnValue *= -1;
2813 }
2814
2815 return returnValue;
2816 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002817
2818 @Override
2819 public void setRadioCapability(RadioAccessFamily[] rafs) {
2820 try {
2821 ProxyController.getInstance().setRadioCapability(rafs);
2822 } catch (RuntimeException e) {
2823 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2824 }
2825 }
2826
2827 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002828 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2829 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2830 return RadioAccessFamily.RAF_UNKNOWN;
2831 }
2832
Wink Saville5d475dd2014-10-17 15:00:58 -07002833 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2834 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002835
2836 @Override
2837 public void enableVideoCalling(boolean enable) {
2838 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002839 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002840 }
2841
2842 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002843 public boolean isVideoCallingEnabled(String callingPackage) {
2844 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2845 return false;
2846 }
2847
Andrew Lee77527ac2014-10-21 16:57:39 -07002848 // Check the user preference and the system-level IMS setting. Even if the user has
2849 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2850 // In the long run, we may instead need to check if there exists a connection service
2851 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002852 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2853 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002854 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002855 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002856
Andrew Leea1239f22015-03-02 17:44:07 -08002857 @Override
2858 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002859 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002860 }
2861
2862 @Override
2863 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002864 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002865 }
2866
Andrew Lee9431b832015-03-09 18:46:45 -07002867 @Override
2868 public boolean isTtyModeSupported() {
2869 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2870 TelephonyManager telephonyManager =
2871 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08002872 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07002873 }
2874
2875 @Override
2876 public boolean isHearingAidCompatibilitySupported() {
2877 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2878 }
2879
Sanket Padawe7310cc72015-01-14 09:53:20 -08002880 /**
2881 * Returns the unique device ID of phone, for example, the IMEI for
2882 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2883 *
2884 * <p>Requires Permission:
2885 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2886 */
2887 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002888 public String getDeviceId(String callingPackage) {
2889 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2890 return null;
2891 }
2892
Sanket Padawe7310cc72015-01-14 09:53:20 -08002893 final Phone phone = PhoneFactory.getPhone(0);
2894 if (phone != null) {
2895 return phone.getDeviceId();
2896 } else {
2897 return null;
2898 }
2899 }
2900
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002901 /*
2902 * {@hide}
2903 * Returns the IMS Registration Status
2904 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002905 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002906 public boolean isImsRegistered() {
2907 return mPhone.isImsRegistered();
2908 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002909
2910 @Override
2911 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2912 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2913 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002914
Nathan Haroldc55097a2015-03-11 18:14:50 -07002915 /*
2916 * {@hide}
2917 * Returns the IMS Registration Status
2918 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002919 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002920 return mPhone.isWifiCallingEnabled();
2921 }
2922
2923 /*
2924 * {@hide}
2925 * Returns the IMS Registration Status
2926 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002927 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002928 return mPhone.isVolteEnabled();
2929 }
Svet Ganovb320e182015-04-16 12:30:10 -07002930
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002931 /*
2932 * {@hide} Returns the IMS Registration Status
2933 */
2934 public boolean isVideoTelephonyAvailable() {
2935 return mPhone.isVideoEnabled();
2936 }
2937
Svet Ganovb320e182015-04-16 12:30:10 -07002938 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002939 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002940 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002941 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2942
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002943 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002944 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002945 } catch (SecurityException e) {
2946 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2947 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002948 }
Svet Ganovb320e182015-04-16 12:30:10 -07002949
2950 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2951 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2952 return false;
2953 }
2954
2955 return true;
2956 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002957
Makoto Onukifee69342015-06-29 14:44:50 -07002958 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002959 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002960 */
2961 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002962 // Default SMS app can always read it.
2963 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2964 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2965 return true;
2966 }
2967 try {
2968 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002969 } catch (SecurityException readPhoneStateSecurityException) {
2970 try {
2971 // Can be read with READ_SMS too.
2972 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2973 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2974 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2975 } catch (SecurityException readSmsSecurityException) {
2976 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2977 // permissions
2978 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2979 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2980 " or " + android.Manifest.permission.READ_SMS + ".");
2981 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002982 }
Makoto Onukifee69342015-06-29 14:44:50 -07002983 }
2984
Stuart Scott8eef64f2015-04-08 15:13:54 -07002985 @Override
2986 public void factoryReset(int subId) {
2987 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002988 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2989 return;
2990 }
2991
Svet Ganovcc087f82015-05-12 20:35:54 -07002992 final long identity = Binder.clearCallingIdentity();
2993 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002994 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2995 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002996 // Enable data
2997 setDataEnabled(subId, true);
2998 // Set network selection mode to automatic
2999 setNetworkSelectionModeAutomatic(subId);
3000 // Set preferred mobile network type to the best available
3001 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3002 // Turn off roaming
3003 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3004 }
3005 } finally {
3006 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003007 }
3008 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003009
3010 @Override
3011 public String getLocaleFromDefaultSim() {
3012 // We query all subscriptions instead of just the active ones, because
3013 // this might be called early on in the provisioning flow when the
3014 // subscriptions potentially aren't active yet.
3015 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3016 if (slist == null || slist.isEmpty()) {
3017 return null;
3018 }
3019
3020 // This function may be called very early, say, from the setup wizard, at
3021 // which point we won't have a default subscription set. If that's the case
3022 // we just choose the first, which will be valid in "most cases".
3023 final int defaultSubId = getDefaultSubscription();
3024 SubscriptionInfo info = null;
3025 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3026 info = slist.get(0);
3027 } else {
3028 for (SubscriptionInfo item : slist) {
3029 if (item.getSubscriptionId() == defaultSubId) {
3030 info = item;
3031 break;
3032 }
3033 }
3034
3035 if (info == null) {
3036 return null;
3037 }
3038 }
3039
3040 // Try and fetch the locale from the carrier properties or from the SIM language
3041 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003042 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003043 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003044 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003045 if (defaultPhone != null) {
3046 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3047 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003048 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003049 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3050 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003051 } else {
3052 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003053 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003054 }
3055 }
3056
Narayan Kamath011676f2015-07-29 12:04:08 +01003057 // The SIM language preferences only store a language (e.g. fr = French), not an
3058 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3059 // the SIM and carrier preferences does not include a country we add the country
3060 // determined from the SIM MCC to provide an exact locale.
3061 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003062 if (mccLocale != null) {
3063 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3064 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003065 }
3066
Tony Hill183b2de2015-06-24 14:53:58 +01003067 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003068 return null;
3069 }
3070
3071 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3072 final long identity = Binder.clearCallingIdentity();
3073 try {
3074 return mSubscriptionController.getAllSubInfoList(
3075 mPhone.getContext().getOpPackageName());
3076 } finally {
3077 Binder.restoreCallingIdentity(identity);
3078 }
3079 }
3080
3081 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3082 final long identity = Binder.clearCallingIdentity();
3083 try {
3084 return mSubscriptionController.getActiveSubscriptionInfoList(
3085 mPhone.getContext().getOpPackageName());
3086 } finally {
3087 Binder.restoreCallingIdentity(identity);
3088 }
3089 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003090
3091 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003092 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3093 * representing the state of the modem.
3094 *
3095 * NOTE: This clears the modem state, so there should only every be one caller.
3096 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003097 */
3098 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003099 public void requestModemActivityInfo(ResultReceiver result) {
3100 enforceModifyPermission();
3101
3102 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3103 Bundle bundle = new Bundle();
3104 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3105 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003106 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003107
3108 /**
3109 * {@hide}
3110 * Returns the service state information on specified subscription.
3111 */
3112 @Override
3113 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3114
3115 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3116 return null;
3117 }
3118
3119 final Phone phone = getPhone(subId);
3120 if (phone == null) {
3121 return null;
3122 }
3123
3124 return phone.getServiceState();
3125 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003126
3127 /**
3128 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3129 *
3130 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3131 * voicemail ringtone.
3132 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3133 * PhoneAccount.
3134 */
3135 @Override
3136 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3137 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3138 if (phone == null) {
3139 return null;
3140 }
3141
3142 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3143 }
3144
3145 /**
3146 * Returns whether vibration is set for voicemail notification in Phone settings.
3147 *
3148 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3149 * voicemail vibration setting.
3150 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3151 */
3152 @Override
3153 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3154 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3155 if (phone == null) {
3156 return false;
3157 }
3158
3159 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3160 }
3161
Youhan Wange64578a2016-05-02 15:32:42 -07003162 /**
3163 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3164 *
3165 * @throws SecurityException if the caller does not have the required permission
3166 */
3167 private void enforceReadPrivilegedPermission() {
3168 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3169 null);
3170 }
3171
3172 /**
3173 * Return the application ID for the app type.
3174 *
3175 * @param subId the subscription ID that this request applies to.
3176 * @param appType the uicc app type.
3177 * @return Application ID for specificied app type, or null if no uicc.
3178 */
3179 @Override
3180 public String getAidForAppType(int subId, int appType) {
3181 enforceReadPrivilegedPermission();
3182 Phone phone = getPhone(subId);
3183 if (phone == null) {
3184 return null;
3185 }
3186 String aid = null;
3187 try {
3188 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3189 .getApplicationByType(appType).getAid();
3190 } catch (Exception e) {
3191 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3192 }
3193 return aid;
3194 }
3195
Youhan Wang4001d252016-05-11 10:29:41 -07003196 /**
3197 * Return the Electronic Serial Number.
3198 *
3199 * @param subId the subscription ID that this request applies to.
3200 * @return ESN or null if error.
3201 */
3202 @Override
3203 public String getEsn(int subId) {
3204 enforceReadPrivilegedPermission();
3205 Phone phone = getPhone(subId);
3206 if (phone == null) {
3207 return null;
3208 }
3209 String esn = null;
3210 try {
3211 esn = phone.getEsn();
3212 } catch (Exception e) {
3213 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3214 }
3215 return esn;
3216 }
3217
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003218 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003219 * Return the Preferred Roaming List Version.
3220 *
3221 * @param subId the subscription ID that this request applies to.
3222 * @return PRLVersion or null if error.
3223 */
3224 @Override
3225 public String getCdmaPrlVersion(int subId) {
3226 enforceReadPrivilegedPermission();
3227 Phone phone = getPhone(subId);
3228 if (phone == null) {
3229 return null;
3230 }
3231 String cdmaPrlVersion = null;
3232 try {
3233 cdmaPrlVersion = phone.getCdmaPrlVersion();
3234 } catch (Exception e) {
3235 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3236 }
3237 return cdmaPrlVersion;
3238 }
3239
3240 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003241 * Get snapshot of Telephony histograms
3242 * @return List of Telephony histograms
3243 * @hide
3244 */
3245 @Override
3246 public List<TelephonyHistogram> getTelephonyHistograms() {
3247 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3248 return RIL.getTelephonyRILTimingHistograms();
3249 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003250
3251 /**
3252 * {@hide}
3253 * Set the allowed carrier list for slotId
3254 * Require system privileges. In the future we may add this to carrier APIs.
3255 *
3256 * @return The number of carriers set successfully, should match length of carriers
3257 */
3258 @Override
3259 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3260 enforceModifyPermission();
3261 int subId = SubscriptionManager.getSubId(slotId)[0];
3262 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3263 return retVal[0];
3264 }
3265
3266 /**
3267 * {@hide}
3268 * Get the allowed carrier list for slotId.
3269 * Require system privileges. In the future we may add this to carrier APIs.
3270 *
3271 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3272 * means all carriers are allowed.
3273 */
3274 @Override
3275 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3276 enforceReadPrivilegedPermission();
3277 int subId = SubscriptionManager.getSubId(slotId)[0];
3278 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3279 }
3280
fionaxu59545b42016-05-25 15:53:37 -07003281 /**
3282 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3283 * @param subId the subscription ID that this action applies to.
3284 * @param enabled control enable or disable metered apns.
3285 * {@hide}
3286 */
3287 @Override
3288 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3289 enforceModifyPermission();
3290 final Phone phone = getPhone(subId);
3291 if (phone == null) {
3292 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3293 return;
3294 }
3295 try {
3296 phone.carrierActionSetMeteredApnsEnabled(enabled);
3297 } catch (Exception e) {
3298 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3299 }
3300 }
3301
3302 /**
3303 * Action set from carrier signalling broadcast receivers to enable/disable radio
3304 * @param subId the subscription ID that this action applies to.
3305 * @param enabled control enable or disable radio.
3306 * {@hide}
3307 */
3308 @Override
3309 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3310 enforceModifyPermission();
3311 final Phone phone = getPhone(subId);
3312 if (phone == null) {
3313 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3314 return;
3315 }
3316 try {
3317 phone.carrierActionSetRadioEnabled(enabled);
3318 } catch (Exception e) {
3319 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3320 }
3321 }
3322
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003323 /**
3324 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3325 * bug report is being generated.
3326 */
3327 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003328 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003329 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3330 != PackageManager.PERMISSION_GRANTED) {
3331 writer.println("Permission Denial: can't dump Phone from pid="
3332 + Binder.getCallingPid()
3333 + ", uid=" + Binder.getCallingUid()
3334 + "without permission "
3335 + android.Manifest.permission.DUMP);
3336 return;
3337 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003338 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003339 }
Jack Yueb89b242016-06-22 13:27:47 -07003340
3341 /**
3342 * Get aggregated video call data usage from all subscriptions since boot.
3343 * @return total data usage in bytes
3344 * {@hide}
3345 */
3346 @Override
3347 public long getVtDataUsage() {
3348 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3349 null);
3350
3351 // NetworkStatsService keeps tracking the active network interface and identity. It will
3352 // record the delta with the corresponding network identity. What we need to do here is
3353 // returning total video call data usage from all subscriptions since boot.
3354
3355 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3356 // simultaneous VT calls.
3357 final Phone[] phones = PhoneFactory.getPhones();
3358 long total = 0;
3359 for (Phone phone : phones) {
3360 total += phone.getVtDataUsage();
3361 }
3362 return total;
3363 }
Jack Yu75ab2952016-07-08 14:29:33 -07003364
3365 /**
3366 * Policy control of data connection. Usually used when data limit is passed.
3367 * @param enabled True if enabling the data, otherwise disabling.
3368 * @param subId Subscription index
3369 * {@hide}
3370 */
3371 @Override
3372 public void setPolicyDataEnabled(boolean enabled, int subId) {
3373 enforceModifyPermission();
3374 Phone phone = getPhone(subId);
3375 if (phone != null) {
3376 phone.setPolicyDataEnabled(enabled);
3377 }
3378 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003379}