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 | |
| 17 | package com.android.telecomm; |
| 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; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 24 | import android.telecomm.ConnectionService; |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 25 | import android.telecomm.PhoneAccount; |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 26 | import android.telecomm.PhoneAccountHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 30 | import android.telecomm.TelecommManager; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 31 | import android.text.TextUtils; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 32 | import android.util.AtomicFile; |
| 33 | import android.util.Xml; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 34 | |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 35 | import com.android.internal.annotations.VisibleForTesting; |
| 36 | import com.android.internal.util.FastXmlSerializer; |
| 37 | import com.android.internal.util.XmlUtils; |
| 38 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 39 | import org.xmlpull.v1.XmlPullParser; |
| 40 | import org.xmlpull.v1.XmlPullParserException; |
| 41 | import org.xmlpull.v1.XmlSerializer; |
| 42 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 43 | import java.io.BufferedInputStream; |
| 44 | import java.io.BufferedOutputStream; |
| 45 | import java.io.File; |
| 46 | import java.io.FileNotFoundException; |
| 47 | import java.io.FileOutputStream; |
| 48 | import java.io.IOException; |
| 49 | import java.io.InputStream; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 50 | import java.lang.SecurityException; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 51 | import java.lang.String; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 52 | import java.util.ArrayList; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 53 | import java.util.Iterator; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 54 | import java.util.List; |
| 55 | import java.util.Objects; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 56 | import java.util.concurrent.CopyOnWriteArrayList; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 59 | * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 60 | * delegate for all the account handling methods on {@link TelecommManager} as implemented in |
| 61 | * {@link TelecommServiceImpl}, with the notable exception that {@link TelecommServiceImpl} is |
| 62 | * 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] | 63 | * the {@code ComponentName}s they are declaring in their {@code PhoneAccountHandle}s. |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 64 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 65 | public final class PhoneAccountRegistrar { |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 66 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame^] | 67 | public static final PhoneAccountHandle NO_ACCOUNT_SELECTED = |
| 68 | new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED"); |
| 69 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 70 | public abstract static class Listener { |
| 71 | public void onAccountsChanged(PhoneAccountRegistrar registrar) {} |
| 72 | public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {} |
| 73 | public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {} |
| 74 | } |
| 75 | |
| 76 | private static final String FILE_NAME = "phone-account-registrar-state.xml"; |
| 77 | |
| 78 | private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); |
| 79 | private final AtomicFile mAtomicFile; |
| 80 | private State mState; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 81 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 82 | public PhoneAccountRegistrar(Context context) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 83 | this(context, FILE_NAME); |
| 84 | } |
| 85 | |
| 86 | @VisibleForTesting |
| 87 | public PhoneAccountRegistrar(Context context, String fileName) { |
| 88 | // TODO: Change file location when Telecomm is part of system |
| 89 | mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName)); |
| 90 | mState = new State(); |
| 91 | read(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 94 | public PhoneAccountHandle getDefaultOutgoingPhoneAccount() { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame^] | 95 | final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(); |
| 96 | if (userSelected != null) { |
| 97 | return userSelected; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 100 | List<PhoneAccountHandle> outgoing = getOutgoingPhoneAccounts(); |
| 101 | switch (outgoing.size()) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 102 | case 0: |
| 103 | // There are no accounts, so there can be no default |
| 104 | return null; |
| 105 | case 1: |
| 106 | // There is only one account, which is by definition the default |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 107 | return outgoing.get(0); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 108 | default: |
| 109 | // There are multiple accounts with no selected default |
| 110 | return null; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 111 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 112 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 113 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame^] | 114 | PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() { |
| 115 | if (mState.defaultOutgoing != null) { |
| 116 | // Return the registered outgoing default iff it still exists (we keep a sticky |
| 117 | // default to survive account deletion and re-addition) |
| 118 | for (int i = 0; i < mState.accounts.size(); i++) { |
| 119 | if (mState.accounts.get(i).getAccountHandle().equals(mState.defaultOutgoing)) { |
| 120 | return mState.defaultOutgoing; |
| 121 | } |
| 122 | } |
| 123 | // At this point, there was a registered default but it has been deleted; proceed |
| 124 | // as though there were no default |
| 125 | } |
| 126 | return null; |
| 127 | } |
| 128 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 129 | public void setDefaultOutgoingPhoneAccount(PhoneAccountHandle accountHandle) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 130 | if (accountHandle == null) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 131 | // Asking to clear the default outgoing is a valid request |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 132 | mState.defaultOutgoing = null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 133 | } else { |
| 134 | boolean found = false; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 135 | for (PhoneAccount m : mState.accounts) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 136 | if (Objects.equals(accountHandle, m.getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 137 | found = true; |
| 138 | break; |
| 139 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 140 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 141 | |
| 142 | if (!found) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 143 | Log.w(this, "Trying to set nonexistent default outgoing %s", |
| 144 | accountHandle); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | if (!has(getPhoneAccount(accountHandle), PhoneAccount.CAPABILITY_CALL_PROVIDER)) { |
| 149 | Log.w(this, "Trying to set non-call-provider default outgoing %s", |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 150 | accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 154 | mState.defaultOutgoing = accountHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 157 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 158 | fireDefaultOutgoingChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 161 | public void setSimCallManager(PhoneAccountHandle callManager) { |
| 162 | if (callManager != null) { |
| 163 | PhoneAccount callManagerAccount = getPhoneAccount(callManager); |
| 164 | if (callManagerAccount == null) { |
| 165 | Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager); |
| 166 | return; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 167 | } else if (!has(callManagerAccount, PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 168 | Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount); |
| 169 | return; |
| 170 | } |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame^] | 171 | } else { |
| 172 | callManager = NO_ACCOUNT_SELECTED; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 173 | } |
| 174 | mState.simCallManager = callManager; |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame^] | 175 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 176 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 177 | fireSimCallManagerChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | public PhoneAccountHandle getSimCallManager() { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 181 | if (mState.simCallManager != null) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame^] | 182 | if (NO_ACCOUNT_SELECTED.equals(mState.simCallManager)) { |
| 183 | return null; |
| 184 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 185 | // Return the registered sim call manager iff it still exists (we keep a sticky |
| 186 | // setting to survive account deletion and re-addition) |
| 187 | for (int i = 0; i < mState.accounts.size(); i++) { |
| 188 | if (mState.accounts.get(i).getAccountHandle().equals(mState.simCallManager)) { |
| 189 | return mState.simCallManager; |
| 190 | } |
| 191 | } |
| 192 | } |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 193 | |
| 194 | // See if the OEM has specified a default one. |
| 195 | Context context = TelecommApp.getInstance(); |
| 196 | String defaultConnectionMgr = |
| 197 | context.getResources().getString(R.string.default_connection_manager_component); |
| 198 | if (!TextUtils.isEmpty(defaultConnectionMgr)) { |
| 199 | PackageManager pm = context.getPackageManager(); |
| 200 | |
| 201 | ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr); |
| 202 | Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE); |
| 203 | intent.setComponent(componentName); |
| 204 | |
| 205 | // Make sure that the component can be resolved. |
| 206 | List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent, 0); |
| 207 | if (!resolveInfos.isEmpty()) { |
| 208 | // See if there is registered PhoneAccount by this component. |
| 209 | List<PhoneAccountHandle> handles = getAllPhoneAccountHandles(); |
| 210 | for (PhoneAccountHandle handle : handles) { |
| 211 | if (componentName.equals(handle.getComponentName())) { |
| 212 | return handle; |
| 213 | } |
| 214 | } |
| 215 | Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName); |
| 216 | } else { |
| 217 | Log.d(this, "%s could not be resolved; not using as default", componentName); |
| 218 | } |
| 219 | } else { |
| 220 | Log.v(this, "No default connection manager specified"); |
| 221 | } |
| 222 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 223 | return null; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | public List<PhoneAccountHandle> getAllPhoneAccountHandles() { |
| 227 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
| 228 | for (PhoneAccount m : mState.accounts) { |
| 229 | accountHandles.add(m.getAccountHandle()); |
| 230 | } |
| 231 | return accountHandles; |
| 232 | } |
| 233 | |
| 234 | public List<PhoneAccount> getAllPhoneAccounts() { |
| 235 | return new ArrayList<>(mState.accounts); |
| 236 | } |
| 237 | |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 238 | public List<PhoneAccountHandle> getOutgoingPhoneAccounts() { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 239 | return getCallProviderAccountHandles(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 242 | public PhoneAccount getPhoneAccount(PhoneAccountHandle handle) { |
| 243 | for (PhoneAccount m : mState.accounts) { |
| 244 | if (Objects.equals(handle, m.getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 245 | return m; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | return null; |
| 249 | } |
| 250 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 251 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 252 | // ComponentName? |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 253 | public void registerPhoneAccount(PhoneAccount account) { |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 254 | // Enforce the requirement that a connection service for a phone account has the correct |
| 255 | // permission. |
| 256 | if (!phoneAccountHasPermission(account.getAccountHandle())) { |
| 257 | Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.", |
| 258 | account.getAccountHandle()); |
| 259 | throw new SecurityException( |
| 260 | "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission."); |
| 261 | } |
| 262 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 263 | mState.accounts.add(account); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 264 | // Search for duplicates and remove any that are found. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 265 | for (int i = 0; i < mState.accounts.size() - 1; i++) { |
| 266 | if (Objects.equals( |
| 267 | account.getAccountHandle(), mState.accounts.get(i).getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 268 | // replace existing entry. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 269 | mState.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 270 | break; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 274 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 275 | fireAccountsChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 278 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 279 | for (int i = 0; i < mState.accounts.size(); i++) { |
| 280 | if (Objects.equals(accountHandle, mState.accounts.get(i).getAccountHandle())) { |
| 281 | mState.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 282 | break; |
| 283 | } |
| 284 | } |
| 285 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 286 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 287 | fireAccountsChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 290 | /** |
| 291 | * Un-registers all phone accounts associated with a specified package. |
| 292 | * |
| 293 | * @param packageName The package for which phone accounts will be removed. |
| 294 | */ |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 295 | public void clearAccounts(String packageName) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 296 | boolean accountsRemoved = false; |
| 297 | Iterator<PhoneAccount> it = mState.accounts.iterator(); |
| 298 | while (it.hasNext()) { |
| 299 | PhoneAccount phoneAccount = it.next(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 300 | if (Objects.equals( |
| 301 | packageName, |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 302 | phoneAccount.getAccountHandle().getComponentName().getPackageName())) { |
| 303 | Log.i(this, "Removing phone account " + phoneAccount.getLabel()); |
| 304 | it.remove(); |
| 305 | accountsRemoved = true; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 309 | if (accountsRemoved) { |
| 310 | write(); |
| 311 | fireAccountsChanged(); |
| 312 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | public void addListener(Listener l) { |
| 316 | mListeners.add(l); |
| 317 | } |
| 318 | |
| 319 | public void removeListener(Listener l) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 320 | if (l != null) { |
| 321 | mListeners.remove(l); |
| 322 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | private void fireAccountsChanged() { |
| 326 | for (Listener l : mListeners) { |
| 327 | l.onAccountsChanged(this); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | private void fireDefaultOutgoingChanged() { |
| 332 | for (Listener l : mListeners) { |
| 333 | l.onDefaultOutgoingChanged(this); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | private void fireSimCallManagerChanged() { |
| 338 | for (Listener l : mListeners) { |
| 339 | l.onSimCallManagerChanged(this); |
| 340 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 343 | /** |
| 344 | * Determines if the connection service specified by a {@link PhoneAccountHandle} has the |
| 345 | * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission. |
| 346 | * |
| 347 | * @param phoneAccountHandle The phone account to check. |
| 348 | * @return {@code True} if the phone account has permission. |
| 349 | */ |
| 350 | public boolean phoneAccountHasPermission(PhoneAccountHandle phoneAccountHandle) { |
| 351 | PackageManager packageManager = TelecommApp.getInstance().getPackageManager(); |
| 352 | try { |
| 353 | ServiceInfo serviceInfo = packageManager.getServiceInfo( |
| 354 | phoneAccountHandle.getComponentName(), 0); |
| 355 | |
| 356 | return serviceInfo.permission != null && |
| 357 | serviceInfo.permission.equals(Manifest.permission.BIND_CONNECTION_SERVICE); |
| 358 | } catch (PackageManager.NameNotFoundException e) { |
| 359 | Log.w(this, "Name not found %s", e); |
| 360 | return false; |
| 361 | } |
| 362 | } |
| 363 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 364 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 365 | |
| 366 | // TODO: Add a corresponding has(...) method to class PhoneAccount itself and remove this one |
| 367 | // Return true iff the given account has all the specified capability flags |
| 368 | static boolean has(PhoneAccount account, int capability) { |
| 369 | return (account.getCapabilities() & capability) == capability; |
| 370 | } |
| 371 | |
| 372 | private List<PhoneAccountHandle> getCallProviderAccountHandles() { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 373 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 374 | for (PhoneAccount m : mState.accounts) { |
| 375 | if (has(m, PhoneAccount.CAPABILITY_CALL_PROVIDER)) { |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 376 | accountHandles.add(m.getAccountHandle()); |
| 377 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 378 | } |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 379 | return accountHandles; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 382 | /** |
| 383 | * The state of this {@code PhoneAccountRegistrar}. |
| 384 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 385 | @VisibleForTesting |
| 386 | public static class State { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 387 | /** |
| 388 | * The account selected by the user to be employed by default for making outgoing calls. |
| 389 | * If the user has not made such a selection, then this is null. |
| 390 | */ |
| 391 | public PhoneAccountHandle defaultOutgoing = null; |
| 392 | |
| 393 | /** |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 394 | * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 395 | * manages and optimizes a user's PSTN SIM connections. |
| 396 | */ |
| 397 | public PhoneAccountHandle simCallManager; |
| 398 | |
| 399 | /** |
| 400 | * The complete list of {@code PhoneAccount}s known to the Telecomm subsystem. |
| 401 | */ |
| 402 | public final List<PhoneAccount> accounts = new ArrayList<>(); |
| 403 | } |
| 404 | |
| 405 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 406 | // |
| 407 | // State management |
| 408 | // |
| 409 | |
| 410 | private void write() { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 411 | final FileOutputStream os; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 412 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 413 | os = mAtomicFile.startWrite(); |
| 414 | boolean success = false; |
| 415 | try { |
| 416 | XmlSerializer serializer = new FastXmlSerializer(); |
| 417 | serializer.setOutput(new BufferedOutputStream(os), "utf-8"); |
| 418 | writeToXml(mState, serializer); |
| 419 | serializer.flush(); |
| 420 | success = true; |
| 421 | } finally { |
| 422 | if (success) { |
| 423 | mAtomicFile.finishWrite(os); |
| 424 | } else { |
| 425 | mAtomicFile.failWrite(os); |
| 426 | } |
| 427 | } |
| 428 | } catch (IOException e) { |
| 429 | Log.e(this, e, "Writing state to XML file"); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 433 | private void read() { |
| 434 | final InputStream is; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 435 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 436 | is = mAtomicFile.openRead(); |
| 437 | } catch (FileNotFoundException ex) { |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | XmlPullParser parser; |
| 442 | try { |
| 443 | parser = Xml.newPullParser(); |
| 444 | parser.setInput(new BufferedInputStream(is), null); |
| 445 | parser.nextTag(); |
| 446 | mState = readFromXml(parser); |
| 447 | } catch (IOException | XmlPullParserException e) { |
| 448 | Log.e(this, e, "Reading state from XML file"); |
| 449 | mState = new State(); |
| 450 | } finally { |
| 451 | try { |
| 452 | is.close(); |
| 453 | } catch (IOException e) { |
| 454 | Log.e(this, e, "Closing InputStream"); |
| 455 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 456 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 459 | private static void writeToXml(State state, XmlSerializer serializer) |
| 460 | throws IOException { |
| 461 | sStateXml.writeToXml(state, serializer); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 462 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 463 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 464 | private static State readFromXml(XmlPullParser parser) |
| 465 | throws IOException, XmlPullParserException { |
| 466 | State s = sStateXml.readFromXml(parser); |
| 467 | return s != null ? s : new State(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 470 | //////////////////////////////////////////////////////////////////////////////////////////////// |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 471 | // |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 472 | // XML serialization |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 473 | // |
| 474 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 475 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 476 | public abstract static class XmlSerialization<T> { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 477 | /** |
| 478 | * Write the supplied object to XML |
| 479 | */ |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 480 | public abstract void writeToXml(T o, XmlSerializer serializer) |
| 481 | throws IOException; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 482 | |
| 483 | /** |
| 484 | * Read from the supplied XML into a new object, returning null in case of an |
| 485 | * unrecoverable schema mismatch or other data error. 'parser' must be already |
| 486 | * positioned at the first tag that is expected to have been emitted by this |
| 487 | * object's writeToXml(). This object tries to fail early without modifying |
| 488 | * 'parser' if it does not recognize the data it sees. |
| 489 | */ |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 490 | public abstract T readFromXml(XmlPullParser parser) |
| 491 | throws IOException, XmlPullParserException; |
| 492 | |
| 493 | protected void writeTextSafely(String tagName, Object value, XmlSerializer serializer) |
| 494 | throws IOException { |
| 495 | if (value != null) { |
| 496 | serializer.startTag(null, tagName); |
| 497 | serializer.text(Objects.toString(value)); |
| 498 | serializer.endTag(null, tagName); |
| 499 | } |
| 500 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 503 | @VisibleForTesting |
| 504 | public static final XmlSerialization<State> sStateXml = |
| 505 | new XmlSerialization<State>() { |
| 506 | private static final String CLASS_STATE = "phone_account_registrar_state"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 507 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 508 | private static final String SIM_CALL_MANAGER = "sim_call_manager"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 509 | private static final String ACCOUNTS = "accounts"; |
| 510 | |
| 511 | @Override |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 512 | public void writeToXml(State o, XmlSerializer serializer) |
| 513 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 514 | if (o != null) { |
| 515 | serializer.startTag(null, CLASS_STATE); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 516 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 517 | if (o.defaultOutgoing != null) { |
| 518 | serializer.startTag(null, DEFAULT_OUTGOING); |
| 519 | sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer); |
| 520 | serializer.endTag(null, DEFAULT_OUTGOING); |
| 521 | } |
| 522 | |
| 523 | if (o.simCallManager != null) { |
| 524 | serializer.startTag(null, SIM_CALL_MANAGER); |
| 525 | sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer); |
| 526 | serializer.endTag(null, SIM_CALL_MANAGER); |
| 527 | } |
| 528 | |
| 529 | serializer.startTag(null, ACCOUNTS); |
| 530 | for (PhoneAccount m : o.accounts) { |
| 531 | sPhoneAccountXml.writeToXml(m, serializer); |
| 532 | } |
| 533 | serializer.endTag(null, ACCOUNTS); |
| 534 | |
| 535 | serializer.endTag(null, CLASS_STATE); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 536 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | @Override |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 540 | public State readFromXml(XmlPullParser parser) |
| 541 | throws IOException, XmlPullParserException { |
| 542 | if (parser.getName().equals(CLASS_STATE)) { |
| 543 | State s = new State(); |
| 544 | int outerDepth = parser.getDepth(); |
| 545 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 546 | if (parser.getName().equals(DEFAULT_OUTGOING)) { |
| 547 | parser.nextTag(); |
| 548 | s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser); |
| 549 | } else if (parser.getName().equals(SIM_CALL_MANAGER)) { |
| 550 | parser.nextTag(); |
| 551 | s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser); |
| 552 | } else if (parser.getName().equals(ACCOUNTS)) { |
| 553 | int accountsDepth = parser.getDepth(); |
| 554 | while (XmlUtils.nextElementWithin(parser, accountsDepth)) { |
| 555 | PhoneAccount account = sPhoneAccountXml.readFromXml(parser); |
| 556 | if (account != null) { |
| 557 | s.accounts.add(account); |
| 558 | } |
| 559 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 562 | return s; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 563 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 564 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 565 | } |
| 566 | }; |
| 567 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 568 | @VisibleForTesting |
| 569 | public static final XmlSerialization<PhoneAccount> sPhoneAccountXml = |
| 570 | new XmlSerialization<PhoneAccount>() { |
| 571 | private static final String CLASS_PHONE_ACCOUNT = "phone_account"; |
| 572 | private static final String ACCOUNT_HANDLE = "account_handle"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 573 | private static final String HANDLE = "handle"; |
Evan Charlton | 484f8d6 | 2014-07-18 14:06:58 -0700 | [diff] [blame] | 574 | private static final String SUBSCRIPTION_NUMBER = "subscription_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 575 | private static final String CAPABILITIES = "capabilities"; |
| 576 | private static final String ICON_RES_ID = "icon_res_id"; |
| 577 | private static final String LABEL = "label"; |
| 578 | private static final String SHORT_DESCRIPTION = "short_description"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 579 | |
| 580 | @Override |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 581 | public void writeToXml(PhoneAccount o, XmlSerializer serializer) |
| 582 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 583 | if (o != null) { |
| 584 | serializer.startTag(null, CLASS_PHONE_ACCOUNT); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 585 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 586 | if (o.getAccountHandle() != null) { |
| 587 | serializer.startTag(null, ACCOUNT_HANDLE); |
| 588 | sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer); |
| 589 | serializer.endTag(null, ACCOUNT_HANDLE); |
| 590 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 591 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 592 | writeTextSafely(HANDLE, o.getHandle(), serializer); |
| 593 | writeTextSafely(SUBSCRIPTION_NUMBER, o.getSubscriptionNumber(), serializer); |
| 594 | writeTextSafely(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer); |
| 595 | writeTextSafely(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer); |
| 596 | writeTextSafely(LABEL, o.getLabel(), serializer); |
| 597 | writeTextSafely(SHORT_DESCRIPTION, o.getShortDescription(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 598 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 599 | serializer.endTag(null, CLASS_PHONE_ACCOUNT); |
| 600 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | @Override |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 604 | public PhoneAccount readFromXml(XmlPullParser parser) |
| 605 | throws IOException, XmlPullParserException { |
| 606 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) { |
| 607 | int outerDepth = parser.getDepth(); |
| 608 | PhoneAccountHandle accountHandle = null; |
| 609 | Uri handle = null; |
| 610 | String subscriptionNumber = null; |
| 611 | int capabilities = 0; |
| 612 | int iconResId = 0; |
| 613 | String label = null; |
| 614 | String shortDescription = null; |
| 615 | |
| 616 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 617 | if (parser.getName().equals(ACCOUNT_HANDLE)) { |
| 618 | parser.nextTag(); |
| 619 | accountHandle = sPhoneAccountHandleXml.readFromXml(parser); |
| 620 | } else if (parser.getName().equals(HANDLE)) { |
| 621 | parser.next(); |
| 622 | handle = Uri.parse(parser.getText()); |
| 623 | } else if (parser.getName().equals(SUBSCRIPTION_NUMBER)) { |
| 624 | parser.next(); |
| 625 | subscriptionNumber = parser.getText(); |
| 626 | } else if (parser.getName().equals(CAPABILITIES)) { |
| 627 | parser.next(); |
| 628 | capabilities = Integer.parseInt(parser.getText()); |
| 629 | } else if (parser.getName().equals(ICON_RES_ID)) { |
| 630 | parser.next(); |
| 631 | iconResId = Integer.parseInt(parser.getText()); |
| 632 | } else if (parser.getName().equals(LABEL)) { |
| 633 | parser.next(); |
| 634 | label = parser.getText(); |
| 635 | } else if (parser.getName().equals(SHORT_DESCRIPTION)) { |
| 636 | parser.next(); |
| 637 | shortDescription = parser.getText(); |
| 638 | } |
| 639 | } |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 640 | return PhoneAccount.builder() |
| 641 | .withAccountHandle(accountHandle) |
| 642 | .withHandle(handle) |
| 643 | .withSubscriptionNumber(subscriptionNumber) |
| 644 | .withCapabilities(capabilities) |
| 645 | .withIconResId(iconResId) |
| 646 | .withLabel(label) |
| 647 | .withShortDescription(shortDescription) |
| 648 | .build(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 649 | } |
| 650 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 651 | } |
| 652 | }; |
| 653 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 654 | @VisibleForTesting |
| 655 | public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml = |
| 656 | new XmlSerialization<PhoneAccountHandle>() { |
| 657 | private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 658 | private static final String COMPONENT_NAME = "component_name"; |
| 659 | private static final String ID = "id"; |
| 660 | |
| 661 | @Override |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 662 | public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer) |
| 663 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 664 | if (o != null) { |
| 665 | serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 666 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 667 | if (o.getComponentName() != null) { |
| 668 | writeTextSafely( |
| 669 | COMPONENT_NAME, o.getComponentName().flattenToString(), serializer); |
| 670 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 671 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 672 | writeTextSafely(ID, o.getId(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 673 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 674 | serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
| 675 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | @Override |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 679 | public PhoneAccountHandle readFromXml(XmlPullParser parser) |
| 680 | throws IOException, XmlPullParserException { |
| 681 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) { |
| 682 | String componentNameString = null; |
| 683 | String idString = null; |
| 684 | int outerDepth = parser.getDepth(); |
| 685 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 686 | if (parser.getName().equals(COMPONENT_NAME)) { |
| 687 | parser.next(); |
| 688 | componentNameString = parser.getText(); |
| 689 | } else if (parser.getName().equals(ID)) { |
| 690 | parser.next(); |
| 691 | idString = parser.getText(); |
| 692 | } |
| 693 | } |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 694 | if (componentNameString != null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 695 | return new PhoneAccountHandle( |
| 696 | ComponentName.unflattenFromString(componentNameString), |
| 697 | idString); |
| 698 | } |
| 699 | } |
| 700 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 701 | } |
| 702 | }; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 703 | } |