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