blob: 45bea8a72a397765a1715e098d91d7b6885f738a [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;
Andrew Lee312e8172014-10-23 17:01:36 -070064import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070065import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070066import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070067import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080070import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010071import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070072import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070074import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070075import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070076import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070077import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070078import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080079import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070080import com.android.internal.telephony.uicc.IccIoResult;
81import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070082import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070083import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080084import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -070085import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -080086import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yenc236d6b2016-06-21 13:33:12 -070087import java.io.FileDescriptor;
88import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080090import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080091import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010092import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080093import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094
95/**
96 * Implementation of the ITelephony interface.
97 */
Santos Cordon117fee72014-05-16 17:56:12 -070098public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070099 private static final String LOG_TAG = "PhoneInterfaceManager";
100 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
101 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800102 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700103
104 // Message codes used with mMainThreadHandler
105 private static final int CMD_HANDLE_PIN_MMI = 1;
106 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
107 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
108 private static final int CMD_ANSWER_RINGING_CALL = 4;
109 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700110 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
111 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700112 private static final int CMD_OPEN_CHANNEL = 9;
113 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
114 private static final int CMD_CLOSE_CHANNEL = 11;
115 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800116 private static final int CMD_NV_READ_ITEM = 13;
117 private static final int EVENT_NV_READ_ITEM_DONE = 14;
118 private static final int CMD_NV_WRITE_ITEM = 15;
119 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
120 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
121 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
122 private static final int CMD_NV_RESET_CONFIG = 19;
123 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800124 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
125 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
126 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
127 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800128 private static final int CMD_SEND_ENVELOPE = 25;
129 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700130 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
131 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
132 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
133 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
134 private static final int CMD_EXCHANGE_SIM_IO = 31;
135 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800136 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
137 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700138 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
139 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700140 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
141 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700142 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
143 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
144 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
145 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700146 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
147 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
148 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
149 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150
151 /** The singleton instance. */
152 private static PhoneInterfaceManager sInstance;
153
Wink Saville3ab207e2014-11-20 13:07:20 -0800154 private PhoneGlobals mApp;
155 private Phone mPhone;
156 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700157 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800158 private AppOpsManager mAppOps;
159 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800160 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800161 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700162
Derek Tan97ebb422014-09-05 16:55:38 -0700163 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
164 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800165 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700166
167 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700168 * A request object to use for transmitting data to an ICC.
169 */
170 private static final class IccAPDUArgument {
171 public int channel, cla, command, p1, p2, p3;
172 public String data;
173
174 public IccAPDUArgument(int channel, int cla, int command,
175 int p1, int p2, int p3, String data) {
176 this.channel = channel;
177 this.cla = cla;
178 this.command = command;
179 this.p1 = p1;
180 this.p2 = p2;
181 this.p3 = p3;
182 this.data = data;
183 }
184 }
185
186 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700187 * A request object to use for transmitting data to an ICC.
188 */
189 private static final class ManualNetworkSelectionArgument {
190 public OperatorInfo operatorInfo;
191 public boolean persistSelection;
192
193 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
194 this.operatorInfo = operatorInfo;
195 this.persistSelection = persistSelection;
196 }
197 }
198
199 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
201 * request after sending. The main thread will notify the request when it is complete.
202 */
203 private static final class MainThreadRequest {
204 /** The argument to use for the request */
205 public Object argument;
206 /** The result of the request that is run on the main thread */
207 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800208 // The subscriber id that this request applies to. Defaults to
209 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
210 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211
212 public MainThreadRequest(Object argument) {
213 this.argument = argument;
214 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800215
216 public MainThreadRequest(Object argument, Integer subId) {
217 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800218 if (subId != null) {
219 this.subId = subId;
220 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800221 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 }
223
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800224 private static final class IncomingThirdPartyCallArgs {
225 public final ComponentName component;
226 public final String callId;
227 public final String callerDisplayName;
228
229 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
230 String callerDisplayName) {
231 this.component = component;
232 this.callId = callId;
233 this.callerDisplayName = callerDisplayName;
234 }
235 }
236
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700237 /**
238 * A handler that processes messages on the main thread in the phone process. Since many
239 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
240 * inbound binder threads to the main thread in the phone process. The Binder thread
241 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
242 * on, which will be notified when the operation completes and will contain the result of the
243 * request.
244 *
245 * <p>If a MainThreadRequest object is provided in the msg.obj field,
246 * note that request.result must be set to something non-null for the calling thread to
247 * unblock.
248 */
249 private final class MainThreadHandler extends Handler {
250 @Override
251 public void handleMessage(Message msg) {
252 MainThreadRequest request;
253 Message onCompleted;
254 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800255 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700256 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257
258 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700259 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700261 final Phone phone = getPhoneFromRequest(request);
262 request.result = phone != null ?
263 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
264 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700265 // Wake up the requesting thread
266 synchronized (request) {
267 request.notifyAll();
268 }
269 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700270 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700271
272 case CMD_HANDLE_NEIGHBORING_CELL:
273 request = (MainThreadRequest) msg.obj;
274 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
275 request);
276 mPhone.getNeighboringCids(onCompleted);
277 break;
278
279 case EVENT_NEIGHBORING_CELL_DONE:
280 ar = (AsyncResult) msg.obj;
281 request = (MainThreadRequest) ar.userObj;
282 if (ar.exception == null && ar.result != null) {
283 request.result = ar.result;
284 } else {
285 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800286 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700287 }
288 // Wake up the requesting thread
289 synchronized (request) {
290 request.notifyAll();
291 }
292 break;
293
294 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700295 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800296 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700297 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298 break;
299
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300 case CMD_END_CALL:
301 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800302 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700303 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700304 Phone phone = getPhone(end_subId);
305 if (phone == null) {
306 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
307 break;
308 }
309 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700310 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
311 // CDMA: If the user presses the Power button we treat it as
312 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700313 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700314 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
315 // GSM: End the call as per the Phone state
316 hungUp = PhoneUtils.hangup(mCM);
317 } else {
318 throw new IllegalStateException("Unexpected phone type: " + phoneType);
319 }
320 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
321 request.result = hungUp;
322 // Wake up the requesting thread
323 synchronized (request) {
324 request.notifyAll();
325 }
326 break;
327
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700328 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700329 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700330 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800331 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700332 if (uiccCard == null) {
333 loge("iccTransmitApduLogicalChannel: No UICC");
334 request.result = new IccIoResult(0x6F, 0, (byte[])null);
335 synchronized (request) {
336 request.notifyAll();
337 }
338 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700339 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
340 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700341 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700342 iccArgument.channel, iccArgument.cla, iccArgument.command,
343 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700344 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700345 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700346 break;
347
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700348 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700349 ar = (AsyncResult) msg.obj;
350 request = (MainThreadRequest) ar.userObj;
351 if (ar.exception == null && ar.result != null) {
352 request.result = ar.result;
353 } else {
354 request.result = new IccIoResult(0x6F, 0, (byte[])null);
355 if (ar.result == null) {
356 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800357 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700358 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800359 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700360 } else {
361 loge("iccTransmitApduLogicalChannel: Unknown exception");
362 }
363 }
364 synchronized (request) {
365 request.notifyAll();
366 }
367 break;
368
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700369 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
370 request = (MainThreadRequest) msg.obj;
371 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800372 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700373 if (uiccCard == null) {
374 loge("iccTransmitApduBasicChannel: No UICC");
375 request.result = new IccIoResult(0x6F, 0, (byte[])null);
376 synchronized (request) {
377 request.notifyAll();
378 }
379 } else {
380 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
381 request);
382 uiccCard.iccTransmitApduBasicChannel(
383 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
384 iccArgument.p3, iccArgument.data, onCompleted);
385 }
386 break;
387
388 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
389 ar = (AsyncResult) msg.obj;
390 request = (MainThreadRequest) ar.userObj;
391 if (ar.exception == null && ar.result != null) {
392 request.result = ar.result;
393 } else {
394 request.result = new IccIoResult(0x6F, 0, (byte[])null);
395 if (ar.result == null) {
396 loge("iccTransmitApduBasicChannel: Empty response");
397 } else if (ar.exception instanceof CommandException) {
398 loge("iccTransmitApduBasicChannel: CommandException: " +
399 ar.exception);
400 } else {
401 loge("iccTransmitApduBasicChannel: Unknown exception");
402 }
403 }
404 synchronized (request) {
405 request.notifyAll();
406 }
407 break;
408
409 case CMD_EXCHANGE_SIM_IO:
410 request = (MainThreadRequest) msg.obj;
411 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800412 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700413 if (uiccCard == null) {
414 loge("iccExchangeSimIO: No UICC");
415 request.result = new IccIoResult(0x6F, 0, (byte[])null);
416 synchronized (request) {
417 request.notifyAll();
418 }
419 } else {
420 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
421 request);
422 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
423 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
424 iccArgument.data, onCompleted);
425 }
426 break;
427
428 case EVENT_EXCHANGE_SIM_IO_DONE:
429 ar = (AsyncResult) msg.obj;
430 request = (MainThreadRequest) ar.userObj;
431 if (ar.exception == null && ar.result != null) {
432 request.result = ar.result;
433 } else {
434 request.result = new IccIoResult(0x6f, 0, (byte[])null);
435 }
436 synchronized (request) {
437 request.notifyAll();
438 }
439 break;
440
Derek Tan4d5e5c12014-02-04 11:54:58 -0800441 case CMD_SEND_ENVELOPE:
442 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800443 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700444 if (uiccCard == null) {
445 loge("sendEnvelopeWithStatus: No UICC");
446 request.result = new IccIoResult(0x6F, 0, (byte[])null);
447 synchronized (request) {
448 request.notifyAll();
449 }
450 } else {
451 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
452 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
453 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800454 break;
455
456 case EVENT_SEND_ENVELOPE_DONE:
457 ar = (AsyncResult) msg.obj;
458 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700459 if (ar.exception == null && ar.result != null) {
460 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800461 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700462 request.result = new IccIoResult(0x6F, 0, (byte[])null);
463 if (ar.result == null) {
464 loge("sendEnvelopeWithStatus: Empty response");
465 } else if (ar.exception instanceof CommandException) {
466 loge("sendEnvelopeWithStatus: CommandException: " +
467 ar.exception);
468 } else {
469 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
470 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800471 }
472 synchronized (request) {
473 request.notifyAll();
474 }
475 break;
476
Shishir Agrawal566b7612013-10-28 14:41:00 -0700477 case CMD_OPEN_CHANNEL:
478 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800479 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700480 if (uiccCard == null) {
481 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800482 request.result = new IccOpenLogicalChannelResponse(-1,
483 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700484 synchronized (request) {
485 request.notifyAll();
486 }
487 } else {
488 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
489 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
490 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700491 break;
492
493 case EVENT_OPEN_CHANNEL_DONE:
494 ar = (AsyncResult) msg.obj;
495 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700496 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700497 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700498 int[] result = (int[]) ar.result;
499 int channelId = result[0];
500 byte[] selectResponse = null;
501 if (result.length > 1) {
502 selectResponse = new byte[result.length - 1];
503 for (int i = 1; i < result.length; ++i) {
504 selectResponse[i - 1] = (byte) result[i];
505 }
506 }
507 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700508 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700509 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 if (ar.result == null) {
511 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700512 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700513 if (ar.exception != null) {
514 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
515 }
516
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700517 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700518 if (ar.exception instanceof CommandException) {
519 CommandException.Error error =
520 ((CommandException) (ar.exception)).getCommandError();
521 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700522 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700523 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700524 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700525 }
526 }
527 openChannelResp = new IccOpenLogicalChannelResponse(
528 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700529 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700530 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700531 synchronized (request) {
532 request.notifyAll();
533 }
534 break;
535
536 case CMD_CLOSE_CHANNEL:
537 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800538 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700539 if (uiccCard == null) {
540 loge("iccCloseLogicalChannel: No UICC");
541 request.result = new IccIoResult(0x6F, 0, (byte[])null);
542 synchronized (request) {
543 request.notifyAll();
544 }
545 } else {
546 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
547 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
548 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700549 break;
550
551 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800552 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
553 break;
554
555 case CMD_NV_READ_ITEM:
556 request = (MainThreadRequest) msg.obj;
557 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
558 mPhone.nvReadItem((Integer) request.argument, onCompleted);
559 break;
560
561 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700562 ar = (AsyncResult) msg.obj;
563 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800564 if (ar.exception == null && ar.result != null) {
565 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700566 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800567 request.result = "";
568 if (ar.result == null) {
569 loge("nvReadItem: Empty response");
570 } else if (ar.exception instanceof CommandException) {
571 loge("nvReadItem: CommandException: " +
572 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700573 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800574 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700575 }
576 }
577 synchronized (request) {
578 request.notifyAll();
579 }
580 break;
581
Jake Hambye994d462014-02-03 13:10:13 -0800582 case CMD_NV_WRITE_ITEM:
583 request = (MainThreadRequest) msg.obj;
584 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
585 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
586 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
587 break;
588
589 case EVENT_NV_WRITE_ITEM_DONE:
590 handleNullReturnEvent(msg, "nvWriteItem");
591 break;
592
593 case CMD_NV_WRITE_CDMA_PRL:
594 request = (MainThreadRequest) msg.obj;
595 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
596 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
597 break;
598
599 case EVENT_NV_WRITE_CDMA_PRL_DONE:
600 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
601 break;
602
603 case CMD_NV_RESET_CONFIG:
604 request = (MainThreadRequest) msg.obj;
605 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
606 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
607 break;
608
609 case EVENT_NV_RESET_CONFIG_DONE:
610 handleNullReturnEvent(msg, "nvResetConfig");
611 break;
612
Jake Hamby7c27be32014-03-03 13:25:59 -0800613 case CMD_GET_PREFERRED_NETWORK_TYPE:
614 request = (MainThreadRequest) msg.obj;
615 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700616 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800617 break;
618
619 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
620 ar = (AsyncResult) msg.obj;
621 request = (MainThreadRequest) ar.userObj;
622 if (ar.exception == null && ar.result != null) {
623 request.result = ar.result; // Integer
624 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800625 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800626 if (ar.result == null) {
627 loge("getPreferredNetworkType: Empty response");
628 } else if (ar.exception instanceof CommandException) {
629 loge("getPreferredNetworkType: CommandException: " +
630 ar.exception);
631 } else {
632 loge("getPreferredNetworkType: Unknown exception");
633 }
634 }
635 synchronized (request) {
636 request.notifyAll();
637 }
638 break;
639
640 case CMD_SET_PREFERRED_NETWORK_TYPE:
641 request = (MainThreadRequest) msg.obj;
642 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
643 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700644 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800645 break;
646
647 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
648 handleNullReturnEvent(msg, "setPreferredNetworkType");
649 break;
650
Steven Liu4bf01bc2014-07-17 11:05:29 -0500651 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
652 request = (MainThreadRequest)msg.obj;
653 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
654 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
655 break;
656
657 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
658 ar = (AsyncResult)msg.obj;
659 request = (MainThreadRequest)ar.userObj;
660 request.result = ar;
661 synchronized (request) {
662 request.notifyAll();
663 }
664 break;
665
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800666 case CMD_SET_VOICEMAIL_NUMBER:
667 request = (MainThreadRequest) msg.obj;
668 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
669 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800670 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
671 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800672 break;
673
674 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
675 handleNullReturnEvent(msg, "setVoicemailNumber");
676 break;
677
Stuart Scott54788802015-03-30 13:18:01 -0700678 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
679 request = (MainThreadRequest) msg.obj;
680 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
681 request);
682 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
683 break;
684
685 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
686 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
687 break;
688
Shishir Agrawal302c8692015-06-19 13:49:39 -0700689 case CMD_PERFORM_NETWORK_SCAN:
690 request = (MainThreadRequest) msg.obj;
691 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
692 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
693 break;
694
695 case EVENT_PERFORM_NETWORK_SCAN_DONE:
696 ar = (AsyncResult) msg.obj;
697 request = (MainThreadRequest) ar.userObj;
698 CellNetworkScanResult cellScanResult;
699 if (ar.exception == null && ar.result != null) {
700 cellScanResult = new CellNetworkScanResult(
701 CellNetworkScanResult.STATUS_SUCCESS,
702 (List<OperatorInfo>) ar.result);
703 } else {
704 if (ar.result == null) {
705 loge("getCellNetworkScanResults: Empty response");
706 }
707 if (ar.exception != null) {
708 loge("getCellNetworkScanResults: Exception: " + ar.exception);
709 }
710 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
711 if (ar.exception instanceof CommandException) {
712 CommandException.Error error =
713 ((CommandException) (ar.exception)).getCommandError();
714 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
715 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
716 } else if (error == CommandException.Error.GENERIC_FAILURE) {
717 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
718 }
719 }
720 cellScanResult = new CellNetworkScanResult(errorCode, null);
721 }
722 request.result = cellScanResult;
723 synchronized (request) {
724 request.notifyAll();
725 }
726 break;
727
728 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
729 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700730 ManualNetworkSelectionArgument selArg =
731 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700732 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
733 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700734 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
735 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700736 break;
737
738 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
739 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
740 break;
741
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700742 case CMD_GET_MODEM_ACTIVITY_INFO:
743 request = (MainThreadRequest) msg.obj;
744 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700745 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700746 break;
747
748 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
749 ar = (AsyncResult) msg.obj;
750 request = (MainThreadRequest) ar.userObj;
751 if (ar.exception == null && ar.result != null) {
752 request.result = ar.result;
753 } else {
754 if (ar.result == null) {
755 loge("queryModemActivityInfo: Empty response");
756 } else if (ar.exception instanceof CommandException) {
757 loge("queryModemActivityInfo: CommandException: " +
758 ar.exception);
759 } else {
760 loge("queryModemActivityInfo: Unknown exception");
761 }
762 }
Amit Mahajand4766222016-01-28 15:28:28 -0800763 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
764 if (request.result == null) {
765 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
766 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700767 synchronized (request) {
768 request.notifyAll();
769 }
770 break;
771
Meng Wang1a7c35a2016-05-05 20:56:15 -0700772 case CMD_SET_ALLOWED_CARRIERS:
773 request = (MainThreadRequest) msg.obj;
774 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
775 mPhone.setAllowedCarriers(
776 (List<CarrierIdentifier>) request.argument,
777 onCompleted);
778 break;
779
780 case EVENT_SET_ALLOWED_CARRIERS_DONE:
781 ar = (AsyncResult) msg.obj;
782 request = (MainThreadRequest) ar.userObj;
783 if (ar.exception == null && ar.result != null) {
784 request.result = ar.result;
785 } else {
786 if (ar.result == null) {
787 loge("setAllowedCarriers: Empty response");
788 } else if (ar.exception instanceof CommandException) {
789 loge("setAllowedCarriers: CommandException: " +
790 ar.exception);
791 } else {
792 loge("setAllowedCarriers: Unknown exception");
793 }
794 }
795 // Result cannot be null. Return -1 on error.
796 if (request.result == null) {
797 request.result = new int[]{-1};
798 }
799 synchronized (request) {
800 request.notifyAll();
801 }
802 break;
803
804 case CMD_GET_ALLOWED_CARRIERS:
805 request = (MainThreadRequest) msg.obj;
806 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
807 mPhone.getAllowedCarriers(onCompleted);
808 break;
809
810 case EVENT_GET_ALLOWED_CARRIERS_DONE:
811 ar = (AsyncResult) msg.obj;
812 request = (MainThreadRequest) ar.userObj;
813 if (ar.exception == null && ar.result != null) {
814 request.result = ar.result;
815 } else {
816 if (ar.result == null) {
817 loge("getAllowedCarriers: Empty response");
818 } else if (ar.exception instanceof CommandException) {
819 loge("getAllowedCarriers: CommandException: " +
820 ar.exception);
821 } else {
822 loge("getAllowedCarriers: Unknown exception");
823 }
824 }
825 // Result cannot be null. Return empty list of CarrierIdentifier.
826 if (request.result == null) {
827 request.result = new ArrayList<CarrierIdentifier>(0);
828 }
829 synchronized (request) {
830 request.notifyAll();
831 }
832 break;
833
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 default:
835 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
836 break;
837 }
838 }
Jake Hambye994d462014-02-03 13:10:13 -0800839
840 private void handleNullReturnEvent(Message msg, String command) {
841 AsyncResult ar = (AsyncResult) msg.obj;
842 MainThreadRequest request = (MainThreadRequest) ar.userObj;
843 if (ar.exception == null) {
844 request.result = true;
845 } else {
846 request.result = false;
847 if (ar.exception instanceof CommandException) {
848 loge(command + ": CommandException: " + ar.exception);
849 } else {
850 loge(command + ": Unknown exception");
851 }
852 }
853 synchronized (request) {
854 request.notifyAll();
855 }
856 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700857 }
858
859 /**
860 * Posts the specified command to be executed on the main thread,
861 * waits for the request to complete, and returns the result.
862 * @see #sendRequestAsync
863 */
864 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800865 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700866 }
867
868 /**
869 * Posts the specified command to be executed on the main thread,
870 * waits for the request to complete, and returns the result.
871 * @see #sendRequestAsync
872 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800873 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700874 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
875 throw new RuntimeException("This method will deadlock if called from the main thread.");
876 }
877
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800878 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700879 Message msg = mMainThreadHandler.obtainMessage(command, request);
880 msg.sendToTarget();
881
882 // Wait for the request to complete
883 synchronized (request) {
884 while (request.result == null) {
885 try {
886 request.wait();
887 } catch (InterruptedException e) {
888 // Do nothing, go back and wait until the request is complete
889 }
890 }
891 }
892 return request.result;
893 }
894
895 /**
896 * Asynchronous ("fire and forget") version of sendRequest():
897 * Posts the specified command to be executed on the main thread, and
898 * returns immediately.
899 * @see #sendRequest
900 */
901 private void sendRequestAsync(int command) {
902 mMainThreadHandler.sendEmptyMessage(command);
903 }
904
905 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700906 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
907 * @see {@link #sendRequest(int,Object)}
908 */
909 private void sendRequestAsync(int command, Object argument) {
910 MainThreadRequest request = new MainThreadRequest(argument);
911 Message msg = mMainThreadHandler.obtainMessage(command, request);
912 msg.sendToTarget();
913 }
914
915 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700916 * Initialize the singleton PhoneInterfaceManager instance.
917 * This is only done once, at startup, from PhoneApp.onCreate().
918 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700919 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 synchronized (PhoneInterfaceManager.class) {
921 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700922 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700923 } else {
924 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
925 }
926 return sInstance;
927 }
928 }
929
930 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700931 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700932 mApp = app;
933 mPhone = phone;
934 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700935 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700936 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
937 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700938 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700939 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800940 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800941
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 publish();
943 }
944
945 private void publish() {
946 if (DBG) log("publish: " + this);
947
948 ServiceManager.addService("phone", this);
949 }
950
Stuart Scott584921c2015-01-15 17:10:34 -0800951 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800952 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
953 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800954 }
955
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800956 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
957 Phone phone = getPhoneFromRequest(request);
958 return phone == null ? null :
959 UiccController.getInstance().getUiccCard(phone.getPhoneId());
960 }
961
Wink Saville36469e72014-06-11 15:17:00 -0700962 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700963 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800964 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700965 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700966 //
967 // Implementation of the ITelephony interface.
968 //
969
970 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700971 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700972 }
973
Wink Savilleb564aae2014-10-23 10:18:09 -0700974 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700975 if (DBG) log("dial: " + number);
976 // No permission check needed here: This is just a wrapper around the
977 // ACTION_DIAL intent, which is available to any app since it puts up
978 // the UI before it does anything.
979
980 String url = createTelUrl(number);
981 if (url == null) {
982 return;
983 }
984
985 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700986 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700987 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
988 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
989 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
990 mApp.startActivity(intent);
991 }
992 }
993
994 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700995 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700996 }
997
Wink Savilleb564aae2014-10-23 10:18:09 -0700998 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700999 if (DBG) log("call: " + number);
1000
1001 // This is just a wrapper around the ACTION_CALL intent, but we still
1002 // need to do a permission check since we're calling startActivity()
1003 // from the context of the phone app.
1004 enforceCallPermission();
1005
1006 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1007 != AppOpsManager.MODE_ALLOWED) {
1008 return;
1009 }
1010
1011 String url = createTelUrl(number);
1012 if (url == null) {
1013 return;
1014 }
1015
Wink Saville08874612014-08-31 19:19:58 -07001016 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001017 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001018 if (slist != null) {
1019 for (SubscriptionInfo subInfoRecord : slist) {
1020 if (subInfoRecord.getSubscriptionId() == subId) {
1021 isValid = true;
1022 break;
1023 }
Wink Saville08874612014-08-31 19:19:58 -07001024 }
1025 }
1026 if (isValid == false) {
1027 return;
1028 }
1029
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001030 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001031 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1033 mApp.startActivity(intent);
1034 }
1035
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001036 /**
1037 * End a call based on call state
1038 * @return true is a call was ended
1039 */
1040 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001041 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001042 }
1043
1044 /**
1045 * End a call based on the call state of the subId
1046 * @return true is a call was ended
1047 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001048 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001049 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001050 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001051 }
1052
1053 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001054 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001055 }
1056
Wink Savilleb564aae2014-10-23 10:18:09 -07001057 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001058 if (DBG) log("answerRingingCall...");
1059 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1060 // but that can probably wait till the big TelephonyManager API overhaul.
1061 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1062 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001063 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001064 }
1065
1066 /**
1067 * Make the actual telephony calls to implement answerRingingCall().
1068 * This should only be called from the main thread of the Phone app.
1069 * @see #answerRingingCall
1070 *
1071 * TODO: it would be nice to return true if we answered the call, or
1072 * false if there wasn't actually a ringing incoming call, or some
1073 * other error occurred. (In other words, pass back the return value
1074 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1075 * But that would require calling this method via sendRequest() rather
1076 * than sendRequestAsync(), and right now we don't actually *need* that
1077 * return value, so let's just return void for now.
1078 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001079 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001080 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001081 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001082 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1083 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001084 if (hasActiveCall && hasHoldingCall) {
1085 // Both lines are in use!
1086 // TODO: provide a flag to let the caller specify what
1087 // policy to use if both lines are in use. (The current
1088 // behavior is hardwired to "answer incoming, end ongoing",
1089 // which is how the CALL button is specced to behave.)
1090 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1091 return;
1092 } else {
1093 // answerCall() will automatically hold the current active
1094 // call, if there is one.
1095 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1096 return;
1097 }
1098 } else {
1099 // No call was ringing.
1100 return;
1101 }
1102 }
1103
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001104 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001105 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001106 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001107 public void silenceRinger() {
1108 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 }
1110
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001111 @Override
1112 public boolean isOffhook(String callingPackage) {
1113 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001114 }
1115
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001116 @Override
1117 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1118 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1119 return false;
1120 }
1121
Sanket Padawe356d7632015-06-22 14:03:32 -07001122 final Phone phone = getPhone(subId);
1123 if (phone != null) {
1124 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1125 } else {
1126 return false;
1127 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001128 }
1129
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001130 @Override
1131 public boolean isRinging(String callingPackage) {
1132 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001133 }
1134
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001135 @Override
1136 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1137 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1138 return false;
1139 }
1140
Sanket Padawe356d7632015-06-22 14:03:32 -07001141 final Phone phone = getPhone(subId);
1142 if (phone != null) {
1143 return (phone.getState() == PhoneConstants.State.RINGING);
1144 } else {
1145 return false;
1146 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001147 }
1148
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001149 @Override
1150 public boolean isIdle(String callingPackage) {
1151 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001152 }
1153
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001154 @Override
1155 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1156 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1157 return false;
1158 }
1159
Sanket Padawe356d7632015-06-22 14:03:32 -07001160 final Phone phone = getPhone(subId);
1161 if (phone != null) {
1162 return (phone.getState() == PhoneConstants.State.IDLE);
1163 } else {
1164 return false;
1165 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001166 }
1167
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001168 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001169 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001170 }
1171
Wink Savilleb564aae2014-10-23 10:18:09 -07001172 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001173 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001174 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1175 }
1176
1177 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001178 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001179 }
1180
Wink Savilleb564aae2014-10-23 10:18:09 -07001181 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001182 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001183 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1184 }
1185
1186 /** {@hide} */
1187 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001188 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001189 }
1190
Wink Savilleb564aae2014-10-23 10:18:09 -07001191 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001192 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001193 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 checkSimPin.start();
1195 return checkSimPin.unlockSim(null, pin);
1196 }
1197
Wink Saville9de0f752013-10-22 19:04:03 -07001198 /** {@hide} */
1199 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001200 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001201 }
1202
Wink Savilleb564aae2014-10-23 10:18:09 -07001203 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001204 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001205 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001206 checkSimPuk.start();
1207 return checkSimPuk.unlockSim(puk, pin);
1208 }
1209
1210 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001211 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001212 * a synchronous one.
1213 */
1214 private static class UnlockSim extends Thread {
1215
1216 private final IccCard mSimCard;
1217
1218 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001219 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1220 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001221
1222 // For replies from SimCard interface
1223 private Handler mHandler;
1224
1225 // For async handler to identify request type
1226 private static final int SUPPLY_PIN_COMPLETE = 100;
1227
1228 public UnlockSim(IccCard simCard) {
1229 mSimCard = simCard;
1230 }
1231
1232 @Override
1233 public void run() {
1234 Looper.prepare();
1235 synchronized (UnlockSim.this) {
1236 mHandler = new Handler() {
1237 @Override
1238 public void handleMessage(Message msg) {
1239 AsyncResult ar = (AsyncResult) msg.obj;
1240 switch (msg.what) {
1241 case SUPPLY_PIN_COMPLETE:
1242 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1243 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001244 mRetryCount = msg.arg1;
1245 if (ar.exception != null) {
1246 if (ar.exception instanceof CommandException &&
1247 ((CommandException)(ar.exception)).getCommandError()
1248 == CommandException.Error.PASSWORD_INCORRECT) {
1249 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1250 } else {
1251 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1252 }
1253 } else {
1254 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1255 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001256 mDone = true;
1257 UnlockSim.this.notifyAll();
1258 }
1259 break;
1260 }
1261 }
1262 };
1263 UnlockSim.this.notifyAll();
1264 }
1265 Looper.loop();
1266 }
1267
1268 /*
1269 * Use PIN or PUK to unlock SIM card
1270 *
1271 * If PUK is null, unlock SIM card with PIN
1272 *
1273 * If PUK is not null, unlock SIM card with PUK and set PIN code
1274 */
Wink Saville9de0f752013-10-22 19:04:03 -07001275 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001276
1277 while (mHandler == null) {
1278 try {
1279 wait();
1280 } catch (InterruptedException e) {
1281 Thread.currentThread().interrupt();
1282 }
1283 }
1284 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1285
1286 if (puk == null) {
1287 mSimCard.supplyPin(pin, callback);
1288 } else {
1289 mSimCard.supplyPuk(puk, pin, callback);
1290 }
1291
1292 while (!mDone) {
1293 try {
1294 Log.d(LOG_TAG, "wait for done");
1295 wait();
1296 } catch (InterruptedException e) {
1297 // Restore the interrupted status
1298 Thread.currentThread().interrupt();
1299 }
1300 }
1301 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001302 int[] resultArray = new int[2];
1303 resultArray[0] = mResult;
1304 resultArray[1] = mRetryCount;
1305 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001306 }
1307 }
1308
1309 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001310 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001311
1312 }
1313
Wink Savilleb564aae2014-10-23 10:18:09 -07001314 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001315 // No permission check needed here: this call is harmless, and it's
1316 // needed for the ServiceState.requestStateUpdate() call (which is
1317 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001318 final Phone phone = getPhone(subId);
1319 if (phone != null) {
1320 phone.updateServiceLocation();
1321 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001322 }
1323
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001324 @Override
1325 public boolean isRadioOn(String callingPackage) {
1326 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001327 }
1328
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001329 @Override
1330 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1331 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1332 return false;
1333 }
1334 return isRadioOnForSubscriber(subId);
1335 }
1336
1337 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001338 final Phone phone = getPhone(subId);
1339 if (phone != null) {
1340 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1341 } else {
1342 return false;
1343 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 }
1345
1346 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001347 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001348
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001349 }
Wink Saville36469e72014-06-11 15:17:00 -07001350
Wink Savilleb564aae2014-10-23 10:18:09 -07001351 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001352 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001353 final Phone phone = getPhone(subId);
1354 if (phone != null) {
1355 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1356 }
Wink Saville36469e72014-06-11 15:17:00 -07001357 }
1358
1359 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001360 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001361 }
1362
Wink Savilleb564aae2014-10-23 10:18:09 -07001363 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001364 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001365 final Phone phone = getPhone(subId);
1366 if (phone == null) {
1367 return false;
1368 }
1369 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001370 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001371 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001372 }
1373 return true;
1374 }
Wink Saville36469e72014-06-11 15:17:00 -07001375
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001376 public boolean needMobileRadioShutdown() {
1377 /*
1378 * If any of the Radios are available, it will need to be
1379 * shutdown. So return true if any Radio is available.
1380 */
1381 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1382 Phone phone = PhoneFactory.getPhone(i);
1383 if (phone != null && phone.isRadioAvailable()) return true;
1384 }
1385 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1386 return false;
1387 }
1388
1389 public void shutdownMobileRadios() {
1390 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1391 logv("Shutting down Phone " + i);
1392 shutdownRadioUsingPhoneId(i);
1393 }
1394 }
1395
1396 private void shutdownRadioUsingPhoneId(int phoneId) {
1397 enforceModifyPermission();
1398 Phone phone = PhoneFactory.getPhone(phoneId);
1399 if (phone != null && phone.isRadioAvailable()) {
1400 phone.shutdownRadio();
1401 }
1402 }
1403
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001405 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001406 }
1407
Wink Savilleb564aae2014-10-23 10:18:09 -07001408 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001409 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001410 final Phone phone = getPhone(subId);
1411 if (phone != null) {
1412 phone.setRadioPower(turnOn);
1413 return true;
1414 } else {
1415 return false;
1416 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001417 }
1418
Wink Saville36469e72014-06-11 15:17:00 -07001419 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001420 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001421 public boolean enableDataConnectivity() {
1422 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001423 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001424 final Phone phone = getPhone(subId);
1425 if (phone != null) {
1426 phone.setDataEnabled(true);
1427 return true;
1428 } else {
1429 return false;
1430 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001431 }
1432
Wink Saville36469e72014-06-11 15:17:00 -07001433 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001434 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 public boolean disableDataConnectivity() {
1436 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001437 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001438 final Phone phone = getPhone(subId);
1439 if (phone != null) {
1440 phone.setDataEnabled(false);
1441 return true;
1442 } else {
1443 return false;
1444 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001445 }
1446
Wink Saville36469e72014-06-11 15:17:00 -07001447 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001448 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001449 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001450 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001451 final Phone phone = getPhone(subId);
1452 if (phone != null) {
1453 return phone.isDataConnectivityPossible();
1454 } else {
1455 return false;
1456 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 }
1458
1459 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001460 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001461 }
1462
Wink Savilleb564aae2014-10-23 10:18:09 -07001463 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001464 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001465 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1466 return false;
1467 }
Wink Saville36469e72014-06-11 15:17:00 -07001468 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001469 }
1470
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001471 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001472 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001473 }
1474
Shishir Agrawala9f32182016-04-12 12:00:16 -07001475 public int getCallStateForSlot(int slotId) {
1476 Phone phone = PhoneFactory.getPhone(slotId);
1477 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1478 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001479 }
1480
Sanket Padawe356d7632015-06-22 14:03:32 -07001481 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001483 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001484 if (phone != null) {
1485 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1486 } else {
1487 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1488 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001489 }
1490
Sanket Padawe356d7632015-06-22 14:03:32 -07001491 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001492 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001493 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001494 if (phone != null) {
1495 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1496 } else {
1497 return TelephonyManager.DATA_ACTIVITY_NONE;
1498 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001499 }
1500
1501 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001502 public Bundle getCellLocation(String callingPackage) {
1503 enforceFineOrCoarseLocationPermission("getCellLocation");
1504
1505 // OP_COARSE_LOCATION controls both fine and coarse location.
1506 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1507 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001508 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001509 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001510 }
1511
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001512 if (checkIfCallerIsSelfOrForegroundUser() ||
1513 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001514 if (DBG_LOC) log("getCellLocation: is active user");
1515 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001516 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001517 if (phone == null) {
1518 return null;
1519 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001520 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001521 return data;
1522 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001523 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001524 return null;
1525 }
1526 }
1527
Svetoslav64fad262015-04-14 14:35:21 -07001528 private void enforceFineOrCoarseLocationPermission(String message) {
1529 try {
1530 mApp.enforceCallingOrSelfPermission(
1531 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1532 } catch (SecurityException e) {
1533 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1534 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1535 // is the weaker precondition
1536 mApp.enforceCallingOrSelfPermission(
1537 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1538 }
1539 }
1540
1541
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001542 @Override
1543 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001544 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001545 }
1546
Sanket Padawe356d7632015-06-22 14:03:32 -07001547 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001548 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001549 mApp.enforceCallingOrSelfPermission(
1550 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001551 final Phone phone = getPhone(subId);
1552 if (phone != null) {
1553 phone.enableLocationUpdates();
1554 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001555 }
1556
1557 @Override
1558 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001559 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001560 }
1561
Sanket Padawe356d7632015-06-22 14:03:32 -07001562 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001563 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001564 mApp.enforceCallingOrSelfPermission(
1565 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001566 final Phone phone = getPhone(subId);
1567 if (phone != null) {
1568 phone.disableLocationUpdates();
1569 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001570 }
1571
1572 @Override
1573 @SuppressWarnings("unchecked")
1574 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001575 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1576
1577 // OP_COARSE_LOCATION controls both fine and coarse location.
1578 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1579 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1580 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001581 }
1582
1583 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1584 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1585 return null;
1586 }
Svetoslav64fad262015-04-14 14:35:21 -07001587
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001588 if (checkIfCallerIsSelfOrForegroundUser() ||
1589 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1591
1592 ArrayList<NeighboringCellInfo> cells = null;
1593
1594 try {
1595 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001596 CMD_HANDLE_NEIGHBORING_CELL, null,
1597 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001598 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001599 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001600 }
1601 return cells;
1602 } else {
1603 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1604 return null;
1605 }
1606 }
1607
1608
1609 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001610 public List<CellInfo> getAllCellInfo(String callingPackage) {
1611 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1612
1613 // OP_COARSE_LOCATION controls both fine and coarse location.
1614 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1615 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1616 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001617 }
1618
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001619 if (checkIfCallerIsSelfOrForegroundUser() ||
1620 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001621 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001622 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1623 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001624 final List<CellInfo> info = phone.getAllCellInfo();
1625 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001626 }
1627 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001628 } else {
1629 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1630 return null;
1631 }
1632 }
1633
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001634 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001635 public void setCellInfoListRate(int rateInMillis) {
1636 mPhone.setCellInfoListRate(rateInMillis);
1637 }
1638
Shishir Agrawala9f32182016-04-12 12:00:16 -07001639 @Override
1640 public String getImeiForSlot(int slotId, String callingPackage) {
1641 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1642 return null;
1643 }
1644 Phone phone = PhoneFactory.getPhone(slotId);
1645 return phone == null ? null : phone.getImei();
1646 }
1647
1648 @Override
1649 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1650 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1651 return null;
1652 }
1653 Phone phone = PhoneFactory.getPhone(slotId);
1654 return phone == null ? null : phone.getDeviceSvn();
1655 }
1656
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 //
1658 // Internal helper methods.
1659 //
1660
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001661 /**
1662 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1663 */
1664 private boolean checkCallerInteractAcrossUsersFull() {
1665 return mPhone.getContext().checkCallingOrSelfPermission(
1666 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1667 == PackageManager.PERMISSION_GRANTED;
1668 }
1669
Jake Hambye994d462014-02-03 13:10:13 -08001670 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001671 boolean ok;
1672
1673 boolean self = Binder.getCallingUid() == Process.myUid();
1674 if (!self) {
1675 // Get the caller's user id then clear the calling identity
1676 // which will be restored in the finally clause.
1677 int callingUser = UserHandle.getCallingUserId();
1678 long ident = Binder.clearCallingIdentity();
1679
1680 try {
1681 // With calling identity cleared the current user is the foreground user.
1682 int foregroundUser = ActivityManager.getCurrentUser();
1683 ok = (foregroundUser == callingUser);
1684 if (DBG_LOC) {
1685 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1686 + " callingUser=" + callingUser + " ok=" + ok);
1687 }
1688 } catch (Exception ex) {
1689 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1690 ok = false;
1691 } finally {
1692 Binder.restoreCallingIdentity(ident);
1693 }
1694 } else {
1695 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1696 ok = true;
1697 }
1698 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1699 return ok;
1700 }
1701
1702 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001703 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1704 *
1705 * @throws SecurityException if the caller does not have the required permission
1706 */
1707 private void enforceModifyPermission() {
1708 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1709 }
1710
1711 /**
Junda Liua2e36012014-07-09 18:30:01 -07001712 * Make sure either system app or the caller has carrier privilege.
1713 *
1714 * @throws SecurityException if the caller does not have the required permission/privilege
1715 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001716 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001717 int permission = mApp.checkCallingOrSelfPermission(
1718 android.Manifest.permission.MODIFY_PHONE_STATE);
1719 if (permission == PackageManager.PERMISSION_GRANTED) {
1720 return;
1721 }
1722
1723 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001724 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001725 }
1726
1727 /**
1728 * Make sure the caller has carrier privilege.
1729 *
1730 * @throws SecurityException if the caller does not have the required permission
1731 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001732 private void enforceCarrierPrivilege(int subId) {
1733 if (getCarrierPrivilegeStatus(subId) !=
1734 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001735 loge("No Carrier Privilege.");
1736 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001737 }
1738 }
1739
1740 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001741 * Make sure the caller has the CALL_PHONE permission.
1742 *
1743 * @throws SecurityException if the caller does not have the required permission
1744 */
1745 private void enforceCallPermission() {
1746 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1747 }
1748
Stuart Scott8eef64f2015-04-08 15:13:54 -07001749 private void enforceConnectivityInternalPermission() {
1750 mApp.enforceCallingOrSelfPermission(
1751 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1752 "ConnectivityService");
1753 }
1754
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001755 private String createTelUrl(String number) {
1756 if (TextUtils.isEmpty(number)) {
1757 return null;
1758 }
1759
Jake Hambye994d462014-02-03 13:10:13 -08001760 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001761 }
1762
Ihab Awadf9e92732013-12-05 18:02:52 -08001763 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001764 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1765 }
1766
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001767 private static void logv(String msg) {
1768 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1769 }
1770
Ihab Awadf9e92732013-12-05 18:02:52 -08001771 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001772 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1773 }
1774
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001775 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001776 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001777 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001778 }
1779
Sanket Padawe356d7632015-06-22 14:03:32 -07001780 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001781 public int getActivePhoneTypeForSlot(int slotId) {
1782 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001783 if (phone == null) {
1784 return PhoneConstants.PHONE_TYPE_NONE;
1785 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001786 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001787 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001788 }
1789
1790 /**
1791 * Returns the CDMA ERI icon index to display
1792 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001793 @Override
1794 public int getCdmaEriIconIndex(String callingPackage) {
1795 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001796 }
1797
Sanket Padawe356d7632015-06-22 14:03:32 -07001798 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001799 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1800 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1801 return -1;
1802 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001803 final Phone phone = getPhone(subId);
1804 if (phone != null) {
1805 return phone.getCdmaEriIconIndex();
1806 } else {
1807 return -1;
1808 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001809 }
1810
1811 /**
1812 * Returns the CDMA ERI icon mode,
1813 * 0 - ON
1814 * 1 - FLASHING
1815 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001816 @Override
1817 public int getCdmaEriIconMode(String callingPackage) {
1818 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001819 }
1820
Sanket Padawe356d7632015-06-22 14:03:32 -07001821 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001822 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1823 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1824 return -1;
1825 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001826 final Phone phone = getPhone(subId);
1827 if (phone != null) {
1828 return phone.getCdmaEriIconMode();
1829 } else {
1830 return -1;
1831 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001832 }
1833
1834 /**
1835 * Returns the CDMA ERI text,
1836 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001837 @Override
1838 public String getCdmaEriText(String callingPackage) {
1839 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001840 }
1841
Sanket Padawe356d7632015-06-22 14:03:32 -07001842 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001843 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1844 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1845 return null;
1846 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001847 final Phone phone = getPhone(subId);
1848 if (phone != null) {
1849 return phone.getCdmaEriText();
1850 } else {
1851 return null;
1852 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001853 }
1854
1855 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001856 * Returns the CDMA MDN.
1857 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001858 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001859 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001860 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001861 final Phone phone = getPhone(subId);
1862 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1863 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001864 } else {
1865 return null;
1866 }
1867 }
1868
1869 /**
1870 * Returns the CDMA MIN.
1871 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001872 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001873 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001874 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001875 final Phone phone = getPhone(subId);
1876 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1877 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001878 } else {
1879 return null;
1880 }
1881 }
1882
1883 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001884 * Returns true if CDMA provisioning needs to run.
1885 */
1886 public boolean needsOtaServiceProvisioning() {
1887 return mPhone.needsOtaServiceProvisioning();
1888 }
1889
1890 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001891 * Sets the voice mail number of a given subId.
1892 */
1893 @Override
1894 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001895 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001896 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1897 new Pair<String, String>(alphaTag, number), new Integer(subId));
1898 return success;
1899 }
1900
Ta-wei Yen87c49842016-05-13 21:19:52 -07001901 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07001902 public void setVisualVoicemailEnabled(String callingPackage,
1903 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
1904 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
1905 if (!TextUtils.equals(callingPackage,
1906 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
1907 enforceModifyPermissionOrCarrierPrivilege(
1908 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
1909 }
1910 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
1911 }
1912
1913 @Override
1914 public boolean isVisualVoicemailEnabled(String callingPackage,
1915 PhoneAccountHandle phoneAccountHandle) {
1916 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
1917 return false;
1918 }
1919 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
1920 }
1921
1922 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001923 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1924 VisualVoicemailSmsFilterSettings settings) {
1925 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001926 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001927 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1928 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001929 }
1930
1931 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001932 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1933 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001934 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001935 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001936 }
1937
1938 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001939 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
1940 String callingPackage, int subId) {
1941 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001942 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001943 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001944 }
1945
1946 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001947 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
1948 String packageName, int subId) {
1949 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07001950 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001951 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001952 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001953 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001954 * Returns the unread count of voicemails
1955 */
1956 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001957 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001958 }
1959
1960 /**
1961 * Returns the unread count of voicemails for a subId
1962 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001963 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001964 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001965 final Phone phone = getPhone(subId);
1966 if (phone != null) {
1967 return phone.getVoiceMessageCount();
1968 } else {
1969 return 0;
1970 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001971 }
1972
1973 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001974 * Returns the data network type.
1975 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001976 *
1977 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1978 */
1979 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001980 public int getNetworkType() {
1981 final Phone phone = getPhone(getDefaultSubscription());
1982 if (phone != null) {
1983 return phone.getServiceState().getDataNetworkType();
1984 } else {
1985 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1986 }
Wink Saville36469e72014-06-11 15:17:00 -07001987 }
1988
1989 /**
1990 * Returns the network type for a subId
1991 */
1992 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001993 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1994 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1995 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1996 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001997
Sanket Padawe356d7632015-06-22 14:03:32 -07001998 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001999 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002000 return phone.getServiceState().getDataNetworkType();
2001 } else {
2002 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2003 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002004 }
2005
2006 /**
2007 * Returns the data network type
2008 */
2009 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002010 public int getDataNetworkType(String callingPackage) {
2011 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002012 }
2013
2014 /**
2015 * Returns the data network type for a subId
2016 */
2017 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002018 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2019 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2020 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2021 }
2022
Sanket Padawe356d7632015-06-22 14:03:32 -07002023 final Phone phone = getPhone(subId);
2024 if (phone != null) {
2025 return phone.getServiceState().getDataNetworkType();
2026 } else {
2027 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2028 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002029 }
2030
2031 /**
Wink Saville36469e72014-06-11 15:17:00 -07002032 * Returns the Voice network type for a subId
2033 */
2034 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002035 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2036 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2037 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2038 }
2039
Sanket Padawe356d7632015-06-22 14:03:32 -07002040 final Phone phone = getPhone(subId);
2041 if (phone != null) {
2042 return phone.getServiceState().getVoiceNetworkType();
2043 } else {
2044 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2045 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002046 }
2047
2048 /**
2049 * @return true if a ICC card is present
2050 */
2051 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002052 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002053 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002054 }
2055
2056 /**
2057 * @return true if a ICC card is present for a slotId
2058 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002059 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002060 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002061 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002062 final Phone phone = getPhone(subId[0]);
2063 if (subId != null && phone != null) {
2064 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002065 } else {
2066 return false;
2067 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002068 }
2069
2070 /**
2071 * Return if the current radio is LTE on CDMA. This
2072 * is a tri-state return value as for a period of time
2073 * the mode may be unknown.
2074 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002075 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002076 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002077 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002078 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002079 @Override
2080 public int getLteOnCdmaMode(String callingPackage) {
2081 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002082 }
2083
Sanket Padawe356d7632015-06-22 14:03:32 -07002084 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002085 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2086 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2087 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2088 }
2089
Sanket Padawe356d7632015-06-22 14:03:32 -07002090 final Phone phone = getPhone(subId);
2091 if (phone == null) {
2092 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2093 } else {
2094 return phone.getLteOnCdmaMode();
2095 }
Wink Saville36469e72014-06-11 15:17:00 -07002096 }
2097
2098 public void setPhone(Phone phone) {
2099 mPhone = phone;
2100 }
2101
2102 /**
2103 * {@hide}
2104 * Returns Default subId, 0 in the case of single standby.
2105 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002106 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002107 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002108 }
2109
Shishir Agrawala9f32182016-04-12 12:00:16 -07002110 private int getSlotForDefaultSubscription() {
2111 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2112 }
2113
Wink Savilleb564aae2014-10-23 10:18:09 -07002114 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002115 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002116 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002117
2118 /**
2119 * @see android.telephony.TelephonyManager.WifiCallingChoices
2120 */
2121 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002122 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2123 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002124 }
2125
2126 /**
2127 * @see android.telephony.TelephonyManager.WifiCallingChoices
2128 */
2129 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002130 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2131 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2132 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002133 }
2134
Sailesh Nepald1e68152013-12-12 19:08:02 -08002135 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002136 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002137 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002138 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002139
Shishir Agrawal566b7612013-10-28 14:41:00 -07002140 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002141 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2142 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002143
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002144 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002145 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002146 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002147 if (DBG) log("iccOpenLogicalChannel: " + response);
2148 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002149 }
2150
2151 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002152 public boolean iccCloseLogicalChannel(int subId, int channel) {
2153 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002154
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002155 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002156 if (channel < 0) {
2157 return false;
2158 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002159 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002160 if (DBG) log("iccCloseLogicalChannel: " + success);
2161 return success;
2162 }
2163
2164 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002165 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002166 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002167 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002168
2169 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002170 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2171 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002172 " data=" + data);
2173 }
2174
2175 if (channel < 0) {
2176 return "";
2177 }
2178
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002179 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002180 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002181 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2182
Shishir Agrawal566b7612013-10-28 14:41:00 -07002183 // Append the returned status code to the end of the response payload.
2184 String s = Integer.toHexString(
2185 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002186 if (response.payload != null) {
2187 s = IccUtils.bytesToHexString(response.payload) + s;
2188 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002189 return s;
2190 }
Jake Hambye994d462014-02-03 13:10:13 -08002191
Evan Charltonc66da362014-05-16 14:06:40 -07002192 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002193 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002194 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002195 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002196
2197 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002198 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2199 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002200 }
2201
2202 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002203 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002204 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2205
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002206 // Append the returned status code to the end of the response payload.
2207 String s = Integer.toHexString(
2208 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002209 if (response.payload != null) {
2210 s = IccUtils.bytesToHexString(response.payload) + s;
2211 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002212 return s;
2213 }
2214
2215 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002216 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002217 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002218 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002219
2220 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002221 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002222 p1 + " " + p2 + " " + p3 + ":" + filePath);
2223 }
2224
2225 IccIoResult response =
2226 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002227 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2228 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002229
2230 if (DBG) {
2231 log("Exchange SIM_IO [R]" + response);
2232 }
2233
2234 byte[] result = null;
2235 int length = 2;
2236 if (response.payload != null) {
2237 length = 2 + response.payload.length;
2238 result = new byte[length];
2239 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2240 } else {
2241 result = new byte[length];
2242 }
2243
2244 result[length - 1] = (byte) response.sw2;
2245 result[length - 2] = (byte) response.sw1;
2246 return result;
2247 }
2248
2249 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002250 public String sendEnvelopeWithStatus(int subId, String content) {
2251 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002252
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002253 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002254 if (response.payload == null) {
2255 return "";
2256 }
2257
2258 // Append the returned status code to the end of the response payload.
2259 String s = Integer.toHexString(
2260 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2261 s = IccUtils.bytesToHexString(response.payload) + s;
2262 return s;
2263 }
2264
Jake Hambye994d462014-02-03 13:10:13 -08002265 /**
2266 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2267 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2268 *
2269 * @param itemID the ID of the item to read
2270 * @return the NV item as a String, or null on error.
2271 */
2272 @Override
2273 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002274 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002275 if (DBG) log("nvReadItem: item " + itemID);
2276 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2277 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2278 return value;
2279 }
2280
2281 /**
2282 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2283 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2284 *
2285 * @param itemID the ID of the item to read
2286 * @param itemValue the value to write, as a String
2287 * @return true on success; false on any failure
2288 */
2289 @Override
2290 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002291 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002292 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2293 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2294 new Pair<Integer, String>(itemID, itemValue));
2295 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2296 return success;
2297 }
2298
2299 /**
2300 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2301 * Used for device configuration by some CDMA operators.
2302 *
2303 * @param preferredRoamingList byte array containing the new PRL
2304 * @return true on success; false on any failure
2305 */
2306 @Override
2307 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002308 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002309 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2310 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2311 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2312 return success;
2313 }
2314
2315 /**
2316 * Perform the specified type of NV config reset.
2317 * Used for device configuration by some CDMA operators.
2318 *
2319 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2320 * @return true on success; false on any failure
2321 */
2322 @Override
2323 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002324 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002325 if (DBG) log("nvResetConfig: type " + resetType);
2326 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2327 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2328 return success;
2329 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002330
2331 /**
Wink Saville36469e72014-06-11 15:17:00 -07002332 * {@hide}
2333 * Returns Default sim, 0 in the case of single standby.
2334 */
2335 public int getDefaultSim() {
2336 //TODO Need to get it from Telephony Devcontroller
2337 return 0;
2338 }
2339
Svet Ganovb320e182015-04-16 12:30:10 -07002340 public String[] getPcscfAddress(String apnType, String callingPackage) {
2341 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2342 return new String[0];
2343 }
2344
2345
ram87fca6f2014-07-18 18:58:44 +05302346 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002347 }
2348
2349 public void setImsRegistrationState(boolean registered) {
2350 enforceModifyPermission();
2351 mPhone.setImsRegistrationState(registered);
2352 }
2353
2354 /**
Stuart Scott54788802015-03-30 13:18:01 -07002355 * Set the network selection mode to automatic.
2356 *
2357 */
2358 @Override
2359 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002360 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002361 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2362 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2363 }
2364
2365 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002366 * Set the network selection mode to manual with the selected carrier.
2367 */
2368 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002369 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2370 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002371 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002372 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002373 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2374 persistSelection);
2375 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002376 }
2377
2378 /**
2379 * Scans for available networks.
2380 */
2381 @Override
2382 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002383 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002384 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2385 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2386 CMD_PERFORM_NETWORK_SCAN, null, subId);
2387 return result;
2388 }
2389
2390 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002391 * Get the calculated preferred network type.
2392 * Used for debugging incorrect network type.
2393 *
2394 * @return the preferred network type, defined in RILConstants.java.
2395 */
2396 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002397 public int getCalculatedPreferredNetworkType(String callingPackage) {
2398 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2399 return RILConstants.PREFERRED_NETWORK_MODE;
2400 }
2401
Amit Mahajan43330e02014-11-18 11:54:45 -08002402 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002403 }
2404
2405 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002406 * Get the preferred network type.
2407 * Used for device configuration by some CDMA operators.
2408 *
2409 * @return the preferred network type, defined in RILConstants.java.
2410 */
2411 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002412 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002413 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002414 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002415 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002416 int networkType = (result != null ? result[0] : -1);
2417 if (DBG) log("getPreferredNetworkType: " + networkType);
2418 return networkType;
2419 }
2420
2421 /**
2422 * Set the preferred network type.
2423 * Used for device configuration by some CDMA operators.
2424 *
2425 * @param networkType the preferred network type, defined in RILConstants.java.
2426 * @return true on success; false on any failure.
2427 */
2428 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002429 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002430 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002431 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2432 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002433 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002434 if (success) {
2435 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002436 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002437 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002438 return success;
2439 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002440
2441 /**
Junda Liu475951f2014-11-07 16:45:03 -08002442 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2443 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2444 * tethering.
2445 *
2446 * @return 0: Not required. 1: required. 2: Not set.
2447 * @hide
2448 */
2449 @Override
2450 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002451 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002452 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2453 Settings.Global.TETHER_DUN_REQUIRED, 2);
2454 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2455 // config_tether_apndata.
2456 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2457 dunRequired = 1;
2458 }
2459 return dunRequired;
2460 }
2461
2462 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002463 * Set mobile data enabled
2464 * Used by the user through settings etc to turn on/off mobile data
2465 *
2466 * @param enable {@code true} turn turn data on, else {@code false}
2467 */
2468 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002469 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002470 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002471 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002472 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002473 Phone phone = PhoneFactory.getPhone(phoneId);
2474 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002475 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002476 phone.setDataEnabled(enable);
2477 } else {
2478 loge("setDataEnabled: no phone for subId=" + subId);
2479 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002480 }
2481
2482 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002483 * Get whether mobile data is enabled.
2484 *
2485 * Note that this used to be available from ConnectivityService, gated by
2486 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2487 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002488 *
2489 * @return {@code true} if data is enabled else {@code false}
2490 */
2491 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002492 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002493 try {
2494 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2495 null);
2496 } catch (Exception e) {
2497 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2498 null);
2499 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002500 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002501 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002502 Phone phone = PhoneFactory.getPhone(phoneId);
2503 if (phone != null) {
2504 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002505 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002506 return retVal;
2507 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002508 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002509 return false;
2510 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002511 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002512
2513 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002514 public int getCarrierPrivilegeStatus(int subId) {
2515 final Phone phone = getPhone(subId);
2516 if (phone == null) {
2517 loge("getCarrierPrivilegeStatus: Invalid subId");
2518 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2519 }
2520 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002521 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002522 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002523 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2524 }
2525 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002526 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002527 }
Junda Liu29340342014-07-10 15:23:27 -07002528
2529 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002530 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002531 if (TextUtils.isEmpty(pkgName))
2532 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002533 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002534 if (card == null) {
2535 loge("checkCarrierPrivilegesForPackage: No UICC");
2536 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2537 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002538 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2539 }
2540
2541 @Override
2542 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002543 if (TextUtils.isEmpty(pkgName))
2544 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002545 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2546 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2547 UiccCard card = UiccController.getInstance().getUiccCard(i);
2548 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002549 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002550 continue;
2551 }
2552
2553 result = card.getCarrierPrivilegeStatus(
2554 mPhone.getContext().getPackageManager(), pkgName);
2555 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2556 break;
2557 }
2558 }
2559
2560 return result;
Junda Liu29340342014-07-10 15:23:27 -07002561 }
Derek Tan89e89d42014-07-08 17:00:10 -07002562
2563 @Override
Junda Liue64de782015-04-16 17:19:16 -07002564 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2565 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2566 loge("phoneId " + phoneId + " is not valid.");
2567 return null;
2568 }
2569 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002570 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002571 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002572 return null ;
2573 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002574 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002575 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002576 }
2577
Amith Yamasani6e118872016-02-19 12:53:51 -08002578 @Override
2579 public List<String> getPackagesWithCarrierPrivileges() {
2580 PackageManager pm = mPhone.getContext().getPackageManager();
2581 List<String> privilegedPackages = new ArrayList<>();
2582 List<PackageInfo> packages = null;
2583 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2584 UiccCard card = UiccController.getInstance().getUiccCard(i);
2585 if (card == null) {
2586 // No UICC in that slot.
2587 continue;
2588 }
2589 if (card.hasCarrierPrivilegeRules()) {
2590 if (packages == null) {
2591 // Only check packages in user 0 for now
2592 packages = pm.getInstalledPackagesAsUser(
2593 PackageManager.MATCH_DISABLED_COMPONENTS
2594 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2595 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2596 }
2597 for (int p = packages.size() - 1; p >= 0; p--) {
2598 PackageInfo pkgInfo = packages.get(p);
2599 if (pkgInfo != null && pkgInfo.packageName != null
2600 && card.getCarrierPrivilegeStatus(pkgInfo)
2601 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2602 privilegedPackages.add(pkgInfo.packageName);
2603 }
2604 }
2605 }
2606 }
2607 return privilegedPackages;
2608 }
2609
Wink Savilleb564aae2014-10-23 10:18:09 -07002610 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002611 final Phone phone = getPhone(subId);
2612 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002613 if (card == null) {
2614 loge("getIccId: No UICC");
2615 return null;
2616 }
2617 String iccId = card.getIccId();
2618 if (TextUtils.isEmpty(iccId)) {
2619 loge("getIccId: ICC ID is null or empty.");
2620 return null;
2621 }
2622 return iccId;
2623 }
2624
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002625 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002626 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2627 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002628 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002629
Jeff Sharkey85190e62014-12-05 09:40:12 -08002630 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002631 final Phone phone = getPhone(subId);
2632 if (phone == null) {
2633 return false;
2634 }
2635 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002636
2637 if (DBG_MERGE) {
2638 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2639 + subscriberId + " to " + number);
2640 }
2641
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002642 if (TextUtils.isEmpty(iccId)) {
2643 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002644 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002645
Jeff Sharkey85190e62014-12-05 09:40:12 -08002646 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2647
2648 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002649 if (alphaTag == null) {
2650 editor.remove(alphaTagPrefKey);
2651 } else {
2652 editor.putString(alphaTagPrefKey, alphaTag);
2653 }
2654
Jeff Sharkey85190e62014-12-05 09:40:12 -08002655 // Record both the line number and IMSI for this ICCID, since we need to
2656 // track all merged IMSIs based on line number
2657 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2658 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002659 if (number == null) {
2660 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002661 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002662 } else {
2663 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002664 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002665 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002666
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002667 editor.commit();
2668 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002669 }
2670
2671 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002672 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002673 // This is open to apps with WRITE_SMS.
2674 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002675 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002676 return null;
2677 }
Derek Tan97ebb422014-09-05 16:55:38 -07002678
2679 String iccId = getIccId(subId);
2680 if (iccId != null) {
2681 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002682 if (DBG_MERGE) {
2683 log("getLine1NumberForDisplay returning " +
2684 mTelephonySharedPreferences.getString(numberPrefKey, null));
2685 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002686 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002687 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002688 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002689 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002690 }
2691
2692 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002693 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2694 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2695 return null;
2696 }
Derek Tan97ebb422014-09-05 16:55:38 -07002697
2698 String iccId = getIccId(subId);
2699 if (iccId != null) {
2700 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002701 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002702 }
Derek Tan97ebb422014-09-05 16:55:38 -07002703 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002704 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002705
2706 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002707 public String[] getMergedSubscriberIds(String callingPackage) {
2708 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2709 return null;
2710 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002711 final Context context = mPhone.getContext();
2712 final TelephonyManager tele = TelephonyManager.from(context);
2713 final SubscriptionManager sub = SubscriptionManager.from(context);
2714
2715 // Figure out what subscribers are currently active
2716 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002717 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2718 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2719 final long identity = Binder.clearCallingIdentity();
2720 try {
2721 final int[] subIds = sub.getActiveSubscriptionIdList();
2722 for (int subId : subIds) {
2723 activeSubscriberIds.add(tele.getSubscriberId(subId));
2724 }
2725 } finally {
2726 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002727 }
2728
2729 // First pass, find a number override for an active subscriber
2730 String mergeNumber = null;
2731 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2732 for (String key : prefs.keySet()) {
2733 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2734 final String subscriberId = (String) prefs.get(key);
2735 if (activeSubscriberIds.contains(subscriberId)) {
2736 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2737 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2738 mergeNumber = (String) prefs.get(numberKey);
2739 if (DBG_MERGE) {
2740 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2741 + " for active subscriber " + subscriberId);
2742 }
2743 if (!TextUtils.isEmpty(mergeNumber)) {
2744 break;
2745 }
2746 }
2747 }
2748 }
2749
2750 // Shortcut when no active merged subscribers
2751 if (TextUtils.isEmpty(mergeNumber)) {
2752 return null;
2753 }
2754
2755 // Second pass, find all subscribers under that line override
2756 final ArraySet<String> result = new ArraySet<>();
2757 for (String key : prefs.keySet()) {
2758 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2759 final String number = (String) prefs.get(key);
2760 if (mergeNumber.equals(number)) {
2761 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2762 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2763 final String subscriberId = (String) prefs.get(subscriberKey);
2764 if (!TextUtils.isEmpty(subscriberId)) {
2765 result.add(subscriberId);
2766 }
2767 }
2768 }
2769 }
2770
2771 final String[] resultArray = result.toArray(new String[result.size()]);
2772 Arrays.sort(resultArray);
2773 if (DBG_MERGE) {
2774 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2775 }
2776 return resultArray;
2777 }
2778
2779 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002780 public boolean setOperatorBrandOverride(int subId, String brand) {
2781 enforceCarrierPrivilege(subId);
2782 final Phone phone = getPhone(subId);
2783 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002784 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002785
2786 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002787 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002788 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2789 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002790 enforceCarrierPrivilege(subId);
2791 final Phone phone = getPhone(subId);
2792 if (phone == null) {
2793 return false;
2794 }
2795 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002796 cdmaNonRoamingList);
2797 }
2798
2799 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002800 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2801 enforceModifyPermission();
2802
2803 int returnValue = 0;
2804 try {
2805 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2806 if(result.exception == null) {
2807 if (result.result != null) {
2808 byte[] responseData = (byte[])(result.result);
2809 if(responseData.length > oemResp.length) {
2810 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2811 responseData.length + "bytes. Buffer Size is " +
2812 oemResp.length + "bytes.");
2813 }
2814 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2815 returnValue = responseData.length;
2816 }
2817 } else {
2818 CommandException ex = (CommandException) result.exception;
2819 returnValue = ex.getCommandError().ordinal();
2820 if(returnValue > 0) returnValue *= -1;
2821 }
2822 } catch (RuntimeException e) {
2823 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2824 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2825 if(returnValue > 0) returnValue *= -1;
2826 }
2827
2828 return returnValue;
2829 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002830
2831 @Override
2832 public void setRadioCapability(RadioAccessFamily[] rafs) {
2833 try {
2834 ProxyController.getInstance().setRadioCapability(rafs);
2835 } catch (RuntimeException e) {
2836 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2837 }
2838 }
2839
2840 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002841 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2842 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2843 return RadioAccessFamily.RAF_UNKNOWN;
2844 }
2845
Wink Saville5d475dd2014-10-17 15:00:58 -07002846 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2847 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002848
2849 @Override
2850 public void enableVideoCalling(boolean enable) {
2851 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002852 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002853 }
2854
2855 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002856 public boolean isVideoCallingEnabled(String callingPackage) {
2857 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2858 return false;
2859 }
2860
Andrew Lee77527ac2014-10-21 16:57:39 -07002861 // Check the user preference and the system-level IMS setting. Even if the user has
2862 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2863 // In the long run, we may instead need to check if there exists a connection service
2864 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002865 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2866 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002867 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002868 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002869
Andrew Leea1239f22015-03-02 17:44:07 -08002870 @Override
2871 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002872 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002873 }
2874
2875 @Override
2876 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002877 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002878 }
2879
Andrew Lee9431b832015-03-09 18:46:45 -07002880 @Override
2881 public boolean isTtyModeSupported() {
2882 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2883 TelephonyManager telephonyManager =
2884 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08002885 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07002886 }
2887
2888 @Override
2889 public boolean isHearingAidCompatibilitySupported() {
2890 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2891 }
2892
Sanket Padawe7310cc72015-01-14 09:53:20 -08002893 /**
2894 * Returns the unique device ID of phone, for example, the IMEI for
2895 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2896 *
2897 * <p>Requires Permission:
2898 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2899 */
2900 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002901 public String getDeviceId(String callingPackage) {
2902 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2903 return null;
2904 }
2905
Sanket Padawe7310cc72015-01-14 09:53:20 -08002906 final Phone phone = PhoneFactory.getPhone(0);
2907 if (phone != null) {
2908 return phone.getDeviceId();
2909 } else {
2910 return null;
2911 }
2912 }
2913
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002914 /*
2915 * {@hide}
2916 * Returns the IMS Registration Status
2917 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002918 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002919 public boolean isImsRegistered() {
2920 return mPhone.isImsRegistered();
2921 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002922
2923 @Override
2924 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2925 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2926 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002927
Nathan Haroldc55097a2015-03-11 18:14:50 -07002928 /*
2929 * {@hide}
2930 * Returns the IMS Registration Status
2931 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002932 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002933 return mPhone.isWifiCallingEnabled();
2934 }
2935
2936 /*
2937 * {@hide}
2938 * Returns the IMS Registration Status
2939 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002940 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002941 return mPhone.isVolteEnabled();
2942 }
Svet Ganovb320e182015-04-16 12:30:10 -07002943
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002944 /*
2945 * {@hide} Returns the IMS Registration Status
2946 */
2947 public boolean isVideoTelephonyAvailable() {
2948 return mPhone.isVideoEnabled();
2949 }
2950
Svet Ganovb320e182015-04-16 12:30:10 -07002951 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002952 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002953 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002954 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2955
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002956 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002957 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002958 } catch (SecurityException e) {
2959 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2960 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002961 }
Svet Ganovb320e182015-04-16 12:30:10 -07002962
2963 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2964 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2965 return false;
2966 }
2967
2968 return true;
2969 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002970
Makoto Onukifee69342015-06-29 14:44:50 -07002971 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002972 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002973 */
2974 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002975 // Default SMS app can always read it.
2976 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2977 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2978 return true;
2979 }
2980 try {
2981 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002982 } catch (SecurityException readPhoneStateSecurityException) {
2983 try {
2984 // Can be read with READ_SMS too.
2985 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2986 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2987 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2988 } catch (SecurityException readSmsSecurityException) {
2989 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2990 // permissions
2991 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2992 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2993 " or " + android.Manifest.permission.READ_SMS + ".");
2994 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002995 }
Makoto Onukifee69342015-06-29 14:44:50 -07002996 }
2997
Stuart Scott8eef64f2015-04-08 15:13:54 -07002998 @Override
2999 public void factoryReset(int subId) {
3000 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003001 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3002 return;
3003 }
3004
Svet Ganovcc087f82015-05-12 20:35:54 -07003005 final long identity = Binder.clearCallingIdentity();
3006 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003007 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3008 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003009 // Enable data
3010 setDataEnabled(subId, true);
3011 // Set network selection mode to automatic
3012 setNetworkSelectionModeAutomatic(subId);
3013 // Set preferred mobile network type to the best available
3014 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3015 // Turn off roaming
3016 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3017 }
3018 } finally {
3019 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003020 }
3021 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003022
3023 @Override
3024 public String getLocaleFromDefaultSim() {
3025 // We query all subscriptions instead of just the active ones, because
3026 // this might be called early on in the provisioning flow when the
3027 // subscriptions potentially aren't active yet.
3028 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3029 if (slist == null || slist.isEmpty()) {
3030 return null;
3031 }
3032
3033 // This function may be called very early, say, from the setup wizard, at
3034 // which point we won't have a default subscription set. If that's the case
3035 // we just choose the first, which will be valid in "most cases".
3036 final int defaultSubId = getDefaultSubscription();
3037 SubscriptionInfo info = null;
3038 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3039 info = slist.get(0);
3040 } else {
3041 for (SubscriptionInfo item : slist) {
3042 if (item.getSubscriptionId() == defaultSubId) {
3043 info = item;
3044 break;
3045 }
3046 }
3047
3048 if (info == null) {
3049 return null;
3050 }
3051 }
3052
3053 // Try and fetch the locale from the carrier properties or from the SIM language
3054 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003055 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003056 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003057 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003058 if (defaultPhone != null) {
3059 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3060 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003061 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003062 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3063 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003064 } else {
3065 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003066 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003067 }
3068 }
3069
Narayan Kamath011676f2015-07-29 12:04:08 +01003070 // The SIM language preferences only store a language (e.g. fr = French), not an
3071 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3072 // the SIM and carrier preferences does not include a country we add the country
3073 // determined from the SIM MCC to provide an exact locale.
3074 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003075 if (mccLocale != null) {
3076 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3077 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003078 }
3079
Tony Hill183b2de2015-06-24 14:53:58 +01003080 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003081 return null;
3082 }
3083
3084 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3085 final long identity = Binder.clearCallingIdentity();
3086 try {
3087 return mSubscriptionController.getAllSubInfoList(
3088 mPhone.getContext().getOpPackageName());
3089 } finally {
3090 Binder.restoreCallingIdentity(identity);
3091 }
3092 }
3093
3094 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3095 final long identity = Binder.clearCallingIdentity();
3096 try {
3097 return mSubscriptionController.getActiveSubscriptionInfoList(
3098 mPhone.getContext().getOpPackageName());
3099 } finally {
3100 Binder.restoreCallingIdentity(identity);
3101 }
3102 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003103
3104 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003105 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3106 * representing the state of the modem.
3107 *
3108 * NOTE: This clears the modem state, so there should only every be one caller.
3109 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003110 */
3111 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003112 public void requestModemActivityInfo(ResultReceiver result) {
3113 enforceModifyPermission();
3114
3115 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3116 Bundle bundle = new Bundle();
3117 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3118 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003119 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003120
3121 /**
3122 * {@hide}
3123 * Returns the service state information on specified subscription.
3124 */
3125 @Override
3126 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3127
3128 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3129 return null;
3130 }
3131
3132 final Phone phone = getPhone(subId);
3133 if (phone == null) {
3134 return null;
3135 }
3136
3137 return phone.getServiceState();
3138 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003139
3140 /**
3141 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3142 *
3143 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3144 * voicemail ringtone.
3145 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3146 * PhoneAccount.
3147 */
3148 @Override
3149 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3150 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3151 if (phone == null) {
3152 return null;
3153 }
3154
3155 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3156 }
3157
3158 /**
3159 * Returns whether vibration is set for voicemail notification in Phone settings.
3160 *
3161 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3162 * voicemail vibration setting.
3163 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3164 */
3165 @Override
3166 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3167 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3168 if (phone == null) {
3169 return false;
3170 }
3171
3172 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3173 }
3174
Youhan Wange64578a2016-05-02 15:32:42 -07003175 /**
3176 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3177 *
3178 * @throws SecurityException if the caller does not have the required permission
3179 */
3180 private void enforceReadPrivilegedPermission() {
3181 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3182 null);
3183 }
3184
3185 /**
3186 * Return the application ID for the app type.
3187 *
3188 * @param subId the subscription ID that this request applies to.
3189 * @param appType the uicc app type.
3190 * @return Application ID for specificied app type, or null if no uicc.
3191 */
3192 @Override
3193 public String getAidForAppType(int subId, int appType) {
3194 enforceReadPrivilegedPermission();
3195 Phone phone = getPhone(subId);
3196 if (phone == null) {
3197 return null;
3198 }
3199 String aid = null;
3200 try {
3201 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3202 .getApplicationByType(appType).getAid();
3203 } catch (Exception e) {
3204 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3205 }
3206 return aid;
3207 }
3208
Youhan Wang4001d252016-05-11 10:29:41 -07003209 /**
3210 * Return the Electronic Serial Number.
3211 *
3212 * @param subId the subscription ID that this request applies to.
3213 * @return ESN or null if error.
3214 */
3215 @Override
3216 public String getEsn(int subId) {
3217 enforceReadPrivilegedPermission();
3218 Phone phone = getPhone(subId);
3219 if (phone == null) {
3220 return null;
3221 }
3222 String esn = null;
3223 try {
3224 esn = phone.getEsn();
3225 } catch (Exception e) {
3226 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3227 }
3228 return esn;
3229 }
3230
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003231 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003232 * Return the Preferred Roaming List Version.
3233 *
3234 * @param subId the subscription ID that this request applies to.
3235 * @return PRLVersion or null if error.
3236 */
3237 @Override
3238 public String getCdmaPrlVersion(int subId) {
3239 enforceReadPrivilegedPermission();
3240 Phone phone = getPhone(subId);
3241 if (phone == null) {
3242 return null;
3243 }
3244 String cdmaPrlVersion = null;
3245 try {
3246 cdmaPrlVersion = phone.getCdmaPrlVersion();
3247 } catch (Exception e) {
3248 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3249 }
3250 return cdmaPrlVersion;
3251 }
3252
3253 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003254 * Get snapshot of Telephony histograms
3255 * @return List of Telephony histograms
3256 * @hide
3257 */
3258 @Override
3259 public List<TelephonyHistogram> getTelephonyHistograms() {
3260 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3261 return RIL.getTelephonyRILTimingHistograms();
3262 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003263
3264 /**
3265 * {@hide}
3266 * Set the allowed carrier list for slotId
3267 * Require system privileges. In the future we may add this to carrier APIs.
3268 *
3269 * @return The number of carriers set successfully, should match length of carriers
3270 */
3271 @Override
3272 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3273 enforceModifyPermission();
3274 int subId = SubscriptionManager.getSubId(slotId)[0];
3275 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3276 return retVal[0];
3277 }
3278
3279 /**
3280 * {@hide}
3281 * Get the allowed carrier list for slotId.
3282 * Require system privileges. In the future we may add this to carrier APIs.
3283 *
3284 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3285 * means all carriers are allowed.
3286 */
3287 @Override
3288 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3289 enforceReadPrivilegedPermission();
3290 int subId = SubscriptionManager.getSubId(slotId)[0];
3291 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3292 }
3293
fionaxu59545b42016-05-25 15:53:37 -07003294 /**
3295 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3296 * @param subId the subscription ID that this action applies to.
3297 * @param enabled control enable or disable metered apns.
3298 * {@hide}
3299 */
3300 @Override
3301 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3302 enforceModifyPermission();
3303 final Phone phone = getPhone(subId);
3304 if (phone == null) {
3305 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3306 return;
3307 }
3308 try {
3309 phone.carrierActionSetMeteredApnsEnabled(enabled);
3310 } catch (Exception e) {
3311 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3312 }
3313 }
3314
3315 /**
3316 * Action set from carrier signalling broadcast receivers to enable/disable radio
3317 * @param subId the subscription ID that this action applies to.
3318 * @param enabled control enable or disable radio.
3319 * {@hide}
3320 */
3321 @Override
3322 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3323 enforceModifyPermission();
3324 final Phone phone = getPhone(subId);
3325 if (phone == null) {
3326 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3327 return;
3328 }
3329 try {
3330 phone.carrierActionSetRadioEnabled(enabled);
3331 } catch (Exception e) {
3332 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3333 }
3334 }
3335
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003336 /**
3337 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3338 * bug report is being generated.
3339 */
3340 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003341 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003342 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3343 != PackageManager.PERMISSION_GRANTED) {
3344 writer.println("Permission Denial: can't dump Phone from pid="
3345 + Binder.getCallingPid()
3346 + ", uid=" + Binder.getCallingUid()
3347 + "without permission "
3348 + android.Manifest.permission.DUMP);
3349 return;
3350 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003351 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003352 }
Jack Yueb89b242016-06-22 13:27:47 -07003353
3354 /**
3355 * Get aggregated video call data usage from all subscriptions since boot.
3356 * @return total data usage in bytes
3357 * {@hide}
3358 */
3359 @Override
3360 public long getVtDataUsage() {
3361 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3362 null);
3363
3364 // NetworkStatsService keeps tracking the active network interface and identity. It will
3365 // record the delta with the corresponding network identity. What we need to do here is
3366 // returning total video call data usage from all subscriptions since boot.
3367
3368 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3369 // simultaneous VT calls.
3370 final Phone[] phones = PhoneFactory.getPhones();
3371 long total = 0;
3372 for (Phone phone : phones) {
3373 total += phone.getVtDataUsage();
3374 }
3375 return total;
3376 }
Jack Yu75ab2952016-07-08 14:29:33 -07003377
3378 /**
3379 * Policy control of data connection. Usually used when data limit is passed.
3380 * @param enabled True if enabling the data, otherwise disabling.
3381 * @param subId Subscription index
3382 * {@hide}
3383 */
3384 @Override
3385 public void setPolicyDataEnabled(boolean enabled, int subId) {
3386 enforceModifyPermission();
3387 Phone phone = getPhone(subId);
3388 if (phone != null) {
3389 phone.setPolicyDataEnabled(enabled);
3390 }
3391 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003392}