blob: 40f63cb08a1a8b132570eadc6df59902df716e3a [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
Nancy Chen309198e2014-09-15 18:02:49 -0700197 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme);
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
Evan Charlton105d9772014-11-25 14:08:53 -0800351 private boolean isVisibleForUser(PhoneAccount account) {
352 if (account == null) {
353 return false;
354 }
355
356 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
357 // all profiles. Only Telephony and SIP accounts should have this capability.
358 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
359 return true;
360 }
361
362 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
363 if (phoneAccountUserHandle == null) {
364 return false;
365 }
366
367 if (mCurrentUserHandle == null) {
368 Log.d(this, "Current user is null; assuming true");
369 return true;
370 }
371
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800372 if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) {
373 return true;
374 }
375
Santos Cordon6a212642015-05-08 16:35:23 -0700376 // Special check for work profiles.
Evan Charlton105d9772014-11-25 14:08:53 -0800377 // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure
378 // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is
379 // fine.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800380 if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) {
381 List<UserInfo> profileUsers =
382 mUserManager.getProfiles(mCurrentUserHandle.getIdentifier());
383 for (UserInfo profileInfo : profileUsers) {
384 if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) {
385 return true;
386 }
Evan Charlton105d9772014-11-25 14:08:53 -0800387 }
388 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800389
Evan Charlton105d9772014-11-25 14:08:53 -0800390 return false;
391 }
392
393 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
394 return resolveComponent(phoneAccountHandle.getComponentName(),
395 phoneAccountHandle.getUserHandle());
396 }
397
398 private List<ResolveInfo> resolveComponent(ComponentName componentName,
399 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800400 PackageManager pm = mContext.getPackageManager();
401 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
402 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800403 try {
404 if (userHandle != null) {
405 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
406 } else {
407 return pm.queryIntentServices(intent, 0);
408 }
409 } catch (SecurityException e) {
410 Log.v(this, "%s is not visible for the calling user", componentName);
411 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800412 }
mike dooley10a58312014-11-06 13:46:19 -0800413 }
414
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700415 /**
416 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
417 *
418 * @return The list of {@link PhoneAccountHandle}s.
419 */
Ihab Awad293edf22014-07-24 17:52:29 -0700420 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
Santos Cordon6a212642015-05-08 16:35:23 -0700421 return getPhoneAccountHandles(0, null, null);
Ihab Awad293edf22014-07-24 17:52:29 -0700422 }
423
424 public List<PhoneAccount> getAllPhoneAccounts() {
Santos Cordon6a212642015-05-08 16:35:23 -0700425 return getPhoneAccounts(0, null, null);
Santos Cordonafe59e52014-08-22 16:48:43 -0700426 }
427
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700428 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700429 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700430 * specified URI scheme.
431 *
432 * @param uriScheme The URI scheme.
433 * @return The phone account handles.
434 */
Nancy Chen309198e2014-09-15 18:02:49 -0700435 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) {
Santos Cordon6a212642015-05-08 16:35:23 -0700436 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme, null);
437 }
438
439 /**
440 * Retrieves a list of all the SIM-based phone accounts.
441 */
442 public List<PhoneAccountHandle> getSimPhoneAccounts() {
443 return getPhoneAccountHandles(
444 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
445 null, null);
Tyler Gunn84253572014-09-02 14:50:05 -0700446 }
447
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700448 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700449 * Retrieves a list of all phone accounts registered by a specified package.
450 *
451 * @param packageName The name of the package that registered the phone accounts.
452 * @return The phone account handles.
453 */
454 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
Santos Cordon6a212642015-05-08 16:35:23 -0700455 return getPhoneAccountHandles(0, null, packageName);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700456 }
457
458 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700459 * Retrieves a list of all phone account handles with the connection manager capability.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700460 *
461 * @return The phone account handles.
462 */
463 public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() {
Santos Cordon6a212642015-05-08 16:35:23 -0700464 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null);
Santos Cordon176ae282014-07-14 02:02:14 -0700465 }
466
Ihab Awad104f8062014-07-17 11:29:35 -0700467 // TODO: Should we implement an artificial limit for # of accounts associated with a single
468 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700469 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700470 // Enforce the requirement that a connection service for a phone account has the correct
471 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700472 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700473 Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.",
474 account.getAccountHandle());
475 throw new SecurityException(
476 "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission.");
477 }
478
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700479 addOrReplacePhoneAccount(account);
480 }
481
482 /**
483 * Adds a {@code PhoneAccount}, replacing an existing one if found.
484 *
485 * @param account The {@code PhoneAccount} to add or replace.
486 */
487 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800488 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
489 account.getAccountHandle(), account);
490
Santos Cordon6a212642015-05-08 16:35:23 -0700491 PhoneAccount oldAccount = getPhoneAccount(account.getAccountHandle());
492 if (oldAccount != null) {
493 mState.accounts.remove(oldAccount);
Santos Cordon176ae282014-07-14 02:02:14 -0700494 }
Santos Cordon6a212642015-05-08 16:35:23 -0700495 mState.accounts.add(account);
Santos Cordon176ae282014-07-14 02:02:14 -0700496
Ihab Awad293edf22014-07-24 17:52:29 -0700497 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700498 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700499 }
500
Evan Charlton89176372014-07-19 18:23:09 -0700501 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700502 PhoneAccount account = getPhoneAccount(accountHandle);
503 if (account != null) {
504 if (mState.accounts.remove(account)) {
505 write();
506 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700507 }
508 }
Santos Cordon176ae282014-07-14 02:02:14 -0700509 }
510
Tyler Gunnd900ce62014-08-13 11:40:59 -0700511 /**
512 * Un-registers all phone accounts associated with a specified package.
513 *
514 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800515 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700516 */
Evan Charlton105d9772014-11-25 14:08:53 -0800517 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700518 boolean accountsRemoved = false;
519 Iterator<PhoneAccount> it = mState.accounts.iterator();
520 while (it.hasNext()) {
521 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800522 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
523 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
524 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700525 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
526 it.remove();
527 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700528 }
529 }
530
Tyler Gunnd900ce62014-08-13 11:40:59 -0700531 if (accountsRemoved) {
532 write();
533 fireAccountsChanged();
534 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700535 }
536
Nancy Chen140004a2014-10-15 15:48:38 -0700537 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700538 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700539 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
540 }
541
Ihab Awadb78b2762014-07-25 15:16:23 -0700542 public void addListener(Listener l) {
543 mListeners.add(l);
544 }
545
546 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700547 if (l != null) {
548 mListeners.remove(l);
549 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700550 }
551
552 private void fireAccountsChanged() {
553 for (Listener l : mListeners) {
554 l.onAccountsChanged(this);
555 }
556 }
557
558 private void fireDefaultOutgoingChanged() {
559 for (Listener l : mListeners) {
560 l.onDefaultOutgoingChanged(this);
561 }
562 }
563
564 private void fireSimCallManagerChanged() {
565 for (Listener l : mListeners) {
566 l.onSimCallManagerChanged(this);
567 }
Santos Cordon176ae282014-07-14 02:02:14 -0700568 }
569
Tyler Gunncb59b672014-08-20 09:02:11 -0700570 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700571 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Tyler Gunncb59b672014-08-20 09:02:11 -0700572 * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission.
573 *
574 * @param phoneAccountHandle The phone account to check.
575 * @return {@code True} if the phone account has permission.
576 */
Santos Cordon6a212642015-05-08 16:35:23 -0700577 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800578 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
579 if (resolveInfos.isEmpty()) {
580 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700581 return false;
582 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800583 for (ResolveInfo resolveInfo : resolveInfos) {
584 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Santos Cordon6a212642015-05-08 16:35:23 -0700585 if (serviceInfo == null ||
586 !Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission)) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800587 return false;
588 }
589 }
590 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700591 }
592
Santos Cordon6a212642015-05-08 16:35:23 -0700593 //
594 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
595 //
Ihab Awad293edf22014-07-24 17:52:29 -0700596
Santos Cordonafe59e52014-08-22 16:48:43 -0700597 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700598 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700599 *
Santos Cordon6a212642015-05-08 16:35:23 -0700600 * @param handle
601 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700602 */
Santos Cordon6a212642015-05-08 16:35:23 -0700603 PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
604 for (PhoneAccount m : mState.accounts) {
605 if (Objects.equals(handle, m.getAccountHandle())) {
606 return m;
607 }
608 }
609 return null;
610 }
611
612 /**
613 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
614 * account before returning it. The current user is the active user on the actual android
615 * device.
616 */
617 public PhoneAccount getPhoneAccountCheckCallingUser(PhoneAccountHandle handle) {
618 PhoneAccount account = getPhoneAccount(handle);
619 if (account != null && isVisibleForUser(account)) {
620 return account;
621 }
622 return null;
623 }
624
625 /**
626 * Returns a list of phone account handles with the specified capabilities, uri scheme,
627 * and package name.
628 */
629 private List<PhoneAccountHandle> getPhoneAccountHandles(
630 int capabilities, String uriScheme, String packageName) {
631 List<PhoneAccountHandle> handles = new ArrayList<>();
632 for (PhoneAccount account : getPhoneAccounts(capabilities, uriScheme, packageName)) {
633 handles.add(account.getAccountHandle());
634 }
635 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700636 }
637
638 /**
639 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700640 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700641 *
Santos Cordon6a212642015-05-08 16:35:23 -0700642 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
643 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700644 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700645 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700646 */
Santos Cordon6a212642015-05-08 16:35:23 -0700647 private List<PhoneAccount> getPhoneAccounts(
648 int capabilities, String uriScheme, String packageName) {
649 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700650 for (PhoneAccount m : mState.accounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700651 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800652 // Account doesn't have the right capabilities; skip this one.
653 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700654 }
Evan Charlton105d9772014-11-25 14:08:53 -0800655 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
656 // Account doesn't support this URI scheme; skip this one.
657 continue;
658 }
Santos Cordon6a212642015-05-08 16:35:23 -0700659 PhoneAccountHandle handle = m.getAccountHandle();
660
661 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800662 // This component cannot be resolved anymore; skip this one.
663 continue;
664 }
Santos Cordon6a212642015-05-08 16:35:23 -0700665 if (packageName != null &&
666 !packageName.equals(handle.getComponentName().getPackageName())) {
667 // Not the right package name; skip this one.
668 continue;
669 }
Evan Charlton105d9772014-11-25 14:08:53 -0800670 if (!isVisibleForUser(m)) {
671 // Account is not visible for the current user; skip this one.
672 continue;
673 }
Santos Cordon6a212642015-05-08 16:35:23 -0700674 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700675 }
Santos Cordon6a212642015-05-08 16:35:23 -0700676 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700677 }
678
Santos Cordon6a212642015-05-08 16:35:23 -0700679 //
680 // State Implementation for PhoneAccountRegistrar
681 //
682
Ihab Awad293edf22014-07-24 17:52:29 -0700683 /**
684 * The state of this {@code PhoneAccountRegistrar}.
685 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700686 @VisibleForTesting
687 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700688 /**
689 * The account selected by the user to be employed by default for making outgoing calls.
690 * If the user has not made such a selection, then this is null.
691 */
692 public PhoneAccountHandle defaultOutgoing = null;
693
694 /**
Ihab Awadb78b2762014-07-25 15:16:23 -0700695 * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which
Ihab Awad293edf22014-07-24 17:52:29 -0700696 * manages and optimizes a user's PSTN SIM connections.
697 */
698 public PhoneAccountHandle simCallManager;
699
700 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700701 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700702 */
703 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700704
705 /**
706 * The version number of the State data.
707 */
708 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700709 }
710
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700711 /**
712 * Dumps the state of the {@link CallsManager}.
713 *
714 * @param pw The {@code IndentingPrintWriter} to write the state to.
715 */
716 public void dump(IndentingPrintWriter pw) {
717 if (mState != null) {
718 pw.println("xmlVersion: " + mState.versionNumber);
719 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
720 mState.defaultOutgoing));
721 pw.println("simCallManager: " + (mState.simCallManager == null ? "none" :
722 mState.simCallManager));
723 pw.println("phoneAccounts:");
724 pw.increaseIndent();
725 for (PhoneAccount phoneAccount : mState.accounts) {
726 pw.println(phoneAccount);
727 }
728 pw.decreaseIndent();
729 }
730 }
731
Ihab Awad293edf22014-07-24 17:52:29 -0700732 ////////////////////////////////////////////////////////////////////////////////////////////////
733 //
734 // State management
735 //
736
737 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700738 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700739 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700740 os = mAtomicFile.startWrite();
741 boolean success = false;
742 try {
743 XmlSerializer serializer = new FastXmlSerializer();
744 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800745 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700746 serializer.flush();
747 success = true;
748 } finally {
749 if (success) {
750 mAtomicFile.finishWrite(os);
751 } else {
752 mAtomicFile.failWrite(os);
753 }
754 }
755 } catch (IOException e) {
756 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700757 }
758 }
759
Ihab Awadb78b2762014-07-25 15:16:23 -0700760 private void read() {
761 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700762 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700763 is = mAtomicFile.openRead();
764 } catch (FileNotFoundException ex) {
765 return;
766 }
767
Tyler Gunn84253572014-09-02 14:50:05 -0700768 boolean versionChanged = false;
769
Ihab Awadb78b2762014-07-25 15:16:23 -0700770 XmlPullParser parser;
771 try {
772 parser = Xml.newPullParser();
773 parser.setInput(new BufferedInputStream(is), null);
774 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700775 mState = readFromXml(parser, mContext);
776 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
777
Ihab Awadb78b2762014-07-25 15:16:23 -0700778 } catch (IOException | XmlPullParserException e) {
779 Log.e(this, e, "Reading state from XML file");
780 mState = new State();
781 } finally {
782 try {
783 is.close();
784 } catch (IOException e) {
785 Log.e(this, e, "Closing InputStream");
786 }
Ihab Awad104f8062014-07-17 11:29:35 -0700787 }
Tyler Gunn84253572014-09-02 14:50:05 -0700788
Evan Charlton105d9772014-11-25 14:08:53 -0800789 // Verify all of the UserHandles.
790 List<PhoneAccount> badAccounts = new ArrayList<>();
791 for (PhoneAccount phoneAccount : mState.accounts) {
792 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
793 if (userHandle == null) {
794 Log.w(this, "Missing UserHandle for %s", phoneAccount);
795 badAccounts.add(phoneAccount);
796 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
797 Log.w(this, "User does not exist for %s", phoneAccount);
798 badAccounts.add(phoneAccount);
799 }
800 }
801 mState.accounts.removeAll(badAccounts);
802
Tyler Gunn84253572014-09-02 14:50:05 -0700803 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -0800804 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -0700805 write();
806 }
Santos Cordon176ae282014-07-14 02:02:14 -0700807 }
808
Evan Charlton105d9772014-11-25 14:08:53 -0800809 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700810 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -0800811 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -0700812 }
Ihab Awad104f8062014-07-17 11:29:35 -0700813
Tyler Gunn84253572014-09-02 14:50:05 -0700814 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700815 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700816 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700817 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700818 }
819
Ihab Awad293edf22014-07-24 17:52:29 -0700820 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700821 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700822 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700823 //
824
Ihab Awadb78b2762014-07-25 15:16:23 -0700825 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700826 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700827 private static final String LENGTH_ATTRIBUTE = "length";
828 private static final String VALUE_TAG = "value";
829
Ihab Awadb78b2762014-07-25 15:16:23 -0700830 /**
831 * Write the supplied object to XML
832 */
Evan Charlton105d9772014-11-25 14:08:53 -0800833 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700834 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700835
836 /**
837 * Read from the supplied XML into a new object, returning null in case of an
838 * unrecoverable schema mismatch or other data error. 'parser' must be already
839 * positioned at the first tag that is expected to have been emitted by this
840 * object's writeToXml(). This object tries to fail early without modifying
841 * 'parser' if it does not recognize the data it sees.
842 */
Tyler Gunn84253572014-09-02 14:50:05 -0700843 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700844 throws IOException, XmlPullParserException;
845
Ihab Awadd9f54382014-10-24 11:44:47 -0700846 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700847 throws IOException {
848 if (value != null) {
849 serializer.startTag(null, tagName);
850 serializer.text(Objects.toString(value));
851 serializer.endTag(null, tagName);
852 }
853 }
Tyler Gunn84253572014-09-02 14:50:05 -0700854
855 /**
856 * Serializes a string array.
857 *
858 * @param tagName The tag name for the string array.
859 * @param values The string values to serialize.
860 * @param serializer The serializer.
861 * @throws IOException
862 */
863 protected void writeStringList(String tagName, List<String> values,
864 XmlSerializer serializer)
865 throws IOException {
866
867 serializer.startTag(null, tagName);
868 if (values != null) {
869 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
870 for (String toSerialize : values) {
871 serializer.startTag(null, VALUE_TAG);
872 if (toSerialize != null ){
873 serializer.text(toSerialize);
874 }
Tyler Gunn84253572014-09-02 14:50:05 -0700875 serializer.endTag(null, VALUE_TAG);
876 }
877 } else {
878 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
879 }
880 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -0700881 }
Tyler Gunn84253572014-09-02 14:50:05 -0700882
Santos Cordon9c30c282015-05-13 16:28:27 -0700883 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -0700884 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -0700885 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -0700886 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -0700887 value.writeToStream(stream);
888 byte[] iconByteArray = stream.toByteArray();
889 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -0700890
891 serializer.startTag(null, tagName);
892 serializer.text(text);
893 serializer.endTag(null, tagName);
894 }
Tyler Gunn84253572014-09-02 14:50:05 -0700895 }
896
Evan Charlton105d9772014-11-25 14:08:53 -0800897 protected void writeLong(String tagName, long value, XmlSerializer serializer)
898 throws IOException {
899 serializer.startTag(null, tagName);
900 serializer.text(Long.valueOf(value).toString());
901 serializer.endTag(null, tagName);
902 }
903
Tyler Gunn84253572014-09-02 14:50:05 -0700904 /**
905 * Reads a string array from the XML parser.
906 *
907 * @param parser The XML parser.
908 * @return String array containing the parsed values.
909 * @throws IOException Exception related to IO.
910 * @throws XmlPullParserException Exception related to parsing.
911 */
912 protected List<String> readStringList(XmlPullParser parser)
913 throws IOException, XmlPullParserException {
914
915 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
916 List<String> arrayEntries = new ArrayList<String>(length);
917 String value = null;
918
919 if (length == 0) {
920 return arrayEntries;
921 }
922
923 int outerDepth = parser.getDepth();
924 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
925 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700926 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -0700927 value = parser.getText();
928 arrayEntries.add(value);
929 }
930 }
931
932 return arrayEntries;
933 }
Ihab Awadd9f54382014-10-24 11:44:47 -0700934
Santos Cordon9c30c282015-05-13 16:28:27 -0700935 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -0700936 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
937 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
938 }
Santos Cordon9c30c282015-05-13 16:28:27 -0700939
940 protected Icon readIcon(XmlPullParser parser) throws IOException {
941 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
942 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
943 return Icon.createFromStream(stream);
944 }
Ihab Awad104f8062014-07-17 11:29:35 -0700945 }
946
Ihab Awadb78b2762014-07-25 15:16:23 -0700947 @VisibleForTesting
948 public static final XmlSerialization<State> sStateXml =
949 new XmlSerialization<State>() {
950 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -0700951 private static final String DEFAULT_OUTGOING = "default_outgoing";
Ihab Awad293edf22014-07-24 17:52:29 -0700952 private static final String SIM_CALL_MANAGER = "sim_call_manager";
Ihab Awad104f8062014-07-17 11:29:35 -0700953 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -0700954 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -0700955
956 @Override
Evan Charlton105d9772014-11-25 14:08:53 -0800957 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700958 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700959 if (o != null) {
960 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -0700961 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -0700962
Ihab Awad26923222014-07-30 10:54:35 -0700963 if (o.defaultOutgoing != null) {
964 serializer.startTag(null, DEFAULT_OUTGOING);
Evan Charlton105d9772014-11-25 14:08:53 -0800965 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -0700966 serializer.endTag(null, DEFAULT_OUTGOING);
967 }
968
969 if (o.simCallManager != null) {
970 serializer.startTag(null, SIM_CALL_MANAGER);
Evan Charlton105d9772014-11-25 14:08:53 -0800971 sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -0700972 serializer.endTag(null, SIM_CALL_MANAGER);
973 }
974
975 serializer.startTag(null, ACCOUNTS);
976 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -0800977 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -0700978 }
979 serializer.endTag(null, ACCOUNTS);
980
981 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -0700982 }
Ihab Awad104f8062014-07-17 11:29:35 -0700983 }
984
985 @Override
Tyler Gunn84253572014-09-02 14:50:05 -0700986 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700987 throws IOException, XmlPullParserException {
988 if (parser.getName().equals(CLASS_STATE)) {
989 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -0700990
991 String rawVersion = parser.getAttributeValue(null, VERSION);
992 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
993 Integer.parseInt(rawVersion);
994
Ihab Awadb78b2762014-07-25 15:16:23 -0700995 int outerDepth = parser.getDepth();
996 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
997 if (parser.getName().equals(DEFAULT_OUTGOING)) {
998 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700999 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
1000 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001001 } else if (parser.getName().equals(SIM_CALL_MANAGER)) {
1002 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001003 s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser,
1004 s.versionNumber, context);
Evan Charlton105d9772014-11-25 14:08:53 -08001005 if (s.simCallManager.getUserHandle() == null) {
1006 // This should never happen, but handle the upgrade case.
1007 s.simCallManager = new PhoneAccountHandle(
1008 s.simCallManager.getComponentName(),
1009 s.simCallManager.getId(),
1010 Process.myUserHandle());
1011 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001012 } else if (parser.getName().equals(ACCOUNTS)) {
1013 int accountsDepth = parser.getDepth();
1014 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001015 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1016 s.versionNumber, context);
1017
1018 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001019 s.accounts.add(account);
1020 }
1021 }
Ihab Awad104f8062014-07-17 11:29:35 -07001022 }
1023 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001024 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001025 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001026 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001027 }
1028 };
1029
Ihab Awadb78b2762014-07-25 15:16:23 -07001030 @VisibleForTesting
1031 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1032 new XmlSerialization<PhoneAccount>() {
1033 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1034 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001035 private static final String ADDRESS = "handle";
1036 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001037 private static final String CAPABILITIES = "capabilities";
1038 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001039 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1040 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001041 private static final String ICON_TINT = "icon_tint";
1042 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001043 private static final String LABEL = "label";
1044 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001045 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001046 private static final String ICON = "icon";
Ihab Awad104f8062014-07-17 11:29:35 -07001047
1048 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001049 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001050 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001051 if (o != null) {
1052 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001053
Ihab Awad26923222014-07-30 10:54:35 -07001054 if (o.getAccountHandle() != null) {
1055 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001056 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001057 serializer.endTag(null, ACCOUNT_HANDLE);
1058 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001059
Ihab Awadd9f54382014-10-24 11:44:47 -07001060 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1061 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1062 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001063 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001064 writeTextIfNonNull(HIGHLIGHT_COLOR,
1065 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001066 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1067 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001068 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001069
Ihab Awad26923222014-07-30 10:54:35 -07001070 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1071 }
Ihab Awad104f8062014-07-17 11:29:35 -07001072 }
1073
Tyler Gunn84253572014-09-02 14:50:05 -07001074 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001075 throws IOException, XmlPullParserException {
1076 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1077 int outerDepth = parser.getDepth();
1078 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001079 Uri address = null;
1080 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001081 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001082 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001083 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001084 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001085 int iconTint = PhoneAccount.NO_ICON_TINT;
1086 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001087 String label = null;
1088 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001089 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001090 Icon icon = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001091
1092 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1093 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1094 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001095 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1096 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001097 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001098 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001099 address = Uri.parse(parser.getText());
1100 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001101 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001102 String nextText = parser.getText();
1103 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001104 } else if (parser.getName().equals(CAPABILITIES)) {
1105 parser.next();
1106 capabilities = Integer.parseInt(parser.getText());
1107 } else if (parser.getName().equals(ICON_RES_ID)) {
1108 parser.next();
1109 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001110 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1111 parser.next();
1112 iconPackageName = parser.getText();
1113 } else if (parser.getName().equals(ICON_BITMAP)) {
1114 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001115 iconBitmap = readBitmap(parser);
1116 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001117 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001118 iconTint = Integer.parseInt(parser.getText());
1119 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1120 parser.next();
1121 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001122 } else if (parser.getName().equals(LABEL)) {
1123 parser.next();
1124 label = parser.getText();
1125 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1126 parser.next();
1127 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001128 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1129 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001130 } else if (parser.getName().equals(ICON)) {
1131 parser.next();
1132 icon = readIcon(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -07001133 }
1134 }
Tyler Gunn84253572014-09-02 14:50:05 -07001135
1136 // Upgrade older phone accounts to specify the supported URI schemes.
1137 if (version < 2) {
1138 ComponentName sipComponentName = new ComponentName("com.android.phone",
1139 "com.android.services.telephony.sip.SipConnectionService");
1140
1141 supportedUriSchemes = new ArrayList<>();
1142
1143 // Handle the SIP connection service.
1144 // Check the system settings to see if it also should handle "tel" calls.
1145 if (accountHandle.getComponentName().equals(sipComponentName)) {
1146 boolean useSipForPstn = useSipForPstnCalls(context);
1147 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1148 if (useSipForPstn) {
1149 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1150 }
1151 } else {
1152 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1153 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1154 }
1155 }
1156
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001157 // Upgrade older phone accounts with explicit package name
1158 if (version < 5) {
1159 if (iconBitmap == null) {
1160 iconPackageName = accountHandle.getComponentName().getPackageName();
1161 }
1162 }
1163
1164 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001165 .setAddress(address)
1166 .setSubscriptionAddress(subscriptionAddress)
1167 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001168 .setShortDescription(shortDescription)
1169 .setSupportedUriSchemes(supportedUriSchemes)
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001170 .setHighlightColor(highlightColor);
1171
Santos Cordon9c30c282015-05-13 16:28:27 -07001172 if (icon != null) {
1173 builder.setIcon(icon);
1174 } else if (iconBitmap != null) {
1175 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1176 } else if (!TextUtils.isEmpty(iconPackageName)) {
1177 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1178 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001179 }
1180
1181 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001182 }
1183 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001184 }
Tyler Gunn84253572014-09-02 14:50:05 -07001185
1186 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001187 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1188 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001189 *
1190 * @param context The context.
1191 * @return {@code True} if SIP should be used for all calls.
1192 */
1193 private boolean useSipForPstnCalls(Context context) {
1194 String option = Settings.System.getString(context.getContentResolver(),
1195 Settings.System.SIP_CALL_OPTIONS);
1196 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1197 return option.equals(Settings.System.SIP_ALWAYS);
1198 }
Ihab Awad104f8062014-07-17 11:29:35 -07001199 };
1200
Ihab Awadb78b2762014-07-25 15:16:23 -07001201 @VisibleForTesting
1202 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1203 new XmlSerialization<PhoneAccountHandle>() {
1204 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001205 private static final String COMPONENT_NAME = "component_name";
1206 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001207 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001208
1209 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001210 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001211 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001212 if (o != null) {
1213 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001214
Ihab Awad26923222014-07-30 10:54:35 -07001215 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001216 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001217 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1218 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001219
Ihab Awadd9f54382014-10-24 11:44:47 -07001220 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001221
Evan Charlton105d9772014-11-25 14:08:53 -08001222 if (o.getUserHandle() != null && context != null) {
1223 UserManager userManager = UserManager.get(context);
1224 writeLong(USER_SERIAL_NUMBER,
1225 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1226 }
1227
Ihab Awad26923222014-07-30 10:54:35 -07001228 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1229 }
Ihab Awad104f8062014-07-17 11:29:35 -07001230 }
1231
1232 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001233 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001234 throws IOException, XmlPullParserException {
1235 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1236 String componentNameString = null;
1237 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001238 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001239 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001240
1241 UserManager userManager = UserManager.get(context);
1242
Ihab Awadb78b2762014-07-25 15:16:23 -07001243 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1244 if (parser.getName().equals(COMPONENT_NAME)) {
1245 parser.next();
1246 componentNameString = parser.getText();
1247 } else if (parser.getName().equals(ID)) {
1248 parser.next();
1249 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001250 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1251 parser.next();
1252 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001253 }
1254 }
Ihab Awad26923222014-07-30 10:54:35 -07001255 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001256 UserHandle userHandle = null;
1257 if (userSerialNumberString != null) {
1258 try {
1259 long serialNumber = Long.parseLong(userSerialNumberString);
1260 userHandle = userManager.getUserForSerialNumber(serialNumber);
1261 } catch (NumberFormatException e) {
1262 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1263 }
1264 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001265 return new PhoneAccountHandle(
1266 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001267 idString,
1268 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001269 }
1270 }
1271 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001272 }
1273 };
Santos Cordon176ae282014-07-14 02:02:14 -07001274}