• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.egg.paint
18 
19 import android.content.Context
20 import android.util.AttributeSet
21 import android.view.*
22 import android.view.ViewGroup.LayoutParams.MATCH_PARENT
23 import android.widget.LinearLayout
24 
25 class CutoutAvoidingToolbar : LinearLayout {
26     private var _insets: WindowInsets? = null
27 
28     constructor(context: Context) : super(context) {
29     }
30 
31     constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
32     }
33 
34     constructor(
35         context: Context,
36         attrs: AttributeSet,
37         defStyle: Int
38     ) : super(context, attrs, defStyle) {
39     }
40 
onSizeChangednull41     override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
42         super.onSizeChanged(w, h, oldw, oldh)
43         adjustLayout()
44     }
45 
onApplyWindowInsetsnull46     override fun onApplyWindowInsets(insets: WindowInsets?): WindowInsets {
47         _insets = insets
48         adjustLayout()
49         return super.onApplyWindowInsets(insets)
50     }
51 
adjustLayoutnull52     fun adjustLayout() {
53         _insets?.displayCutout?.boundingRects?.let {
54             var cutoutCenter = 0
55             var cutoutLeft = 0
56             var cutoutRight = 0
57 
58             // collect at most three cutouts
59             for (r in it) {
60                 if (r.top > 0) continue
61 
62                 if (r.left == left) {
63                     cutoutLeft = r.width()
64                 } else if (r.right == right) {
65                     cutoutRight = r.width()
66                 } else {
67                     cutoutCenter = r.width()
68                 }
69             }
70 
71             // apply to layout
72             (findViewWithTag("cutoutLeft") as View?)?.let {
73                 it.layoutParams = LayoutParams(cutoutLeft, MATCH_PARENT)
74             }
75             (findViewWithTag("cutoutCenter") as View?)?.let {
76                 it.layoutParams = LayoutParams(cutoutCenter, MATCH_PARENT)
77             }
78             (findViewWithTag("cutoutRight") as View?)?.let {
79                 it.layoutParams = LayoutParams(cutoutRight, MATCH_PARENT)
80             }
81 
82             requestLayout()
83         }
84     }
85 }
86