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