blob: b5c078c080ad7584645d97de1856fb7e745f1de0 [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;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070024import android.os.Environment;
25import android.os.UserHandle;
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;
30import android.telecom.TelecomManager;
Nancy Chen140004a2014-10-15 15:48:38 -070031import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070032import android.telephony.SubscriptionManager;
Santos Cordon176ae282014-07-14 02:02:14 -070033import android.content.ComponentName;
34import android.content.Context;
Santos Cordon176ae282014-07-14 02:02:14 -070035import android.net.Uri;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070036import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070037import android.util.AtomicFile;
38import 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;
52import java.io.File;
53import java.io.FileNotFoundException;
54import java.io.FileOutputStream;
55import java.io.IOException;
56import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070057import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070058import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070059import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070060import java.util.ArrayList;
Santos Cordonafe59e52014-08-22 16:48:43 -070061import java.util.Collections;
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
Tyler Gunn8e0fef42014-09-08 18:34:44 -070087 public static final int EXPECTED_STATE_VERSION = 3;
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++) {
253 if (mState.accounts.get(i).getAccountHandle().equals(mState.simCallManager)) {
254 return mState.simCallManager;
255 }
256 }
257 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700258
259 // See if the OEM has specified a default one.
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700260 String defaultConnectionMgr =
Santos Cordonafe59e52014-08-22 16:48:43 -0700261 mContext.getResources().getString(R.string.default_connection_manager_component);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700262 if (!TextUtils.isEmpty(defaultConnectionMgr)) {
Santos Cordonafe59e52014-08-22 16:48:43 -0700263 PackageManager pm = mContext.getPackageManager();
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700264
265 ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr);
266 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
267 intent.setComponent(componentName);
268
269 // Make sure that the component can be resolved.
270 List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent, 0);
271 if (!resolveInfos.isEmpty()) {
272 // See if there is registered PhoneAccount by this component.
273 List<PhoneAccountHandle> handles = getAllPhoneAccountHandles();
274 for (PhoneAccountHandle handle : handles) {
275 if (componentName.equals(handle.getComponentName())) {
276 return handle;
277 }
278 }
279 Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName);
280 } else {
281 Log.d(this, "%s could not be resolved; not using as default", componentName);
282 }
283 } else {
284 Log.v(this, "No default connection manager specified");
285 }
286
Ihab Awadb78b2762014-07-25 15:16:23 -0700287 return null;
Ihab Awad293edf22014-07-24 17:52:29 -0700288 }
289
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700290 /**
291 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
292 *
293 * @return The list of {@link PhoneAccountHandle}s.
294 */
Ihab Awad293edf22014-07-24 17:52:29 -0700295 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
296 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
297 for (PhoneAccount m : mState.accounts) {
298 accountHandles.add(m.getAccountHandle());
299 }
300 return accountHandles;
301 }
302
303 public List<PhoneAccount> getAllPhoneAccounts() {
304 return new ArrayList<>(mState.accounts);
305 }
306
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700307 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700308 * Determines the number of all {@link PhoneAccount}s.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700309 *
Nancy Chen309198e2014-09-15 18:02:49 -0700310 * @return The total number {@link PhoneAccount}s.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700311 */
312 public int getAllPhoneAccountsCount() {
313 return mState.accounts.size();
314 }
315
316 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700317 * Retrieves a list of all call provider phone accounts.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700318 *
319 * @return The phone account handles.
320 */
Nancy Chen309198e2014-09-15 18:02:49 -0700321 public List<PhoneAccountHandle> getCallCapablePhoneAccounts() {
Santos Cordonafe59e52014-08-22 16:48:43 -0700322 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER);
323 }
324
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700325 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700326 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700327 * specified URI scheme.
328 *
329 * @param uriScheme The URI scheme.
330 * @return The phone account handles.
331 */
Nancy Chen309198e2014-09-15 18:02:49 -0700332 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) {
333 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme);
Tyler Gunn84253572014-09-02 14:50:05 -0700334 }
335
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700336 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700337 * Retrieves a list of all phone accounts registered by a specified package.
338 *
339 * @param packageName The name of the package that registered the phone accounts.
340 * @return The phone account handles.
341 */
342 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
343 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
344 for (PhoneAccount m : mState.accounts) {
345 if (Objects.equals(
346 packageName,
347 m.getAccountHandle().getComponentName().getPackageName())) {
348 accountHandles.add(m.getAccountHandle());
349 }
350 }
351 return accountHandles;
352 }
353
354 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700355 * Retrieves a list of all phone account handles with the connection manager capability.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700356 *
357 * @return The phone account handles.
358 */
359 public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() {
Santos Cordone3d82452014-09-15 13:44:29 -0700360 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER,
Nancy Chen309198e2014-09-15 18:02:49 -0700361 null /* supportedUriScheme */);
Santos Cordon176ae282014-07-14 02:02:14 -0700362 }
363
Ihab Awad293edf22014-07-24 17:52:29 -0700364 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
365 for (PhoneAccount m : mState.accounts) {
366 if (Objects.equals(handle, m.getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700367 return m;
Santos Cordon176ae282014-07-14 02:02:14 -0700368 }
369 }
370 return null;
371 }
372
Ihab Awad104f8062014-07-17 11:29:35 -0700373 // TODO: Should we implement an artificial limit for # of accounts associated with a single
374 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700375 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700376 // Enforce the requirement that a connection service for a phone account has the correct
377 // permission.
378 if (!phoneAccountHasPermission(account.getAccountHandle())) {
379 Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.",
380 account.getAccountHandle());
381 throw new SecurityException(
382 "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission.");
383 }
384
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700385 addOrReplacePhoneAccount(account);
386 }
387
388 /**
389 * Adds a {@code PhoneAccount}, replacing an existing one if found.
390 *
391 * @param account The {@code PhoneAccount} to add or replace.
392 */
393 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad293edf22014-07-24 17:52:29 -0700394 mState.accounts.add(account);
Ihab Awad104f8062014-07-17 11:29:35 -0700395 // Search for duplicates and remove any that are found.
Ihab Awad293edf22014-07-24 17:52:29 -0700396 for (int i = 0; i < mState.accounts.size() - 1; i++) {
397 if (Objects.equals(
398 account.getAccountHandle(), mState.accounts.get(i).getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700399 // replace existing entry.
Ihab Awad293edf22014-07-24 17:52:29 -0700400 mState.accounts.remove(i);
Ihab Awad104f8062014-07-17 11:29:35 -0700401 break;
Santos Cordon176ae282014-07-14 02:02:14 -0700402 }
403 }
404
Ihab Awad293edf22014-07-24 17:52:29 -0700405 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700406 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700407 }
408
Evan Charlton89176372014-07-19 18:23:09 -0700409 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Ihab Awad293edf22014-07-24 17:52:29 -0700410 for (int i = 0; i < mState.accounts.size(); i++) {
411 if (Objects.equals(accountHandle, mState.accounts.get(i).getAccountHandle())) {
412 mState.accounts.remove(i);
Ihab Awad104f8062014-07-17 11:29:35 -0700413 break;
414 }
415 }
416
Ihab Awad293edf22014-07-24 17:52:29 -0700417 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700418 fireAccountsChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700419 }
420
Tyler Gunnd900ce62014-08-13 11:40:59 -0700421 /**
422 * Un-registers all phone accounts associated with a specified package.
423 *
424 * @param packageName The package for which phone accounts will be removed.
425 */
Ihab Awad104f8062014-07-17 11:29:35 -0700426 public void clearAccounts(String packageName) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700427 boolean accountsRemoved = false;
428 Iterator<PhoneAccount> it = mState.accounts.iterator();
429 while (it.hasNext()) {
430 PhoneAccount phoneAccount = it.next();
Ihab Awad104f8062014-07-17 11:29:35 -0700431 if (Objects.equals(
432 packageName,
Tyler Gunnd900ce62014-08-13 11:40:59 -0700433 phoneAccount.getAccountHandle().getComponentName().getPackageName())) {
434 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
435 it.remove();
436 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700437 }
438 }
439
Tyler Gunnd900ce62014-08-13 11:40:59 -0700440 if (accountsRemoved) {
441 write();
442 fireAccountsChanged();
443 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700444 }
445
Nancy Chen140004a2014-10-15 15:48:38 -0700446 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700447 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700448 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
449 }
450
Ihab Awadb78b2762014-07-25 15:16:23 -0700451 public void addListener(Listener l) {
452 mListeners.add(l);
453 }
454
455 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700456 if (l != null) {
457 mListeners.remove(l);
458 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700459 }
460
461 private void fireAccountsChanged() {
462 for (Listener l : mListeners) {
463 l.onAccountsChanged(this);
464 }
465 }
466
467 private void fireDefaultOutgoingChanged() {
468 for (Listener l : mListeners) {
469 l.onDefaultOutgoingChanged(this);
470 }
471 }
472
473 private void fireSimCallManagerChanged() {
474 for (Listener l : mListeners) {
475 l.onSimCallManagerChanged(this);
476 }
Santos Cordon176ae282014-07-14 02:02:14 -0700477 }
478
Tyler Gunncb59b672014-08-20 09:02:11 -0700479 /**
480 * Determines if the connection service specified by a {@link PhoneAccountHandle} has the
481 * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission.
482 *
483 * @param phoneAccountHandle The phone account to check.
484 * @return {@code True} if the phone account has permission.
485 */
486 public boolean phoneAccountHasPermission(PhoneAccountHandle phoneAccountHandle) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700487 PackageManager packageManager = mContext.getPackageManager();
Tyler Gunncb59b672014-08-20 09:02:11 -0700488 try {
489 ServiceInfo serviceInfo = packageManager.getServiceInfo(
490 phoneAccountHandle.getComponentName(), 0);
491
492 return serviceInfo.permission != null &&
493 serviceInfo.permission.equals(Manifest.permission.BIND_CONNECTION_SERVICE);
494 } catch (PackageManager.NameNotFoundException e) {
495 Log.w(this, "Name not found %s", e);
496 return false;
497 }
498 }
499
Ihab Awad293edf22014-07-24 17:52:29 -0700500 ////////////////////////////////////////////////////////////////////////////////////////////////
501
Santos Cordonafe59e52014-08-22 16:48:43 -0700502 /**
503 * Returns a list of phone account handles with the specified flag.
Tyler Gunn84253572014-09-02 14:50:05 -0700504 *
505 * @param flags Flags which the {@code PhoneAccount} must have.
Santos Cordonafe59e52014-08-22 16:48:43 -0700506 */
507 private List<PhoneAccountHandle> getPhoneAccountHandles(int flags) {
Nancy Chen309198e2014-09-15 18:02:49 -0700508 return getPhoneAccountHandles(flags, null);
Tyler Gunn84253572014-09-02 14:50:05 -0700509 }
510
511 /**
512 * Returns a list of phone account handles with the specified flag, supporting the specified
Nancy Chen309198e2014-09-15 18:02:49 -0700513 * URI scheme.
Tyler Gunn84253572014-09-02 14:50:05 -0700514 *
515 * @param flags Flags which the {@code PhoneAccount} must have.
516 * @param uriScheme URI schemes the PhoneAccount must handle. {@code Null} bypasses the
517 * URI scheme check.
518 */
Nancy Chen309198e2014-09-15 18:02:49 -0700519 private List<PhoneAccountHandle> getPhoneAccountHandles(int flags, String uriScheme) {
Evan Charlton94d01622014-07-20 12:32:05 -0700520 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
Ihab Awad293edf22014-07-24 17:52:29 -0700521 for (PhoneAccount m : mState.accounts) {
Nancy Chen309198e2014-09-15 18:02:49 -0700522 if (m.hasCapabilities(flags) && (uriScheme == null || m.supportsUriScheme(uriScheme))) {
Ihab Awadf2a84912014-07-22 21:09:25 -0700523 accountHandles.add(m.getAccountHandle());
524 }
Ihab Awad104f8062014-07-17 11:29:35 -0700525 }
Evan Charlton94d01622014-07-20 12:32:05 -0700526 return accountHandles;
Ihab Awad104f8062014-07-17 11:29:35 -0700527 }
528
Ihab Awad293edf22014-07-24 17:52:29 -0700529 /**
530 * The state of this {@code PhoneAccountRegistrar}.
531 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700532 @VisibleForTesting
533 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700534 /**
535 * The account selected by the user to be employed by default for making outgoing calls.
536 * If the user has not made such a selection, then this is null.
537 */
538 public PhoneAccountHandle defaultOutgoing = null;
539
540 /**
Ihab Awadb78b2762014-07-25 15:16:23 -0700541 * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which
Ihab Awad293edf22014-07-24 17:52:29 -0700542 * manages and optimizes a user's PSTN SIM connections.
543 */
544 public PhoneAccountHandle simCallManager;
545
546 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700547 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700548 */
549 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700550
551 /**
552 * The version number of the State data.
553 */
554 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700555 }
556
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700557 /**
558 * Dumps the state of the {@link CallsManager}.
559 *
560 * @param pw The {@code IndentingPrintWriter} to write the state to.
561 */
562 public void dump(IndentingPrintWriter pw) {
563 if (mState != null) {
564 pw.println("xmlVersion: " + mState.versionNumber);
565 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
566 mState.defaultOutgoing));
567 pw.println("simCallManager: " + (mState.simCallManager == null ? "none" :
568 mState.simCallManager));
569 pw.println("phoneAccounts:");
570 pw.increaseIndent();
571 for (PhoneAccount phoneAccount : mState.accounts) {
572 pw.println(phoneAccount);
573 }
574 pw.decreaseIndent();
575 }
576 }
577
Ihab Awad293edf22014-07-24 17:52:29 -0700578 ////////////////////////////////////////////////////////////////////////////////////////////////
579 //
580 // State management
581 //
582
583 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700584 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700585 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700586 os = mAtomicFile.startWrite();
587 boolean success = false;
588 try {
589 XmlSerializer serializer = new FastXmlSerializer();
590 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
591 writeToXml(mState, serializer);
592 serializer.flush();
593 success = true;
594 } finally {
595 if (success) {
596 mAtomicFile.finishWrite(os);
597 } else {
598 mAtomicFile.failWrite(os);
599 }
600 }
601 } catch (IOException e) {
602 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700603 }
604 }
605
Ihab Awadb78b2762014-07-25 15:16:23 -0700606 private void read() {
607 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700608 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700609 is = mAtomicFile.openRead();
610 } catch (FileNotFoundException ex) {
611 return;
612 }
613
Tyler Gunn84253572014-09-02 14:50:05 -0700614 boolean versionChanged = false;
615
Ihab Awadb78b2762014-07-25 15:16:23 -0700616 XmlPullParser parser;
617 try {
618 parser = Xml.newPullParser();
619 parser.setInput(new BufferedInputStream(is), null);
620 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700621 mState = readFromXml(parser, mContext);
622 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
623
Ihab Awadb78b2762014-07-25 15:16:23 -0700624 } catch (IOException | XmlPullParserException e) {
625 Log.e(this, e, "Reading state from XML file");
626 mState = new State();
627 } finally {
628 try {
629 is.close();
630 } catch (IOException e) {
631 Log.e(this, e, "Closing InputStream");
632 }
Ihab Awad104f8062014-07-17 11:29:35 -0700633 }
Tyler Gunn84253572014-09-02 14:50:05 -0700634
635 // If an upgrade occurred, write out the changed data.
636 if (versionChanged) {
637 write();
638 }
Santos Cordon176ae282014-07-14 02:02:14 -0700639 }
640
Ihab Awadb78b2762014-07-25 15:16:23 -0700641 private static void writeToXml(State state, XmlSerializer serializer)
642 throws IOException {
643 sStateXml.writeToXml(state, serializer);
Santos Cordon176ae282014-07-14 02:02:14 -0700644 }
Ihab Awad104f8062014-07-17 11:29:35 -0700645
Tyler Gunn84253572014-09-02 14:50:05 -0700646 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700647 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700648 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700649 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700650 }
651
Ihab Awad293edf22014-07-24 17:52:29 -0700652 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700653 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700654 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700655 //
656
Ihab Awadb78b2762014-07-25 15:16:23 -0700657 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700658 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700659 private static final String LENGTH_ATTRIBUTE = "length";
660 private static final String VALUE_TAG = "value";
661
Ihab Awadb78b2762014-07-25 15:16:23 -0700662 /**
663 * Write the supplied object to XML
664 */
Ihab Awad26923222014-07-30 10:54:35 -0700665 public abstract void writeToXml(T o, XmlSerializer serializer)
666 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700667
668 /**
669 * Read from the supplied XML into a new object, returning null in case of an
670 * unrecoverable schema mismatch or other data error. 'parser' must be already
671 * positioned at the first tag that is expected to have been emitted by this
672 * object's writeToXml(). This object tries to fail early without modifying
673 * 'parser' if it does not recognize the data it sees.
674 */
Tyler Gunn84253572014-09-02 14:50:05 -0700675 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700676 throws IOException, XmlPullParserException;
677
678 protected void writeTextSafely(String tagName, Object value, XmlSerializer serializer)
679 throws IOException {
680 if (value != null) {
681 serializer.startTag(null, tagName);
682 serializer.text(Objects.toString(value));
683 serializer.endTag(null, tagName);
684 }
685 }
Tyler Gunn84253572014-09-02 14:50:05 -0700686
687 /**
688 * Serializes a string array.
689 *
690 * @param tagName The tag name for the string array.
691 * @param values The string values to serialize.
692 * @param serializer The serializer.
693 * @throws IOException
694 */
695 protected void writeStringList(String tagName, List<String> values,
696 XmlSerializer serializer)
697 throws IOException {
698
699 serializer.startTag(null, tagName);
700 if (values != null) {
701 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
702 for (String toSerialize : values) {
703 serializer.startTag(null, VALUE_TAG);
704 if (toSerialize != null ){
705 serializer.text(toSerialize);
706 }
Tyler Gunn84253572014-09-02 14:50:05 -0700707 serializer.endTag(null, VALUE_TAG);
708 }
709 } else {
710 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
711 }
712 serializer.endTag(null, tagName);
713
714 }
715
716 /**
717 * Reads a string array from the XML parser.
718 *
719 * @param parser The XML parser.
720 * @return String array containing the parsed values.
721 * @throws IOException Exception related to IO.
722 * @throws XmlPullParserException Exception related to parsing.
723 */
724 protected List<String> readStringList(XmlPullParser parser)
725 throws IOException, XmlPullParserException {
726
727 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
728 List<String> arrayEntries = new ArrayList<String>(length);
729 String value = null;
730
731 if (length == 0) {
732 return arrayEntries;
733 }
734
735 int outerDepth = parser.getDepth();
736 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
737 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700738 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -0700739 value = parser.getText();
740 arrayEntries.add(value);
741 }
742 }
743
744 return arrayEntries;
745 }
Ihab Awad104f8062014-07-17 11:29:35 -0700746 }
747
Ihab Awadb78b2762014-07-25 15:16:23 -0700748 @VisibleForTesting
749 public static final XmlSerialization<State> sStateXml =
750 new XmlSerialization<State>() {
751 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -0700752 private static final String DEFAULT_OUTGOING = "default_outgoing";
Ihab Awad293edf22014-07-24 17:52:29 -0700753 private static final String SIM_CALL_MANAGER = "sim_call_manager";
Ihab Awad104f8062014-07-17 11:29:35 -0700754 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -0700755 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -0700756
757 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700758 public void writeToXml(State o, XmlSerializer serializer)
759 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700760 if (o != null) {
761 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -0700762 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -0700763
Ihab Awad26923222014-07-30 10:54:35 -0700764 if (o.defaultOutgoing != null) {
765 serializer.startTag(null, DEFAULT_OUTGOING);
766 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer);
767 serializer.endTag(null, DEFAULT_OUTGOING);
768 }
769
770 if (o.simCallManager != null) {
771 serializer.startTag(null, SIM_CALL_MANAGER);
772 sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer);
773 serializer.endTag(null, SIM_CALL_MANAGER);
774 }
775
776 serializer.startTag(null, ACCOUNTS);
777 for (PhoneAccount m : o.accounts) {
778 sPhoneAccountXml.writeToXml(m, serializer);
779 }
780 serializer.endTag(null, ACCOUNTS);
781
782 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -0700783 }
Ihab Awad104f8062014-07-17 11:29:35 -0700784 }
785
786 @Override
Tyler Gunn84253572014-09-02 14:50:05 -0700787 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700788 throws IOException, XmlPullParserException {
789 if (parser.getName().equals(CLASS_STATE)) {
790 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -0700791
792 String rawVersion = parser.getAttributeValue(null, VERSION);
793 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
794 Integer.parseInt(rawVersion);
795
Ihab Awadb78b2762014-07-25 15:16:23 -0700796 int outerDepth = parser.getDepth();
797 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
798 if (parser.getName().equals(DEFAULT_OUTGOING)) {
799 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700800 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
801 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700802 } else if (parser.getName().equals(SIM_CALL_MANAGER)) {
803 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700804 s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser,
805 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700806 } else if (parser.getName().equals(ACCOUNTS)) {
807 int accountsDepth = parser.getDepth();
808 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700809 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
810 s.versionNumber, context);
811
812 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700813 s.accounts.add(account);
814 }
815 }
Ihab Awad104f8062014-07-17 11:29:35 -0700816 }
817 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700818 return s;
Ihab Awad104f8062014-07-17 11:29:35 -0700819 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700820 return null;
Ihab Awad104f8062014-07-17 11:29:35 -0700821 }
822 };
823
Ihab Awadb78b2762014-07-25 15:16:23 -0700824 @VisibleForTesting
825 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
826 new XmlSerialization<PhoneAccount>() {
827 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
828 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -0700829 private static final String ADDRESS = "handle";
830 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -0700831 private static final String CAPABILITIES = "capabilities";
832 private static final String ICON_RES_ID = "icon_res_id";
Nancy Chen06ce0622014-10-23 01:17:35 +0000833 private static final String COLOR = "color";
Ihab Awad104f8062014-07-17 11:29:35 -0700834 private static final String LABEL = "label";
835 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -0700836 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700837 private static final String TRUE = "true";
838 private static final String FALSE = "false";
Ihab Awad104f8062014-07-17 11:29:35 -0700839
840 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700841 public void writeToXml(PhoneAccount o, XmlSerializer serializer)
842 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700843 if (o != null) {
844 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -0700845
Ihab Awad26923222014-07-30 10:54:35 -0700846 if (o.getAccountHandle() != null) {
847 serializer.startTag(null, ACCOUNT_HANDLE);
848 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer);
849 serializer.endTag(null, ACCOUNT_HANDLE);
850 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700851
Andrew Lee7129f1c2014-09-04 11:55:07 -0700852 writeTextSafely(ADDRESS, o.getAddress(), serializer);
853 writeTextSafely(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
Ihab Awad26923222014-07-30 10:54:35 -0700854 writeTextSafely(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
855 writeTextSafely(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer);
Nancy Chen06ce0622014-10-23 01:17:35 +0000856 writeTextSafely(COLOR, Integer.toString(o.getColor()), serializer);
Ihab Awad26923222014-07-30 10:54:35 -0700857 writeTextSafely(LABEL, o.getLabel(), serializer);
858 writeTextSafely(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -0700859 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -0700860
Ihab Awad26923222014-07-30 10:54:35 -0700861 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
862 }
Ihab Awad104f8062014-07-17 11:29:35 -0700863 }
864
Tyler Gunn84253572014-09-02 14:50:05 -0700865 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700866 throws IOException, XmlPullParserException {
867 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
868 int outerDepth = parser.getDepth();
869 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -0700870 Uri address = null;
871 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -0700872 int capabilities = 0;
873 int iconResId = 0;
Nancy Chen06ce0622014-10-23 01:17:35 +0000874 int color = 0;
Ihab Awadb78b2762014-07-25 15:16:23 -0700875 String label = null;
876 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -0700877 List<String> supportedUriSchemes = null;
Ihab Awadb78b2762014-07-25 15:16:23 -0700878
879 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
880 if (parser.getName().equals(ACCOUNT_HANDLE)) {
881 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700882 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
883 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -0700884 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700885 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -0700886 address = Uri.parse(parser.getText());
887 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700888 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -0700889 String nextText = parser.getText();
890 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -0700891 } else if (parser.getName().equals(CAPABILITIES)) {
892 parser.next();
893 capabilities = Integer.parseInt(parser.getText());
894 } else if (parser.getName().equals(ICON_RES_ID)) {
895 parser.next();
896 iconResId = Integer.parseInt(parser.getText());
Nancy Chen06ce0622014-10-23 01:17:35 +0000897 } else if (parser.getName().equals(COLOR)) {
898 parser.next();
899 color = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -0700900 } else if (parser.getName().equals(LABEL)) {
901 parser.next();
902 label = parser.getText();
903 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
904 parser.next();
905 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -0700906 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
907 supportedUriSchemes = readStringList(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -0700908 }
909 }
Tyler Gunn84253572014-09-02 14:50:05 -0700910
911 // Upgrade older phone accounts to specify the supported URI schemes.
912 if (version < 2) {
913 ComponentName sipComponentName = new ComponentName("com.android.phone",
914 "com.android.services.telephony.sip.SipConnectionService");
915
916 supportedUriSchemes = new ArrayList<>();
917
918 // Handle the SIP connection service.
919 // Check the system settings to see if it also should handle "tel" calls.
920 if (accountHandle.getComponentName().equals(sipComponentName)) {
921 boolean useSipForPstn = useSipForPstnCalls(context);
922 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
923 if (useSipForPstn) {
924 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
925 }
926 } else {
927 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
928 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
929 }
930 }
931
Andrew Lee7129f1c2014-09-04 11:55:07 -0700932 return PhoneAccount.builder(accountHandle, label)
933 .setAddress(address)
934 .setSubscriptionAddress(subscriptionAddress)
935 .setCapabilities(capabilities)
936 .setIconResId(iconResId)
Nancy Chen06ce0622014-10-23 01:17:35 +0000937 .setColor(color)
Andrew Lee7129f1c2014-09-04 11:55:07 -0700938 .setShortDescription(shortDescription)
939 .setSupportedUriSchemes(supportedUriSchemes)
Ihab Awad6fb37c82014-08-07 19:48:57 -0700940 .build();
Ihab Awadb78b2762014-07-25 15:16:23 -0700941 }
942 return null;
Ihab Awad104f8062014-07-17 11:29:35 -0700943 }
Tyler Gunn84253572014-09-02 14:50:05 -0700944
945 /**
946 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN calls.
947 *
948 * @param context The context.
949 * @return {@code True} if SIP should be used for all calls.
950 */
951 private boolean useSipForPstnCalls(Context context) {
952 String option = Settings.System.getString(context.getContentResolver(),
953 Settings.System.SIP_CALL_OPTIONS);
954 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
955 return option.equals(Settings.System.SIP_ALWAYS);
956 }
Ihab Awad104f8062014-07-17 11:29:35 -0700957 };
958
Ihab Awadb78b2762014-07-25 15:16:23 -0700959 @VisibleForTesting
960 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
961 new XmlSerialization<PhoneAccountHandle>() {
962 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -0700963 private static final String COMPONENT_NAME = "component_name";
964 private static final String ID = "id";
965
966 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700967 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer)
968 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700969 if (o != null) {
970 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -0700971
Ihab Awad26923222014-07-30 10:54:35 -0700972 if (o.getComponentName() != null) {
973 writeTextSafely(
974 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
975 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700976
Ihab Awad26923222014-07-30 10:54:35 -0700977 writeTextSafely(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -0700978
Ihab Awad26923222014-07-30 10:54:35 -0700979 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
980 }
Ihab Awad104f8062014-07-17 11:29:35 -0700981 }
982
983 @Override
Tyler Gunn84253572014-09-02 14:50:05 -0700984 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700985 throws IOException, XmlPullParserException {
986 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
987 String componentNameString = null;
988 String idString = null;
989 int outerDepth = parser.getDepth();
990 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
991 if (parser.getName().equals(COMPONENT_NAME)) {
992 parser.next();
993 componentNameString = parser.getText();
994 } else if (parser.getName().equals(ID)) {
995 parser.next();
996 idString = parser.getText();
997 }
998 }
Ihab Awad26923222014-07-30 10:54:35 -0700999 if (componentNameString != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001000 return new PhoneAccountHandle(
1001 ComponentName.unflattenFromString(componentNameString),
1002 idString);
1003 }
1004 }
1005 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001006 }
1007 };
Santos Cordon176ae282014-07-14 02:02:14 -07001008}