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