1 /*
2  * Copyright 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.managedprovisioning;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.res.ColorStateList;
22 import android.content.res.Resources;
23 import android.graphics.Canvas;
24 import android.graphics.Color;
25 import android.graphics.ColorFilter;
26 import android.graphics.Paint;
27 import android.graphics.PixelFormat;
28 import android.graphics.drawable.ColorDrawable;
29 import android.graphics.drawable.Drawable;
30 import android.util.DisplayMetrics;
31 import android.util.TypedValue;
32 import android.view.View;
33 import android.view.Window;
34 import android.view.WindowManager;
35 import android.widget.Button;
36 import android.widget.ImageView;
37 import android.widget.TextView;
38 
39 import com.android.managedprovisioning.common.Utils;
40 import com.android.setupwizardlib.SetupWizardLayout;
41 import com.android.setupwizardlib.util.SystemBarHelper;
42 import com.android.setupwizardlib.view.NavigationBar;
43 import com.android.setupwizardlib.view.NavigationBar.NavigationBarListener;
44 
45 /**
46  * Base class for setting up the layout.
47  */
48 public abstract class SetupLayoutActivity extends Activity implements NavigationBarListener {
49     protected final Utils mUtils = new Utils();
50 
51     protected Button mNextButton;
52     protected Button mBackButton;
53 
54     public static final int NEXT_BUTTON_EMPTY_LABEL = 0;
55 
maybeSetLogoAndMainColor(Integer mainColor)56     protected void maybeSetLogoAndMainColor(Integer mainColor) {
57         // null means the default value
58         if (mainColor == null) {
59             mainColor = getResources().getColor(R.color.orange);
60         }
61         // We should always use a value of 255 for the alpha.
62         mainColor = Color.argb(255, Color.red(mainColor), Color.green(mainColor),
63                 Color.blue(mainColor));
64         Window window = getWindow();
65         window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
66         window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
67 
68         SetupWizardLayout layout = (SetupWizardLayout) findViewById(R.id.setup_wizard_layout);
69 
70         layout.setIllustration(new HeaderDrawable(this, mainColor));
71         layout.setLayoutBackground(new ColorDrawable(mainColor));
72         layout.setProgressBarColor(ColorStateList.valueOf(mainColor));
73 
74         final TextView titleView = (TextView) findViewById(R.id.suw_layout_title);
75         if (mUtils.isBrightColor(mainColor)) {
76             titleView.setTextColor(Color.BLACK);
77         } else {
78             titleView.setTextColor(Color.WHITE);
79         }
80         if (!mUtils.isUserSetupCompleted(this)) {
81             SystemBarHelper.hideSystemBars(window);
82         }
83     }
84 
initializeLayoutParams(int layoutResourceId, int headerResourceId, boolean showProgressBar)85     public void initializeLayoutParams(int layoutResourceId, int headerResourceId,
86             boolean showProgressBar) {
87         setContentView(layoutResourceId);
88         SetupWizardLayout layout = (SetupWizardLayout) findViewById(R.id.setup_wizard_layout);
89         layout.setHeaderText(headerResourceId);
90         if (showProgressBar) {
91             layout.showProgressBar();
92         }
93         setupNavigationBar(layout.getNavigationBar());
94     }
95 
setupNavigationBar(NavigationBar bar)96     private void setupNavigationBar(NavigationBar bar) {
97         bar.setNavigationBarListener(this);
98         mNextButton = bar.getNextButton();
99         mBackButton = bar.getBackButton();
100     }
101 
configureNavigationButtons(int nextButtonResourceId, int nextButtonVisibility, int backButtonVisibility)102     public void configureNavigationButtons(int nextButtonResourceId, int nextButtonVisibility,
103             int backButtonVisibility) {
104         if (nextButtonResourceId != NEXT_BUTTON_EMPTY_LABEL) {
105             mNextButton.setText(nextButtonResourceId);
106         }
107         mNextButton.setVisibility(nextButtonVisibility);
108         mBackButton.setVisibility(backButtonVisibility);
109     }
110 
111     @Override
onNavigateBack()112     public void onNavigateBack() {
113         onBackPressed();
114     }
115 
116     @Override
onNavigateNext()117     public void onNavigateNext() {
118     }
119 
120     private class HeaderDrawable extends Drawable {
121         private Activity mActivity;
122         private int mMainColor;
123 
HeaderDrawable(Activity a, int mainColor)124         HeaderDrawable(Activity a, int mainColor) {
125             mActivity = a;
126             mMainColor = mainColor;
127         }
128 
129         @Override
draw(Canvas canvas)130         public void draw(Canvas canvas) {
131             Drawable logo = LogoUtils.getOrganisationLogo(mActivity);
132             // At this point, the logo has already been resized.
133             int logoWidth = logo.getIntrinsicWidth();
134             int logoHeight = logo.getIntrinsicHeight();
135             Resources resources = mActivity.getResources();
136 
137             int logoPaddingLeftRight = (int) resources
138                     .getDimension(R.dimen.logo_padding_left_right);
139             int logoPaddingBottom = (int) resources
140                     .getDimension(R.dimen.logo_padding_bottom);
141             int totalWidth = getIntrinsicWidth();
142             int totalHeight = getIntrinsicHeight();
143 
144             // By default, the drawable is materialized: it is not a solid color. Draw a white
145             // rectangle over the whole drawable so that it is a solid color.
146             Paint paint = new Paint();
147             paint.setColor(resources.getColor(R.color.white));
148             canvas.drawRect(0, 0, totalWidth, totalHeight, paint);
149 
150             // Draw the logo.
151             if (shouldDrawLogoOnLeftSide()) {
152                 logo.setBounds(logoPaddingLeftRight,
153                         totalHeight - logoPaddingBottom - logoHeight,
154                         logoPaddingLeftRight + logoWidth,
155                         totalHeight - logoPaddingBottom);
156             } else {
157                 logo.setBounds(totalWidth - logoPaddingLeftRight - logoWidth,
158                         totalHeight - logoPaddingBottom - logoHeight,
159                         totalWidth - logoPaddingLeftRight,
160                         totalHeight - logoPaddingBottom);
161             }
162             logo.draw(canvas);
163 
164         }
165 
166         @Override
getIntrinsicHeight()167         public int getIntrinsicHeight() {
168             if (mActivity.getResources().getBoolean(R.bool.suwUseTabletLayout)) {
169                 return (int) mActivity.getResources()
170                         .getDimension(R.dimen.suw_tablet_illustration_height);
171             }
172             return getScreenWidth() * 9 / 20;
173         }
174 
175         @Override
getIntrinsicWidth()176         public int getIntrinsicWidth() {
177             return getScreenWidth();
178         }
179 
180         @Override
getOpacity()181         public int getOpacity() {
182             return PixelFormat.OPAQUE;
183         }
184 
185         @Override
setAlpha(int alpha)186         public void setAlpha(int alpha) {
187             //ignore
188         }
189 
190         @Override
setColorFilter(ColorFilter cf)191         public void setColorFilter(ColorFilter cf) {
192             // ignore
193         }
194 
getScreenWidth()195         private int getScreenWidth() {
196             DisplayMetrics metrics = new DisplayMetrics();
197             mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
198             return metrics.widthPixels;
199         }
200 
shouldDrawLogoOnLeftSide()201         private boolean shouldDrawLogoOnLeftSide() {
202             // for a tablet layout, the logo should be in the bottom left
203             boolean result = useTabletLayout();
204             // for a right-to-left language, reverse it.
205             if (mActivity.getResources().getConfiguration().getLayoutDirection()
206                     == View.LAYOUT_DIRECTION_RTL) {
207                 result = !result;
208             }
209             return result;
210         }
211 
useTabletLayout()212         private boolean useTabletLayout() {
213             return mActivity.getResources().getBoolean(R.bool.suwUseTabletLayout);
214         }
215     }
216 }
217