Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.telecomm; |
| 18 | |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 19 | import android.telecomm.PhoneAccount; |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 20 | import android.telecomm.PhoneAccountHandle; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 21 | import org.json.JSONArray; |
| 22 | import org.json.JSONException; |
| 23 | import org.json.JSONObject; |
| 24 | import org.json.JSONTokener; |
| 25 | |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
| 28 | |
| 29 | import android.content.SharedPreferences; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 30 | import android.net.Uri; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 31 | import android.telecomm.TelecommManager; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 32 | |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | import java.util.Objects; |
| 36 | |
| 37 | /** |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 38 | * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 39 | * delegate for all the account handling methods on {@link TelecommManager} as implemented in |
| 40 | * {@link TelecommServiceImpl}, with the notable exception that {@link TelecommServiceImpl} is |
| 41 | * 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] | 42 | * the {@code ComponentName}s they are declaring in their {@code PhoneAccountHandle}s. |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 43 | * |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 44 | * TODO(santoscordon): Replace this implementation with a proper database stored in a Telecomm |
| 45 | * provider. |
| 46 | */ |
| 47 | final class PhoneAccountRegistrar { |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 48 | private static final String TELECOMM_PREFERENCES = "telecomm_prefs"; |
| 49 | private static final String PREFERENCE_PHONE_ACCOUNTS = "phone_accounts"; |
| 50 | |
| 51 | private final Context mContext; |
| 52 | |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 53 | PhoneAccountRegistrar(Context context) { |
| 54 | mContext = context; |
| 55 | } |
| 56 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 57 | public PhoneAccountHandle getDefaultOutgoingPhoneAccount() { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 58 | State s = read(); |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame^] | 59 | |
| 60 | if (s.defaultOutgoingHandle != null) { |
| 61 | // Return the registered outgoing default iff it still exists (we keep a sticky |
| 62 | // default to survive account deletion and re-addition) |
| 63 | for (int i = 0; i < s.accounts.size(); i++) { |
| 64 | if (s.accounts.get(i).getAccountHandle().equals(s.defaultOutgoingHandle)) { |
| 65 | return s.defaultOutgoingHandle; |
| 66 | } |
| 67 | } |
| 68 | // At this point, there was a registered default but it has been deleted; remember |
| 69 | // it for the future, but return null from this method |
| 70 | return null; |
| 71 | } else { |
| 72 | List<PhoneAccountHandle> enabled = getEnabledPhoneAccounts(); |
| 73 | switch (enabled.size()) { |
| 74 | case 0: |
| 75 | // There are no accounts, so there can be no default |
| 76 | return null; |
| 77 | case 1: |
| 78 | // There is only one account, which is by definition the default |
| 79 | return enabled.get(0); |
| 80 | default: |
| 81 | // There are multiple accounts with no selected default |
| 82 | return null; |
| 83 | } |
| 84 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 85 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 86 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 87 | public void setDefaultOutgoingPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 88 | State s = read(); |
| 89 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 90 | if (accountHandle == null) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 91 | // Asking to clear the default outgoing is a valid request |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 92 | s.defaultOutgoingHandle = null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 93 | } else { |
| 94 | boolean found = false; |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 95 | for (PhoneAccount m : s.accounts) { |
| 96 | if (Objects.equals(accountHandle, m.getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 97 | found = true; |
| 98 | break; |
| 99 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 100 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 101 | |
| 102 | if (!found) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 103 | Log.w(this, "Trying to set nonexistent default outgoing phone accountHandle %s", |
| 104 | accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 108 | s.defaultOutgoingHandle = accountHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 111 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 114 | public List<PhoneAccountHandle> getEnabledPhoneAccounts() { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 115 | State s = read(); |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame^] | 116 | return simSubscriptionAccountHandles(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 119 | public PhoneAccount getPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 120 | State s = read(); |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 121 | for (PhoneAccount m : s.accounts) { |
| 122 | if (Objects.equals(accountHandle, m.getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 123 | return m; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | return null; |
| 127 | } |
| 128 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 129 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 130 | // ComponentName? |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 131 | public void registerPhoneAccount(PhoneAccount metadata) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 132 | State s = read(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 133 | |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 134 | s.accounts.add(metadata); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 135 | // Search for duplicates and remove any that are found. |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 136 | for (int i = 0; i < s.accounts.size() - 1; i++) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 137 | if (Objects.equals(metadata.getAccountHandle(), s.accounts.get(i).getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 138 | // replace existing entry. |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 139 | s.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 140 | break; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 144 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 147 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 148 | State s = read(); |
| 149 | |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 150 | for (int i = 0; i < s.accounts.size(); i++) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 151 | if (Objects.equals(accountHandle, s.accounts.get(i).getAccountHandle())) { |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 152 | s.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 157 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 160 | public void clearAccounts(String packageName) { |
| 161 | State s = read(); |
| 162 | |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 163 | for (int i = 0; i < s.accounts.size(); i++) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 164 | if (Objects.equals( |
| 165 | packageName, |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 166 | s.accounts.get(i).getAccountHandle().getComponentName().getPackageName())) { |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 167 | s.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 171 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame^] | 174 | private List<PhoneAccountHandle> simSubscriptionAccountHandles(State s) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 175 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
| 176 | for (PhoneAccount m : s.accounts) { |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame^] | 177 | if ((m.getCapabilities() & PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) != 0) { |
| 178 | accountHandles.add(m.getAccountHandle()); |
| 179 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 180 | } |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 181 | return accountHandles; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | private State read() { |
| 185 | try { |
| 186 | String serialized = getPreferences().getString(PREFERENCE_PHONE_ACCOUNTS, null); |
| 187 | Log.d(this, "read() obtained serialized state: %s", serialized); |
| 188 | State state = serialized == null |
| 189 | ? new State() |
| 190 | : deserializeState(serialized); |
| 191 | Log.d(this, "read() obtained state: %s", state); |
| 192 | return state; |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 193 | } catch (JSONException e) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 194 | Log.e(this, e, "read"); |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 195 | return new State(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | private boolean write(State state) { |
| 200 | try { |
| 201 | Log.d(this, "write() writing state: %s", state); |
| 202 | String serialized = serializeState(state); |
| 203 | Log.d(this, "write() writing serialized state: %s", serialized); |
| 204 | boolean success = getPreferences() |
| 205 | .edit() |
| 206 | .putString(PREFERENCE_PHONE_ACCOUNTS, serialized) |
| 207 | .commit(); |
| 208 | Log.d(this, "serialized state was written with succcess = %b", success); |
| 209 | return success; |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 210 | } catch (JSONException e) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 211 | Log.e(this, e, "write"); |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 212 | return false; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 213 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | private SharedPreferences getPreferences() { |
| 217 | return mContext.getSharedPreferences(TELECOMM_PREFERENCES, Context.MODE_PRIVATE); |
| 218 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 219 | |
| 220 | private String serializeState(State s) throws JSONException { |
| 221 | // TODO: If this is used in production, remove the indent (=> do not pretty print) |
| 222 | return sStateJson.toJson(s).toString(2); |
| 223 | } |
| 224 | |
| 225 | private State deserializeState(String s) throws JSONException { |
| 226 | return sStateJson.fromJson(new JSONObject(new JSONTokener(s))); |
| 227 | } |
| 228 | |
| 229 | private static class State { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 230 | public PhoneAccountHandle defaultOutgoingHandle = null; |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 231 | public final List<PhoneAccount> accounts = new ArrayList<>(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // |
| 235 | // JSON serialization |
| 236 | // |
| 237 | |
| 238 | private interface Json<T> { |
| 239 | JSONObject toJson(T o) throws JSONException; |
| 240 | T fromJson(JSONObject json) throws JSONException; |
| 241 | } |
| 242 | |
| 243 | private static final Json<State> sStateJson = |
| 244 | new Json<State>() { |
| 245 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
| 246 | private static final String ACCOUNTS = "accounts"; |
| 247 | |
| 248 | @Override |
| 249 | public JSONObject toJson(State o) throws JSONException { |
| 250 | JSONObject json = new JSONObject(); |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 251 | if (o.defaultOutgoingHandle != null) { |
| 252 | json.put(DEFAULT_OUTGOING, sPhoneAccountJson.toJson(o.defaultOutgoingHandle)); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 253 | } |
| 254 | JSONArray accounts = new JSONArray(); |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 255 | for (PhoneAccount m : o.accounts) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 256 | accounts.put(sPhoneAccountMetadataJson.toJson(m)); |
| 257 | } |
| 258 | json.put(ACCOUNTS, accounts); |
| 259 | return json; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public State fromJson(JSONObject json) throws JSONException { |
| 264 | State s = new State(); |
| 265 | if (json.has(DEFAULT_OUTGOING)) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 266 | s.defaultOutgoingHandle = sPhoneAccountJson.fromJson( |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 267 | (JSONObject) json.get(DEFAULT_OUTGOING)); |
| 268 | } |
| 269 | if (json.has(ACCOUNTS)) { |
| 270 | JSONArray accounts = (JSONArray) json.get(ACCOUNTS); |
| 271 | for (int i = 0; i < accounts.length(); i++) { |
| 272 | try { |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 273 | s.accounts.add(sPhoneAccountMetadataJson.fromJson( |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 274 | (JSONObject) accounts.get(i))); |
| 275 | } catch (Exception e) { |
| 276 | Log.e(this, e, "Extracting phone account"); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | return s; |
| 281 | } |
| 282 | }; |
| 283 | |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 284 | private static final Json<PhoneAccount> sPhoneAccountMetadataJson = |
| 285 | new Json<PhoneAccount>() { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 286 | private static final String ACCOUNT = "account"; |
| 287 | private static final String HANDLE = "handle"; |
Evan Charlton | 484f8d6 | 2014-07-18 14:06:58 -0700 | [diff] [blame] | 288 | private static final String SUBSCRIPTION_NUMBER = "subscription_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 289 | private static final String CAPABILITIES = "capabilities"; |
| 290 | private static final String ICON_RES_ID = "icon_res_id"; |
| 291 | private static final String LABEL = "label"; |
| 292 | private static final String SHORT_DESCRIPTION = "short_description"; |
| 293 | private static final String VIDEO_CALLING_SUPPORTED = "video_calling_supported"; |
| 294 | |
| 295 | @Override |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 296 | public JSONObject toJson(PhoneAccount o) throws JSONException { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 297 | return new JSONObject() |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 298 | .put(ACCOUNT, sPhoneAccountJson.toJson(o.getAccountHandle())) |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 299 | .put(HANDLE, o.getHandle().toString()) |
Evan Charlton | 484f8d6 | 2014-07-18 14:06:58 -0700 | [diff] [blame] | 300 | .put(SUBSCRIPTION_NUMBER, o.getSubscriptionNumber()) |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 301 | .put(CAPABILITIES, o.getCapabilities()) |
| 302 | .put(ICON_RES_ID, o.getIconResId()) |
| 303 | .put(LABEL, o.getLabel()) |
| 304 | .put(SHORT_DESCRIPTION, o.getShortDescription()) |
| 305 | .put(VIDEO_CALLING_SUPPORTED, (Boolean) o.isVideoCallingSupported()); |
| 306 | } |
| 307 | |
| 308 | @Override |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 309 | public PhoneAccount fromJson(JSONObject json) throws JSONException { |
| 310 | return new PhoneAccount( |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 311 | sPhoneAccountJson.fromJson((JSONObject) json.get(ACCOUNT)), |
| 312 | Uri.parse((String) json.get(HANDLE)), |
Evan Charlton | 484f8d6 | 2014-07-18 14:06:58 -0700 | [diff] [blame] | 313 | (String) json.get(SUBSCRIPTION_NUMBER), |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 314 | (int) json.get(CAPABILITIES), |
| 315 | (int) json.get(ICON_RES_ID), |
| 316 | (String) json.get(LABEL), |
| 317 | (String) json.get(SHORT_DESCRIPTION), |
| 318 | (Boolean) json.get(VIDEO_CALLING_SUPPORTED)); |
| 319 | } |
| 320 | }; |
| 321 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 322 | private static final Json<PhoneAccountHandle> sPhoneAccountJson = |
| 323 | new Json<PhoneAccountHandle>() { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 324 | private static final String COMPONENT_NAME = "component_name"; |
| 325 | private static final String ID = "id"; |
| 326 | |
| 327 | @Override |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 328 | public JSONObject toJson(PhoneAccountHandle o) throws JSONException { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 329 | return new JSONObject() |
| 330 | .put(COMPONENT_NAME, o.getComponentName().flattenToString()) |
| 331 | .put(ID, o.getId()); |
| 332 | } |
| 333 | |
| 334 | @Override |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 335 | public PhoneAccountHandle fromJson(JSONObject json) throws JSONException { |
| 336 | return new PhoneAccountHandle( |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 337 | ComponentName.unflattenFromString((String) json.get(COMPONENT_NAME)), |
| 338 | (String) json.get(ID)); |
| 339 | } |
| 340 | }; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 341 | } |