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