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; 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.google.android.setupcompat.partnerconfig.PartnerConfigHelper; 30 import com.google.android.setupcompat.util.Logger; 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 static final Logger LOG = new Logger("StatusBarBgLayout"); 44 45 private Drawable statusBarBackground; 46 private Object lastInsets; // Use generic Object type for compatibility 47 StatusBarBackgroundLayout(Context context)48 public StatusBarBackgroundLayout(Context context) { 49 super(context); 50 } 51 StatusBarBackgroundLayout(Context context, AttributeSet attrs)52 public StatusBarBackgroundLayout(Context context, AttributeSet attrs) { 53 super(context, attrs); 54 } 55 56 @TargetApi(VERSION_CODES.HONEYCOMB) StatusBarBackgroundLayout(Context context, AttributeSet attrs, int defStyleAttr)57 public StatusBarBackgroundLayout(Context context, AttributeSet attrs, int defStyleAttr) { 58 super(context, attrs, defStyleAttr); 59 } 60 61 @Override onAttachedToWindow()62 protected void onAttachedToWindow() { 63 super.onAttachedToWindow(); 64 if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 65 if (lastInsets == null) { 66 requestApplyInsets(); 67 } 68 } 69 } 70 71 @Override onDraw(Canvas canvas)72 protected void onDraw(Canvas canvas) { 73 super.onDraw(canvas); 74 if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 75 if (lastInsets != null) { 76 final int insetTop = ((WindowInsets) lastInsets).getSystemWindowInsetTop(); 77 if (insetTop > 0) { 78 statusBarBackground.setBounds( 79 /* left= */ 0, /* top= */ 0, /* right= */ getWidth(), /* bottom= */ insetTop); 80 statusBarBackground.draw(canvas); 81 } 82 } 83 } 84 } 85 setStatusBarBackground(Drawable background)86 public void setStatusBarBackground(Drawable background) { 87 statusBarBackground = background; 88 if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { 89 setWillNotDraw(background == null); 90 setFitsSystemWindows(background != null); 91 invalidate(); 92 } 93 } 94 getStatusBarBackground()95 public Drawable getStatusBarBackground() { 96 return statusBarBackground; 97 } 98 99 @Override onApplyWindowInsets(WindowInsets insets)100 public WindowInsets onApplyWindowInsets(WindowInsets insets) { 101 // TODO: b/398407478 - Add test case for edge to edge to layout from library. 102 if (PartnerConfigHelper.isGlifExpressiveEnabled(getContext())) { 103 if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP && insets.getSystemWindowInsetBottom() > 0) { 104 LOG.atDebug("NavigationBarHeight: " + insets.getSystemWindowInsetBottom()); 105 insets = 106 insets.replaceSystemWindowInsets( 107 insets.getSystemWindowInsetLeft(), 108 insets.getSystemWindowInsetTop(), 109 insets.getSystemWindowInsetRight(), 110 /* bottom= */ 0); 111 } 112 } 113 lastInsets = insets; 114 return super.onApplyWindowInsets(insets); 115 } 116 } 117