blob: f710f7fc47e2b9646c5c482a598a28b9a1dcabab [file] [log] [blame]
Beth Thibodeau07d20c32019-10-16 13:45:56 -04001/*
2 * Copyright (C) 2019 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
17package com.android.systemui.qs
18
19import android.content.Context
20import android.content.res.Configuration
21import android.view.View
22import android.view.ViewGroup
23import com.android.systemui.R
24import com.android.systemui.qs.TileLayout.exactly
25
26class DoubleLineTileLayout(context: Context) : ViewGroup(context), QSPanel.QSTileLayout {
27
28 protected val mRecords = ArrayList<QSPanel.TileRecord>()
29 private var _listening = false
30 private var smallTileSize = 0
31 private val twoLineHeight
32 get() = smallTileSize * 2 + cellMarginVertical
33 private var cellMarginHorizontal = 0
34 private var cellMarginVertical = 0
35
36 init {
37 isFocusableInTouchMode = true
38 clipChildren = false
39 clipToPadding = false
40
41 updateResources()
42 }
43
44 override fun addTile(tile: QSPanel.TileRecord) {
45 mRecords.add(tile)
46 tile.tile.setListening(this, _listening)
47 addTileView(tile)
48 }
49
50 protected fun addTileView(tile: QSPanel.TileRecord) {
51 addView(tile.tileView)
52 }
53
54 override fun removeTile(tile: QSPanel.TileRecord) {
55 mRecords.remove(tile)
56 tile.tile.setListening(this, false)
57 removeView(tile.tileView)
58 }
59
60 override fun removeAllViews() {
61 mRecords.forEach { it.tile.setListening(this, false) }
62 mRecords.clear()
63 super.removeAllViews()
64 }
65
66 override fun getOffsetTop(tile: QSPanel.TileRecord?) = top
67
68 override fun updateResources(): Boolean {
69 with(mContext.resources) {
70 smallTileSize = getDimensionPixelSize(R.dimen.qs_quick_tile_size)
71 cellMarginHorizontal = getDimensionPixelSize(R.dimen.qs_tile_margin_horizontal)
72 cellMarginVertical = getDimensionPixelSize(R.dimen.new_qs_vertical_margin)
73 }
74 requestLayout()
75 return false
76 }
77
78 override fun setListening(listening: Boolean) {
79 if (_listening == listening) return
80 _listening = listening
81 for (record in mRecords) {
82 record.tile.setListening(this, listening)
83 }
84 }
85
86 override fun getNumVisibleTiles() = mRecords.size
87
88 override fun onConfigurationChanged(newConfig: Configuration) {
89 super.onConfigurationChanged(newConfig)
90 updateResources()
91 }
92
93 override fun onFinishInflate() {
94 updateResources()
95 }
96
97 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
98 var previousView: View = this
99 var tiles = 0
100
101 mRecords.forEach {
102 val tileView = it.tileView
103 if (tileView.visibility != View.GONE) {
104 tileView.updateAccessibilityOrder(previousView)
105 previousView = tileView
106 tiles++
107 tileView.measure(exactly(smallTileSize), exactly(smallTileSize))
108 }
109 }
110
111 val height = twoLineHeight
112 val columns = tiles / 2
113 val width = paddingStart + paddingEnd +
114 columns * smallTileSize +
115 (columns - 1) * cellMarginHorizontal
116 setMeasuredDimension(width, height)
117 }
118
119 override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
120 val tiles = mRecords.filter { it.tileView.visibility != View.GONE }
121 tiles.forEachIndexed {
122 index, tile ->
123 val column = index % (tiles.size / 2)
124 val left = getLeftForColumn(column)
125 val top = if (index < tiles.size / 2) 0 else getTopBottomRow()
126 tile.tileView.layout(left, top, left + smallTileSize, top + smallTileSize)
127 }
128 }
129
130 private fun getLeftForColumn(column: Int) = column * (smallTileSize + cellMarginHorizontal)
131
132 private fun getTopBottomRow() = smallTileSize + cellMarginVertical
133}