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