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