1 /* 2 * Copyright (C) 2017 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.car.setupwizardlib.util; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.provider.Settings; 22 23 /** 24 * <p>Derived from {@code com.android.setupwizardlib/WizardManagerHelper.java} 25 */ 26 public final class CarWizardManagerHelper { 27 public static final String EXTRA_WIZARD_BUNDLE = "wizardBundle"; 28 public static final String EXTRA_IS_FIRST_RUN = "firstRun"; 29 public static final String EXTRA_IS_DEALER = "dealer"; 30 public static final String EXTRA_IS_DEFERRED_SETUP = "deferredSetup"; 31 private static final String ACTION_NEXT = "com.android.wizard.NEXT"; 32 private static final String EXTRA_RESULT_CODE = "com.android.setupwizard.ResultCode"; 33 34 private CarWizardManagerHelper() { 35 } 36 37 /** 38 * Get an intent that will invoke the next step of setup wizard. 39 * 40 * @param originalIntent The original intent that was used to start the step, usually via 41 * {@link android.app.Activity#getIntent()}. 42 * @param resultCode The result code of the step. See {@link ResultCodes}. 43 * @return A new intent that can be used with 44 * {@link android.app.Activity#startActivityForResult(Intent, int)} to start the next 45 * step of the setup flow. 46 */ 47 public static Intent getNextIntent(Intent originalIntent, int resultCode) { 48 return getNextIntent(originalIntent, resultCode, null); 49 } 50 51 /** 52 * Get an intent that will invoke the next step of setup wizard. 53 * 54 * @param originalIntent The original intent that was used to start the step, usually via 55 * {@link android.app.Activity#getIntent()}. 56 * @param resultCode The result code of the step. See {@link ResultCodes}. 57 * @param data An intent containing extra result data. 58 * @return A new intent that can be used with 59 * {@link android.app.Activity#startActivityForResult(Intent, int)} to start the next 60 * step of the setup flow. 61 */ 62 public static Intent getNextIntent(Intent originalIntent, int resultCode, Intent data) { 63 Intent intent = new Intent(ACTION_NEXT); 64 copyWizardManagerExtras(originalIntent, intent); 65 intent.putExtra(EXTRA_RESULT_CODE, resultCode); 66 if (data != null && data.getExtras() != null) { 67 intent.putExtras(data.getExtras()); 68 } 69 70 return intent; 71 } 72 73 /** 74 * Copy the internal extras used by setup wizard from one intent to another. For low-level use 75 * only, such as when using {@link Intent#FLAG_ACTIVITY_FORWARD_RESULT} to relay to another 76 * intent. 77 * 78 * @param srcIntent Intent to get the wizard manager extras from. 79 * @param dstIntent Intent to copy the wizard manager extras to. 80 */ 81 public static void copyWizardManagerExtras(Intent srcIntent, Intent dstIntent) { 82 dstIntent.putExtra(EXTRA_WIZARD_BUNDLE, srcIntent.getBundleExtra(EXTRA_WIZARD_BUNDLE)); 83 dstIntent.putExtra(EXTRA_IS_FIRST_RUN, 84 srcIntent.getBooleanExtra(EXTRA_IS_FIRST_RUN, false)); 85 dstIntent.putExtra(EXTRA_IS_DEALER, 86 srcIntent.getBooleanExtra(EXTRA_IS_DEALER, false)); 87 dstIntent.putExtra(EXTRA_IS_DEFERRED_SETUP, 88 srcIntent.getBooleanExtra(EXTRA_IS_DEFERRED_SETUP, false)); 89 } 90 91 /** 92 * Check whether an intent is intended to be used within the setup wizard flow. 93 * 94 * @param intent The intent to be checked, usually from 95 * {@link android.app.Activity#getIntent()}. 96 * @return true if the intent passed in was intended to be used with setup wizard. 97 */ 98 public static boolean isSetupWizardIntent(Intent intent) { 99 return intent.getBooleanExtra(EXTRA_IS_FIRST_RUN, false); 100 } 101 102 /** 103 * Check whether an intent is intended for the dealer. 104 * 105 * @param intent The intent to be checked, usually from 106 * {@link android.app.Activity#getIntent()}. 107 * @return true if the intent passed in was intended to be used with setup wizard. 108 */ 109 public static boolean isDealerIntent(Intent intent) { 110 return intent.getBooleanExtra(EXTRA_IS_DEALER, false); 111 } 112 113 /** 114 * Checks whether the current user has completed Setup Wizard. This is true if the current user 115 * has gone through Setup Wizard. The current user may or may not be the device owner and the 116 * device owner may have already completed setup wizard. 117 * 118 * @param context The context to retrieve the settings. 119 * @return true if the current user has completed Setup Wizard. 120 * @see #isDeviceProvisioned(Context) 121 */ 122 public static boolean isUserSetupComplete(Context context) { 123 return Settings.Secure.getInt(context.getContentResolver(), 124 Settings.Secure.USER_SETUP_COMPLETE, 0) == 1; 125 } 126 127 /** 128 * Checks whether the device is provisioned. This means that the device has gone through Setup 129 * Wizard at least once. Note that the user can still be in Setup Wizard even if this is true, 130 * for a secondary user profile triggered through Settings > Add account. 131 * 132 * @param context The context to retrieve the settings. 133 * @return true if the device is provisioned. 134 * @see #isUserSetupComplete(Context) 135 */ 136 public static boolean isDeviceProvisioned(Context context) { 137 return Settings.Global.getInt(context.getContentResolver(), 138 Settings.Global.DEVICE_PROVISIONED, 0) == 1; 139 } 140 } 141