blob: 8f9a468059c8233259be5322c85a09652f742c9c [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;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070032import android.os.PersistableBundle;
Evan Charlton105d9772014-11-25 14:08:53 -080033import android.os.Process;
34import android.os.UserHandle;
35import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070036import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070037import android.telecom.ConnectionService;
Santos Cordon59c21a72015-06-11 10:11:21 -070038import android.telecom.DefaultDialerManager;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070039import android.telecom.PhoneAccount;
40import android.telecom.PhoneAccountHandle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070041import android.telephony.CarrierConfigManager;
Nancy Chen140004a2014-10-15 15:48:38 -070042import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070043import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080044import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070045import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070046import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070047import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070048import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070049
Tyler Gunn91d43cf2014-09-17 12:19:39 -070050// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070051import com.android.internal.annotations.VisibleForTesting;
52import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070053import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070054import com.android.internal.util.XmlUtils;
55
Evan Charltonaf51ceb2014-07-30 11:56:36 -070056import org.xmlpull.v1.XmlPullParser;
57import org.xmlpull.v1.XmlPullParserException;
58import org.xmlpull.v1.XmlSerializer;
59
Ihab Awadb78b2762014-07-25 15:16:23 -070060import java.io.BufferedInputStream;
61import java.io.BufferedOutputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070062import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070063import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070064import java.io.File;
65import java.io.FileNotFoundException;
66import java.io.FileOutputStream;
67import java.io.IOException;
68import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070069import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070070import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070071import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070072import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080073import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070074import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070075import java.util.List;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000076import java.util.Map;
Santos Cordon176ae282014-07-14 02:02:14 -070077import java.util.Objects;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000078import java.util.concurrent.ConcurrentHashMap;
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
Tony Mak240656f2015-12-04 11:36:22 +000099 * has a work profile running as another user (B2). Each user/profile only have the visibility of
100 * phone accounts owned by them. Lets say, user B (settings) is requesting a list of phone accounts,
101 * and the list only contains phone accounts owned by user B and accounts with
102 * {@link PhoneAccount#CAPABILITY_MULTI_USER}.
Santos Cordon6a212642015-05-08 16:35:23 -0700103 *
104 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
105 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
106 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700107 */
Brad Ebinger53855132015-10-30 10:58:19 -0700108public class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700109
Yorke Lee5e8836a2014-08-22 15:25:18 -0700110 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
111 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
112
Ihab Awadb78b2762014-07-25 15:16:23 -0700113 public abstract static class Listener {
114 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
115 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
116 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
117 }
118
119 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700120 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000121 public static final int EXPECTED_STATE_VERSION = 9;
Tyler Gunn84253572014-09-02 14:50:05 -0700122
123 /** Keep in sync with the same in SipSettings.java */
124 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700125
126 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
127 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700128 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800129 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800130 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700131 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800132 private UserHandle mCurrentUserHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700133
Nancy Chen06ce0622014-10-23 01:17:35 +0000134 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700135 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700136 this(context, FILE_NAME);
137 }
138
139 @VisibleForTesting
140 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700141 // TODO: This file path is subject to change -- it is storing the phone account registry
142 // state file in the path /data/system/users/0/, which is likely not correct in a
143 // multi-user setting.
144 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
145 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
146 getAbsolutePath();
147 mAtomicFile = new AtomicFile(new File(filePath, fileName));
148 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700149 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700150
Ihab Awadb78b2762014-07-25 15:16:23 -0700151 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700152 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800153 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800154 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800155 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700156 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700157 }
158
Tyler Gunn84253572014-09-02 14:50:05 -0700159 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700160 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
161 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
162 * subscription id.
163 * @param accountHandle The handle for the phone account for which to retrieve the
164 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700165 * @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 -0700166 */
Wink Saville35850602014-10-23 15:57:21 -0700167 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak240656f2015-12-04 11:36:22 +0000168 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800169
Santos Cordon6a212642015-05-08 16:35:23 -0700170 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800171 TelephonyManager tm =
172 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
173 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700174 }
Santos Cordon479b3022015-02-06 04:27:15 -0800175 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700176 }
177
178 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800179 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
180 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
181 * will be returned.
182 *
Tyler Gunn84253572014-09-02 14:50:05 -0700183 * @param uriScheme The URI scheme for the outgoing call.
184 * @return The {@link PhoneAccountHandle} to use.
185 */
Tony Mak240656f2015-12-04 11:36:22 +0000186 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme,
187 UserHandle userHandle) {
188 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700189
Yorke Lee5e8836a2014-08-22 15:25:18 -0700190 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700191 // If there is a default PhoneAccount, ensure it supports calls to handles with the
192 // specified uriScheme.
Tony Mak240656f2015-12-04 11:36:22 +0000193 final PhoneAccount userSelectedAccount = getPhoneAccountUnchecked(userSelected);
Santos Cordon6a212642015-05-08 16:35:23 -0700194 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700195 return userSelected;
196 }
Ihab Awad293edf22014-07-24 17:52:29 -0700197 }
198
Tony Mak240656f2015-12-04 11:36:22 +0000199 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false,
200 userHandle);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700201 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700202 case 0:
203 // There are no accounts, so there can be no default
204 return null;
205 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800206 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700207 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700208 default:
209 // There are multiple accounts with no selected default
210 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700211 }
Ihab Awad104f8062014-07-17 11:29:35 -0700212 }
Santos Cordon176ae282014-07-14 02:02:14 -0700213
Tony Mak240656f2015-12-04 11:36:22 +0000214 public PhoneAccountHandle getOutgoingPhoneAccountForSchemeOfCurrentUser(String uriScheme) {
215 return getOutgoingPhoneAccountForScheme(uriScheme, mCurrentUserHandle);
216 }
217
Evan Charlton105d9772014-11-25 14:08:53 -0800218 /**
219 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
220 * if it was set by another user).
221 */
Hall Liuecda5542015-12-04 11:31:31 -0800222 @VisibleForTesting
223 public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount(UserHandle userHandle) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000224 if (userHandle == null) {
225 return null;
226 }
227 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
228 .get(userHandle);
229 if (defaultPhoneAccountHandle == null) {
230 return null;
231 }
232 // Make sure the account is still registered and owned by the user.
233 PhoneAccount account = getPhoneAccount(defaultPhoneAccountHandle.phoneAccountHandle,
234 userHandle);
Hall Liuecda5542015-12-04 11:31:31 -0800235
Santos Cordon6a212642015-05-08 16:35:23 -0700236 if (account != null) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000237 return defaultPhoneAccountHandle.phoneAccountHandle;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700238 }
239 return null;
240 }
241
Santos Cordon6a212642015-05-08 16:35:23 -0700242 /**
243 * Sets the phone account with which to place all calls by default. Set by the user
244 * within phone settings.
245 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000246 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle,
247 UserHandle userHandle) {
248 if (userHandle == null) {
249 return;
250 }
Evan Charlton89176372014-07-19 18:23:09 -0700251 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700252 // Asking to clear the default outgoing is a valid request
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000253 mState.defaultOutgoingAccountHandles.remove(userHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700254 } else {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000255 PhoneAccount account = getPhoneAccount(accountHandle, userHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700256 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700257 Log.w(this, "Trying to set nonexistent default outgoing %s",
258 accountHandle);
259 return;
260 }
261
Santos Cordon6a212642015-05-08 16:35:23 -0700262 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700263 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700264 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700265 return;
266 }
267
Santos Cordon6a212642015-05-08 16:35:23 -0700268 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700269 // If the account selected is a SIM account, propagate down to the subscription
270 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700271 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800272 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700273 }
274
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000275 mState.defaultOutgoingAccountHandles
276 .put(userHandle, new DefaultPhoneAccountHandle(userHandle, accountHandle));
Santos Cordon176ae282014-07-14 02:02:14 -0700277 }
278
Ihab Awad293edf22014-07-24 17:52:29 -0700279 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700280 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700281 }
282
Nancy Chen668dee02014-11-19 15:31:31 -0800283 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
Shishir Agrawalce688742016-01-25 15:02:21 -0800284 return getSubscriptionIdForPhoneAccount(accountHandle) ==
285 SubscriptionManager.getDefaultSmsSubscriptionId();
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700286 }
287
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700288 public ComponentName getSystemSimCallManagerComponent() {
289 String defaultSimCallManager = null;
290 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
291 Context.CARRIER_CONFIG_SERVICE);
292 PersistableBundle configBundle = configManager.getConfig();
293 if (configBundle != null) {
294 defaultSimCallManager = configBundle.getString(
295 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
296 }
297 return TextUtils.isEmpty(defaultSimCallManager)
298 ? null : ComponentName.unflattenFromString(defaultSimCallManager);
299 }
300
Tony Mak240656f2015-12-04 11:36:22 +0000301 public PhoneAccountHandle getSimCallManagerOfCurrentUser() {
302 return getSimCallManager(mCurrentUserHandle);
303 }
304
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700305 /**
306 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
307 * Manager. SIM Call Manager returned corresponds to the following priority order:
308 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
309 * default dialer, then that one is returned.
310 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
311 * carrier configuration's default, then that one is returned.
312 * 3. Otherwise, we return null.
313 */
Tony Mak240656f2015-12-04 11:36:22 +0000314 public PhoneAccountHandle getSimCallManager(UserHandle userHandle) {
Santos Cordon59c21a72015-06-11 10:11:21 -0700315 // Get the default dialer in case it has a connection manager associated with it.
Tony Mak240656f2015-12-04 11:36:22 +0000316 String dialerPackage = DefaultDialerManager
317 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700318
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700319 // Check carrier config.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700320 ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800321
Santos Cordon59c21a72015-06-11 10:11:21 -0700322 PhoneAccountHandle dialerSimCallManager = null;
323 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800324
Santos Cordon59c21a72015-06-11 10:11:21 -0700325 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
326 // loop through and look for any connection manager in the same package.
327 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
328 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
Tony Mak240656f2015-12-04 11:36:22 +0000329 true /* includeDisabledAccounts */, userHandle);
Santos Cordon59c21a72015-06-11 10:11:21 -0700330 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
331 ComponentName component = accountHandle.getComponentName();
332
333 // Store the system connection manager if found
334 if (systemSimCallManager == null
335 && Objects.equals(component, systemSimCallManagerComponent)
336 && !resolveComponent(accountHandle).isEmpty()) {
337 systemSimCallManager = accountHandle;
338
339 // Store the dialer connection manager if found
340 } else if (dialerSimCallManager == null
341 && Objects.equals(component.getPackageName(), dialerPackage)
342 && !resolveComponent(accountHandle).isEmpty()) {
343 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700344 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700345 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700346 }
347
Santos Cordon59c21a72015-06-11 10:11:21 -0700348 PhoneAccountHandle retval = dialerSimCallManager != null ?
349 dialerSimCallManager : systemSimCallManager;
350
351 Log.i(this, "SimCallManager queried, returning: %s", retval);
352
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700353 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700354 }
355
Evan Charlton105d9772014-11-25 14:08:53 -0800356 /**
Tony Mak240656f2015-12-04 11:36:22 +0000357 * If it is a outgoing call, sim call manager of call-initiating user is returned.
358 * Otherwise, we return the sim call manager of the user associated with the
359 * target phone account.
360 * @return phone account handle of sim call manager based on the ongoing call.
361 */
362 public PhoneAccountHandle getSimCallManagerFromCall(Call call) {
363 if (call == null) {
364 return null;
365 }
366 UserHandle userHandle = call.getInitiatingUser();
367 if (userHandle == null) {
368 userHandle = call.getTargetPhoneAccount().getUserHandle();
369 }
370 return getSimCallManager(userHandle);
371 }
372
373 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800374 * Update the current UserHandle to track when users are switched. This will allow the
375 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
376 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700377 * We cannot simply check the calling user because that would always return the primary user for
378 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800379 *
380 * @param userHandle The {@link UserHandle}, as delivered by
381 * {@link Intent#ACTION_USER_SWITCHED}.
382 */
383 public void setCurrentUserHandle(UserHandle userHandle) {
384 if (userHandle == null) {
385 Log.d(this, "setCurrentUserHandle, userHandle = null");
386 userHandle = Process.myUserHandle();
387 }
388 Log.d(this, "setCurrentUserHandle, %s", userHandle);
389 mCurrentUserHandle = userHandle;
390 }
391
Yorke Lee71734c22015-06-02 14:22:56 -0700392 /**
393 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
394 * otherwise.
395 */
396 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000397 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon638f05c2015-05-19 17:12:33 -0700398 if (account == null) {
399 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700400 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700401 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700402 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700403 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700404 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700405 }
406
Santos Cordon638f05c2015-05-19 17:12:33 -0700407 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700408 account.setIsEnabled(isEnabled);
Tony Mak48367162016-01-15 22:58:18 +0000409 if (!isEnabled) {
410 // If the disabled account is the default, remove it.
411 removeDefaultPhoneAccountHandle(accountHandle);
412 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700413 write();
414 fireAccountsChanged();
415 }
Yorke Lee71734c22015-06-02 14:22:56 -0700416 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700417 }
418
Tony Mak48367162016-01-15 22:58:18 +0000419 private void removeDefaultPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) {
420 Iterator<Map.Entry<UserHandle, DefaultPhoneAccountHandle>> iterator =
421 mState.defaultOutgoingAccountHandles.entrySet().iterator();
422 while (iterator.hasNext()) {
423 Map.Entry<UserHandle, DefaultPhoneAccountHandle> entry = iterator.next();
424 if (phoneAccountHandle.equals(entry.getValue().phoneAccountHandle)) {
425 iterator.remove();
426 }
427 }
428 }
429
Tony Mak240656f2015-12-04 11:36:22 +0000430 private boolean isVisibleForUser(PhoneAccount account, UserHandle userHandle,
431 boolean acrossProfiles) {
Evan Charlton105d9772014-11-25 14:08:53 -0800432 if (account == null) {
433 return false;
434 }
435
Tony Mak240656f2015-12-04 11:36:22 +0000436 if (userHandle == null) {
437 Log.w(this, "userHandle is null in isVisibleForUser");
438 return false;
439 }
440
Evan Charlton105d9772014-11-25 14:08:53 -0800441 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
442 // all profiles. Only Telephony and SIP accounts should have this capability.
443 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
444 return true;
445 }
446
447 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
448 if (phoneAccountUserHandle == null) {
449 return false;
450 }
451
452 if (mCurrentUserHandle == null) {
Tony Mak240656f2015-12-04 11:36:22 +0000453 // In case we need to have emergency phone calls from the lock screen.
Evan Charlton105d9772014-11-25 14:08:53 -0800454 Log.d(this, "Current user is null; assuming true");
455 return true;
456 }
457
Tony Mak240656f2015-12-04 11:36:22 +0000458 if (acrossProfiles) {
459 return UserManager.get(mContext).isSameProfileGroup(userHandle.getIdentifier(),
460 phoneAccountUserHandle.getIdentifier());
461 } else {
462 return phoneAccountUserHandle.equals(userHandle);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800463 }
Evan Charlton105d9772014-11-25 14:08:53 -0800464 }
465
466 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
467 return resolveComponent(phoneAccountHandle.getComponentName(),
Tony Mak240656f2015-12-04 11:36:22 +0000468 phoneAccountHandle.getUserHandle());
Evan Charlton105d9772014-11-25 14:08:53 -0800469 }
470
471 private List<ResolveInfo> resolveComponent(ComponentName componentName,
472 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800473 PackageManager pm = mContext.getPackageManager();
474 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
475 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800476 try {
477 if (userHandle != null) {
478 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
479 } else {
480 return pm.queryIntentServices(intent, 0);
481 }
482 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700483 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800484 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800485 }
mike dooley10a58312014-11-06 13:46:19 -0800486 }
487
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700488 /**
489 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700490 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700491 *
492 * @return The list of {@link PhoneAccountHandle}s.
493 */
Tony Mak240656f2015-12-04 11:36:22 +0000494 public List<PhoneAccountHandle> getAllPhoneAccountHandles(UserHandle userHandle) {
495 return getPhoneAccountHandles(0, null, null, false, userHandle);
Ihab Awad293edf22014-07-24 17:52:29 -0700496 }
497
Tony Mak240656f2015-12-04 11:36:22 +0000498 public List<PhoneAccount> getAllPhoneAccounts(UserHandle userHandle) {
499 return getPhoneAccounts(0, null, null, false, userHandle);
500 }
501
502 public List<PhoneAccount> getAllPhoneAccountsOfCurrentUser() {
503 return getAllPhoneAccounts(mCurrentUserHandle);
Santos Cordonafe59e52014-08-22 16:48:43 -0700504 }
505
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700506 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700507 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700508 * specified URI scheme.
509 *
510 * @param uriScheme The URI scheme.
511 * @return The phone account handles.
512 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700513 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
Tony Mak240656f2015-12-04 11:36:22 +0000514 String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700515 return getPhoneAccountHandles(
Bryce Leea0bb7052015-10-16 13:31:40 -0700516 PhoneAccount.CAPABILITY_CALL_PROVIDER,
517 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/,
Tony Mak240656f2015-12-04 11:36:22 +0000518 uriScheme, null, includeDisabledAccounts, userHandle);
519 }
520
521 public List<PhoneAccountHandle> getCallCapablePhoneAccountsOfCurrentUser(
522 String uriScheme, boolean includeDisabledAccounts) {
523 return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, mCurrentUserHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700524 }
525
526 /**
527 * Retrieves a list of all the SIM-based phone accounts.
528 */
Tony Mak240656f2015-12-04 11:36:22 +0000529 public List<PhoneAccountHandle> getSimPhoneAccounts(UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700530 return getPhoneAccountHandles(
531 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Tony Mak240656f2015-12-04 11:36:22 +0000532 null, null, false, userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700533 }
534
Tony Mak240656f2015-12-04 11:36:22 +0000535 public List<PhoneAccountHandle> getSimPhoneAccountsOfCurrentUser() {
536 return getSimPhoneAccounts(mCurrentUserHandle);
537 }
538
539 /**
540 * Retrieves a list of all phone accounts registered by a specified package.
541 *
542 * @param packageName The name of the package that registered the phone accounts.
543 * @return The phone account handles.
544 */
545 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName,
546 UserHandle userHandle) {
547 return getPhoneAccountHandles(0, null, packageName, false, userHandle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700548 }
549
Ihab Awad104f8062014-07-17 11:29:35 -0700550 // TODO: Should we implement an artificial limit for # of accounts associated with a single
551 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700552 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700553 // Enforce the requirement that a connection service for a phone account has the correct
554 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700555 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700556 Log.w(this,
557 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700558 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700559 throw new SecurityException("PhoneAccount connection service requires "
560 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700561 }
562
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700563 addOrReplacePhoneAccount(account);
564 }
565
566 /**
567 * Adds a {@code PhoneAccount}, replacing an existing one if found.
568 *
569 * @param account The {@code PhoneAccount} to add or replace.
570 */
571 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800572 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
573 account.getAccountHandle(), account);
574
Santos Cordonea5cb932015-05-07 16:28:38 -0700575 // Start _enabled_ property as false.
576 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
577 // source app provides or else an third party app could enable itself.
578 boolean isEnabled = false;
579
Tony Mak98e6bdc2015-11-25 21:54:05 +0000580 PhoneAccount oldAccount = getPhoneAccountUnchecked(account.getAccountHandle());
Santos Cordon6a212642015-05-08 16:35:23 -0700581 if (oldAccount != null) {
582 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700583 isEnabled = oldAccount.isEnabled();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700584 Log.i(this, getAccountDiffString(account, oldAccount));
585 } else {
586 Log.i(this, "New phone account registered: " + account);
Santos Cordon176ae282014-07-14 02:02:14 -0700587 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700588
Santos Cordon6a212642015-05-08 16:35:23 -0700589 mState.accounts.add(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700590 // Reset enabled state to whatever the value was if the account was already registered,
591 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled.
592 account.setIsEnabled(
593 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION));
Santos Cordon176ae282014-07-14 02:02:14 -0700594
Ihab Awad293edf22014-07-24 17:52:29 -0700595 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700596 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700597 }
598
Evan Charlton89176372014-07-19 18:23:09 -0700599 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000600 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700601 if (account != null) {
602 if (mState.accounts.remove(account)) {
603 write();
604 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700605 }
606 }
Santos Cordon176ae282014-07-14 02:02:14 -0700607 }
608
Tyler Gunnd900ce62014-08-13 11:40:59 -0700609 /**
610 * Un-registers all phone accounts associated with a specified package.
611 *
612 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800613 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700614 */
Evan Charlton105d9772014-11-25 14:08:53 -0800615 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700616 boolean accountsRemoved = false;
617 Iterator<PhoneAccount> it = mState.accounts.iterator();
618 while (it.hasNext()) {
619 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800620 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
621 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
622 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700623 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530624 mState.accounts.remove(phoneAccount);
Tyler Gunnd900ce62014-08-13 11:40:59 -0700625 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700626 }
627 }
628
Tyler Gunnd900ce62014-08-13 11:40:59 -0700629 if (accountsRemoved) {
630 write();
631 fireAccountsChanged();
632 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700633 }
634
Nancy Chen140004a2014-10-15 15:48:38 -0700635 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700636 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Yorke Leeb1957232015-06-30 10:09:54 -0700637 return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number);
Nancy Chen140004a2014-10-15 15:48:38 -0700638 }
639
Ihab Awadb78b2762014-07-25 15:16:23 -0700640 public void addListener(Listener l) {
641 mListeners.add(l);
642 }
643
644 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700645 if (l != null) {
646 mListeners.remove(l);
647 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700648 }
649
650 private void fireAccountsChanged() {
651 for (Listener l : mListeners) {
652 l.onAccountsChanged(this);
653 }
654 }
655
656 private void fireDefaultOutgoingChanged() {
657 for (Listener l : mListeners) {
658 l.onDefaultOutgoingChanged(this);
659 }
660 }
661
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700662 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
663 if (account1 == null || account2 == null) {
664 return "Diff: " + account1 + ", " + account2;
665 }
666
667 StringBuffer sb = new StringBuffer();
668 sb.append("[").append(account1.getAccountHandle());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700669 appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()),
670 Log.piiHandle(account2.getAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700671 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
672 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
673 appendDiff(sb, "icon", account1.getIcon(), account2.getIcon());
674 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
675 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700676 appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()),
677 Log.piiHandle(account2.getSubscriptionAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700678 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
679 account2.getSupportedUriSchemes());
680 sb.append("]");
681 return sb.toString();
682 }
683
684 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
685 if (!Objects.equals(obj1, obj2)) {
686 sb.append("(")
687 .append(attrName)
688 .append(": ")
689 .append(obj1)
690 .append(" -> ")
691 .append(obj2)
692 .append(")");
693 }
694 }
695
Tyler Gunncb59b672014-08-20 09:02:11 -0700696 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700697 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700698 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700699 *
700 * @param phoneAccountHandle The phone account to check.
701 * @return {@code True} if the phone account has permission.
702 */
Santos Cordon6a212642015-05-08 16:35:23 -0700703 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800704 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
705 if (resolveInfos.isEmpty()) {
706 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700707 return false;
708 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800709 for (ResolveInfo resolveInfo : resolveInfos) {
710 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700711 if (serviceInfo == null) {
712 return false;
713 }
714
715 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
716 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
717 serviceInfo.permission)) {
718 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
719 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
720 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800721 return false;
722 }
723 }
724 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700725 }
726
Santos Cordon6a212642015-05-08 16:35:23 -0700727 //
728 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
729 //
Ihab Awad293edf22014-07-24 17:52:29 -0700730
Santos Cordonafe59e52014-08-22 16:48:43 -0700731 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700732 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700733 *
Santos Cordon6a212642015-05-08 16:35:23 -0700734 * @param handle
735 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700736 */
Tony Mak98e6bdc2015-11-25 21:54:05 +0000737 public PhoneAccount getPhoneAccountUnchecked(PhoneAccountHandle handle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700738 for (PhoneAccount m : mState.accounts) {
739 if (Objects.equals(handle, m.getAccountHandle())) {
740 return m;
741 }
742 }
743 return null;
744 }
745
746 /**
747 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
748 * account before returning it. The current user is the active user on the actual android
749 * device.
750 */
Tony Mak240656f2015-12-04 11:36:22 +0000751 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, UserHandle userHandle) {
752 return getPhoneAccount(handle, userHandle, /* acrossProfiles */ false);
753 }
754
755 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle,
756 UserHandle userHandle, boolean acrossProfiles) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000757 PhoneAccount account = getPhoneAccountUnchecked(handle);
Tony Mak240656f2015-12-04 11:36:22 +0000758 if (account != null && (isVisibleForUser(account, userHandle, acrossProfiles))) {
Santos Cordon6a212642015-05-08 16:35:23 -0700759 return account;
760 }
761 return null;
762 }
763
Tony Mak240656f2015-12-04 11:36:22 +0000764 public PhoneAccount getPhoneAccountOfCurrentUser(PhoneAccountHandle handle) {
765 return getPhoneAccount(handle, mCurrentUserHandle);
766 }
767
Bryce Leea0bb7052015-10-16 13:31:40 -0700768 private List<PhoneAccountHandle> getPhoneAccountHandles(
769 int capabilities,
770 String uriScheme,
771 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000772 boolean includeDisabledAccounts,
773 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700774 return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme,
Tony Mak240656f2015-12-04 11:36:22 +0000775 packageName, includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700776 }
777
Santos Cordon6a212642015-05-08 16:35:23 -0700778 /**
779 * Returns a list of phone account handles with the specified capabilities, uri scheme,
780 * and package name.
781 */
782 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700783 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700784 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700785 String uriScheme,
786 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000787 boolean includeDisabledAccounts,
788 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700789 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700790
791 for (PhoneAccount account : getPhoneAccounts(
Bryce Leea0bb7052015-10-16 13:31:40 -0700792 capabilities, excludedCapabilities, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000793 includeDisabledAccounts, userHandle)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700794 handles.add(account.getAccountHandle());
795 }
796 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700797 }
798
Bryce Leea0bb7052015-10-16 13:31:40 -0700799 private List<PhoneAccount> getPhoneAccounts(
800 int capabilities,
801 String uriScheme,
802 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000803 boolean includeDisabledAccounts,
804 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700805 return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000806 includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700807 }
808
Tyler Gunn84253572014-09-02 14:50:05 -0700809 /**
810 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700811 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700812 *
Santos Cordon6a212642015-05-08 16:35:23 -0700813 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
Bryce Leea0bb7052015-10-16 13:31:40 -0700814 * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have.
815 * Ignored if 0.
Santos Cordon6a212642015-05-08 16:35:23 -0700816 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700817 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700818 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700819 */
Santos Cordon6a212642015-05-08 16:35:23 -0700820 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700821 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700822 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700823 String uriScheme,
824 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000825 boolean includeDisabledAccounts,
826 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700827 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700828 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700829 if (!(m.isEnabled() || includeDisabledAccounts)) {
830 // Do not include disabled accounts.
831 continue;
832 }
833
Bryce Leea0bb7052015-10-16 13:31:40 -0700834 if ((m.getCapabilities() & excludedCapabilities) != 0) {
835 // If an excluded capability is present, skip.
836 continue;
837 }
838
Santos Cordon6a212642015-05-08 16:35:23 -0700839 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800840 // Account doesn't have the right capabilities; skip this one.
841 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700842 }
Evan Charlton105d9772014-11-25 14:08:53 -0800843 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
844 // Account doesn't support this URI scheme; skip this one.
845 continue;
846 }
Santos Cordon6a212642015-05-08 16:35:23 -0700847 PhoneAccountHandle handle = m.getAccountHandle();
848
849 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800850 // This component cannot be resolved anymore; skip this one.
851 continue;
852 }
Santos Cordon6a212642015-05-08 16:35:23 -0700853 if (packageName != null &&
854 !packageName.equals(handle.getComponentName().getPackageName())) {
855 // Not the right package name; skip this one.
856 continue;
857 }
Tony Mak240656f2015-12-04 11:36:22 +0000858 if (!isVisibleForUser(m, userHandle, false)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800859 // Account is not visible for the current user; skip this one.
860 continue;
861 }
Santos Cordon6a212642015-05-08 16:35:23 -0700862 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700863 }
Santos Cordon6a212642015-05-08 16:35:23 -0700864 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700865 }
866
Santos Cordon6a212642015-05-08 16:35:23 -0700867 //
868 // State Implementation for PhoneAccountRegistrar
869 //
870
Ihab Awad293edf22014-07-24 17:52:29 -0700871 /**
872 * The state of this {@code PhoneAccountRegistrar}.
873 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700874 @VisibleForTesting
875 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700876 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000877 * Store the default phone account handle of users. If no record of a user can be found in
878 * the map, it means that no default phone account handle is set in that user.
Ihab Awad293edf22014-07-24 17:52:29 -0700879 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000880 public final Map<UserHandle, DefaultPhoneAccountHandle> defaultOutgoingAccountHandles
881 = new ConcurrentHashMap<>();
Ihab Awad293edf22014-07-24 17:52:29 -0700882
883 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700884 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700885 */
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530886 public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700887
888 /**
889 * The version number of the State data.
890 */
891 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700892 }
893
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700894 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000895 * The default {@link PhoneAccountHandle} of a user.
896 */
897 public static class DefaultPhoneAccountHandle {
898
899 public final UserHandle userHandle;
900
901 public final PhoneAccountHandle phoneAccountHandle;
902
903 public DefaultPhoneAccountHandle(UserHandle userHandle,
904 PhoneAccountHandle phoneAccountHandle) {
905 this.userHandle = userHandle;
906 this.phoneAccountHandle = phoneAccountHandle;
907 }
908 }
909
910 /**
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700911 * Dumps the state of the {@link CallsManager}.
912 *
913 * @param pw The {@code IndentingPrintWriter} to write the state to.
914 */
915 public void dump(IndentingPrintWriter pw) {
916 if (mState != null) {
917 pw.println("xmlVersion: " + mState.versionNumber);
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000918 DefaultPhoneAccountHandle defaultPhoneAccountHandle
919 = mState.defaultOutgoingAccountHandles.get(Process.myUserHandle());
920 pw.println("defaultOutgoing: " + (defaultPhoneAccountHandle == null ? "none" :
921 defaultPhoneAccountHandle.phoneAccountHandle));
Tony Mak240656f2015-12-04 11:36:22 +0000922 pw.println("simCallManager: " + getSimCallManager(mCurrentUserHandle));
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700923 pw.println("phoneAccounts:");
924 pw.increaseIndent();
925 for (PhoneAccount phoneAccount : mState.accounts) {
926 pw.println(phoneAccount);
927 }
928 pw.decreaseIndent();
929 }
930 }
931
Ihab Awad293edf22014-07-24 17:52:29 -0700932 ////////////////////////////////////////////////////////////////////////////////////////////////
933 //
934 // State management
935 //
936
937 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700938 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700939 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700940 os = mAtomicFile.startWrite();
941 boolean success = false;
942 try {
943 XmlSerializer serializer = new FastXmlSerializer();
944 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800945 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700946 serializer.flush();
947 success = true;
948 } finally {
949 if (success) {
950 mAtomicFile.finishWrite(os);
951 } else {
952 mAtomicFile.failWrite(os);
953 }
954 }
955 } catch (IOException e) {
956 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700957 }
958 }
959
Ihab Awadb78b2762014-07-25 15:16:23 -0700960 private void read() {
961 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700962 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700963 is = mAtomicFile.openRead();
964 } catch (FileNotFoundException ex) {
965 return;
966 }
967
Tyler Gunn84253572014-09-02 14:50:05 -0700968 boolean versionChanged = false;
969
Ihab Awadb78b2762014-07-25 15:16:23 -0700970 XmlPullParser parser;
971 try {
972 parser = Xml.newPullParser();
973 parser.setInput(new BufferedInputStream(is), null);
974 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700975 mState = readFromXml(parser, mContext);
976 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
977
Ihab Awadb78b2762014-07-25 15:16:23 -0700978 } catch (IOException | XmlPullParserException e) {
979 Log.e(this, e, "Reading state from XML file");
980 mState = new State();
981 } finally {
982 try {
983 is.close();
984 } catch (IOException e) {
985 Log.e(this, e, "Closing InputStream");
986 }
Ihab Awad104f8062014-07-17 11:29:35 -0700987 }
Tyler Gunn84253572014-09-02 14:50:05 -0700988
Evan Charlton105d9772014-11-25 14:08:53 -0800989 // Verify all of the UserHandles.
990 List<PhoneAccount> badAccounts = new ArrayList<>();
991 for (PhoneAccount phoneAccount : mState.accounts) {
992 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
993 if (userHandle == null) {
994 Log.w(this, "Missing UserHandle for %s", phoneAccount);
995 badAccounts.add(phoneAccount);
996 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
997 Log.w(this, "User does not exist for %s", phoneAccount);
998 badAccounts.add(phoneAccount);
999 }
1000 }
1001 mState.accounts.removeAll(badAccounts);
1002
Tyler Gunn84253572014-09-02 14:50:05 -07001003 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -08001004 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -07001005 write();
1006 }
Santos Cordon176ae282014-07-14 02:02:14 -07001007 }
1008
Evan Charlton105d9772014-11-25 14:08:53 -08001009 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001010 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -08001011 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -07001012 }
Ihab Awad104f8062014-07-17 11:29:35 -07001013
Tyler Gunn84253572014-09-02 14:50:05 -07001014 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001015 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -07001016 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001017 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -07001018 }
1019
Ihab Awad293edf22014-07-24 17:52:29 -07001020 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -07001021 //
Ihab Awadb78b2762014-07-25 15:16:23 -07001022 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -07001023 //
1024
Ihab Awadb78b2762014-07-25 15:16:23 -07001025 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -07001026 public abstract static class XmlSerialization<T> {
Tyler Gunn369c8742015-10-07 15:39:28 -07001027 private static final String TAG_VALUE = "value";
1028 private static final String ATTRIBUTE_LENGTH = "length";
1029 private static final String ATTRIBUTE_KEY = "key";
1030 private static final String ATTRIBUTE_VALUE_TYPE = "type";
1031 private static final String VALUE_TYPE_STRING = "string";
1032 private static final String VALUE_TYPE_INTEGER = "integer";
1033 private static final String VALUE_TYPE_BOOLEAN = "boolean";
Tyler Gunn84253572014-09-02 14:50:05 -07001034
Ihab Awadb78b2762014-07-25 15:16:23 -07001035 /**
1036 * Write the supplied object to XML
1037 */
Evan Charlton105d9772014-11-25 14:08:53 -08001038 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001039 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -07001040
1041 /**
1042 * Read from the supplied XML into a new object, returning null in case of an
1043 * unrecoverable schema mismatch or other data error. 'parser' must be already
1044 * positioned at the first tag that is expected to have been emitted by this
1045 * object's writeToXml(). This object tries to fail early without modifying
1046 * 'parser' if it does not recognize the data it sees.
1047 */
Tyler Gunn84253572014-09-02 14:50:05 -07001048 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001049 throws IOException, XmlPullParserException;
1050
Ihab Awadd9f54382014-10-24 11:44:47 -07001051 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -07001052 throws IOException {
1053 if (value != null) {
1054 serializer.startTag(null, tagName);
1055 serializer.text(Objects.toString(value));
1056 serializer.endTag(null, tagName);
1057 }
1058 }
Tyler Gunn84253572014-09-02 14:50:05 -07001059
1060 /**
1061 * Serializes a string array.
1062 *
1063 * @param tagName The tag name for the string array.
1064 * @param values The string values to serialize.
1065 * @param serializer The serializer.
1066 * @throws IOException
1067 */
1068 protected void writeStringList(String tagName, List<String> values,
1069 XmlSerializer serializer)
1070 throws IOException {
1071
1072 serializer.startTag(null, tagName);
1073 if (values != null) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001074 serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size()));
Tyler Gunn84253572014-09-02 14:50:05 -07001075 for (String toSerialize : values) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001076 serializer.startTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001077 if (toSerialize != null ){
1078 serializer.text(toSerialize);
1079 }
Tyler Gunn369c8742015-10-07 15:39:28 -07001080 serializer.endTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001081 }
1082 } else {
Tyler Gunn369c8742015-10-07 15:39:28 -07001083 serializer.attribute(null, ATTRIBUTE_LENGTH, "0");
1084 }
1085 serializer.endTag(null, tagName);
1086 }
1087
1088 protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer)
1089 throws IOException {
1090
1091 serializer.startTag(null, tagName);
1092 if (values != null) {
1093 for (String key : values.keySet()) {
1094 Object value = values.get(key);
1095
1096 if (value == null) {
1097 continue;
1098 }
1099
1100 String valueType;
1101 if (value instanceof String) {
1102 valueType = VALUE_TYPE_STRING;
1103 } else if (value instanceof Integer) {
1104 valueType = VALUE_TYPE_INTEGER;
1105 } else if (value instanceof Boolean) {
1106 valueType = VALUE_TYPE_BOOLEAN;
1107 } else {
1108 Log.w(this,
1109 "PhoneAccounts support only string, integer and boolean extras TY.");
1110 continue;
1111 }
1112
1113 serializer.startTag(null, TAG_VALUE);
1114 serializer.attribute(null, ATTRIBUTE_KEY, key);
1115 serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType);
1116 serializer.text(Objects.toString(value));
1117 serializer.endTag(null, TAG_VALUE);
1118 }
Tyler Gunn84253572014-09-02 14:50:05 -07001119 }
1120 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -07001121 }
Tyler Gunn84253572014-09-02 14:50:05 -07001122
Santos Cordon9c30c282015-05-13 16:28:27 -07001123 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -07001124 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001125 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001126 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001127 value.writeToStream(stream);
1128 byte[] iconByteArray = stream.toByteArray();
1129 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001130
1131 serializer.startTag(null, tagName);
1132 serializer.text(text);
1133 serializer.endTag(null, tagName);
1134 }
Tyler Gunn84253572014-09-02 14:50:05 -07001135 }
1136
Evan Charlton105d9772014-11-25 14:08:53 -08001137 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1138 throws IOException {
1139 serializer.startTag(null, tagName);
1140 serializer.text(Long.valueOf(value).toString());
1141 serializer.endTag(null, tagName);
1142 }
1143
Tyler Gunn84253572014-09-02 14:50:05 -07001144 /**
1145 * Reads a string array from the XML parser.
1146 *
1147 * @param parser The XML parser.
1148 * @return String array containing the parsed values.
1149 * @throws IOException Exception related to IO.
1150 * @throws XmlPullParserException Exception related to parsing.
1151 */
1152 protected List<String> readStringList(XmlPullParser parser)
1153 throws IOException, XmlPullParserException {
1154
Tyler Gunn369c8742015-10-07 15:39:28 -07001155 int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH));
Tyler Gunn84253572014-09-02 14:50:05 -07001156 List<String> arrayEntries = new ArrayList<String>(length);
1157 String value = null;
1158
1159 if (length == 0) {
1160 return arrayEntries;
1161 }
1162
1163 int outerDepth = parser.getDepth();
1164 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001165 if (parser.getName().equals(TAG_VALUE)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001166 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001167 value = parser.getText();
1168 arrayEntries.add(value);
1169 }
1170 }
1171
1172 return arrayEntries;
1173 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001174
Tyler Gunn369c8742015-10-07 15:39:28 -07001175 /**
1176 * Reads a bundle from the XML parser.
1177 *
1178 * @param parser The XML parser.
1179 * @return Bundle containing the parsed values.
1180 * @throws IOException Exception related to IO.
1181 * @throws XmlPullParserException Exception related to parsing.
1182 */
1183 protected Bundle readBundle(XmlPullParser parser)
1184 throws IOException, XmlPullParserException {
1185
1186 Bundle bundle = null;
1187 int outerDepth = parser.getDepth();
1188 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1189 if (parser.getName().equals(TAG_VALUE)) {
1190 String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE);
1191 String key = parser.getAttributeValue(null, ATTRIBUTE_KEY);
1192 parser.next();
1193 String value = parser.getText();
1194
1195 if (bundle == null) {
1196 bundle = new Bundle();
1197 }
1198
1199 // Do not write null values to the bundle.
1200 if (value == null) {
1201 continue;
1202 }
1203
1204 if (VALUE_TYPE_STRING.equals(valueType)) {
1205 bundle.putString(key, value);
1206 } else if (VALUE_TYPE_INTEGER.equals(valueType)) {
1207 try {
1208 int intValue = Integer.parseInt(value);
1209 bundle.putInt(key, intValue);
1210 } catch (NumberFormatException nfe) {
1211 Log.w(this, "Invalid integer PhoneAccount extra.");
1212 }
1213 } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) {
1214 boolean boolValue = Boolean.parseBoolean(value);
1215 bundle.putBoolean(key, boolValue);
1216 } else {
1217 Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle.");
1218 }
1219 }
1220 }
1221 return bundle;
1222 }
1223
Santos Cordon9c30c282015-05-13 16:28:27 -07001224 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001225 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1226 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1227 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001228
1229 protected Icon readIcon(XmlPullParser parser) throws IOException {
1230 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1231 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1232 return Icon.createFromStream(stream);
1233 }
Ihab Awad104f8062014-07-17 11:29:35 -07001234 }
1235
Ihab Awadb78b2762014-07-25 15:16:23 -07001236 @VisibleForTesting
1237 public static final XmlSerialization<State> sStateXml =
1238 new XmlSerialization<State>() {
1239 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001240 private static final String DEFAULT_OUTGOING = "default_outgoing";
1241 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001242 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001243
1244 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001245 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001246 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001247 if (o != null) {
1248 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001249 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001250
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001251 serializer.startTag(null, DEFAULT_OUTGOING);
1252 for (DefaultPhoneAccountHandle defaultPhoneAccountHandle : o
1253 .defaultOutgoingAccountHandles.values()) {
1254 sDefaultPhoneAcountHandleXml
1255 .writeToXml(defaultPhoneAccountHandle, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001256 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001257 serializer.endTag(null, DEFAULT_OUTGOING);
Ihab Awad26923222014-07-30 10:54:35 -07001258
Ihab Awad26923222014-07-30 10:54:35 -07001259 serializer.startTag(null, ACCOUNTS);
1260 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001261 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001262 }
1263 serializer.endTag(null, ACCOUNTS);
1264
1265 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001266 }
Ihab Awad104f8062014-07-17 11:29:35 -07001267 }
1268
1269 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001270 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001271 throws IOException, XmlPullParserException {
1272 if (parser.getName().equals(CLASS_STATE)) {
1273 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001274
1275 String rawVersion = parser.getAttributeValue(null, VERSION);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001276 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : Integer.parseInt(rawVersion);
Tyler Gunn84253572014-09-02 14:50:05 -07001277
Ihab Awadb78b2762014-07-25 15:16:23 -07001278 int outerDepth = parser.getDepth();
1279 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1280 if (parser.getName().equals(DEFAULT_OUTGOING)) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001281 if (s.versionNumber < 9) {
1282 // Migration old default phone account handle here by assuming the
1283 // default phone account handle is belong to primary user.
1284 parser.nextTag();
1285 PhoneAccountHandle phoneAccountHandle = sPhoneAccountHandleXml
1286 .readFromXml(parser, s.versionNumber, context);
1287 UserManager userManager = UserManager.get(context);
1288 UserInfo primaryUser = userManager.getPrimaryUser();
1289 if (primaryUser != null) {
1290 UserHandle userHandle = primaryUser.getUserHandle();
1291 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1292 = new DefaultPhoneAccountHandle(userHandle,
1293 phoneAccountHandle);
1294 s.defaultOutgoingAccountHandles
1295 .put(userHandle, defaultPhoneAccountHandle);
1296 }
1297 } else {
1298 int defaultAccountHandlesDepth = parser.getDepth();
1299 while (XmlUtils.nextElementWithin(parser, defaultAccountHandlesDepth)) {
1300 DefaultPhoneAccountHandle accountHandle
1301 = sDefaultPhoneAcountHandleXml
1302 .readFromXml(parser, s.versionNumber, context);
1303 if (accountHandle != null && s.accounts != null) {
1304 s.defaultOutgoingAccountHandles
1305 .put(accountHandle.userHandle, accountHandle);
1306 }
1307 }
1308 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001309 } else if (parser.getName().equals(ACCOUNTS)) {
1310 int accountsDepth = parser.getDepth();
1311 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001312 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1313 s.versionNumber, context);
1314
1315 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001316 s.accounts.add(account);
1317 }
1318 }
Ihab Awad104f8062014-07-17 11:29:35 -07001319 }
1320 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001321 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001322 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001323 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001324 }
1325 };
1326
Ihab Awadb78b2762014-07-25 15:16:23 -07001327 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001328 public static final XmlSerialization<DefaultPhoneAccountHandle> sDefaultPhoneAcountHandleXml =
1329 new XmlSerialization<DefaultPhoneAccountHandle>() {
1330 private static final String CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE
1331 = "default_outgoing_phone_account_handle";
1332 private static final String USER_SERIAL_NUMBER = "user_serial_number";
1333 private static final String ACCOUNT_HANDLE = "account_handle";
1334
1335 @Override
1336 public void writeToXml(DefaultPhoneAccountHandle o, XmlSerializer serializer,
1337 Context context) throws IOException {
1338 if (o != null) {
1339 final UserManager userManager = UserManager.get(context);
1340 final long serialNumber = userManager.getSerialNumberForUser(o.userHandle);
1341 if (serialNumber != -1) {
1342 serializer.startTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1343 writeLong(USER_SERIAL_NUMBER, serialNumber, serializer);
1344 serializer.startTag(null, ACCOUNT_HANDLE);
1345 sPhoneAccountHandleXml.writeToXml(o.phoneAccountHandle, serializer,
1346 context);
1347 serializer.endTag(null, ACCOUNT_HANDLE);
1348 serializer.endTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1349 }
1350 }
1351 }
1352
1353 @Override
1354 public DefaultPhoneAccountHandle readFromXml(XmlPullParser parser, int version,
1355 Context context)
1356 throws IOException, XmlPullParserException {
1357 if (parser.getName().equals(CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE)) {
1358 int outerDepth = parser.getDepth();
1359 PhoneAccountHandle accountHandle = null;
1360 String userSerialNumberString = null;
1361 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1362 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1363 parser.nextTag();
1364 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1365 context);
1366 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1367 parser.next();
1368 userSerialNumberString = parser.getText();
1369 }
1370 }
1371 UserHandle userHandle = null;
1372 if (userSerialNumberString != null) {
1373 try {
1374 long serialNumber = Long.parseLong(userSerialNumberString);
1375 userHandle = UserManager.get(context)
1376 .getUserForSerialNumber(serialNumber);
1377 } catch (NumberFormatException e) {
1378 Log.e(this, e,
1379 "Could not parse UserHandle " + userSerialNumberString);
1380 }
1381 }
1382 if (accountHandle != null && userHandle != null) {
1383 return new DefaultPhoneAccountHandle(userHandle, accountHandle);
1384 }
1385 }
1386 return null;
1387 }
1388 };
1389
1390
1391 @VisibleForTesting
Ihab Awadb78b2762014-07-25 15:16:23 -07001392 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1393 new XmlSerialization<PhoneAccount>() {
1394 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1395 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001396 private static final String ADDRESS = "handle";
1397 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001398 private static final String CAPABILITIES = "capabilities";
1399 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001400 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1401 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001402 private static final String ICON_TINT = "icon_tint";
1403 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001404 private static final String LABEL = "label";
1405 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001406 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001407 private static final String ICON = "icon";
Tyler Gunn369c8742015-10-07 15:39:28 -07001408 private static final String EXTRAS = "extras";
Santos Cordonea5cb932015-05-07 16:28:38 -07001409 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001410
1411 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001412 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001413 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001414 if (o != null) {
1415 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001416
Ihab Awad26923222014-07-30 10:54:35 -07001417 if (o.getAccountHandle() != null) {
1418 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001419 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001420 serializer.endTag(null, ACCOUNT_HANDLE);
1421 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001422
Ihab Awadd9f54382014-10-24 11:44:47 -07001423 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1424 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1425 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001426 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001427 writeTextIfNonNull(HIGHLIGHT_COLOR,
1428 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001429 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1430 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001431 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Tyler Gunn369c8742015-10-07 15:39:28 -07001432 writeBundle(EXTRAS, o.getExtras(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001433 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001434
Ihab Awad26923222014-07-30 10:54:35 -07001435 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1436 }
Ihab Awad104f8062014-07-17 11:29:35 -07001437 }
1438
Tyler Gunn84253572014-09-02 14:50:05 -07001439 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001440 throws IOException, XmlPullParserException {
1441 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1442 int outerDepth = parser.getDepth();
1443 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001444 Uri address = null;
1445 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001446 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001447 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001448 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001449 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001450 int iconTint = PhoneAccount.NO_ICON_TINT;
1451 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001452 String label = null;
1453 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001454 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001455 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001456 boolean enabled = false;
Tyler Gunn369c8742015-10-07 15:39:28 -07001457 Bundle extras = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001458
1459 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1460 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1461 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001462 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1463 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001464 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001465 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001466 address = Uri.parse(parser.getText());
1467 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001468 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001469 String nextText = parser.getText();
1470 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001471 } else if (parser.getName().equals(CAPABILITIES)) {
1472 parser.next();
1473 capabilities = Integer.parseInt(parser.getText());
1474 } else if (parser.getName().equals(ICON_RES_ID)) {
1475 parser.next();
1476 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001477 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1478 parser.next();
1479 iconPackageName = parser.getText();
1480 } else if (parser.getName().equals(ICON_BITMAP)) {
1481 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001482 iconBitmap = readBitmap(parser);
1483 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001484 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001485 iconTint = Integer.parseInt(parser.getText());
1486 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1487 parser.next();
1488 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001489 } else if (parser.getName().equals(LABEL)) {
1490 parser.next();
1491 label = parser.getText();
1492 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1493 parser.next();
1494 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001495 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1496 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001497 } else if (parser.getName().equals(ICON)) {
1498 parser.next();
1499 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001500 } else if (parser.getName().equals(ENABLED)) {
1501 parser.next();
1502 enabled = "true".equalsIgnoreCase(parser.getText());
Tyler Gunn369c8742015-10-07 15:39:28 -07001503 } else if (parser.getName().equals(EXTRAS)) {
1504 parser.next();
1505 extras = readBundle(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -07001506 }
1507 }
Tyler Gunn84253572014-09-02 14:50:05 -07001508
Santos Cordona9eebe42015-06-11 14:07:44 -07001509 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1510 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001511 ComponentName sipComponentName = new ComponentName("com.android.phone",
1512 "com.android.services.telephony.sip.SipConnectionService");
1513
Tyler Gunn84253572014-09-02 14:50:05 -07001514 // Upgrade older phone accounts to specify the supported URI schemes.
1515 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001516 supportedUriSchemes = new ArrayList<>();
1517
1518 // Handle the SIP connection service.
1519 // Check the system settings to see if it also should handle "tel" calls.
1520 if (accountHandle.getComponentName().equals(sipComponentName)) {
1521 boolean useSipForPstn = useSipForPstnCalls(context);
1522 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1523 if (useSipForPstn) {
1524 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1525 }
1526 } else {
1527 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1528 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1529 }
1530 }
1531
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001532 // Upgrade older phone accounts with explicit package name
1533 if (version < 5) {
1534 if (iconBitmap == null) {
1535 iconPackageName = accountHandle.getComponentName().getPackageName();
1536 }
1537 }
1538
Santos Cordona82aed82015-05-26 10:43:56 -07001539 if (version < 6) {
1540 // Always enable all SIP accounts on upgrade to version 6
1541 if (accountHandle.getComponentName().equals(sipComponentName)) {
1542 enabled = true;
1543 }
1544 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001545 if (version < 7) {
1546 // Always enabled all PSTN acocunts on upgrade to version 7
1547 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1548 enabled = true;
1549 }
1550 }
Roshan Pius6f752c82015-08-10 10:42:06 -07001551 if (version < 8) {
1552 // Migrate the SIP account handle ids to use SIP username instead of SIP URI.
1553 if (accountHandle.getComponentName().equals(sipComponentName)) {
1554 Uri accountUri = Uri.parse(accountHandle.getId());
1555 if (accountUri.getScheme() != null &&
1556 accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) {
1557 accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(),
1558 accountUri.getSchemeSpecificPart(),
1559 accountHandle.getUserHandle());
1560 }
1561 }
1562 }
Santos Cordona82aed82015-05-26 10:43:56 -07001563
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001564 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001565 .setAddress(address)
1566 .setSubscriptionAddress(subscriptionAddress)
1567 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001568 .setShortDescription(shortDescription)
1569 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001570 .setHighlightColor(highlightColor)
Tyler Gunn369c8742015-10-07 15:39:28 -07001571 .setExtras(extras)
Santos Cordonea5cb932015-05-07 16:28:38 -07001572 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001573
Santos Cordon9c30c282015-05-13 16:28:27 -07001574 if (icon != null) {
1575 builder.setIcon(icon);
1576 } else if (iconBitmap != null) {
1577 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1578 } else if (!TextUtils.isEmpty(iconPackageName)) {
1579 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1580 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001581 }
1582
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001583 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001584 }
1585 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001586 }
Tyler Gunn84253572014-09-02 14:50:05 -07001587
1588 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001589 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1590 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001591 *
1592 * @param context The context.
1593 * @return {@code True} if SIP should be used for all calls.
1594 */
1595 private boolean useSipForPstnCalls(Context context) {
1596 String option = Settings.System.getString(context.getContentResolver(),
1597 Settings.System.SIP_CALL_OPTIONS);
1598 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1599 return option.equals(Settings.System.SIP_ALWAYS);
1600 }
Ihab Awad104f8062014-07-17 11:29:35 -07001601 };
1602
Ihab Awadb78b2762014-07-25 15:16:23 -07001603 @VisibleForTesting
1604 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1605 new XmlSerialization<PhoneAccountHandle>() {
1606 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001607 private static final String COMPONENT_NAME = "component_name";
1608 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001609 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001610
1611 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001612 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001613 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001614 if (o != null) {
1615 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001616
Ihab Awad26923222014-07-30 10:54:35 -07001617 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001618 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001619 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1620 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001621
Ihab Awadd9f54382014-10-24 11:44:47 -07001622 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001623
Evan Charlton105d9772014-11-25 14:08:53 -08001624 if (o.getUserHandle() != null && context != null) {
1625 UserManager userManager = UserManager.get(context);
1626 writeLong(USER_SERIAL_NUMBER,
1627 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1628 }
1629
Ihab Awad26923222014-07-30 10:54:35 -07001630 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1631 }
Ihab Awad104f8062014-07-17 11:29:35 -07001632 }
1633
1634 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001635 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001636 throws IOException, XmlPullParserException {
1637 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1638 String componentNameString = null;
1639 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001640 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001641 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001642
1643 UserManager userManager = UserManager.get(context);
1644
Ihab Awadb78b2762014-07-25 15:16:23 -07001645 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1646 if (parser.getName().equals(COMPONENT_NAME)) {
1647 parser.next();
1648 componentNameString = parser.getText();
1649 } else if (parser.getName().equals(ID)) {
1650 parser.next();
1651 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001652 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1653 parser.next();
1654 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001655 }
1656 }
Ihab Awad26923222014-07-30 10:54:35 -07001657 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001658 UserHandle userHandle = null;
1659 if (userSerialNumberString != null) {
1660 try {
1661 long serialNumber = Long.parseLong(userSerialNumberString);
1662 userHandle = userManager.getUserForSerialNumber(serialNumber);
1663 } catch (NumberFormatException e) {
1664 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1665 }
1666 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001667 return new PhoneAccountHandle(
1668 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001669 idString,
1670 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001671 }
1672 }
1673 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001674 }
1675 };
Santos Cordon176ae282014-07-14 02:02:14 -07001676}