blob: 891d469e8eeadee0764a53c8361dea9ae7ab4850 [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;
Ihab Awadf2177b72013-11-25 13:33:23 -080056import android.telephony.TelephonyManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070057import android.telephony.ModemActivityInfo;
58import android.telephony.TelephonyHistogram;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080060import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080062import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080063import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064
Andrew Lee312e8172014-10-23 17:01:36 -070065import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070066import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070067import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070068import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080071import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010072import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070073import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070075import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070076import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070077import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070078import com.android.internal.telephony.PhoneConstants;
79import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070080import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080081import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070082import com.android.internal.telephony.uicc.IccIoResult;
83import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070084import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070085import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080086import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080087import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088
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
1902 public void setVisualVoicemailSmsFilterEnabled(int subId, boolean value) {
1903 VisualVoicemailSmsFilterConfig
1904 .setVisualVoicemailSmsFilterEnabled(mPhone.getContext(), subId, value);
1905 }
1906
1907 @Override
1908 public boolean isVisualVoicemailSmsFilterEnabled(String packageName, int subId) {
1909 return VisualVoicemailSmsFilterConfig
1910 .isVisualVoicemailSmsFilterEnabled(mPhone.getContext(), packageName, subId);
1911 }
1912
1913 @Override
1914 public void setVisualVoicemailSmsFilterClientPrefix(int subId, String prefix) {
1915 VisualVoicemailSmsFilterConfig
1916 .setVisualVoicemailSmsFilterClientPrefix(mPhone.getContext(), subId, prefix);
1917 }
1918
1919 @Override
1920 public String getVisualVoicemailSmsFilterClientPrefix(String packageName, int subId) {
1921 return VisualVoicemailSmsFilterConfig
1922 .getVisualVoicemailSmsFilterClientPrefix(mPhone.getContext(), packageName, subId);
1923 }
1924
1925 @Override
1926 public void setVisualVoicemailSmsFilterOriginatingNumbers(int subId, String[] numbers) {
1927 VisualVoicemailSmsFilterConfig
1928 .setVisualVoicemailSmsFilterOriginatingNumbers(mPhone.getContext(), subId, numbers);
1929 }
1930
1931 @Override
1932 public String[] getVisualVoicemailSmsFilterOriginatingNumbers(String packageName, int subId) {
1933 return VisualVoicemailSmsFilterConfig
1934 .getVisualVoicemailSmsFilterOriginatingNumbers(mPhone.getContext(), packageName,
1935 subId);
1936 }
1937
1938 @Override
1939 public void setVisualVoicemailSmsFilterDestinationPort(int subId, int port) {
1940 VisualVoicemailSmsFilterConfig
1941 .setVisualVoicemailSmsFilterDestinationPort(mPhone.getContext(), subId, port);
1942 }
1943
1944 @Override
1945 public int getVisualVoicemailSmsFilterDestinationPort(String packageName, int subId) {
1946 return VisualVoicemailSmsFilterConfig
1947 .getVisualVoicemailSmsFilterDestinationPort(mPhone.getContext(), packageName,
1948 subId);
1949 }
1950
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001951 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001952 * Returns the unread count of voicemails
1953 */
1954 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001955 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001956 }
1957
1958 /**
1959 * Returns the unread count of voicemails for a subId
1960 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001961 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001962 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001963 final Phone phone = getPhone(subId);
1964 if (phone != null) {
1965 return phone.getVoiceMessageCount();
1966 } else {
1967 return 0;
1968 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001969 }
1970
1971 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001972 * Returns the data network type.
1973 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001974 *
1975 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1976 */
1977 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001978 public int getNetworkType() {
1979 final Phone phone = getPhone(getDefaultSubscription());
1980 if (phone != null) {
1981 return phone.getServiceState().getDataNetworkType();
1982 } else {
1983 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1984 }
Wink Saville36469e72014-06-11 15:17:00 -07001985 }
1986
1987 /**
1988 * Returns the network type for a subId
1989 */
1990 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001991 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1992 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1993 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1994 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001995
Sanket Padawe356d7632015-06-22 14:03:32 -07001996 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001997 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001998 return phone.getServiceState().getDataNetworkType();
1999 } else {
2000 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2001 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002002 }
2003
2004 /**
2005 * Returns the data network type
2006 */
2007 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002008 public int getDataNetworkType(String callingPackage) {
2009 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002010 }
2011
2012 /**
2013 * Returns the data network type for a subId
2014 */
2015 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002016 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2017 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2018 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2019 }
2020
Sanket Padawe356d7632015-06-22 14:03:32 -07002021 final Phone phone = getPhone(subId);
2022 if (phone != null) {
2023 return phone.getServiceState().getDataNetworkType();
2024 } else {
2025 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2026 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002027 }
2028
2029 /**
Wink Saville36469e72014-06-11 15:17:00 -07002030 * Returns the Voice network type for a subId
2031 */
2032 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002033 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2034 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2035 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2036 }
2037
Sanket Padawe356d7632015-06-22 14:03:32 -07002038 final Phone phone = getPhone(subId);
2039 if (phone != null) {
2040 return phone.getServiceState().getVoiceNetworkType();
2041 } else {
2042 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2043 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002044 }
2045
2046 /**
2047 * @return true if a ICC card is present
2048 */
2049 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002050 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002051 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002052 }
2053
2054 /**
2055 * @return true if a ICC card is present for a slotId
2056 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002057 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002058 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002059 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002060 final Phone phone = getPhone(subId[0]);
2061 if (subId != null && phone != null) {
2062 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002063 } else {
2064 return false;
2065 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002066 }
2067
2068 /**
2069 * Return if the current radio is LTE on CDMA. This
2070 * is a tri-state return value as for a period of time
2071 * the mode may be unknown.
2072 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002073 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002074 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002075 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002076 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002077 @Override
2078 public int getLteOnCdmaMode(String callingPackage) {
2079 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002080 }
2081
Sanket Padawe356d7632015-06-22 14:03:32 -07002082 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002083 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2084 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2085 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2086 }
2087
Sanket Padawe356d7632015-06-22 14:03:32 -07002088 final Phone phone = getPhone(subId);
2089 if (phone == null) {
2090 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2091 } else {
2092 return phone.getLteOnCdmaMode();
2093 }
Wink Saville36469e72014-06-11 15:17:00 -07002094 }
2095
2096 public void setPhone(Phone phone) {
2097 mPhone = phone;
2098 }
2099
2100 /**
2101 * {@hide}
2102 * Returns Default subId, 0 in the case of single standby.
2103 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002104 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002105 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002106 }
2107
Shishir Agrawala9f32182016-04-12 12:00:16 -07002108 private int getSlotForDefaultSubscription() {
2109 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2110 }
2111
Wink Savilleb564aae2014-10-23 10:18:09 -07002112 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002113 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002114 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002115
2116 /**
2117 * @see android.telephony.TelephonyManager.WifiCallingChoices
2118 */
2119 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002120 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2121 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002122 }
2123
2124 /**
2125 * @see android.telephony.TelephonyManager.WifiCallingChoices
2126 */
2127 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002128 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2129 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2130 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002131 }
2132
Sailesh Nepald1e68152013-12-12 19:08:02 -08002133 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002134 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002135 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002136 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002137
Shishir Agrawal566b7612013-10-28 14:41:00 -07002138 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002139 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2140 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002141
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002142 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002143 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002144 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002145 if (DBG) log("iccOpenLogicalChannel: " + response);
2146 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002147 }
2148
2149 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002150 public boolean iccCloseLogicalChannel(int subId, int channel) {
2151 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002152
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002153 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002154 if (channel < 0) {
2155 return false;
2156 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002157 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002158 if (DBG) log("iccCloseLogicalChannel: " + success);
2159 return success;
2160 }
2161
2162 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002163 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002164 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002165 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002166
2167 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002168 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2169 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002170 " data=" + data);
2171 }
2172
2173 if (channel < 0) {
2174 return "";
2175 }
2176
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002177 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002178 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002179 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2180
Shishir Agrawal566b7612013-10-28 14:41:00 -07002181 // Append the returned status code to the end of the response payload.
2182 String s = Integer.toHexString(
2183 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002184 if (response.payload != null) {
2185 s = IccUtils.bytesToHexString(response.payload) + s;
2186 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002187 return s;
2188 }
Jake Hambye994d462014-02-03 13:10:13 -08002189
Evan Charltonc66da362014-05-16 14:06:40 -07002190 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002191 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002192 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002193 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002194
2195 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002196 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2197 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002198 }
2199
2200 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002201 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002202 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2203
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002204 // Append the returned status code to the end of the response payload.
2205 String s = Integer.toHexString(
2206 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002207 if (response.payload != null) {
2208 s = IccUtils.bytesToHexString(response.payload) + s;
2209 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002210 return s;
2211 }
2212
2213 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002214 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002215 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002216 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002217
2218 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002219 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002220 p1 + " " + p2 + " " + p3 + ":" + filePath);
2221 }
2222
2223 IccIoResult response =
2224 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002225 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2226 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002227
2228 if (DBG) {
2229 log("Exchange SIM_IO [R]" + response);
2230 }
2231
2232 byte[] result = null;
2233 int length = 2;
2234 if (response.payload != null) {
2235 length = 2 + response.payload.length;
2236 result = new byte[length];
2237 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2238 } else {
2239 result = new byte[length];
2240 }
2241
2242 result[length - 1] = (byte) response.sw2;
2243 result[length - 2] = (byte) response.sw1;
2244 return result;
2245 }
2246
2247 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002248 public String sendEnvelopeWithStatus(int subId, String content) {
2249 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002250
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002251 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002252 if (response.payload == null) {
2253 return "";
2254 }
2255
2256 // Append the returned status code to the end of the response payload.
2257 String s = Integer.toHexString(
2258 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2259 s = IccUtils.bytesToHexString(response.payload) + s;
2260 return s;
2261 }
2262
Jake Hambye994d462014-02-03 13:10:13 -08002263 /**
2264 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2265 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2266 *
2267 * @param itemID the ID of the item to read
2268 * @return the NV item as a String, or null on error.
2269 */
2270 @Override
2271 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002272 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002273 if (DBG) log("nvReadItem: item " + itemID);
2274 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2275 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2276 return value;
2277 }
2278
2279 /**
2280 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2281 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2282 *
2283 * @param itemID the ID of the item to read
2284 * @param itemValue the value to write, as a String
2285 * @return true on success; false on any failure
2286 */
2287 @Override
2288 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002289 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002290 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2291 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2292 new Pair<Integer, String>(itemID, itemValue));
2293 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2294 return success;
2295 }
2296
2297 /**
2298 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2299 * Used for device configuration by some CDMA operators.
2300 *
2301 * @param preferredRoamingList byte array containing the new PRL
2302 * @return true on success; false on any failure
2303 */
2304 @Override
2305 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002306 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002307 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2308 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2309 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2310 return success;
2311 }
2312
2313 /**
2314 * Perform the specified type of NV config reset.
2315 * Used for device configuration by some CDMA operators.
2316 *
2317 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2318 * @return true on success; false on any failure
2319 */
2320 @Override
2321 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002322 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002323 if (DBG) log("nvResetConfig: type " + resetType);
2324 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2325 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2326 return success;
2327 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002328
2329 /**
Wink Saville36469e72014-06-11 15:17:00 -07002330 * {@hide}
2331 * Returns Default sim, 0 in the case of single standby.
2332 */
2333 public int getDefaultSim() {
2334 //TODO Need to get it from Telephony Devcontroller
2335 return 0;
2336 }
2337
Svet Ganovb320e182015-04-16 12:30:10 -07002338 public String[] getPcscfAddress(String apnType, String callingPackage) {
2339 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2340 return new String[0];
2341 }
2342
2343
ram87fca6f2014-07-18 18:58:44 +05302344 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002345 }
2346
2347 public void setImsRegistrationState(boolean registered) {
2348 enforceModifyPermission();
2349 mPhone.setImsRegistrationState(registered);
2350 }
2351
2352 /**
Stuart Scott54788802015-03-30 13:18:01 -07002353 * Set the network selection mode to automatic.
2354 *
2355 */
2356 @Override
2357 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002358 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002359 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2360 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2361 }
2362
2363 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002364 * Set the network selection mode to manual with the selected carrier.
2365 */
2366 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002367 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2368 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002369 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002370 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002371 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2372 persistSelection);
2373 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002374 }
2375
2376 /**
2377 * Scans for available networks.
2378 */
2379 @Override
2380 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002381 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002382 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2383 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2384 CMD_PERFORM_NETWORK_SCAN, null, subId);
2385 return result;
2386 }
2387
2388 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002389 * Get the calculated preferred network type.
2390 * Used for debugging incorrect network type.
2391 *
2392 * @return the preferred network type, defined in RILConstants.java.
2393 */
2394 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002395 public int getCalculatedPreferredNetworkType(String callingPackage) {
2396 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2397 return RILConstants.PREFERRED_NETWORK_MODE;
2398 }
2399
Amit Mahajan43330e02014-11-18 11:54:45 -08002400 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002401 }
2402
2403 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002404 * Get the preferred network type.
2405 * Used for device configuration by some CDMA operators.
2406 *
2407 * @return the preferred network type, defined in RILConstants.java.
2408 */
2409 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002410 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002411 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002412 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002413 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002414 int networkType = (result != null ? result[0] : -1);
2415 if (DBG) log("getPreferredNetworkType: " + networkType);
2416 return networkType;
2417 }
2418
2419 /**
2420 * Set the preferred network type.
2421 * Used for device configuration by some CDMA operators.
2422 *
2423 * @param networkType the preferred network type, defined in RILConstants.java.
2424 * @return true on success; false on any failure.
2425 */
2426 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002427 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002428 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002429 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2430 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002431 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002432 if (success) {
2433 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002434 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002435 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002436 return success;
2437 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002438
2439 /**
Junda Liu475951f2014-11-07 16:45:03 -08002440 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2441 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2442 * tethering.
2443 *
2444 * @return 0: Not required. 1: required. 2: Not set.
2445 * @hide
2446 */
2447 @Override
2448 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002449 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002450 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2451 Settings.Global.TETHER_DUN_REQUIRED, 2);
2452 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2453 // config_tether_apndata.
2454 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2455 dunRequired = 1;
2456 }
2457 return dunRequired;
2458 }
2459
2460 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002461 * Set mobile data enabled
2462 * Used by the user through settings etc to turn on/off mobile data
2463 *
2464 * @param enable {@code true} turn turn data on, else {@code false}
2465 */
2466 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002467 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002468 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002469 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002470 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002471 Phone phone = PhoneFactory.getPhone(phoneId);
2472 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002473 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002474 phone.setDataEnabled(enable);
2475 } else {
2476 loge("setDataEnabled: no phone for subId=" + subId);
2477 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002478 }
2479
2480 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002481 * Get whether mobile data is enabled.
2482 *
2483 * Note that this used to be available from ConnectivityService, gated by
2484 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2485 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002486 *
2487 * @return {@code true} if data is enabled else {@code false}
2488 */
2489 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002490 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002491 try {
2492 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2493 null);
2494 } catch (Exception e) {
2495 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2496 null);
2497 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002498 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002499 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002500 Phone phone = PhoneFactory.getPhone(phoneId);
2501 if (phone != null) {
2502 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002503 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002504 return retVal;
2505 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002506 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002507 return false;
2508 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002509 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002510
2511 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002512 public int getCarrierPrivilegeStatus(int subId) {
2513 final Phone phone = getPhone(subId);
2514 if (phone == null) {
2515 loge("getCarrierPrivilegeStatus: Invalid subId");
2516 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2517 }
2518 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002519 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002520 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002521 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2522 }
2523 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002524 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002525 }
Junda Liu29340342014-07-10 15:23:27 -07002526
2527 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002528 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002529 if (TextUtils.isEmpty(pkgName))
2530 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002531 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002532 if (card == null) {
2533 loge("checkCarrierPrivilegesForPackage: No UICC");
2534 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2535 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002536 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2537 }
2538
2539 @Override
2540 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002541 if (TextUtils.isEmpty(pkgName))
2542 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002543 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2544 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2545 UiccCard card = UiccController.getInstance().getUiccCard(i);
2546 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002547 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002548 continue;
2549 }
2550
2551 result = card.getCarrierPrivilegeStatus(
2552 mPhone.getContext().getPackageManager(), pkgName);
2553 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2554 break;
2555 }
2556 }
2557
2558 return result;
Junda Liu29340342014-07-10 15:23:27 -07002559 }
Derek Tan89e89d42014-07-08 17:00:10 -07002560
2561 @Override
Junda Liue64de782015-04-16 17:19:16 -07002562 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2563 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2564 loge("phoneId " + phoneId + " is not valid.");
2565 return null;
2566 }
2567 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002568 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002569 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002570 return null ;
2571 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002572 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002573 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002574 }
2575
Amith Yamasani6e118872016-02-19 12:53:51 -08002576 @Override
2577 public List<String> getPackagesWithCarrierPrivileges() {
2578 PackageManager pm = mPhone.getContext().getPackageManager();
2579 List<String> privilegedPackages = new ArrayList<>();
2580 List<PackageInfo> packages = null;
2581 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2582 UiccCard card = UiccController.getInstance().getUiccCard(i);
2583 if (card == null) {
2584 // No UICC in that slot.
2585 continue;
2586 }
2587 if (card.hasCarrierPrivilegeRules()) {
2588 if (packages == null) {
2589 // Only check packages in user 0 for now
2590 packages = pm.getInstalledPackagesAsUser(
2591 PackageManager.MATCH_DISABLED_COMPONENTS
2592 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2593 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2594 }
2595 for (int p = packages.size() - 1; p >= 0; p--) {
2596 PackageInfo pkgInfo = packages.get(p);
2597 if (pkgInfo != null && pkgInfo.packageName != null
2598 && card.getCarrierPrivilegeStatus(pkgInfo)
2599 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2600 privilegedPackages.add(pkgInfo.packageName);
2601 }
2602 }
2603 }
2604 }
2605 return privilegedPackages;
2606 }
2607
Wink Savilleb564aae2014-10-23 10:18:09 -07002608 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002609 final Phone phone = getPhone(subId);
2610 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002611 if (card == null) {
2612 loge("getIccId: No UICC");
2613 return null;
2614 }
2615 String iccId = card.getIccId();
2616 if (TextUtils.isEmpty(iccId)) {
2617 loge("getIccId: ICC ID is null or empty.");
2618 return null;
2619 }
2620 return iccId;
2621 }
2622
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002623 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002624 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2625 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002626 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002627
Jeff Sharkey85190e62014-12-05 09:40:12 -08002628 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002629 final Phone phone = getPhone(subId);
2630 if (phone == null) {
2631 return false;
2632 }
2633 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002634
2635 if (DBG_MERGE) {
2636 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2637 + subscriberId + " to " + number);
2638 }
2639
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002640 if (TextUtils.isEmpty(iccId)) {
2641 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002642 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002643
Jeff Sharkey85190e62014-12-05 09:40:12 -08002644 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2645
2646 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002647 if (alphaTag == null) {
2648 editor.remove(alphaTagPrefKey);
2649 } else {
2650 editor.putString(alphaTagPrefKey, alphaTag);
2651 }
2652
Jeff Sharkey85190e62014-12-05 09:40:12 -08002653 // Record both the line number and IMSI for this ICCID, since we need to
2654 // track all merged IMSIs based on line number
2655 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2656 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002657 if (number == null) {
2658 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002659 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002660 } else {
2661 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002662 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002663 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002664
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002665 editor.commit();
2666 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002667 }
2668
2669 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002670 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002671 // This is open to apps with WRITE_SMS.
2672 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002673 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002674 return null;
2675 }
Derek Tan97ebb422014-09-05 16:55:38 -07002676
2677 String iccId = getIccId(subId);
2678 if (iccId != null) {
2679 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002680 if (DBG_MERGE) {
2681 log("getLine1NumberForDisplay returning " +
2682 mTelephonySharedPreferences.getString(numberPrefKey, null));
2683 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002684 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002685 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002686 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002687 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002688 }
2689
2690 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002691 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2692 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2693 return null;
2694 }
Derek Tan97ebb422014-09-05 16:55:38 -07002695
2696 String iccId = getIccId(subId);
2697 if (iccId != null) {
2698 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002699 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002700 }
Derek Tan97ebb422014-09-05 16:55:38 -07002701 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002702 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002703
2704 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002705 public String[] getMergedSubscriberIds(String callingPackage) {
2706 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2707 return null;
2708 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002709 final Context context = mPhone.getContext();
2710 final TelephonyManager tele = TelephonyManager.from(context);
2711 final SubscriptionManager sub = SubscriptionManager.from(context);
2712
2713 // Figure out what subscribers are currently active
2714 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002715 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2716 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2717 final long identity = Binder.clearCallingIdentity();
2718 try {
2719 final int[] subIds = sub.getActiveSubscriptionIdList();
2720 for (int subId : subIds) {
2721 activeSubscriberIds.add(tele.getSubscriberId(subId));
2722 }
2723 } finally {
2724 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002725 }
2726
2727 // First pass, find a number override for an active subscriber
2728 String mergeNumber = null;
2729 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2730 for (String key : prefs.keySet()) {
2731 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2732 final String subscriberId = (String) prefs.get(key);
2733 if (activeSubscriberIds.contains(subscriberId)) {
2734 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2735 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2736 mergeNumber = (String) prefs.get(numberKey);
2737 if (DBG_MERGE) {
2738 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2739 + " for active subscriber " + subscriberId);
2740 }
2741 if (!TextUtils.isEmpty(mergeNumber)) {
2742 break;
2743 }
2744 }
2745 }
2746 }
2747
2748 // Shortcut when no active merged subscribers
2749 if (TextUtils.isEmpty(mergeNumber)) {
2750 return null;
2751 }
2752
2753 // Second pass, find all subscribers under that line override
2754 final ArraySet<String> result = new ArraySet<>();
2755 for (String key : prefs.keySet()) {
2756 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2757 final String number = (String) prefs.get(key);
2758 if (mergeNumber.equals(number)) {
2759 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2760 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2761 final String subscriberId = (String) prefs.get(subscriberKey);
2762 if (!TextUtils.isEmpty(subscriberId)) {
2763 result.add(subscriberId);
2764 }
2765 }
2766 }
2767 }
2768
2769 final String[] resultArray = result.toArray(new String[result.size()]);
2770 Arrays.sort(resultArray);
2771 if (DBG_MERGE) {
2772 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2773 }
2774 return resultArray;
2775 }
2776
2777 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002778 public boolean setOperatorBrandOverride(int subId, String brand) {
2779 enforceCarrierPrivilege(subId);
2780 final Phone phone = getPhone(subId);
2781 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002782 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002783
2784 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002785 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002786 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2787 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002788 enforceCarrierPrivilege(subId);
2789 final Phone phone = getPhone(subId);
2790 if (phone == null) {
2791 return false;
2792 }
2793 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002794 cdmaNonRoamingList);
2795 }
2796
2797 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002798 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2799 enforceModifyPermission();
2800
2801 int returnValue = 0;
2802 try {
2803 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2804 if(result.exception == null) {
2805 if (result.result != null) {
2806 byte[] responseData = (byte[])(result.result);
2807 if(responseData.length > oemResp.length) {
2808 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2809 responseData.length + "bytes. Buffer Size is " +
2810 oemResp.length + "bytes.");
2811 }
2812 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2813 returnValue = responseData.length;
2814 }
2815 } else {
2816 CommandException ex = (CommandException) result.exception;
2817 returnValue = ex.getCommandError().ordinal();
2818 if(returnValue > 0) returnValue *= -1;
2819 }
2820 } catch (RuntimeException e) {
2821 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2822 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2823 if(returnValue > 0) returnValue *= -1;
2824 }
2825
2826 return returnValue;
2827 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002828
2829 @Override
2830 public void setRadioCapability(RadioAccessFamily[] rafs) {
2831 try {
2832 ProxyController.getInstance().setRadioCapability(rafs);
2833 } catch (RuntimeException e) {
2834 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2835 }
2836 }
2837
2838 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002839 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2840 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2841 return RadioAccessFamily.RAF_UNKNOWN;
2842 }
2843
Wink Saville5d475dd2014-10-17 15:00:58 -07002844 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2845 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002846
2847 @Override
2848 public void enableVideoCalling(boolean enable) {
2849 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002850 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002851 }
2852
2853 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002854 public boolean isVideoCallingEnabled(String callingPackage) {
2855 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2856 return false;
2857 }
2858
Andrew Lee77527ac2014-10-21 16:57:39 -07002859 // Check the user preference and the system-level IMS setting. Even if the user has
2860 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2861 // In the long run, we may instead need to check if there exists a connection service
2862 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002863 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2864 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002865 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002866 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002867
Andrew Leea1239f22015-03-02 17:44:07 -08002868 @Override
2869 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002870 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002871 }
2872
2873 @Override
2874 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002875 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002876 }
2877
Andrew Lee9431b832015-03-09 18:46:45 -07002878 @Override
2879 public boolean isTtyModeSupported() {
2880 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2881 TelephonyManager telephonyManager =
2882 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2883 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2884 }
2885
2886 @Override
2887 public boolean isHearingAidCompatibilitySupported() {
2888 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2889 }
2890
Sanket Padawe7310cc72015-01-14 09:53:20 -08002891 /**
2892 * Returns the unique device ID of phone, for example, the IMEI for
2893 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2894 *
2895 * <p>Requires Permission:
2896 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2897 */
2898 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002899 public String getDeviceId(String callingPackage) {
2900 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2901 return null;
2902 }
2903
Sanket Padawe7310cc72015-01-14 09:53:20 -08002904 final Phone phone = PhoneFactory.getPhone(0);
2905 if (phone != null) {
2906 return phone.getDeviceId();
2907 } else {
2908 return null;
2909 }
2910 }
2911
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002912 /*
2913 * {@hide}
2914 * Returns the IMS Registration Status
2915 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002916 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002917 public boolean isImsRegistered() {
2918 return mPhone.isImsRegistered();
2919 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002920
2921 @Override
2922 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2923 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2924 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002925
Nathan Haroldc55097a2015-03-11 18:14:50 -07002926 /*
2927 * {@hide}
2928 * Returns the IMS Registration Status
2929 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002930 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002931 return mPhone.isWifiCallingEnabled();
2932 }
2933
2934 /*
2935 * {@hide}
2936 * Returns the IMS Registration Status
2937 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002938 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002939 return mPhone.isVolteEnabled();
2940 }
Svet Ganovb320e182015-04-16 12:30:10 -07002941
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002942 /*
2943 * {@hide} Returns the IMS Registration Status
2944 */
2945 public boolean isVideoTelephonyAvailable() {
2946 return mPhone.isVideoEnabled();
2947 }
2948
Svet Ganovb320e182015-04-16 12:30:10 -07002949 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002950 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002951 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002952 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2953
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002954 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002955 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002956 } catch (SecurityException e) {
2957 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2958 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002959 }
Svet Ganovb320e182015-04-16 12:30:10 -07002960
2961 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2962 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2963 return false;
2964 }
2965
2966 return true;
2967 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002968
Makoto Onukifee69342015-06-29 14:44:50 -07002969 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002970 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002971 */
2972 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002973 // Default SMS app can always read it.
2974 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2975 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2976 return true;
2977 }
2978 try {
2979 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002980 } catch (SecurityException readPhoneStateSecurityException) {
2981 try {
2982 // Can be read with READ_SMS too.
2983 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2984 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2985 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2986 } catch (SecurityException readSmsSecurityException) {
2987 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2988 // permissions
2989 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2990 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2991 " or " + android.Manifest.permission.READ_SMS + ".");
2992 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002993 }
Makoto Onukifee69342015-06-29 14:44:50 -07002994 }
2995
Stuart Scott8eef64f2015-04-08 15:13:54 -07002996 @Override
2997 public void factoryReset(int subId) {
2998 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002999 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3000 return;
3001 }
3002
Svet Ganovcc087f82015-05-12 20:35:54 -07003003 final long identity = Binder.clearCallingIdentity();
3004 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003005 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3006 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003007 // Enable data
3008 setDataEnabled(subId, true);
3009 // Set network selection mode to automatic
3010 setNetworkSelectionModeAutomatic(subId);
3011 // Set preferred mobile network type to the best available
3012 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3013 // Turn off roaming
3014 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3015 }
3016 } finally {
3017 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003018 }
3019 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003020
3021 @Override
3022 public String getLocaleFromDefaultSim() {
3023 // We query all subscriptions instead of just the active ones, because
3024 // this might be called early on in the provisioning flow when the
3025 // subscriptions potentially aren't active yet.
3026 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3027 if (slist == null || slist.isEmpty()) {
3028 return null;
3029 }
3030
3031 // This function may be called very early, say, from the setup wizard, at
3032 // which point we won't have a default subscription set. If that's the case
3033 // we just choose the first, which will be valid in "most cases".
3034 final int defaultSubId = getDefaultSubscription();
3035 SubscriptionInfo info = null;
3036 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3037 info = slist.get(0);
3038 } else {
3039 for (SubscriptionInfo item : slist) {
3040 if (item.getSubscriptionId() == defaultSubId) {
3041 info = item;
3042 break;
3043 }
3044 }
3045
3046 if (info == null) {
3047 return null;
3048 }
3049 }
3050
3051 // Try and fetch the locale from the carrier properties or from the SIM language
3052 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003053 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003054 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003055 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003056 if (defaultPhone != null) {
3057 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3058 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003059 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003060 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3061 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003062 } else {
3063 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003064 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003065 }
3066 }
3067
Narayan Kamath011676f2015-07-29 12:04:08 +01003068 // The SIM language preferences only store a language (e.g. fr = French), not an
3069 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3070 // the SIM and carrier preferences does not include a country we add the country
3071 // determined from the SIM MCC to provide an exact locale.
3072 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003073 if (mccLocale != null) {
3074 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3075 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003076 }
3077
Tony Hill183b2de2015-06-24 14:53:58 +01003078 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003079 return null;
3080 }
3081
3082 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3083 final long identity = Binder.clearCallingIdentity();
3084 try {
3085 return mSubscriptionController.getAllSubInfoList(
3086 mPhone.getContext().getOpPackageName());
3087 } finally {
3088 Binder.restoreCallingIdentity(identity);
3089 }
3090 }
3091
3092 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3093 final long identity = Binder.clearCallingIdentity();
3094 try {
3095 return mSubscriptionController.getActiveSubscriptionInfoList(
3096 mPhone.getContext().getOpPackageName());
3097 } finally {
3098 Binder.restoreCallingIdentity(identity);
3099 }
3100 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003101
3102 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003103 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3104 * representing the state of the modem.
3105 *
3106 * NOTE: This clears the modem state, so there should only every be one caller.
3107 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003108 */
3109 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003110 public void requestModemActivityInfo(ResultReceiver result) {
3111 enforceModifyPermission();
3112
3113 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3114 Bundle bundle = new Bundle();
3115 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3116 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003117 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003118
3119 /**
3120 * {@hide}
3121 * Returns the service state information on specified subscription.
3122 */
3123 @Override
3124 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3125
3126 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3127 return null;
3128 }
3129
3130 final Phone phone = getPhone(subId);
3131 if (phone == null) {
3132 return null;
3133 }
3134
3135 return phone.getServiceState();
3136 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003137
3138 /**
3139 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3140 *
3141 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3142 * voicemail ringtone.
3143 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3144 * PhoneAccount.
3145 */
3146 @Override
3147 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3148 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3149 if (phone == null) {
3150 return null;
3151 }
3152
3153 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3154 }
3155
3156 /**
3157 * Returns whether vibration is set for voicemail notification in Phone settings.
3158 *
3159 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3160 * voicemail vibration setting.
3161 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3162 */
3163 @Override
3164 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3165 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3166 if (phone == null) {
3167 return false;
3168 }
3169
3170 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3171 }
3172
Youhan Wange64578a2016-05-02 15:32:42 -07003173 /**
3174 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3175 *
3176 * @throws SecurityException if the caller does not have the required permission
3177 */
3178 private void enforceReadPrivilegedPermission() {
3179 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3180 null);
3181 }
3182
3183 /**
3184 * Return the application ID for the app type.
3185 *
3186 * @param subId the subscription ID that this request applies to.
3187 * @param appType the uicc app type.
3188 * @return Application ID for specificied app type, or null if no uicc.
3189 */
3190 @Override
3191 public String getAidForAppType(int subId, int appType) {
3192 enforceReadPrivilegedPermission();
3193 Phone phone = getPhone(subId);
3194 if (phone == null) {
3195 return null;
3196 }
3197 String aid = null;
3198 try {
3199 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3200 .getApplicationByType(appType).getAid();
3201 } catch (Exception e) {
3202 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3203 }
3204 return aid;
3205 }
3206
Youhan Wang4001d252016-05-11 10:29:41 -07003207 /**
3208 * Return the Electronic Serial Number.
3209 *
3210 * @param subId the subscription ID that this request applies to.
3211 * @return ESN or null if error.
3212 */
3213 @Override
3214 public String getEsn(int subId) {
3215 enforceReadPrivilegedPermission();
3216 Phone phone = getPhone(subId);
3217 if (phone == null) {
3218 return null;
3219 }
3220 String esn = null;
3221 try {
3222 esn = phone.getEsn();
3223 } catch (Exception e) {
3224 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3225 }
3226 return esn;
3227 }
3228
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003229 /**
3230 * Get snapshot of Telephony histograms
3231 * @return List of Telephony histograms
3232 * @hide
3233 */
3234 @Override
3235 public List<TelephonyHistogram> getTelephonyHistograms() {
3236 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3237 return RIL.getTelephonyRILTimingHistograms();
3238 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003239
3240 /**
3241 * {@hide}
3242 * Set the allowed carrier list for slotId
3243 * Require system privileges. In the future we may add this to carrier APIs.
3244 *
3245 * @return The number of carriers set successfully, should match length of carriers
3246 */
3247 @Override
3248 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3249 enforceModifyPermission();
3250 int subId = SubscriptionManager.getSubId(slotId)[0];
3251 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3252 return retVal[0];
3253 }
3254
3255 /**
3256 * {@hide}
3257 * Get the allowed carrier list for slotId.
3258 * Require system privileges. In the future we may add this to carrier APIs.
3259 *
3260 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3261 * means all carriers are allowed.
3262 */
3263 @Override
3264 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3265 enforceReadPrivilegedPermission();
3266 int subId = SubscriptionManager.getSubId(slotId)[0];
3267 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3268 }
3269
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003270}