Dan Sandler | 45f17c5 | 2018-05-02 20:01:38 -0400 | [diff] [blame] | 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 | init(null, 0) |
| 30 | } |
| 31 | |
| 32 | constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { |
| 33 | init(attrs, 0) |
| 34 | } |
| 35 | |
| 36 | constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) { |
| 37 | init(attrs, defStyle) |
| 38 | } |
| 39 | |
| 40 | override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { |
| 41 | super.onSizeChanged(w, h, oldw, oldh) |
| 42 | adjustLayout() |
| 43 | } |
| 44 | |
| 45 | override fun onApplyWindowInsets(insets: WindowInsets?): WindowInsets { |
| 46 | _insets = insets |
| 47 | adjustLayout() |
| 48 | return super.onApplyWindowInsets(insets) |
| 49 | } |
| 50 | |
| 51 | fun adjustLayout() { |
| 52 | _insets?.displayCutout?.boundingRects?.let { |
| 53 | var cutoutCenter = 0 |
| 54 | var cutoutLeft = 0 |
| 55 | var cutoutRight = 0 |
| 56 | |
| 57 | // collect at most three cutouts |
| 58 | for (r in it) { |
| 59 | if (r.top > 0) continue |
| 60 | |
| 61 | if (r.left == left) { |
| 62 | cutoutLeft = r.width() |
| 63 | } else if (r.right == right) { |
| 64 | cutoutRight = r.width() |
| 65 | } else { |
| 66 | cutoutCenter = r.width() |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // apply to layout |
| 71 | (findViewWithTag("cutoutLeft") as View?)?.let { |
| 72 | it.layoutParams = LayoutParams(cutoutLeft, MATCH_PARENT) |
| 73 | } |
| 74 | (findViewWithTag("cutoutCenter") as View?)?.let { |
| 75 | it.layoutParams = LayoutParams(cutoutCenter, MATCH_PARENT) |
| 76 | } |
| 77 | (findViewWithTag("cutoutRight") as View?)?.let { |
| 78 | it.layoutParams = LayoutParams(cutoutRight, MATCH_PARENT) |
| 79 | } |
| 80 | |
| 81 | requestLayout() |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private fun init(attrs: AttributeSet?, defStyle: Int) { |
| 86 | } |
| 87 | |
| 88 | } |