summaryrefslogtreecommitdiff
path: root/src/com/android/launcher3/util/OnboardingPrefs.java
blob: 348c8d8a4c29f1a074e5a5e4ec44c2a7ca9b22f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.util;

import android.content.SharedPreferences;
import android.util.ArrayMap;

import androidx.annotation.StringDef;

import com.android.launcher3.views.ActivityContext;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.Map;

/**
 * Stores and retrieves onboarding-related data via SharedPreferences.
 *
 * @param <T> Context which owns these preferences.
 */
public class OnboardingPrefs<T extends ActivityContext> {

    public static final String HOME_BOUNCE_SEEN = "launcher.apps_view_shown";
    public static final String HOME_BOUNCE_COUNT = "launcher.home_bounce_count";
    public static final String HOTSEAT_DISCOVERY_TIP_COUNT = "launcher.hotseat_discovery_tip_count";
    public static final String HOTSEAT_LONGPRESS_TIP_SEEN = "launcher.hotseat_longpress_tip_seen";
    public static final String SEARCH_KEYBOARD_EDU_SEEN = "launcher.search_edu_seen";
    public static final String SEARCH_SNACKBAR_COUNT = "launcher.keyboard_snackbar_count";
    public static final String ALL_APPS_VISITED_COUNT = "launcher.all_apps_visited_count";
    public static final String TASKBAR_EDU_TOOLTIP_STEP = "launcher.taskbar_edu_tooltip_step";
    // When adding a new key, add it here as well, to be able to reset it from Developer Options.
    public static final Map<String, String[]> ALL_PREF_KEYS = Map.of(
            "All Apps Bounce", new String[] { HOME_BOUNCE_SEEN, HOME_BOUNCE_COUNT },
            "Hybrid Hotseat Education", new String[] { HOTSEAT_DISCOVERY_TIP_COUNT,
                    HOTSEAT_LONGPRESS_TIP_SEEN },
            "Search Education", new String[] { SEARCH_KEYBOARD_EDU_SEEN, SEARCH_SNACKBAR_COUNT},
            "Taskbar Education", new String[] { TASKBAR_EDU_TOOLTIP_STEP },
            "All Apps Visited Count", new String[] {ALL_APPS_VISITED_COUNT}
    );

    /**
     * Events that either have happened or have not (booleans).
     */
    @StringDef(value = {
            HOME_BOUNCE_SEEN,
            HOTSEAT_LONGPRESS_TIP_SEEN,
            SEARCH_KEYBOARD_EDU_SEEN,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface EventBoolKey {}

    /**
     * Events that occur multiple times, which we count up to a max defined in {@link #MAX_COUNTS}.
     */
    @StringDef(value = {
            HOME_BOUNCE_COUNT,
            HOTSEAT_DISCOVERY_TIP_COUNT,
            SEARCH_SNACKBAR_COUNT,
            ALL_APPS_VISITED_COUNT,
            TASKBAR_EDU_TOOLTIP_STEP,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface EventCountKey {}

    private static final Map<String, Integer> MAX_COUNTS;

    static {
        Map<String, Integer> maxCounts = new ArrayMap<>(5);
        maxCounts.put(HOME_BOUNCE_COUNT, 3);
        maxCounts.put(HOTSEAT_DISCOVERY_TIP_COUNT, 5);
        maxCounts.put(SEARCH_SNACKBAR_COUNT, 3);
        maxCounts.put(ALL_APPS_VISITED_COUNT, 20);
        maxCounts.put(TASKBAR_EDU_TOOLTIP_STEP, 2);
        MAX_COUNTS = Collections.unmodifiableMap(maxCounts);
    }

    protected final T mLauncher;
    protected final SharedPreferences mSharedPrefs;

    public OnboardingPrefs(T launcher, SharedPreferences sharedPrefs) {
        mLauncher = launcher;
        mSharedPrefs = sharedPrefs;
    }

    /** @return The number of times we have seen the given event. */
    public int getCount(@EventCountKey String key) {
        return mSharedPrefs.getInt(key, 0);
    }

    /** @return Whether we have seen this event enough times, as defined by {@link #MAX_COUNTS}. */
    public boolean hasReachedMaxCount(@EventCountKey String eventKey) {
        return hasReachedMaxCount(getCount(eventKey), eventKey);
    }

    private boolean hasReachedMaxCount(int count, @EventCountKey String eventKey) {
        return count >= MAX_COUNTS.get(eventKey);
    }

    /** @return Whether we have seen the given event. */
    public boolean getBoolean(@EventBoolKey String key) {
        return mSharedPrefs.getBoolean(key, false);
    }

    /**
     * Marks on-boarding preference boolean at true
     */
    public void markChecked(String flag) {
        mSharedPrefs.edit().putBoolean(flag, true).apply();
    }

    /**
     * Add 1 to the given event count, if we haven't already reached the max count.
     *
     * @return Whether we have now reached the max count.
     */
    public boolean incrementEventCount(@EventCountKey String eventKey) {
        int count = getCount(eventKey);
        if (hasReachedMaxCount(count, eventKey)) {
            return true;
        }
        count++;
        mSharedPrefs.edit().putInt(eventKey, count).apply();
        return hasReachedMaxCount(count, eventKey);
    }

    /**
     * Sets the event count to the given value.
     *
     * @return Whether we have now reached the max count.
     */
    public boolean setEventCount(int count, @EventCountKey String eventKey) {
        mSharedPrefs.edit().putInt(eventKey, count).apply();
        return hasReachedMaxCount(count, eventKey);
    }
}