1 /* 2 * Copyright (C) 2020 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 package com.android.launcher3.util; 17 18 import android.content.SharedPreferences; 19 import android.util.ArrayMap; 20 21 import androidx.annotation.StringDef; 22 23 import com.android.launcher3.Launcher; 24 25 import java.lang.annotation.Retention; 26 import java.lang.annotation.RetentionPolicy; 27 import java.util.Collections; 28 import java.util.Map; 29 30 /** 31 * Stores and retrieves onboarding-related data via SharedPreferences. 32 */ 33 public class OnboardingPrefs<T extends Launcher> { 34 35 public static final String HOME_BOUNCE_SEEN = "launcher.apps_view_shown"; 36 public static final String HOME_BOUNCE_COUNT = "launcher.home_bounce_count"; 37 public static final String HOTSEAT_DISCOVERY_TIP_COUNT = "launcher.hotseat_discovery_tip_count"; 38 public static final String HOTSEAT_LONGPRESS_TIP_SEEN = "launcher.hotseat_longpress_tip_seen"; 39 public static final String SEARCH_EDU_SEEN = "launcher.search_edu_seen"; 40 public static final String SEARCH_SNACKBAR_COUNT = "launcher.keyboard_snackbar_count"; 41 public static final String TASKBAR_EDU_SEEN = "launcher.taskbar_edu_seen"; 42 // When adding a new key, add it here as well, to be able to reset it from Developer Options. 43 public static final Map<String, String[]> ALL_PREF_KEYS = Map.of( 44 "All Apps Bounce", new String[] { HOME_BOUNCE_SEEN, HOME_BOUNCE_COUNT }, 45 "Hybrid Hotseat Education", new String[] { HOTSEAT_DISCOVERY_TIP_COUNT, 46 HOTSEAT_LONGPRESS_TIP_SEEN }, 47 "Search Education", new String[] { SEARCH_EDU_SEEN, SEARCH_SNACKBAR_COUNT }, 48 "Taskbar Education", new String[] { TASKBAR_EDU_SEEN } 49 ); 50 51 /** 52 * Events that either have happened or have not (booleans). 53 */ 54 @StringDef(value = { 55 HOME_BOUNCE_SEEN, 56 HOTSEAT_LONGPRESS_TIP_SEEN, 57 SEARCH_EDU_SEEN, 58 TASKBAR_EDU_SEEN 59 }) 60 @Retention(RetentionPolicy.SOURCE) 61 public @interface EventBoolKey { 62 } 63 64 /** 65 * Events that occur multiple times, which we count up to a max defined in {@link #MAX_COUNTS}. 66 */ 67 @StringDef(value = { 68 HOME_BOUNCE_COUNT, 69 HOTSEAT_DISCOVERY_TIP_COUNT, 70 SEARCH_SNACKBAR_COUNT 71 }) 72 @Retention(RetentionPolicy.SOURCE) 73 public @interface EventCountKey { 74 } 75 76 private static final Map<String, Integer> MAX_COUNTS; 77 78 static { 79 Map<String, Integer> maxCounts = new ArrayMap<>(4); maxCounts.put(HOME_BOUNCE_COUNT, 3)80 maxCounts.put(HOME_BOUNCE_COUNT, 3); maxCounts.put(HOTSEAT_DISCOVERY_TIP_COUNT, 5)81 maxCounts.put(HOTSEAT_DISCOVERY_TIP_COUNT, 5); maxCounts.put(SEARCH_SNACKBAR_COUNT, 3)82 maxCounts.put(SEARCH_SNACKBAR_COUNT, 3); 83 MAX_COUNTS = Collections.unmodifiableMap(maxCounts); 84 } 85 86 protected final T mLauncher; 87 protected final SharedPreferences mSharedPrefs; 88 OnboardingPrefs(T launcher, SharedPreferences sharedPrefs)89 public OnboardingPrefs(T launcher, SharedPreferences sharedPrefs) { 90 mLauncher = launcher; 91 mSharedPrefs = sharedPrefs; 92 } 93 94 /** @return The number of times we have seen the given event. */ getCount(@ventCountKey String key)95 public int getCount(@EventCountKey String key) { 96 return mSharedPrefs.getInt(key, 0); 97 } 98 99 /** @return Whether we have seen this event enough times, as defined by {@link #MAX_COUNTS}. */ hasReachedMaxCount(@ventCountKey String eventKey)100 public boolean hasReachedMaxCount(@EventCountKey String eventKey) { 101 return hasReachedMaxCount(getCount(eventKey), eventKey); 102 } 103 hasReachedMaxCount(int count, @EventCountKey String eventKey)104 private boolean hasReachedMaxCount(int count, @EventCountKey String eventKey) { 105 return count >= MAX_COUNTS.get(eventKey); 106 } 107 108 /** @return Whether we have seen the given event. */ getBoolean(@ventBoolKey String key)109 public boolean getBoolean(@EventBoolKey String key) { 110 return mSharedPrefs.getBoolean(key, false); 111 } 112 113 /** 114 * Marks on-boarding preference boolean at true 115 */ markChecked(String flag)116 public void markChecked(String flag) { 117 mSharedPrefs.edit().putBoolean(flag, true).apply(); 118 } 119 120 /** 121 * Add 1 to the given event count, if we haven't already reached the max count. 122 * 123 * @return Whether we have now reached the max count. 124 */ incrementEventCount(@ventCountKey String eventKey)125 public boolean incrementEventCount(@EventCountKey String eventKey) { 126 int count = getCount(eventKey); 127 if (hasReachedMaxCount(count, eventKey)) { 128 return true; 129 } 130 count++; 131 mSharedPrefs.edit().putInt(eventKey, count).apply(); 132 return hasReachedMaxCount(count, eventKey); 133 } 134 } 135