blob: 16047112e4102688de23bb1f450d3aafb97ef1e8 [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
James Laskeybc0137f2016-12-06 09:05:51 -080019import android.app.backup.BackupManager;
Tingting Wangac9596e2016-08-02 22:24:24 -070020import android.content.Context;
21import android.content.SharedPreferences;
John Shao3ed3af22016-10-18 18:31:29 -070022
Gary Mai69c182a2016-12-05 13:07:03 -080023import com.android.contacts.model.SimCard;
Marcus Hagerott66e8b222016-10-23 15:41:55 -070024
25import java.util.ArrayList;
26import java.util.Collection;
John Shao3ed3af22016-10-18 18:31:29 -070027import java.util.Collections;
28import java.util.HashSet;
Marcus Hagerott66e8b222016-10-23 15:41:55 -070029import java.util.List;
John Shao3ed3af22016-10-18 18:31:29 -070030import java.util.Set;
Tingting Wangac9596e2016-08-02 22:24:24 -070031
32public class SharedPreferenceUtil {
33
John Shao3ed3af22016-10-18 18:31:29 -070034 public static final String PREFERENCE_KEY_ACCOUNT_SYNC_OFF_DISMISSES =
35 "num-of-dismisses-account-sync-off";
36
37 public static final String PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES =
38 "num-of-dismisses-auto-sync-off";
39
James Laskeybc0137f2016-12-06 09:05:51 -080040 public static final String PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED =
yaoluff561a62016-11-17 12:22:10 -080041 "hamburgerPromoDisplayed";
Tingting Wangac9596e2016-08-02 22:24:24 -070042
James Laskeybc0137f2016-12-06 09:05:51 -080043 public static final String PREFERENCE_KEY_HAMBURGER_MENU_CLICKED =
yaoluff561a62016-11-17 12:22:10 -080044 "hamburgerMenuClicked";
Tingting Wangac9596e2016-08-02 22:24:24 -070045
James Laskeybc0137f2016-12-06 09:05:51 -080046 public static final String PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED =
yaoluff561a62016-11-17 12:22:10 -080047 "hamburgerPromoTriggerActionHappened";
Tingting Wangac9596e2016-08-02 22:24:24 -070048
John Shao3ed3af22016-10-18 18:31:29 -070049 private static final String PREFERENCE_KEY_IMPORTED_SIM_CARDS =
50 "importedSimCards";
yaolu491cca52016-09-30 14:04:40 -070051
Marcus Hagerott66e8b222016-10-23 15:41:55 -070052 private static final String PREFERENCE_KEY_DISMISSED_SIM_CARDS =
53 "dismissedSimCards";
54
Gary Mai86f27712016-10-27 15:17:49 -070055 private static final String PREFERENCE_KEY_RESTORED_DEVICES =
56 "restoredDevices";
57
58 private static final String PREFERENCE_KEY_DISMISSED_DEVICES =
59 "dismissedDevices";
60
Sean Midforddfd0b0d2016-11-01 15:05:33 -070061 public static final String PREFERENCE_WELCOME_CARD_DISMISSED =
62 "welcome-reminder-card-dismissed";
63
Tingting Wangac9596e2016-08-02 22:24:24 -070064 public static boolean getHamburgerPromoDisplayedBefore(Context context) {
65 return getSharedPreferences(context)
yaoluff561a62016-11-17 12:22:10 -080066 .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED, false);
Tingting Wangac9596e2016-08-02 22:24:24 -070067 }
68
69 public static void setHamburgerPromoDisplayedBefore(Context context) {
70 getSharedPreferences(context).edit()
yaoluff561a62016-11-17 12:22:10 -080071 .putBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED, true)
Tingting Wangac9596e2016-08-02 22:24:24 -070072 .apply();
James Laskeybc0137f2016-12-06 09:05:51 -080073 new BackupManager(context).dataChanged();
Tingting Wangac9596e2016-08-02 22:24:24 -070074 }
75
76 public static boolean getHamburgerMenuClickedBefore(Context context) {
77 return getSharedPreferences(context)
yaoluff561a62016-11-17 12:22:10 -080078 .getBoolean(PREFERENCE_KEY_HAMBURGER_MENU_CLICKED, false);
Tingting Wangac9596e2016-08-02 22:24:24 -070079 }
80
81 public static void setHamburgerMenuClickedBefore(Context context) {
82 getSharedPreferences(context).edit()
yaoluff561a62016-11-17 12:22:10 -080083 .putBoolean(PREFERENCE_KEY_HAMBURGER_MENU_CLICKED, true)
Tingting Wangac9596e2016-08-02 22:24:24 -070084 .apply();
James Laskeybc0137f2016-12-06 09:05:51 -080085 new BackupManager(context).dataChanged();
Tingting Wangac9596e2016-08-02 22:24:24 -070086 }
87
88 public static boolean getHamburgerPromoTriggerActionHappenedBefore(Context context) {
89 return getSharedPreferences(context)
yaoluff561a62016-11-17 12:22:10 -080090 .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED, false);
Tingting Wangac9596e2016-08-02 22:24:24 -070091 }
92
93 public static void setHamburgerPromoTriggerActionHappenedBefore(Context context) {
94 getSharedPreferences(context).edit()
yaoluff561a62016-11-17 12:22:10 -080095 .putBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED, true)
Tingting Wangac9596e2016-08-02 22:24:24 -070096 .apply();
James Laskeybc0137f2016-12-06 09:05:51 -080097 new BackupManager(context).dataChanged();
Tingting Wangac9596e2016-08-02 22:24:24 -070098 }
99
100 /**
101 * Show hamburger promo if:
102 * 1) Hamburger menu is never clicked before
103 * 2) Hamburger menu promo is never displayed before
104 * 3) There is at least one available user action
105 * (for now, available user actions to trigger to displayed hamburger promo are:
106 * a: QuickContact UI back to PeopleActivity
107 * b: Search action back to PeopleActivity)
108 */
109 public static boolean getShouldShowHamburgerPromo(Context context) {
110 return !getHamburgerMenuClickedBefore(context)
111 && getHamburgerPromoTriggerActionHappenedBefore(context)
112 && !getHamburgerPromoDisplayedBefore(context);
113 }
114
Marcus Hagerott596eb7e2016-10-18 10:25:12 -0700115 protected static SharedPreferences getSharedPreferences(Context context) {
James Laskeybc0137f2016-12-06 09:05:51 -0800116 return context.getSharedPreferences(getSharedPreferencesFilename(context),
117 Context.MODE_PRIVATE);
118 }
119
120 public static String getSharedPreferencesFilename(Context context) {
121 return context.getPackageName();
Tingting Wangac9596e2016-08-02 22:24:24 -0700122 }
yaolu491cca52016-09-30 14:04:40 -0700123
124 public static int getNumOfDismissesForAutoSyncOff(Context context) {
125 return getSharedPreferences(context).getInt(PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES, 0);
126 }
127
128 public static void resetNumOfDismissesForAutoSyncOff(Context context) {
129 final int value = getSharedPreferences(context).getInt(
130 PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES, 0);
131 if (value != 0) {
132 getSharedPreferences(context).edit()
133 .putInt(PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES, 0).apply();
134 }
135 }
136
137 public static void incNumOfDismissesForAutoSyncOff(Context context) {
138 final int value = getSharedPreferences(context).getInt(
139 PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES, 0);
140 getSharedPreferences(context).edit()
141 .putInt(PREFERENCE_KEY_GLOBAL_SYNC_OFF_DISMISSES, value + 1).apply();
142 }
143
144 private static String buildSharedPrefsName(String accountName) {
145 return accountName + "-" + PREFERENCE_KEY_ACCOUNT_SYNC_OFF_DISMISSES;
146 }
147
148 public static int getNumOfDismissesforAccountSyncOff(Context context, String accountName) {
149 return getSharedPreferences(context).getInt(buildSharedPrefsName(accountName), 0);
150 }
151
152 public static void resetNumOfDismissesForAccountSyncOff(Context context, String accountName) {
153 final int value = getSharedPreferences(context).getInt(
154 buildSharedPrefsName(accountName), 0);
155 if (value != 0) {
156 getSharedPreferences(context).edit()
157 .putInt(buildSharedPrefsName(accountName), 0).apply();
158 }
159 }
160
161 public static void incNumOfDismissesForAccountSyncOff(Context context, String accountName) {
162 final int value = getSharedPreferences(context).getInt(
163 buildSharedPrefsName(accountName), 0);
164 getSharedPreferences(context).edit()
165 .putInt(buildSharedPrefsName(accountName), value + 1).apply();
166 }
John Shao3ed3af22016-10-18 18:31:29 -0700167
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700168 public static void persistSimStates(Context context, Collection<SimCard> sims) {
169 final Set<String> imported = new HashSet<>(getImportedSims(context));
170 final Set<String> dismissed = new HashSet<>(getDismissedSims(context));
171 for (SimCard sim : sims) {
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700172 final String id = sim.getSimId();
173 if (id == null) {
174 continue;
175 }
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700176 if (sim.isImported()) {
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700177 imported.add(id);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700178 } else {
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700179 imported.remove(id);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700180 }
181 if (sim.isDismissed()) {
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700182 dismissed.add(id);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700183 } else {
Marcus Hagerott2aa31982016-10-25 14:36:25 -0700184 dismissed.remove(id);
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700185 }
186 }
John Shao3ed3af22016-10-18 18:31:29 -0700187 getSharedPreferences(context).edit()
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700188 .putStringSet(PREFERENCE_KEY_IMPORTED_SIM_CARDS, imported)
189 .putStringSet(PREFERENCE_KEY_DISMISSED_SIM_CARDS, dismissed)
190 .apply();
John Shao3ed3af22016-10-18 18:31:29 -0700191 }
192
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700193 public static List<SimCard> restoreSimStates(Context context, List<SimCard> sims) {
194 final Set<String> imported = getImportedSims(context);
195 final Set<String> dismissed = getDismissedSims(context);
196 List<SimCard> result = new ArrayList<>();
197 for (SimCard sim : sims) {
198 result.add(sim.withImportAndDismissStates(imported.contains(sim.getSimId()),
199 dismissed.contains(sim.getSimId())));
200 }
201 return result;
202 }
203
204 private static Set<String> getImportedSims(Context context) {
John Shao3ed3af22016-10-18 18:31:29 -0700205 return getSharedPreferences(context)
206 .getStringSet(PREFERENCE_KEY_IMPORTED_SIM_CARDS, Collections.<String>emptySet());
207 }
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700208
209 private static Set<String> getDismissedSims(Context context) {
210 return getSharedPreferences(context)
211 .getStringSet(PREFERENCE_KEY_DISMISSED_SIM_CARDS, Collections.<String>emptySet());
212 }
213
Gary Mai86f27712016-10-27 15:17:49 -0700214 public static Set<String> getRestoredDevices(Context context) {
215 return getSharedPreferences(context)
216 .getStringSet(PREFERENCE_KEY_RESTORED_DEVICES, Collections.<String>emptySet());
217 }
218
219 public static Set<String> getDismissedDevices(Context context) {
220 return getSharedPreferences(context)
221 .getStringSet(PREFERENCE_KEY_DISMISSED_DEVICES, Collections.<String>emptySet());
222 }
223
224 public static void addRestoredDevice(Context context, String deviceId) {
225 final Set<String> restoredDevices = new HashSet<>(getRestoredDevices(context));
226 restoredDevices.add(deviceId);
227 getSharedPreferences(context).edit()
228 .putStringSet(PREFERENCE_KEY_RESTORED_DEVICES, restoredDevices)
229 .apply();
230 }
231
232 public static void addDismissedDevice(Context context, String deviceId) {
233 final Set<String> dismissedDevices = new HashSet<>(getDismissedDevices(context));
234 dismissedDevices.add(deviceId);
235 getSharedPreferences(context).edit()
236 .putStringSet(PREFERENCE_KEY_DISMISSED_DEVICES, dismissedDevices)
237 .commit();
238 }
239
240 public static void removeDismissedDevice(Context context, String deviceId) {
241 final Set<String> dismissedDevices = new HashSet<>(getDismissedDevices(context));
242 dismissedDevices.remove(deviceId);
243 getSharedPreferences(context).edit()
244 .putStringSet(PREFERENCE_KEY_DISMISSED_DEVICES, dismissedDevices)
245 .commit();
246 }
247
Sean Midforddfd0b0d2016-11-01 15:05:33 -0700248 public static boolean isWelcomeCardDismissed(Context context) {
249 return getSharedPreferences(context).getBoolean(PREFERENCE_WELCOME_CARD_DISMISSED,
250 false);
251 }
252
253 public static void setWelcomeCardDismissed(Context context, boolean isDismissed) {
254 getSharedPreferences(context).edit().putBoolean(PREFERENCE_WELCOME_CARD_DISMISSED,
255 isDismissed).apply();
James Laskeybc0137f2016-12-06 09:05:51 -0800256 new BackupManager(context).dataChanged();
Sean Midforddfd0b0d2016-11-01 15:05:33 -0700257 }
258
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700259 public static void clear(Context context) {
260 getSharedPreferences(context).edit().clear().commit();
James Laskeybc0137f2016-12-06 09:05:51 -0800261 new BackupManager(context).dataChanged();
Marcus Hagerott66e8b222016-10-23 15:41:55 -0700262 }
Tingting Wangac9596e2016-08-02 22:24:24 -0700263}