Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.contacts.util; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.SharedPreferences; |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 21 | |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 22 | import com.android.contacts.common.model.SimCard; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Collection; |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 26 | import java.util.Collections; |
| 27 | import java.util.HashSet; |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 28 | import java.util.List; |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 29 | import java.util.Set; |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 30 | |
| 31 | public class SharedPreferenceUtil { |
| 32 | |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 33 | 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 | |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 39 | private static final String PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED_BEFORE = |
| 40 | "hamburgerPromoDisplayedBefore"; |
| 41 | |
| 42 | private static final String PREFERENCE_KEY_HAMBURGER_MENU_CLICKED_BEFORE = |
| 43 | "hamburgerMenuClickedBefore"; |
| 44 | |
| 45 | private static final String PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED_BEFORE = |
| 46 | "hamburgerPromoTriggerActionHappenedBefore"; |
| 47 | |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 48 | private static final String PREFERENCE_KEY_IMPORTED_SIM_CARDS = |
| 49 | "importedSimCards"; |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 50 | |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 51 | private static final String PREFERENCE_KEY_DISMISSED_SIM_CARDS = |
| 52 | "dismissedSimCards"; |
| 53 | |
Gary Mai | 86f2771 | 2016-10-27 15:17:49 -0700 | [diff] [blame] | 54 | private static final String PREFERENCE_KEY_RESTORED_DEVICES = |
| 55 | "restoredDevices"; |
| 56 | |
| 57 | private static final String PREFERENCE_KEY_DISMISSED_DEVICES = |
| 58 | "dismissedDevices"; |
| 59 | |
Sean Midford | dfd0b0d | 2016-11-01 15:05:33 -0700 | [diff] [blame^] | 60 | public static final String PREFERENCE_WELCOME_CARD_DISMISSED = |
| 61 | "welcome-reminder-card-dismissed"; |
| 62 | |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 63 | public static boolean getHamburgerPromoDisplayedBefore(Context context) { |
| 64 | return getSharedPreferences(context) |
| 65 | .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED_BEFORE, false); |
| 66 | } |
| 67 | |
| 68 | public static void setHamburgerPromoDisplayedBefore(Context context) { |
| 69 | getSharedPreferences(context).edit() |
| 70 | .putBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED_BEFORE, true) |
| 71 | .apply(); |
| 72 | } |
| 73 | |
| 74 | public static boolean getHamburgerMenuClickedBefore(Context context) { |
| 75 | return getSharedPreferences(context) |
| 76 | .getBoolean(PREFERENCE_KEY_HAMBURGER_MENU_CLICKED_BEFORE, false); |
| 77 | } |
| 78 | |
| 79 | public static void setHamburgerMenuClickedBefore(Context context) { |
| 80 | getSharedPreferences(context).edit() |
| 81 | .putBoolean(PREFERENCE_KEY_HAMBURGER_MENU_CLICKED_BEFORE, true) |
| 82 | .apply(); |
| 83 | } |
| 84 | |
| 85 | public static boolean getHamburgerPromoTriggerActionHappenedBefore(Context context) { |
| 86 | return getSharedPreferences(context) |
| 87 | .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED_BEFORE, false); |
| 88 | } |
| 89 | |
| 90 | public static void setHamburgerPromoTriggerActionHappenedBefore(Context context) { |
| 91 | getSharedPreferences(context).edit() |
| 92 | .putBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED_BEFORE, true) |
| 93 | .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 Hagerott | 596eb7e | 2016-10-18 10:25:12 -0700 | [diff] [blame] | 111 | protected static SharedPreferences getSharedPreferences(Context context) { |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 112 | return context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE); |
| 113 | } |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 114 | |
| 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 Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 158 | |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 159 | 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 Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 163 | final String id = sim.getSimId(); |
| 164 | if (id == null) { |
| 165 | continue; |
| 166 | } |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 167 | if (sim.isImported()) { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 168 | imported.add(id); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 169 | } else { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 170 | imported.remove(id); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 171 | } |
| 172 | if (sim.isDismissed()) { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 173 | dismissed.add(id); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 174 | } else { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 175 | dismissed.remove(id); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 178 | getSharedPreferences(context).edit() |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 179 | .putStringSet(PREFERENCE_KEY_IMPORTED_SIM_CARDS, imported) |
| 180 | .putStringSet(PREFERENCE_KEY_DISMISSED_SIM_CARDS, dismissed) |
| 181 | .apply(); |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 184 | 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 Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 196 | return getSharedPreferences(context) |
| 197 | .getStringSet(PREFERENCE_KEY_IMPORTED_SIM_CARDS, Collections.<String>emptySet()); |
| 198 | } |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 199 | |
| 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 Mai | 86f2771 | 2016-10-27 15:17:49 -0700 | [diff] [blame] | 205 | 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 Midford | dfd0b0d | 2016-11-01 15:05:33 -0700 | [diff] [blame^] | 239 | 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 Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 249 | public static void clear(Context context) { |
| 250 | getSharedPreferences(context).edit().clear().commit(); |
| 251 | } |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 252 | } |