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