• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
18 
19 import android.annotation.SuppressLint;
20 import android.annotation.TargetApi;
21 import android.content.Context;
22 import android.content.res.ColorStateList;
23 import android.content.res.TypedArray;
24 import android.graphics.Shader.TileMode;
25 import android.graphics.drawable.BitmapDrawable;
26 import android.graphics.drawable.Drawable;
27 import android.graphics.drawable.LayerDrawable;
28 import android.os.Build.VERSION;
29 import android.os.Build.VERSION_CODES;
30 import android.os.Parcel;
31 import android.os.Parcelable;
32 import android.util.AttributeSet;
33 import android.util.Log;
34 import android.util.TypedValue;
35 import android.view.Gravity;
36 import android.view.LayoutInflater;
37 import android.view.View;
38 import android.view.ViewGroup;
39 import android.widget.ScrollView;
40 import android.widget.TextView;
41 
42 import com.android.setupwizardlib.template.HeaderMixin;
43 import com.android.setupwizardlib.template.NavigationBarMixin;
44 import com.android.setupwizardlib.template.ProgressBarMixin;
45 import com.android.setupwizardlib.template.RequireScrollMixin;
46 import com.android.setupwizardlib.template.ScrollViewScrollHandlingDelegate;
47 import com.android.setupwizardlib.view.Illustration;
48 import com.android.setupwizardlib.view.NavigationBar;
49 
50 public class SetupWizardLayout extends TemplateLayout {
51 
52     private static final String TAG = "SetupWizardLayout";
53 
SetupWizardLayout(Context context)54     public SetupWizardLayout(Context context) {
55         super(context, 0, 0);
56         init(null, R.attr.suwLayoutTheme);
57     }
58 
SetupWizardLayout(Context context, int template)59     public SetupWizardLayout(Context context, int template) {
60         this(context, template, 0);
61     }
62 
SetupWizardLayout(Context context, int template, int containerId)63     public SetupWizardLayout(Context context, int template, int containerId) {
64         super(context, template, containerId);
65         init(null, R.attr.suwLayoutTheme);
66     }
67 
SetupWizardLayout(Context context, AttributeSet attrs)68     public SetupWizardLayout(Context context, AttributeSet attrs) {
69         super(context, attrs);
70         init(attrs, R.attr.suwLayoutTheme);
71     }
72 
73     @TargetApi(VERSION_CODES.HONEYCOMB)
SetupWizardLayout(Context context, AttributeSet attrs, int defStyleAttr)74     public SetupWizardLayout(Context context, AttributeSet attrs, int defStyleAttr) {
75         super(context, attrs, defStyleAttr);
76         init(attrs, defStyleAttr);
77     }
78 
79     // All the constructors delegate to this init method. The 3-argument constructor is not
80     // available in LinearLayout before v11, so call super with the exact same arguments.
init(AttributeSet attrs, int defStyleAttr)81     private void init(AttributeSet attrs, int defStyleAttr) {
82         registerMixin(HeaderMixin.class, new HeaderMixin(this, attrs, defStyleAttr));
83         registerMixin(ProgressBarMixin.class, new ProgressBarMixin(this));
84         registerMixin(NavigationBarMixin.class, new NavigationBarMixin(this));
85         final RequireScrollMixin requireScrollMixin = new RequireScrollMixin(this);
86         registerMixin(RequireScrollMixin.class, requireScrollMixin);
87 
88         final ScrollView scrollView = getScrollView();
89         if (scrollView != null) {
90             requireScrollMixin.setScrollHandlingDelegate(
91                     new ScrollViewScrollHandlingDelegate(requireScrollMixin, scrollView));
92         }
93 
94         final TypedArray a = getContext().obtainStyledAttributes(attrs,
95                 R.styleable.SuwSetupWizardLayout, defStyleAttr, 0);
96 
97         // Set the background from XML, either directly or built from a bitmap tile
98         final Drawable background =
99                 a.getDrawable(R.styleable.SuwSetupWizardLayout_suwBackground);
100         if (background != null) {
101             setLayoutBackground(background);
102         } else {
103             final Drawable backgroundTile =
104                     a.getDrawable(R.styleable.SuwSetupWizardLayout_suwBackgroundTile);
105             if (backgroundTile != null) {
106                 setBackgroundTile(backgroundTile);
107             }
108         }
109 
110         // Set the illustration from XML, either directly or built from image + horizontal tile
111         final Drawable illustration =
112                 a.getDrawable(R.styleable.SuwSetupWizardLayout_suwIllustration);
113         if (illustration != null) {
114             setIllustration(illustration);
115         } else {
116             final Drawable illustrationImage =
117                     a.getDrawable(R.styleable.SuwSetupWizardLayout_suwIllustrationImage);
118             final Drawable horizontalTile = a.getDrawable(
119                     R.styleable.SuwSetupWizardLayout_suwIllustrationHorizontalTile);
120             if (illustrationImage != null && horizontalTile != null) {
121                 setIllustration(illustrationImage, horizontalTile);
122             }
123         }
124 
125         // Set the top padding of the illustration
126         int decorPaddingTop = a.getDimensionPixelSize(
127                 R.styleable.SuwSetupWizardLayout_suwDecorPaddingTop, -1);
128         if (decorPaddingTop == -1) {
129             decorPaddingTop = getResources().getDimensionPixelSize(R.dimen.suw_decor_padding_top);
130         }
131         setDecorPaddingTop(decorPaddingTop);
132 
133 
134         // Set the illustration aspect ratio. See Illustration.setAspectRatio(float). This will
135         // override suwDecorPaddingTop if its value is not 0.
136         float illustrationAspectRatio = a.getFloat(
137                 R.styleable.SuwSetupWizardLayout_suwIllustrationAspectRatio, -1f);
138         if (illustrationAspectRatio == -1f) {
139             final TypedValue out = new TypedValue();
140             getResources().getValue(R.dimen.suw_illustration_aspect_ratio, out, true);
141             illustrationAspectRatio = out.getFloat();
142         }
143         setIllustrationAspectRatio(illustrationAspectRatio);
144 
145         a.recycle();
146     }
147 
148     @Override
onSaveInstanceState()149     protected Parcelable onSaveInstanceState() {
150         final Parcelable parcelable = super.onSaveInstanceState();
151         final SavedState ss = new SavedState(parcelable);
152         ss.mIsProgressBarShown = isProgressBarShown();
153         return ss;
154     }
155 
156     @Override
onRestoreInstanceState(Parcelable state)157     protected void onRestoreInstanceState(Parcelable state) {
158         if (!(state instanceof SavedState)) {
159             Log.w(TAG, "Ignoring restore instance state " + state);
160             super.onRestoreInstanceState(state);
161             return;
162         }
163 
164         final SavedState ss = (SavedState) state;
165         super.onRestoreInstanceState(ss.getSuperState());
166         final boolean isProgressBarShown = ss.mIsProgressBarShown;
167         setProgressBarShown(isProgressBarShown);
168     }
169 
170     @Override
onInflateTemplate(LayoutInflater inflater, int template)171     protected View onInflateTemplate(LayoutInflater inflater, int template) {
172         if (template == 0) {
173             template = R.layout.suw_template;
174         }
175         return inflateTemplate(inflater, R.style.SuwThemeMaterial_Light, template);
176     }
177 
178     @Override
findContainer(int containerId)179     protected ViewGroup findContainer(int containerId) {
180         if (containerId == 0) {
181             containerId = R.id.suw_layout_content;
182         }
183         return super.findContainer(containerId);
184     }
185 
getNavigationBar()186     public NavigationBar getNavigationBar() {
187         return getMixin(NavigationBarMixin.class).getNavigationBar();
188     }
189 
getScrollView()190     public ScrollView getScrollView() {
191         final View view = findManagedViewById(R.id.suw_bottom_scroll_view);
192         return view instanceof ScrollView ? (ScrollView) view : null;
193     }
194 
requireScrollToBottom()195     public void requireScrollToBottom() {
196         final RequireScrollMixin requireScrollMixin = getMixin(RequireScrollMixin.class);
197         final NavigationBar navigationBar = getNavigationBar();
198         if (navigationBar != null) {
199             requireScrollMixin.requireScrollWithNavigationBar(navigationBar);
200         } else {
201             Log.e(TAG, "Cannot require scroll. Navigation bar is null.");
202         }
203     }
204 
setHeaderText(int title)205     public void setHeaderText(int title) {
206         getMixin(HeaderMixin.class).setText(title);
207     }
208 
setHeaderText(CharSequence title)209     public void setHeaderText(CharSequence title) {
210         getMixin(HeaderMixin.class).setText(title);
211     }
212 
getHeaderText()213     public CharSequence getHeaderText() {
214         return getMixin(HeaderMixin.class).getText();
215     }
216 
getHeaderTextView()217     public TextView getHeaderTextView() {
218         return getMixin(HeaderMixin.class).getTextView();
219     }
220 
221     /**
222      * Set the illustration of the layout. The drawable will be applied as is, and the bounds will
223      * be set as implemented in {@link com.android.setupwizardlib.view.Illustration}. To create
224      * a suitable drawable from an asset and a horizontal repeating tile, use
225      * {@link #setIllustration(int, int)} instead.
226      *
227      * @param drawable The drawable specifying the illustration.
228      */
setIllustration(Drawable drawable)229     public void setIllustration(Drawable drawable) {
230         final View view = findManagedViewById(R.id.suw_layout_decor);
231         if (view instanceof Illustration) {
232             final Illustration illustration = (Illustration) view;
233             illustration.setIllustration(drawable);
234         }
235     }
236 
237     /**
238      * Set the illustration of the layout, which will be created asset and the horizontal tile as
239      * suitable. On phone layouts (not sw600dp), the asset will be scaled, maintaining aspect ratio.
240      * On tablets (sw600dp), the assets will always have 256dp height and the rest of the
241      * illustration area that the asset doesn't fill will be covered by the horizontalTile.
242      *
243      * @param asset Resource ID of the illustration asset.
244      * @param horizontalTile Resource ID of the horizontally repeating tile for tablet layout.
245      */
setIllustration(int asset, int horizontalTile)246     public void setIllustration(int asset, int horizontalTile) {
247         final View view = findManagedViewById(R.id.suw_layout_decor);
248         if (view instanceof Illustration) {
249             final Illustration illustration = (Illustration) view;
250             final Drawable illustrationDrawable = getIllustration(asset, horizontalTile);
251             illustration.setIllustration(illustrationDrawable);
252         }
253     }
254 
setIllustration(Drawable asset, Drawable horizontalTile)255     private void setIllustration(Drawable asset, Drawable horizontalTile) {
256         final View view = findManagedViewById(R.id.suw_layout_decor);
257         if (view instanceof Illustration) {
258             final Illustration illustration = (Illustration) view;
259             final Drawable illustrationDrawable = getIllustration(asset, horizontalTile);
260             illustration.setIllustration(illustrationDrawable);
261         }
262     }
263 
264     /**
265      * Sets the aspect ratio of the illustration. This will be the space (padding top) reserved
266      * above the header text. This will override the padding top of the illustration.
267      *
268      * @param aspectRatio The aspect ratio
269      * @see com.android.setupwizardlib.view.Illustration#setAspectRatio(float)
270      */
setIllustrationAspectRatio(float aspectRatio)271     public void setIllustrationAspectRatio(float aspectRatio) {
272         final View view = findManagedViewById(R.id.suw_layout_decor);
273         if (view instanceof Illustration) {
274             final Illustration illustration = (Illustration) view;
275             illustration.setAspectRatio(aspectRatio);
276         }
277     }
278 
279     /**
280      * Set the top padding of the decor view. If the decor is an Illustration and the aspect ratio
281      * is set, this value will be overridden.
282      *
283      * <p>Note: Currently the default top padding for tablet landscape is 128dp, which is the offset
284      * of the card from the top. This is likely to change in future versions so this value aligns
285      * with the height of the illustration instead.
286      *
287      * @param paddingTop The top padding in pixels.
288      */
setDecorPaddingTop(int paddingTop)289     public void setDecorPaddingTop(int paddingTop) {
290         final View view = findManagedViewById(R.id.suw_layout_decor);
291         if (view != null) {
292             view.setPadding(view.getPaddingLeft(), paddingTop, view.getPaddingRight(),
293                     view.getPaddingBottom());
294         }
295     }
296 
297     /**
298      * Set the background of the layout, which is expected to be able to extend infinitely. If it is
299      * a bitmap tile and you want it to repeat, use {@link #setBackgroundTile(int)} instead.
300      */
setLayoutBackground(Drawable background)301     public void setLayoutBackground(Drawable background) {
302         final View view = findManagedViewById(R.id.suw_layout_decor);
303         if (view != null) {
304             //noinspection deprecation
305             view.setBackgroundDrawable(background);
306         }
307     }
308 
309     /**
310      * Set the background of the layout to a repeating bitmap tile. To use a different kind of
311      * drawable, use {@link #setLayoutBackground(android.graphics.drawable.Drawable)} instead.
312      */
setBackgroundTile(int backgroundTile)313     public void setBackgroundTile(int backgroundTile) {
314         final Drawable backgroundTileDrawable =
315                 getContext().getResources().getDrawable(backgroundTile);
316         setBackgroundTile(backgroundTileDrawable);
317     }
318 
setBackgroundTile(Drawable backgroundTile)319     private void setBackgroundTile(Drawable backgroundTile) {
320         if (backgroundTile instanceof BitmapDrawable) {
321             ((BitmapDrawable) backgroundTile).setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
322         }
323         setLayoutBackground(backgroundTile);
324     }
325 
getIllustration(int asset, int horizontalTile)326     private Drawable getIllustration(int asset, int horizontalTile) {
327         final Context context = getContext();
328         final Drawable assetDrawable = context.getResources().getDrawable(asset);
329         final Drawable tile = context.getResources().getDrawable(horizontalTile);
330         return getIllustration(assetDrawable, tile);
331     }
332 
333     @SuppressLint("RtlHardcoded")
getIllustration(Drawable asset, Drawable horizontalTile)334     private Drawable getIllustration(Drawable asset, Drawable horizontalTile) {
335         final Context context = getContext();
336         if (context.getResources().getBoolean(R.bool.suwUseTabletLayout)) {
337             // If it is a "tablet" (sw600dp), create a LayerDrawable with the horizontal tile.
338             if (horizontalTile instanceof BitmapDrawable) {
339                 ((BitmapDrawable) horizontalTile).setTileModeX(TileMode.REPEAT);
340                 ((BitmapDrawable) horizontalTile).setGravity(Gravity.TOP);
341             }
342             if (asset instanceof BitmapDrawable) {
343                 // Always specify TOP | LEFT, Illustration will flip the entire LayerDrawable.
344                 ((BitmapDrawable) asset).setGravity(Gravity.TOP | Gravity.LEFT);
345             }
346             final LayerDrawable layers =
347                     new LayerDrawable(new Drawable[] { horizontalTile, asset });
348             if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
349                 layers.setAutoMirrored(true);
350             }
351             return layers;
352         } else {
353             // If it is a "phone" (not sw600dp), simply return the illustration
354             if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
355                 asset.setAutoMirrored(true);
356             }
357             return asset;
358         }
359     }
360 
isProgressBarShown()361     public boolean isProgressBarShown() {
362         return getMixin(ProgressBarMixin.class).isShown();
363     }
364 
365     /**
366      * Sets whether the progress bar below the header text is shown or not. The progress bar is
367      * a lazily inflated ViewStub, which means the progress bar will not actually be part of the
368      * view hierarchy until the first time this is set to {@code true}.
369      */
setProgressBarShown(boolean shown)370     public void setProgressBarShown(boolean shown) {
371         getMixin(ProgressBarMixin.class).setShown(shown);
372     }
373 
374     /**
375      * @deprecated Use {@link #setProgressBarShown(boolean)}
376      */
377     @Deprecated
showProgressBar()378     public void showProgressBar() {
379         setProgressBarShown(true);
380     }
381 
382     /**
383      * @deprecated Use {@link #setProgressBarShown(boolean)}
384      */
385     @Deprecated
hideProgressBar()386     public void hideProgressBar() {
387         setProgressBarShown(false);
388     }
389 
setProgressBarColor(ColorStateList color)390     public void setProgressBarColor(ColorStateList color) {
391         getMixin(ProgressBarMixin.class).setColor(color);
392     }
393 
getProgressBarColor()394     public ColorStateList getProgressBarColor() {
395         return getMixin(ProgressBarMixin.class).getColor();
396     }
397 
398     /* Misc */
399 
400     protected static class SavedState extends BaseSavedState {
401 
402         boolean mIsProgressBarShown = false;
403 
SavedState(Parcelable parcelable)404         public SavedState(Parcelable parcelable) {
405             super(parcelable);
406         }
407 
SavedState(Parcel source)408         public SavedState(Parcel source) {
409             super(source);
410             mIsProgressBarShown = source.readInt() != 0;
411         }
412 
413         @Override
writeToParcel(Parcel dest, int flags)414         public void writeToParcel(Parcel dest, int flags) {
415             super.writeToParcel(dest, flags);
416             dest.writeInt(mIsProgressBarShown ? 1 : 0);
417         }
418 
419         public static final Parcelable.Creator<SavedState> CREATOR =
420                 new Parcelable.Creator<SavedState>() {
421 
422                     @Override
423                     public SavedState createFromParcel(Parcel parcel) {
424                         return new SavedState(parcel);
425                     }
426 
427                     @Override
428                     public SavedState[] newArray(int size) {
429                         return new SavedState[size];
430                     }
431                 };
432     }
433 }
434