blob: 35502015b16b7bed34b038253f6b6e0c32df3565 [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 Charltonaf51ceb2014-07-30 11:56:36 -070020import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Tyler Gunncb59b672014-08-20 09:02:11 -070023import android.content.pm.ServiceInfo;
Ihab Awadd9f54382014-10-24 11:44:47 -070024import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Tyler Gunn84253572014-09-02 14:50:05 -070026import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070027import android.telecom.ConnectionService;
28import android.telecom.PhoneAccount;
29import android.telecom.PhoneAccountHandle;
Nancy Chen140004a2014-10-15 15:48:38 -070030import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070031import android.telephony.SubscriptionManager;
Santos Cordon176ae282014-07-14 02:02:14 -070032import android.content.ComponentName;
33import android.content.Context;
Santos Cordon176ae282014-07-14 02:02:14 -070034import android.net.Uri;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070035import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070036import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070037import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070038import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070039
Tyler Gunn91d43cf2014-09-17 12:19:39 -070040// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070041import com.android.internal.annotations.VisibleForTesting;
42import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070043import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070044import com.android.internal.util.XmlUtils;
45
Evan Charltonaf51ceb2014-07-30 11:56:36 -070046import org.xmlpull.v1.XmlPullParser;
47import org.xmlpull.v1.XmlPullParserException;
48import org.xmlpull.v1.XmlSerializer;
49
Ihab Awadb78b2762014-07-25 15:16:23 -070050import java.io.BufferedInputStream;
51import java.io.BufferedOutputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070052import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070053import java.io.File;
54import java.io.FileNotFoundException;
55import java.io.FileOutputStream;
56import java.io.IOException;
57import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070058import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070059import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070060import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070061import java.util.ArrayList;
Tyler Gunnd900ce62014-08-13 11:40:59 -070062import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070063import java.util.List;
64import java.util.Objects;
Ihab Awadb78b2762014-07-25 15:16:23 -070065import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070066
67/**
Evan Charlton89176372014-07-19 18:23:09 -070068 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Tyler Gunn7cc70b42014-09-12 22:17:27 -070069 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as implemented in
70 * {@link TelecomServiceImpl}, with the notable exception that {@link TelecomServiceImpl} is
Ihab Awad104f8062014-07-17 11:29:35 -070071 * responsible for security checking to make sure that the caller has proper authority over
Evan Charlton89176372014-07-19 18:23:09 -070072 * the {@code ComponentName}s they are declaring in their {@code PhoneAccountHandle}s.
Santos Cordon176ae282014-07-14 02:02:14 -070073 */
Ihab Awadb78b2762014-07-25 15:16:23 -070074public final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -070075
Yorke Lee5e8836a2014-08-22 15:25:18 -070076 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
77 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
78
Ihab Awadb78b2762014-07-25 15:16:23 -070079 public abstract static class Listener {
80 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
81 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
82 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
83 }
84
85 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -070086 @VisibleForTesting
Ihab Awadd9f54382014-10-24 11:44:47 -070087 public static final int EXPECTED_STATE_VERSION = 4;
Tyler Gunn84253572014-09-02 14:50:05 -070088
89 /** Keep in sync with the same in SipSettings.java */
90 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -070091
92 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
93 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -070094 private final Context mContext;
Ihab Awadb78b2762014-07-25 15:16:23 -070095 private State mState;
Santos Cordon176ae282014-07-14 02:02:14 -070096
Nancy Chen06ce0622014-10-23 01:17:35 +000097 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -070098 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -070099 this(context, FILE_NAME);
100 }
101
102 @VisibleForTesting
103 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700104 // TODO: This file path is subject to change -- it is storing the phone account registry
105 // state file in the path /data/system/users/0/, which is likely not correct in a
106 // multi-user setting.
107 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
108 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
109 getAbsolutePath();
110 mAtomicFile = new AtomicFile(new File(filePath, fileName));
111 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700112 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700113
Ihab Awadb78b2762014-07-25 15:16:23 -0700114 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700115 mContext = context;
Ihab Awadb78b2762014-07-25 15:16:23 -0700116 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700117 }
118
Tyler Gunn84253572014-09-02 14:50:05 -0700119 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700120 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
121 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
122 * subscription id.
123 * @param accountHandle The handle for the phone account for which to retrieve the
124 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700125 * @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 -0700126 */
Wink Saville35850602014-10-23 15:57:21 -0700127 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Nancy Chen140004a2014-10-15 15:48:38 -0700128 PhoneAccount account = getPhoneAccount(accountHandle);
129 if (account == null || !account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) ||
130 !TextUtils.isDigitsOnly(accountHandle.getId())) {
131 // Since no decimals or negative numbers can be valid subscription ids, only a string of
132 // numbers can be subscription id
133 return -1;
134 }
Wink Saville35850602014-10-23 15:57:21 -0700135 return Integer.parseInt(accountHandle.getId());
Nancy Chen140004a2014-10-15 15:48:38 -0700136 }
137
138 /**
Tyler Gunn84253572014-09-02 14:50:05 -0700139 * Retrieves the default outgoing phone account supporting the specified uriScheme.
140 * @param uriScheme The URI scheme for the outgoing call.
141 * @return The {@link PhoneAccountHandle} to use.
142 */
143 public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700144 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700145
Yorke Lee5e8836a2014-08-22 15:25:18 -0700146 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700147 // If there is a default PhoneAccount, ensure it supports calls to handles with the
148 // specified uriScheme.
149 final PhoneAccount userSelectedAccount = getPhoneAccount(userSelected);
150 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
151 return userSelected;
152 }
Ihab Awad293edf22014-07-24 17:52:29 -0700153 }
154
Nancy Chen309198e2014-09-15 18:02:49 -0700155 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700156 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700157 case 0:
158 // There are no accounts, so there can be no default
159 return null;
160 case 1:
161 // There is only one account, which is by definition the default
Ihab Awad6fb37c82014-08-07 19:48:57 -0700162 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700163 default:
164 // There are multiple accounts with no selected default
165 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700166 }
Ihab Awad104f8062014-07-17 11:29:35 -0700167 }
Santos Cordon176ae282014-07-14 02:02:14 -0700168
Yorke Lee5e8836a2014-08-22 15:25:18 -0700169 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
170 if (mState.defaultOutgoing != null) {
171 // Return the registered outgoing default iff it still exists (we keep a sticky
172 // default to survive account deletion and re-addition)
173 for (int i = 0; i < mState.accounts.size(); i++) {
174 if (mState.accounts.get(i).getAccountHandle().equals(mState.defaultOutgoing)) {
175 return mState.defaultOutgoing;
176 }
177 }
178 // At this point, there was a registered default but it has been deleted; proceed
179 // as though there were no default
180 }
181 return null;
182 }
183
Andrew Leea51a3862014-09-03 14:58:45 -0700184 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700185 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700186 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700187 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700188 } else {
189 boolean found = false;
Ihab Awad293edf22014-07-24 17:52:29 -0700190 for (PhoneAccount m : mState.accounts) {
Evan Charlton94d01622014-07-20 12:32:05 -0700191 if (Objects.equals(accountHandle, m.getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700192 found = true;
193 break;
194 }
Santos Cordon176ae282014-07-14 02:02:14 -0700195 }
Ihab Awad104f8062014-07-17 11:29:35 -0700196
197 if (!found) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700198 Log.w(this, "Trying to set nonexistent default outgoing %s",
199 accountHandle);
200 return;
201 }
202
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700203 if (!getPhoneAccount(accountHandle).hasCapabilities(
204 PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700205 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700206 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700207 return;
208 }
209
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700210 if (getPhoneAccount(accountHandle).hasCapabilities(
211 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
212 // If the account selected is a SIM account, propagate down to the subscription
213 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700214 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700215 SubscriptionManager.setDefaultVoiceSubId(subId);
216 }
217
Ihab Awad293edf22014-07-24 17:52:29 -0700218 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700219 }
220
Ihab Awad293edf22014-07-24 17:52:29 -0700221 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700222 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700223 }
224
Ihab Awad293edf22014-07-24 17:52:29 -0700225 public void setSimCallManager(PhoneAccountHandle callManager) {
226 if (callManager != null) {
227 PhoneAccount callManagerAccount = getPhoneAccount(callManager);
228 if (callManagerAccount == null) {
229 Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager);
230 return;
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700231 } else if (!callManagerAccount.hasCapabilities(
232 PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) {
Ihab Awad293edf22014-07-24 17:52:29 -0700233 Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount);
234 return;
235 }
Yorke Lee5e8836a2014-08-22 15:25:18 -0700236 } else {
237 callManager = NO_ACCOUNT_SELECTED;
Ihab Awad293edf22014-07-24 17:52:29 -0700238 }
239 mState.simCallManager = callManager;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700240
Ihab Awad293edf22014-07-24 17:52:29 -0700241 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700242 fireSimCallManagerChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700243 }
244
245 public PhoneAccountHandle getSimCallManager() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700246 if (mState.simCallManager != null) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700247 if (NO_ACCOUNT_SELECTED.equals(mState.simCallManager)) {
248 return null;
249 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700250 // Return the registered sim call manager iff it still exists (we keep a sticky
251 // setting to survive account deletion and re-addition)
252 for (int i = 0; i < mState.accounts.size(); i++) {
mike dooley10a58312014-11-06 13:46:19 -0800253 if (mState.accounts.get(i).getAccountHandle().equals(mState.simCallManager)
254 && !resolveComponent(mState.simCallManager.getComponentName()).isEmpty()) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700255 return mState.simCallManager;
256 }
257 }
258 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700259
260 // See if the OEM has specified a default one.
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700261 String defaultConnectionMgr =
Santos Cordonafe59e52014-08-22 16:48:43 -0700262 mContext.getResources().getString(R.string.default_connection_manager_component);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700263 if (!TextUtils.isEmpty(defaultConnectionMgr)) {
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700264 ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700265 // Make sure that the component can be resolved.
mike dooley10a58312014-11-06 13:46:19 -0800266 List<ResolveInfo> resolveInfos = resolveComponent(componentName);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700267 if (!resolveInfos.isEmpty()) {
268 // See if there is registered PhoneAccount by this component.
269 List<PhoneAccountHandle> handles = getAllPhoneAccountHandles();
270 for (PhoneAccountHandle handle : handles) {
271 if (componentName.equals(handle.getComponentName())) {
272 return handle;
273 }
274 }
275 Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName);
276 } else {
277 Log.d(this, "%s could not be resolved; not using as default", componentName);
278 }
279 } else {
280 Log.v(this, "No default connection manager specified");
281 }
282
Ihab Awadb78b2762014-07-25 15:16:23 -0700283 return null;
Ihab Awad293edf22014-07-24 17:52:29 -0700284 }
285
mike dooley10a58312014-11-06 13:46:19 -0800286 private List<ResolveInfo> resolveComponent(ComponentName componentName) {
287 PackageManager pm = mContext.getPackageManager();
288 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
289 intent.setComponent(componentName);
290 return pm.queryIntentServices(intent, 0);
291 }
292
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700293 /**
294 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
295 *
296 * @return The list of {@link PhoneAccountHandle}s.
297 */
Ihab Awad293edf22014-07-24 17:52:29 -0700298 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
299 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
300 for (PhoneAccount m : mState.accounts) {
301 accountHandles.add(m.getAccountHandle());
302 }
303 return accountHandles;
304 }
305
306 public List<PhoneAccount> getAllPhoneAccounts() {
307 return new ArrayList<>(mState.accounts);
308 }
309
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700310 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700311 * Determines the number of all {@link PhoneAccount}s.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700312 *
Nancy Chen309198e2014-09-15 18:02:49 -0700313 * @return The total number {@link PhoneAccount}s.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700314 */
315 public int getAllPhoneAccountsCount() {
316 return mState.accounts.size();
317 }
318
319 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700320 * Retrieves a list of all call provider phone accounts.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700321 *
322 * @return The phone account handles.
323 */
Nancy Chen309198e2014-09-15 18:02:49 -0700324 public List<PhoneAccountHandle> getCallCapablePhoneAccounts() {
Santos Cordonafe59e52014-08-22 16:48:43 -0700325 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER);
326 }
327
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700328 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700329 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700330 * specified URI scheme.
331 *
332 * @param uriScheme The URI scheme.
333 * @return The phone account handles.
334 */
Nancy Chen309198e2014-09-15 18:02:49 -0700335 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) {
336 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme);
Tyler Gunn84253572014-09-02 14:50:05 -0700337 }
338
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700339 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700340 * Retrieves a list of all phone accounts registered by a specified package.
341 *
342 * @param packageName The name of the package that registered the phone accounts.
343 * @return The phone account handles.
344 */
345 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
346 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
347 for (PhoneAccount m : mState.accounts) {
348 if (Objects.equals(
349 packageName,
350 m.getAccountHandle().getComponentName().getPackageName())) {
351 accountHandles.add(m.getAccountHandle());
352 }
353 }
354 return accountHandles;
355 }
356
357 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700358 * Retrieves a list of all phone account handles with the connection manager capability.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700359 *
360 * @return The phone account handles.
361 */
362 public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() {
Santos Cordone3d82452014-09-15 13:44:29 -0700363 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER,
Nancy Chen309198e2014-09-15 18:02:49 -0700364 null /* supportedUriScheme */);
Santos Cordon176ae282014-07-14 02:02:14 -0700365 }
366
Ihab Awad293edf22014-07-24 17:52:29 -0700367 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
368 for (PhoneAccount m : mState.accounts) {
369 if (Objects.equals(handle, m.getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700370 return m;
Santos Cordon176ae282014-07-14 02:02:14 -0700371 }
372 }
373 return null;
374 }
375
Ihab Awad104f8062014-07-17 11:29:35 -0700376 // TODO: Should we implement an artificial limit for # of accounts associated with a single
377 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700378 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700379 // Enforce the requirement that a connection service for a phone account has the correct
380 // permission.
381 if (!phoneAccountHasPermission(account.getAccountHandle())) {
382 Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.",
383 account.getAccountHandle());
384 throw new SecurityException(
385 "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission.");
386 }
387
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700388 addOrReplacePhoneAccount(account);
389 }
390
391 /**
392 * Adds a {@code PhoneAccount}, replacing an existing one if found.
393 *
394 * @param account The {@code PhoneAccount} to add or replace.
395 */
396 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad293edf22014-07-24 17:52:29 -0700397 mState.accounts.add(account);
Ihab Awad104f8062014-07-17 11:29:35 -0700398 // Search for duplicates and remove any that are found.
Ihab Awad293edf22014-07-24 17:52:29 -0700399 for (int i = 0; i < mState.accounts.size() - 1; i++) {
400 if (Objects.equals(
401 account.getAccountHandle(), mState.accounts.get(i).getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700402 // replace existing entry.
Ihab Awad293edf22014-07-24 17:52:29 -0700403 mState.accounts.remove(i);
Ihab Awad104f8062014-07-17 11:29:35 -0700404 break;
Santos Cordon176ae282014-07-14 02:02:14 -0700405 }
406 }
407
Ihab Awad293edf22014-07-24 17:52:29 -0700408 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700409 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700410 }
411
Evan Charlton89176372014-07-19 18:23:09 -0700412 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Ihab Awad293edf22014-07-24 17:52:29 -0700413 for (int i = 0; i < mState.accounts.size(); i++) {
414 if (Objects.equals(accountHandle, mState.accounts.get(i).getAccountHandle())) {
415 mState.accounts.remove(i);
Ihab Awad104f8062014-07-17 11:29:35 -0700416 break;
417 }
418 }
419
Ihab Awad293edf22014-07-24 17:52:29 -0700420 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700421 fireAccountsChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700422 }
423
Tyler Gunnd900ce62014-08-13 11:40:59 -0700424 /**
425 * Un-registers all phone accounts associated with a specified package.
426 *
427 * @param packageName The package for which phone accounts will be removed.
428 */
Ihab Awad104f8062014-07-17 11:29:35 -0700429 public void clearAccounts(String packageName) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700430 boolean accountsRemoved = false;
431 Iterator<PhoneAccount> it = mState.accounts.iterator();
432 while (it.hasNext()) {
433 PhoneAccount phoneAccount = it.next();
Ihab Awad104f8062014-07-17 11:29:35 -0700434 if (Objects.equals(
435 packageName,
Tyler Gunnd900ce62014-08-13 11:40:59 -0700436 phoneAccount.getAccountHandle().getComponentName().getPackageName())) {
437 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
438 it.remove();
439 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700440 }
441 }
442
Tyler Gunnd900ce62014-08-13 11:40:59 -0700443 if (accountsRemoved) {
444 write();
445 fireAccountsChanged();
446 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700447 }
448
Nancy Chen140004a2014-10-15 15:48:38 -0700449 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700450 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700451 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
452 }
453
Ihab Awadb78b2762014-07-25 15:16:23 -0700454 public void addListener(Listener l) {
455 mListeners.add(l);
456 }
457
458 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700459 if (l != null) {
460 mListeners.remove(l);
461 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700462 }
463
464 private void fireAccountsChanged() {
465 for (Listener l : mListeners) {
466 l.onAccountsChanged(this);
467 }
468 }
469
470 private void fireDefaultOutgoingChanged() {
471 for (Listener l : mListeners) {
472 l.onDefaultOutgoingChanged(this);
473 }
474 }
475
476 private void fireSimCallManagerChanged() {
477 for (Listener l : mListeners) {
478 l.onSimCallManagerChanged(this);
479 }
Santos Cordon176ae282014-07-14 02:02:14 -0700480 }
481
Tyler Gunncb59b672014-08-20 09:02:11 -0700482 /**
483 * Determines if the connection service specified by a {@link PhoneAccountHandle} has the
484 * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission.
485 *
486 * @param phoneAccountHandle The phone account to check.
487 * @return {@code True} if the phone account has permission.
488 */
489 public boolean phoneAccountHasPermission(PhoneAccountHandle phoneAccountHandle) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700490 PackageManager packageManager = mContext.getPackageManager();
Tyler Gunncb59b672014-08-20 09:02:11 -0700491 try {
492 ServiceInfo serviceInfo = packageManager.getServiceInfo(
493 phoneAccountHandle.getComponentName(), 0);
494
495 return serviceInfo.permission != null &&
496 serviceInfo.permission.equals(Manifest.permission.BIND_CONNECTION_SERVICE);
497 } catch (PackageManager.NameNotFoundException e) {
498 Log.w(this, "Name not found %s", e);
499 return false;
500 }
501 }
502
Ihab Awad293edf22014-07-24 17:52:29 -0700503 ////////////////////////////////////////////////////////////////////////////////////////////////
504
Santos Cordonafe59e52014-08-22 16:48:43 -0700505 /**
506 * Returns a list of phone account handles with the specified flag.
Tyler Gunn84253572014-09-02 14:50:05 -0700507 *
508 * @param flags Flags which the {@code PhoneAccount} must have.
Santos Cordonafe59e52014-08-22 16:48:43 -0700509 */
510 private List<PhoneAccountHandle> getPhoneAccountHandles(int flags) {
Nancy Chen309198e2014-09-15 18:02:49 -0700511 return getPhoneAccountHandles(flags, null);
Tyler Gunn84253572014-09-02 14:50:05 -0700512 }
513
514 /**
515 * Returns a list of phone account handles with the specified flag, supporting the specified
Nancy Chen309198e2014-09-15 18:02:49 -0700516 * URI scheme.
Tyler Gunn84253572014-09-02 14:50:05 -0700517 *
518 * @param flags Flags which the {@code PhoneAccount} must have.
519 * @param uriScheme URI schemes the PhoneAccount must handle. {@code Null} bypasses the
520 * URI scheme check.
521 */
Nancy Chen309198e2014-09-15 18:02:49 -0700522 private List<PhoneAccountHandle> getPhoneAccountHandles(int flags, String uriScheme) {
Evan Charlton94d01622014-07-20 12:32:05 -0700523 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
Ihab Awad293edf22014-07-24 17:52:29 -0700524 for (PhoneAccount m : mState.accounts) {
Nancy Chen309198e2014-09-15 18:02:49 -0700525 if (m.hasCapabilities(flags) && (uriScheme == null || m.supportsUriScheme(uriScheme))) {
mike dooley10a58312014-11-06 13:46:19 -0800526 // Also filter out unresolveable accounts
527 if (!resolveComponent(m.getAccountHandle().getComponentName()).isEmpty()) {
528 accountHandles.add(m.getAccountHandle());
529 }
Ihab Awadf2a84912014-07-22 21:09:25 -0700530 }
Ihab Awad104f8062014-07-17 11:29:35 -0700531 }
Evan Charlton94d01622014-07-20 12:32:05 -0700532 return accountHandles;
Ihab Awad104f8062014-07-17 11:29:35 -0700533 }
534
Ihab Awad293edf22014-07-24 17:52:29 -0700535 /**
536 * The state of this {@code PhoneAccountRegistrar}.
537 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700538 @VisibleForTesting
539 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700540 /**
541 * The account selected by the user to be employed by default for making outgoing calls.
542 * If the user has not made such a selection, then this is null.
543 */
544 public PhoneAccountHandle defaultOutgoing = null;
545
546 /**
Ihab Awadb78b2762014-07-25 15:16:23 -0700547 * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which
Ihab Awad293edf22014-07-24 17:52:29 -0700548 * manages and optimizes a user's PSTN SIM connections.
549 */
550 public PhoneAccountHandle simCallManager;
551
552 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700553 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700554 */
555 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700556
557 /**
558 * The version number of the State data.
559 */
560 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700561 }
562
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700563 /**
564 * Dumps the state of the {@link CallsManager}.
565 *
566 * @param pw The {@code IndentingPrintWriter} to write the state to.
567 */
568 public void dump(IndentingPrintWriter pw) {
569 if (mState != null) {
570 pw.println("xmlVersion: " + mState.versionNumber);
571 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
572 mState.defaultOutgoing));
573 pw.println("simCallManager: " + (mState.simCallManager == null ? "none" :
574 mState.simCallManager));
575 pw.println("phoneAccounts:");
576 pw.increaseIndent();
577 for (PhoneAccount phoneAccount : mState.accounts) {
578 pw.println(phoneAccount);
579 }
580 pw.decreaseIndent();
581 }
582 }
583
Ihab Awad293edf22014-07-24 17:52:29 -0700584 ////////////////////////////////////////////////////////////////////////////////////////////////
585 //
586 // State management
587 //
588
589 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700590 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700591 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700592 os = mAtomicFile.startWrite();
593 boolean success = false;
594 try {
595 XmlSerializer serializer = new FastXmlSerializer();
596 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
597 writeToXml(mState, serializer);
598 serializer.flush();
599 success = true;
600 } finally {
601 if (success) {
602 mAtomicFile.finishWrite(os);
603 } else {
604 mAtomicFile.failWrite(os);
605 }
606 }
607 } catch (IOException e) {
608 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700609 }
610 }
611
Ihab Awadb78b2762014-07-25 15:16:23 -0700612 private void read() {
613 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700614 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700615 is = mAtomicFile.openRead();
616 } catch (FileNotFoundException ex) {
617 return;
618 }
619
Tyler Gunn84253572014-09-02 14:50:05 -0700620 boolean versionChanged = false;
621
Ihab Awadb78b2762014-07-25 15:16:23 -0700622 XmlPullParser parser;
623 try {
624 parser = Xml.newPullParser();
625 parser.setInput(new BufferedInputStream(is), null);
626 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700627 mState = readFromXml(parser, mContext);
628 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
629
Ihab Awadb78b2762014-07-25 15:16:23 -0700630 } catch (IOException | XmlPullParserException e) {
631 Log.e(this, e, "Reading state from XML file");
632 mState = new State();
633 } finally {
634 try {
635 is.close();
636 } catch (IOException e) {
637 Log.e(this, e, "Closing InputStream");
638 }
Ihab Awad104f8062014-07-17 11:29:35 -0700639 }
Tyler Gunn84253572014-09-02 14:50:05 -0700640
641 // If an upgrade occurred, write out the changed data.
642 if (versionChanged) {
643 write();
644 }
Santos Cordon176ae282014-07-14 02:02:14 -0700645 }
646
Ihab Awadb78b2762014-07-25 15:16:23 -0700647 private static void writeToXml(State state, XmlSerializer serializer)
648 throws IOException {
649 sStateXml.writeToXml(state, serializer);
Santos Cordon176ae282014-07-14 02:02:14 -0700650 }
Ihab Awad104f8062014-07-17 11:29:35 -0700651
Tyler Gunn84253572014-09-02 14:50:05 -0700652 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700653 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700654 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700655 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700656 }
657
Ihab Awad293edf22014-07-24 17:52:29 -0700658 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700659 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700660 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700661 //
662
Ihab Awadb78b2762014-07-25 15:16:23 -0700663 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700664 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700665 private static final String LENGTH_ATTRIBUTE = "length";
666 private static final String VALUE_TAG = "value";
667
Ihab Awadb78b2762014-07-25 15:16:23 -0700668 /**
669 * Write the supplied object to XML
670 */
Ihab Awad26923222014-07-30 10:54:35 -0700671 public abstract void writeToXml(T o, XmlSerializer serializer)
672 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700673
674 /**
675 * Read from the supplied XML into a new object, returning null in case of an
676 * unrecoverable schema mismatch or other data error. 'parser' must be already
677 * positioned at the first tag that is expected to have been emitted by this
678 * object's writeToXml(). This object tries to fail early without modifying
679 * 'parser' if it does not recognize the data it sees.
680 */
Tyler Gunn84253572014-09-02 14:50:05 -0700681 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700682 throws IOException, XmlPullParserException;
683
Ihab Awadd9f54382014-10-24 11:44:47 -0700684 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700685 throws IOException {
686 if (value != null) {
687 serializer.startTag(null, tagName);
688 serializer.text(Objects.toString(value));
689 serializer.endTag(null, tagName);
690 }
691 }
Tyler Gunn84253572014-09-02 14:50:05 -0700692
693 /**
694 * Serializes a string array.
695 *
696 * @param tagName The tag name for the string array.
697 * @param values The string values to serialize.
698 * @param serializer The serializer.
699 * @throws IOException
700 */
701 protected void writeStringList(String tagName, List<String> values,
702 XmlSerializer serializer)
703 throws IOException {
704
705 serializer.startTag(null, tagName);
706 if (values != null) {
707 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
708 for (String toSerialize : values) {
709 serializer.startTag(null, VALUE_TAG);
710 if (toSerialize != null ){
711 serializer.text(toSerialize);
712 }
Tyler Gunn84253572014-09-02 14:50:05 -0700713 serializer.endTag(null, VALUE_TAG);
714 }
715 } else {
716 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
717 }
718 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -0700719 }
Tyler Gunn84253572014-09-02 14:50:05 -0700720
Ihab Awadd9f54382014-10-24 11:44:47 -0700721 protected void writeBitmapIfNonNull(String tagName, Bitmap value, XmlSerializer serializer)
722 throws IOException {
723 if (value != null && value.getByteCount() > 0) {
724 ByteArrayOutputStream stream = new ByteArrayOutputStream();
725 value.compress(Bitmap.CompressFormat.PNG, 100, stream);
726 byte[] imageByteArray = stream.toByteArray();
727 String text = Base64.encodeToString(imageByteArray, 0, imageByteArray.length, 0);
728
729 serializer.startTag(null, tagName);
730 serializer.text(text);
731 serializer.endTag(null, tagName);
732 }
Tyler Gunn84253572014-09-02 14:50:05 -0700733 }
734
735 /**
736 * Reads a string array from the XML parser.
737 *
738 * @param parser The XML parser.
739 * @return String array containing the parsed values.
740 * @throws IOException Exception related to IO.
741 * @throws XmlPullParserException Exception related to parsing.
742 */
743 protected List<String> readStringList(XmlPullParser parser)
744 throws IOException, XmlPullParserException {
745
746 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
747 List<String> arrayEntries = new ArrayList<String>(length);
748 String value = null;
749
750 if (length == 0) {
751 return arrayEntries;
752 }
753
754 int outerDepth = parser.getDepth();
755 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
756 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700757 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -0700758 value = parser.getText();
759 arrayEntries.add(value);
760 }
761 }
762
763 return arrayEntries;
764 }
Ihab Awadd9f54382014-10-24 11:44:47 -0700765
766 protected Bitmap readBitmap(XmlPullParser parser)
767 throws IOException, XmlPullParserException {
768 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
769 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
770 }
Ihab Awad104f8062014-07-17 11:29:35 -0700771 }
772
Ihab Awadb78b2762014-07-25 15:16:23 -0700773 @VisibleForTesting
774 public static final XmlSerialization<State> sStateXml =
775 new XmlSerialization<State>() {
776 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -0700777 private static final String DEFAULT_OUTGOING = "default_outgoing";
Ihab Awad293edf22014-07-24 17:52:29 -0700778 private static final String SIM_CALL_MANAGER = "sim_call_manager";
Ihab Awad104f8062014-07-17 11:29:35 -0700779 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -0700780 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -0700781
782 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700783 public void writeToXml(State o, XmlSerializer serializer)
784 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700785 if (o != null) {
786 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -0700787 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -0700788
Ihab Awad26923222014-07-30 10:54:35 -0700789 if (o.defaultOutgoing != null) {
790 serializer.startTag(null, DEFAULT_OUTGOING);
791 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer);
792 serializer.endTag(null, DEFAULT_OUTGOING);
793 }
794
795 if (o.simCallManager != null) {
796 serializer.startTag(null, SIM_CALL_MANAGER);
797 sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer);
798 serializer.endTag(null, SIM_CALL_MANAGER);
799 }
800
801 serializer.startTag(null, ACCOUNTS);
802 for (PhoneAccount m : o.accounts) {
803 sPhoneAccountXml.writeToXml(m, serializer);
804 }
805 serializer.endTag(null, ACCOUNTS);
806
807 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -0700808 }
Ihab Awad104f8062014-07-17 11:29:35 -0700809 }
810
811 @Override
Tyler Gunn84253572014-09-02 14:50:05 -0700812 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700813 throws IOException, XmlPullParserException {
814 if (parser.getName().equals(CLASS_STATE)) {
815 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -0700816
817 String rawVersion = parser.getAttributeValue(null, VERSION);
818 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
819 Integer.parseInt(rawVersion);
820
Ihab Awadb78b2762014-07-25 15:16:23 -0700821 int outerDepth = parser.getDepth();
822 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
823 if (parser.getName().equals(DEFAULT_OUTGOING)) {
824 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700825 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
826 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700827 } else if (parser.getName().equals(SIM_CALL_MANAGER)) {
828 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700829 s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser,
830 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700831 } else if (parser.getName().equals(ACCOUNTS)) {
832 int accountsDepth = parser.getDepth();
833 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700834 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
835 s.versionNumber, context);
836
837 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700838 s.accounts.add(account);
839 }
840 }
Ihab Awad104f8062014-07-17 11:29:35 -0700841 }
842 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700843 return s;
Ihab Awad104f8062014-07-17 11:29:35 -0700844 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700845 return null;
Ihab Awad104f8062014-07-17 11:29:35 -0700846 }
847 };
848
Ihab Awadb78b2762014-07-25 15:16:23 -0700849 @VisibleForTesting
850 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
851 new XmlSerialization<PhoneAccount>() {
852 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
853 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -0700854 private static final String ADDRESS = "handle";
855 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -0700856 private static final String CAPABILITIES = "capabilities";
857 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -0700858 private static final String ICON_PACKAGE_NAME = "icon_package_name";
859 private static final String ICON_BITMAP = "icon_bitmap";
Nancy Chen06ce0622014-10-23 01:17:35 +0000860 private static final String COLOR = "color";
Ihab Awad104f8062014-07-17 11:29:35 -0700861 private static final String LABEL = "label";
862 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -0700863 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Ihab Awad104f8062014-07-17 11:29:35 -0700864
865 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700866 public void writeToXml(PhoneAccount o, XmlSerializer serializer)
867 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700868 if (o != null) {
869 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -0700870
Ihab Awad26923222014-07-30 10:54:35 -0700871 if (o.getAccountHandle() != null) {
872 serializer.startTag(null, ACCOUNT_HANDLE);
873 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer);
874 serializer.endTag(null, ACCOUNT_HANDLE);
875 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700876
Ihab Awadd9f54382014-10-24 11:44:47 -0700877 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
878 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
879 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
880 writeTextIfNonNull(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer);
881 writeTextIfNonNull(ICON_PACKAGE_NAME, o.getIconPackageName(), serializer);
882 writeBitmapIfNonNull(ICON_BITMAP, o.getIconBitmap(), serializer);
883 writeTextIfNonNull(COLOR, Integer.toString(o.getColor()), serializer);
884 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
885 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -0700886 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -0700887
Ihab Awad26923222014-07-30 10:54:35 -0700888 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
889 }
Ihab Awad104f8062014-07-17 11:29:35 -0700890 }
891
Tyler Gunn84253572014-09-02 14:50:05 -0700892 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700893 throws IOException, XmlPullParserException {
894 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
895 int outerDepth = parser.getDepth();
896 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -0700897 Uri address = null;
898 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -0700899 int capabilities = 0;
900 int iconResId = 0;
Ihab Awadd9f54382014-10-24 11:44:47 -0700901 String iconPackageName = null;
902 Bitmap icon = null;
Nancy Chen06ce0622014-10-23 01:17:35 +0000903 int color = 0;
Ihab Awadb78b2762014-07-25 15:16:23 -0700904 String label = null;
905 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -0700906 List<String> supportedUriSchemes = null;
Ihab Awadb78b2762014-07-25 15:16:23 -0700907
908 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
909 if (parser.getName().equals(ACCOUNT_HANDLE)) {
910 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700911 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
912 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -0700913 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700914 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -0700915 address = Uri.parse(parser.getText());
916 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700917 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -0700918 String nextText = parser.getText();
919 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -0700920 } else if (parser.getName().equals(CAPABILITIES)) {
921 parser.next();
922 capabilities = Integer.parseInt(parser.getText());
923 } else if (parser.getName().equals(ICON_RES_ID)) {
924 parser.next();
925 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -0700926 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
927 parser.next();
928 iconPackageName = parser.getText();
929 } else if (parser.getName().equals(ICON_BITMAP)) {
930 parser.next();
931 icon = readBitmap(parser);
Nancy Chen06ce0622014-10-23 01:17:35 +0000932 } else if (parser.getName().equals(COLOR)) {
933 parser.next();
934 color = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -0700935 } else if (parser.getName().equals(LABEL)) {
936 parser.next();
937 label = parser.getText();
938 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
939 parser.next();
940 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -0700941 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
942 supportedUriSchemes = readStringList(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -0700943 }
944 }
Tyler Gunn84253572014-09-02 14:50:05 -0700945
946 // Upgrade older phone accounts to specify the supported URI schemes.
947 if (version < 2) {
948 ComponentName sipComponentName = new ComponentName("com.android.phone",
949 "com.android.services.telephony.sip.SipConnectionService");
950
951 supportedUriSchemes = new ArrayList<>();
952
953 // Handle the SIP connection service.
954 // Check the system settings to see if it also should handle "tel" calls.
955 if (accountHandle.getComponentName().equals(sipComponentName)) {
956 boolean useSipForPstn = useSipForPstnCalls(context);
957 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
958 if (useSipForPstn) {
959 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
960 }
961 } else {
962 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
963 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
964 }
965 }
966
Andrew Lee7129f1c2014-09-04 11:55:07 -0700967 return PhoneAccount.builder(accountHandle, label)
968 .setAddress(address)
969 .setSubscriptionAddress(subscriptionAddress)
970 .setCapabilities(capabilities)
971 .setIconResId(iconResId)
Ihab Awadd9f54382014-10-24 11:44:47 -0700972 .setIconPackageName(iconPackageName)
973 .setIconBitmap(icon)
Nancy Chen06ce0622014-10-23 01:17:35 +0000974 .setColor(color)
Andrew Lee7129f1c2014-09-04 11:55:07 -0700975 .setShortDescription(shortDescription)
976 .setSupportedUriSchemes(supportedUriSchemes)
Ihab Awad6fb37c82014-08-07 19:48:57 -0700977 .build();
Ihab Awadb78b2762014-07-25 15:16:23 -0700978 }
979 return null;
Ihab Awad104f8062014-07-17 11:29:35 -0700980 }
Tyler Gunn84253572014-09-02 14:50:05 -0700981
982 /**
983 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN calls.
984 *
985 * @param context The context.
986 * @return {@code True} if SIP should be used for all calls.
987 */
988 private boolean useSipForPstnCalls(Context context) {
989 String option = Settings.System.getString(context.getContentResolver(),
990 Settings.System.SIP_CALL_OPTIONS);
991 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
992 return option.equals(Settings.System.SIP_ALWAYS);
993 }
Ihab Awad104f8062014-07-17 11:29:35 -0700994 };
995
Ihab Awadb78b2762014-07-25 15:16:23 -0700996 @VisibleForTesting
997 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
998 new XmlSerialization<PhoneAccountHandle>() {
999 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001000 private static final String COMPONENT_NAME = "component_name";
1001 private static final String ID = "id";
1002
1003 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -07001004 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer)
1005 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001006 if (o != null) {
1007 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001008
Ihab Awad26923222014-07-30 10:54:35 -07001009 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001010 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001011 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1012 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001013
Ihab Awadd9f54382014-10-24 11:44:47 -07001014 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001015
Ihab Awad26923222014-07-30 10:54:35 -07001016 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1017 }
Ihab Awad104f8062014-07-17 11:29:35 -07001018 }
1019
1020 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001021 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001022 throws IOException, XmlPullParserException {
1023 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1024 String componentNameString = null;
1025 String idString = null;
1026 int outerDepth = parser.getDepth();
1027 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1028 if (parser.getName().equals(COMPONENT_NAME)) {
1029 parser.next();
1030 componentNameString = parser.getText();
1031 } else if (parser.getName().equals(ID)) {
1032 parser.next();
1033 idString = parser.getText();
1034 }
1035 }
Ihab Awad26923222014-07-30 10:54:35 -07001036 if (componentNameString != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001037 return new PhoneAccountHandle(
1038 ComponentName.unflattenFromString(componentNameString),
1039 idString);
1040 }
1041 }
1042 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001043 }
1044 };
Santos Cordon176ae282014-07-14 02:02:14 -07001045}