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