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