blob: eb6b7454f20cf263944192133c204c872873e1bd [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;
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;
37import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070038
Tyler Gunn91d43cf2014-09-17 12:19:39 -070039// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070040import com.android.internal.annotations.VisibleForTesting;
41import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070042import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070043import com.android.internal.util.XmlUtils;
44
Evan Charltonaf51ceb2014-07-30 11:56:36 -070045import org.xmlpull.v1.XmlPullParser;
46import org.xmlpull.v1.XmlPullParserException;
47import org.xmlpull.v1.XmlSerializer;
48
Ihab Awadb78b2762014-07-25 15:16:23 -070049import java.io.BufferedInputStream;
50import java.io.BufferedOutputStream;
51import java.io.File;
52import java.io.FileNotFoundException;
53import java.io.FileOutputStream;
54import java.io.IOException;
55import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070056import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070057import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070058import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070059import java.util.ArrayList;
Santos Cordonafe59e52014-08-22 16:48:43 -070060import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070061import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070062import java.util.List;
63import java.util.Objects;
Ihab Awadb78b2762014-07-25 15:16:23 -070064import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070065
66/**
Evan Charlton89176372014-07-19 18:23:09 -070067 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Tyler Gunn7cc70b42014-09-12 22:17:27 -070068 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as implemented in
69 * {@link TelecomServiceImpl}, with the notable exception that {@link TelecomServiceImpl} is
Ihab Awad104f8062014-07-17 11:29:35 -070070 * responsible for security checking to make sure that the caller has proper authority over
Evan Charlton89176372014-07-19 18:23:09 -070071 * the {@code ComponentName}s they are declaring in their {@code PhoneAccountHandle}s.
Santos Cordon176ae282014-07-14 02:02:14 -070072 */
Ihab Awadb78b2762014-07-25 15:16:23 -070073public final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -070074
Yorke Lee5e8836a2014-08-22 15:25:18 -070075 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
76 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
77
Ihab Awadb78b2762014-07-25 15:16:23 -070078 public abstract static class Listener {
79 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
80 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
81 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
82 }
83
84 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -070085 @VisibleForTesting
Tyler Gunn8e0fef42014-09-08 18:34:44 -070086 public static final int EXPECTED_STATE_VERSION = 3;
Tyler Gunn84253572014-09-02 14:50:05 -070087
88 /** Keep in sync with the same in SipSettings.java */
89 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -070090
91 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
92 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -070093 private final Context mContext;
Ihab Awadb78b2762014-07-25 15:16:23 -070094 private State mState;
Santos Cordon176ae282014-07-14 02:02:14 -070095
Nancy Chen06ce0622014-10-23 01:17:35 +000096 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -070097 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -070098 this(context, FILE_NAME);
99 }
100
101 @VisibleForTesting
102 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700103 // TODO: This file path is subject to change -- it is storing the phone account registry
104 // state file in the path /data/system/users/0/, which is likely not correct in a
105 // multi-user setting.
106 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
107 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
108 getAbsolutePath();
109 mAtomicFile = new AtomicFile(new File(filePath, fileName));
110 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700111 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700112
Ihab Awadb78b2762014-07-25 15:16:23 -0700113 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700114 mContext = context;
Ihab Awadb78b2762014-07-25 15:16:23 -0700115 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700116 }
117
Tyler Gunn84253572014-09-02 14:50:05 -0700118 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700119 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
120 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
121 * subscription id.
122 * @param accountHandle The handle for the phone account for which to retrieve the
123 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700124 * @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 -0700125 */
Wink Saville35850602014-10-23 15:57:21 -0700126 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Nancy Chen140004a2014-10-15 15:48:38 -0700127 PhoneAccount account = getPhoneAccount(accountHandle);
128 if (account == null || !account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) ||
129 !TextUtils.isDigitsOnly(accountHandle.getId())) {
130 // Since no decimals or negative numbers can be valid subscription ids, only a string of
131 // numbers can be subscription id
132 return -1;
133 }
Wink Saville35850602014-10-23 15:57:21 -0700134 return Integer.parseInt(accountHandle.getId());
Nancy Chen140004a2014-10-15 15:48:38 -0700135 }
136
137 /**
Tyler Gunn84253572014-09-02 14:50:05 -0700138 * Retrieves the default outgoing phone account supporting the specified uriScheme.
139 * @param uriScheme The URI scheme for the outgoing call.
140 * @return The {@link PhoneAccountHandle} to use.
141 */
142 public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700143 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700144
Yorke Lee5e8836a2014-08-22 15:25:18 -0700145 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700146 // If there is a default PhoneAccount, ensure it supports calls to handles with the
147 // specified uriScheme.
148 final PhoneAccount userSelectedAccount = getPhoneAccount(userSelected);
149 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
150 return userSelected;
151 }
Ihab Awad293edf22014-07-24 17:52:29 -0700152 }
153
Nancy Chen309198e2014-09-15 18:02:49 -0700154 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700155 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700156 case 0:
157 // There are no accounts, so there can be no default
158 return null;
159 case 1:
160 // There is only one account, which is by definition the default
Ihab Awad6fb37c82014-08-07 19:48:57 -0700161 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700162 default:
163 // There are multiple accounts with no selected default
164 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700165 }
Ihab Awad104f8062014-07-17 11:29:35 -0700166 }
Santos Cordon176ae282014-07-14 02:02:14 -0700167
Yorke Lee5e8836a2014-08-22 15:25:18 -0700168 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
169 if (mState.defaultOutgoing != null) {
170 // Return the registered outgoing default iff it still exists (we keep a sticky
171 // default to survive account deletion and re-addition)
172 for (int i = 0; i < mState.accounts.size(); i++) {
173 if (mState.accounts.get(i).getAccountHandle().equals(mState.defaultOutgoing)) {
174 return mState.defaultOutgoing;
175 }
176 }
177 // At this point, there was a registered default but it has been deleted; proceed
178 // as though there were no default
179 }
180 return null;
181 }
182
Andrew Leea51a3862014-09-03 14:58:45 -0700183 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700184 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700185 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700186 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700187 } else {
188 boolean found = false;
Ihab Awad293edf22014-07-24 17:52:29 -0700189 for (PhoneAccount m : mState.accounts) {
Evan Charlton94d01622014-07-20 12:32:05 -0700190 if (Objects.equals(accountHandle, m.getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700191 found = true;
192 break;
193 }
Santos Cordon176ae282014-07-14 02:02:14 -0700194 }
Ihab Awad104f8062014-07-17 11:29:35 -0700195
196 if (!found) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700197 Log.w(this, "Trying to set nonexistent default outgoing %s",
198 accountHandle);
199 return;
200 }
201
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700202 if (!getPhoneAccount(accountHandle).hasCapabilities(
203 PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700204 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700205 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700206 return;
207 }
208
Ihab Awad293edf22014-07-24 17:52:29 -0700209 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700210 }
211
Ihab Awad293edf22014-07-24 17:52:29 -0700212 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700213 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700214 }
215
Ihab Awad293edf22014-07-24 17:52:29 -0700216 public void setSimCallManager(PhoneAccountHandle callManager) {
217 if (callManager != null) {
218 PhoneAccount callManagerAccount = getPhoneAccount(callManager);
219 if (callManagerAccount == null) {
220 Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager);
221 return;
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700222 } else if (!callManagerAccount.hasCapabilities(
223 PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) {
Ihab Awad293edf22014-07-24 17:52:29 -0700224 Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount);
225 return;
226 }
Yorke Lee5e8836a2014-08-22 15:25:18 -0700227 } else {
228 callManager = NO_ACCOUNT_SELECTED;
Ihab Awad293edf22014-07-24 17:52:29 -0700229 }
230 mState.simCallManager = callManager;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700231
Ihab Awad293edf22014-07-24 17:52:29 -0700232 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700233 fireSimCallManagerChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700234 }
235
236 public PhoneAccountHandle getSimCallManager() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700237 if (mState.simCallManager != null) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700238 if (NO_ACCOUNT_SELECTED.equals(mState.simCallManager)) {
239 return null;
240 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700241 // Return the registered sim call manager iff it still exists (we keep a sticky
242 // setting to survive account deletion and re-addition)
243 for (int i = 0; i < mState.accounts.size(); i++) {
244 if (mState.accounts.get(i).getAccountHandle().equals(mState.simCallManager)) {
245 return mState.simCallManager;
246 }
247 }
248 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700249
250 // See if the OEM has specified a default one.
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700251 String defaultConnectionMgr =
Santos Cordonafe59e52014-08-22 16:48:43 -0700252 mContext.getResources().getString(R.string.default_connection_manager_component);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700253 if (!TextUtils.isEmpty(defaultConnectionMgr)) {
Santos Cordonafe59e52014-08-22 16:48:43 -0700254 PackageManager pm = mContext.getPackageManager();
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700255
256 ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr);
257 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
258 intent.setComponent(componentName);
259
260 // Make sure that the component can be resolved.
261 List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent, 0);
262 if (!resolveInfos.isEmpty()) {
263 // See if there is registered PhoneAccount by this component.
264 List<PhoneAccountHandle> handles = getAllPhoneAccountHandles();
265 for (PhoneAccountHandle handle : handles) {
266 if (componentName.equals(handle.getComponentName())) {
267 return handle;
268 }
269 }
270 Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName);
271 } else {
272 Log.d(this, "%s could not be resolved; not using as default", componentName);
273 }
274 } else {
275 Log.v(this, "No default connection manager specified");
276 }
277
Ihab Awadb78b2762014-07-25 15:16:23 -0700278 return null;
Ihab Awad293edf22014-07-24 17:52:29 -0700279 }
280
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700281 /**
282 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
283 *
284 * @return The list of {@link PhoneAccountHandle}s.
285 */
Ihab Awad293edf22014-07-24 17:52:29 -0700286 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
287 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
288 for (PhoneAccount m : mState.accounts) {
289 accountHandles.add(m.getAccountHandle());
290 }
291 return accountHandles;
292 }
293
294 public List<PhoneAccount> getAllPhoneAccounts() {
295 return new ArrayList<>(mState.accounts);
296 }
297
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700298 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700299 * Determines the number of all {@link PhoneAccount}s.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700300 *
Nancy Chen309198e2014-09-15 18:02:49 -0700301 * @return The total number {@link PhoneAccount}s.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700302 */
303 public int getAllPhoneAccountsCount() {
304 return mState.accounts.size();
305 }
306
307 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700308 * Retrieves a list of all call provider phone accounts.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700309 *
310 * @return The phone account handles.
311 */
Nancy Chen309198e2014-09-15 18:02:49 -0700312 public List<PhoneAccountHandle> getCallCapablePhoneAccounts() {
Santos Cordonafe59e52014-08-22 16:48:43 -0700313 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER);
314 }
315
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700316 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700317 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700318 * specified URI scheme.
319 *
320 * @param uriScheme The URI scheme.
321 * @return The phone account handles.
322 */
Nancy Chen309198e2014-09-15 18:02:49 -0700323 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) {
324 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme);
Tyler Gunn84253572014-09-02 14:50:05 -0700325 }
326
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700327 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700328 * Retrieves a list of all phone accounts registered by a specified package.
329 *
330 * @param packageName The name of the package that registered the phone accounts.
331 * @return The phone account handles.
332 */
333 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
334 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
335 for (PhoneAccount m : mState.accounts) {
336 if (Objects.equals(
337 packageName,
338 m.getAccountHandle().getComponentName().getPackageName())) {
339 accountHandles.add(m.getAccountHandle());
340 }
341 }
342 return accountHandles;
343 }
344
345 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700346 * Retrieves a list of all phone account handles with the connection manager capability.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700347 *
348 * @return The phone account handles.
349 */
350 public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() {
Santos Cordone3d82452014-09-15 13:44:29 -0700351 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER,
Nancy Chen309198e2014-09-15 18:02:49 -0700352 null /* supportedUriScheme */);
Santos Cordon176ae282014-07-14 02:02:14 -0700353 }
354
Ihab Awad293edf22014-07-24 17:52:29 -0700355 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
356 for (PhoneAccount m : mState.accounts) {
357 if (Objects.equals(handle, m.getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700358 return m;
Santos Cordon176ae282014-07-14 02:02:14 -0700359 }
360 }
361 return null;
362 }
363
Ihab Awad104f8062014-07-17 11:29:35 -0700364 // TODO: Should we implement an artificial limit for # of accounts associated with a single
365 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700366 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700367 // Enforce the requirement that a connection service for a phone account has the correct
368 // permission.
369 if (!phoneAccountHasPermission(account.getAccountHandle())) {
370 Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.",
371 account.getAccountHandle());
372 throw new SecurityException(
373 "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission.");
374 }
375
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700376 addOrReplacePhoneAccount(account);
377 }
378
379 /**
380 * Adds a {@code PhoneAccount}, replacing an existing one if found.
381 *
382 * @param account The {@code PhoneAccount} to add or replace.
383 */
384 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad293edf22014-07-24 17:52:29 -0700385 mState.accounts.add(account);
Ihab Awad104f8062014-07-17 11:29:35 -0700386 // Search for duplicates and remove any that are found.
Ihab Awad293edf22014-07-24 17:52:29 -0700387 for (int i = 0; i < mState.accounts.size() - 1; i++) {
388 if (Objects.equals(
389 account.getAccountHandle(), mState.accounts.get(i).getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700390 // replace existing entry.
Ihab Awad293edf22014-07-24 17:52:29 -0700391 mState.accounts.remove(i);
Ihab Awad104f8062014-07-17 11:29:35 -0700392 break;
Santos Cordon176ae282014-07-14 02:02:14 -0700393 }
394 }
395
Ihab Awad293edf22014-07-24 17:52:29 -0700396 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700397 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700398 }
399
Evan Charlton89176372014-07-19 18:23:09 -0700400 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Ihab Awad293edf22014-07-24 17:52:29 -0700401 for (int i = 0; i < mState.accounts.size(); i++) {
402 if (Objects.equals(accountHandle, mState.accounts.get(i).getAccountHandle())) {
403 mState.accounts.remove(i);
Ihab Awad104f8062014-07-17 11:29:35 -0700404 break;
405 }
406 }
407
Ihab Awad293edf22014-07-24 17:52:29 -0700408 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700409 fireAccountsChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700410 }
411
Tyler Gunnd900ce62014-08-13 11:40:59 -0700412 /**
413 * Un-registers all phone accounts associated with a specified package.
414 *
415 * @param packageName The package for which phone accounts will be removed.
416 */
Ihab Awad104f8062014-07-17 11:29:35 -0700417 public void clearAccounts(String packageName) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700418 boolean accountsRemoved = false;
419 Iterator<PhoneAccount> it = mState.accounts.iterator();
420 while (it.hasNext()) {
421 PhoneAccount phoneAccount = it.next();
Ihab Awad104f8062014-07-17 11:29:35 -0700422 if (Objects.equals(
423 packageName,
Tyler Gunnd900ce62014-08-13 11:40:59 -0700424 phoneAccount.getAccountHandle().getComponentName().getPackageName())) {
425 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
426 it.remove();
427 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700428 }
429 }
430
Tyler Gunnd900ce62014-08-13 11:40:59 -0700431 if (accountsRemoved) {
432 write();
433 fireAccountsChanged();
434 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700435 }
436
Nancy Chen140004a2014-10-15 15:48:38 -0700437 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700438 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700439 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
440 }
441
Ihab Awadb78b2762014-07-25 15:16:23 -0700442 public void addListener(Listener l) {
443 mListeners.add(l);
444 }
445
446 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700447 if (l != null) {
448 mListeners.remove(l);
449 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700450 }
451
452 private void fireAccountsChanged() {
453 for (Listener l : mListeners) {
454 l.onAccountsChanged(this);
455 }
456 }
457
458 private void fireDefaultOutgoingChanged() {
459 for (Listener l : mListeners) {
460 l.onDefaultOutgoingChanged(this);
461 }
462 }
463
464 private void fireSimCallManagerChanged() {
465 for (Listener l : mListeners) {
466 l.onSimCallManagerChanged(this);
467 }
Santos Cordon176ae282014-07-14 02:02:14 -0700468 }
469
Tyler Gunncb59b672014-08-20 09:02:11 -0700470 /**
471 * Determines if the connection service specified by a {@link PhoneAccountHandle} has the
472 * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission.
473 *
474 * @param phoneAccountHandle The phone account to check.
475 * @return {@code True} if the phone account has permission.
476 */
477 public boolean phoneAccountHasPermission(PhoneAccountHandle phoneAccountHandle) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700478 PackageManager packageManager = mContext.getPackageManager();
Tyler Gunncb59b672014-08-20 09:02:11 -0700479 try {
480 ServiceInfo serviceInfo = packageManager.getServiceInfo(
481 phoneAccountHandle.getComponentName(), 0);
482
483 return serviceInfo.permission != null &&
484 serviceInfo.permission.equals(Manifest.permission.BIND_CONNECTION_SERVICE);
485 } catch (PackageManager.NameNotFoundException e) {
486 Log.w(this, "Name not found %s", e);
487 return false;
488 }
489 }
490
Ihab Awad293edf22014-07-24 17:52:29 -0700491 ////////////////////////////////////////////////////////////////////////////////////////////////
492
Santos Cordonafe59e52014-08-22 16:48:43 -0700493 /**
494 * Returns a list of phone account handles with the specified flag.
Tyler Gunn84253572014-09-02 14:50:05 -0700495 *
496 * @param flags Flags which the {@code PhoneAccount} must have.
Santos Cordonafe59e52014-08-22 16:48:43 -0700497 */
498 private List<PhoneAccountHandle> getPhoneAccountHandles(int flags) {
Nancy Chen309198e2014-09-15 18:02:49 -0700499 return getPhoneAccountHandles(flags, null);
Tyler Gunn84253572014-09-02 14:50:05 -0700500 }
501
502 /**
503 * Returns a list of phone account handles with the specified flag, supporting the specified
Nancy Chen309198e2014-09-15 18:02:49 -0700504 * URI scheme.
Tyler Gunn84253572014-09-02 14:50:05 -0700505 *
506 * @param flags Flags which the {@code PhoneAccount} must have.
507 * @param uriScheme URI schemes the PhoneAccount must handle. {@code Null} bypasses the
508 * URI scheme check.
509 */
Nancy Chen309198e2014-09-15 18:02:49 -0700510 private List<PhoneAccountHandle> getPhoneAccountHandles(int flags, String uriScheme) {
Evan Charlton94d01622014-07-20 12:32:05 -0700511 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
Ihab Awad293edf22014-07-24 17:52:29 -0700512 for (PhoneAccount m : mState.accounts) {
Nancy Chen309198e2014-09-15 18:02:49 -0700513 if (m.hasCapabilities(flags) && (uriScheme == null || m.supportsUriScheme(uriScheme))) {
Ihab Awadf2a84912014-07-22 21:09:25 -0700514 accountHandles.add(m.getAccountHandle());
515 }
Ihab Awad104f8062014-07-17 11:29:35 -0700516 }
Evan Charlton94d01622014-07-20 12:32:05 -0700517 return accountHandles;
Ihab Awad104f8062014-07-17 11:29:35 -0700518 }
519
Ihab Awad293edf22014-07-24 17:52:29 -0700520 /**
521 * The state of this {@code PhoneAccountRegistrar}.
522 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700523 @VisibleForTesting
524 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700525 /**
526 * The account selected by the user to be employed by default for making outgoing calls.
527 * If the user has not made such a selection, then this is null.
528 */
529 public PhoneAccountHandle defaultOutgoing = null;
530
531 /**
Ihab Awadb78b2762014-07-25 15:16:23 -0700532 * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which
Ihab Awad293edf22014-07-24 17:52:29 -0700533 * manages and optimizes a user's PSTN SIM connections.
534 */
535 public PhoneAccountHandle simCallManager;
536
537 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700538 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700539 */
540 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700541
542 /**
543 * The version number of the State data.
544 */
545 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700546 }
547
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700548 /**
549 * Dumps the state of the {@link CallsManager}.
550 *
551 * @param pw The {@code IndentingPrintWriter} to write the state to.
552 */
553 public void dump(IndentingPrintWriter pw) {
554 if (mState != null) {
555 pw.println("xmlVersion: " + mState.versionNumber);
556 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
557 mState.defaultOutgoing));
558 pw.println("simCallManager: " + (mState.simCallManager == null ? "none" :
559 mState.simCallManager));
560 pw.println("phoneAccounts:");
561 pw.increaseIndent();
562 for (PhoneAccount phoneAccount : mState.accounts) {
563 pw.println(phoneAccount);
564 }
565 pw.decreaseIndent();
566 }
567 }
568
Ihab Awad293edf22014-07-24 17:52:29 -0700569 ////////////////////////////////////////////////////////////////////////////////////////////////
570 //
571 // State management
572 //
573
574 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700575 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700576 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700577 os = mAtomicFile.startWrite();
578 boolean success = false;
579 try {
580 XmlSerializer serializer = new FastXmlSerializer();
581 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
582 writeToXml(mState, serializer);
583 serializer.flush();
584 success = true;
585 } finally {
586 if (success) {
587 mAtomicFile.finishWrite(os);
588 } else {
589 mAtomicFile.failWrite(os);
590 }
591 }
592 } catch (IOException e) {
593 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700594 }
595 }
596
Ihab Awadb78b2762014-07-25 15:16:23 -0700597 private void read() {
598 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700599 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700600 is = mAtomicFile.openRead();
601 } catch (FileNotFoundException ex) {
602 return;
603 }
604
Tyler Gunn84253572014-09-02 14:50:05 -0700605 boolean versionChanged = false;
606
Ihab Awadb78b2762014-07-25 15:16:23 -0700607 XmlPullParser parser;
608 try {
609 parser = Xml.newPullParser();
610 parser.setInput(new BufferedInputStream(is), null);
611 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700612 mState = readFromXml(parser, mContext);
613 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
614
Ihab Awadb78b2762014-07-25 15:16:23 -0700615 } catch (IOException | XmlPullParserException e) {
616 Log.e(this, e, "Reading state from XML file");
617 mState = new State();
618 } finally {
619 try {
620 is.close();
621 } catch (IOException e) {
622 Log.e(this, e, "Closing InputStream");
623 }
Ihab Awad104f8062014-07-17 11:29:35 -0700624 }
Tyler Gunn84253572014-09-02 14:50:05 -0700625
626 // If an upgrade occurred, write out the changed data.
627 if (versionChanged) {
628 write();
629 }
Santos Cordon176ae282014-07-14 02:02:14 -0700630 }
631
Ihab Awadb78b2762014-07-25 15:16:23 -0700632 private static void writeToXml(State state, XmlSerializer serializer)
633 throws IOException {
634 sStateXml.writeToXml(state, serializer);
Santos Cordon176ae282014-07-14 02:02:14 -0700635 }
Ihab Awad104f8062014-07-17 11:29:35 -0700636
Tyler Gunn84253572014-09-02 14:50:05 -0700637 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700638 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700639 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700640 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700641 }
642
Ihab Awad293edf22014-07-24 17:52:29 -0700643 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700644 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700645 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700646 //
647
Ihab Awadb78b2762014-07-25 15:16:23 -0700648 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700649 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700650 private static final String LENGTH_ATTRIBUTE = "length";
651 private static final String VALUE_TAG = "value";
652
Ihab Awadb78b2762014-07-25 15:16:23 -0700653 /**
654 * Write the supplied object to XML
655 */
Ihab Awad26923222014-07-30 10:54:35 -0700656 public abstract void writeToXml(T o, XmlSerializer serializer)
657 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700658
659 /**
660 * Read from the supplied XML into a new object, returning null in case of an
661 * unrecoverable schema mismatch or other data error. 'parser' must be already
662 * positioned at the first tag that is expected to have been emitted by this
663 * object's writeToXml(). This object tries to fail early without modifying
664 * 'parser' if it does not recognize the data it sees.
665 */
Tyler Gunn84253572014-09-02 14:50:05 -0700666 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700667 throws IOException, XmlPullParserException;
668
669 protected void writeTextSafely(String tagName, Object value, XmlSerializer serializer)
670 throws IOException {
671 if (value != null) {
672 serializer.startTag(null, tagName);
673 serializer.text(Objects.toString(value));
674 serializer.endTag(null, tagName);
675 }
676 }
Tyler Gunn84253572014-09-02 14:50:05 -0700677
678 /**
679 * Serializes a string array.
680 *
681 * @param tagName The tag name for the string array.
682 * @param values The string values to serialize.
683 * @param serializer The serializer.
684 * @throws IOException
685 */
686 protected void writeStringList(String tagName, List<String> values,
687 XmlSerializer serializer)
688 throws IOException {
689
690 serializer.startTag(null, tagName);
691 if (values != null) {
692 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
693 for (String toSerialize : values) {
694 serializer.startTag(null, VALUE_TAG);
695 if (toSerialize != null ){
696 serializer.text(toSerialize);
697 }
Tyler Gunn84253572014-09-02 14:50:05 -0700698 serializer.endTag(null, VALUE_TAG);
699 }
700 } else {
701 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
702 }
703 serializer.endTag(null, tagName);
704
705 }
706
707 /**
708 * Reads a string array from the XML parser.
709 *
710 * @param parser The XML parser.
711 * @return String array containing the parsed values.
712 * @throws IOException Exception related to IO.
713 * @throws XmlPullParserException Exception related to parsing.
714 */
715 protected List<String> readStringList(XmlPullParser parser)
716 throws IOException, XmlPullParserException {
717
718 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
719 List<String> arrayEntries = new ArrayList<String>(length);
720 String value = null;
721
722 if (length == 0) {
723 return arrayEntries;
724 }
725
726 int outerDepth = parser.getDepth();
727 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
728 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700729 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -0700730 value = parser.getText();
731 arrayEntries.add(value);
732 }
733 }
734
735 return arrayEntries;
736 }
Ihab Awad104f8062014-07-17 11:29:35 -0700737 }
738
Ihab Awadb78b2762014-07-25 15:16:23 -0700739 @VisibleForTesting
740 public static final XmlSerialization<State> sStateXml =
741 new XmlSerialization<State>() {
742 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -0700743 private static final String DEFAULT_OUTGOING = "default_outgoing";
Ihab Awad293edf22014-07-24 17:52:29 -0700744 private static final String SIM_CALL_MANAGER = "sim_call_manager";
Ihab Awad104f8062014-07-17 11:29:35 -0700745 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -0700746 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -0700747
748 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700749 public void writeToXml(State o, XmlSerializer serializer)
750 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700751 if (o != null) {
752 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -0700753 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -0700754
Ihab Awad26923222014-07-30 10:54:35 -0700755 if (o.defaultOutgoing != null) {
756 serializer.startTag(null, DEFAULT_OUTGOING);
757 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer);
758 serializer.endTag(null, DEFAULT_OUTGOING);
759 }
760
761 if (o.simCallManager != null) {
762 serializer.startTag(null, SIM_CALL_MANAGER);
763 sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer);
764 serializer.endTag(null, SIM_CALL_MANAGER);
765 }
766
767 serializer.startTag(null, ACCOUNTS);
768 for (PhoneAccount m : o.accounts) {
769 sPhoneAccountXml.writeToXml(m, serializer);
770 }
771 serializer.endTag(null, ACCOUNTS);
772
773 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -0700774 }
Ihab Awad104f8062014-07-17 11:29:35 -0700775 }
776
777 @Override
Tyler Gunn84253572014-09-02 14:50:05 -0700778 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700779 throws IOException, XmlPullParserException {
780 if (parser.getName().equals(CLASS_STATE)) {
781 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -0700782
783 String rawVersion = parser.getAttributeValue(null, VERSION);
784 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
785 Integer.parseInt(rawVersion);
786
Ihab Awadb78b2762014-07-25 15:16:23 -0700787 int outerDepth = parser.getDepth();
788 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
789 if (parser.getName().equals(DEFAULT_OUTGOING)) {
790 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700791 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
792 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700793 } else if (parser.getName().equals(SIM_CALL_MANAGER)) {
794 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700795 s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser,
796 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700797 } else if (parser.getName().equals(ACCOUNTS)) {
798 int accountsDepth = parser.getDepth();
799 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700800 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
801 s.versionNumber, context);
802
803 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700804 s.accounts.add(account);
805 }
806 }
Ihab Awad104f8062014-07-17 11:29:35 -0700807 }
808 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700809 return s;
Ihab Awad104f8062014-07-17 11:29:35 -0700810 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700811 return null;
Ihab Awad104f8062014-07-17 11:29:35 -0700812 }
813 };
814
Ihab Awadb78b2762014-07-25 15:16:23 -0700815 @VisibleForTesting
816 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
817 new XmlSerialization<PhoneAccount>() {
818 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
819 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -0700820 private static final String ADDRESS = "handle";
821 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -0700822 private static final String CAPABILITIES = "capabilities";
823 private static final String ICON_RES_ID = "icon_res_id";
Nancy Chen06ce0622014-10-23 01:17:35 +0000824 private static final String COLOR = "color";
Ihab Awad104f8062014-07-17 11:29:35 -0700825 private static final String LABEL = "label";
826 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -0700827 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700828 private static final String TRUE = "true";
829 private static final String FALSE = "false";
Ihab Awad104f8062014-07-17 11:29:35 -0700830
831 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700832 public void writeToXml(PhoneAccount o, XmlSerializer serializer)
833 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700834 if (o != null) {
835 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -0700836
Ihab Awad26923222014-07-30 10:54:35 -0700837 if (o.getAccountHandle() != null) {
838 serializer.startTag(null, ACCOUNT_HANDLE);
839 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer);
840 serializer.endTag(null, ACCOUNT_HANDLE);
841 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700842
Andrew Lee7129f1c2014-09-04 11:55:07 -0700843 writeTextSafely(ADDRESS, o.getAddress(), serializer);
844 writeTextSafely(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
Ihab Awad26923222014-07-30 10:54:35 -0700845 writeTextSafely(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
846 writeTextSafely(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer);
Nancy Chen06ce0622014-10-23 01:17:35 +0000847 writeTextSafely(COLOR, Integer.toString(o.getColor()), serializer);
Ihab Awad26923222014-07-30 10:54:35 -0700848 writeTextSafely(LABEL, o.getLabel(), serializer);
849 writeTextSafely(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -0700850 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -0700851
Ihab Awad26923222014-07-30 10:54:35 -0700852 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
853 }
Ihab Awad104f8062014-07-17 11:29:35 -0700854 }
855
Tyler Gunn84253572014-09-02 14:50:05 -0700856 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700857 throws IOException, XmlPullParserException {
858 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
859 int outerDepth = parser.getDepth();
860 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -0700861 Uri address = null;
862 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -0700863 int capabilities = 0;
864 int iconResId = 0;
Nancy Chen06ce0622014-10-23 01:17:35 +0000865 int color = 0;
Ihab Awadb78b2762014-07-25 15:16:23 -0700866 String label = null;
867 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -0700868 List<String> supportedUriSchemes = null;
Ihab Awadb78b2762014-07-25 15:16:23 -0700869
870 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
871 if (parser.getName().equals(ACCOUNT_HANDLE)) {
872 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700873 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
874 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -0700875 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700876 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -0700877 address = Uri.parse(parser.getText());
878 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700879 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -0700880 String nextText = parser.getText();
881 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -0700882 } else if (parser.getName().equals(CAPABILITIES)) {
883 parser.next();
884 capabilities = Integer.parseInt(parser.getText());
885 } else if (parser.getName().equals(ICON_RES_ID)) {
886 parser.next();
887 iconResId = Integer.parseInt(parser.getText());
Nancy Chen06ce0622014-10-23 01:17:35 +0000888 } else if (parser.getName().equals(COLOR)) {
889 parser.next();
890 color = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -0700891 } else if (parser.getName().equals(LABEL)) {
892 parser.next();
893 label = parser.getText();
894 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
895 parser.next();
896 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -0700897 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
898 supportedUriSchemes = readStringList(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -0700899 }
900 }
Tyler Gunn84253572014-09-02 14:50:05 -0700901
902 // Upgrade older phone accounts to specify the supported URI schemes.
903 if (version < 2) {
904 ComponentName sipComponentName = new ComponentName("com.android.phone",
905 "com.android.services.telephony.sip.SipConnectionService");
906
907 supportedUriSchemes = new ArrayList<>();
908
909 // Handle the SIP connection service.
910 // Check the system settings to see if it also should handle "tel" calls.
911 if (accountHandle.getComponentName().equals(sipComponentName)) {
912 boolean useSipForPstn = useSipForPstnCalls(context);
913 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
914 if (useSipForPstn) {
915 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
916 }
917 } else {
918 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
919 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
920 }
921 }
922
Andrew Lee7129f1c2014-09-04 11:55:07 -0700923 return PhoneAccount.builder(accountHandle, label)
924 .setAddress(address)
925 .setSubscriptionAddress(subscriptionAddress)
926 .setCapabilities(capabilities)
927 .setIconResId(iconResId)
Nancy Chen06ce0622014-10-23 01:17:35 +0000928 .setColor(color)
Andrew Lee7129f1c2014-09-04 11:55:07 -0700929 .setShortDescription(shortDescription)
930 .setSupportedUriSchemes(supportedUriSchemes)
Ihab Awad6fb37c82014-08-07 19:48:57 -0700931 .build();
Ihab Awadb78b2762014-07-25 15:16:23 -0700932 }
933 return null;
Ihab Awad104f8062014-07-17 11:29:35 -0700934 }
Tyler Gunn84253572014-09-02 14:50:05 -0700935
936 /**
937 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN calls.
938 *
939 * @param context The context.
940 * @return {@code True} if SIP should be used for all calls.
941 */
942 private boolean useSipForPstnCalls(Context context) {
943 String option = Settings.System.getString(context.getContentResolver(),
944 Settings.System.SIP_CALL_OPTIONS);
945 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
946 return option.equals(Settings.System.SIP_ALWAYS);
947 }
Ihab Awad104f8062014-07-17 11:29:35 -0700948 };
949
Ihab Awadb78b2762014-07-25 15:16:23 -0700950 @VisibleForTesting
951 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
952 new XmlSerialization<PhoneAccountHandle>() {
953 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -0700954 private static final String COMPONENT_NAME = "component_name";
955 private static final String ID = "id";
956
957 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700958 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer)
959 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700960 if (o != null) {
961 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -0700962
Ihab Awad26923222014-07-30 10:54:35 -0700963 if (o.getComponentName() != null) {
964 writeTextSafely(
965 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
966 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700967
Ihab Awad26923222014-07-30 10:54:35 -0700968 writeTextSafely(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -0700969
Ihab Awad26923222014-07-30 10:54:35 -0700970 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
971 }
Ihab Awad104f8062014-07-17 11:29:35 -0700972 }
973
974 @Override
Tyler Gunn84253572014-09-02 14:50:05 -0700975 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700976 throws IOException, XmlPullParserException {
977 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
978 String componentNameString = null;
979 String idString = null;
980 int outerDepth = parser.getDepth();
981 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
982 if (parser.getName().equals(COMPONENT_NAME)) {
983 parser.next();
984 componentNameString = parser.getText();
985 } else if (parser.getName().equals(ID)) {
986 parser.next();
987 idString = parser.getText();
988 }
989 }
Ihab Awad26923222014-07-30 10:54:35 -0700990 if (componentNameString != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700991 return new PhoneAccountHandle(
992 ComponentName.unflattenFromString(componentNameString),
993 idString);
994 }
995 }
996 return null;
Ihab Awad104f8062014-07-17 11:29:35 -0700997 }
998 };
Santos Cordon176ae282014-07-14 02:02:14 -0700999}