1 /*
2  * Copyright (C) 2015 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 
17 package com.android.tv.util;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.preference.PreferenceManager;
23 import android.support.annotation.Nullable;
24 import android.text.TextUtils;
25 
26 import com.android.tv.common.flags.UiFlags;
27 
28 /** A utility class related to onboarding experience. */
29 public final class OnboardingUtils {
30     private static final String PREF_KEY_IS_FIRST_BOOT = "pref_onbaording_is_first_boot";
31     private static final String PREF_KEY_ONBOARDING_VERSION_CODE = "pref_onbaording_versionCode";
32     private static final int ONBOARDING_VERSION = 1;
33 
34     @Nullable
createOnlineStoreIntent(UiFlags uiFlags)35     public static Intent createOnlineStoreIntent(UiFlags uiFlags) {
36         String uriString = uiFlags.moreChannelsUrl();
37         return TextUtils.isEmpty(uriString)
38                 ? null
39                 : new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
40     }
41 
42     /** Checks if this is the first boot after the onboarding experience has been applied. */
isFirstBoot(Context context)43     public static boolean isFirstBoot(Context context) {
44         return PreferenceManager.getDefaultSharedPreferences(context)
45                 .getBoolean(PREF_KEY_IS_FIRST_BOOT, true);
46     }
47 
48     /** Marks that the first boot has been completed. */
setFirstBootCompleted(Context context)49     public static void setFirstBootCompleted(Context context) {
50         PreferenceManager.getDefaultSharedPreferences(context)
51                 .edit()
52                 .putBoolean(PREF_KEY_IS_FIRST_BOOT, false)
53                 .apply();
54     }
55 
56     /**
57      * Checks if this is the first run of {@link com.android.tv.MainActivity} with the current
58      * onboarding version.
59      */
isFirstRunWithCurrentVersion(Context context)60     public static boolean isFirstRunWithCurrentVersion(Context context) {
61         int versionCode =
62                 PreferenceManager.getDefaultSharedPreferences(context)
63                         .getInt(PREF_KEY_ONBOARDING_VERSION_CODE, 0);
64         return versionCode != ONBOARDING_VERSION;
65     }
66 
67     /**
68      * Marks that the first run of {@link com.android.tv.MainActivity} with the current onboarding
69      * version has been completed.
70      */
setFirstRunWithCurrentVersionCompleted(Context context)71     public static void setFirstRunWithCurrentVersionCompleted(Context context) {
72         PreferenceManager.getDefaultSharedPreferences(context)
73                 .edit()
74                 .putInt(PREF_KEY_ONBOARDING_VERSION_CODE, ONBOARDING_VERSION)
75                 .apply();
76     }
77 
OnboardingUtils()78     private OnboardingUtils() {}
79 }
80