blob: 3e24ddd8647657abdc23957a2f726a1f846122fb [file] [log] [blame]
Mindy Pereira4fa30612012-02-23 17:50:38 -08001/*
2 * Copyright (C) 2012 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
17package com.android.mail.persistence;
18
19import com.android.mail.utils.LogUtils;
20import com.google.common.annotations.VisibleForTesting;
21
22import android.content.Context;
23import android.content.SharedPreferences;
24import android.content.SharedPreferences.Editor;
25
26import java.util.Set;
27
28/**
29 * This class is used to read and write Mail data to the persistent store. Note that each
30 * key is suffixed with the account name, which is why I need to do some manual work in
31 * order to get/set these values.
32 */
33public class Persistence {
34 public static final String TAG = new LogUtils().getLogTag();
35
36 private static Persistence mInstance = null;
37
38 private static SharedPreferences sSharedPrefs;
39
40 private Persistence() {
41 // Singleton only, use getInstance()
42 }
43
44 // The name of our shared preferences store
45 public static final String SHARED_PREFERENCES_NAME = "UnifiedEmail";
46
47 public static Persistence getInstance() {
48 if (mInstance == null) {
49 mInstance = new Persistence();
50 }
51
52 return mInstance;
53 }
54
55 public static SharedPreferences getPreferences(Context context) {
56 if (sSharedPrefs == null) {
57 sSharedPrefs = context.getSharedPreferences(
58 SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
59 }
60 return sSharedPrefs;
61 }
62
63 private String makeKey(String account, String key) {
64 return account != null ? account + "-" + key : key;
65 }
66
67 public void setString(Context context, String account, String key, String value) {
68 Editor editor = getPreferences(context).edit();
69 editor.putString(makeKey(account, key), value);
70 editor.apply();
71 }
72
73 public void setStringSet(Context context, String account, String key, Set<String> values) {
74 Editor editor = getPreferences(context).edit();
75 editor.putStringSet(makeKey(account, key) , values);
76 editor.apply();
77 }
78
79 public Set<String> getStringSet(Context context, String account, String key, Set<String> def) {
80 return getPreferences(context).getStringSet(makeKey(account, key), def);
81 }
82
83 public void setBoolean(Context context, String account, String key, Boolean value) {
84 Editor editor = getPreferences(context).edit();
85 editor.putBoolean(makeKey(account, key), value);
86 editor.apply();
87 }
88
89 @VisibleForTesting
90 public void remove(Context context, String account, String key) {
91 remove(context, makeKey(account, key));
92 }
93
94 private void remove(Context context, String key) {
95 Editor editor = getPreferences(context).edit();
96 editor.remove(key);
97 editor.apply();
98 }
99}