blob: 3531df691b18119b63519bab85c4940fa805104c [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;
Evan Charlton105d9772014-11-25 14:08:53 -080026import 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;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080031import android.os.Binder;
Evan Charlton105d9772014-11-25 14:08:53 -080032import android.os.Process;
33import android.os.UserHandle;
34import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070035import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070036import android.telecom.ConnectionService;
37import android.telecom.PhoneAccount;
38import android.telecom.PhoneAccountHandle;
Nancy Chen140004a2014-10-15 15:48:38 -070039import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070040import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080041import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070042import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070043import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070044import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070045import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070046
Tyler Gunn91d43cf2014-09-17 12:19:39 -070047// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070048import com.android.internal.annotations.VisibleForTesting;
49import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070050import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070051import com.android.internal.util.XmlUtils;
52
Evan Charltonaf51ceb2014-07-30 11:56:36 -070053import org.xmlpull.v1.XmlPullParser;
54import org.xmlpull.v1.XmlPullParserException;
55import org.xmlpull.v1.XmlSerializer;
56
Ihab Awadb78b2762014-07-25 15:16:23 -070057import java.io.BufferedInputStream;
58import java.io.BufferedOutputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070059import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070060import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070061import java.io.File;
62import java.io.FileNotFoundException;
63import java.io.FileOutputStream;
64import java.io.IOException;
65import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070066import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070067import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070068import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070069import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080070import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070071import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070072import java.util.List;
73import java.util.Objects;
Ihab Awadb78b2762014-07-25 15:16:23 -070074import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070075
76/**
Evan Charlton89176372014-07-19 18:23:09 -070077 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070078 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
79 * implemented in {@link TelecomServiceImpl}, with the notable exception that
80 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
81 * proper authority over the {@code ComponentName}s they are declaring in their
82 * {@code PhoneAccountHandle}s.
83 *
84 *
85 * -- About Users and Phone Accounts --
86 *
Santos Cordon9c30c282015-05-13 16:28:27 -070087 * We store all phone accounts for all users in a single place, which means that there are three
88 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -070089 * 1) The Android User that is currently active on the device.
90 * 2) The user which owns/registers the phone account.
91 * 3) The user running the app that is requesting the phone account information.
92 *
93 * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
94 * has a work profile running as another user (B2). Lets say that user B opens the phone settings
95 * (not currently supported, but theoretically speaking), and phone settings queries for a phone
Santos Cordon9c30c282015-05-13 16:28:27 -070096 * account list. Lets also say that an app running in the work profile has registered a phone
97 * account. This means that:
Santos Cordon6a212642015-05-08 16:35:23 -070098 *
99 * Since phone settings always runs as the primary user, We have the following situation:
Santos Cordon9c30c282015-05-13 16:28:27 -0700100 * User A (settings) is requesting a list of phone accounts while the active user is User B, and
101 * that list contains a phone account for profile User B2.
Santos Cordon6a212642015-05-08 16:35:23 -0700102 *
103 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
104 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
105 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700106 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700107public final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700108
Yorke Lee5e8836a2014-08-22 15:25:18 -0700109 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
110 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
111
Ihab Awadb78b2762014-07-25 15:16:23 -0700112 public abstract static class Listener {
113 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
114 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
115 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
116 }
117
118 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700119 @VisibleForTesting
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800120 public static final int EXPECTED_STATE_VERSION = 5;
Tyler Gunn84253572014-09-02 14:50:05 -0700121
122 /** Keep in sync with the same in SipSettings.java */
123 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700124
125 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
126 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700127 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800128 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800129 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700130 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800131 private UserHandle mCurrentUserHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700132
Nancy Chen06ce0622014-10-23 01:17:35 +0000133 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700134 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700135 this(context, FILE_NAME);
136 }
137
138 @VisibleForTesting
139 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700140 // TODO: This file path is subject to change -- it is storing the phone account registry
141 // state file in the path /data/system/users/0/, which is likely not correct in a
142 // multi-user setting.
143 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
144 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
145 getAbsolutePath();
146 mAtomicFile = new AtomicFile(new File(filePath, fileName));
147 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700148 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700149
Ihab Awadb78b2762014-07-25 15:16:23 -0700150 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700151 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800152 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800153 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800154 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700155 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700156 }
157
Tyler Gunn84253572014-09-02 14:50:05 -0700158 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700159 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
160 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
161 * subscription id.
162 * @param accountHandle The handle for the phone account for which to retrieve the
163 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700164 * @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 -0700165 */
Wink Saville35850602014-10-23 15:57:21 -0700166 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700167 PhoneAccount account = getPhoneAccountCheckCallingUser(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800168
Santos Cordon6a212642015-05-08 16:35:23 -0700169 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800170 TelephonyManager tm =
171 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
172 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700173 }
Santos Cordon479b3022015-02-06 04:27:15 -0800174 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700175 }
176
177 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800178 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
179 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
180 * will be returned.
181 *
Tyler Gunn84253572014-09-02 14:50:05 -0700182 * @param uriScheme The URI scheme for the outgoing call.
183 * @return The {@link PhoneAccountHandle} to use.
184 */
Santos Cordon6a212642015-05-08 16:35:23 -0700185 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700186 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700187
Yorke Lee5e8836a2014-08-22 15:25:18 -0700188 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700189 // If there is a default PhoneAccount, ensure it supports calls to handles with the
190 // specified uriScheme.
Santos Cordon6a212642015-05-08 16:35:23 -0700191 final PhoneAccount userSelectedAccount = getPhoneAccountCheckCallingUser(userSelected);
192 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700193 return userSelected;
194 }
Ihab Awad293edf22014-07-24 17:52:29 -0700195 }
196
Santos Cordonea5cb932015-05-07 16:28:38 -0700197 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700198 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700199 case 0:
200 // There are no accounts, so there can be no default
201 return null;
202 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800203 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700204 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700205 default:
206 // There are multiple accounts with no selected default
207 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700208 }
Ihab Awad104f8062014-07-17 11:29:35 -0700209 }
Santos Cordon176ae282014-07-14 02:02:14 -0700210
Evan Charlton105d9772014-11-25 14:08:53 -0800211 /**
212 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
213 * if it was set by another user).
214 */
Yorke Lee5e8836a2014-08-22 15:25:18 -0700215 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
Santos Cordon6a212642015-05-08 16:35:23 -0700216 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.defaultOutgoing);
217 if (account != null) {
218 return mState.defaultOutgoing;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700219 }
220 return null;
221 }
222
Santos Cordon6a212642015-05-08 16:35:23 -0700223 /**
224 * Sets the phone account with which to place all calls by default. Set by the user
225 * within phone settings.
226 */
Andrew Leea51a3862014-09-03 14:58:45 -0700227 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700228 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700229 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700230 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700231 } else {
Santos Cordon6a212642015-05-08 16:35:23 -0700232 // TODO: Do we really want to return for *any* user?
233 PhoneAccount account = getPhoneAccount(accountHandle);
234 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700235 Log.w(this, "Trying to set nonexistent default outgoing %s",
236 accountHandle);
237 return;
238 }
239
Santos Cordon6a212642015-05-08 16:35:23 -0700240 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700241 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700242 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700243 return;
244 }
245
Santos Cordon6a212642015-05-08 16:35:23 -0700246 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700247 // If the account selected is a SIM account, propagate down to the subscription
248 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700249 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800250 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700251 }
252
Ihab Awad293edf22014-07-24 17:52:29 -0700253 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700254 }
255
Ihab Awad293edf22014-07-24 17:52:29 -0700256 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700257 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700258 }
259
Nancy Chen668dee02014-11-19 15:31:31 -0800260 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
261 return getSubscriptionIdForPhoneAccount(accountHandle) ==
262 SubscriptionManager.getDefaultSmsSubId();
263 }
264
Ihab Awad293edf22014-07-24 17:52:29 -0700265 public void setSimCallManager(PhoneAccountHandle callManager) {
266 if (callManager != null) {
Santos Cordon6a212642015-05-08 16:35:23 -0700267 // TODO: Do we really want to return for *any* user?
268 PhoneAccount callManagerAccount = getPhoneAccount(callManager);
Ihab Awad293edf22014-07-24 17:52:29 -0700269 if (callManagerAccount == null) {
270 Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager);
271 return;
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700272 } else if (!callManagerAccount.hasCapabilities(
273 PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) {
Ihab Awad293edf22014-07-24 17:52:29 -0700274 Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount);
275 return;
276 }
Yorke Lee5e8836a2014-08-22 15:25:18 -0700277 } else {
278 callManager = NO_ACCOUNT_SELECTED;
Ihab Awad293edf22014-07-24 17:52:29 -0700279 }
280 mState.simCallManager = callManager;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700281
Ihab Awad293edf22014-07-24 17:52:29 -0700282 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700283 fireSimCallManagerChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700284 }
285
Evan Charlton105d9772014-11-25 14:08:53 -0800286 /**
287 * @return The {@link PhoneAccount}s which are visible to {@link #mCurrentUserHandle}.
288 */
Ihab Awad293edf22014-07-24 17:52:29 -0700289 public PhoneAccountHandle getSimCallManager() {
Santos Cordon6a212642015-05-08 16:35:23 -0700290 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.simCallManager);
291
292 // Return the registered sim call manager iff it still exists (we keep a sticky
293 // setting to survive account deletion and re-addition)
294 if (account != null && !resolveComponent(mState.simCallManager).isEmpty()) {
295 return mState.simCallManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700296 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700297
Santos Cordon6a212642015-05-08 16:35:23 -0700298 // We have no set call manager, but check to see if the OEM has specified a default one.
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700299 String defaultConnectionMgr =
Santos Cordonafe59e52014-08-22 16:48:43 -0700300 mContext.getResources().getString(R.string.default_connection_manager_component);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700301 if (!TextUtils.isEmpty(defaultConnectionMgr)) {
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700302 ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800303 if (componentName == null) {
304 return null;
305 }
306
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700307 // Make sure that the component can be resolved.
Evan Charlton105d9772014-11-25 14:08:53 -0800308 List<ResolveInfo> resolveInfos = resolveComponent(componentName, null);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800309 if (resolveInfos.isEmpty()) {
310 resolveInfos = resolveComponent(componentName, Binder.getCallingUserHandle());
311 }
312
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700313 if (!resolveInfos.isEmpty()) {
314 // See if there is registered PhoneAccount by this component.
315 List<PhoneAccountHandle> handles = getAllPhoneAccountHandles();
316 for (PhoneAccountHandle handle : handles) {
Santos Cordon6a212642015-05-08 16:35:23 -0700317 if (componentName.equals(handle.getComponentName())) {
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700318 return handle;
319 }
320 }
321 Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName);
322 } else {
323 Log.d(this, "%s could not be resolved; not using as default", componentName);
324 }
325 } else {
326 Log.v(this, "No default connection manager specified");
327 }
328
Ihab Awadb78b2762014-07-25 15:16:23 -0700329 return null;
Ihab Awad293edf22014-07-24 17:52:29 -0700330 }
331
Evan Charlton105d9772014-11-25 14:08:53 -0800332 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800333 * Update the current UserHandle to track when users are switched. This will allow the
334 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
335 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700336 * We cannot simply check the calling user because that would always return the primary user for
337 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800338 *
339 * @param userHandle The {@link UserHandle}, as delivered by
340 * {@link Intent#ACTION_USER_SWITCHED}.
341 */
342 public void setCurrentUserHandle(UserHandle userHandle) {
343 if (userHandle == null) {
344 Log.d(this, "setCurrentUserHandle, userHandle = null");
345 userHandle = Process.myUserHandle();
346 }
347 Log.d(this, "setCurrentUserHandle, %s", userHandle);
348 mCurrentUserHandle = userHandle;
349 }
350
Santos Cordonea5cb932015-05-07 16:28:38 -0700351 public void enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
352 PhoneAccount account = getPhoneAccount(accountHandle);
353 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
354 // We never change the enabled state of SIM-based accounts.
355 return;
356 }
357
358 if (account != null && account.isEnabled() != isEnabled) {
359 account.setIsEnabled(isEnabled);
360 write();
361 fireAccountsChanged();
362 }
363 }
364
Evan Charlton105d9772014-11-25 14:08:53 -0800365 private boolean isVisibleForUser(PhoneAccount account) {
366 if (account == null) {
367 return false;
368 }
369
370 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
371 // all profiles. Only Telephony and SIP accounts should have this capability.
372 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
373 return true;
374 }
375
376 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
377 if (phoneAccountUserHandle == null) {
378 return false;
379 }
380
381 if (mCurrentUserHandle == null) {
382 Log.d(this, "Current user is null; assuming true");
383 return true;
384 }
385
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800386 if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) {
387 return true;
388 }
389
Santos Cordon6a212642015-05-08 16:35:23 -0700390 // Special check for work profiles.
Evan Charlton105d9772014-11-25 14:08:53 -0800391 // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure
392 // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is
393 // fine.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800394 if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) {
395 List<UserInfo> profileUsers =
396 mUserManager.getProfiles(mCurrentUserHandle.getIdentifier());
397 for (UserInfo profileInfo : profileUsers) {
398 if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) {
399 return true;
400 }
Evan Charlton105d9772014-11-25 14:08:53 -0800401 }
402 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800403
Evan Charlton105d9772014-11-25 14:08:53 -0800404 return false;
405 }
406
407 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
408 return resolveComponent(phoneAccountHandle.getComponentName(),
409 phoneAccountHandle.getUserHandle());
410 }
411
412 private List<ResolveInfo> resolveComponent(ComponentName componentName,
413 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800414 PackageManager pm = mContext.getPackageManager();
415 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
416 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800417 try {
418 if (userHandle != null) {
419 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
420 } else {
421 return pm.queryIntentServices(intent, 0);
422 }
423 } catch (SecurityException e) {
424 Log.v(this, "%s is not visible for the calling user", componentName);
425 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800426 }
mike dooley10a58312014-11-06 13:46:19 -0800427 }
428
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700429 /**
430 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700431 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700432 *
433 * @return The list of {@link PhoneAccountHandle}s.
434 */
Ihab Awad293edf22014-07-24 17:52:29 -0700435 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700436 return getPhoneAccountHandles(0, null, null, false);
Ihab Awad293edf22014-07-24 17:52:29 -0700437 }
438
439 public List<PhoneAccount> getAllPhoneAccounts() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700440 return getPhoneAccounts(0, null, null, false);
Santos Cordonafe59e52014-08-22 16:48:43 -0700441 }
442
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700443 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700444 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700445 * specified URI scheme.
446 *
447 * @param uriScheme The URI scheme.
448 * @return The phone account handles.
449 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700450 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
451 String uriScheme, boolean includeDisabledAccounts) {
452 return getPhoneAccountHandles(
453 PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme, null, includeDisabledAccounts);
Santos Cordon6a212642015-05-08 16:35:23 -0700454 }
455
456 /**
457 * Retrieves a list of all the SIM-based phone accounts.
458 */
459 public List<PhoneAccountHandle> getSimPhoneAccounts() {
460 return getPhoneAccountHandles(
461 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Santos Cordonea5cb932015-05-07 16:28:38 -0700462 null, null, false);
Tyler Gunn84253572014-09-02 14:50:05 -0700463 }
464
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700465 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700466 * Retrieves a list of all phone accounts registered by a specified package.
467 *
468 * @param packageName The name of the package that registered the phone accounts.
469 * @return The phone account handles.
470 */
471 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700472 return getPhoneAccountHandles(0, null, packageName, false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700473 }
474
475 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700476 * Retrieves a list of all phone account handles with the connection manager capability.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700477 *
478 * @return The phone account handles.
479 */
480 public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700481 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null, true);
Santos Cordon176ae282014-07-14 02:02:14 -0700482 }
483
Ihab Awad104f8062014-07-17 11:29:35 -0700484 // TODO: Should we implement an artificial limit for # of accounts associated with a single
485 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700486 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700487 // Enforce the requirement that a connection service for a phone account has the correct
488 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700489 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700490 Log.w(this,
491 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700492 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700493 throw new SecurityException("PhoneAccount connection service requires "
494 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700495 }
496
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700497 addOrReplacePhoneAccount(account);
498 }
499
500 /**
501 * Adds a {@code PhoneAccount}, replacing an existing one if found.
502 *
503 * @param account The {@code PhoneAccount} to add or replace.
504 */
505 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800506 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
507 account.getAccountHandle(), account);
508
Santos Cordonea5cb932015-05-07 16:28:38 -0700509 // Start _enabled_ property as false.
510 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
511 // source app provides or else an third party app could enable itself.
512 boolean isEnabled = false;
513
Santos Cordon6a212642015-05-08 16:35:23 -0700514 PhoneAccount oldAccount = getPhoneAccount(account.getAccountHandle());
515 if (oldAccount != null) {
516 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700517 isEnabled = oldAccount.isEnabled();
Santos Cordon176ae282014-07-14 02:02:14 -0700518 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700519
Santos Cordon6a212642015-05-08 16:35:23 -0700520 mState.accounts.add(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700521 // Reset enabled state to whatever the value was if the account was already registered,
522 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled.
523 account.setIsEnabled(
524 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION));
Santos Cordon176ae282014-07-14 02:02:14 -0700525
Ihab Awad293edf22014-07-24 17:52:29 -0700526 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700527 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700528 }
529
Evan Charlton89176372014-07-19 18:23:09 -0700530 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700531 PhoneAccount account = getPhoneAccount(accountHandle);
532 if (account != null) {
533 if (mState.accounts.remove(account)) {
534 write();
535 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700536 }
537 }
Santos Cordon176ae282014-07-14 02:02:14 -0700538 }
539
Tyler Gunnd900ce62014-08-13 11:40:59 -0700540 /**
541 * Un-registers all phone accounts associated with a specified package.
542 *
543 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800544 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700545 */
Evan Charlton105d9772014-11-25 14:08:53 -0800546 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700547 boolean accountsRemoved = false;
548 Iterator<PhoneAccount> it = mState.accounts.iterator();
549 while (it.hasNext()) {
550 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800551 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
552 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
553 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700554 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
555 it.remove();
556 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700557 }
558 }
559
Tyler Gunnd900ce62014-08-13 11:40:59 -0700560 if (accountsRemoved) {
561 write();
562 fireAccountsChanged();
563 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700564 }
565
Nancy Chen140004a2014-10-15 15:48:38 -0700566 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700567 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700568 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
569 }
570
Ihab Awadb78b2762014-07-25 15:16:23 -0700571 public void addListener(Listener l) {
572 mListeners.add(l);
573 }
574
575 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700576 if (l != null) {
577 mListeners.remove(l);
578 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700579 }
580
581 private void fireAccountsChanged() {
582 for (Listener l : mListeners) {
583 l.onAccountsChanged(this);
584 }
585 }
586
587 private void fireDefaultOutgoingChanged() {
588 for (Listener l : mListeners) {
589 l.onDefaultOutgoingChanged(this);
590 }
591 }
592
593 private void fireSimCallManagerChanged() {
594 for (Listener l : mListeners) {
595 l.onSimCallManagerChanged(this);
596 }
Santos Cordon176ae282014-07-14 02:02:14 -0700597 }
598
Tyler Gunncb59b672014-08-20 09:02:11 -0700599 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700600 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700601 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700602 *
603 * @param phoneAccountHandle The phone account to check.
604 * @return {@code True} if the phone account has permission.
605 */
Santos Cordon6a212642015-05-08 16:35:23 -0700606 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800607 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
608 if (resolveInfos.isEmpty()) {
609 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700610 return false;
611 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800612 for (ResolveInfo resolveInfo : resolveInfos) {
613 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700614 if (serviceInfo == null) {
615 return false;
616 }
617
618 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
619 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
620 serviceInfo.permission)) {
621 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
622 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
623 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800624 return false;
625 }
626 }
627 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700628 }
629
Santos Cordon6a212642015-05-08 16:35:23 -0700630 //
631 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
632 //
Ihab Awad293edf22014-07-24 17:52:29 -0700633
Santos Cordonafe59e52014-08-22 16:48:43 -0700634 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700635 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700636 *
Santos Cordon6a212642015-05-08 16:35:23 -0700637 * @param handle
638 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700639 */
Santos Cordon6a212642015-05-08 16:35:23 -0700640 PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
641 for (PhoneAccount m : mState.accounts) {
642 if (Objects.equals(handle, m.getAccountHandle())) {
643 return m;
644 }
645 }
646 return null;
647 }
648
649 /**
650 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
651 * account before returning it. The current user is the active user on the actual android
652 * device.
653 */
654 public PhoneAccount getPhoneAccountCheckCallingUser(PhoneAccountHandle handle) {
655 PhoneAccount account = getPhoneAccount(handle);
656 if (account != null && isVisibleForUser(account)) {
657 return account;
658 }
659 return null;
660 }
661
662 /**
663 * Returns a list of phone account handles with the specified capabilities, uri scheme,
664 * and package name.
665 */
666 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700667 int capabilities,
668 String uriScheme,
669 String packageName,
670 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700671 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700672
673 for (PhoneAccount account : getPhoneAccounts(
674 capabilities, uriScheme, packageName, includeDisabledAccounts)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700675 handles.add(account.getAccountHandle());
676 }
677 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700678 }
679
680 /**
681 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700682 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700683 *
Santos Cordon6a212642015-05-08 16:35:23 -0700684 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
685 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700686 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700687 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700688 */
Santos Cordon6a212642015-05-08 16:35:23 -0700689 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700690 int capabilities,
691 String uriScheme,
692 String packageName,
693 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700694 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700695 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700696 if (!(m.isEnabled() || includeDisabledAccounts)) {
697 // Do not include disabled accounts.
698 continue;
699 }
700
Santos Cordon6a212642015-05-08 16:35:23 -0700701 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800702 // Account doesn't have the right capabilities; skip this one.
703 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700704 }
Evan Charlton105d9772014-11-25 14:08:53 -0800705 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
706 // Account doesn't support this URI scheme; skip this one.
707 continue;
708 }
Santos Cordon6a212642015-05-08 16:35:23 -0700709 PhoneAccountHandle handle = m.getAccountHandle();
710
711 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800712 // This component cannot be resolved anymore; skip this one.
713 continue;
714 }
Santos Cordon6a212642015-05-08 16:35:23 -0700715 if (packageName != null &&
716 !packageName.equals(handle.getComponentName().getPackageName())) {
717 // Not the right package name; skip this one.
718 continue;
719 }
Evan Charlton105d9772014-11-25 14:08:53 -0800720 if (!isVisibleForUser(m)) {
721 // Account is not visible for the current user; skip this one.
722 continue;
723 }
Santos Cordon6a212642015-05-08 16:35:23 -0700724 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700725 }
Santos Cordon6a212642015-05-08 16:35:23 -0700726 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700727 }
728
Santos Cordon6a212642015-05-08 16:35:23 -0700729 //
730 // State Implementation for PhoneAccountRegistrar
731 //
732
Ihab Awad293edf22014-07-24 17:52:29 -0700733 /**
734 * The state of this {@code PhoneAccountRegistrar}.
735 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700736 @VisibleForTesting
737 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700738 /**
739 * The account selected by the user to be employed by default for making outgoing calls.
740 * If the user has not made such a selection, then this is null.
741 */
742 public PhoneAccountHandle defaultOutgoing = null;
743
744 /**
Ihab Awadb78b2762014-07-25 15:16:23 -0700745 * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which
Ihab Awad293edf22014-07-24 17:52:29 -0700746 * manages and optimizes a user's PSTN SIM connections.
747 */
748 public PhoneAccountHandle simCallManager;
749
750 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700751 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700752 */
753 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700754
755 /**
756 * The version number of the State data.
757 */
758 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700759 }
760
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700761 /**
762 * Dumps the state of the {@link CallsManager}.
763 *
764 * @param pw The {@code IndentingPrintWriter} to write the state to.
765 */
766 public void dump(IndentingPrintWriter pw) {
767 if (mState != null) {
768 pw.println("xmlVersion: " + mState.versionNumber);
769 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
770 mState.defaultOutgoing));
771 pw.println("simCallManager: " + (mState.simCallManager == null ? "none" :
772 mState.simCallManager));
773 pw.println("phoneAccounts:");
774 pw.increaseIndent();
775 for (PhoneAccount phoneAccount : mState.accounts) {
776 pw.println(phoneAccount);
777 }
778 pw.decreaseIndent();
779 }
780 }
781
Ihab Awad293edf22014-07-24 17:52:29 -0700782 ////////////////////////////////////////////////////////////////////////////////////////////////
783 //
784 // State management
785 //
786
787 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700788 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700789 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700790 os = mAtomicFile.startWrite();
791 boolean success = false;
792 try {
793 XmlSerializer serializer = new FastXmlSerializer();
794 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800795 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700796 serializer.flush();
797 success = true;
798 } finally {
799 if (success) {
800 mAtomicFile.finishWrite(os);
801 } else {
802 mAtomicFile.failWrite(os);
803 }
804 }
805 } catch (IOException e) {
806 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700807 }
808 }
809
Ihab Awadb78b2762014-07-25 15:16:23 -0700810 private void read() {
811 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700812 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700813 is = mAtomicFile.openRead();
814 } catch (FileNotFoundException ex) {
815 return;
816 }
817
Tyler Gunn84253572014-09-02 14:50:05 -0700818 boolean versionChanged = false;
819
Ihab Awadb78b2762014-07-25 15:16:23 -0700820 XmlPullParser parser;
821 try {
822 parser = Xml.newPullParser();
823 parser.setInput(new BufferedInputStream(is), null);
824 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700825 mState = readFromXml(parser, mContext);
826 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
827
Ihab Awadb78b2762014-07-25 15:16:23 -0700828 } catch (IOException | XmlPullParserException e) {
829 Log.e(this, e, "Reading state from XML file");
830 mState = new State();
831 } finally {
832 try {
833 is.close();
834 } catch (IOException e) {
835 Log.e(this, e, "Closing InputStream");
836 }
Ihab Awad104f8062014-07-17 11:29:35 -0700837 }
Tyler Gunn84253572014-09-02 14:50:05 -0700838
Evan Charlton105d9772014-11-25 14:08:53 -0800839 // Verify all of the UserHandles.
840 List<PhoneAccount> badAccounts = new ArrayList<>();
841 for (PhoneAccount phoneAccount : mState.accounts) {
842 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
843 if (userHandle == null) {
844 Log.w(this, "Missing UserHandle for %s", phoneAccount);
845 badAccounts.add(phoneAccount);
846 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
847 Log.w(this, "User does not exist for %s", phoneAccount);
848 badAccounts.add(phoneAccount);
849 }
850 }
851 mState.accounts.removeAll(badAccounts);
852
Tyler Gunn84253572014-09-02 14:50:05 -0700853 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -0800854 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -0700855 write();
856 }
Santos Cordon176ae282014-07-14 02:02:14 -0700857 }
858
Evan Charlton105d9772014-11-25 14:08:53 -0800859 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700860 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -0800861 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -0700862 }
Ihab Awad104f8062014-07-17 11:29:35 -0700863
Tyler Gunn84253572014-09-02 14:50:05 -0700864 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700865 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700866 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700867 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700868 }
869
Ihab Awad293edf22014-07-24 17:52:29 -0700870 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700871 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700872 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700873 //
874
Ihab Awadb78b2762014-07-25 15:16:23 -0700875 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700876 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700877 private static final String LENGTH_ATTRIBUTE = "length";
878 private static final String VALUE_TAG = "value";
879
Ihab Awadb78b2762014-07-25 15:16:23 -0700880 /**
881 * Write the supplied object to XML
882 */
Evan Charlton105d9772014-11-25 14:08:53 -0800883 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700884 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700885
886 /**
887 * Read from the supplied XML into a new object, returning null in case of an
888 * unrecoverable schema mismatch or other data error. 'parser' must be already
889 * positioned at the first tag that is expected to have been emitted by this
890 * object's writeToXml(). This object tries to fail early without modifying
891 * 'parser' if it does not recognize the data it sees.
892 */
Tyler Gunn84253572014-09-02 14:50:05 -0700893 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700894 throws IOException, XmlPullParserException;
895
Ihab Awadd9f54382014-10-24 11:44:47 -0700896 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700897 throws IOException {
898 if (value != null) {
899 serializer.startTag(null, tagName);
900 serializer.text(Objects.toString(value));
901 serializer.endTag(null, tagName);
902 }
903 }
Tyler Gunn84253572014-09-02 14:50:05 -0700904
905 /**
906 * Serializes a string array.
907 *
908 * @param tagName The tag name for the string array.
909 * @param values The string values to serialize.
910 * @param serializer The serializer.
911 * @throws IOException
912 */
913 protected void writeStringList(String tagName, List<String> values,
914 XmlSerializer serializer)
915 throws IOException {
916
917 serializer.startTag(null, tagName);
918 if (values != null) {
919 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
920 for (String toSerialize : values) {
921 serializer.startTag(null, VALUE_TAG);
922 if (toSerialize != null ){
923 serializer.text(toSerialize);
924 }
Tyler Gunn84253572014-09-02 14:50:05 -0700925 serializer.endTag(null, VALUE_TAG);
926 }
927 } else {
928 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
929 }
930 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -0700931 }
Tyler Gunn84253572014-09-02 14:50:05 -0700932
Santos Cordon9c30c282015-05-13 16:28:27 -0700933 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -0700934 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -0700935 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -0700936 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -0700937 value.writeToStream(stream);
938 byte[] iconByteArray = stream.toByteArray();
939 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -0700940
941 serializer.startTag(null, tagName);
942 serializer.text(text);
943 serializer.endTag(null, tagName);
944 }
Tyler Gunn84253572014-09-02 14:50:05 -0700945 }
946
Evan Charlton105d9772014-11-25 14:08:53 -0800947 protected void writeLong(String tagName, long value, XmlSerializer serializer)
948 throws IOException {
949 serializer.startTag(null, tagName);
950 serializer.text(Long.valueOf(value).toString());
951 serializer.endTag(null, tagName);
952 }
953
Tyler Gunn84253572014-09-02 14:50:05 -0700954 /**
955 * Reads a string array from the XML parser.
956 *
957 * @param parser The XML parser.
958 * @return String array containing the parsed values.
959 * @throws IOException Exception related to IO.
960 * @throws XmlPullParserException Exception related to parsing.
961 */
962 protected List<String> readStringList(XmlPullParser parser)
963 throws IOException, XmlPullParserException {
964
965 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
966 List<String> arrayEntries = new ArrayList<String>(length);
967 String value = null;
968
969 if (length == 0) {
970 return arrayEntries;
971 }
972
973 int outerDepth = parser.getDepth();
974 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
975 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700976 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -0700977 value = parser.getText();
978 arrayEntries.add(value);
979 }
980 }
981
982 return arrayEntries;
983 }
Ihab Awadd9f54382014-10-24 11:44:47 -0700984
Santos Cordon9c30c282015-05-13 16:28:27 -0700985 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -0700986 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
987 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
988 }
Santos Cordon9c30c282015-05-13 16:28:27 -0700989
990 protected Icon readIcon(XmlPullParser parser) throws IOException {
991 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
992 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
993 return Icon.createFromStream(stream);
994 }
Ihab Awad104f8062014-07-17 11:29:35 -0700995 }
996
Ihab Awadb78b2762014-07-25 15:16:23 -0700997 @VisibleForTesting
998 public static final XmlSerialization<State> sStateXml =
999 new XmlSerialization<State>() {
1000 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001001 private static final String DEFAULT_OUTGOING = "default_outgoing";
Ihab Awad293edf22014-07-24 17:52:29 -07001002 private static final String SIM_CALL_MANAGER = "sim_call_manager";
Ihab Awad104f8062014-07-17 11:29:35 -07001003 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001004 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001005
1006 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001007 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001008 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001009 if (o != null) {
1010 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001011 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001012
Ihab Awad26923222014-07-30 10:54:35 -07001013 if (o.defaultOutgoing != null) {
1014 serializer.startTag(null, DEFAULT_OUTGOING);
Evan Charlton105d9772014-11-25 14:08:53 -08001015 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001016 serializer.endTag(null, DEFAULT_OUTGOING);
1017 }
1018
1019 if (o.simCallManager != null) {
1020 serializer.startTag(null, SIM_CALL_MANAGER);
Evan Charlton105d9772014-11-25 14:08:53 -08001021 sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001022 serializer.endTag(null, SIM_CALL_MANAGER);
1023 }
1024
1025 serializer.startTag(null, ACCOUNTS);
1026 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001027 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001028 }
1029 serializer.endTag(null, ACCOUNTS);
1030
1031 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001032 }
Ihab Awad104f8062014-07-17 11:29:35 -07001033 }
1034
1035 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001036 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001037 throws IOException, XmlPullParserException {
1038 if (parser.getName().equals(CLASS_STATE)) {
1039 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001040
1041 String rawVersion = parser.getAttributeValue(null, VERSION);
1042 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
1043 Integer.parseInt(rawVersion);
1044
Ihab Awadb78b2762014-07-25 15:16:23 -07001045 int outerDepth = parser.getDepth();
1046 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1047 if (parser.getName().equals(DEFAULT_OUTGOING)) {
1048 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001049 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
1050 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001051 } else if (parser.getName().equals(SIM_CALL_MANAGER)) {
1052 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001053 s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser,
1054 s.versionNumber, context);
Evan Charlton105d9772014-11-25 14:08:53 -08001055 if (s.simCallManager.getUserHandle() == null) {
1056 // This should never happen, but handle the upgrade case.
1057 s.simCallManager = new PhoneAccountHandle(
1058 s.simCallManager.getComponentName(),
1059 s.simCallManager.getId(),
1060 Process.myUserHandle());
1061 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001062 } else if (parser.getName().equals(ACCOUNTS)) {
1063 int accountsDepth = parser.getDepth();
1064 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001065 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1066 s.versionNumber, context);
1067
1068 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001069 s.accounts.add(account);
1070 }
1071 }
Ihab Awad104f8062014-07-17 11:29:35 -07001072 }
1073 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001074 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001075 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001076 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001077 }
1078 };
1079
Ihab Awadb78b2762014-07-25 15:16:23 -07001080 @VisibleForTesting
1081 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1082 new XmlSerialization<PhoneAccount>() {
1083 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1084 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001085 private static final String ADDRESS = "handle";
1086 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001087 private static final String CAPABILITIES = "capabilities";
1088 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001089 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1090 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001091 private static final String ICON_TINT = "icon_tint";
1092 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001093 private static final String LABEL = "label";
1094 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001095 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001096 private static final String ICON = "icon";
Santos Cordonea5cb932015-05-07 16:28:38 -07001097 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001098
1099 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001100 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001101 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001102 if (o != null) {
1103 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001104
Ihab Awad26923222014-07-30 10:54:35 -07001105 if (o.getAccountHandle() != null) {
1106 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001107 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001108 serializer.endTag(null, ACCOUNT_HANDLE);
1109 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001110
Ihab Awadd9f54382014-10-24 11:44:47 -07001111 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1112 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1113 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001114 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001115 writeTextIfNonNull(HIGHLIGHT_COLOR,
1116 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001117 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1118 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001119 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001120 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001121
Ihab Awad26923222014-07-30 10:54:35 -07001122 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1123 }
Ihab Awad104f8062014-07-17 11:29:35 -07001124 }
1125
Tyler Gunn84253572014-09-02 14:50:05 -07001126 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001127 throws IOException, XmlPullParserException {
1128 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1129 int outerDepth = parser.getDepth();
1130 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001131 Uri address = null;
1132 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001133 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001134 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001135 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001136 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001137 int iconTint = PhoneAccount.NO_ICON_TINT;
1138 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001139 String label = null;
1140 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001141 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001142 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001143 boolean enabled = false;
Ihab Awadb78b2762014-07-25 15:16:23 -07001144
1145 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1146 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1147 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001148 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1149 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001150 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001151 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001152 address = Uri.parse(parser.getText());
1153 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001154 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001155 String nextText = parser.getText();
1156 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001157 } else if (parser.getName().equals(CAPABILITIES)) {
1158 parser.next();
1159 capabilities = Integer.parseInt(parser.getText());
1160 } else if (parser.getName().equals(ICON_RES_ID)) {
1161 parser.next();
1162 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001163 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1164 parser.next();
1165 iconPackageName = parser.getText();
1166 } else if (parser.getName().equals(ICON_BITMAP)) {
1167 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001168 iconBitmap = readBitmap(parser);
1169 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001170 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001171 iconTint = Integer.parseInt(parser.getText());
1172 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1173 parser.next();
1174 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001175 } else if (parser.getName().equals(LABEL)) {
1176 parser.next();
1177 label = parser.getText();
1178 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1179 parser.next();
1180 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001181 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1182 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001183 } else if (parser.getName().equals(ICON)) {
1184 parser.next();
1185 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001186 } else if (parser.getName().equals(ENABLED)) {
1187 parser.next();
1188 enabled = "true".equalsIgnoreCase(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001189 }
1190 }
Tyler Gunn84253572014-09-02 14:50:05 -07001191
1192 // Upgrade older phone accounts to specify the supported URI schemes.
1193 if (version < 2) {
1194 ComponentName sipComponentName = new ComponentName("com.android.phone",
1195 "com.android.services.telephony.sip.SipConnectionService");
1196
1197 supportedUriSchemes = new ArrayList<>();
1198
1199 // Handle the SIP connection service.
1200 // Check the system settings to see if it also should handle "tel" calls.
1201 if (accountHandle.getComponentName().equals(sipComponentName)) {
1202 boolean useSipForPstn = useSipForPstnCalls(context);
1203 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1204 if (useSipForPstn) {
1205 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1206 }
1207 } else {
1208 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1209 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1210 }
1211 }
1212
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001213 // Upgrade older phone accounts with explicit package name
1214 if (version < 5) {
1215 if (iconBitmap == null) {
1216 iconPackageName = accountHandle.getComponentName().getPackageName();
1217 }
1218 }
1219
1220 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001221 .setAddress(address)
1222 .setSubscriptionAddress(subscriptionAddress)
1223 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001224 .setShortDescription(shortDescription)
1225 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001226 .setHighlightColor(highlightColor)
1227 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001228
Santos Cordon9c30c282015-05-13 16:28:27 -07001229 if (icon != null) {
1230 builder.setIcon(icon);
1231 } else if (iconBitmap != null) {
1232 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1233 } else if (!TextUtils.isEmpty(iconPackageName)) {
1234 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1235 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001236 }
1237
Santos Cordonea5cb932015-05-07 16:28:38 -07001238 builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001239 }
1240 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001241 }
Tyler Gunn84253572014-09-02 14:50:05 -07001242
1243 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001244 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1245 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001246 *
1247 * @param context The context.
1248 * @return {@code True} if SIP should be used for all calls.
1249 */
1250 private boolean useSipForPstnCalls(Context context) {
1251 String option = Settings.System.getString(context.getContentResolver(),
1252 Settings.System.SIP_CALL_OPTIONS);
1253 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1254 return option.equals(Settings.System.SIP_ALWAYS);
1255 }
Ihab Awad104f8062014-07-17 11:29:35 -07001256 };
1257
Ihab Awadb78b2762014-07-25 15:16:23 -07001258 @VisibleForTesting
1259 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1260 new XmlSerialization<PhoneAccountHandle>() {
1261 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001262 private static final String COMPONENT_NAME = "component_name";
1263 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001264 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001265
1266 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001267 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001268 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001269 if (o != null) {
1270 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001271
Ihab Awad26923222014-07-30 10:54:35 -07001272 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001273 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001274 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1275 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001276
Ihab Awadd9f54382014-10-24 11:44:47 -07001277 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001278
Evan Charlton105d9772014-11-25 14:08:53 -08001279 if (o.getUserHandle() != null && context != null) {
1280 UserManager userManager = UserManager.get(context);
1281 writeLong(USER_SERIAL_NUMBER,
1282 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1283 }
1284
Ihab Awad26923222014-07-30 10:54:35 -07001285 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1286 }
Ihab Awad104f8062014-07-17 11:29:35 -07001287 }
1288
1289 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001290 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001291 throws IOException, XmlPullParserException {
1292 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1293 String componentNameString = null;
1294 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001295 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001296 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001297
1298 UserManager userManager = UserManager.get(context);
1299
Ihab Awadb78b2762014-07-25 15:16:23 -07001300 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1301 if (parser.getName().equals(COMPONENT_NAME)) {
1302 parser.next();
1303 componentNameString = parser.getText();
1304 } else if (parser.getName().equals(ID)) {
1305 parser.next();
1306 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001307 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1308 parser.next();
1309 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001310 }
1311 }
Ihab Awad26923222014-07-30 10:54:35 -07001312 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001313 UserHandle userHandle = null;
1314 if (userSerialNumberString != null) {
1315 try {
1316 long serialNumber = Long.parseLong(userSerialNumberString);
1317 userHandle = userManager.getUserForSerialNumber(serialNumber);
1318 } catch (NumberFormatException e) {
1319 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1320 }
1321 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001322 return new PhoneAccountHandle(
1323 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001324 idString,
1325 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001326 }
1327 }
1328 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001329 }
1330 };
Santos Cordon176ae282014-07-14 02:02:14 -07001331}