Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 18 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ResolveInfo; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 25 | import android.content.pm.ServiceInfo; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 26 | import android.content.pm.UserInfo; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 27 | import android.graphics.Bitmap; |
| 28 | import android.graphics.BitmapFactory; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 29 | import android.graphics.drawable.Icon; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 30 | import android.net.Uri; |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 31 | import android.os.Binder; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 32 | import android.os.Process; |
| 33 | import android.os.UserHandle; |
| 34 | import android.os.UserManager; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 35 | import android.provider.Settings; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 36 | import android.telecom.ConnectionService; |
| 37 | import android.telecom.PhoneAccount; |
| 38 | import android.telecom.PhoneAccountHandle; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 39 | import android.telephony.PhoneNumberUtils; |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 40 | import android.telephony.SubscriptionManager; |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 41 | import android.telephony.TelephonyManager; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 42 | import android.text.TextUtils; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 43 | import android.util.AtomicFile; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 44 | import android.util.Base64; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 45 | import android.util.Xml; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 46 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 47 | // TODO: Needed for move to system service: import com.android.internal.R; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 48 | import com.android.internal.annotations.VisibleForTesting; |
| 49 | import com.android.internal.util.FastXmlSerializer; |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 50 | import com.android.internal.util.IndentingPrintWriter; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 51 | import com.android.internal.util.XmlUtils; |
| 52 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 53 | import org.xmlpull.v1.XmlPullParser; |
| 54 | import org.xmlpull.v1.XmlPullParserException; |
| 55 | import org.xmlpull.v1.XmlSerializer; |
| 56 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 57 | import java.io.BufferedInputStream; |
| 58 | import java.io.BufferedOutputStream; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 59 | import java.io.ByteArrayInputStream; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 60 | import java.io.ByteArrayOutputStream; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 61 | import java.io.File; |
| 62 | import java.io.FileNotFoundException; |
| 63 | import java.io.FileOutputStream; |
| 64 | import java.io.IOException; |
| 65 | import java.io.InputStream; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 66 | import java.lang.Integer; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 67 | import java.lang.SecurityException; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 68 | import java.lang.String; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 69 | import java.util.ArrayList; |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 70 | import java.util.Collections; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 71 | import java.util.Iterator; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 72 | import java.util.List; |
| 73 | import java.util.Objects; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 74 | import java.util.concurrent.CopyOnWriteArrayList; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 77 | * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 78 | * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as |
| 79 | * implemented in {@link TelecomServiceImpl}, with the notable exception that |
| 80 | * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has |
| 81 | * proper authority over the {@code ComponentName}s they are declaring in their |
| 82 | * {@code PhoneAccountHandle}s. |
| 83 | * |
| 84 | * |
| 85 | * -- About Users and Phone Accounts -- |
| 86 | * |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 87 | * We store all phone accounts for all users in a single place, which means that there are three |
| 88 | * users that we have to deal with in code: |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 89 | * 1) The Android User that is currently active on the device. |
| 90 | * 2) The user which owns/registers the phone account. |
| 91 | * 3) The user running the app that is requesting the phone account information. |
| 92 | * |
| 93 | * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user |
| 94 | * has a work profile running as another user (B2). Lets say that user B opens the phone settings |
| 95 | * (not currently supported, but theoretically speaking), and phone settings queries for a phone |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 96 | * account list. Lets also say that an app running in the work profile has registered a phone |
| 97 | * account. This means that: |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 98 | * |
| 99 | * Since phone settings always runs as the primary user, We have the following situation: |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 100 | * User A (settings) is requesting a list of phone accounts while the active user is User B, and |
| 101 | * that list contains a phone account for profile User B2. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 102 | * |
| 103 | * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is |
| 104 | * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these |
| 105 | * users for visibility before returning any phone accounts. |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 106 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 107 | public final class PhoneAccountRegistrar { |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 108 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 109 | public static final PhoneAccountHandle NO_ACCOUNT_SELECTED = |
| 110 | new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED"); |
| 111 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 112 | public abstract static class Listener { |
| 113 | public void onAccountsChanged(PhoneAccountRegistrar registrar) {} |
| 114 | public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {} |
| 115 | public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {} |
| 116 | } |
| 117 | |
| 118 | private static final String FILE_NAME = "phone-account-registrar-state.xml"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 119 | @VisibleForTesting |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 120 | public static final int EXPECTED_STATE_VERSION = 5; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 121 | |
| 122 | /** Keep in sync with the same in SipSettings.java */ |
| 123 | private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES"; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 124 | |
| 125 | private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); |
| 126 | private final AtomicFile mAtomicFile; |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 127 | private final Context mContext; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 128 | private final UserManager mUserManager; |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 129 | private final SubscriptionManager mSubscriptionManager; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 130 | private State mState; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 131 | private UserHandle mCurrentUserHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 132 | |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 133 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 134 | public PhoneAccountRegistrar(Context context) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 135 | this(context, FILE_NAME); |
| 136 | } |
| 137 | |
| 138 | @VisibleForTesting |
| 139 | public PhoneAccountRegistrar(Context context, String fileName) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 140 | // TODO: This file path is subject to change -- it is storing the phone account registry |
| 141 | // state file in the path /data/system/users/0/, which is likely not correct in a |
| 142 | // multi-user setting. |
| 143 | /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE |
| 144 | String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()). |
| 145 | getAbsolutePath(); |
| 146 | mAtomicFile = new AtomicFile(new File(filePath, fileName)); |
| 147 | UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 148 | mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName)); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 149 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 150 | mState = new State(); |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 151 | mContext = context; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 152 | mUserManager = UserManager.get(context); |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 153 | mSubscriptionManager = SubscriptionManager.from(mContext); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 154 | mCurrentUserHandle = Process.myUserHandle(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 155 | read(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 158 | /** |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 159 | * Retrieves the subscription id for a given phone account if it exists. Subscription ids |
| 160 | * apply only to PSTN/SIM card phone accounts so all other accounts should not have a |
| 161 | * subscription id. |
| 162 | * @param accountHandle The handle for the phone account for which to retrieve the |
| 163 | * subscription id. |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 164 | * @return The value of the subscription id or -1 if it does not exist or is not valid. |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 165 | */ |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 166 | public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 167 | PhoneAccount account = getPhoneAccountCheckCallingUser(accountHandle); |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 168 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 169 | if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 170 | TelephonyManager tm = |
| 171 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 172 | return tm.getSubIdForPhoneAccount(account); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 173 | } |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 174 | return SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 178 | * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if |
| 179 | * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null} |
| 180 | * will be returned. |
| 181 | * |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 182 | * @param uriScheme The URI scheme for the outgoing call. |
| 183 | * @return The {@link PhoneAccountHandle} to use. |
| 184 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 185 | public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 186 | final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 187 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 188 | if (userSelected != null) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 189 | // If there is a default PhoneAccount, ensure it supports calls to handles with the |
| 190 | // specified uriScheme. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 191 | final PhoneAccount userSelectedAccount = getPhoneAccountCheckCallingUser(userSelected); |
| 192 | if (userSelectedAccount.supportsUriScheme(uriScheme)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 193 | return userSelected; |
| 194 | } |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 197 | List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 198 | switch (outgoing.size()) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 199 | case 0: |
| 200 | // There are no accounts, so there can be no default |
| 201 | return null; |
| 202 | case 1: |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 203 | // There is only one account, which is by definition the default. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 204 | return outgoing.get(0); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 205 | default: |
| 206 | // There are multiple accounts with no selected default |
| 207 | return null; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 208 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 209 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 210 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 211 | /** |
| 212 | * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or |
| 213 | * if it was set by another user). |
| 214 | */ |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 215 | PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 216 | PhoneAccount account = getPhoneAccountCheckCallingUser(mState.defaultOutgoing); |
| 217 | if (account != null) { |
| 218 | return mState.defaultOutgoing; |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 219 | } |
| 220 | return null; |
| 221 | } |
| 222 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 223 | /** |
| 224 | * Sets the phone account with which to place all calls by default. Set by the user |
| 225 | * within phone settings. |
| 226 | */ |
Andrew Lee | a51a386 | 2014-09-03 14:58:45 -0700 | [diff] [blame] | 227 | public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 228 | if (accountHandle == null) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 229 | // Asking to clear the default outgoing is a valid request |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 230 | mState.defaultOutgoing = null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 231 | } else { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 232 | // TODO: Do we really want to return for *any* user? |
| 233 | PhoneAccount account = getPhoneAccount(accountHandle); |
| 234 | if (account == null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 235 | Log.w(this, "Trying to set nonexistent default outgoing %s", |
| 236 | accountHandle); |
| 237 | return; |
| 238 | } |
| 239 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 240 | if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 241 | Log.w(this, "Trying to set non-call-provider default outgoing %s", |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 242 | accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 246 | if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 247 | // If the account selected is a SIM account, propagate down to the subscription |
| 248 | // record. |
Wink Saville | 7ce6e78 | 2014-10-27 10:56:46 -0700 | [diff] [blame] | 249 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 250 | mSubscriptionManager.setDefaultVoiceSubId(subId); |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 253 | mState.defaultOutgoing = accountHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 256 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 257 | fireDefaultOutgoingChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Nancy Chen | 668dee0 | 2014-11-19 15:31:31 -0800 | [diff] [blame] | 260 | boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) { |
| 261 | return getSubscriptionIdForPhoneAccount(accountHandle) == |
| 262 | SubscriptionManager.getDefaultSmsSubId(); |
| 263 | } |
| 264 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 265 | public void setSimCallManager(PhoneAccountHandle callManager) { |
| 266 | if (callManager != null) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 267 | // TODO: Do we really want to return for *any* user? |
| 268 | PhoneAccount callManagerAccount = getPhoneAccount(callManager); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 269 | if (callManagerAccount == null) { |
| 270 | Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager); |
| 271 | return; |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 272 | } else if (!callManagerAccount.hasCapabilities( |
| 273 | PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 274 | Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount); |
| 275 | return; |
| 276 | } |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 277 | } else { |
| 278 | callManager = NO_ACCOUNT_SELECTED; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 279 | } |
| 280 | mState.simCallManager = callManager; |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 281 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 282 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 283 | fireSimCallManagerChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 286 | /** |
| 287 | * @return The {@link PhoneAccount}s which are visible to {@link #mCurrentUserHandle}. |
| 288 | */ |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 289 | public PhoneAccountHandle getSimCallManager() { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 290 | PhoneAccount account = getPhoneAccountCheckCallingUser(mState.simCallManager); |
| 291 | |
| 292 | // Return the registered sim call manager iff it still exists (we keep a sticky |
| 293 | // setting to survive account deletion and re-addition) |
| 294 | if (account != null && !resolveComponent(mState.simCallManager).isEmpty()) { |
| 295 | return mState.simCallManager; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 296 | } |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 297 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 298 | // We have no set call manager, but check to see if the OEM has specified a default one. |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 299 | String defaultConnectionMgr = |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 300 | mContext.getResources().getString(R.string.default_connection_manager_component); |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 301 | if (!TextUtils.isEmpty(defaultConnectionMgr)) { |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 302 | ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 303 | if (componentName == null) { |
| 304 | return null; |
| 305 | } |
| 306 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 307 | // Make sure that the component can be resolved. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 308 | List<ResolveInfo> resolveInfos = resolveComponent(componentName, null); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 309 | if (resolveInfos.isEmpty()) { |
| 310 | resolveInfos = resolveComponent(componentName, Binder.getCallingUserHandle()); |
| 311 | } |
| 312 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 313 | if (!resolveInfos.isEmpty()) { |
| 314 | // See if there is registered PhoneAccount by this component. |
| 315 | List<PhoneAccountHandle> handles = getAllPhoneAccountHandles(); |
| 316 | for (PhoneAccountHandle handle : handles) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 317 | if (componentName.equals(handle.getComponentName())) { |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 318 | return handle; |
| 319 | } |
| 320 | } |
| 321 | Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName); |
| 322 | } else { |
| 323 | Log.d(this, "%s could not be resolved; not using as default", componentName); |
| 324 | } |
| 325 | } else { |
| 326 | Log.v(this, "No default connection manager specified"); |
| 327 | } |
| 328 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 329 | return null; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 332 | /** |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 333 | * Update the current UserHandle to track when users are switched. This will allow the |
| 334 | * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything |
| 335 | * across users. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 336 | * We cannot simply check the calling user because that would always return the primary user for |
| 337 | * all invocations originating with the system process. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 338 | * |
| 339 | * @param userHandle The {@link UserHandle}, as delivered by |
| 340 | * {@link Intent#ACTION_USER_SWITCHED}. |
| 341 | */ |
| 342 | public void setCurrentUserHandle(UserHandle userHandle) { |
| 343 | if (userHandle == null) { |
| 344 | Log.d(this, "setCurrentUserHandle, userHandle = null"); |
| 345 | userHandle = Process.myUserHandle(); |
| 346 | } |
| 347 | Log.d(this, "setCurrentUserHandle, %s", userHandle); |
| 348 | mCurrentUserHandle = userHandle; |
| 349 | } |
| 350 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 351 | private boolean isVisibleForUser(PhoneAccount account) { |
| 352 | if (account == null) { |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and |
| 357 | // all profiles. Only Telephony and SIP accounts should have this capability. |
| 358 | if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) { |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle(); |
| 363 | if (phoneAccountUserHandle == null) { |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | if (mCurrentUserHandle == null) { |
| 368 | Log.d(this, "Current user is null; assuming true"); |
| 369 | return true; |
| 370 | } |
| 371 | |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 372 | if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) { |
| 373 | return true; |
| 374 | } |
| 375 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 376 | // Special check for work profiles. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 377 | // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure |
| 378 | // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is |
| 379 | // fine. |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 380 | if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) { |
| 381 | List<UserInfo> profileUsers = |
| 382 | mUserManager.getProfiles(mCurrentUserHandle.getIdentifier()); |
| 383 | for (UserInfo profileInfo : profileUsers) { |
| 384 | if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) { |
| 385 | return true; |
| 386 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 389 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 390 | return false; |
| 391 | } |
| 392 | |
| 393 | private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) { |
| 394 | return resolveComponent(phoneAccountHandle.getComponentName(), |
| 395 | phoneAccountHandle.getUserHandle()); |
| 396 | } |
| 397 | |
| 398 | private List<ResolveInfo> resolveComponent(ComponentName componentName, |
| 399 | UserHandle userHandle) { |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 400 | PackageManager pm = mContext.getPackageManager(); |
| 401 | Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE); |
| 402 | intent.setComponent(componentName); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 403 | try { |
| 404 | if (userHandle != null) { |
| 405 | return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier()); |
| 406 | } else { |
| 407 | return pm.queryIntentServices(intent, 0); |
| 408 | } |
| 409 | } catch (SecurityException e) { |
| 410 | Log.v(this, "%s is not visible for the calling user", componentName); |
| 411 | return Collections.EMPTY_LIST; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 412 | } |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 415 | /** |
| 416 | * Retrieves a list of all {@link PhoneAccountHandle}s registered. |
| 417 | * |
| 418 | * @return The list of {@link PhoneAccountHandle}s. |
| 419 | */ |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 420 | public List<PhoneAccountHandle> getAllPhoneAccountHandles() { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 421 | return getPhoneAccountHandles(0, null, null); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | public List<PhoneAccount> getAllPhoneAccounts() { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 425 | return getPhoneAccounts(0, null, null); |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 428 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 429 | * Retrieves a list of all phone account call provider phone accounts supporting the |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 430 | * specified URI scheme. |
| 431 | * |
| 432 | * @param uriScheme The URI scheme. |
| 433 | * @return The phone account handles. |
| 434 | */ |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 435 | public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 436 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme, null); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Retrieves a list of all the SIM-based phone accounts. |
| 441 | */ |
| 442 | public List<PhoneAccountHandle> getSimPhoneAccounts() { |
| 443 | return getPhoneAccountHandles( |
| 444 | PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION, |
| 445 | null, null); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 448 | /** |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 449 | * Retrieves a list of all phone accounts registered by a specified package. |
| 450 | * |
| 451 | * @param packageName The name of the package that registered the phone accounts. |
| 452 | * @return The phone account handles. |
| 453 | */ |
| 454 | public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 455 | return getPhoneAccountHandles(0, null, packageName); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 459 | * Retrieves a list of all phone account handles with the connection manager capability. |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 460 | * |
| 461 | * @return The phone account handles. |
| 462 | */ |
| 463 | public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 464 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 467 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 468 | // ComponentName? |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 469 | public void registerPhoneAccount(PhoneAccount account) { |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 470 | // Enforce the requirement that a connection service for a phone account has the correct |
| 471 | // permission. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 472 | if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) { |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 473 | Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.", |
| 474 | account.getAccountHandle()); |
| 475 | throw new SecurityException( |
| 476 | "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission."); |
| 477 | } |
| 478 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 479 | addOrReplacePhoneAccount(account); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Adds a {@code PhoneAccount}, replacing an existing one if found. |
| 484 | * |
| 485 | * @param account The {@code PhoneAccount} to add or replace. |
| 486 | */ |
| 487 | private void addOrReplacePhoneAccount(PhoneAccount account) { |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 488 | Log.d(this, "addOrReplacePhoneAccount(%s -> %s)", |
| 489 | account.getAccountHandle(), account); |
| 490 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 491 | PhoneAccount oldAccount = getPhoneAccount(account.getAccountHandle()); |
| 492 | if (oldAccount != null) { |
| 493 | mState.accounts.remove(oldAccount); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 494 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 495 | mState.accounts.add(account); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 496 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 497 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 498 | fireAccountsChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 501 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 502 | PhoneAccount account = getPhoneAccount(accountHandle); |
| 503 | if (account != null) { |
| 504 | if (mState.accounts.remove(account)) { |
| 505 | write(); |
| 506 | fireAccountsChanged(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 507 | } |
| 508 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 511 | /** |
| 512 | * Un-registers all phone accounts associated with a specified package. |
| 513 | * |
| 514 | * @param packageName The package for which phone accounts will be removed. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 515 | * @param userHandle The {@link UserHandle} the package is running under. |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 516 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 517 | public void clearAccounts(String packageName, UserHandle userHandle) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 518 | boolean accountsRemoved = false; |
| 519 | Iterator<PhoneAccount> it = mState.accounts.iterator(); |
| 520 | while (it.hasNext()) { |
| 521 | PhoneAccount phoneAccount = it.next(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 522 | PhoneAccountHandle handle = phoneAccount.getAccountHandle(); |
| 523 | if (Objects.equals(packageName, handle.getComponentName().getPackageName()) |
| 524 | && Objects.equals(userHandle, handle.getUserHandle())) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 525 | Log.i(this, "Removing phone account " + phoneAccount.getLabel()); |
| 526 | it.remove(); |
| 527 | accountsRemoved = true; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 531 | if (accountsRemoved) { |
| 532 | write(); |
| 533 | fireAccountsChanged(); |
| 534 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 537 | public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) { |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 538 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 539 | return PhoneNumberUtils.isVoiceMailNumber(subId, number); |
| 540 | } |
| 541 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 542 | public void addListener(Listener l) { |
| 543 | mListeners.add(l); |
| 544 | } |
| 545 | |
| 546 | public void removeListener(Listener l) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 547 | if (l != null) { |
| 548 | mListeners.remove(l); |
| 549 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | private void fireAccountsChanged() { |
| 553 | for (Listener l : mListeners) { |
| 554 | l.onAccountsChanged(this); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | private void fireDefaultOutgoingChanged() { |
| 559 | for (Listener l : mListeners) { |
| 560 | l.onDefaultOutgoingChanged(this); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | private void fireSimCallManagerChanged() { |
| 565 | for (Listener l : mListeners) { |
| 566 | l.onSimCallManagerChanged(this); |
| 567 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 570 | /** |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 571 | * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 572 | * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission. |
| 573 | * |
| 574 | * @param phoneAccountHandle The phone account to check. |
| 575 | * @return {@code True} if the phone account has permission. |
| 576 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 577 | public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) { |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 578 | List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle); |
| 579 | if (resolveInfos.isEmpty()) { |
| 580 | Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName()); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 581 | return false; |
| 582 | } |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 583 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 584 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 585 | if (serviceInfo == null || |
| 586 | !Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission)) { |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 587 | return false; |
| 588 | } |
| 589 | } |
| 590 | return true; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 593 | // |
| 594 | // Methods for retrieving PhoneAccounts and PhoneAccountHandles |
| 595 | // |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 596 | |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 597 | /** |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 598 | * Returns the PhoneAccount for the specified handle. Does no user checking. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 599 | * |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 600 | * @param handle |
| 601 | * @return The corresponding phone account if one exists. |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 602 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 603 | PhoneAccount getPhoneAccount(PhoneAccountHandle handle) { |
| 604 | for (PhoneAccount m : mState.accounts) { |
| 605 | if (Objects.equals(handle, m.getAccountHandle())) { |
| 606 | return m; |
| 607 | } |
| 608 | } |
| 609 | return null; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone |
| 614 | * account before returning it. The current user is the active user on the actual android |
| 615 | * device. |
| 616 | */ |
| 617 | public PhoneAccount getPhoneAccountCheckCallingUser(PhoneAccountHandle handle) { |
| 618 | PhoneAccount account = getPhoneAccount(handle); |
| 619 | if (account != null && isVisibleForUser(account)) { |
| 620 | return account; |
| 621 | } |
| 622 | return null; |
| 623 | } |
| 624 | |
| 625 | /** |
| 626 | * Returns a list of phone account handles with the specified capabilities, uri scheme, |
| 627 | * and package name. |
| 628 | */ |
| 629 | private List<PhoneAccountHandle> getPhoneAccountHandles( |
| 630 | int capabilities, String uriScheme, String packageName) { |
| 631 | List<PhoneAccountHandle> handles = new ArrayList<>(); |
| 632 | for (PhoneAccount account : getPhoneAccounts(capabilities, uriScheme, packageName)) { |
| 633 | handles.add(account.getAccountHandle()); |
| 634 | } |
| 635 | return handles; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Returns a list of phone account handles with the specified flag, supporting the specified |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 640 | * URI scheme, within the specified package name. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 641 | * |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 642 | * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0. |
| 643 | * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 644 | * URI scheme check. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 645 | * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 646 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 647 | private List<PhoneAccount> getPhoneAccounts( |
| 648 | int capabilities, String uriScheme, String packageName) { |
| 649 | List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size()); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 650 | for (PhoneAccount m : mState.accounts) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 651 | if (capabilities != 0 && !m.hasCapabilities(capabilities)) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 652 | // Account doesn't have the right capabilities; skip this one. |
| 653 | continue; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 654 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 655 | if (uriScheme != null && !m.supportsUriScheme(uriScheme)) { |
| 656 | // Account doesn't support this URI scheme; skip this one. |
| 657 | continue; |
| 658 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 659 | PhoneAccountHandle handle = m.getAccountHandle(); |
| 660 | |
| 661 | if (resolveComponent(handle).isEmpty()) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 662 | // This component cannot be resolved anymore; skip this one. |
| 663 | continue; |
| 664 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 665 | if (packageName != null && |
| 666 | !packageName.equals(handle.getComponentName().getPackageName())) { |
| 667 | // Not the right package name; skip this one. |
| 668 | continue; |
| 669 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 670 | if (!isVisibleForUser(m)) { |
| 671 | // Account is not visible for the current user; skip this one. |
| 672 | continue; |
| 673 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 674 | accounts.add(m); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 675 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 676 | return accounts; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 679 | // |
| 680 | // State Implementation for PhoneAccountRegistrar |
| 681 | // |
| 682 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 683 | /** |
| 684 | * The state of this {@code PhoneAccountRegistrar}. |
| 685 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 686 | @VisibleForTesting |
| 687 | public static class State { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 688 | /** |
| 689 | * The account selected by the user to be employed by default for making outgoing calls. |
| 690 | * If the user has not made such a selection, then this is null. |
| 691 | */ |
| 692 | public PhoneAccountHandle defaultOutgoing = null; |
| 693 | |
| 694 | /** |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 695 | * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 696 | * manages and optimizes a user's PSTN SIM connections. |
| 697 | */ |
| 698 | public PhoneAccountHandle simCallManager; |
| 699 | |
| 700 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 701 | * The complete list of {@code PhoneAccount}s known to the Telecom subsystem. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 702 | */ |
| 703 | public final List<PhoneAccount> accounts = new ArrayList<>(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 704 | |
| 705 | /** |
| 706 | * The version number of the State data. |
| 707 | */ |
| 708 | public int versionNumber; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 711 | /** |
| 712 | * Dumps the state of the {@link CallsManager}. |
| 713 | * |
| 714 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 715 | */ |
| 716 | public void dump(IndentingPrintWriter pw) { |
| 717 | if (mState != null) { |
| 718 | pw.println("xmlVersion: " + mState.versionNumber); |
| 719 | pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" : |
| 720 | mState.defaultOutgoing)); |
| 721 | pw.println("simCallManager: " + (mState.simCallManager == null ? "none" : |
| 722 | mState.simCallManager)); |
| 723 | pw.println("phoneAccounts:"); |
| 724 | pw.increaseIndent(); |
| 725 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 726 | pw.println(phoneAccount); |
| 727 | } |
| 728 | pw.decreaseIndent(); |
| 729 | } |
| 730 | } |
| 731 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 732 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 733 | // |
| 734 | // State management |
| 735 | // |
| 736 | |
| 737 | private void write() { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 738 | final FileOutputStream os; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 739 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 740 | os = mAtomicFile.startWrite(); |
| 741 | boolean success = false; |
| 742 | try { |
| 743 | XmlSerializer serializer = new FastXmlSerializer(); |
| 744 | serializer.setOutput(new BufferedOutputStream(os), "utf-8"); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 745 | writeToXml(mState, serializer, mContext); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 746 | serializer.flush(); |
| 747 | success = true; |
| 748 | } finally { |
| 749 | if (success) { |
| 750 | mAtomicFile.finishWrite(os); |
| 751 | } else { |
| 752 | mAtomicFile.failWrite(os); |
| 753 | } |
| 754 | } |
| 755 | } catch (IOException e) { |
| 756 | Log.e(this, e, "Writing state to XML file"); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 757 | } |
| 758 | } |
| 759 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 760 | private void read() { |
| 761 | final InputStream is; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 762 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 763 | is = mAtomicFile.openRead(); |
| 764 | } catch (FileNotFoundException ex) { |
| 765 | return; |
| 766 | } |
| 767 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 768 | boolean versionChanged = false; |
| 769 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 770 | XmlPullParser parser; |
| 771 | try { |
| 772 | parser = Xml.newPullParser(); |
| 773 | parser.setInput(new BufferedInputStream(is), null); |
| 774 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 775 | mState = readFromXml(parser, mContext); |
| 776 | versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION; |
| 777 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 778 | } catch (IOException | XmlPullParserException e) { |
| 779 | Log.e(this, e, "Reading state from XML file"); |
| 780 | mState = new State(); |
| 781 | } finally { |
| 782 | try { |
| 783 | is.close(); |
| 784 | } catch (IOException e) { |
| 785 | Log.e(this, e, "Closing InputStream"); |
| 786 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 787 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 788 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 789 | // Verify all of the UserHandles. |
| 790 | List<PhoneAccount> badAccounts = new ArrayList<>(); |
| 791 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 792 | UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle(); |
| 793 | if (userHandle == null) { |
| 794 | Log.w(this, "Missing UserHandle for %s", phoneAccount); |
| 795 | badAccounts.add(phoneAccount); |
| 796 | } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) { |
| 797 | Log.w(this, "User does not exist for %s", phoneAccount); |
| 798 | badAccounts.add(phoneAccount); |
| 799 | } |
| 800 | } |
| 801 | mState.accounts.removeAll(badAccounts); |
| 802 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 803 | // If an upgrade occurred, write out the changed data. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 804 | if (versionChanged || !badAccounts.isEmpty()) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 805 | write(); |
| 806 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 809 | private static void writeToXml(State state, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 810 | throws IOException { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 811 | sStateXml.writeToXml(state, serializer, context); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 812 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 813 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 814 | private static State readFromXml(XmlPullParser parser, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 815 | throws IOException, XmlPullParserException { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 816 | State s = sStateXml.readFromXml(parser, 0, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 817 | return s != null ? s : new State(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 820 | //////////////////////////////////////////////////////////////////////////////////////////////// |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 821 | // |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 822 | // XML serialization |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 823 | // |
| 824 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 825 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 826 | public abstract static class XmlSerialization<T> { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 827 | private static final String LENGTH_ATTRIBUTE = "length"; |
| 828 | private static final String VALUE_TAG = "value"; |
| 829 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 830 | /** |
| 831 | * Write the supplied object to XML |
| 832 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 833 | public abstract void writeToXml(T o, XmlSerializer serializer, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 834 | throws IOException; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 835 | |
| 836 | /** |
| 837 | * Read from the supplied XML into a new object, returning null in case of an |
| 838 | * unrecoverable schema mismatch or other data error. 'parser' must be already |
| 839 | * positioned at the first tag that is expected to have been emitted by this |
| 840 | * object's writeToXml(). This object tries to fail early without modifying |
| 841 | * 'parser' if it does not recognize the data it sees. |
| 842 | */ |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 843 | public abstract T readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 844 | throws IOException, XmlPullParserException; |
| 845 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 846 | protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 847 | throws IOException { |
| 848 | if (value != null) { |
| 849 | serializer.startTag(null, tagName); |
| 850 | serializer.text(Objects.toString(value)); |
| 851 | serializer.endTag(null, tagName); |
| 852 | } |
| 853 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 854 | |
| 855 | /** |
| 856 | * Serializes a string array. |
| 857 | * |
| 858 | * @param tagName The tag name for the string array. |
| 859 | * @param values The string values to serialize. |
| 860 | * @param serializer The serializer. |
| 861 | * @throws IOException |
| 862 | */ |
| 863 | protected void writeStringList(String tagName, List<String> values, |
| 864 | XmlSerializer serializer) |
| 865 | throws IOException { |
| 866 | |
| 867 | serializer.startTag(null, tagName); |
| 868 | if (values != null) { |
| 869 | serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size())); |
| 870 | for (String toSerialize : values) { |
| 871 | serializer.startTag(null, VALUE_TAG); |
| 872 | if (toSerialize != null ){ |
| 873 | serializer.text(toSerialize); |
| 874 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 875 | serializer.endTag(null, VALUE_TAG); |
| 876 | } |
| 877 | } else { |
| 878 | serializer.attribute(null, LENGTH_ATTRIBUTE, "0"); |
| 879 | } |
| 880 | serializer.endTag(null, tagName); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 881 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 882 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 883 | protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer) |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 884 | throws IOException { |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 885 | if (value != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 886 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 887 | value.writeToStream(stream); |
| 888 | byte[] iconByteArray = stream.toByteArray(); |
| 889 | String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 890 | |
| 891 | serializer.startTag(null, tagName); |
| 892 | serializer.text(text); |
| 893 | serializer.endTag(null, tagName); |
| 894 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 897 | protected void writeLong(String tagName, long value, XmlSerializer serializer) |
| 898 | throws IOException { |
| 899 | serializer.startTag(null, tagName); |
| 900 | serializer.text(Long.valueOf(value).toString()); |
| 901 | serializer.endTag(null, tagName); |
| 902 | } |
| 903 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 904 | /** |
| 905 | * Reads a string array from the XML parser. |
| 906 | * |
| 907 | * @param parser The XML parser. |
| 908 | * @return String array containing the parsed values. |
| 909 | * @throws IOException Exception related to IO. |
| 910 | * @throws XmlPullParserException Exception related to parsing. |
| 911 | */ |
| 912 | protected List<String> readStringList(XmlPullParser parser) |
| 913 | throws IOException, XmlPullParserException { |
| 914 | |
| 915 | int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE)); |
| 916 | List<String> arrayEntries = new ArrayList<String>(length); |
| 917 | String value = null; |
| 918 | |
| 919 | if (length == 0) { |
| 920 | return arrayEntries; |
| 921 | } |
| 922 | |
| 923 | int outerDepth = parser.getDepth(); |
| 924 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 925 | if (parser.getName().equals(VALUE_TAG)) { |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 926 | parser.next(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 927 | value = parser.getText(); |
| 928 | arrayEntries.add(value); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | return arrayEntries; |
| 933 | } |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 934 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 935 | protected Bitmap readBitmap(XmlPullParser parser) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 936 | byte[] imageByteArray = Base64.decode(parser.getText(), 0); |
| 937 | return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length); |
| 938 | } |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 939 | |
| 940 | protected Icon readIcon(XmlPullParser parser) throws IOException { |
| 941 | byte[] iconByteArray = Base64.decode(parser.getText(), 0); |
| 942 | ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray); |
| 943 | return Icon.createFromStream(stream); |
| 944 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 947 | @VisibleForTesting |
| 948 | public static final XmlSerialization<State> sStateXml = |
| 949 | new XmlSerialization<State>() { |
| 950 | private static final String CLASS_STATE = "phone_account_registrar_state"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 951 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 952 | private static final String SIM_CALL_MANAGER = "sim_call_manager"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 953 | private static final String ACCOUNTS = "accounts"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 954 | private static final String VERSION = "version"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 955 | |
| 956 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 957 | public void writeToXml(State o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 958 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 959 | if (o != null) { |
| 960 | serializer.startTag(null, CLASS_STATE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 961 | serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION)); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 962 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 963 | if (o.defaultOutgoing != null) { |
| 964 | serializer.startTag(null, DEFAULT_OUTGOING); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 965 | sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 966 | serializer.endTag(null, DEFAULT_OUTGOING); |
| 967 | } |
| 968 | |
| 969 | if (o.simCallManager != null) { |
| 970 | serializer.startTag(null, SIM_CALL_MANAGER); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 971 | sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 972 | serializer.endTag(null, SIM_CALL_MANAGER); |
| 973 | } |
| 974 | |
| 975 | serializer.startTag(null, ACCOUNTS); |
| 976 | for (PhoneAccount m : o.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 977 | sPhoneAccountXml.writeToXml(m, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 978 | } |
| 979 | serializer.endTag(null, ACCOUNTS); |
| 980 | |
| 981 | serializer.endTag(null, CLASS_STATE); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 982 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 986 | public State readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 987 | throws IOException, XmlPullParserException { |
| 988 | if (parser.getName().equals(CLASS_STATE)) { |
| 989 | State s = new State(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 990 | |
| 991 | String rawVersion = parser.getAttributeValue(null, VERSION); |
| 992 | s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : |
| 993 | Integer.parseInt(rawVersion); |
| 994 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 995 | int outerDepth = parser.getDepth(); |
| 996 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 997 | if (parser.getName().equals(DEFAULT_OUTGOING)) { |
| 998 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 999 | s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser, |
| 1000 | s.versionNumber, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1001 | } else if (parser.getName().equals(SIM_CALL_MANAGER)) { |
| 1002 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1003 | s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser, |
| 1004 | s.versionNumber, context); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1005 | if (s.simCallManager.getUserHandle() == null) { |
| 1006 | // This should never happen, but handle the upgrade case. |
| 1007 | s.simCallManager = new PhoneAccountHandle( |
| 1008 | s.simCallManager.getComponentName(), |
| 1009 | s.simCallManager.getId(), |
| 1010 | Process.myUserHandle()); |
| 1011 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1012 | } else if (parser.getName().equals(ACCOUNTS)) { |
| 1013 | int accountsDepth = parser.getDepth(); |
| 1014 | while (XmlUtils.nextElementWithin(parser, accountsDepth)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1015 | PhoneAccount account = sPhoneAccountXml.readFromXml(parser, |
| 1016 | s.versionNumber, context); |
| 1017 | |
| 1018 | if (account != null && s.accounts != null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1019 | s.accounts.add(account); |
| 1020 | } |
| 1021 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1022 | } |
| 1023 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1024 | return s; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1025 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1026 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1027 | } |
| 1028 | }; |
| 1029 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1030 | @VisibleForTesting |
| 1031 | public static final XmlSerialization<PhoneAccount> sPhoneAccountXml = |
| 1032 | new XmlSerialization<PhoneAccount>() { |
| 1033 | private static final String CLASS_PHONE_ACCOUNT = "phone_account"; |
| 1034 | private static final String ACCOUNT_HANDLE = "account_handle"; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1035 | private static final String ADDRESS = "handle"; |
| 1036 | private static final String SUBSCRIPTION_ADDRESS = "subscription_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1037 | private static final String CAPABILITIES = "capabilities"; |
| 1038 | private static final String ICON_RES_ID = "icon_res_id"; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1039 | private static final String ICON_PACKAGE_NAME = "icon_package_name"; |
| 1040 | private static final String ICON_BITMAP = "icon_bitmap"; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1041 | private static final String ICON_TINT = "icon_tint"; |
| 1042 | private static final String HIGHLIGHT_COLOR = "highlight_color"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1043 | private static final String LABEL = "label"; |
| 1044 | private static final String SHORT_DESCRIPTION = "short_description"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1045 | private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes"; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 1046 | private static final String ICON = "icon"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1047 | |
| 1048 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1049 | public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1050 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1051 | if (o != null) { |
| 1052 | serializer.startTag(null, CLASS_PHONE_ACCOUNT); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1053 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1054 | if (o.getAccountHandle() != null) { |
| 1055 | serializer.startTag(null, ACCOUNT_HANDLE); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1056 | sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1057 | serializer.endTag(null, ACCOUNT_HANDLE); |
| 1058 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1059 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1060 | writeTextIfNonNull(ADDRESS, o.getAddress(), serializer); |
| 1061 | writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer); |
| 1062 | writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 1063 | writeIconIfNonNull(ICON, o.getIcon(), serializer); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1064 | writeTextIfNonNull(HIGHLIGHT_COLOR, |
| 1065 | Integer.toString(o.getHighlightColor()), serializer); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1066 | writeTextIfNonNull(LABEL, o.getLabel(), serializer); |
| 1067 | writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1068 | writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1069 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1070 | serializer.endTag(null, CLASS_PHONE_ACCOUNT); |
| 1071 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1074 | public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1075 | throws IOException, XmlPullParserException { |
| 1076 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) { |
| 1077 | int outerDepth = parser.getDepth(); |
| 1078 | PhoneAccountHandle accountHandle = null; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1079 | Uri address = null; |
| 1080 | Uri subscriptionAddress = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1081 | int capabilities = 0; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1082 | int iconResId = PhoneAccount.NO_RESOURCE_ID; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1083 | String iconPackageName = null; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1084 | Bitmap iconBitmap = null; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1085 | int iconTint = PhoneAccount.NO_ICON_TINT; |
| 1086 | int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1087 | String label = null; |
| 1088 | String shortDescription = null; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1089 | List<String> supportedUriSchemes = null; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 1090 | Icon icon = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1091 | |
| 1092 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1093 | if (parser.getName().equals(ACCOUNT_HANDLE)) { |
| 1094 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1095 | accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version, |
| 1096 | context); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1097 | } else if (parser.getName().equals(ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1098 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1099 | address = Uri.parse(parser.getText()); |
| 1100 | } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1101 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1102 | String nextText = parser.getText(); |
| 1103 | subscriptionAddress = nextText == null ? null : Uri.parse(nextText); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1104 | } else if (parser.getName().equals(CAPABILITIES)) { |
| 1105 | parser.next(); |
| 1106 | capabilities = Integer.parseInt(parser.getText()); |
| 1107 | } else if (parser.getName().equals(ICON_RES_ID)) { |
| 1108 | parser.next(); |
| 1109 | iconResId = Integer.parseInt(parser.getText()); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1110 | } else if (parser.getName().equals(ICON_PACKAGE_NAME)) { |
| 1111 | parser.next(); |
| 1112 | iconPackageName = parser.getText(); |
| 1113 | } else if (parser.getName().equals(ICON_BITMAP)) { |
| 1114 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1115 | iconBitmap = readBitmap(parser); |
| 1116 | } else if (parser.getName().equals(ICON_TINT)) { |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 1117 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1118 | iconTint = Integer.parseInt(parser.getText()); |
| 1119 | } else if (parser.getName().equals(HIGHLIGHT_COLOR)) { |
| 1120 | parser.next(); |
| 1121 | highlightColor = Integer.parseInt(parser.getText()); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1122 | } else if (parser.getName().equals(LABEL)) { |
| 1123 | parser.next(); |
| 1124 | label = parser.getText(); |
| 1125 | } else if (parser.getName().equals(SHORT_DESCRIPTION)) { |
| 1126 | parser.next(); |
| 1127 | shortDescription = parser.getText(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1128 | } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) { |
| 1129 | supportedUriSchemes = readStringList(parser); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 1130 | } else if (parser.getName().equals(ICON)) { |
| 1131 | parser.next(); |
| 1132 | icon = readIcon(parser); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1133 | } |
| 1134 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1135 | |
| 1136 | // Upgrade older phone accounts to specify the supported URI schemes. |
| 1137 | if (version < 2) { |
| 1138 | ComponentName sipComponentName = new ComponentName("com.android.phone", |
| 1139 | "com.android.services.telephony.sip.SipConnectionService"); |
| 1140 | |
| 1141 | supportedUriSchemes = new ArrayList<>(); |
| 1142 | |
| 1143 | // Handle the SIP connection service. |
| 1144 | // Check the system settings to see if it also should handle "tel" calls. |
| 1145 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1146 | boolean useSipForPstn = useSipForPstnCalls(context); |
| 1147 | supportedUriSchemes.add(PhoneAccount.SCHEME_SIP); |
| 1148 | if (useSipForPstn) { |
| 1149 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1150 | } |
| 1151 | } else { |
| 1152 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1153 | supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL); |
| 1154 | } |
| 1155 | } |
| 1156 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1157 | // Upgrade older phone accounts with explicit package name |
| 1158 | if (version < 5) { |
| 1159 | if (iconBitmap == null) { |
| 1160 | iconPackageName = accountHandle.getComponentName().getPackageName(); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1165 | .setAddress(address) |
| 1166 | .setSubscriptionAddress(subscriptionAddress) |
| 1167 | .setCapabilities(capabilities) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1168 | .setShortDescription(shortDescription) |
| 1169 | .setSupportedUriSchemes(supportedUriSchemes) |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1170 | .setHighlightColor(highlightColor); |
| 1171 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 1172 | if (icon != null) { |
| 1173 | builder.setIcon(icon); |
| 1174 | } else if (iconBitmap != null) { |
| 1175 | builder.setIcon(Icon.createWithBitmap(iconBitmap)); |
| 1176 | } else if (!TextUtils.isEmpty(iconPackageName)) { |
| 1177 | builder.setIcon(Icon.createWithResource(iconPackageName, iconResId)); |
| 1178 | // TODO: Need to set tint. |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | return builder.build(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1182 | } |
| 1183 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1184 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1185 | |
| 1186 | /** |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame^] | 1187 | * Determines if the SIP call settings specify to use SIP for all calls, including PSTN |
| 1188 | * calls. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1189 | * |
| 1190 | * @param context The context. |
| 1191 | * @return {@code True} if SIP should be used for all calls. |
| 1192 | */ |
| 1193 | private boolean useSipForPstnCalls(Context context) { |
| 1194 | String option = Settings.System.getString(context.getContentResolver(), |
| 1195 | Settings.System.SIP_CALL_OPTIONS); |
| 1196 | option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY; |
| 1197 | return option.equals(Settings.System.SIP_ALWAYS); |
| 1198 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1199 | }; |
| 1200 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1201 | @VisibleForTesting |
| 1202 | public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml = |
| 1203 | new XmlSerialization<PhoneAccountHandle>() { |
| 1204 | private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1205 | private static final String COMPONENT_NAME = "component_name"; |
| 1206 | private static final String ID = "id"; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1207 | private static final String USER_SERIAL_NUMBER = "user_serial_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1208 | |
| 1209 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1210 | public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1211 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1212 | if (o != null) { |
| 1213 | serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1214 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1215 | if (o.getComponentName() != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1216 | writeTextIfNonNull( |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1217 | COMPONENT_NAME, o.getComponentName().flattenToString(), serializer); |
| 1218 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1219 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1220 | writeTextIfNonNull(ID, o.getId(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1221 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1222 | if (o.getUserHandle() != null && context != null) { |
| 1223 | UserManager userManager = UserManager.get(context); |
| 1224 | writeLong(USER_SERIAL_NUMBER, |
| 1225 | userManager.getSerialNumberForUser(o.getUserHandle()), serializer); |
| 1226 | } |
| 1227 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1228 | serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
| 1229 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1233 | public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1234 | throws IOException, XmlPullParserException { |
| 1235 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) { |
| 1236 | String componentNameString = null; |
| 1237 | String idString = null; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1238 | String userSerialNumberString = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1239 | int outerDepth = parser.getDepth(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1240 | |
| 1241 | UserManager userManager = UserManager.get(context); |
| 1242 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1243 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1244 | if (parser.getName().equals(COMPONENT_NAME)) { |
| 1245 | parser.next(); |
| 1246 | componentNameString = parser.getText(); |
| 1247 | } else if (parser.getName().equals(ID)) { |
| 1248 | parser.next(); |
| 1249 | idString = parser.getText(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1250 | } else if (parser.getName().equals(USER_SERIAL_NUMBER)) { |
| 1251 | parser.next(); |
| 1252 | userSerialNumberString = parser.getText(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1253 | } |
| 1254 | } |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1255 | if (componentNameString != null) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1256 | UserHandle userHandle = null; |
| 1257 | if (userSerialNumberString != null) { |
| 1258 | try { |
| 1259 | long serialNumber = Long.parseLong(userSerialNumberString); |
| 1260 | userHandle = userManager.getUserForSerialNumber(serialNumber); |
| 1261 | } catch (NumberFormatException e) { |
| 1262 | Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString); |
| 1263 | } |
| 1264 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1265 | return new PhoneAccountHandle( |
| 1266 | ComponentName.unflattenFromString(componentNameString), |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1267 | idString, |
| 1268 | userHandle); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1269 | } |
| 1270 | } |
| 1271 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1272 | } |
| 1273 | }; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1274 | } |