WizardManagerHelper.java revision 53f9de0cc4b08886f1636cde9db67728ae9464f2
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
17package com.android.setupwizardlib.util;
18
19import android.content.Context;
20import android.content.Intent;
21import android.os.Build.VERSION;
22import android.os.Build.VERSION_CODES;
23import android.provider.Settings;
24import android.support.annotation.VisibleForTesting;
25
26public class WizardManagerHelper {
27
28    private static final String ACTION_NEXT = "com.android.wizard.NEXT";
29
30    // EXTRA_SCRIPT_URI and EXTRA_ACTION_ID are used in setup wizard in versions before M and are
31    // kept for backwards compatibility.
32    @VisibleForTesting
33    static final String EXTRA_SCRIPT_URI = "scriptUri";
34    @VisibleForTesting
35    static final String EXTRA_ACTION_ID = "actionId";
36
37    @VisibleForTesting
38    static final String EXTRA_WIZARD_BUNDLE = "wizardBundle";
39    private static final String EXTRA_RESULT_CODE = "com.android.setupwizard.ResultCode";
40    @VisibleForTesting
41    static final String EXTRA_IS_FIRST_RUN = "firstRun";
42
43    public static final String EXTRA_THEME = "theme";
44    public static final String EXTRA_USE_IMMERSIVE_MODE = "useImmersiveMode";
45
46    public static final String SETTINGS_GLOBAL_DEVICE_PROVISIONED = "device_provisioned";
47    public static final String SETTINGS_SECURE_USER_SETUP_COMPLETE = "user_setup_complete";
48
49    public static final String THEME_HOLO = "holo";
50    public static final String THEME_HOLO_LIGHT = "holo_light";
51    public static final String THEME_MATERIAL = "material";
52    public static final String THEME_MATERIAL_LIGHT = "material_light";
53
54    /**
55     * Passed in a setup wizard intent as {@link #EXTRA_THEME}. This is the dark variant of the
56     * theme used in setup wizard for Nougat MR1.
57     */
58    public static final String THEME_GLIF = "glif";
59
60    /**
61     * Passed in a setup wizard intent as {@link #EXTRA_THEME}. This is the default theme used in
62     * setup wizard for Nougat MR1.
63     */
64    public static final String THEME_GLIF_LIGHT = "glif_light";
65
66    /**
67     * Passed in a setup wizard intent as {@link #EXTRA_THEME}. This is the dark variant of the
68     * theme used in setup wizard for O DR.
69     */
70    public static final String THEME_GLIF_PIXEL = "glif_pixel";
71
72    /**
73     * Passed in a setup wizard intent as {@link #EXTRA_THEME}. This is the default theme used in
74     * setup wizard for O DR.
75     */
76    public static final String THEME_GLIF_PIXEL_LIGHT = "glif_pixel_light";
77
78    /**
79     * Get an intent that will invoke the next step of setup wizard.
80     *
81     * @param originalIntent The original intent that was used to start the step, usually via
82     *                       {@link android.app.Activity#getIntent()}.
83     * @param resultCode The result code of the step. See {@link ResultCodes}.
84     * @return A new intent that can be used with
85     *         {@link android.app.Activity#startActivityForResult(Intent, int)} to start the next
86     *         step of the setup flow.
87     */
88    public static Intent getNextIntent(Intent originalIntent, int resultCode) {
89        return getNextIntent(originalIntent, resultCode, null);
90    }
91
92    /**
93     * Get an intent that will invoke the next step of setup wizard.
94     *
95     * @param originalIntent The original intent that was used to start the step, usually via
96     *                       {@link android.app.Activity#getIntent()}.
97     * @param resultCode The result code of the step. See {@link ResultCodes}.
98     * @param data An intent containing extra result data.
99     * @return A new intent that can be used with
100     *         {@link android.app.Activity#startActivityForResult(Intent, int)} to start the next
101     *         step of the setup flow.
102     */
103    public static Intent getNextIntent(Intent originalIntent, int resultCode, Intent data) {
104        Intent intent = new Intent(ACTION_NEXT);
105        copyWizardManagerExtras(originalIntent, intent);
106        intent.putExtra(EXTRA_RESULT_CODE, resultCode);
107        if (data != null && data.getExtras() != null) {
108            intent.putExtras(data.getExtras());
109        }
110        intent.putExtra(EXTRA_THEME, originalIntent.getStringExtra(EXTRA_THEME));
111
112        return intent;
113    }
114
115    /**
116     * Copy the internal extras used by setup wizard from one intent to another. For low-level use
117     * only, such as when using {@link Intent#FLAG_ACTIVITY_FORWARD_RESULT} to relay to another
118     * intent.
119     *
120     * @param srcIntent Intent to get the wizard manager extras from.
121     * @param dstIntent Intent to copy the wizard manager extras to.
122     */
123    public static void copyWizardManagerExtras(Intent srcIntent, Intent dstIntent) {
124        dstIntent.putExtra(EXTRA_WIZARD_BUNDLE, srcIntent.getBundleExtra(EXTRA_WIZARD_BUNDLE));
125        dstIntent.putExtra(EXTRA_THEME, srcIntent.getStringExtra(EXTRA_THEME));
126        dstIntent.putExtra(EXTRA_IS_FIRST_RUN,
127                srcIntent.getBooleanExtra(EXTRA_IS_FIRST_RUN, false));
128        dstIntent.putExtra(EXTRA_SCRIPT_URI, srcIntent.getStringExtra(EXTRA_SCRIPT_URI));
129        dstIntent.putExtra(EXTRA_ACTION_ID, srcIntent.getStringExtra(EXTRA_ACTION_ID));
130    }
131
132    /**
133     * Check whether an intent is intended to be used within the setup wizard flow.
134     *
135     * @param intent The intent to be checked, usually from
136     *               {@link android.app.Activity#getIntent()}.
137     * @return true if the intent passed in was intended to be used with setup wizard.
138     */
139    public static boolean isSetupWizardIntent(Intent intent) {
140        return intent.getBooleanExtra(EXTRA_IS_FIRST_RUN, false);
141    }
142
143    /**
144     * Checks whether the current user has completed Setup Wizard. This is true if the current user
145     * has gone through Setup Wizard. The current user may or may not be the device owner and the
146     * device owner may have already completed setup wizard.
147     *
148     * @param context The context to retrieve the settings.
149     * @return true if the current user has completed Setup Wizard.
150     * @see #isDeviceProvisioned(android.content.Context)
151     */
152    public static boolean isUserSetupComplete(Context context) {
153        if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
154            return Settings.Secure.getInt(context.getContentResolver(),
155                    SETTINGS_SECURE_USER_SETUP_COMPLETE, 0) == 1;
156        } else {
157            // For versions below JB MR1, there are no user profiles. Just return the global device
158            // provisioned state.
159            return Settings.Secure.getInt(context.getContentResolver(),
160                    SETTINGS_GLOBAL_DEVICE_PROVISIONED, 0) == 1;
161        }
162    }
163
164    /**
165     * Checks whether the device is provisioned. This means that the device has gone through Setup
166     * Wizard at least once. Note that the user can still be in Setup Wizard even if this is true,
167     * for a secondary user profile triggered through Settings > Add account.
168     *
169     * @param context The context to retrieve the settings.
170     * @return true if the device is provisioned.
171     * @see #isUserSetupComplete(android.content.Context)
172     */
173    public static boolean isDeviceProvisioned(Context context) {
174        if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
175            return Settings.Global.getInt(context.getContentResolver(),
176                    SETTINGS_GLOBAL_DEVICE_PROVISIONED, 0) == 1;
177        } else {
178            return Settings.Secure.getInt(context.getContentResolver(),
179                    SETTINGS_GLOBAL_DEVICE_PROVISIONED, 0) == 1;
180        }
181    }
182
183    /**
184     * Checks the intent whether the extra indicates that the light theme should be used or not. If
185     * the theme is not specified in the intent, or the theme specified is unknown, the value def
186     * will be returned.
187     *
188     * @param intent The intent used to start the activity, which the theme extra will be read from.
189     * @param def The default value if the theme is not specified.
190     * @return True if the activity started by the given intent should use light theme.
191     */
192    public static boolean isLightTheme(Intent intent, boolean def) {
193        final String theme = intent.getStringExtra(EXTRA_THEME);
194        return isLightTheme(theme, def);
195    }
196
197    /**
198     * Checks whether {@code theme} represents a light or dark theme. If the theme specified is
199     * unknown, the value def will be returned.
200     *
201     * @param theme The theme as specified from an intent sent from setup wizard.
202     * @param def The default value if the theme is not known.
203     * @return True if {@code theme} represents a light theme.
204     */
205    public static boolean isLightTheme(String theme, boolean def) {
206        if (THEME_HOLO_LIGHT.equals(theme) || THEME_MATERIAL_LIGHT.equals(theme)
207                || THEME_GLIF_LIGHT.equals(theme) || THEME_GLIF_PIXEL_LIGHT.equals(theme)) {
208            return true;
209        } else if (THEME_HOLO.equals(theme) || THEME_MATERIAL.equals(theme)
210                || THEME_GLIF.equals(theme) || THEME_GLIF_PIXEL.equals(theme)) {
211            return false;
212        } else {
213            return def;
214        }
215    }
216}
217