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