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.ContentResolver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.database.Cursor;
23 import android.media.tv.TvContract.Channels;
24 import android.net.Uri;
25 import android.preference.PreferenceManager;
26 import android.support.annotation.UiThread;
27 
28 import com.android.tv.TvApplication;
29 import com.android.tv.data.ChannelDataManager;
30 
31 /**
32  * A utility class related to onboarding experience.
33  */
34 public final class OnboardingUtils {
35     private static final String PREF_KEY_IS_FIRST_BOOT = "pref_onbaording_is_first_boot";
36     private static final String PREF_KEY_ONBOARDING_VERSION_CODE = "pref_onbaording_versionCode";
37     private static final int ONBOARDING_VERSION = 1;
38 
39     private static final String MERCHANT_COLLECTION_URL_STRING = getMerchantCollectionUrl();
40 
41     /**
42      * Intent to show merchant collection in online store.
43      */
44     public static final Intent ONLINE_STORE_INTENT = new Intent(Intent.ACTION_VIEW,
45             Uri.parse(MERCHANT_COLLECTION_URL_STRING));
46 
47     /**
48      * Checks if this is the first boot after the onboarding experience has been applied.
49      */
isFirstBoot(Context context)50     public static boolean isFirstBoot(Context context) {
51         return PreferenceManager.getDefaultSharedPreferences(context)
52                 .getBoolean(PREF_KEY_IS_FIRST_BOOT, true);
53     }
54 
55     /**
56      * Marks that the first boot has been completed.
57      */
setFirstBootCompleted(Context context)58     public static void setFirstBootCompleted(Context context) {
59         PreferenceManager.getDefaultSharedPreferences(context)
60                 .edit()
61                 .putBoolean(PREF_KEY_IS_FIRST_BOOT, false)
62                 .apply();
63     }
64 
65     /**
66      * Checks if this is the first run of {@link com.android.tv.MainActivity} with the
67      * current onboarding version.
68      */
isFirstRunWithCurrentVersion(Context context)69     public static boolean isFirstRunWithCurrentVersion(Context context) {
70         int versionCode = PreferenceManager.getDefaultSharedPreferences(context)
71                 .getInt(PREF_KEY_ONBOARDING_VERSION_CODE, 0);
72         return versionCode != ONBOARDING_VERSION;
73     }
74 
75     /**
76      * Marks that the first run of {@link com.android.tv.MainActivity} with the current
77      * onboarding version has been completed.
78      */
setFirstRunWithCurrentVersionCompleted(Context context)79     public static void setFirstRunWithCurrentVersionCompleted(Context context) {
80         PreferenceManager.getDefaultSharedPreferences(context).edit()
81                 .putInt(PREF_KEY_ONBOARDING_VERSION_CODE, ONBOARDING_VERSION).apply();
82     }
83 
84     /**
85      * Checks whether the onboarding screen should be shown or not.
86      */
needToShowOnboarding(Context context)87     public static boolean needToShowOnboarding(Context context) {
88         return isFirstRunWithCurrentVersion(context) || !areChannelsAvailable(context);
89     }
90 
91     /**
92      * Checks if there are any available tuner channels.
93      */
94     @UiThread
areChannelsAvailable(Context context)95     public static boolean areChannelsAvailable(Context context) {
96         ChannelDataManager manager = TvApplication.getSingletons(context).getChannelDataManager();
97         if (manager.isDbLoadFinished()) {
98             return manager.getChannelCount() != 0;
99         }
100         // This method should block the UI thread.
101         ContentResolver resolver = context.getContentResolver();
102         try (Cursor c = resolver.query(Channels.CONTENT_URI, new String[] {Channels._ID}, null,
103                 null, null)) {
104             return c != null && c.getCount() != 0;
105         }
106     }
107 
108     /**
109      * Checks if there are any available TV inputs.
110      */
areInputsAvailable(Context context)111     public static boolean areInputsAvailable(Context context) {
112         return TvApplication.getSingletons(context).getTvInputManagerHelper()
113                 .getTvInputInfos(true, false).size() > 0;
114     }
115 
116     /**
117      * Returns merchant collection URL.
118      */
getMerchantCollectionUrl()119     private static String getMerchantCollectionUrl() {
120         return "TODO: add a merchant collection url";
121     }
122 }
123