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 | |
James Laskey | bc0137f | 2016-12-06 09:05:51 -0800 | [diff] [blame] | 19 | import android.app.backup.BackupManager; |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.SharedPreferences; |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 22 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 23 | import com.android.contacts.model.SimCard; |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Collection; |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 27 | import java.util.Collections; |
| 28 | import java.util.HashSet; |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 29 | import java.util.List; |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 30 | import java.util.Set; |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 31 | |
| 32 | public class SharedPreferenceUtil { |
| 33 | |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 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 | |
James Laskey | bc0137f | 2016-12-06 09:05:51 -0800 | [diff] [blame] | 40 | public static final String PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED = |
yaolu | ff561a6 | 2016-11-17 12:22:10 -0800 | [diff] [blame] | 41 | "hamburgerPromoDisplayed"; |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 42 | |
James Laskey | bc0137f | 2016-12-06 09:05:51 -0800 | [diff] [blame] | 43 | public static final String PREFERENCE_KEY_HAMBURGER_MENU_CLICKED = |
yaolu | ff561a6 | 2016-11-17 12:22:10 -0800 | [diff] [blame] | 44 | "hamburgerMenuClicked"; |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 45 | |
James Laskey | bc0137f | 2016-12-06 09:05:51 -0800 | [diff] [blame] | 46 | public static final String PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED = |
yaolu | ff561a6 | 2016-11-17 12:22:10 -0800 | [diff] [blame] | 47 | "hamburgerPromoTriggerActionHappened"; |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 48 | |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 49 | private static final String PREFERENCE_KEY_IMPORTED_SIM_CARDS = |
| 50 | "importedSimCards"; |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 51 | |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 52 | private static final String PREFERENCE_KEY_DISMISSED_SIM_CARDS = |
| 53 | "dismissedSimCards"; |
| 54 | |
Gary Mai | 86f2771 | 2016-10-27 15:17:49 -0700 | [diff] [blame] | 55 | private static final String PREFERENCE_KEY_RESTORED_DEVICES = |
| 56 | "restoredDevices"; |
| 57 | |
| 58 | private static final String PREFERENCE_KEY_DISMISSED_DEVICES = |
| 59 | "dismissedDevices"; |
| 60 | |
Sean Midford | dfd0b0d | 2016-11-01 15:05:33 -0700 | [diff] [blame] | 61 | public static final String PREFERENCE_WELCOME_CARD_DISMISSED = |
| 62 | "welcome-reminder-card-dismissed"; |
| 63 | |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 64 | public static boolean getHamburgerPromoDisplayedBefore(Context context) { |
| 65 | return getSharedPreferences(context) |
yaolu | ff561a6 | 2016-11-17 12:22:10 -0800 | [diff] [blame] | 66 | .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED, false); |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | public static void setHamburgerPromoDisplayedBefore(Context context) { |
| 70 | getSharedPreferences(context).edit() |
yaolu | ff561a6 | 2016-11-17 12:22:10 -0800 | [diff] [blame] | 71 | .putBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_DISPLAYED, true) |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 72 | .apply(); |
James Laskey | bc0137f | 2016-12-06 09:05:51 -0800 | [diff] [blame] | 73 | new BackupManager(context).dataChanged(); |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | public static boolean getHamburgerMenuClickedBefore(Context context) { |
| 77 | return getSharedPreferences(context) |
yaolu | ff561a6 | 2016-11-17 12:22:10 -0800 | [diff] [blame] | 78 | .getBoolean(PREFERENCE_KEY_HAMBURGER_MENU_CLICKED, false); |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | public static void setHamburgerMenuClickedBefore(Context context) { |
| 82 | getSharedPreferences(context).edit() |
yaolu | ff561a6 | 2016-11-17 12:22:10 -0800 | [diff] [blame] | 83 | .putBoolean(PREFERENCE_KEY_HAMBURGER_MENU_CLICKED, true) |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 84 | .apply(); |
James Laskey | bc0137f | 2016-12-06 09:05:51 -0800 | [diff] [blame] | 85 | new BackupManager(context).dataChanged(); |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | public static boolean getHamburgerPromoTriggerActionHappenedBefore(Context context) { |
| 89 | return getSharedPreferences(context) |
yaolu | ff561a6 | 2016-11-17 12:22:10 -0800 | [diff] [blame] | 90 | .getBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED, false); |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | public static void setHamburgerPromoTriggerActionHappenedBefore(Context context) { |
| 94 | getSharedPreferences(context).edit() |
yaolu | ff561a6 | 2016-11-17 12:22:10 -0800 | [diff] [blame] | 95 | .putBoolean(PREFERENCE_KEY_HAMBURGER_PROMO_TRIGGER_ACTION_HAPPENED, true) |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 96 | .apply(); |
James Laskey | bc0137f | 2016-12-06 09:05:51 -0800 | [diff] [blame] | 97 | new BackupManager(context).dataChanged(); |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 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 | |
Marcus Hagerott | 596eb7e | 2016-10-18 10:25:12 -0700 | [diff] [blame] | 115 | protected static SharedPreferences getSharedPreferences(Context context) { |
James Laskey | bc0137f | 2016-12-06 09:05:51 -0800 | [diff] [blame] | 116 | return context.getSharedPreferences(getSharedPreferencesFilename(context), |
| 117 | Context.MODE_PRIVATE); |
| 118 | } |
| 119 | |
| 120 | public static String getSharedPreferencesFilename(Context context) { |
| 121 | return context.getPackageName(); |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 122 | } |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 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 | } |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 167 | |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 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) { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 172 | final String id = sim.getSimId(); |
| 173 | if (id == null) { |
| 174 | continue; |
| 175 | } |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 176 | if (sim.isImported()) { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 177 | imported.add(id); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 178 | } else { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 179 | imported.remove(id); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 180 | } |
| 181 | if (sim.isDismissed()) { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 182 | dismissed.add(id); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 183 | } else { |
Marcus Hagerott | 2aa3198 | 2016-10-25 14:36:25 -0700 | [diff] [blame] | 184 | dismissed.remove(id); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 187 | getSharedPreferences(context).edit() |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 188 | .putStringSet(PREFERENCE_KEY_IMPORTED_SIM_CARDS, imported) |
| 189 | .putStringSet(PREFERENCE_KEY_DISMISSED_SIM_CARDS, dismissed) |
| 190 | .apply(); |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 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) { |
John Shao | 3ed3af2 | 2016-10-18 18:31:29 -0700 | [diff] [blame] | 205 | return getSharedPreferences(context) |
| 206 | .getStringSet(PREFERENCE_KEY_IMPORTED_SIM_CARDS, Collections.<String>emptySet()); |
| 207 | } |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 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 | |
Gary Mai | 86f2771 | 2016-10-27 15:17:49 -0700 | [diff] [blame] | 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 | |
Sean Midford | dfd0b0d | 2016-11-01 15:05:33 -0700 | [diff] [blame] | 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(); |
James Laskey | bc0137f | 2016-12-06 09:05:51 -0800 | [diff] [blame] | 256 | new BackupManager(context).dataChanged(); |
Sean Midford | dfd0b0d | 2016-11-01 15:05:33 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 259 | public static void clear(Context context) { |
| 260 | getSharedPreferences(context).edit().clear().commit(); |
James Laskey | bc0137f | 2016-12-06 09:05:51 -0800 | [diff] [blame] | 261 | new BackupManager(context).dataChanged(); |
Marcus Hagerott | 66e8b22 | 2016-10-23 15:41:55 -0700 | [diff] [blame] | 262 | } |
Tingting Wang | ac9596e | 2016-08-02 22:24:24 -0700 | [diff] [blame] | 263 | } |