1/*
2 * Copyright (C) 2016 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
18package android.support.v4.app;
19
20import android.app.Activity;
21import android.content.Context;
22import android.content.ContextWrapper;
23import android.content.Intent;
24import android.content.SharedPreferences;
25import android.os.Bundle;
26import android.support.v4.content.IntentCompat;
27import android.support.v4.content.SharedPreferencesCompat;
28
29/**
30 * This class provides APIs for determining how an app has been launched.
31 * This can be useful if you want to confirm that a user has launched your
32 * app through its front door activity from their launcher/home screen, rather
33 * than just if the app has been opened in the past in order to view a link,
34 * open a document or perform some other service for other apps on the device.
35 */
36public class AppLaunchChecker {
37    private static final String SHARED_PREFS_NAME = "android.support.AppLaunchChecker";
38    private static final String KEY_STARTED_FROM_LAUNCHER = "startedFromLauncher";
39
40    /**
41     * Checks if this app has been launched by the user from their launcher or home screen
42     * since it was installed.
43     *
44     * <p>To track this state properly you must call {@link #onActivityCreate(Activity)}
45     * in your launcher activity's {@link Activity#onCreate(Bundle)} method.</p>
46     *
47     * @param context Context to check
48     * @return true if this app has been started by the user from the launcher at least once
49     */
50    public static boolean hasStartedFromLauncher(Context context) {
51        return context.getSharedPreferences(SHARED_PREFS_NAME, 0)
52                .getBoolean(KEY_STARTED_FROM_LAUNCHER, false);
53    }
54
55    /**
56     * Records the parameters of an activity's launch for later use by the other
57     * methods available on this class.
58     *
59     * <p>Your app should call this method in your launcher activity's
60     * {@link Activity#onCreate(Bundle)} method to track launch state.
61     * If the app targets API 23 (Android 6.0 Marshmallow) or later, this state will be
62     * eligible for full data backup and may be restored to the user's device automatically.</p>     *
63     *
64     * @param activity the Activity currently running onCreate
65     */
66    public static void onActivityCreate(Activity activity) {
67        final SharedPreferences sp = activity.getSharedPreferences(SHARED_PREFS_NAME, 0);
68        if (sp.getBoolean(KEY_STARTED_FROM_LAUNCHER, false)) {
69            return;
70        }
71
72        final Intent launchIntent = activity.getIntent();
73        if (launchIntent == null) {
74            return;
75        }
76
77        if (Intent.ACTION_MAIN.equals(launchIntent.getAction())
78                && (launchIntent.hasCategory(Intent.CATEGORY_LAUNCHER)
79                || launchIntent.hasCategory(IntentCompat.CATEGORY_LEANBACK_LAUNCHER))) {
80            SharedPreferencesCompat.EditorCompat.getInstance().apply(
81                    sp.edit().putBoolean(KEY_STARTED_FROM_LAUNCHER, true));
82        }
83    }
84}
85