1 /*
2  * Copyright (C) 2014 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.settings;
18 
19 import com.android.settings.widget.SetupWizardIllustration;
20 import com.android.setupwizard.navigationbar.SetupWizardNavBar;
21 
22 
23 import android.app.Activity;
24 import android.content.Intent;
25 import android.graphics.Color;
26 import android.graphics.drawable.BitmapDrawable;
27 import android.graphics.drawable.Drawable;
28 import android.graphics.drawable.LayerDrawable;
29 import android.view.Gravity;
30 import android.view.Window;
31 import android.widget.TextView;
32 
33 public class SetupWizardUtils {
34     private static final String TAG = "SetupWizardUtils";
35 
36     // Extra containing the resource name of the theme to be used
37     public static final String EXTRA_THEME = "theme";
38     public static final String THEME_HOLO = "holo";
39     public static final String THEME_HOLO_LIGHT = "holo_light";
40     public static final String THEME_MATERIAL = "material";
41     public static final String THEME_MATERIAL_LIGHT = "material_light";
42 
43     public static final String EXTRA_USE_IMMERSIVE_MODE = "useImmersiveMode";
44 
45     // From WizardManager (must match constants maintained there)
46     public static final String ACTION_NEXT = "com.android.wizard.NEXT";
47     public static final String EXTRA_SCRIPT_URI = "scriptUri";
48     public static final String EXTRA_ACTION_ID = "actionId";
49     public static final String EXTRA_RESULT_CODE = "com.android.setupwizard.ResultCode";
50     public static final int NEXT_REQUEST = 10000;
51 
isUsingWizardManager(Activity activity)52     public static boolean isUsingWizardManager(Activity activity) {
53         return activity.getIntent().hasExtra(EXTRA_SCRIPT_URI);
54     }
55 
56     /**
57      * Send the results of this activity to WizardManager, which will then send out the next
58      * scripted activity. WizardManager does not actually return an activity result, but if we
59      * invoke WizardManager without requesting a result, the framework will choose not to issue a
60      * call to onActivityResult with RESULT_CANCELED when navigating backward.
61      */
sendResultsToSetupWizard(Activity activity, int resultCode)62     public static void sendResultsToSetupWizard(Activity activity, int resultCode) {
63         final Intent intent = activity.getIntent();
64         final Intent nextIntent = new Intent(ACTION_NEXT);
65         nextIntent.putExtra(EXTRA_SCRIPT_URI, intent.getStringExtra(EXTRA_SCRIPT_URI));
66         nextIntent.putExtra(EXTRA_ACTION_ID, intent.getStringExtra(EXTRA_ACTION_ID));
67         nextIntent.putExtra(EXTRA_THEME, intent.getStringExtra(EXTRA_THEME));
68         nextIntent.putExtra(EXTRA_RESULT_CODE, resultCode);
69         activity.startActivityForResult(nextIntent, NEXT_REQUEST);
70     }
71 
getTheme(Intent intent, int defaultResId)72     public static int getTheme(Intent intent, int defaultResId) {
73         final String themeName = intent.getStringExtra(EXTRA_THEME);
74         int resid = defaultResId;
75         if (THEME_HOLO_LIGHT.equalsIgnoreCase(themeName) ||
76                 THEME_MATERIAL_LIGHT.equalsIgnoreCase(themeName)) {
77             resid = R.style.SetupWizardTheme_Light;
78         } else if (THEME_HOLO.equalsIgnoreCase(themeName) ||
79                 THEME_MATERIAL.equalsIgnoreCase(themeName)) {
80             resid = R.style.SetupWizardTheme;
81         }
82         return resid;
83     }
84 
85     /**
86      * Sets the immersive mode related flags based on the extra in the intent which started the
87      * activity.
88      */
setImmersiveMode(Activity activity, SetupWizardNavBar navBar)89     public static void setImmersiveMode(Activity activity, SetupWizardNavBar navBar) {
90         final boolean useImmersiveMode =
91                 activity.getIntent().getBooleanExtra(EXTRA_USE_IMMERSIVE_MODE, false);
92         navBar.setUseImmersiveMode(useImmersiveMode);
93         if (useImmersiveMode) {
94             final Window window = activity.getWindow();
95             window.setNavigationBarColor(Color.TRANSPARENT);
96             window.setStatusBarColor(Color.TRANSPARENT);
97         }
98     }
99 
getHeader(Activity activity)100     public static TextView getHeader(Activity activity) {
101         return (TextView) activity.findViewById(R.id.title);
102     }
103 
setHeaderText(Activity activity, int text)104     public static void setHeaderText(Activity activity, int text) {
105         getHeader(activity).setText(text);
106     }
107 
setHeaderText(Activity activity, CharSequence text)108     public static void setHeaderText(Activity activity, CharSequence text) {
109         getHeader(activity).setText(text);
110     }
111 
copySetupExtras(Intent fromIntent, Intent toIntent)112     public static void copySetupExtras(Intent fromIntent, Intent toIntent) {
113         toIntent.putExtra(EXTRA_THEME, fromIntent.getStringExtra(EXTRA_THEME));
114         toIntent.putExtra(EXTRA_USE_IMMERSIVE_MODE,
115                 fromIntent.getBooleanExtra(EXTRA_USE_IMMERSIVE_MODE, false));
116     }
117 
setIllustration(Activity activity, int asset)118     public static void setIllustration(Activity activity, int asset) {
119         SetupWizardIllustration illustration =
120                 (SetupWizardIllustration) activity.findViewById(R.id.setup_illustration);
121         if (illustration != null) {
122             Drawable drawable = activity.getDrawable(R.drawable.setup_illustration);
123             Drawable newIllustration = activity.getDrawable(asset);
124             if (drawable instanceof LayerDrawable) {
125                 LayerDrawable layers = (LayerDrawable) drawable;
126                 Drawable oldIllustration = layers.findDrawableByLayerId(R.id.illustration_image);
127                 if (newIllustration instanceof BitmapDrawable
128                         && oldIllustration instanceof BitmapDrawable) {
129                     final int gravity = ((BitmapDrawable) oldIllustration).getGravity();
130                     ((BitmapDrawable) newIllustration).setGravity(gravity);
131                 }
132                 layers.setDrawableByLayerId(R.id.illustration_image, newIllustration);
133                 illustration.setForeground(layers);
134             }
135         }
136     }
137 }
138