1 /*
2  * Copyright (C) 2013 Google Inc.
3  * Licensed to The Android Open Source Project.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.android.mail.preferences;
18 
19 import com.google.common.collect.Sets;
20 
21 import org.json.JSONArray;
22 import org.json.JSONException;
23 import org.json.JSONObject;
24 
25 import java.util.Set;
26 
27 /**
28 * A POJO for shared preferences to be used for backing up and restoring.
29 */
30 public class SimpleBackupSharedPreference implements BackupSharedPreference {
31     private String mKey;
32     private Object mValue;
33 
34     private static final String KEY = "key";
35     private static final String VALUE = "value";
36 
SimpleBackupSharedPreference(final String key, final Object value)37     public SimpleBackupSharedPreference(final String key, final Object value) {
38         mKey = key;
39         mValue = value;
40     }
41 
42     @Override
getKey()43     public String getKey() {
44         return mKey;
45     }
46 
47     @Override
getValue()48     public Object getValue() {
49         return mValue;
50     }
51 
setValue(Object value)52     public void setValue(Object value) {
53         mValue = value;
54     }
55 
56     @Override
toJson()57     public JSONObject toJson() throws JSONException {
58         final JSONObject json = new JSONObject();
59         json.put(KEY, mKey);
60         if (mValue instanceof Set) {
61             final Set<?> set = (Set<?>) mValue;
62             final JSONArray array = new JSONArray();
63             for (final Object o : set) {
64                 array.put(o);
65             }
66             json.put(VALUE, array);
67         } else {
68             json.put(VALUE, mValue);
69         }
70         return json;
71     }
72 
fromJson(final JSONObject json)73     public static BackupSharedPreference fromJson(final JSONObject json) throws JSONException {
74         Object value = json.get(VALUE);
75         if (value instanceof JSONArray) {
76             final Set<Object> set = Sets.newHashSet();
77             final JSONArray array = (JSONArray) value;
78             for (int i = 0, len = array.length(); i < len; i++) {
79                   set.add(array.get(i));
80             }
81             value = set;
82         }
83         return new SimpleBackupSharedPreference(json.getString(KEY), value);
84     }
85 
86     @Override
toString()87     public String toString() {
88         return "BackupSharedPreference{" + "mKey='" + mKey + '\'' + ", mValue=" + mValue + '}';
89     }
90 }
91