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