blob: 15a2a30d903135e7a18a7c283be593be544f9b69 [file] [log] [blame]
Santos Cordon176ae282014-07-14 02:02:14 -07001/*
2 * Copyright (C) 2014 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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Santos Cordon176ae282014-07-14 02:02:14 -070018
Sailesh Nepalfc43ea82015-07-28 19:30:28 -070019import android.app.ActivityManager;
Tyler Gunncb59b672014-08-20 09:02:11 -070020import android.Manifest;
Evan Charlton105d9772014-11-25 14:08:53 -080021import android.content.ComponentName;
22import android.content.Context;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070023import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
Tyler Gunncb59b672014-08-20 09:02:11 -070026import android.content.pm.ServiceInfo;
Evan Charlton105d9772014-11-25 14:08:53 -080027import android.content.pm.UserInfo;
Ihab Awadd9f54382014-10-24 11:44:47 -070028import android.graphics.Bitmap;
29import android.graphics.BitmapFactory;
Santos Cordon9c30c282015-05-13 16:28:27 -070030import android.graphics.drawable.Icon;
Evan Charlton105d9772014-11-25 14:08:53 -080031import android.net.Uri;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080032import android.os.Binder;
Tyler Gunn369c8742015-10-07 15:39:28 -070033import android.os.Bundle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070034import android.os.PersistableBundle;
Evan Charlton105d9772014-11-25 14:08:53 -080035import android.os.Process;
36import android.os.UserHandle;
37import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070038import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070039import android.telecom.ConnectionService;
Santos Cordon59c21a72015-06-11 10:11:21 -070040import android.telecom.DefaultDialerManager;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070041import android.telecom.PhoneAccount;
42import android.telecom.PhoneAccountHandle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070043import android.telephony.CarrierConfigManager;
Nancy Chen140004a2014-10-15 15:48:38 -070044import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070045import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080046import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070047import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070048import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070049import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070050import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070051
Tyler Gunn91d43cf2014-09-17 12:19:39 -070052// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070053import com.android.internal.annotations.VisibleForTesting;
54import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070055import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070056import com.android.internal.util.XmlUtils;
57
Evan Charltonaf51ceb2014-07-30 11:56:36 -070058import org.xmlpull.v1.XmlPullParser;
59import org.xmlpull.v1.XmlPullParserException;
60import org.xmlpull.v1.XmlSerializer;
61
Ihab Awadb78b2762014-07-25 15:16:23 -070062import java.io.BufferedInputStream;
63import java.io.BufferedOutputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070064import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070065import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070066import java.io.File;
67import java.io.FileNotFoundException;
68import java.io.FileOutputStream;
69import java.io.IOException;
70import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070071import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070072import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070073import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070074import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080075import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070076import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070077import java.util.List;
78import java.util.Objects;
Ihab Awadb78b2762014-07-25 15:16:23 -070079import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070080
81/**
Evan Charlton89176372014-07-19 18:23:09 -070082 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070083 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
84 * implemented in {@link TelecomServiceImpl}, with the notable exception that
85 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
86 * proper authority over the {@code ComponentName}s they are declaring in their
87 * {@code PhoneAccountHandle}s.
88 *
89 *
90 * -- About Users and Phone Accounts --
91 *
Santos Cordon9c30c282015-05-13 16:28:27 -070092 * We store all phone accounts for all users in a single place, which means that there are three
93 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -070094 * 1) The Android User that is currently active on the device.
95 * 2) The user which owns/registers the phone account.
96 * 3) The user running the app that is requesting the phone account information.
97 *
98 * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
99 * has a work profile running as another user (B2). Lets say that user B opens the phone settings
100 * (not currently supported, but theoretically speaking), and phone settings queries for a phone
Santos Cordon9c30c282015-05-13 16:28:27 -0700101 * account list. Lets also say that an app running in the work profile has registered a phone
102 * account. This means that:
Santos Cordon6a212642015-05-08 16:35:23 -0700103 *
104 * Since phone settings always runs as the primary user, We have the following situation:
Santos Cordon9c30c282015-05-13 16:28:27 -0700105 * User A (settings) is requesting a list of phone accounts while the active user is User B, and
106 * that list contains a phone account for profile User B2.
Santos Cordon6a212642015-05-08 16:35:23 -0700107 *
108 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
109 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
110 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700111 */
Brad Ebinger53855132015-10-30 10:58:19 -0700112public class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700113
Yorke Lee5e8836a2014-08-22 15:25:18 -0700114 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
115 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
116
Ihab Awadb78b2762014-07-25 15:16:23 -0700117 public abstract static class Listener {
118 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
119 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
120 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
121 }
122
123 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700124 @VisibleForTesting
Roshan Pius6f752c82015-08-10 10:42:06 -0700125 public static final int EXPECTED_STATE_VERSION = 8;
Tyler Gunn84253572014-09-02 14:50:05 -0700126
127 /** Keep in sync with the same in SipSettings.java */
128 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700129
130 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
131 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700132 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800133 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800134 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700135 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800136 private UserHandle mCurrentUserHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700137
Nancy Chen06ce0622014-10-23 01:17:35 +0000138 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700139 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700140 this(context, FILE_NAME);
141 }
142
143 @VisibleForTesting
144 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700145 // TODO: This file path is subject to change -- it is storing the phone account registry
146 // state file in the path /data/system/users/0/, which is likely not correct in a
147 // multi-user setting.
148 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
149 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
150 getAbsolutePath();
151 mAtomicFile = new AtomicFile(new File(filePath, fileName));
152 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700153 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700154
Ihab Awadb78b2762014-07-25 15:16:23 -0700155 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700156 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800157 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800158 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800159 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700160 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700161 }
162
Tyler Gunn84253572014-09-02 14:50:05 -0700163 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700164 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
165 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
166 * subscription id.
167 * @param accountHandle The handle for the phone account for which to retrieve the
168 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700169 * @return The value of the subscription id or -1 if it does not exist or is not valid.
Nancy Chen140004a2014-10-15 15:48:38 -0700170 */
Wink Saville35850602014-10-23 15:57:21 -0700171 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700172 PhoneAccount account = getPhoneAccountCheckCallingUser(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800173
Santos Cordon6a212642015-05-08 16:35:23 -0700174 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800175 TelephonyManager tm =
176 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
177 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700178 }
Santos Cordon479b3022015-02-06 04:27:15 -0800179 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700180 }
181
182 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800183 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
184 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
185 * will be returned.
186 *
Tyler Gunn84253572014-09-02 14:50:05 -0700187 * @param uriScheme The URI scheme for the outgoing call.
188 * @return The {@link PhoneAccountHandle} to use.
189 */
Santos Cordon6a212642015-05-08 16:35:23 -0700190 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700191 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700192
Yorke Lee5e8836a2014-08-22 15:25:18 -0700193 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700194 // If there is a default PhoneAccount, ensure it supports calls to handles with the
195 // specified uriScheme.
Santos Cordon6a212642015-05-08 16:35:23 -0700196 final PhoneAccount userSelectedAccount = getPhoneAccountCheckCallingUser(userSelected);
197 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700198 return userSelected;
199 }
Ihab Awad293edf22014-07-24 17:52:29 -0700200 }
201
Santos Cordonea5cb932015-05-07 16:28:38 -0700202 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700203 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700204 case 0:
205 // There are no accounts, so there can be no default
206 return null;
207 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800208 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700209 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700210 default:
211 // There are multiple accounts with no selected default
212 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700213 }
Ihab Awad104f8062014-07-17 11:29:35 -0700214 }
Santos Cordon176ae282014-07-14 02:02:14 -0700215
Evan Charlton105d9772014-11-25 14:08:53 -0800216 /**
217 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
218 * if it was set by another user).
219 */
Yorke Lee5e8836a2014-08-22 15:25:18 -0700220 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
Santos Cordon6a212642015-05-08 16:35:23 -0700221 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.defaultOutgoing);
222 if (account != null) {
223 return mState.defaultOutgoing;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700224 }
225 return null;
226 }
227
Santos Cordon6a212642015-05-08 16:35:23 -0700228 /**
229 * Sets the phone account with which to place all calls by default. Set by the user
230 * within phone settings.
231 */
Andrew Leea51a3862014-09-03 14:58:45 -0700232 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700233 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700234 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700235 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700236 } else {
Santos Cordon6a212642015-05-08 16:35:23 -0700237 // TODO: Do we really want to return for *any* user?
238 PhoneAccount account = getPhoneAccount(accountHandle);
239 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700240 Log.w(this, "Trying to set nonexistent default outgoing %s",
241 accountHandle);
242 return;
243 }
244
Santos Cordon6a212642015-05-08 16:35:23 -0700245 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700246 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700247 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700248 return;
249 }
250
Santos Cordon6a212642015-05-08 16:35:23 -0700251 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700252 // If the account selected is a SIM account, propagate down to the subscription
253 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700254 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800255 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700256 }
257
Ihab Awad293edf22014-07-24 17:52:29 -0700258 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700259 }
260
Ihab Awad293edf22014-07-24 17:52:29 -0700261 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700262 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700263 }
264
Nancy Chen668dee02014-11-19 15:31:31 -0800265 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
266 return getSubscriptionIdForPhoneAccount(accountHandle) ==
267 SubscriptionManager.getDefaultSmsSubId();
268 }
269
Evan Charlton105d9772014-11-25 14:08:53 -0800270 /**
Santos Cordon59c21a72015-06-11 10:11:21 -0700271 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
272 * Manager. SIM Call Manager returned corresponds to the following priority order:
273 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
274 * default dialer, then that one is returned.
275 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
276 * carrier configuration's default, then that one is returned.
277 * 3. Otherwise, we return null.
Evan Charlton105d9772014-11-25 14:08:53 -0800278 */
Ihab Awad293edf22014-07-24 17:52:29 -0700279 public PhoneAccountHandle getSimCallManager() {
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700280 long token = Binder.clearCallingIdentity();
281 int user;
282 try {
283 user = ActivityManager.getCurrentUser();
284 } finally {
285 Binder.restoreCallingIdentity(token);
286 }
287 return getSimCallManager(user);
288 }
289
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700290 public ComponentName getSystemSimCallManagerComponent() {
291 String defaultSimCallManager = null;
292 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
293 Context.CARRIER_CONFIG_SERVICE);
294 PersistableBundle configBundle = configManager.getConfig();
295 if (configBundle != null) {
296 defaultSimCallManager = configBundle.getString(
297 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
298 }
299 return TextUtils.isEmpty(defaultSimCallManager)
300 ? null : ComponentName.unflattenFromString(defaultSimCallManager);
301 }
302
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700303 /**
304 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
305 * Manager. SIM Call Manager returned corresponds to the following priority order:
306 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
307 * default dialer, then that one is returned.
308 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
309 * carrier configuration's default, then that one is returned.
310 * 3. Otherwise, we return null.
311 */
312 public PhoneAccountHandle getSimCallManager(int user) {
Santos Cordon59c21a72015-06-11 10:11:21 -0700313 // Get the default dialer in case it has a connection manager associated with it.
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700314 String dialerPackage = DefaultDialerManager.getDefaultDialerApplication(mContext, user);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700315
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700316 // Check carrier config.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700317 ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800318
Santos Cordon59c21a72015-06-11 10:11:21 -0700319 PhoneAccountHandle dialerSimCallManager = null;
320 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800321
Santos Cordon59c21a72015-06-11 10:11:21 -0700322 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
323 // loop through and look for any connection manager in the same package.
324 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
325 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
326 true /* includeDisabledAccounts */);
327 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
328 ComponentName component = accountHandle.getComponentName();
329
330 // Store the system connection manager if found
331 if (systemSimCallManager == null
332 && Objects.equals(component, systemSimCallManagerComponent)
333 && !resolveComponent(accountHandle).isEmpty()) {
334 systemSimCallManager = accountHandle;
335
336 // Store the dialer connection manager if found
337 } else if (dialerSimCallManager == null
338 && Objects.equals(component.getPackageName(), dialerPackage)
339 && !resolveComponent(accountHandle).isEmpty()) {
340 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700341 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700342 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700343 }
344
Santos Cordon59c21a72015-06-11 10:11:21 -0700345 PhoneAccountHandle retval = dialerSimCallManager != null ?
346 dialerSimCallManager : systemSimCallManager;
347
348 Log.i(this, "SimCallManager queried, returning: %s", retval);
349
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700350 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700351 }
352
Evan Charlton105d9772014-11-25 14:08:53 -0800353 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800354 * Update the current UserHandle to track when users are switched. This will allow the
355 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
356 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700357 * We cannot simply check the calling user because that would always return the primary user for
358 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800359 *
360 * @param userHandle The {@link UserHandle}, as delivered by
361 * {@link Intent#ACTION_USER_SWITCHED}.
362 */
363 public void setCurrentUserHandle(UserHandle userHandle) {
364 if (userHandle == null) {
365 Log.d(this, "setCurrentUserHandle, userHandle = null");
366 userHandle = Process.myUserHandle();
367 }
368 Log.d(this, "setCurrentUserHandle, %s", userHandle);
369 mCurrentUserHandle = userHandle;
370 }
371
Yorke Lee71734c22015-06-02 14:22:56 -0700372 /**
373 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
374 * otherwise.
375 */
376 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700377 PhoneAccount account = getPhoneAccount(accountHandle);
Santos Cordon638f05c2015-05-19 17:12:33 -0700378 if (account == null) {
379 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700380 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700381 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700382 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700383 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700384 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700385 }
386
Santos Cordon638f05c2015-05-19 17:12:33 -0700387 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700388 account.setIsEnabled(isEnabled);
389 write();
390 fireAccountsChanged();
391 }
Yorke Lee71734c22015-06-02 14:22:56 -0700392 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700393 }
394
Evan Charlton105d9772014-11-25 14:08:53 -0800395 private boolean isVisibleForUser(PhoneAccount account) {
396 if (account == null) {
397 return false;
398 }
399
400 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
401 // all profiles. Only Telephony and SIP accounts should have this capability.
402 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
403 return true;
404 }
405
406 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
407 if (phoneAccountUserHandle == null) {
408 return false;
409 }
410
411 if (mCurrentUserHandle == null) {
412 Log.d(this, "Current user is null; assuming true");
413 return true;
414 }
415
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800416 if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) {
417 return true;
418 }
419
Xiaohui Chen64196862015-10-13 13:59:56 -0700420 // Special check for work profiles, any user could have a profile.
Evan Charlton105d9772014-11-25 14:08:53 -0800421 // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure
422 // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is
423 // fine.
Xiaohui Chen64196862015-10-13 13:59:56 -0700424 return mUserManager.isSameProfileGroup(
425 mCurrentUserHandle.getIdentifier(), phoneAccountUserHandle.getIdentifier());
Evan Charlton105d9772014-11-25 14:08:53 -0800426 }
427
428 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
429 return resolveComponent(phoneAccountHandle.getComponentName(),
430 phoneAccountHandle.getUserHandle());
431 }
432
433 private List<ResolveInfo> resolveComponent(ComponentName componentName,
434 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800435 PackageManager pm = mContext.getPackageManager();
436 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
437 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800438 try {
439 if (userHandle != null) {
440 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
441 } else {
442 return pm.queryIntentServices(intent, 0);
443 }
444 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700445 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800446 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800447 }
mike dooley10a58312014-11-06 13:46:19 -0800448 }
449
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700450 /**
451 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700452 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700453 *
454 * @return The list of {@link PhoneAccountHandle}s.
455 */
Ihab Awad293edf22014-07-24 17:52:29 -0700456 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700457 return getPhoneAccountHandles(0, null, null, false);
Ihab Awad293edf22014-07-24 17:52:29 -0700458 }
459
460 public List<PhoneAccount> getAllPhoneAccounts() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700461 return getPhoneAccounts(0, null, null, false);
Santos Cordonafe59e52014-08-22 16:48:43 -0700462 }
463
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700464 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700465 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700466 * specified URI scheme.
467 *
468 * @param uriScheme The URI scheme.
469 * @return The phone account handles.
470 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700471 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
472 String uriScheme, boolean includeDisabledAccounts) {
473 return getPhoneAccountHandles(
Bryce Leea0bb7052015-10-16 13:31:40 -0700474 PhoneAccount.CAPABILITY_CALL_PROVIDER,
475 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/,
476 uriScheme, null, includeDisabledAccounts);
Santos Cordon6a212642015-05-08 16:35:23 -0700477 }
478
479 /**
480 * Retrieves a list of all the SIM-based phone accounts.
481 */
482 public List<PhoneAccountHandle> getSimPhoneAccounts() {
483 return getPhoneAccountHandles(
484 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Santos Cordonea5cb932015-05-07 16:28:38 -0700485 null, null, false);
Tyler Gunn84253572014-09-02 14:50:05 -0700486 }
487
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700488 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700489 * Retrieves a list of all phone accounts registered by a specified package.
490 *
491 * @param packageName The name of the package that registered the phone accounts.
492 * @return The phone account handles.
493 */
494 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700495 return getPhoneAccountHandles(0, null, packageName, false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700496 }
497
Ihab Awad104f8062014-07-17 11:29:35 -0700498 // TODO: Should we implement an artificial limit for # of accounts associated with a single
499 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700500 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700501 // Enforce the requirement that a connection service for a phone account has the correct
502 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700503 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700504 Log.w(this,
505 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700506 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700507 throw new SecurityException("PhoneAccount connection service requires "
508 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700509 }
510
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700511 addOrReplacePhoneAccount(account);
512 }
513
514 /**
515 * Adds a {@code PhoneAccount}, replacing an existing one if found.
516 *
517 * @param account The {@code PhoneAccount} to add or replace.
518 */
519 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800520 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
521 account.getAccountHandle(), account);
522
Santos Cordonea5cb932015-05-07 16:28:38 -0700523 // Start _enabled_ property as false.
524 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
525 // source app provides or else an third party app could enable itself.
526 boolean isEnabled = false;
527
Santos Cordon6a212642015-05-08 16:35:23 -0700528 PhoneAccount oldAccount = getPhoneAccount(account.getAccountHandle());
529 if (oldAccount != null) {
530 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700531 isEnabled = oldAccount.isEnabled();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700532 Log.i(this, getAccountDiffString(account, oldAccount));
533 } else {
534 Log.i(this, "New phone account registered: " + account);
Santos Cordon176ae282014-07-14 02:02:14 -0700535 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700536
Santos Cordon6a212642015-05-08 16:35:23 -0700537 mState.accounts.add(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700538 // Reset enabled state to whatever the value was if the account was already registered,
539 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled.
540 account.setIsEnabled(
541 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION));
Santos Cordon176ae282014-07-14 02:02:14 -0700542
Ihab Awad293edf22014-07-24 17:52:29 -0700543 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700544 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700545 }
546
Evan Charlton89176372014-07-19 18:23:09 -0700547 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700548 PhoneAccount account = getPhoneAccount(accountHandle);
549 if (account != null) {
550 if (mState.accounts.remove(account)) {
551 write();
552 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700553 }
554 }
Santos Cordon176ae282014-07-14 02:02:14 -0700555 }
556
Tyler Gunnd900ce62014-08-13 11:40:59 -0700557 /**
558 * Un-registers all phone accounts associated with a specified package.
559 *
560 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800561 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700562 */
Evan Charlton105d9772014-11-25 14:08:53 -0800563 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700564 boolean accountsRemoved = false;
565 Iterator<PhoneAccount> it = mState.accounts.iterator();
566 while (it.hasNext()) {
567 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800568 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
569 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
570 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700571 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530572 mState.accounts.remove(phoneAccount);
Tyler Gunnd900ce62014-08-13 11:40:59 -0700573 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700574 }
575 }
576
Tyler Gunnd900ce62014-08-13 11:40:59 -0700577 if (accountsRemoved) {
578 write();
579 fireAccountsChanged();
580 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700581 }
582
Nancy Chen140004a2014-10-15 15:48:38 -0700583 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700584 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Yorke Leeb1957232015-06-30 10:09:54 -0700585 return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number);
Nancy Chen140004a2014-10-15 15:48:38 -0700586 }
587
Ihab Awadb78b2762014-07-25 15:16:23 -0700588 public void addListener(Listener l) {
589 mListeners.add(l);
590 }
591
592 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700593 if (l != null) {
594 mListeners.remove(l);
595 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700596 }
597
598 private void fireAccountsChanged() {
599 for (Listener l : mListeners) {
600 l.onAccountsChanged(this);
601 }
602 }
603
604 private void fireDefaultOutgoingChanged() {
605 for (Listener l : mListeners) {
606 l.onDefaultOutgoingChanged(this);
607 }
608 }
609
610 private void fireSimCallManagerChanged() {
611 for (Listener l : mListeners) {
612 l.onSimCallManagerChanged(this);
613 }
Santos Cordon176ae282014-07-14 02:02:14 -0700614 }
615
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700616 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
617 if (account1 == null || account2 == null) {
618 return "Diff: " + account1 + ", " + account2;
619 }
620
621 StringBuffer sb = new StringBuffer();
622 sb.append("[").append(account1.getAccountHandle());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700623 appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()),
624 Log.piiHandle(account2.getAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700625 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
626 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
627 appendDiff(sb, "icon", account1.getIcon(), account2.getIcon());
628 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
629 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700630 appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()),
631 Log.piiHandle(account2.getSubscriptionAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700632 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
633 account2.getSupportedUriSchemes());
634 sb.append("]");
635 return sb.toString();
636 }
637
638 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
639 if (!Objects.equals(obj1, obj2)) {
640 sb.append("(")
641 .append(attrName)
642 .append(": ")
643 .append(obj1)
644 .append(" -> ")
645 .append(obj2)
646 .append(")");
647 }
648 }
649
Tyler Gunncb59b672014-08-20 09:02:11 -0700650 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700651 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700652 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700653 *
654 * @param phoneAccountHandle The phone account to check.
655 * @return {@code True} if the phone account has permission.
656 */
Santos Cordon6a212642015-05-08 16:35:23 -0700657 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800658 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
659 if (resolveInfos.isEmpty()) {
660 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700661 return false;
662 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800663 for (ResolveInfo resolveInfo : resolveInfos) {
664 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700665 if (serviceInfo == null) {
666 return false;
667 }
668
669 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
670 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
671 serviceInfo.permission)) {
672 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
673 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
674 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800675 return false;
676 }
677 }
678 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700679 }
680
Santos Cordon6a212642015-05-08 16:35:23 -0700681 //
682 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
683 //
Ihab Awad293edf22014-07-24 17:52:29 -0700684
Santos Cordonafe59e52014-08-22 16:48:43 -0700685 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700686 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700687 *
Santos Cordon6a212642015-05-08 16:35:23 -0700688 * @param handle
689 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700690 */
Santos Cordon6a212642015-05-08 16:35:23 -0700691 PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
692 for (PhoneAccount m : mState.accounts) {
693 if (Objects.equals(handle, m.getAccountHandle())) {
694 return m;
695 }
696 }
697 return null;
698 }
699
700 /**
701 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
702 * account before returning it. The current user is the active user on the actual android
703 * device.
704 */
705 public PhoneAccount getPhoneAccountCheckCallingUser(PhoneAccountHandle handle) {
706 PhoneAccount account = getPhoneAccount(handle);
707 if (account != null && isVisibleForUser(account)) {
708 return account;
709 }
710 return null;
711 }
712
Bryce Leea0bb7052015-10-16 13:31:40 -0700713 private List<PhoneAccountHandle> getPhoneAccountHandles(
714 int capabilities,
715 String uriScheme,
716 String packageName,
717 boolean includeDisabledAccounts) {
718 return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme,
719 packageName, includeDisabledAccounts);
720 }
721
Santos Cordon6a212642015-05-08 16:35:23 -0700722 /**
723 * Returns a list of phone account handles with the specified capabilities, uri scheme,
724 * and package name.
725 */
726 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700727 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700728 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700729 String uriScheme,
730 String packageName,
731 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700732 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700733
734 for (PhoneAccount account : getPhoneAccounts(
Bryce Leea0bb7052015-10-16 13:31:40 -0700735 capabilities, excludedCapabilities, uriScheme, packageName,
736 includeDisabledAccounts)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700737 handles.add(account.getAccountHandle());
738 }
739 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700740 }
741
Bryce Leea0bb7052015-10-16 13:31:40 -0700742 private List<PhoneAccount> getPhoneAccounts(
743 int capabilities,
744 String uriScheme,
745 String packageName,
746 boolean includeDisabledAccounts) {
747 return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName,
748 includeDisabledAccounts);
749 }
750
Tyler Gunn84253572014-09-02 14:50:05 -0700751 /**
752 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700753 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700754 *
Santos Cordon6a212642015-05-08 16:35:23 -0700755 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
Bryce Leea0bb7052015-10-16 13:31:40 -0700756 * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have.
757 * Ignored if 0.
Santos Cordon6a212642015-05-08 16:35:23 -0700758 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700759 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700760 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700761 */
Santos Cordon6a212642015-05-08 16:35:23 -0700762 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700763 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700764 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700765 String uriScheme,
766 String packageName,
767 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700768 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700769 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700770 if (!(m.isEnabled() || includeDisabledAccounts)) {
771 // Do not include disabled accounts.
772 continue;
773 }
774
Bryce Leea0bb7052015-10-16 13:31:40 -0700775 if ((m.getCapabilities() & excludedCapabilities) != 0) {
776 // If an excluded capability is present, skip.
777 continue;
778 }
779
Santos Cordon6a212642015-05-08 16:35:23 -0700780 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800781 // Account doesn't have the right capabilities; skip this one.
782 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700783 }
Evan Charlton105d9772014-11-25 14:08:53 -0800784 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
785 // Account doesn't support this URI scheme; skip this one.
786 continue;
787 }
Santos Cordon6a212642015-05-08 16:35:23 -0700788 PhoneAccountHandle handle = m.getAccountHandle();
789
790 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800791 // This component cannot be resolved anymore; skip this one.
792 continue;
793 }
Santos Cordon6a212642015-05-08 16:35:23 -0700794 if (packageName != null &&
795 !packageName.equals(handle.getComponentName().getPackageName())) {
796 // Not the right package name; skip this one.
797 continue;
798 }
Evan Charlton105d9772014-11-25 14:08:53 -0800799 if (!isVisibleForUser(m)) {
800 // Account is not visible for the current user; skip this one.
801 continue;
802 }
Santos Cordon6a212642015-05-08 16:35:23 -0700803 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700804 }
Santos Cordon6a212642015-05-08 16:35:23 -0700805 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700806 }
807
Santos Cordon6a212642015-05-08 16:35:23 -0700808 //
809 // State Implementation for PhoneAccountRegistrar
810 //
811
Ihab Awad293edf22014-07-24 17:52:29 -0700812 /**
813 * The state of this {@code PhoneAccountRegistrar}.
814 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700815 @VisibleForTesting
816 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700817 /**
818 * The account selected by the user to be employed by default for making outgoing calls.
819 * If the user has not made such a selection, then this is null.
820 */
821 public PhoneAccountHandle defaultOutgoing = null;
822
823 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700824 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700825 */
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530826 public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700827
828 /**
829 * The version number of the State data.
830 */
831 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700832 }
833
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700834 /**
835 * Dumps the state of the {@link CallsManager}.
836 *
837 * @param pw The {@code IndentingPrintWriter} to write the state to.
838 */
839 public void dump(IndentingPrintWriter pw) {
840 if (mState != null) {
841 pw.println("xmlVersion: " + mState.versionNumber);
842 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
843 mState.defaultOutgoing));
Santos Cordon59c21a72015-06-11 10:11:21 -0700844 pw.println("simCallManager: " + getSimCallManager());
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700845 pw.println("phoneAccounts:");
846 pw.increaseIndent();
847 for (PhoneAccount phoneAccount : mState.accounts) {
848 pw.println(phoneAccount);
849 }
850 pw.decreaseIndent();
851 }
852 }
853
Ihab Awad293edf22014-07-24 17:52:29 -0700854 ////////////////////////////////////////////////////////////////////////////////////////////////
855 //
856 // State management
857 //
858
859 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700860 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700861 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700862 os = mAtomicFile.startWrite();
863 boolean success = false;
864 try {
865 XmlSerializer serializer = new FastXmlSerializer();
866 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800867 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700868 serializer.flush();
869 success = true;
870 } finally {
871 if (success) {
872 mAtomicFile.finishWrite(os);
873 } else {
874 mAtomicFile.failWrite(os);
875 }
876 }
877 } catch (IOException e) {
878 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700879 }
880 }
881
Ihab Awadb78b2762014-07-25 15:16:23 -0700882 private void read() {
883 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700884 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700885 is = mAtomicFile.openRead();
886 } catch (FileNotFoundException ex) {
887 return;
888 }
889
Tyler Gunn84253572014-09-02 14:50:05 -0700890 boolean versionChanged = false;
891
Ihab Awadb78b2762014-07-25 15:16:23 -0700892 XmlPullParser parser;
893 try {
894 parser = Xml.newPullParser();
895 parser.setInput(new BufferedInputStream(is), null);
896 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700897 mState = readFromXml(parser, mContext);
898 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
899
Ihab Awadb78b2762014-07-25 15:16:23 -0700900 } catch (IOException | XmlPullParserException e) {
901 Log.e(this, e, "Reading state from XML file");
902 mState = new State();
903 } finally {
904 try {
905 is.close();
906 } catch (IOException e) {
907 Log.e(this, e, "Closing InputStream");
908 }
Ihab Awad104f8062014-07-17 11:29:35 -0700909 }
Tyler Gunn84253572014-09-02 14:50:05 -0700910
Evan Charlton105d9772014-11-25 14:08:53 -0800911 // Verify all of the UserHandles.
912 List<PhoneAccount> badAccounts = new ArrayList<>();
913 for (PhoneAccount phoneAccount : mState.accounts) {
914 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
915 if (userHandle == null) {
916 Log.w(this, "Missing UserHandle for %s", phoneAccount);
917 badAccounts.add(phoneAccount);
918 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
919 Log.w(this, "User does not exist for %s", phoneAccount);
920 badAccounts.add(phoneAccount);
921 }
922 }
923 mState.accounts.removeAll(badAccounts);
924
Tyler Gunn84253572014-09-02 14:50:05 -0700925 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -0800926 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -0700927 write();
928 }
Santos Cordon176ae282014-07-14 02:02:14 -0700929 }
930
Evan Charlton105d9772014-11-25 14:08:53 -0800931 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700932 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -0800933 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -0700934 }
Ihab Awad104f8062014-07-17 11:29:35 -0700935
Tyler Gunn84253572014-09-02 14:50:05 -0700936 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700937 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700938 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700939 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700940 }
941
Ihab Awad293edf22014-07-24 17:52:29 -0700942 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700943 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700944 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700945 //
946
Ihab Awadb78b2762014-07-25 15:16:23 -0700947 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700948 public abstract static class XmlSerialization<T> {
Tyler Gunn369c8742015-10-07 15:39:28 -0700949 private static final String TAG_VALUE = "value";
950 private static final String ATTRIBUTE_LENGTH = "length";
951 private static final String ATTRIBUTE_KEY = "key";
952 private static final String ATTRIBUTE_VALUE_TYPE = "type";
953 private static final String VALUE_TYPE_STRING = "string";
954 private static final String VALUE_TYPE_INTEGER = "integer";
955 private static final String VALUE_TYPE_BOOLEAN = "boolean";
Tyler Gunn84253572014-09-02 14:50:05 -0700956
Ihab Awadb78b2762014-07-25 15:16:23 -0700957 /**
958 * Write the supplied object to XML
959 */
Evan Charlton105d9772014-11-25 14:08:53 -0800960 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700961 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700962
963 /**
964 * Read from the supplied XML into a new object, returning null in case of an
965 * unrecoverable schema mismatch or other data error. 'parser' must be already
966 * positioned at the first tag that is expected to have been emitted by this
967 * object's writeToXml(). This object tries to fail early without modifying
968 * 'parser' if it does not recognize the data it sees.
969 */
Tyler Gunn84253572014-09-02 14:50:05 -0700970 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700971 throws IOException, XmlPullParserException;
972
Ihab Awadd9f54382014-10-24 11:44:47 -0700973 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700974 throws IOException {
975 if (value != null) {
976 serializer.startTag(null, tagName);
977 serializer.text(Objects.toString(value));
978 serializer.endTag(null, tagName);
979 }
980 }
Tyler Gunn84253572014-09-02 14:50:05 -0700981
982 /**
983 * Serializes a string array.
984 *
985 * @param tagName The tag name for the string array.
986 * @param values The string values to serialize.
987 * @param serializer The serializer.
988 * @throws IOException
989 */
990 protected void writeStringList(String tagName, List<String> values,
991 XmlSerializer serializer)
992 throws IOException {
993
994 serializer.startTag(null, tagName);
995 if (values != null) {
Tyler Gunn369c8742015-10-07 15:39:28 -0700996 serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size()));
Tyler Gunn84253572014-09-02 14:50:05 -0700997 for (String toSerialize : values) {
Tyler Gunn369c8742015-10-07 15:39:28 -0700998 serializer.startTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -0700999 if (toSerialize != null ){
1000 serializer.text(toSerialize);
1001 }
Tyler Gunn369c8742015-10-07 15:39:28 -07001002 serializer.endTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001003 }
1004 } else {
Tyler Gunn369c8742015-10-07 15:39:28 -07001005 serializer.attribute(null, ATTRIBUTE_LENGTH, "0");
1006 }
1007 serializer.endTag(null, tagName);
1008 }
1009
1010 protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer)
1011 throws IOException {
1012
1013 serializer.startTag(null, tagName);
1014 if (values != null) {
1015 for (String key : values.keySet()) {
1016 Object value = values.get(key);
1017
1018 if (value == null) {
1019 continue;
1020 }
1021
1022 String valueType;
1023 if (value instanceof String) {
1024 valueType = VALUE_TYPE_STRING;
1025 } else if (value instanceof Integer) {
1026 valueType = VALUE_TYPE_INTEGER;
1027 } else if (value instanceof Boolean) {
1028 valueType = VALUE_TYPE_BOOLEAN;
1029 } else {
1030 Log.w(this,
1031 "PhoneAccounts support only string, integer and boolean extras TY.");
1032 continue;
1033 }
1034
1035 serializer.startTag(null, TAG_VALUE);
1036 serializer.attribute(null, ATTRIBUTE_KEY, key);
1037 serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType);
1038 serializer.text(Objects.toString(value));
1039 serializer.endTag(null, TAG_VALUE);
1040 }
Tyler Gunn84253572014-09-02 14:50:05 -07001041 }
1042 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -07001043 }
Tyler Gunn84253572014-09-02 14:50:05 -07001044
Santos Cordon9c30c282015-05-13 16:28:27 -07001045 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -07001046 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001047 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001048 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001049 value.writeToStream(stream);
1050 byte[] iconByteArray = stream.toByteArray();
1051 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001052
1053 serializer.startTag(null, tagName);
1054 serializer.text(text);
1055 serializer.endTag(null, tagName);
1056 }
Tyler Gunn84253572014-09-02 14:50:05 -07001057 }
1058
Evan Charlton105d9772014-11-25 14:08:53 -08001059 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1060 throws IOException {
1061 serializer.startTag(null, tagName);
1062 serializer.text(Long.valueOf(value).toString());
1063 serializer.endTag(null, tagName);
1064 }
1065
Tyler Gunn84253572014-09-02 14:50:05 -07001066 /**
1067 * Reads a string array from the XML parser.
1068 *
1069 * @param parser The XML parser.
1070 * @return String array containing the parsed values.
1071 * @throws IOException Exception related to IO.
1072 * @throws XmlPullParserException Exception related to parsing.
1073 */
1074 protected List<String> readStringList(XmlPullParser parser)
1075 throws IOException, XmlPullParserException {
1076
Tyler Gunn369c8742015-10-07 15:39:28 -07001077 int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH));
Tyler Gunn84253572014-09-02 14:50:05 -07001078 List<String> arrayEntries = new ArrayList<String>(length);
1079 String value = null;
1080
1081 if (length == 0) {
1082 return arrayEntries;
1083 }
1084
1085 int outerDepth = parser.getDepth();
1086 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001087 if (parser.getName().equals(TAG_VALUE)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001088 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001089 value = parser.getText();
1090 arrayEntries.add(value);
1091 }
1092 }
1093
1094 return arrayEntries;
1095 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001096
Tyler Gunn369c8742015-10-07 15:39:28 -07001097 /**
1098 * Reads a bundle from the XML parser.
1099 *
1100 * @param parser The XML parser.
1101 * @return Bundle containing the parsed values.
1102 * @throws IOException Exception related to IO.
1103 * @throws XmlPullParserException Exception related to parsing.
1104 */
1105 protected Bundle readBundle(XmlPullParser parser)
1106 throws IOException, XmlPullParserException {
1107
1108 Bundle bundle = null;
1109 int outerDepth = parser.getDepth();
1110 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1111 if (parser.getName().equals(TAG_VALUE)) {
1112 String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE);
1113 String key = parser.getAttributeValue(null, ATTRIBUTE_KEY);
1114 parser.next();
1115 String value = parser.getText();
1116
1117 if (bundle == null) {
1118 bundle = new Bundle();
1119 }
1120
1121 // Do not write null values to the bundle.
1122 if (value == null) {
1123 continue;
1124 }
1125
1126 if (VALUE_TYPE_STRING.equals(valueType)) {
1127 bundle.putString(key, value);
1128 } else if (VALUE_TYPE_INTEGER.equals(valueType)) {
1129 try {
1130 int intValue = Integer.parseInt(value);
1131 bundle.putInt(key, intValue);
1132 } catch (NumberFormatException nfe) {
1133 Log.w(this, "Invalid integer PhoneAccount extra.");
1134 }
1135 } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) {
1136 boolean boolValue = Boolean.parseBoolean(value);
1137 bundle.putBoolean(key, boolValue);
1138 } else {
1139 Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle.");
1140 }
1141 }
1142 }
1143 return bundle;
1144 }
1145
Santos Cordon9c30c282015-05-13 16:28:27 -07001146 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001147 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1148 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1149 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001150
1151 protected Icon readIcon(XmlPullParser parser) throws IOException {
1152 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1153 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1154 return Icon.createFromStream(stream);
1155 }
Ihab Awad104f8062014-07-17 11:29:35 -07001156 }
1157
Ihab Awadb78b2762014-07-25 15:16:23 -07001158 @VisibleForTesting
1159 public static final XmlSerialization<State> sStateXml =
1160 new XmlSerialization<State>() {
1161 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001162 private static final String DEFAULT_OUTGOING = "default_outgoing";
1163 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001164 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001165
1166 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001167 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001168 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001169 if (o != null) {
1170 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001171 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001172
Ihab Awad26923222014-07-30 10:54:35 -07001173 if (o.defaultOutgoing != null) {
1174 serializer.startTag(null, DEFAULT_OUTGOING);
Evan Charlton105d9772014-11-25 14:08:53 -08001175 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001176 serializer.endTag(null, DEFAULT_OUTGOING);
1177 }
1178
Ihab Awad26923222014-07-30 10:54:35 -07001179 serializer.startTag(null, ACCOUNTS);
1180 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001181 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001182 }
1183 serializer.endTag(null, ACCOUNTS);
1184
1185 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001186 }
Ihab Awad104f8062014-07-17 11:29:35 -07001187 }
1188
1189 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001190 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001191 throws IOException, XmlPullParserException {
1192 if (parser.getName().equals(CLASS_STATE)) {
1193 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001194
1195 String rawVersion = parser.getAttributeValue(null, VERSION);
1196 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
1197 Integer.parseInt(rawVersion);
1198
Ihab Awadb78b2762014-07-25 15:16:23 -07001199 int outerDepth = parser.getDepth();
1200 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1201 if (parser.getName().equals(DEFAULT_OUTGOING)) {
1202 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001203 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
1204 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001205 } else if (parser.getName().equals(ACCOUNTS)) {
1206 int accountsDepth = parser.getDepth();
1207 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001208 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1209 s.versionNumber, context);
1210
1211 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001212 s.accounts.add(account);
1213 }
1214 }
Ihab Awad104f8062014-07-17 11:29:35 -07001215 }
1216 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001217 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001218 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001219 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001220 }
1221 };
1222
Ihab Awadb78b2762014-07-25 15:16:23 -07001223 @VisibleForTesting
1224 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1225 new XmlSerialization<PhoneAccount>() {
1226 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1227 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001228 private static final String ADDRESS = "handle";
1229 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001230 private static final String CAPABILITIES = "capabilities";
1231 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001232 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1233 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001234 private static final String ICON_TINT = "icon_tint";
1235 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001236 private static final String LABEL = "label";
1237 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001238 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001239 private static final String ICON = "icon";
Tyler Gunn369c8742015-10-07 15:39:28 -07001240 private static final String EXTRAS = "extras";
Santos Cordonea5cb932015-05-07 16:28:38 -07001241 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001242
1243 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001244 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001245 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001246 if (o != null) {
1247 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001248
Ihab Awad26923222014-07-30 10:54:35 -07001249 if (o.getAccountHandle() != null) {
1250 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001251 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001252 serializer.endTag(null, ACCOUNT_HANDLE);
1253 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001254
Ihab Awadd9f54382014-10-24 11:44:47 -07001255 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1256 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1257 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001258 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001259 writeTextIfNonNull(HIGHLIGHT_COLOR,
1260 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001261 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1262 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001263 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Tyler Gunn369c8742015-10-07 15:39:28 -07001264 writeBundle(EXTRAS, o.getExtras(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001265 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001266
Ihab Awad26923222014-07-30 10:54:35 -07001267 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1268 }
Ihab Awad104f8062014-07-17 11:29:35 -07001269 }
1270
Tyler Gunn84253572014-09-02 14:50:05 -07001271 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001272 throws IOException, XmlPullParserException {
1273 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1274 int outerDepth = parser.getDepth();
1275 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001276 Uri address = null;
1277 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001278 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001279 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001280 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001281 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001282 int iconTint = PhoneAccount.NO_ICON_TINT;
1283 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001284 String label = null;
1285 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001286 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001287 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001288 boolean enabled = false;
Tyler Gunn369c8742015-10-07 15:39:28 -07001289 Bundle extras = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001290
1291 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1292 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1293 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001294 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1295 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001296 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001297 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001298 address = Uri.parse(parser.getText());
1299 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001300 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001301 String nextText = parser.getText();
1302 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001303 } else if (parser.getName().equals(CAPABILITIES)) {
1304 parser.next();
1305 capabilities = Integer.parseInt(parser.getText());
1306 } else if (parser.getName().equals(ICON_RES_ID)) {
1307 parser.next();
1308 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001309 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1310 parser.next();
1311 iconPackageName = parser.getText();
1312 } else if (parser.getName().equals(ICON_BITMAP)) {
1313 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001314 iconBitmap = readBitmap(parser);
1315 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001316 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001317 iconTint = Integer.parseInt(parser.getText());
1318 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1319 parser.next();
1320 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001321 } else if (parser.getName().equals(LABEL)) {
1322 parser.next();
1323 label = parser.getText();
1324 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1325 parser.next();
1326 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001327 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1328 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001329 } else if (parser.getName().equals(ICON)) {
1330 parser.next();
1331 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001332 } else if (parser.getName().equals(ENABLED)) {
1333 parser.next();
1334 enabled = "true".equalsIgnoreCase(parser.getText());
Tyler Gunn369c8742015-10-07 15:39:28 -07001335 } else if (parser.getName().equals(EXTRAS)) {
1336 parser.next();
1337 extras = readBundle(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -07001338 }
1339 }
Tyler Gunn84253572014-09-02 14:50:05 -07001340
Santos Cordona9eebe42015-06-11 14:07:44 -07001341 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1342 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001343 ComponentName sipComponentName = new ComponentName("com.android.phone",
1344 "com.android.services.telephony.sip.SipConnectionService");
1345
Tyler Gunn84253572014-09-02 14:50:05 -07001346 // Upgrade older phone accounts to specify the supported URI schemes.
1347 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001348 supportedUriSchemes = new ArrayList<>();
1349
1350 // Handle the SIP connection service.
1351 // Check the system settings to see if it also should handle "tel" calls.
1352 if (accountHandle.getComponentName().equals(sipComponentName)) {
1353 boolean useSipForPstn = useSipForPstnCalls(context);
1354 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1355 if (useSipForPstn) {
1356 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1357 }
1358 } else {
1359 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1360 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1361 }
1362 }
1363
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001364 // Upgrade older phone accounts with explicit package name
1365 if (version < 5) {
1366 if (iconBitmap == null) {
1367 iconPackageName = accountHandle.getComponentName().getPackageName();
1368 }
1369 }
1370
Santos Cordona82aed82015-05-26 10:43:56 -07001371 if (version < 6) {
1372 // Always enable all SIP accounts on upgrade to version 6
1373 if (accountHandle.getComponentName().equals(sipComponentName)) {
1374 enabled = true;
1375 }
1376 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001377 if (version < 7) {
1378 // Always enabled all PSTN acocunts on upgrade to version 7
1379 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1380 enabled = true;
1381 }
1382 }
Roshan Pius6f752c82015-08-10 10:42:06 -07001383 if (version < 8) {
1384 // Migrate the SIP account handle ids to use SIP username instead of SIP URI.
1385 if (accountHandle.getComponentName().equals(sipComponentName)) {
1386 Uri accountUri = Uri.parse(accountHandle.getId());
1387 if (accountUri.getScheme() != null &&
1388 accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) {
1389 accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(),
1390 accountUri.getSchemeSpecificPart(),
1391 accountHandle.getUserHandle());
1392 }
1393 }
1394 }
Santos Cordona82aed82015-05-26 10:43:56 -07001395
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001396 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001397 .setAddress(address)
1398 .setSubscriptionAddress(subscriptionAddress)
1399 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001400 .setShortDescription(shortDescription)
1401 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001402 .setHighlightColor(highlightColor)
Tyler Gunn369c8742015-10-07 15:39:28 -07001403 .setExtras(extras)
Santos Cordonea5cb932015-05-07 16:28:38 -07001404 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001405
Santos Cordon9c30c282015-05-13 16:28:27 -07001406 if (icon != null) {
1407 builder.setIcon(icon);
1408 } else if (iconBitmap != null) {
1409 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1410 } else if (!TextUtils.isEmpty(iconPackageName)) {
1411 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1412 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001413 }
1414
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001415 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001416 }
1417 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001418 }
Tyler Gunn84253572014-09-02 14:50:05 -07001419
1420 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001421 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1422 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001423 *
1424 * @param context The context.
1425 * @return {@code True} if SIP should be used for all calls.
1426 */
1427 private boolean useSipForPstnCalls(Context context) {
1428 String option = Settings.System.getString(context.getContentResolver(),
1429 Settings.System.SIP_CALL_OPTIONS);
1430 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1431 return option.equals(Settings.System.SIP_ALWAYS);
1432 }
Ihab Awad104f8062014-07-17 11:29:35 -07001433 };
1434
Ihab Awadb78b2762014-07-25 15:16:23 -07001435 @VisibleForTesting
1436 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1437 new XmlSerialization<PhoneAccountHandle>() {
1438 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001439 private static final String COMPONENT_NAME = "component_name";
1440 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001441 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001442
1443 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001444 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001445 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001446 if (o != null) {
1447 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001448
Ihab Awad26923222014-07-30 10:54:35 -07001449 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001450 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001451 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1452 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001453
Ihab Awadd9f54382014-10-24 11:44:47 -07001454 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001455
Evan Charlton105d9772014-11-25 14:08:53 -08001456 if (o.getUserHandle() != null && context != null) {
1457 UserManager userManager = UserManager.get(context);
1458 writeLong(USER_SERIAL_NUMBER,
1459 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1460 }
1461
Ihab Awad26923222014-07-30 10:54:35 -07001462 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1463 }
Ihab Awad104f8062014-07-17 11:29:35 -07001464 }
1465
1466 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001467 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001468 throws IOException, XmlPullParserException {
1469 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1470 String componentNameString = null;
1471 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001472 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001473 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001474
1475 UserManager userManager = UserManager.get(context);
1476
Ihab Awadb78b2762014-07-25 15:16:23 -07001477 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1478 if (parser.getName().equals(COMPONENT_NAME)) {
1479 parser.next();
1480 componentNameString = parser.getText();
1481 } else if (parser.getName().equals(ID)) {
1482 parser.next();
1483 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001484 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1485 parser.next();
1486 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001487 }
1488 }
Ihab Awad26923222014-07-30 10:54:35 -07001489 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001490 UserHandle userHandle = null;
1491 if (userSerialNumberString != null) {
1492 try {
1493 long serialNumber = Long.parseLong(userSerialNumberString);
1494 userHandle = userManager.getUserForSerialNumber(serialNumber);
1495 } catch (NumberFormatException e) {
1496 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1497 }
1498 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001499 return new PhoneAccountHandle(
1500 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001501 idString,
1502 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001503 }
1504 }
1505 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001506 }
1507 };
Santos Cordon176ae282014-07-14 02:02:14 -07001508}