blob: 7b4af5d2aac54cbe0af3106f1013b41e195b61b5 [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
Tyler Gunncb59b672014-08-20 09:02:11 -070019import android.Manifest;
Evan Charlton105d9772014-11-25 14:08:53 -080020import android.content.ComponentName;
21import android.content.Context;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070022import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
Tyler Gunncb59b672014-08-20 09:02:11 -070025import android.content.pm.ServiceInfo;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000026import android.content.pm.UserInfo;
Ihab Awadd9f54382014-10-24 11:44:47 -070027import android.graphics.Bitmap;
28import android.graphics.BitmapFactory;
Santos Cordon9c30c282015-05-13 16:28:27 -070029import android.graphics.drawable.Icon;
Evan Charlton105d9772014-11-25 14:08:53 -080030import android.net.Uri;
Tyler Gunn369c8742015-10-07 15:39:28 -070031import android.os.Bundle;
Hall Liu3f7f64d2016-01-07 17:40:13 -080032import android.os.AsyncTask;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070033import android.os.PersistableBundle;
Evan Charlton105d9772014-11-25 14:08:53 -080034import android.os.Process;
35import android.os.UserHandle;
36import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070037import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070038import android.telecom.ConnectionService;
Santos Cordon59c21a72015-06-11 10:11:21 -070039import android.telecom.DefaultDialerManager;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070040import android.telecom.PhoneAccount;
41import android.telecom.PhoneAccountHandle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070042import android.telephony.CarrierConfigManager;
Nancy Chen140004a2014-10-15 15:48:38 -070043import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070044import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080045import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070046import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070047import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070048import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070049import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070050
Tyler Gunn91d43cf2014-09-17 12:19:39 -070051// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070052import com.android.internal.annotations.VisibleForTesting;
53import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070054import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070055import com.android.internal.util.XmlUtils;
56
Evan Charltonaf51ceb2014-07-30 11:56:36 -070057import org.xmlpull.v1.XmlPullParser;
58import org.xmlpull.v1.XmlPullParserException;
59import org.xmlpull.v1.XmlSerializer;
60
Ihab Awadb78b2762014-07-25 15:16:23 -070061import java.io.BufferedInputStream;
62import java.io.BufferedOutputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070063import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070064import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070065import java.io.File;
66import java.io.FileNotFoundException;
67import java.io.FileOutputStream;
68import java.io.IOException;
69import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070070import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070071import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070072import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070073import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080074import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070075import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070076import java.util.List;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000077import java.util.Map;
Santos Cordon176ae282014-07-14 02:02:14 -070078import java.util.Objects;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000079import java.util.concurrent.ConcurrentHashMap;
Ihab Awadb78b2762014-07-25 15:16:23 -070080import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070081
82/**
Evan Charlton89176372014-07-19 18:23:09 -070083 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070084 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
85 * implemented in {@link TelecomServiceImpl}, with the notable exception that
86 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
87 * proper authority over the {@code ComponentName}s they are declaring in their
88 * {@code PhoneAccountHandle}s.
89 *
90 *
91 * -- About Users and Phone Accounts --
92 *
Santos Cordon9c30c282015-05-13 16:28:27 -070093 * We store all phone accounts for all users in a single place, which means that there are three
94 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -070095 * 1) The Android User that is currently active on the device.
96 * 2) The user which owns/registers the phone account.
97 * 3) The user running the app that is requesting the phone account information.
98 *
99 * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
Tony Mak240656f2015-12-04 11:36:22 +0000100 * has a work profile running as another user (B2). Each user/profile only have the visibility of
101 * phone accounts owned by them. Lets say, user B (settings) is requesting a list of phone accounts,
102 * and the list only contains phone accounts owned by user B and accounts with
103 * {@link PhoneAccount#CAPABILITY_MULTI_USER}.
Santos Cordon6a212642015-05-08 16:35:23 -0700104 *
105 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
106 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
107 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700108 */
Brad Ebinger53855132015-10-30 10:58:19 -0700109public class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700110
Yorke Lee5e8836a2014-08-22 15:25:18 -0700111 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
112 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
113
Ihab Awadb78b2762014-07-25 15:16:23 -0700114 public abstract static class Listener {
115 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
116 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
117 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
118 }
119
120 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700121 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000122 public static final int EXPECTED_STATE_VERSION = 9;
Tyler Gunn84253572014-09-02 14:50:05 -0700123
124 /** Keep in sync with the same in SipSettings.java */
125 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700126
127 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
128 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700129 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800130 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800131 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700132 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800133 private UserHandle mCurrentUserHandle;
Hall Liu3f7f64d2016-01-07 17:40:13 -0800134 private interface PhoneAccountRegistrarWriteLock {}
135 private final PhoneAccountRegistrarWriteLock mWriteLock =
136 new PhoneAccountRegistrarWriteLock() {};
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) {
Tony Mak240656f2015-12-04 11:36:22 +0000172 PhoneAccount account = getPhoneAccountUnchecked(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 */
Tony Mak240656f2015-12-04 11:36:22 +0000190 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme,
191 UserHandle userHandle) {
192 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700193
Yorke Lee5e8836a2014-08-22 15:25:18 -0700194 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700195 // If there is a default PhoneAccount, ensure it supports calls to handles with the
196 // specified uriScheme.
Tony Mak240656f2015-12-04 11:36:22 +0000197 final PhoneAccount userSelectedAccount = getPhoneAccountUnchecked(userSelected);
Santos Cordon6a212642015-05-08 16:35:23 -0700198 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700199 return userSelected;
200 }
Ihab Awad293edf22014-07-24 17:52:29 -0700201 }
202
Tony Mak240656f2015-12-04 11:36:22 +0000203 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false,
204 userHandle);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700205 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700206 case 0:
207 // There are no accounts, so there can be no default
208 return null;
209 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800210 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700211 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700212 default:
213 // There are multiple accounts with no selected default
214 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700215 }
Ihab Awad104f8062014-07-17 11:29:35 -0700216 }
Santos Cordon176ae282014-07-14 02:02:14 -0700217
Tony Mak240656f2015-12-04 11:36:22 +0000218 public PhoneAccountHandle getOutgoingPhoneAccountForSchemeOfCurrentUser(String uriScheme) {
219 return getOutgoingPhoneAccountForScheme(uriScheme, mCurrentUserHandle);
220 }
221
Evan Charlton105d9772014-11-25 14:08:53 -0800222 /**
223 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
224 * if it was set by another user).
225 */
Hall Liuecda5542015-12-04 11:31:31 -0800226 @VisibleForTesting
227 public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount(UserHandle userHandle) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000228 if (userHandle == null) {
229 return null;
230 }
231 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
232 .get(userHandle);
233 if (defaultPhoneAccountHandle == null) {
234 return null;
235 }
236 // Make sure the account is still registered and owned by the user.
237 PhoneAccount account = getPhoneAccount(defaultPhoneAccountHandle.phoneAccountHandle,
238 userHandle);
Hall Liuecda5542015-12-04 11:31:31 -0800239
Santos Cordon6a212642015-05-08 16:35:23 -0700240 if (account != null) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000241 return defaultPhoneAccountHandle.phoneAccountHandle;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700242 }
243 return null;
244 }
245
Santos Cordon6a212642015-05-08 16:35:23 -0700246 /**
247 * Sets the phone account with which to place all calls by default. Set by the user
248 * within phone settings.
249 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000250 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle,
251 UserHandle userHandle) {
252 if (userHandle == null) {
253 return;
254 }
Evan Charlton89176372014-07-19 18:23:09 -0700255 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700256 // Asking to clear the default outgoing is a valid request
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000257 mState.defaultOutgoingAccountHandles.remove(userHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700258 } else {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000259 PhoneAccount account = getPhoneAccount(accountHandle, userHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700260 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700261 Log.w(this, "Trying to set nonexistent default outgoing %s",
262 accountHandle);
263 return;
264 }
265
Santos Cordon6a212642015-05-08 16:35:23 -0700266 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700267 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700268 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700269 return;
270 }
271
Santos Cordon6a212642015-05-08 16:35:23 -0700272 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700273 // If the account selected is a SIM account, propagate down to the subscription
274 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700275 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800276 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700277 }
278
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000279 mState.defaultOutgoingAccountHandles
280 .put(userHandle, new DefaultPhoneAccountHandle(userHandle, accountHandle));
Santos Cordon176ae282014-07-14 02:02:14 -0700281 }
282
Ihab Awad293edf22014-07-24 17:52:29 -0700283 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700284 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700285 }
286
Nancy Chen668dee02014-11-19 15:31:31 -0800287 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak240656f2015-12-04 11:36:22 +0000288 return getSubscriptionIdForPhoneAccount(accountHandle) == SubscriptionManager
289 .getDefaultSmsSubId();
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700290 }
291
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700292 public ComponentName getSystemSimCallManagerComponent() {
293 String defaultSimCallManager = null;
294 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
295 Context.CARRIER_CONFIG_SERVICE);
296 PersistableBundle configBundle = configManager.getConfig();
297 if (configBundle != null) {
298 defaultSimCallManager = configBundle.getString(
299 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
300 }
301 return TextUtils.isEmpty(defaultSimCallManager)
302 ? null : ComponentName.unflattenFromString(defaultSimCallManager);
303 }
304
Tony Mak240656f2015-12-04 11:36:22 +0000305 public PhoneAccountHandle getSimCallManagerOfCurrentUser() {
306 return getSimCallManager(mCurrentUserHandle);
307 }
308
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700309 /**
310 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
311 * Manager. SIM Call Manager returned corresponds to the following priority order:
312 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
313 * default dialer, then that one is returned.
314 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
315 * carrier configuration's default, then that one is returned.
316 * 3. Otherwise, we return null.
317 */
Tony Mak240656f2015-12-04 11:36:22 +0000318 public PhoneAccountHandle getSimCallManager(UserHandle userHandle) {
Santos Cordon59c21a72015-06-11 10:11:21 -0700319 // Get the default dialer in case it has a connection manager associated with it.
Tony Mak240656f2015-12-04 11:36:22 +0000320 String dialerPackage = DefaultDialerManager
321 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700322
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700323 // Check carrier config.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700324 ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800325
Santos Cordon59c21a72015-06-11 10:11:21 -0700326 PhoneAccountHandle dialerSimCallManager = null;
327 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800328
Santos Cordon59c21a72015-06-11 10:11:21 -0700329 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
330 // loop through and look for any connection manager in the same package.
331 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
332 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
Tony Mak240656f2015-12-04 11:36:22 +0000333 true /* includeDisabledAccounts */, userHandle);
Santos Cordon59c21a72015-06-11 10:11:21 -0700334 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
335 ComponentName component = accountHandle.getComponentName();
336
337 // Store the system connection manager if found
338 if (systemSimCallManager == null
339 && Objects.equals(component, systemSimCallManagerComponent)
340 && !resolveComponent(accountHandle).isEmpty()) {
341 systemSimCallManager = accountHandle;
342
343 // Store the dialer connection manager if found
344 } else if (dialerSimCallManager == null
345 && Objects.equals(component.getPackageName(), dialerPackage)
346 && !resolveComponent(accountHandle).isEmpty()) {
347 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700348 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700349 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700350 }
351
Santos Cordon59c21a72015-06-11 10:11:21 -0700352 PhoneAccountHandle retval = dialerSimCallManager != null ?
353 dialerSimCallManager : systemSimCallManager;
354
355 Log.i(this, "SimCallManager queried, returning: %s", retval);
356
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700357 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700358 }
359
Evan Charlton105d9772014-11-25 14:08:53 -0800360 /**
Tony Mak240656f2015-12-04 11:36:22 +0000361 * If it is a outgoing call, sim call manager of call-initiating user is returned.
362 * Otherwise, we return the sim call manager of the user associated with the
363 * target phone account.
364 * @return phone account handle of sim call manager based on the ongoing call.
365 */
366 public PhoneAccountHandle getSimCallManagerFromCall(Call call) {
367 if (call == null) {
368 return null;
369 }
370 UserHandle userHandle = call.getInitiatingUser();
371 if (userHandle == null) {
372 userHandle = call.getTargetPhoneAccount().getUserHandle();
373 }
374 return getSimCallManager(userHandle);
375 }
376
377 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800378 * Update the current UserHandle to track when users are switched. This will allow the
379 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
380 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700381 * We cannot simply check the calling user because that would always return the primary user for
382 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800383 *
384 * @param userHandle The {@link UserHandle}, as delivered by
385 * {@link Intent#ACTION_USER_SWITCHED}.
386 */
387 public void setCurrentUserHandle(UserHandle userHandle) {
388 if (userHandle == null) {
389 Log.d(this, "setCurrentUserHandle, userHandle = null");
390 userHandle = Process.myUserHandle();
391 }
392 Log.d(this, "setCurrentUserHandle, %s", userHandle);
393 mCurrentUserHandle = userHandle;
394 }
395
Yorke Lee71734c22015-06-02 14:22:56 -0700396 /**
397 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
398 * otherwise.
399 */
400 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000401 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon638f05c2015-05-19 17:12:33 -0700402 if (account == null) {
403 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700404 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700405 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700406 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700407 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700408 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700409 }
410
Santos Cordon638f05c2015-05-19 17:12:33 -0700411 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700412 account.setIsEnabled(isEnabled);
Tony Mak48367162016-01-15 22:58:18 +0000413 if (!isEnabled) {
414 // If the disabled account is the default, remove it.
415 removeDefaultPhoneAccountHandle(accountHandle);
416 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700417 write();
418 fireAccountsChanged();
419 }
Yorke Lee71734c22015-06-02 14:22:56 -0700420 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700421 }
422
Tony Mak48367162016-01-15 22:58:18 +0000423 private void removeDefaultPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) {
424 Iterator<Map.Entry<UserHandle, DefaultPhoneAccountHandle>> iterator =
425 mState.defaultOutgoingAccountHandles.entrySet().iterator();
426 while (iterator.hasNext()) {
427 Map.Entry<UserHandle, DefaultPhoneAccountHandle> entry = iterator.next();
428 if (phoneAccountHandle.equals(entry.getValue().phoneAccountHandle)) {
429 iterator.remove();
430 }
431 }
432 }
433
Tony Mak240656f2015-12-04 11:36:22 +0000434 private boolean isVisibleForUser(PhoneAccount account, UserHandle userHandle,
435 boolean acrossProfiles) {
Evan Charlton105d9772014-11-25 14:08:53 -0800436 if (account == null) {
437 return false;
438 }
439
Tony Mak240656f2015-12-04 11:36:22 +0000440 if (userHandle == null) {
441 Log.w(this, "userHandle is null in isVisibleForUser");
442 return false;
443 }
444
Evan Charlton105d9772014-11-25 14:08:53 -0800445 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
446 // all profiles. Only Telephony and SIP accounts should have this capability.
447 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
448 return true;
449 }
450
451 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
452 if (phoneAccountUserHandle == null) {
453 return false;
454 }
455
456 if (mCurrentUserHandle == null) {
Tony Mak240656f2015-12-04 11:36:22 +0000457 // In case we need to have emergency phone calls from the lock screen.
Evan Charlton105d9772014-11-25 14:08:53 -0800458 Log.d(this, "Current user is null; assuming true");
459 return true;
460 }
461
Tony Mak240656f2015-12-04 11:36:22 +0000462 if (acrossProfiles) {
463 return UserManager.get(mContext).isSameProfileGroup(userHandle.getIdentifier(),
464 phoneAccountUserHandle.getIdentifier());
465 } else {
466 return phoneAccountUserHandle.equals(userHandle);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800467 }
Evan Charlton105d9772014-11-25 14:08:53 -0800468 }
469
470 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
471 return resolveComponent(phoneAccountHandle.getComponentName(),
Tony Mak240656f2015-12-04 11:36:22 +0000472 phoneAccountHandle.getUserHandle());
Evan Charlton105d9772014-11-25 14:08:53 -0800473 }
474
475 private List<ResolveInfo> resolveComponent(ComponentName componentName,
476 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800477 PackageManager pm = mContext.getPackageManager();
478 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
479 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800480 try {
481 if (userHandle != null) {
482 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
483 } else {
484 return pm.queryIntentServices(intent, 0);
485 }
486 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700487 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800488 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800489 }
mike dooley10a58312014-11-06 13:46:19 -0800490 }
491
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700492 /**
493 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700494 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700495 *
496 * @return The list of {@link PhoneAccountHandle}s.
497 */
Tony Mak240656f2015-12-04 11:36:22 +0000498 public List<PhoneAccountHandle> getAllPhoneAccountHandles(UserHandle userHandle) {
499 return getPhoneAccountHandles(0, null, null, false, userHandle);
Ihab Awad293edf22014-07-24 17:52:29 -0700500 }
501
Tony Mak240656f2015-12-04 11:36:22 +0000502 public List<PhoneAccount> getAllPhoneAccounts(UserHandle userHandle) {
503 return getPhoneAccounts(0, null, null, false, userHandle);
504 }
505
506 public List<PhoneAccount> getAllPhoneAccountsOfCurrentUser() {
507 return getAllPhoneAccounts(mCurrentUserHandle);
Santos Cordonafe59e52014-08-22 16:48:43 -0700508 }
509
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700510 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700511 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700512 * specified URI scheme.
513 *
514 * @param uriScheme The URI scheme.
515 * @return The phone account handles.
516 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700517 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
Tony Mak240656f2015-12-04 11:36:22 +0000518 String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700519 return getPhoneAccountHandles(
Bryce Leea0bb7052015-10-16 13:31:40 -0700520 PhoneAccount.CAPABILITY_CALL_PROVIDER,
521 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/,
Tony Mak240656f2015-12-04 11:36:22 +0000522 uriScheme, null, includeDisabledAccounts, userHandle);
523 }
524
525 public List<PhoneAccountHandle> getCallCapablePhoneAccountsOfCurrentUser(
526 String uriScheme, boolean includeDisabledAccounts) {
527 return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, mCurrentUserHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700528 }
529
530 /**
531 * Retrieves a list of all the SIM-based phone accounts.
532 */
Tony Mak240656f2015-12-04 11:36:22 +0000533 public List<PhoneAccountHandle> getSimPhoneAccounts(UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700534 return getPhoneAccountHandles(
535 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Tony Mak240656f2015-12-04 11:36:22 +0000536 null, null, false, userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700537 }
538
Tony Mak240656f2015-12-04 11:36:22 +0000539 public List<PhoneAccountHandle> getSimPhoneAccountsOfCurrentUser() {
540 return getSimPhoneAccounts(mCurrentUserHandle);
541 }
542
543 /**
544 * Retrieves a list of all phone accounts registered by a specified package.
545 *
546 * @param packageName The name of the package that registered the phone accounts.
547 * @return The phone account handles.
548 */
549 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName,
550 UserHandle userHandle) {
551 return getPhoneAccountHandles(0, null, packageName, false, userHandle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700552 }
553
Ihab Awad104f8062014-07-17 11:29:35 -0700554 // TODO: Should we implement an artificial limit for # of accounts associated with a single
555 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700556 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700557 // Enforce the requirement that a connection service for a phone account has the correct
558 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700559 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700560 Log.w(this,
561 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700562 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700563 throw new SecurityException("PhoneAccount connection service requires "
564 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700565 }
566
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700567 addOrReplacePhoneAccount(account);
568 }
569
570 /**
571 * Adds a {@code PhoneAccount}, replacing an existing one if found.
572 *
573 * @param account The {@code PhoneAccount} to add or replace.
574 */
575 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800576 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
577 account.getAccountHandle(), account);
578
Santos Cordonea5cb932015-05-07 16:28:38 -0700579 // Start _enabled_ property as false.
580 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
581 // source app provides or else an third party app could enable itself.
582 boolean isEnabled = false;
583
Tony Mak98e6bdc2015-11-25 21:54:05 +0000584 PhoneAccount oldAccount = getPhoneAccountUnchecked(account.getAccountHandle());
Santos Cordon6a212642015-05-08 16:35:23 -0700585 if (oldAccount != null) {
586 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700587 isEnabled = oldAccount.isEnabled();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700588 Log.i(this, getAccountDiffString(account, oldAccount));
589 } else {
590 Log.i(this, "New phone account registered: " + account);
Santos Cordon176ae282014-07-14 02:02:14 -0700591 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700592
Santos Cordon6a212642015-05-08 16:35:23 -0700593 mState.accounts.add(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700594 // Reset enabled state to whatever the value was if the account was already registered,
595 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled.
596 account.setIsEnabled(
597 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION));
Santos Cordon176ae282014-07-14 02:02:14 -0700598
Ihab Awad293edf22014-07-24 17:52:29 -0700599 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700600 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700601 }
602
Evan Charlton89176372014-07-19 18:23:09 -0700603 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000604 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700605 if (account != null) {
606 if (mState.accounts.remove(account)) {
607 write();
608 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700609 }
610 }
Santos Cordon176ae282014-07-14 02:02:14 -0700611 }
612
Tyler Gunnd900ce62014-08-13 11:40:59 -0700613 /**
614 * Un-registers all phone accounts associated with a specified package.
615 *
616 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800617 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700618 */
Evan Charlton105d9772014-11-25 14:08:53 -0800619 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700620 boolean accountsRemoved = false;
621 Iterator<PhoneAccount> it = mState.accounts.iterator();
622 while (it.hasNext()) {
623 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800624 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
625 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
626 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700627 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530628 mState.accounts.remove(phoneAccount);
Tyler Gunnd900ce62014-08-13 11:40:59 -0700629 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700630 }
631 }
632
Tyler Gunnd900ce62014-08-13 11:40:59 -0700633 if (accountsRemoved) {
634 write();
635 fireAccountsChanged();
636 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700637 }
638
Nancy Chen140004a2014-10-15 15:48:38 -0700639 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700640 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Yorke Leeb1957232015-06-30 10:09:54 -0700641 return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number);
Nancy Chen140004a2014-10-15 15:48:38 -0700642 }
643
Ihab Awadb78b2762014-07-25 15:16:23 -0700644 public void addListener(Listener l) {
645 mListeners.add(l);
646 }
647
648 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700649 if (l != null) {
650 mListeners.remove(l);
651 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700652 }
653
654 private void fireAccountsChanged() {
655 for (Listener l : mListeners) {
656 l.onAccountsChanged(this);
657 }
658 }
659
660 private void fireDefaultOutgoingChanged() {
661 for (Listener l : mListeners) {
662 l.onDefaultOutgoingChanged(this);
663 }
664 }
665
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700666 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
667 if (account1 == null || account2 == null) {
668 return "Diff: " + account1 + ", " + account2;
669 }
670
671 StringBuffer sb = new StringBuffer();
672 sb.append("[").append(account1.getAccountHandle());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700673 appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()),
674 Log.piiHandle(account2.getAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700675 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
676 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
677 appendDiff(sb, "icon", account1.getIcon(), account2.getIcon());
678 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
679 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700680 appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()),
681 Log.piiHandle(account2.getSubscriptionAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700682 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
683 account2.getSupportedUriSchemes());
684 sb.append("]");
685 return sb.toString();
686 }
687
688 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
689 if (!Objects.equals(obj1, obj2)) {
690 sb.append("(")
691 .append(attrName)
692 .append(": ")
693 .append(obj1)
694 .append(" -> ")
695 .append(obj2)
696 .append(")");
697 }
698 }
699
Tyler Gunncb59b672014-08-20 09:02:11 -0700700 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700701 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700702 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700703 *
704 * @param phoneAccountHandle The phone account to check.
705 * @return {@code True} if the phone account has permission.
706 */
Santos Cordon6a212642015-05-08 16:35:23 -0700707 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800708 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
709 if (resolveInfos.isEmpty()) {
710 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700711 return false;
712 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800713 for (ResolveInfo resolveInfo : resolveInfos) {
714 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700715 if (serviceInfo == null) {
716 return false;
717 }
718
719 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
720 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
721 serviceInfo.permission)) {
722 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
723 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
724 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800725 return false;
726 }
727 }
728 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700729 }
730
Santos Cordon6a212642015-05-08 16:35:23 -0700731 //
732 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
733 //
Ihab Awad293edf22014-07-24 17:52:29 -0700734
Santos Cordonafe59e52014-08-22 16:48:43 -0700735 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700736 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700737 *
Santos Cordon6a212642015-05-08 16:35:23 -0700738 * @param handle
739 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700740 */
Tony Mak98e6bdc2015-11-25 21:54:05 +0000741 public PhoneAccount getPhoneAccountUnchecked(PhoneAccountHandle handle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700742 for (PhoneAccount m : mState.accounts) {
743 if (Objects.equals(handle, m.getAccountHandle())) {
744 return m;
745 }
746 }
747 return null;
748 }
749
750 /**
751 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
752 * account before returning it. The current user is the active user on the actual android
753 * device.
754 */
Tony Mak240656f2015-12-04 11:36:22 +0000755 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, UserHandle userHandle) {
756 return getPhoneAccount(handle, userHandle, /* acrossProfiles */ false);
757 }
758
759 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle,
760 UserHandle userHandle, boolean acrossProfiles) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000761 PhoneAccount account = getPhoneAccountUnchecked(handle);
Tony Mak240656f2015-12-04 11:36:22 +0000762 if (account != null && (isVisibleForUser(account, userHandle, acrossProfiles))) {
Santos Cordon6a212642015-05-08 16:35:23 -0700763 return account;
764 }
765 return null;
766 }
767
Tony Mak240656f2015-12-04 11:36:22 +0000768 public PhoneAccount getPhoneAccountOfCurrentUser(PhoneAccountHandle handle) {
769 return getPhoneAccount(handle, mCurrentUserHandle);
770 }
771
Bryce Leea0bb7052015-10-16 13:31:40 -0700772 private List<PhoneAccountHandle> getPhoneAccountHandles(
773 int capabilities,
774 String uriScheme,
775 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000776 boolean includeDisabledAccounts,
777 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700778 return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme,
Tony Mak240656f2015-12-04 11:36:22 +0000779 packageName, includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700780 }
781
Santos Cordon6a212642015-05-08 16:35:23 -0700782 /**
783 * Returns a list of phone account handles with the specified capabilities, uri scheme,
784 * and package name.
785 */
786 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700787 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700788 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700789 String uriScheme,
790 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000791 boolean includeDisabledAccounts,
792 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700793 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700794
795 for (PhoneAccount account : getPhoneAccounts(
Bryce Leea0bb7052015-10-16 13:31:40 -0700796 capabilities, excludedCapabilities, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000797 includeDisabledAccounts, userHandle)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700798 handles.add(account.getAccountHandle());
799 }
800 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700801 }
802
Bryce Leea0bb7052015-10-16 13:31:40 -0700803 private List<PhoneAccount> getPhoneAccounts(
804 int capabilities,
805 String uriScheme,
806 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000807 boolean includeDisabledAccounts,
808 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700809 return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000810 includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700811 }
812
Tyler Gunn84253572014-09-02 14:50:05 -0700813 /**
814 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700815 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700816 *
Santos Cordon6a212642015-05-08 16:35:23 -0700817 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
Bryce Leea0bb7052015-10-16 13:31:40 -0700818 * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have.
819 * Ignored if 0.
Santos Cordon6a212642015-05-08 16:35:23 -0700820 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700821 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700822 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700823 */
Santos Cordon6a212642015-05-08 16:35:23 -0700824 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700825 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700826 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700827 String uriScheme,
828 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000829 boolean includeDisabledAccounts,
830 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700831 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700832 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700833 if (!(m.isEnabled() || includeDisabledAccounts)) {
834 // Do not include disabled accounts.
835 continue;
836 }
837
Bryce Leea0bb7052015-10-16 13:31:40 -0700838 if ((m.getCapabilities() & excludedCapabilities) != 0) {
839 // If an excluded capability is present, skip.
840 continue;
841 }
842
Santos Cordon6a212642015-05-08 16:35:23 -0700843 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800844 // Account doesn't have the right capabilities; skip this one.
845 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700846 }
Evan Charlton105d9772014-11-25 14:08:53 -0800847 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
848 // Account doesn't support this URI scheme; skip this one.
849 continue;
850 }
Santos Cordon6a212642015-05-08 16:35:23 -0700851 PhoneAccountHandle handle = m.getAccountHandle();
852
853 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800854 // This component cannot be resolved anymore; skip this one.
855 continue;
856 }
Santos Cordon6a212642015-05-08 16:35:23 -0700857 if (packageName != null &&
858 !packageName.equals(handle.getComponentName().getPackageName())) {
859 // Not the right package name; skip this one.
860 continue;
861 }
Tony Mak240656f2015-12-04 11:36:22 +0000862 if (!isVisibleForUser(m, userHandle, false)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800863 // Account is not visible for the current user; skip this one.
864 continue;
865 }
Santos Cordon6a212642015-05-08 16:35:23 -0700866 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700867 }
Santos Cordon6a212642015-05-08 16:35:23 -0700868 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700869 }
870
Santos Cordon6a212642015-05-08 16:35:23 -0700871 //
872 // State Implementation for PhoneAccountRegistrar
873 //
874
Ihab Awad293edf22014-07-24 17:52:29 -0700875 /**
876 * The state of this {@code PhoneAccountRegistrar}.
877 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700878 @VisibleForTesting
879 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700880 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000881 * Store the default phone account handle of users. If no record of a user can be found in
882 * the map, it means that no default phone account handle is set in that user.
Ihab Awad293edf22014-07-24 17:52:29 -0700883 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000884 public final Map<UserHandle, DefaultPhoneAccountHandle> defaultOutgoingAccountHandles
885 = new ConcurrentHashMap<>();
Ihab Awad293edf22014-07-24 17:52:29 -0700886
887 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700888 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700889 */
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530890 public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700891
892 /**
893 * The version number of the State data.
894 */
895 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700896 }
897
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700898 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000899 * The default {@link PhoneAccountHandle} of a user.
900 */
901 public static class DefaultPhoneAccountHandle {
902
903 public final UserHandle userHandle;
904
905 public final PhoneAccountHandle phoneAccountHandle;
906
907 public DefaultPhoneAccountHandle(UserHandle userHandle,
908 PhoneAccountHandle phoneAccountHandle) {
909 this.userHandle = userHandle;
910 this.phoneAccountHandle = phoneAccountHandle;
911 }
912 }
913
914 /**
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700915 * Dumps the state of the {@link CallsManager}.
916 *
917 * @param pw The {@code IndentingPrintWriter} to write the state to.
918 */
919 public void dump(IndentingPrintWriter pw) {
920 if (mState != null) {
921 pw.println("xmlVersion: " + mState.versionNumber);
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000922 DefaultPhoneAccountHandle defaultPhoneAccountHandle
923 = mState.defaultOutgoingAccountHandles.get(Process.myUserHandle());
924 pw.println("defaultOutgoing: " + (defaultPhoneAccountHandle == null ? "none" :
925 defaultPhoneAccountHandle.phoneAccountHandle));
Tony Mak240656f2015-12-04 11:36:22 +0000926 pw.println("simCallManager: " + getSimCallManager(mCurrentUserHandle));
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700927 pw.println("phoneAccounts:");
928 pw.increaseIndent();
929 for (PhoneAccount phoneAccount : mState.accounts) {
930 pw.println(phoneAccount);
931 }
932 pw.decreaseIndent();
933 }
934 }
935
Ihab Awad293edf22014-07-24 17:52:29 -0700936 ////////////////////////////////////////////////////////////////////////////////////////////////
937 //
938 // State management
939 //
940
Hall Liu3f7f64d2016-01-07 17:40:13 -0800941 private class AsyncXmlWriter extends AsyncTask<ByteArrayOutputStream, Void, Void> {
942 @Override
943 public Void doInBackground(ByteArrayOutputStream... args) {
944 final ByteArrayOutputStream buffer = args[0];
945 FileOutputStream fileOutput = null;
Ihab Awadb78b2762014-07-25 15:16:23 -0700946 try {
Hall Liu3f7f64d2016-01-07 17:40:13 -0800947 synchronized (mWriteLock) {
948 fileOutput = mAtomicFile.startWrite();
949 buffer.writeTo(fileOutput);
950 mAtomicFile.finishWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -0700951 }
Hall Liu3f7f64d2016-01-07 17:40:13 -0800952 } catch (IOException e) {
953 Log.e(this, e, "Writing state to XML file");
954 mAtomicFile.failWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -0700955 }
Hall Liu3f7f64d2016-01-07 17:40:13 -0800956 return null;
957 }
958 }
959
960 private void write() {
961 try {
962 ByteArrayOutputStream os = new ByteArrayOutputStream();
963 XmlSerializer serializer = new FastXmlSerializer();
964 serializer.setOutput(os, "utf-8");
965 writeToXml(mState, serializer, mContext);
966 serializer.flush();
967 new AsyncXmlWriter().execute(os);
Ihab Awadb78b2762014-07-25 15:16:23 -0700968 } catch (IOException e) {
Hall Liu3f7f64d2016-01-07 17:40:13 -0800969 Log.e(this, e, "Writing state to XML buffer");
Ihab Awad104f8062014-07-17 11:29:35 -0700970 }
971 }
972
Ihab Awadb78b2762014-07-25 15:16:23 -0700973 private void read() {
974 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700975 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700976 is = mAtomicFile.openRead();
977 } catch (FileNotFoundException ex) {
978 return;
979 }
980
Tyler Gunn84253572014-09-02 14:50:05 -0700981 boolean versionChanged = false;
982
Ihab Awadb78b2762014-07-25 15:16:23 -0700983 XmlPullParser parser;
984 try {
985 parser = Xml.newPullParser();
986 parser.setInput(new BufferedInputStream(is), null);
987 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700988 mState = readFromXml(parser, mContext);
989 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
990
Ihab Awadb78b2762014-07-25 15:16:23 -0700991 } catch (IOException | XmlPullParserException e) {
992 Log.e(this, e, "Reading state from XML file");
993 mState = new State();
994 } finally {
995 try {
996 is.close();
997 } catch (IOException e) {
998 Log.e(this, e, "Closing InputStream");
999 }
Ihab Awad104f8062014-07-17 11:29:35 -07001000 }
Tyler Gunn84253572014-09-02 14:50:05 -07001001
Evan Charlton105d9772014-11-25 14:08:53 -08001002 // Verify all of the UserHandles.
1003 List<PhoneAccount> badAccounts = new ArrayList<>();
1004 for (PhoneAccount phoneAccount : mState.accounts) {
1005 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
1006 if (userHandle == null) {
1007 Log.w(this, "Missing UserHandle for %s", phoneAccount);
1008 badAccounts.add(phoneAccount);
1009 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
1010 Log.w(this, "User does not exist for %s", phoneAccount);
1011 badAccounts.add(phoneAccount);
1012 }
1013 }
1014 mState.accounts.removeAll(badAccounts);
1015
Tyler Gunn84253572014-09-02 14:50:05 -07001016 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -08001017 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -07001018 write();
1019 }
Santos Cordon176ae282014-07-14 02:02:14 -07001020 }
1021
Evan Charlton105d9772014-11-25 14:08:53 -08001022 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001023 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -08001024 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -07001025 }
Ihab Awad104f8062014-07-17 11:29:35 -07001026
Tyler Gunn84253572014-09-02 14:50:05 -07001027 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001028 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -07001029 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001030 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -07001031 }
1032
Ihab Awad293edf22014-07-24 17:52:29 -07001033 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -07001034 //
Ihab Awadb78b2762014-07-25 15:16:23 -07001035 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -07001036 //
1037
Ihab Awadb78b2762014-07-25 15:16:23 -07001038 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -07001039 public abstract static class XmlSerialization<T> {
Tyler Gunn369c8742015-10-07 15:39:28 -07001040 private static final String TAG_VALUE = "value";
1041 private static final String ATTRIBUTE_LENGTH = "length";
1042 private static final String ATTRIBUTE_KEY = "key";
1043 private static final String ATTRIBUTE_VALUE_TYPE = "type";
1044 private static final String VALUE_TYPE_STRING = "string";
1045 private static final String VALUE_TYPE_INTEGER = "integer";
1046 private static final String VALUE_TYPE_BOOLEAN = "boolean";
Tyler Gunn84253572014-09-02 14:50:05 -07001047
Ihab Awadb78b2762014-07-25 15:16:23 -07001048 /**
1049 * Write the supplied object to XML
1050 */
Evan Charlton105d9772014-11-25 14:08:53 -08001051 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001052 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -07001053
1054 /**
1055 * Read from the supplied XML into a new object, returning null in case of an
1056 * unrecoverable schema mismatch or other data error. 'parser' must be already
1057 * positioned at the first tag that is expected to have been emitted by this
1058 * object's writeToXml(). This object tries to fail early without modifying
1059 * 'parser' if it does not recognize the data it sees.
1060 */
Tyler Gunn84253572014-09-02 14:50:05 -07001061 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001062 throws IOException, XmlPullParserException;
1063
Ihab Awadd9f54382014-10-24 11:44:47 -07001064 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -07001065 throws IOException {
1066 if (value != null) {
1067 serializer.startTag(null, tagName);
1068 serializer.text(Objects.toString(value));
1069 serializer.endTag(null, tagName);
1070 }
1071 }
Tyler Gunn84253572014-09-02 14:50:05 -07001072
1073 /**
1074 * Serializes a string array.
1075 *
1076 * @param tagName The tag name for the string array.
1077 * @param values The string values to serialize.
1078 * @param serializer The serializer.
1079 * @throws IOException
1080 */
1081 protected void writeStringList(String tagName, List<String> values,
1082 XmlSerializer serializer)
1083 throws IOException {
1084
1085 serializer.startTag(null, tagName);
1086 if (values != null) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001087 serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size()));
Tyler Gunn84253572014-09-02 14:50:05 -07001088 for (String toSerialize : values) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001089 serializer.startTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001090 if (toSerialize != null ){
1091 serializer.text(toSerialize);
1092 }
Tyler Gunn369c8742015-10-07 15:39:28 -07001093 serializer.endTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001094 }
1095 } else {
Tyler Gunn369c8742015-10-07 15:39:28 -07001096 serializer.attribute(null, ATTRIBUTE_LENGTH, "0");
1097 }
1098 serializer.endTag(null, tagName);
1099 }
1100
1101 protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer)
1102 throws IOException {
1103
1104 serializer.startTag(null, tagName);
1105 if (values != null) {
1106 for (String key : values.keySet()) {
1107 Object value = values.get(key);
1108
1109 if (value == null) {
1110 continue;
1111 }
1112
1113 String valueType;
1114 if (value instanceof String) {
1115 valueType = VALUE_TYPE_STRING;
1116 } else if (value instanceof Integer) {
1117 valueType = VALUE_TYPE_INTEGER;
1118 } else if (value instanceof Boolean) {
1119 valueType = VALUE_TYPE_BOOLEAN;
1120 } else {
1121 Log.w(this,
1122 "PhoneAccounts support only string, integer and boolean extras TY.");
1123 continue;
1124 }
1125
1126 serializer.startTag(null, TAG_VALUE);
1127 serializer.attribute(null, ATTRIBUTE_KEY, key);
1128 serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType);
1129 serializer.text(Objects.toString(value));
1130 serializer.endTag(null, TAG_VALUE);
1131 }
Tyler Gunn84253572014-09-02 14:50:05 -07001132 }
1133 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -07001134 }
Tyler Gunn84253572014-09-02 14:50:05 -07001135
Santos Cordon9c30c282015-05-13 16:28:27 -07001136 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -07001137 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001138 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001139 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001140 value.writeToStream(stream);
1141 byte[] iconByteArray = stream.toByteArray();
1142 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001143
1144 serializer.startTag(null, tagName);
1145 serializer.text(text);
1146 serializer.endTag(null, tagName);
1147 }
Tyler Gunn84253572014-09-02 14:50:05 -07001148 }
1149
Evan Charlton105d9772014-11-25 14:08:53 -08001150 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1151 throws IOException {
1152 serializer.startTag(null, tagName);
1153 serializer.text(Long.valueOf(value).toString());
1154 serializer.endTag(null, tagName);
1155 }
1156
Tyler Gunn84253572014-09-02 14:50:05 -07001157 /**
1158 * Reads a string array from the XML parser.
1159 *
1160 * @param parser The XML parser.
1161 * @return String array containing the parsed values.
1162 * @throws IOException Exception related to IO.
1163 * @throws XmlPullParserException Exception related to parsing.
1164 */
1165 protected List<String> readStringList(XmlPullParser parser)
1166 throws IOException, XmlPullParserException {
1167
Tyler Gunn369c8742015-10-07 15:39:28 -07001168 int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH));
Tyler Gunn84253572014-09-02 14:50:05 -07001169 List<String> arrayEntries = new ArrayList<String>(length);
1170 String value = null;
1171
1172 if (length == 0) {
1173 return arrayEntries;
1174 }
1175
1176 int outerDepth = parser.getDepth();
1177 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001178 if (parser.getName().equals(TAG_VALUE)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001179 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001180 value = parser.getText();
1181 arrayEntries.add(value);
1182 }
1183 }
1184
1185 return arrayEntries;
1186 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001187
Tyler Gunn369c8742015-10-07 15:39:28 -07001188 /**
1189 * Reads a bundle from the XML parser.
1190 *
1191 * @param parser The XML parser.
1192 * @return Bundle containing the parsed values.
1193 * @throws IOException Exception related to IO.
1194 * @throws XmlPullParserException Exception related to parsing.
1195 */
1196 protected Bundle readBundle(XmlPullParser parser)
1197 throws IOException, XmlPullParserException {
1198
1199 Bundle bundle = null;
1200 int outerDepth = parser.getDepth();
1201 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1202 if (parser.getName().equals(TAG_VALUE)) {
1203 String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE);
1204 String key = parser.getAttributeValue(null, ATTRIBUTE_KEY);
1205 parser.next();
1206 String value = parser.getText();
1207
1208 if (bundle == null) {
1209 bundle = new Bundle();
1210 }
1211
1212 // Do not write null values to the bundle.
1213 if (value == null) {
1214 continue;
1215 }
1216
1217 if (VALUE_TYPE_STRING.equals(valueType)) {
1218 bundle.putString(key, value);
1219 } else if (VALUE_TYPE_INTEGER.equals(valueType)) {
1220 try {
1221 int intValue = Integer.parseInt(value);
1222 bundle.putInt(key, intValue);
1223 } catch (NumberFormatException nfe) {
1224 Log.w(this, "Invalid integer PhoneAccount extra.");
1225 }
1226 } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) {
1227 boolean boolValue = Boolean.parseBoolean(value);
1228 bundle.putBoolean(key, boolValue);
1229 } else {
1230 Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle.");
1231 }
1232 }
1233 }
1234 return bundle;
1235 }
1236
Santos Cordon9c30c282015-05-13 16:28:27 -07001237 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001238 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1239 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1240 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001241
1242 protected Icon readIcon(XmlPullParser parser) throws IOException {
1243 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1244 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1245 return Icon.createFromStream(stream);
1246 }
Ihab Awad104f8062014-07-17 11:29:35 -07001247 }
1248
Ihab Awadb78b2762014-07-25 15:16:23 -07001249 @VisibleForTesting
1250 public static final XmlSerialization<State> sStateXml =
1251 new XmlSerialization<State>() {
1252 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001253 private static final String DEFAULT_OUTGOING = "default_outgoing";
1254 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001255 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001256
1257 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001258 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001259 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001260 if (o != null) {
1261 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001262 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001263
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001264 serializer.startTag(null, DEFAULT_OUTGOING);
1265 for (DefaultPhoneAccountHandle defaultPhoneAccountHandle : o
1266 .defaultOutgoingAccountHandles.values()) {
1267 sDefaultPhoneAcountHandleXml
1268 .writeToXml(defaultPhoneAccountHandle, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001269 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001270 serializer.endTag(null, DEFAULT_OUTGOING);
Ihab Awad26923222014-07-30 10:54:35 -07001271
Ihab Awad26923222014-07-30 10:54:35 -07001272 serializer.startTag(null, ACCOUNTS);
1273 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001274 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001275 }
1276 serializer.endTag(null, ACCOUNTS);
1277
1278 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001279 }
Ihab Awad104f8062014-07-17 11:29:35 -07001280 }
1281
1282 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001283 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001284 throws IOException, XmlPullParserException {
1285 if (parser.getName().equals(CLASS_STATE)) {
1286 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001287
1288 String rawVersion = parser.getAttributeValue(null, VERSION);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001289 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : Integer.parseInt(rawVersion);
Tyler Gunn84253572014-09-02 14:50:05 -07001290
Ihab Awadb78b2762014-07-25 15:16:23 -07001291 int outerDepth = parser.getDepth();
1292 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1293 if (parser.getName().equals(DEFAULT_OUTGOING)) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001294 if (s.versionNumber < 9) {
1295 // Migration old default phone account handle here by assuming the
1296 // default phone account handle is belong to primary user.
1297 parser.nextTag();
1298 PhoneAccountHandle phoneAccountHandle = sPhoneAccountHandleXml
1299 .readFromXml(parser, s.versionNumber, context);
1300 UserManager userManager = UserManager.get(context);
1301 UserInfo primaryUser = userManager.getPrimaryUser();
1302 if (primaryUser != null) {
1303 UserHandle userHandle = primaryUser.getUserHandle();
1304 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1305 = new DefaultPhoneAccountHandle(userHandle,
1306 phoneAccountHandle);
1307 s.defaultOutgoingAccountHandles
1308 .put(userHandle, defaultPhoneAccountHandle);
1309 }
1310 } else {
1311 int defaultAccountHandlesDepth = parser.getDepth();
1312 while (XmlUtils.nextElementWithin(parser, defaultAccountHandlesDepth)) {
1313 DefaultPhoneAccountHandle accountHandle
1314 = sDefaultPhoneAcountHandleXml
1315 .readFromXml(parser, s.versionNumber, context);
1316 if (accountHandle != null && s.accounts != null) {
1317 s.defaultOutgoingAccountHandles
1318 .put(accountHandle.userHandle, accountHandle);
1319 }
1320 }
1321 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001322 } else if (parser.getName().equals(ACCOUNTS)) {
1323 int accountsDepth = parser.getDepth();
1324 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001325 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1326 s.versionNumber, context);
1327
1328 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001329 s.accounts.add(account);
1330 }
1331 }
Ihab Awad104f8062014-07-17 11:29:35 -07001332 }
1333 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001334 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001335 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001336 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001337 }
1338 };
1339
Ihab Awadb78b2762014-07-25 15:16:23 -07001340 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001341 public static final XmlSerialization<DefaultPhoneAccountHandle> sDefaultPhoneAcountHandleXml =
1342 new XmlSerialization<DefaultPhoneAccountHandle>() {
1343 private static final String CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE
1344 = "default_outgoing_phone_account_handle";
1345 private static final String USER_SERIAL_NUMBER = "user_serial_number";
1346 private static final String ACCOUNT_HANDLE = "account_handle";
1347
1348 @Override
1349 public void writeToXml(DefaultPhoneAccountHandle o, XmlSerializer serializer,
1350 Context context) throws IOException {
1351 if (o != null) {
1352 final UserManager userManager = UserManager.get(context);
1353 final long serialNumber = userManager.getSerialNumberForUser(o.userHandle);
1354 if (serialNumber != -1) {
1355 serializer.startTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1356 writeLong(USER_SERIAL_NUMBER, serialNumber, serializer);
1357 serializer.startTag(null, ACCOUNT_HANDLE);
1358 sPhoneAccountHandleXml.writeToXml(o.phoneAccountHandle, serializer,
1359 context);
1360 serializer.endTag(null, ACCOUNT_HANDLE);
1361 serializer.endTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1362 }
1363 }
1364 }
1365
1366 @Override
1367 public DefaultPhoneAccountHandle readFromXml(XmlPullParser parser, int version,
1368 Context context)
1369 throws IOException, XmlPullParserException {
1370 if (parser.getName().equals(CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE)) {
1371 int outerDepth = parser.getDepth();
1372 PhoneAccountHandle accountHandle = null;
1373 String userSerialNumberString = null;
1374 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1375 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1376 parser.nextTag();
1377 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1378 context);
1379 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1380 parser.next();
1381 userSerialNumberString = parser.getText();
1382 }
1383 }
1384 UserHandle userHandle = null;
1385 if (userSerialNumberString != null) {
1386 try {
1387 long serialNumber = Long.parseLong(userSerialNumberString);
1388 userHandle = UserManager.get(context)
1389 .getUserForSerialNumber(serialNumber);
1390 } catch (NumberFormatException e) {
1391 Log.e(this, e,
1392 "Could not parse UserHandle " + userSerialNumberString);
1393 }
1394 }
1395 if (accountHandle != null && userHandle != null) {
1396 return new DefaultPhoneAccountHandle(userHandle, accountHandle);
1397 }
1398 }
1399 return null;
1400 }
1401 };
1402
1403
1404 @VisibleForTesting
Ihab Awadb78b2762014-07-25 15:16:23 -07001405 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1406 new XmlSerialization<PhoneAccount>() {
1407 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1408 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001409 private static final String ADDRESS = "handle";
1410 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001411 private static final String CAPABILITIES = "capabilities";
1412 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001413 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1414 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001415 private static final String ICON_TINT = "icon_tint";
1416 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001417 private static final String LABEL = "label";
1418 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001419 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001420 private static final String ICON = "icon";
Tyler Gunn369c8742015-10-07 15:39:28 -07001421 private static final String EXTRAS = "extras";
Santos Cordonea5cb932015-05-07 16:28:38 -07001422 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001423
1424 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001425 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001426 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001427 if (o != null) {
1428 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001429
Ihab Awad26923222014-07-30 10:54:35 -07001430 if (o.getAccountHandle() != null) {
1431 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001432 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001433 serializer.endTag(null, ACCOUNT_HANDLE);
1434 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001435
Ihab Awadd9f54382014-10-24 11:44:47 -07001436 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1437 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1438 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001439 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001440 writeTextIfNonNull(HIGHLIGHT_COLOR,
1441 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001442 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1443 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001444 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Tyler Gunn369c8742015-10-07 15:39:28 -07001445 writeBundle(EXTRAS, o.getExtras(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001446 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001447
Ihab Awad26923222014-07-30 10:54:35 -07001448 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1449 }
Ihab Awad104f8062014-07-17 11:29:35 -07001450 }
1451
Tyler Gunn84253572014-09-02 14:50:05 -07001452 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001453 throws IOException, XmlPullParserException {
1454 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1455 int outerDepth = parser.getDepth();
1456 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001457 Uri address = null;
1458 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001459 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001460 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001461 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001462 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001463 int iconTint = PhoneAccount.NO_ICON_TINT;
1464 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001465 String label = null;
1466 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001467 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001468 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001469 boolean enabled = false;
Tyler Gunn369c8742015-10-07 15:39:28 -07001470 Bundle extras = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001471
1472 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1473 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1474 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001475 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1476 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001477 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001478 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001479 address = Uri.parse(parser.getText());
1480 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001481 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001482 String nextText = parser.getText();
1483 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001484 } else if (parser.getName().equals(CAPABILITIES)) {
1485 parser.next();
1486 capabilities = Integer.parseInt(parser.getText());
1487 } else if (parser.getName().equals(ICON_RES_ID)) {
1488 parser.next();
1489 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001490 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1491 parser.next();
1492 iconPackageName = parser.getText();
1493 } else if (parser.getName().equals(ICON_BITMAP)) {
1494 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001495 iconBitmap = readBitmap(parser);
1496 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001497 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001498 iconTint = Integer.parseInt(parser.getText());
1499 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1500 parser.next();
1501 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001502 } else if (parser.getName().equals(LABEL)) {
1503 parser.next();
1504 label = parser.getText();
1505 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1506 parser.next();
1507 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001508 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1509 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001510 } else if (parser.getName().equals(ICON)) {
1511 parser.next();
1512 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001513 } else if (parser.getName().equals(ENABLED)) {
1514 parser.next();
1515 enabled = "true".equalsIgnoreCase(parser.getText());
Tyler Gunn369c8742015-10-07 15:39:28 -07001516 } else if (parser.getName().equals(EXTRAS)) {
1517 parser.next();
1518 extras = readBundle(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -07001519 }
1520 }
Tyler Gunn84253572014-09-02 14:50:05 -07001521
Santos Cordona9eebe42015-06-11 14:07:44 -07001522 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1523 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001524 ComponentName sipComponentName = new ComponentName("com.android.phone",
1525 "com.android.services.telephony.sip.SipConnectionService");
1526
Tyler Gunn84253572014-09-02 14:50:05 -07001527 // Upgrade older phone accounts to specify the supported URI schemes.
1528 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001529 supportedUriSchemes = new ArrayList<>();
1530
1531 // Handle the SIP connection service.
1532 // Check the system settings to see if it also should handle "tel" calls.
1533 if (accountHandle.getComponentName().equals(sipComponentName)) {
1534 boolean useSipForPstn = useSipForPstnCalls(context);
1535 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1536 if (useSipForPstn) {
1537 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1538 }
1539 } else {
1540 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1541 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1542 }
1543 }
1544
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001545 // Upgrade older phone accounts with explicit package name
1546 if (version < 5) {
1547 if (iconBitmap == null) {
1548 iconPackageName = accountHandle.getComponentName().getPackageName();
1549 }
1550 }
1551
Santos Cordona82aed82015-05-26 10:43:56 -07001552 if (version < 6) {
1553 // Always enable all SIP accounts on upgrade to version 6
1554 if (accountHandle.getComponentName().equals(sipComponentName)) {
1555 enabled = true;
1556 }
1557 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001558 if (version < 7) {
1559 // Always enabled all PSTN acocunts on upgrade to version 7
1560 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1561 enabled = true;
1562 }
1563 }
Roshan Pius6f752c82015-08-10 10:42:06 -07001564 if (version < 8) {
1565 // Migrate the SIP account handle ids to use SIP username instead of SIP URI.
1566 if (accountHandle.getComponentName().equals(sipComponentName)) {
1567 Uri accountUri = Uri.parse(accountHandle.getId());
1568 if (accountUri.getScheme() != null &&
1569 accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) {
1570 accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(),
1571 accountUri.getSchemeSpecificPart(),
1572 accountHandle.getUserHandle());
1573 }
1574 }
1575 }
Santos Cordona82aed82015-05-26 10:43:56 -07001576
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001577 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001578 .setAddress(address)
1579 .setSubscriptionAddress(subscriptionAddress)
1580 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001581 .setShortDescription(shortDescription)
1582 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001583 .setHighlightColor(highlightColor)
Tyler Gunn369c8742015-10-07 15:39:28 -07001584 .setExtras(extras)
Santos Cordonea5cb932015-05-07 16:28:38 -07001585 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001586
Santos Cordon9c30c282015-05-13 16:28:27 -07001587 if (icon != null) {
1588 builder.setIcon(icon);
1589 } else if (iconBitmap != null) {
1590 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1591 } else if (!TextUtils.isEmpty(iconPackageName)) {
1592 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1593 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001594 }
1595
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001596 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001597 }
1598 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001599 }
Tyler Gunn84253572014-09-02 14:50:05 -07001600
1601 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001602 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1603 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001604 *
1605 * @param context The context.
1606 * @return {@code True} if SIP should be used for all calls.
1607 */
1608 private boolean useSipForPstnCalls(Context context) {
1609 String option = Settings.System.getString(context.getContentResolver(),
1610 Settings.System.SIP_CALL_OPTIONS);
1611 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1612 return option.equals(Settings.System.SIP_ALWAYS);
1613 }
Ihab Awad104f8062014-07-17 11:29:35 -07001614 };
1615
Ihab Awadb78b2762014-07-25 15:16:23 -07001616 @VisibleForTesting
1617 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1618 new XmlSerialization<PhoneAccountHandle>() {
1619 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001620 private static final String COMPONENT_NAME = "component_name";
1621 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001622 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001623
1624 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001625 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001626 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001627 if (o != null) {
1628 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001629
Ihab Awad26923222014-07-30 10:54:35 -07001630 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001631 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001632 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1633 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001634
Ihab Awadd9f54382014-10-24 11:44:47 -07001635 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001636
Evan Charlton105d9772014-11-25 14:08:53 -08001637 if (o.getUserHandle() != null && context != null) {
1638 UserManager userManager = UserManager.get(context);
1639 writeLong(USER_SERIAL_NUMBER,
1640 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1641 }
1642
Ihab Awad26923222014-07-30 10:54:35 -07001643 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1644 }
Ihab Awad104f8062014-07-17 11:29:35 -07001645 }
1646
1647 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001648 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001649 throws IOException, XmlPullParserException {
1650 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1651 String componentNameString = null;
1652 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001653 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001654 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001655
1656 UserManager userManager = UserManager.get(context);
1657
Ihab Awadb78b2762014-07-25 15:16:23 -07001658 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1659 if (parser.getName().equals(COMPONENT_NAME)) {
1660 parser.next();
1661 componentNameString = parser.getText();
1662 } else if (parser.getName().equals(ID)) {
1663 parser.next();
1664 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001665 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1666 parser.next();
1667 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001668 }
1669 }
Ihab Awad26923222014-07-30 10:54:35 -07001670 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001671 UserHandle userHandle = null;
1672 if (userSerialNumberString != null) {
1673 try {
1674 long serialNumber = Long.parseLong(userSerialNumberString);
1675 userHandle = userManager.getUserForSerialNumber(serialNumber);
1676 } catch (NumberFormatException e) {
1677 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1678 }
1679 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001680 return new PhoneAccountHandle(
1681 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001682 idString,
1683 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001684 }
1685 }
1686 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001687 }
1688 };
Santos Cordon176ae282014-07-14 02:02:14 -07001689}