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