Home | History | Annotate | Download | only in view
      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.view;
     18 
     19 import android.annotation.TargetApi;
     20 import android.content.Context;
     21 import android.content.res.TypedArray;
     22 import android.graphics.Canvas;
     23 import android.graphics.drawable.Drawable;
     24 import android.os.Build;
     25 import android.os.Build.VERSION_CODES;
     26 import android.util.AttributeSet;
     27 import android.view.WindowInsets;
     28 import android.widget.FrameLayout;
     29 
     30 import com.android.setupwizardlib.R;
     31 
     32 /**
     33  * A FrameLayout subclass that will responds to onApplyWindowInsets to draw a drawable in the top
     34  * inset area, making a background effect for the navigation bar. To make use of this layout,
     35  * specify the system UI visibility {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN} and
     36  * set specify fitsSystemWindows.
     37  *
     38  * <p>This view is a normal FrameLayout if either of those are not set, or if the platform version
     39  * is lower than Lollipop.
     40  */
     41 public class StatusBarBackgroundLayout extends FrameLayout {
     42 
     43     private Drawable mStatusBarBackground;
     44     private Object mLastInsets;  // Use generic Object type for compatibility
     45 
     46     public StatusBarBackgroundLayout(Context context) {
     47         super(context);
     48         init(context, null, 0);
     49     }
     50 
     51     public StatusBarBackgroundLayout(Context context, AttributeSet attrs) {
     52         super(context, attrs);
     53         init(context, attrs, 0);
     54     }
     55 
     56     @TargetApi(VERSION_CODES.HONEYCOMB)
     57     public StatusBarBackgroundLayout(Context context, AttributeSet attrs, int defStyleAttr) {
     58         super(context, attrs, defStyleAttr);
     59         init(context, attrs, defStyleAttr);
     60     }
     61 
     62     private void init(Context context, AttributeSet attrs, int defStyleAttr) {
     63         final TypedArray a = context.obtainStyledAttributes(attrs,
     64                 R.styleable.SuwStatusBarBackgroundLayout, defStyleAttr, 0);
     65         final Drawable statusBarBackground =
     66                 a.getDrawable(R.styleable.SuwStatusBarBackgroundLayout_suwStatusBarBackground);
     67         setStatusBarBackground(statusBarBackground);
     68         a.recycle();
     69     }
     70 
     71     @Override
     72     protected void onAttachedToWindow() {
     73         super.onAttachedToWindow();
     74         if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
     75             if (mLastInsets == null) {
     76                 requestApplyInsets();
     77             }
     78         }
     79     }
     80 
     81     @Override
     82     protected void onDraw(Canvas canvas) {
     83         super.onDraw(canvas);
     84         if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
     85             if (mLastInsets != null) {
     86                 final int insetTop = ((WindowInsets) mLastInsets).getSystemWindowInsetTop();
     87                 if (insetTop > 0) {
     88                     mStatusBarBackground.setBounds(0, 0, getWidth(), insetTop);
     89                     mStatusBarBackground.draw(canvas);
     90                 }
     91             }
     92         }
     93     }
     94 
     95     public void setStatusBarBackground(Drawable background) {
     96         mStatusBarBackground = background;
     97         if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
     98             setWillNotDraw(background == null);
     99             setFitsSystemWindows(background != null);
    100             invalidate();
    101         }
    102     }
    103 
    104     public Drawable getStatusBarBackground() {
    105         return mStatusBarBackground;
    106     }
    107 
    108     @Override
    109     public WindowInsets onApplyWindowInsets(WindowInsets insets) {
    110         mLastInsets = insets;
    111         return super.onApplyWindowInsets(insets);
    112     }
    113 }
    114