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 | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
| 21 | import android.content.pm.PackageManager; |
| 22 | import android.content.pm.ResolveInfo; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 23 | import android.content.pm.ServiceInfo; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 26 | import android.provider.Settings; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 27 | import android.telecom.ConnectionService; |
| 28 | import android.telecom.PhoneAccount; |
| 29 | import android.telecom.PhoneAccountHandle; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 30 | import android.telephony.PhoneNumberUtils; |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 31 | import android.telephony.SubscriptionManager; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 32 | import android.content.ComponentName; |
| 33 | import android.content.Context; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 34 | import android.net.Uri; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 35 | import android.text.TextUtils; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 36 | import android.util.AtomicFile; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 37 | import android.util.Base64; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 38 | import android.util.Xml; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 39 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 40 | // TODO: Needed for move to system service: import com.android.internal.R; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 41 | import com.android.internal.annotations.VisibleForTesting; |
| 42 | import com.android.internal.util.FastXmlSerializer; |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 43 | import com.android.internal.util.IndentingPrintWriter; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 44 | import com.android.internal.util.XmlUtils; |
| 45 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 46 | import org.xmlpull.v1.XmlPullParser; |
| 47 | import org.xmlpull.v1.XmlPullParserException; |
| 48 | import org.xmlpull.v1.XmlSerializer; |
| 49 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 50 | import java.io.BufferedInputStream; |
| 51 | import java.io.BufferedOutputStream; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 52 | import java.io.ByteArrayOutputStream; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 53 | import java.io.File; |
| 54 | import java.io.FileNotFoundException; |
| 55 | import java.io.FileOutputStream; |
| 56 | import java.io.IOException; |
| 57 | import java.io.InputStream; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 58 | import java.lang.Integer; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 59 | import java.lang.SecurityException; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 60 | import java.lang.String; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 61 | import java.util.ArrayList; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 62 | import java.util.Iterator; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 63 | import java.util.List; |
| 64 | import java.util.Objects; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 65 | import java.util.concurrent.CopyOnWriteArrayList; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 68 | * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 69 | * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as implemented in |
| 70 | * {@link TelecomServiceImpl}, with the notable exception that {@link TelecomServiceImpl} is |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 71 | * responsible for security checking to make sure that the caller has proper authority over |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 72 | * the {@code ComponentName}s they are declaring in their {@code PhoneAccountHandle}s. |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 73 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 74 | public final class PhoneAccountRegistrar { |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 75 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 76 | public static final PhoneAccountHandle NO_ACCOUNT_SELECTED = |
| 77 | new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED"); |
| 78 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 79 | public abstract static class Listener { |
| 80 | public void onAccountsChanged(PhoneAccountRegistrar registrar) {} |
| 81 | public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {} |
| 82 | public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {} |
| 83 | } |
| 84 | |
| 85 | private static final String FILE_NAME = "phone-account-registrar-state.xml"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 86 | @VisibleForTesting |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 87 | public static final int EXPECTED_STATE_VERSION = 4; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 88 | |
| 89 | /** Keep in sync with the same in SipSettings.java */ |
| 90 | private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES"; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 91 | |
| 92 | private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); |
| 93 | private final AtomicFile mAtomicFile; |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 94 | private final Context mContext; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 95 | private State mState; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 96 | |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 97 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 98 | public PhoneAccountRegistrar(Context context) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 99 | this(context, FILE_NAME); |
| 100 | } |
| 101 | |
| 102 | @VisibleForTesting |
| 103 | public PhoneAccountRegistrar(Context context, String fileName) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 104 | // TODO: This file path is subject to change -- it is storing the phone account registry |
| 105 | // state file in the path /data/system/users/0/, which is likely not correct in a |
| 106 | // multi-user setting. |
| 107 | /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE |
| 108 | String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()). |
| 109 | getAbsolutePath(); |
| 110 | mAtomicFile = new AtomicFile(new File(filePath, fileName)); |
| 111 | UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 112 | mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName)); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 113 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 114 | mState = new State(); |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 115 | mContext = context; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 116 | read(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 119 | /** |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 120 | * Retrieves the subscription id for a given phone account if it exists. Subscription ids |
| 121 | * apply only to PSTN/SIM card phone accounts so all other accounts should not have a |
| 122 | * subscription id. |
| 123 | * @param accountHandle The handle for the phone account for which to retrieve the |
| 124 | * subscription id. |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 125 | * @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] | 126 | */ |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 127 | public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) { |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 128 | PhoneAccount account = getPhoneAccount(accountHandle); |
| 129 | if (account == null || !account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) || |
| 130 | !TextUtils.isDigitsOnly(accountHandle.getId())) { |
| 131 | // Since no decimals or negative numbers can be valid subscription ids, only a string of |
| 132 | // numbers can be subscription id |
| 133 | return -1; |
| 134 | } |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 135 | return Integer.parseInt(accountHandle.getId()); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 139 | * Retrieves the default outgoing phone account supporting the specified uriScheme. |
| 140 | * @param uriScheme The URI scheme for the outgoing call. |
| 141 | * @return The {@link PhoneAccountHandle} to use. |
| 142 | */ |
| 143 | public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 144 | final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 145 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 146 | if (userSelected != null) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 147 | // If there is a default PhoneAccount, ensure it supports calls to handles with the |
| 148 | // specified uriScheme. |
| 149 | final PhoneAccount userSelectedAccount = getPhoneAccount(userSelected); |
| 150 | if (userSelectedAccount.supportsUriScheme(uriScheme)) { |
| 151 | return userSelected; |
| 152 | } |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 155 | List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 156 | switch (outgoing.size()) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 157 | case 0: |
| 158 | // There are no accounts, so there can be no default |
| 159 | return null; |
| 160 | case 1: |
| 161 | // There is only one account, which is by definition the default |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 162 | return outgoing.get(0); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 163 | default: |
| 164 | // There are multiple accounts with no selected default |
| 165 | return null; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 166 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 167 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 168 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 169 | PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() { |
| 170 | if (mState.defaultOutgoing != null) { |
| 171 | // Return the registered outgoing default iff it still exists (we keep a sticky |
| 172 | // default to survive account deletion and re-addition) |
| 173 | for (int i = 0; i < mState.accounts.size(); i++) { |
| 174 | if (mState.accounts.get(i).getAccountHandle().equals(mState.defaultOutgoing)) { |
| 175 | return mState.defaultOutgoing; |
| 176 | } |
| 177 | } |
| 178 | // At this point, there was a registered default but it has been deleted; proceed |
| 179 | // as though there were no default |
| 180 | } |
| 181 | return null; |
| 182 | } |
| 183 | |
Andrew Lee | a51a386 | 2014-09-03 14:58:45 -0700 | [diff] [blame] | 184 | public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 185 | if (accountHandle == null) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 186 | // Asking to clear the default outgoing is a valid request |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 187 | mState.defaultOutgoing = null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 188 | } else { |
| 189 | boolean found = false; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 190 | for (PhoneAccount m : mState.accounts) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 191 | if (Objects.equals(accountHandle, m.getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 192 | found = true; |
| 193 | break; |
| 194 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 195 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 196 | |
| 197 | if (!found) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 198 | Log.w(this, "Trying to set nonexistent default outgoing %s", |
| 199 | accountHandle); |
| 200 | return; |
| 201 | } |
| 202 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 203 | if (!getPhoneAccount(accountHandle).hasCapabilities( |
| 204 | PhoneAccount.CAPABILITY_CALL_PROVIDER)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 205 | Log.w(this, "Trying to set non-call-provider default outgoing %s", |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 206 | accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 210 | if (getPhoneAccount(accountHandle).hasCapabilities( |
| 211 | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
| 212 | // If the account selected is a SIM account, propagate down to the subscription |
| 213 | // record. |
Wink Saville | 7ce6e78 | 2014-10-27 10:56:46 -0700 | [diff] [blame] | 214 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 215 | SubscriptionManager.setDefaultVoiceSubId(subId); |
| 216 | } |
| 217 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 218 | mState.defaultOutgoing = accountHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 221 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 222 | fireDefaultOutgoingChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 225 | public void setSimCallManager(PhoneAccountHandle callManager) { |
| 226 | if (callManager != null) { |
| 227 | PhoneAccount callManagerAccount = getPhoneAccount(callManager); |
| 228 | if (callManagerAccount == null) { |
| 229 | Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager); |
| 230 | return; |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 231 | } else if (!callManagerAccount.hasCapabilities( |
| 232 | PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 233 | Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount); |
| 234 | return; |
| 235 | } |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 236 | } else { |
| 237 | callManager = NO_ACCOUNT_SELECTED; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 238 | } |
| 239 | mState.simCallManager = callManager; |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 240 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 241 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 242 | fireSimCallManagerChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | public PhoneAccountHandle getSimCallManager() { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 246 | if (mState.simCallManager != null) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 247 | if (NO_ACCOUNT_SELECTED.equals(mState.simCallManager)) { |
| 248 | return null; |
| 249 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 250 | // Return the registered sim call manager iff it still exists (we keep a sticky |
| 251 | // setting to survive account deletion and re-addition) |
| 252 | for (int i = 0; i < mState.accounts.size(); i++) { |
| 253 | if (mState.accounts.get(i).getAccountHandle().equals(mState.simCallManager)) { |
| 254 | return mState.simCallManager; |
| 255 | } |
| 256 | } |
| 257 | } |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 258 | |
| 259 | // See if the OEM has specified a default one. |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 260 | String defaultConnectionMgr = |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 261 | mContext.getResources().getString(R.string.default_connection_manager_component); |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 262 | if (!TextUtils.isEmpty(defaultConnectionMgr)) { |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 263 | PackageManager pm = mContext.getPackageManager(); |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 264 | |
| 265 | ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr); |
| 266 | Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE); |
| 267 | intent.setComponent(componentName); |
| 268 | |
| 269 | // Make sure that the component can be resolved. |
| 270 | List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent, 0); |
| 271 | if (!resolveInfos.isEmpty()) { |
| 272 | // See if there is registered PhoneAccount by this component. |
| 273 | List<PhoneAccountHandle> handles = getAllPhoneAccountHandles(); |
| 274 | for (PhoneAccountHandle handle : handles) { |
| 275 | if (componentName.equals(handle.getComponentName())) { |
| 276 | return handle; |
| 277 | } |
| 278 | } |
| 279 | Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName); |
| 280 | } else { |
| 281 | Log.d(this, "%s could not be resolved; not using as default", componentName); |
| 282 | } |
| 283 | } else { |
| 284 | Log.v(this, "No default connection manager specified"); |
| 285 | } |
| 286 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 287 | return null; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 290 | /** |
| 291 | * Retrieves a list of all {@link PhoneAccountHandle}s registered. |
| 292 | * |
| 293 | * @return The list of {@link PhoneAccountHandle}s. |
| 294 | */ |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 295 | public List<PhoneAccountHandle> getAllPhoneAccountHandles() { |
| 296 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
| 297 | for (PhoneAccount m : mState.accounts) { |
| 298 | accountHandles.add(m.getAccountHandle()); |
| 299 | } |
| 300 | return accountHandles; |
| 301 | } |
| 302 | |
| 303 | public List<PhoneAccount> getAllPhoneAccounts() { |
| 304 | return new ArrayList<>(mState.accounts); |
| 305 | } |
| 306 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 307 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 308 | * Determines the number of all {@link PhoneAccount}s. |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 309 | * |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 310 | * @return The total number {@link PhoneAccount}s. |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 311 | */ |
| 312 | public int getAllPhoneAccountsCount() { |
| 313 | return mState.accounts.size(); |
| 314 | } |
| 315 | |
| 316 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 317 | * Retrieves a list of all call provider phone accounts. |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 318 | * |
| 319 | * @return The phone account handles. |
| 320 | */ |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 321 | public List<PhoneAccountHandle> getCallCapablePhoneAccounts() { |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 322 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER); |
| 323 | } |
| 324 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 325 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 326 | * 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] | 327 | * specified URI scheme. |
| 328 | * |
| 329 | * @param uriScheme The URI scheme. |
| 330 | * @return The phone account handles. |
| 331 | */ |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 332 | public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) { |
| 333 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 336 | /** |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 337 | * Retrieves a list of all phone accounts registered by a specified package. |
| 338 | * |
| 339 | * @param packageName The name of the package that registered the phone accounts. |
| 340 | * @return The phone account handles. |
| 341 | */ |
| 342 | public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) { |
| 343 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
| 344 | for (PhoneAccount m : mState.accounts) { |
| 345 | if (Objects.equals( |
| 346 | packageName, |
| 347 | m.getAccountHandle().getComponentName().getPackageName())) { |
| 348 | accountHandles.add(m.getAccountHandle()); |
| 349 | } |
| 350 | } |
| 351 | return accountHandles; |
| 352 | } |
| 353 | |
| 354 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 355 | * 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] | 356 | * |
| 357 | * @return The phone account handles. |
| 358 | */ |
| 359 | public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() { |
Santos Cordon | e3d8245 | 2014-09-15 13:44:29 -0700 | [diff] [blame] | 360 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER, |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 361 | null /* supportedUriScheme */); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 364 | public PhoneAccount getPhoneAccount(PhoneAccountHandle handle) { |
| 365 | for (PhoneAccount m : mState.accounts) { |
| 366 | if (Objects.equals(handle, m.getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 367 | return m; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | return null; |
| 371 | } |
| 372 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 373 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 374 | // ComponentName? |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 375 | public void registerPhoneAccount(PhoneAccount account) { |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 376 | // Enforce the requirement that a connection service for a phone account has the correct |
| 377 | // permission. |
| 378 | if (!phoneAccountHasPermission(account.getAccountHandle())) { |
| 379 | Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.", |
| 380 | account.getAccountHandle()); |
| 381 | throw new SecurityException( |
| 382 | "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission."); |
| 383 | } |
| 384 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 385 | addOrReplacePhoneAccount(account); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Adds a {@code PhoneAccount}, replacing an existing one if found. |
| 390 | * |
| 391 | * @param account The {@code PhoneAccount} to add or replace. |
| 392 | */ |
| 393 | private void addOrReplacePhoneAccount(PhoneAccount account) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 394 | mState.accounts.add(account); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 395 | // Search for duplicates and remove any that are found. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 396 | for (int i = 0; i < mState.accounts.size() - 1; i++) { |
| 397 | if (Objects.equals( |
| 398 | account.getAccountHandle(), mState.accounts.get(i).getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 399 | // replace existing entry. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 400 | mState.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 401 | break; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 405 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 406 | fireAccountsChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 409 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 410 | for (int i = 0; i < mState.accounts.size(); i++) { |
| 411 | if (Objects.equals(accountHandle, mState.accounts.get(i).getAccountHandle())) { |
| 412 | mState.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 417 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 418 | fireAccountsChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 421 | /** |
| 422 | * Un-registers all phone accounts associated with a specified package. |
| 423 | * |
| 424 | * @param packageName The package for which phone accounts will be removed. |
| 425 | */ |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 426 | public void clearAccounts(String packageName) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 427 | boolean accountsRemoved = false; |
| 428 | Iterator<PhoneAccount> it = mState.accounts.iterator(); |
| 429 | while (it.hasNext()) { |
| 430 | PhoneAccount phoneAccount = it.next(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 431 | if (Objects.equals( |
| 432 | packageName, |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 433 | phoneAccount.getAccountHandle().getComponentName().getPackageName())) { |
| 434 | Log.i(this, "Removing phone account " + phoneAccount.getLabel()); |
| 435 | it.remove(); |
| 436 | accountsRemoved = true; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 440 | if (accountsRemoved) { |
| 441 | write(); |
| 442 | fireAccountsChanged(); |
| 443 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 446 | public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) { |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 447 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 448 | return PhoneNumberUtils.isVoiceMailNumber(subId, number); |
| 449 | } |
| 450 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 451 | public void addListener(Listener l) { |
| 452 | mListeners.add(l); |
| 453 | } |
| 454 | |
| 455 | public void removeListener(Listener l) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 456 | if (l != null) { |
| 457 | mListeners.remove(l); |
| 458 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | private void fireAccountsChanged() { |
| 462 | for (Listener l : mListeners) { |
| 463 | l.onAccountsChanged(this); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | private void fireDefaultOutgoingChanged() { |
| 468 | for (Listener l : mListeners) { |
| 469 | l.onDefaultOutgoingChanged(this); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | private void fireSimCallManagerChanged() { |
| 474 | for (Listener l : mListeners) { |
| 475 | l.onSimCallManagerChanged(this); |
| 476 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 479 | /** |
| 480 | * Determines if the connection service specified by a {@link PhoneAccountHandle} has the |
| 481 | * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission. |
| 482 | * |
| 483 | * @param phoneAccountHandle The phone account to check. |
| 484 | * @return {@code True} if the phone account has permission. |
| 485 | */ |
| 486 | public boolean phoneAccountHasPermission(PhoneAccountHandle phoneAccountHandle) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 487 | PackageManager packageManager = mContext.getPackageManager(); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 488 | try { |
| 489 | ServiceInfo serviceInfo = packageManager.getServiceInfo( |
| 490 | phoneAccountHandle.getComponentName(), 0); |
| 491 | |
| 492 | return serviceInfo.permission != null && |
| 493 | serviceInfo.permission.equals(Manifest.permission.BIND_CONNECTION_SERVICE); |
| 494 | } catch (PackageManager.NameNotFoundException e) { |
| 495 | Log.w(this, "Name not found %s", e); |
| 496 | return false; |
| 497 | } |
| 498 | } |
| 499 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 500 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 501 | |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 502 | /** |
| 503 | * Returns a list of phone account handles with the specified flag. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 504 | * |
| 505 | * @param flags Flags which the {@code PhoneAccount} must have. |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 506 | */ |
| 507 | private List<PhoneAccountHandle> getPhoneAccountHandles(int flags) { |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 508 | return getPhoneAccountHandles(flags, null); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Returns a list of phone account handles with the specified flag, supporting the specified |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 513 | * URI scheme. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 514 | * |
| 515 | * @param flags Flags which the {@code PhoneAccount} must have. |
| 516 | * @param uriScheme URI schemes the PhoneAccount must handle. {@code Null} bypasses the |
| 517 | * URI scheme check. |
| 518 | */ |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 519 | private List<PhoneAccountHandle> getPhoneAccountHandles(int flags, String uriScheme) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 520 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 521 | for (PhoneAccount m : mState.accounts) { |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 522 | if (m.hasCapabilities(flags) && (uriScheme == null || m.supportsUriScheme(uriScheme))) { |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 523 | accountHandles.add(m.getAccountHandle()); |
| 524 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 525 | } |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 526 | return accountHandles; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 529 | /** |
| 530 | * The state of this {@code PhoneAccountRegistrar}. |
| 531 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 532 | @VisibleForTesting |
| 533 | public static class State { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 534 | /** |
| 535 | * The account selected by the user to be employed by default for making outgoing calls. |
| 536 | * If the user has not made such a selection, then this is null. |
| 537 | */ |
| 538 | public PhoneAccountHandle defaultOutgoing = null; |
| 539 | |
| 540 | /** |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 541 | * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 542 | * manages and optimizes a user's PSTN SIM connections. |
| 543 | */ |
| 544 | public PhoneAccountHandle simCallManager; |
| 545 | |
| 546 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 547 | * The complete list of {@code PhoneAccount}s known to the Telecom subsystem. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 548 | */ |
| 549 | public final List<PhoneAccount> accounts = new ArrayList<>(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 550 | |
| 551 | /** |
| 552 | * The version number of the State data. |
| 553 | */ |
| 554 | public int versionNumber; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 557 | /** |
| 558 | * Dumps the state of the {@link CallsManager}. |
| 559 | * |
| 560 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 561 | */ |
| 562 | public void dump(IndentingPrintWriter pw) { |
| 563 | if (mState != null) { |
| 564 | pw.println("xmlVersion: " + mState.versionNumber); |
| 565 | pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" : |
| 566 | mState.defaultOutgoing)); |
| 567 | pw.println("simCallManager: " + (mState.simCallManager == null ? "none" : |
| 568 | mState.simCallManager)); |
| 569 | pw.println("phoneAccounts:"); |
| 570 | pw.increaseIndent(); |
| 571 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 572 | pw.println(phoneAccount); |
| 573 | } |
| 574 | pw.decreaseIndent(); |
| 575 | } |
| 576 | } |
| 577 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 578 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 579 | // |
| 580 | // State management |
| 581 | // |
| 582 | |
| 583 | private void write() { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 584 | final FileOutputStream os; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 585 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 586 | os = mAtomicFile.startWrite(); |
| 587 | boolean success = false; |
| 588 | try { |
| 589 | XmlSerializer serializer = new FastXmlSerializer(); |
| 590 | serializer.setOutput(new BufferedOutputStream(os), "utf-8"); |
| 591 | writeToXml(mState, serializer); |
| 592 | serializer.flush(); |
| 593 | success = true; |
| 594 | } finally { |
| 595 | if (success) { |
| 596 | mAtomicFile.finishWrite(os); |
| 597 | } else { |
| 598 | mAtomicFile.failWrite(os); |
| 599 | } |
| 600 | } |
| 601 | } catch (IOException e) { |
| 602 | Log.e(this, e, "Writing state to XML file"); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 606 | private void read() { |
| 607 | final InputStream is; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 608 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 609 | is = mAtomicFile.openRead(); |
| 610 | } catch (FileNotFoundException ex) { |
| 611 | return; |
| 612 | } |
| 613 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 614 | boolean versionChanged = false; |
| 615 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 616 | XmlPullParser parser; |
| 617 | try { |
| 618 | parser = Xml.newPullParser(); |
| 619 | parser.setInput(new BufferedInputStream(is), null); |
| 620 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 621 | mState = readFromXml(parser, mContext); |
| 622 | versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION; |
| 623 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 624 | } catch (IOException | XmlPullParserException e) { |
| 625 | Log.e(this, e, "Reading state from XML file"); |
| 626 | mState = new State(); |
| 627 | } finally { |
| 628 | try { |
| 629 | is.close(); |
| 630 | } catch (IOException e) { |
| 631 | Log.e(this, e, "Closing InputStream"); |
| 632 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 633 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 634 | |
| 635 | // If an upgrade occurred, write out the changed data. |
| 636 | if (versionChanged) { |
| 637 | write(); |
| 638 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 641 | private static void writeToXml(State state, XmlSerializer serializer) |
| 642 | throws IOException { |
| 643 | sStateXml.writeToXml(state, serializer); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 644 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 645 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 646 | private static State readFromXml(XmlPullParser parser, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 647 | throws IOException, XmlPullParserException { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 648 | State s = sStateXml.readFromXml(parser, 0, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 649 | return s != null ? s : new State(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 652 | //////////////////////////////////////////////////////////////////////////////////////////////// |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 653 | // |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 654 | // XML serialization |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 655 | // |
| 656 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 657 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 658 | public abstract static class XmlSerialization<T> { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 659 | private static final String LENGTH_ATTRIBUTE = "length"; |
| 660 | private static final String VALUE_TAG = "value"; |
| 661 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 662 | /** |
| 663 | * Write the supplied object to XML |
| 664 | */ |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 665 | public abstract void writeToXml(T o, XmlSerializer serializer) |
| 666 | throws IOException; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 667 | |
| 668 | /** |
| 669 | * Read from the supplied XML into a new object, returning null in case of an |
| 670 | * unrecoverable schema mismatch or other data error. 'parser' must be already |
| 671 | * positioned at the first tag that is expected to have been emitted by this |
| 672 | * object's writeToXml(). This object tries to fail early without modifying |
| 673 | * 'parser' if it does not recognize the data it sees. |
| 674 | */ |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 675 | public abstract T readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 676 | throws IOException, XmlPullParserException; |
| 677 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 678 | protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 679 | throws IOException { |
| 680 | if (value != null) { |
| 681 | serializer.startTag(null, tagName); |
| 682 | serializer.text(Objects.toString(value)); |
| 683 | serializer.endTag(null, tagName); |
| 684 | } |
| 685 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 686 | |
| 687 | /** |
| 688 | * Serializes a string array. |
| 689 | * |
| 690 | * @param tagName The tag name for the string array. |
| 691 | * @param values The string values to serialize. |
| 692 | * @param serializer The serializer. |
| 693 | * @throws IOException |
| 694 | */ |
| 695 | protected void writeStringList(String tagName, List<String> values, |
| 696 | XmlSerializer serializer) |
| 697 | throws IOException { |
| 698 | |
| 699 | serializer.startTag(null, tagName); |
| 700 | if (values != null) { |
| 701 | serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size())); |
| 702 | for (String toSerialize : values) { |
| 703 | serializer.startTag(null, VALUE_TAG); |
| 704 | if (toSerialize != null ){ |
| 705 | serializer.text(toSerialize); |
| 706 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 707 | serializer.endTag(null, VALUE_TAG); |
| 708 | } |
| 709 | } else { |
| 710 | serializer.attribute(null, LENGTH_ATTRIBUTE, "0"); |
| 711 | } |
| 712 | serializer.endTag(null, tagName); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 713 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 714 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 715 | protected void writeBitmapIfNonNull(String tagName, Bitmap value, XmlSerializer serializer) |
| 716 | throws IOException { |
| 717 | if (value != null && value.getByteCount() > 0) { |
| 718 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 719 | value.compress(Bitmap.CompressFormat.PNG, 100, stream); |
| 720 | byte[] imageByteArray = stream.toByteArray(); |
| 721 | String text = Base64.encodeToString(imageByteArray, 0, imageByteArray.length, 0); |
| 722 | |
| 723 | serializer.startTag(null, tagName); |
| 724 | serializer.text(text); |
| 725 | serializer.endTag(null, tagName); |
| 726 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /** |
| 730 | * Reads a string array from the XML parser. |
| 731 | * |
| 732 | * @param parser The XML parser. |
| 733 | * @return String array containing the parsed values. |
| 734 | * @throws IOException Exception related to IO. |
| 735 | * @throws XmlPullParserException Exception related to parsing. |
| 736 | */ |
| 737 | protected List<String> readStringList(XmlPullParser parser) |
| 738 | throws IOException, XmlPullParserException { |
| 739 | |
| 740 | int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE)); |
| 741 | List<String> arrayEntries = new ArrayList<String>(length); |
| 742 | String value = null; |
| 743 | |
| 744 | if (length == 0) { |
| 745 | return arrayEntries; |
| 746 | } |
| 747 | |
| 748 | int outerDepth = parser.getDepth(); |
| 749 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 750 | if (parser.getName().equals(VALUE_TAG)) { |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 751 | parser.next(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 752 | value = parser.getText(); |
| 753 | arrayEntries.add(value); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | return arrayEntries; |
| 758 | } |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 759 | |
| 760 | protected Bitmap readBitmap(XmlPullParser parser) |
| 761 | throws IOException, XmlPullParserException { |
| 762 | byte[] imageByteArray = Base64.decode(parser.getText(), 0); |
| 763 | return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length); |
| 764 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 767 | @VisibleForTesting |
| 768 | public static final XmlSerialization<State> sStateXml = |
| 769 | new XmlSerialization<State>() { |
| 770 | private static final String CLASS_STATE = "phone_account_registrar_state"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 771 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 772 | private static final String SIM_CALL_MANAGER = "sim_call_manager"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 773 | private static final String ACCOUNTS = "accounts"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 774 | private static final String VERSION = "version"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 775 | |
| 776 | @Override |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 777 | public void writeToXml(State o, XmlSerializer serializer) |
| 778 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 779 | if (o != null) { |
| 780 | serializer.startTag(null, CLASS_STATE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 781 | serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION)); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 782 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 783 | if (o.defaultOutgoing != null) { |
| 784 | serializer.startTag(null, DEFAULT_OUTGOING); |
| 785 | sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer); |
| 786 | serializer.endTag(null, DEFAULT_OUTGOING); |
| 787 | } |
| 788 | |
| 789 | if (o.simCallManager != null) { |
| 790 | serializer.startTag(null, SIM_CALL_MANAGER); |
| 791 | sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer); |
| 792 | serializer.endTag(null, SIM_CALL_MANAGER); |
| 793 | } |
| 794 | |
| 795 | serializer.startTag(null, ACCOUNTS); |
| 796 | for (PhoneAccount m : o.accounts) { |
| 797 | sPhoneAccountXml.writeToXml(m, serializer); |
| 798 | } |
| 799 | serializer.endTag(null, ACCOUNTS); |
| 800 | |
| 801 | serializer.endTag(null, CLASS_STATE); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 802 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 806 | public State readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 807 | throws IOException, XmlPullParserException { |
| 808 | if (parser.getName().equals(CLASS_STATE)) { |
| 809 | State s = new State(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 810 | |
| 811 | String rawVersion = parser.getAttributeValue(null, VERSION); |
| 812 | s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : |
| 813 | Integer.parseInt(rawVersion); |
| 814 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 815 | int outerDepth = parser.getDepth(); |
| 816 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 817 | if (parser.getName().equals(DEFAULT_OUTGOING)) { |
| 818 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 819 | s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser, |
| 820 | s.versionNumber, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 821 | } else if (parser.getName().equals(SIM_CALL_MANAGER)) { |
| 822 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 823 | s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser, |
| 824 | s.versionNumber, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 825 | } else if (parser.getName().equals(ACCOUNTS)) { |
| 826 | int accountsDepth = parser.getDepth(); |
| 827 | while (XmlUtils.nextElementWithin(parser, accountsDepth)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 828 | PhoneAccount account = sPhoneAccountXml.readFromXml(parser, |
| 829 | s.versionNumber, context); |
| 830 | |
| 831 | if (account != null && s.accounts != null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 832 | s.accounts.add(account); |
| 833 | } |
| 834 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 835 | } |
| 836 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 837 | return s; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 838 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 839 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 840 | } |
| 841 | }; |
| 842 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 843 | @VisibleForTesting |
| 844 | public static final XmlSerialization<PhoneAccount> sPhoneAccountXml = |
| 845 | new XmlSerialization<PhoneAccount>() { |
| 846 | private static final String CLASS_PHONE_ACCOUNT = "phone_account"; |
| 847 | private static final String ACCOUNT_HANDLE = "account_handle"; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 848 | private static final String ADDRESS = "handle"; |
| 849 | private static final String SUBSCRIPTION_ADDRESS = "subscription_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 850 | private static final String CAPABILITIES = "capabilities"; |
| 851 | private static final String ICON_RES_ID = "icon_res_id"; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 852 | private static final String ICON_PACKAGE_NAME = "icon_package_name"; |
| 853 | private static final String ICON_BITMAP = "icon_bitmap"; |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 854 | private static final String COLOR = "color"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 855 | private static final String LABEL = "label"; |
| 856 | private static final String SHORT_DESCRIPTION = "short_description"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 857 | private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 858 | |
| 859 | @Override |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 860 | public void writeToXml(PhoneAccount o, XmlSerializer serializer) |
| 861 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 862 | if (o != null) { |
| 863 | serializer.startTag(null, CLASS_PHONE_ACCOUNT); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 864 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 865 | if (o.getAccountHandle() != null) { |
| 866 | serializer.startTag(null, ACCOUNT_HANDLE); |
| 867 | sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer); |
| 868 | serializer.endTag(null, ACCOUNT_HANDLE); |
| 869 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 870 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 871 | writeTextIfNonNull(ADDRESS, o.getAddress(), serializer); |
| 872 | writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer); |
| 873 | writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer); |
| 874 | writeTextIfNonNull(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer); |
| 875 | writeTextIfNonNull(ICON_PACKAGE_NAME, o.getIconPackageName(), serializer); |
| 876 | writeBitmapIfNonNull(ICON_BITMAP, o.getIconBitmap(), serializer); |
| 877 | writeTextIfNonNull(COLOR, Integer.toString(o.getColor()), serializer); |
| 878 | writeTextIfNonNull(LABEL, o.getLabel(), serializer); |
| 879 | writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 880 | writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 881 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 882 | serializer.endTag(null, CLASS_PHONE_ACCOUNT); |
| 883 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 886 | public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 887 | throws IOException, XmlPullParserException { |
| 888 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) { |
| 889 | int outerDepth = parser.getDepth(); |
| 890 | PhoneAccountHandle accountHandle = null; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 891 | Uri address = null; |
| 892 | Uri subscriptionAddress = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 893 | int capabilities = 0; |
| 894 | int iconResId = 0; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 895 | String iconPackageName = null; |
| 896 | Bitmap icon = null; |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 897 | int color = 0; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 898 | String label = null; |
| 899 | String shortDescription = null; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 900 | List<String> supportedUriSchemes = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 901 | |
| 902 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 903 | if (parser.getName().equals(ACCOUNT_HANDLE)) { |
| 904 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 905 | accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version, |
| 906 | context); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 907 | } else if (parser.getName().equals(ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 908 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 909 | address = Uri.parse(parser.getText()); |
| 910 | } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 911 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 912 | String nextText = parser.getText(); |
| 913 | subscriptionAddress = nextText == null ? null : Uri.parse(nextText); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 914 | } else if (parser.getName().equals(CAPABILITIES)) { |
| 915 | parser.next(); |
| 916 | capabilities = Integer.parseInt(parser.getText()); |
| 917 | } else if (parser.getName().equals(ICON_RES_ID)) { |
| 918 | parser.next(); |
| 919 | iconResId = Integer.parseInt(parser.getText()); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 920 | } else if (parser.getName().equals(ICON_PACKAGE_NAME)) { |
| 921 | parser.next(); |
| 922 | iconPackageName = parser.getText(); |
| 923 | } else if (parser.getName().equals(ICON_BITMAP)) { |
| 924 | parser.next(); |
| 925 | icon = readBitmap(parser); |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 926 | } else if (parser.getName().equals(COLOR)) { |
| 927 | parser.next(); |
| 928 | color = Integer.parseInt(parser.getText()); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 929 | } else if (parser.getName().equals(LABEL)) { |
| 930 | parser.next(); |
| 931 | label = parser.getText(); |
| 932 | } else if (parser.getName().equals(SHORT_DESCRIPTION)) { |
| 933 | parser.next(); |
| 934 | shortDescription = parser.getText(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 935 | } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) { |
| 936 | supportedUriSchemes = readStringList(parser); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 937 | } |
| 938 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 939 | |
| 940 | // Upgrade older phone accounts to specify the supported URI schemes. |
| 941 | if (version < 2) { |
| 942 | ComponentName sipComponentName = new ComponentName("com.android.phone", |
| 943 | "com.android.services.telephony.sip.SipConnectionService"); |
| 944 | |
| 945 | supportedUriSchemes = new ArrayList<>(); |
| 946 | |
| 947 | // Handle the SIP connection service. |
| 948 | // Check the system settings to see if it also should handle "tel" calls. |
| 949 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 950 | boolean useSipForPstn = useSipForPstnCalls(context); |
| 951 | supportedUriSchemes.add(PhoneAccount.SCHEME_SIP); |
| 952 | if (useSipForPstn) { |
| 953 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 954 | } |
| 955 | } else { |
| 956 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 957 | supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL); |
| 958 | } |
| 959 | } |
| 960 | |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 961 | return PhoneAccount.builder(accountHandle, label) |
| 962 | .setAddress(address) |
| 963 | .setSubscriptionAddress(subscriptionAddress) |
| 964 | .setCapabilities(capabilities) |
| 965 | .setIconResId(iconResId) |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 966 | .setIconPackageName(iconPackageName) |
| 967 | .setIconBitmap(icon) |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 968 | .setColor(color) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 969 | .setShortDescription(shortDescription) |
| 970 | .setSupportedUriSchemes(supportedUriSchemes) |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 971 | .build(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 972 | } |
| 973 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 974 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 975 | |
| 976 | /** |
| 977 | * Determines if the SIP call settings specify to use SIP for all calls, including PSTN calls. |
| 978 | * |
| 979 | * @param context The context. |
| 980 | * @return {@code True} if SIP should be used for all calls. |
| 981 | */ |
| 982 | private boolean useSipForPstnCalls(Context context) { |
| 983 | String option = Settings.System.getString(context.getContentResolver(), |
| 984 | Settings.System.SIP_CALL_OPTIONS); |
| 985 | option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY; |
| 986 | return option.equals(Settings.System.SIP_ALWAYS); |
| 987 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 988 | }; |
| 989 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 990 | @VisibleForTesting |
| 991 | public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml = |
| 992 | new XmlSerialization<PhoneAccountHandle>() { |
| 993 | private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 994 | private static final String COMPONENT_NAME = "component_name"; |
| 995 | private static final String ID = "id"; |
| 996 | |
| 997 | @Override |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 998 | public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer) |
| 999 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1000 | if (o != null) { |
| 1001 | serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1002 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1003 | if (o.getComponentName() != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 1004 | writeTextIfNonNull( |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1005 | COMPONENT_NAME, o.getComponentName().flattenToString(), serializer); |
| 1006 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1007 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame^] | 1008 | writeTextIfNonNull(ID, o.getId(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1009 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1010 | serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
| 1011 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1015 | public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1016 | throws IOException, XmlPullParserException { |
| 1017 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) { |
| 1018 | String componentNameString = null; |
| 1019 | String idString = null; |
| 1020 | int outerDepth = parser.getDepth(); |
| 1021 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1022 | if (parser.getName().equals(COMPONENT_NAME)) { |
| 1023 | parser.next(); |
| 1024 | componentNameString = parser.getText(); |
| 1025 | } else if (parser.getName().equals(ID)) { |
| 1026 | parser.next(); |
| 1027 | idString = parser.getText(); |
| 1028 | } |
| 1029 | } |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1030 | if (componentNameString != null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1031 | return new PhoneAccountHandle( |
| 1032 | ComponentName.unflattenFromString(componentNameString), |
| 1033 | idString); |
| 1034 | } |
| 1035 | } |
| 1036 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1037 | } |
| 1038 | }; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1039 | } |