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.Context
19 import com.android.launcher3.LauncherPrefs
20 import com.android.launcher3.LauncherPrefs.Companion.backedUpItem
21 
22 /** Stores and retrieves onboarding-related data via SharedPreferences. */
23 object OnboardingPrefs {
24 
25     data class CountedItem(
26         val sharedPrefKey: String,
27         val maxCount: Int,
28     ) {
29         private val prefItem = backedUpItem(sharedPrefKey, 0)
30 
31         /** @return The number of times we have seen the given event. */
getnull32         fun get(c: Context): Int {
33             return prefItem.get(c)
34         }
35 
36         /** @return Whether we have seen this event enough times, as defined by [.MAX_COUNTS]. */
hasReachedMaxnull37         fun hasReachedMax(c: Context): Boolean {
38             return get(c) >= maxCount
39         }
40 
41         /**
42          * Add 1 to the given event count, if we haven't already reached the max count.
43          *
44          * @return Whether we have now reached the max count.
45          */
incrementnull46         fun increment(c: Context): Boolean {
47             val count = get(c)
48             if (count >= maxCount) {
49                 return true
50             }
51             return set(count + 1, c)
52         }
53 
54         /**
55          * Sets the event count to the given value.
56          *
57          * @return Whether we have now reached the max count.
58          */
setnull59         fun set(count: Int, c: Context): Boolean {
60             LauncherPrefs.get(c).put(prefItem, count)
61             return count >= maxCount
62         }
63     }
64 
65     @JvmField val TASKBAR_EDU_TOOLTIP_STEP = CountedItem("launcher.taskbar_edu_tooltip_step", 3)
66 
67     @JvmField val HOME_BOUNCE_COUNT = CountedItem("launcher.home_bounce_count", 3)
68 
69     @JvmField
70     val HOTSEAT_DISCOVERY_TIP_COUNT = CountedItem("launcher.hotseat_discovery_tip_count", 5)
71 
72     @JvmField val ALL_APPS_VISITED_COUNT = CountedItem("launcher.all_apps_visited_count", 20)
73 
74     @JvmField val HOME_BOUNCE_SEEN = backedUpItem("launcher.apps_view_shown", false)
75 
76     @JvmField
77     val HOTSEAT_LONGPRESS_TIP_SEEN = backedUpItem("launcher.hotseat_longpress_tip_seen", false)
78 
79     @JvmField val TASKBAR_SEARCH_EDU_SEEN = backedUpItem("launcher.taskbar_search_edu_seen", false)
80 }
81