• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
42     /**
43      * Events that either have happened or have not (booleans).
44      */
45     @StringDef(value = {
46             HOME_BOUNCE_SEEN,
47             HOTSEAT_LONGPRESS_TIP_SEEN,
48             SEARCH_EDU_SEEN
49     })
50     @Retention(RetentionPolicy.SOURCE)
51     public @interface EventBoolKey {
52     }
53 
54     /**
55      * Events that occur multiple times, which we count up to a max defined in {@link #MAX_COUNTS}.
56      */
57     @StringDef(value = {
58             HOME_BOUNCE_COUNT,
59             HOTSEAT_DISCOVERY_TIP_COUNT,
60             SEARCH_SNACKBAR_COUNT
61     })
62     @Retention(RetentionPolicy.SOURCE)
63     public @interface EventCountKey {
64     }
65 
66     private static final Map<String, Integer> MAX_COUNTS;
67 
68     static {
69         Map<String, Integer> maxCounts = new ArrayMap<>(4);
maxCounts.put(HOME_BOUNCE_COUNT, 3)70         maxCounts.put(HOME_BOUNCE_COUNT, 3);
maxCounts.put(HOTSEAT_DISCOVERY_TIP_COUNT, 5)71         maxCounts.put(HOTSEAT_DISCOVERY_TIP_COUNT, 5);
maxCounts.put(SEARCH_SNACKBAR_COUNT, 3)72         maxCounts.put(SEARCH_SNACKBAR_COUNT, 3);
73         MAX_COUNTS = Collections.unmodifiableMap(maxCounts);
74     }
75 
76     protected final T mLauncher;
77     protected final SharedPreferences mSharedPrefs;
78 
OnboardingPrefs(T launcher, SharedPreferences sharedPrefs)79     public OnboardingPrefs(T launcher, SharedPreferences sharedPrefs) {
80         mLauncher = launcher;
81         mSharedPrefs = sharedPrefs;
82     }
83 
84     /** @return The number of times we have seen the given event. */
getCount(@ventCountKey String key)85     public int getCount(@EventCountKey String key) {
86         return mSharedPrefs.getInt(key, 0);
87     }
88 
89     /** @return Whether we have seen this event enough times, as defined by {@link #MAX_COUNTS}. */
hasReachedMaxCount(@ventCountKey String eventKey)90     public boolean hasReachedMaxCount(@EventCountKey String eventKey) {
91         return hasReachedMaxCount(getCount(eventKey), eventKey);
92     }
93 
hasReachedMaxCount(int count, @EventCountKey String eventKey)94     private boolean hasReachedMaxCount(int count, @EventCountKey String eventKey) {
95         return count >= MAX_COUNTS.get(eventKey);
96     }
97 
98     /** @return Whether we have seen the given event. */
getBoolean(@ventBoolKey String key)99     public boolean getBoolean(@EventBoolKey String key) {
100         return mSharedPrefs.getBoolean(key, false);
101     }
102 
103     /**
104      * Marks on-boarding preference boolean at true
105      */
markChecked(String flag)106     public void markChecked(String flag) {
107         mSharedPrefs.edit().putBoolean(flag, true).apply();
108     }
109 
110     /**
111      * Add 1 to the given event count, if we haven't already reached the max count.
112      *
113      * @return Whether we have now reached the max count.
114      */
incrementEventCount(@ventCountKey String eventKey)115     public boolean incrementEventCount(@EventCountKey String eventKey) {
116         int count = getCount(eventKey);
117         if (hasReachedMaxCount(count, eventKey)) {
118             return true;
119         }
120         count++;
121         mSharedPrefs.edit().putInt(eventKey, count).apply();
122         return hasReachedMaxCount(count, eventKey);
123     }
124 }
125