blob: 3d138fbcbe64fb23970cbef0ac931bc9f5a8dee6 [file] [log] [blame]
Tingting Wangac9596e2016-08-02 22:24:24 -07001/*
2 * Copyright (C) 2016 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.contacts.util;
18
19import android.content.Context;
20import android.content.SharedPreferences;
John Shao3ed3af22016-10-18 18:31:29 -070021
Marcus Hagerott66e8b222016-10-23 15:41:55 -070022import com.android.contacts.common.model.SimCard;
23
24import java.util.ArrayList;
25import java.util.Collection;
John Shao3ed3af22016-10-18 18:31:29 -070026import java.util.Collections;
27import java.util.HashSet;
Marcus Hagerott66e8b222016-10-23 15:41:55 -070028import java.util.List;
John Shao3ed3af22016-10-18 18:31:29 -070029import java.util.Set;
Tingting Wangac9596e2016-08-02 22:24:24 -070030
31public class SharedPreferenceUtil {
32
John Shao3ed3af22016-10-18 18:31:29 -070033 public static final String PREFERENCE_KEY_ACCOUNT_SYNC_OFF_DISMISSES =
34 "num-of-dismisses-account-sync-off";
35
36 public static final String PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES =
37 "num-of-dismisses-auto-sync-off";
38
yaoluff561a62016-11-17 12:22:10 -080039 private static final String PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED =
40 "hamburgerPromoDisplayed";
Tingting Wangac9596e2016-08-02 22:24:24 -070041
yaoluff561a62016-11-17 12:22:10 -080042 private static final String PREFERENCE_KEY_HAMBURGER_MENU_CLICKED =
43 "hamburgerMenuClicked";
Tingting Wangac9596e2016-08-02 22:24:24 -070044
yaoluff561a62016-11-17 12:22:10 -080045 private static final String PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED =
46 "hamburgerPromoTriggerActionHappened";
Tingting Wangac9596e2016-08-02 22:24:24 -070047
John Shao3ed3af22016-10-18 18:31:29 -070048 private static final String PREFERENCE_KEY_IMPORTED_SIM_CARDS =
49 "importedSimCards";
yaolu491cca52016-09-30 14:04:40 -070050
Marcus Hagerott66e8b222016-10-23 15:41:55 -070051 private static final String PREFERENCE_KEY_DISMISSED_SIM_CARDS =
52 "dismissedSimCards";
53
Gary Mai86f27712016-10-27 15:17:49 -070054 private static final String PREFERENCE_KEY_RESTORED_DEVICES =
55 "restoredDevices";
56
57 private static final String PREFERENCE_KEY_DISMISSED_DEVICES =
58 "dismissedDevices";
59
Sean Midforddfd0b0d2016-11-01 15:05:33 -070060 public static final String PREFERENCE_WELCOME_CARD_DISMISSED =
61 "welcome-reminder-card-dismissed";
62
Tingting Wangac9596e2016-08-02 22:24:24 -070063 public static boolean getHamburgerPromoDisplayedBefore(Context context) {
64 return getSharedPreferences(context)
yaoluff561a62016-11-17 12:22:10 -080065 .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED, false);
Tingting Wangac9596e2016-08-02 22:24:24 -070066 }
67
68 public static void setHamburgerPromoDisplayedBefore(Context context) {
69 getSharedPreferences(context).edit()
yaoluff561a62016-11-17 12:22:10 -080070 .putBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED, true)
Tingting Wangac9596e2016-08-02 22:24:24 -070071 .apply();
72 }
73
74 public static boolean getHamburgerMenuClickedBefore(Context context) {
75 return getSharedPreferences(context)
yaoluff561a62016-11-17 12:22:10 -080076 .getBoolean(PREFERENCE_KEY_HAMBURGER_MENU_CLICKED, false);
Tingting Wangac9596e2016-08-02 22:24:24 -070077 }
78
79 public static void setHamburgerMenuClickedBefore(Context context) {
80 getSharedPreferences(context).edit()
yaoluff561a62016-11-17 12:22:10 -080081 .putBoolean(PREFERENCE_KEY_HAMBURGER_MENU_CLICKED, true)
Tingting Wangac9596e2016-08-02 22:24:24 -070082 .apply();
83 }
84
85 public static boolean getHamburgerPromoTriggerActionHappenedBefore(Context context) {
86 return getSharedPreferences(context)
yaoluff561a62016-11-17 12:22:10 -080087 .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED, false);
Tingting Wangac9596e2016-08-02 22:24:24 -070088 }
89
90 public static void setHamburgerPromoTriggerActionHappenedBefore(Context context) {
91 getSharedPreferences(context).edit()
yaoluff561a62016-11-17 12:22:10 -080092 .putBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED, true)
Tingting Wangac9596e2016-08-02 22:24:24 -070093 .apply();
94 }
95
96 /**
97 * Show hamburger promo if:
98 * 1) Hamburger menu is never clicked before
99 * 2) Hamburger menu promo is never displayed before
100 * 3) There is at least one available user action
101 * (for now, available user actions to trigger to displayed hamburger promo are:
102 * a: QuickContact UI back to PeopleActivity
103 * b: Search action back to PeopleActivity)
104 */
105 public static boolean getShouldShowHamburgerPromo(Context context) {
106 return !getHamburgerMenuClickedBefore(context)
107 && getHamburgerPromoTriggerActionHappenedBefore(context)
108 && !getHamburgerPromoDisplayedBefore(context);
109 }
110
Marcus Hagerott596eb7e2016-10-18 10:25:12 -0700111 protected static SharedPreferences getSharedPreferences(Context context) {
Tingting Wangac9596e2016-08-02 22:24:24 -0700112 return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
113 }
yaolu491cca52016-09-30 14:04:40 -0700114
115 public static int getNumOfDismissesForAutoSyncOff(Context context) {
116 return getSharedPreferences(context).getInt(PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES, 0);
117 }
118
119 public static void resetNumOfDismissesForAutoSyncOff(Context context) {
120 final int value = getSharedPreferences(context).getInt(
121 PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES, 0);
122 if (value != 0) {
123 getSharedPreferences(context).edit()
124 .putInt(PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES, 0).apply();
125 }
126 }
127
128 public static void incNumOfDismissesForAutoSyncOff(Context context) {
129 final int value = getSharedPreferences(context).getInt(
130 PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES, 0);
131 getSharedPreferences(context).edit()
132 .putInt(PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES, value + 1).apply();
133 }
134
135 private static String buildSharedPrefsName(String accountName) {
136 return accountName + "-" + PREFERENCE_KEY_ACCOUNT_SYNC_OFF_DISMISSES;
137 }
138
139 public static int getNumOfDismissesforAccountSyncOff(Context context, String accountName) {
140 return getSharedPreferences(context).getInt(buildSharedPrefsName(accountName), 0);
141 }
142
143 public static void resetNumOfDismissesForAccountSyncOff(Context context, String accountName) {
144 final int value = getSharedPreferences(context).getInt(
145 buildSharedPrefsName(accountName), 0);
146 if (value != 0) {
147 getSharedPreferences(context).edit()
148 .putInt(buildSharedPrefsName(accountName), 0).apply();
149 }
150 }
151
152 public static void incNumOfDismissesForAccountSyncOff(Context context, String accountName) {
153 final int value = getSharedPreferences(context).getInt(
154 buildSharedPrefsName(accountName), 0);
155 getSharedPreferences(context).edit()
156 .putInt(buildSharedPrefsName(accountName), value + 1).apply();
157 }
John Shao3ed3af22016-10-18 18:31:29 -0700158
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700159 public static void persistSimStates(Context context, Collection<SimCard> sims) {
160 final Set<String> imported = new HashSet<>(getImportedSims(context));
161 final Set<String> dismissed = new HashSet<>(getDismissedSims(context));
162 for (SimCard sim : sims) {
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700163 final String id = sim.getSimId();
164 if (id == null) {
165 continue;
166 }
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700167 if (sim.isImported()) {
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700168 imported.add(id);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700169 } else {
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700170 imported.remove(id);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700171 }
172 if (sim.isDismissed()) {
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700173 dismissed.add(id);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700174 } else {
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700175 dismissed.remove(id);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700176 }
177 }
John Shao3ed3af22016-10-18 18:31:29 -0700178 getSharedPreferences(context).edit()
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700179 .putStringSet(PREFERENCE_KEY_IMPORTED_SIM_CARDS, imported)
180 .putStringSet(PREFERENCE_KEY_DISMISSED_SIM_CARDS, dismissed)
181 .apply();
John Shao3ed3af22016-10-18 18:31:29 -0700182 }
183
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700184 public static List<SimCard> restoreSimStates(Context context, List<SimCard> sims) {
185 final Set<String> imported = getImportedSims(context);
186 final Set<String> dismissed = getDismissedSims(context);
187 List<SimCard> result = new ArrayList<>();
188 for (SimCard sim : sims) {
189 result.add(sim.withImportAndDismissStates(imported.contains(sim.getSimId()),
190 dismissed.contains(sim.getSimId())));
191 }
192 return result;
193 }
194
195 private static Set<String> getImportedSims(Context context) {
John Shao3ed3af22016-10-18 18:31:29 -0700196 return getSharedPreferences(context)
197 .getStringSet(PREFERENCE_KEY_IMPORTED_SIM_CARDS, Collections.<String>emptySet());
198 }
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700199
200 private static Set<String> getDismissedSims(Context context) {
201 return getSharedPreferences(context)
202 .getStringSet(PREFERENCE_KEY_DISMISSED_SIM_CARDS, Collections.<String>emptySet());
203 }
204
Gary Mai86f27712016-10-27 15:17:49 -0700205 public static Set<String> getRestoredDevices(Context context) {
206 return getSharedPreferences(context)
207 .getStringSet(PREFERENCE_KEY_RESTORED_DEVICES, Collections.<String>emptySet());
208 }
209
210 public static Set<String> getDismissedDevices(Context context) {
211 return getSharedPreferences(context)
212 .getStringSet(PREFERENCE_KEY_DISMISSED_DEVICES, Collections.<String>emptySet());
213 }
214
215 public static void addRestoredDevice(Context context, String deviceId) {
216 final Set<String> restoredDevices = new HashSet<>(getRestoredDevices(context));
217 restoredDevices.add(deviceId);
218 getSharedPreferences(context).edit()
219 .putStringSet(PREFERENCE_KEY_RESTORED_DEVICES, restoredDevices)
220 .apply();
221 }
222
223 public static void addDismissedDevice(Context context, String deviceId) {
224 final Set<String> dismissedDevices = new HashSet<>(getDismissedDevices(context));
225 dismissedDevices.add(deviceId);
226 getSharedPreferences(context).edit()
227 .putStringSet(PREFERENCE_KEY_DISMISSED_DEVICES, dismissedDevices)
228 .commit();
229 }
230
231 public static void removeDismissedDevice(Context context, String deviceId) {
232 final Set<String> dismissedDevices = new HashSet<>(getDismissedDevices(context));
233 dismissedDevices.remove(deviceId);
234 getSharedPreferences(context).edit()
235 .putStringSet(PREFERENCE_KEY_DISMISSED_DEVICES, dismissedDevices)
236 .commit();
237 }
238
Sean Midforddfd0b0d2016-11-01 15:05:33 -0700239 public static boolean isWelcomeCardDismissed(Context context) {
240 return getSharedPreferences(context).getBoolean(PREFERENCE_WELCOME_CARD_DISMISSED,
241 false);
242 }
243
244 public static void setWelcomeCardDismissed(Context context, boolean isDismissed) {
245 getSharedPreferences(context).edit().putBoolean(PREFERENCE_WELCOME_CARD_DISMISSED,
246 isDismissed).apply();
247 }
248
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700249 public static void clear(Context context) {
250 getSharedPreferences(context).edit().clear().commit();
251 }
Tingting Wangac9596e2016-08-02 22:24:24 -0700252}