1/*
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.app.backup.BackupManager;
20import android.content.Context;
21import android.content.SharedPreferences;
22
23import com.android.contacts.model.SimCard;
24
25import java.util.ArrayList;
26import java.util.Collection;
27import java.util.Collections;
28import java.util.HashSet;
29import java.util.List;
30import java.util.Set;
31
32public class SharedPreferenceUtil {
33
34    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
40    public static final String PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED =
41            "hamburgerPromoDisplayed";
42
43    public static final String PREFERENCE_KEY_HAMBURGER_MENU_CLICKED =
44            "hamburgerMenuClicked";
45
46    public static final String PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED =
47            "hamburgerPromoTriggerActionHappened";
48
49    private static final String PREFERENCE_KEY_IMPORTED_SIM_CARDS =
50            "importedSimCards";
51
52    private static final String PREFERENCE_KEY_DISMISSED_SIM_CARDS =
53            "dismissedSimCards";
54
55    private static final String PREFERENCE_KEY_RESTORED_DEVICES =
56            "restoredDevices";
57
58    private static final String PREFERENCE_KEY_DISMISSED_DEVICES =
59            "dismissedDevices";
60
61    public static final String PREFERENCE_WELCOME_CARD_DISMISSED =
62            "welcome-reminder-card-dismissed";
63
64    public static boolean getHamburgerPromoDisplayedBefore(Context context) {
65        return getSharedPreferences(context)
66                .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED, false);
67    }
68
69    public static void setHamburgerPromoDisplayedBefore(Context context) {
70        getSharedPreferences(context).edit()
71                .putBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED, true)
72                .apply();
73        new BackupManager(context).dataChanged();
74    }
75
76    public static boolean getHamburgerMenuClickedBefore(Context context) {
77        return getSharedPreferences(context)
78                .getBoolean(PREFERENCE_KEY_HAMBURGER_MENU_CLICKED, false);
79    }
80
81    public static void setHamburgerMenuClickedBefore(Context context) {
82        getSharedPreferences(context).edit()
83                .putBoolean(PREFERENCE_KEY_HAMBURGER_MENU_CLICKED, true)
84                .apply();
85        new BackupManager(context).dataChanged();
86    }
87
88    public static boolean getHamburgerPromoTriggerActionHappenedBefore(Context context) {
89        return getSharedPreferences(context)
90                .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED, false);
91    }
92
93    public static void setHamburgerPromoTriggerActionHappenedBefore(Context context) {
94        getSharedPreferences(context).edit()
95                .putBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED, true)
96                .apply();
97        new BackupManager(context).dataChanged();
98    }
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
115    protected static SharedPreferences getSharedPreferences(Context context) {
116        return context.getSharedPreferences(getSharedPreferencesFilename(context),
117                Context.MODE_PRIVATE);
118    }
119
120    public static String getSharedPreferencesFilename(Context context) {
121        return context.getPackageName();
122    }
123
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    }
167
168    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) {
172            final String id = sim.getSimId();
173            if (id == null) {
174                continue;
175            }
176            if (sim.isImported()) {
177                imported.add(id);
178            } else {
179                imported.remove(id);
180            }
181            if (sim.isDismissed()) {
182                dismissed.add(id);
183            } else {
184                dismissed.remove(id);
185            }
186        }
187        getSharedPreferences(context).edit()
188                .putStringSet(PREFERENCE_KEY_IMPORTED_SIM_CARDS, imported)
189                .putStringSet(PREFERENCE_KEY_DISMISSED_SIM_CARDS, dismissed)
190                .apply();
191    }
192
193    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) {
205        return getSharedPreferences(context)
206                .getStringSet(PREFERENCE_KEY_IMPORTED_SIM_CARDS, Collections.<String>emptySet());
207    }
208
209    private static Set<String> getDismissedSims(Context context) {
210        return getSharedPreferences(context)
211                .getStringSet(PREFERENCE_KEY_DISMISSED_SIM_CARDS, Collections.<String>emptySet());
212    }
213
214    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
248    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();
256        new BackupManager(context).dataChanged();
257    }
258
259    public static void clear(Context context) {
260        getSharedPreferences(context).edit().clear().commit();
261        new BackupManager(context).dataChanged();
262    }
263}
264