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 | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 19 | import android.telecomm.PhoneAccountHandle; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 20 | import org.json.JSONArray; |
| 21 | import org.json.JSONException; |
| 22 | import org.json.JSONObject; |
| 23 | import org.json.JSONTokener; |
| 24 | |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
| 27 | |
| 28 | import android.content.SharedPreferences; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 30 | import android.telecomm.PhoneAccountMetadata; |
| 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(); |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 59 | return s.defaultOutgoingHandle; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 60 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 61 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 62 | public void setDefaultOutgoingPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 63 | State s = read(); |
| 64 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 65 | if (accountHandle == null) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 66 | // Asking to clear the default outgoing is a valid request |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 67 | s.defaultOutgoingHandle = null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 68 | } else { |
| 69 | boolean found = false; |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 70 | for (PhoneAccountMetadata m : s.accounts) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 71 | if (Objects.equals(accountHandle, m.getAccount())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 72 | found = true; |
| 73 | break; |
| 74 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 75 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 76 | |
| 77 | if (!found) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 78 | Log.w(this, "Trying to set nonexistent default outgoing phone accountHandle %s", |
| 79 | accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 83 | s.defaultOutgoingHandle = accountHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 86 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 89 | public List<PhoneAccountHandle> getEnabledPhoneAccounts() { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 90 | State s = read(); |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 91 | return accountHandlesOnly(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 94 | public PhoneAccountMetadata getPhoneAccountMetadata(PhoneAccountHandle accountHandle) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 95 | State s = read(); |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 96 | for (PhoneAccountMetadata m : s.accounts) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 97 | if (Objects.equals(accountHandle, m.getAccount())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 98 | return m; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | return null; |
| 102 | } |
| 103 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 104 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 105 | // ComponentName? |
| 106 | public void registerPhoneAccount(PhoneAccountMetadata metadata) { |
| 107 | State s = read(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 108 | |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 109 | s.accounts.add(metadata); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 110 | // Search for duplicates and remove any that are found. |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 111 | for (int i = 0; i < s.accounts.size() - 1; i++) { |
| 112 | if (Objects.equals(metadata.getAccount(), s.accounts.get(i).getAccount())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 113 | // replace existing entry. |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 114 | s.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 115 | break; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 119 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 122 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 123 | State s = read(); |
| 124 | |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 125 | for (int i = 0; i < s.accounts.size(); i++) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 126 | if (Objects.equals(accountHandle, s.accounts.get(i).getAccount())) { |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 127 | s.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | checkDefaultOutgoing(s); |
| 133 | |
| 134 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 137 | public void clearAccounts(String packageName) { |
| 138 | State s = read(); |
| 139 | |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 140 | for (int i = 0; i < s.accounts.size(); i++) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 141 | if (Objects.equals( |
| 142 | packageName, |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 143 | s.accounts.get(i).getAccount().getComponentName().getPackageName())) { |
| 144 | s.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | checkDefaultOutgoing(s); |
| 149 | |
| 150 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 153 | private void checkDefaultOutgoing(State s) { |
| 154 | // Check that, after an operation that removes accounts, the account set up as the "default |
| 155 | // outgoing" has not been deleted. If it has, then clear out the setting. |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 156 | for (PhoneAccountMetadata m : s.accounts) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 157 | if (Objects.equals(s.defaultOutgoingHandle, m.getAccount())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | } |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 161 | s.defaultOutgoingHandle = null; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 164 | private List<PhoneAccountHandle> accountHandlesOnly(State s) { |
| 165 | List<PhoneAccountHandle> result = new ArrayList<>(); |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 166 | for (PhoneAccountMetadata m : s.accounts) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 167 | result.add(m.getAccount()); |
| 168 | } |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | private State read() { |
| 173 | try { |
| 174 | String serialized = getPreferences().getString(PREFERENCE_PHONE_ACCOUNTS, null); |
| 175 | Log.d(this, "read() obtained serialized state: %s", serialized); |
| 176 | State state = serialized == null |
| 177 | ? new State() |
| 178 | : deserializeState(serialized); |
| 179 | Log.d(this, "read() obtained state: %s", state); |
| 180 | return state; |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 181 | } catch (JSONException e) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 182 | Log.e(this, e, "read"); |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 183 | return new State(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | private boolean write(State state) { |
| 188 | try { |
| 189 | Log.d(this, "write() writing state: %s", state); |
| 190 | String serialized = serializeState(state); |
| 191 | Log.d(this, "write() writing serialized state: %s", serialized); |
| 192 | boolean success = getPreferences() |
| 193 | .edit() |
| 194 | .putString(PREFERENCE_PHONE_ACCOUNTS, serialized) |
| 195 | .commit(); |
| 196 | Log.d(this, "serialized state was written with succcess = %b", success); |
| 197 | return success; |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 198 | } catch (JSONException e) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 199 | Log.e(this, e, "write"); |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 200 | return false; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 201 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | private SharedPreferences getPreferences() { |
| 205 | return mContext.getSharedPreferences(TELECOMM_PREFERENCES, Context.MODE_PRIVATE); |
| 206 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 207 | |
| 208 | private String serializeState(State s) throws JSONException { |
| 209 | // TODO: If this is used in production, remove the indent (=> do not pretty print) |
| 210 | return sStateJson.toJson(s).toString(2); |
| 211 | } |
| 212 | |
| 213 | private State deserializeState(String s) throws JSONException { |
| 214 | return sStateJson.fromJson(new JSONObject(new JSONTokener(s))); |
| 215 | } |
| 216 | |
| 217 | private static class State { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 218 | public PhoneAccountHandle defaultOutgoingHandle = null; |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 219 | public final List<PhoneAccountMetadata> accounts = new ArrayList<>(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // |
| 223 | // JSON serialization |
| 224 | // |
| 225 | |
| 226 | private interface Json<T> { |
| 227 | JSONObject toJson(T o) throws JSONException; |
| 228 | T fromJson(JSONObject json) throws JSONException; |
| 229 | } |
| 230 | |
| 231 | private static final Json<State> sStateJson = |
| 232 | new Json<State>() { |
| 233 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
| 234 | private static final String ACCOUNTS = "accounts"; |
| 235 | |
| 236 | @Override |
| 237 | public JSONObject toJson(State o) throws JSONException { |
| 238 | JSONObject json = new JSONObject(); |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 239 | if (o.defaultOutgoingHandle != null) { |
| 240 | json.put(DEFAULT_OUTGOING, sPhoneAccountJson.toJson(o.defaultOutgoingHandle)); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 241 | } |
| 242 | JSONArray accounts = new JSONArray(); |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 243 | for (PhoneAccountMetadata m : o.accounts) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 244 | accounts.put(sPhoneAccountMetadataJson.toJson(m)); |
| 245 | } |
| 246 | json.put(ACCOUNTS, accounts); |
| 247 | return json; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public State fromJson(JSONObject json) throws JSONException { |
| 252 | State s = new State(); |
| 253 | if (json.has(DEFAULT_OUTGOING)) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 254 | s.defaultOutgoingHandle = sPhoneAccountJson.fromJson( |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 255 | (JSONObject) json.get(DEFAULT_OUTGOING)); |
| 256 | } |
| 257 | if (json.has(ACCOUNTS)) { |
| 258 | JSONArray accounts = (JSONArray) json.get(ACCOUNTS); |
| 259 | for (int i = 0; i < accounts.length(); i++) { |
| 260 | try { |
Ihab Awad | 78ac0ce | 2014-07-18 13:32:53 -0700 | [diff] [blame] | 261 | s.accounts.add(sPhoneAccountMetadataJson.fromJson( |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 262 | (JSONObject) accounts.get(i))); |
| 263 | } catch (Exception e) { |
| 264 | Log.e(this, e, "Extracting phone account"); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | return s; |
| 269 | } |
| 270 | }; |
| 271 | |
| 272 | private static final Json<PhoneAccountMetadata> sPhoneAccountMetadataJson = |
| 273 | new Json<PhoneAccountMetadata>() { |
| 274 | private static final String ACCOUNT = "account"; |
| 275 | private static final String HANDLE = "handle"; |
Evan Charlton | 484f8d6 | 2014-07-18 14:06:58 -0700 | [diff] [blame] | 276 | private static final String SUBSCRIPTION_NUMBER = "subscription_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 277 | private static final String CAPABILITIES = "capabilities"; |
| 278 | private static final String ICON_RES_ID = "icon_res_id"; |
| 279 | private static final String LABEL = "label"; |
| 280 | private static final String SHORT_DESCRIPTION = "short_description"; |
| 281 | private static final String VIDEO_CALLING_SUPPORTED = "video_calling_supported"; |
| 282 | |
| 283 | @Override |
| 284 | public JSONObject toJson(PhoneAccountMetadata o) throws JSONException { |
| 285 | return new JSONObject() |
| 286 | .put(ACCOUNT, sPhoneAccountJson.toJson(o.getAccount())) |
| 287 | .put(HANDLE, o.getHandle().toString()) |
Evan Charlton | 484f8d6 | 2014-07-18 14:06:58 -0700 | [diff] [blame] | 288 | .put(SUBSCRIPTION_NUMBER, o.getSubscriptionNumber()) |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 289 | .put(CAPABILITIES, o.getCapabilities()) |
| 290 | .put(ICON_RES_ID, o.getIconResId()) |
| 291 | .put(LABEL, o.getLabel()) |
| 292 | .put(SHORT_DESCRIPTION, o.getShortDescription()) |
| 293 | .put(VIDEO_CALLING_SUPPORTED, (Boolean) o.isVideoCallingSupported()); |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public PhoneAccountMetadata fromJson(JSONObject json) throws JSONException { |
| 298 | return new PhoneAccountMetadata( |
| 299 | sPhoneAccountJson.fromJson((JSONObject) json.get(ACCOUNT)), |
| 300 | Uri.parse((String) json.get(HANDLE)), |
Evan Charlton | 484f8d6 | 2014-07-18 14:06:58 -0700 | [diff] [blame] | 301 | (String) json.get(SUBSCRIPTION_NUMBER), |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 302 | (int) json.get(CAPABILITIES), |
| 303 | (int) json.get(ICON_RES_ID), |
| 304 | (String) json.get(LABEL), |
| 305 | (String) json.get(SHORT_DESCRIPTION), |
| 306 | (Boolean) json.get(VIDEO_CALLING_SUPPORTED)); |
| 307 | } |
| 308 | }; |
| 309 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 310 | private static final Json<PhoneAccountHandle> sPhoneAccountJson = |
| 311 | new Json<PhoneAccountHandle>() { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 312 | private static final String COMPONENT_NAME = "component_name"; |
| 313 | private static final String ID = "id"; |
| 314 | |
| 315 | @Override |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 316 | public JSONObject toJson(PhoneAccountHandle o) throws JSONException { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 317 | return new JSONObject() |
| 318 | .put(COMPONENT_NAME, o.getComponentName().flattenToString()) |
| 319 | .put(ID, o.getId()); |
| 320 | } |
| 321 | |
| 322 | @Override |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame^] | 323 | public PhoneAccountHandle fromJson(JSONObject json) throws JSONException { |
| 324 | return new PhoneAccountHandle( |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 325 | ComponentName.unflattenFromString((String) json.get(COMPONENT_NAME)), |
| 326 | (String) json.get(ID)); |
| 327 | } |
| 328 | }; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 329 | } |