Home | History | Annotate | Download | only in util
      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 
     17 package com.android.setupwizardlib.util;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.os.Build.VERSION;
     22 import android.os.Build.VERSION_CODES;
     23 import android.provider.Settings;
     24 
     25 public 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 dark variant of the
     69      * theme used in setup wizard for NYC.
     70      */
     71     public static final String THEME_GLIF = "glif";
     72 
     73     /**
     74      * Passed in a setup wizard intent as {@link #EXTRA_THEME}. This is the default theme used in
     75      * setup wizard for NYC.
     76      */
     77     public static final String THEME_GLIF_LIGHT = "glif_light";
     78 
     79     /**
     80      * Get an intent that will invoke the next step of setup wizard.
     81      *
     82      * @param originalIntent The original intent that was used to start the step, usually via
     83      *                       {@link android.app.Activity#getIntent()}.
     84      * @param resultCode The result code of the step. See {@link ResultCodes}.
     85      * @return A new intent that can be used with
     86      *         {@link android.app.Activity#startActivityForResult(Intent, int)} to start the next
     87      *         step of the setup flow.
     88      */
     89     public static Intent getNextIntent(Intent originalIntent, int resultCode) {
     90         return getNextIntent(originalIntent, resultCode, null);
     91     }
     92 
     93     /**
     94      * Get an intent that will invoke the next step of setup wizard.
     95      *
     96      * @param originalIntent The original intent that was used to start the step, usually via
     97      *                       {@link android.app.Activity#getIntent()}.
     98      * @param resultCode The result code of the step. See {@link ResultCodes}.
     99      * @param data An intent containing extra result data.
    100      * @return A new intent that can be used with
    101      *         {@link android.app.Activity#startActivityForResult(Intent, int)} to start the next
    102      *         step of the setup flow.
    103      */
    104     public static Intent getNextIntent(Intent originalIntent, int resultCode, Intent data) {
    105         Intent intent = new Intent(ACTION_NEXT);
    106         copyWizardManagerExtras(originalIntent, intent);
    107         intent.putExtra(EXTRA_RESULT_CODE, resultCode);
    108         if (data != null && data.getExtras() != null) {
    109             intent.putExtras(data.getExtras());
    110         }
    111         intent.putExtra(EXTRA_THEME, originalIntent.getStringExtra(EXTRA_THEME));
    112 
    113         return intent;
    114     }
    115 
    116     /**
    117      * Copy the internal extras used by setup wizard from one intent to another. For low-level use
    118      * only, such as when using {@link Intent#FLAG_ACTIVITY_FORWARD_RESULT} to relay to another
    119      * intent.
    120      *
    121      * @param srcIntent Intent to get the wizard manager extras from.
    122      * @param dstIntent Intent to copy the wizard manager extras to.
    123      */
    124     public static void copyWizardManagerExtras(Intent srcIntent, Intent dstIntent) {
    125         dstIntent.putExtra(EXTRA_WIZARD_BUNDLE, srcIntent.getBundleExtra(EXTRA_WIZARD_BUNDLE));
    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.ICE_CREAM_SANDWICH) {
    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_MATERIAL_BLUE_LIGHT.equals(theme) || THEME_GLIF_LIGHT.equals(theme)) {
    208             return true;
    209         } else if (THEME_HOLO.equals(theme) || THEME_MATERIAL.equals(theme)
    210                 || THEME_MATERIAL_BLUE.equals(theme) || THEME_GLIF.equals(theme)) {
    211             return false;
    212         } else {
    213             return def;
    214         }
    215     }
    216 }
    217