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