1 /*
2  * Copyright (C) 2020 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.systemui.qs
18 
19 import android.content.Context
20 import android.util.AttributeSet
21 import com.android.systemui.flags.Flags
22 import com.android.systemui.flags.RefactorFlag
23 import com.android.systemui.res.R
24 
25 open class SideLabelTileLayout(
26     context: Context,
27     attrs: AttributeSet?
28 ) : TileLayout(context, attrs) {
29 
30     private val isSmallLandscapeLockscreenEnabled =
31             RefactorFlag.forView(Flags.LOCKSCREEN_ENABLE_LANDSCAPE).isEnabled
32 
updateResourcesnull33     override fun updateResources(): Boolean {
34         return super.updateResources().also {
35             // TODO (b/293252410) remove condition here when flag is launched
36             //  Instead update quick_settings_max_rows resource to be the same as
37             //  small_land_lockscreen_quick_settings_max_rows whenever is_small_screen_landscape is
38             //  true. Then, only use quick_settings_max_rows resource.
39             val useSmallLandscapeLockscreenResources =
40                     isSmallLandscapeLockscreenEnabled &&
41                     mContext.resources.getBoolean(R.bool.is_small_screen_landscape)
42 
43             mMaxAllowedRows = if (useSmallLandscapeLockscreenResources) {
44                 context.resources.getInteger(
45                         R.integer.small_land_lockscreen_quick_settings_max_rows)
46                 } else {
47                     context.resources.getInteger(R.integer.quick_settings_max_rows)
48                 }
49         }
50     }
51 
isFullnull52     override fun isFull(): Boolean {
53         return mRecords.size >= maxTiles()
54     }
55 
useSidePaddingnull56     override fun useSidePadding(): Boolean {
57         return false
58     }
59 
60     /**
61      * Return the position from the top of the layout of the tile with this index.
62      *
63      * This will return a position even for indices that go beyond [maxTiles], continuing the rows
64      * beyond that.
65      */
getPhantomTopPositionnull66     fun getPhantomTopPosition(index: Int): Int {
67         val row = index / mColumns
68         return getRowTop(row)
69     }
70 
updateMaxRowsnull71     override fun updateMaxRows(allowedHeight: Int, tilesCount: Int): Boolean {
72         val previousRows = mRows
73         mRows = mMaxAllowedRows
74         // We want at most mMaxAllowedRows, but it could be that we don't have enough tiles to fit
75         // that many rows. In that case, we want
76         // `tilesCount = (mRows - 1) * mColumns + X`
77         // where X is some remainder between 1 and `mColumns - 1`
78         // Adding `mColumns - 1` will guarantee that the final value F will satisfy
79         // `mRows * mColumns <= F < (mRows + 1) * mColumns
80         if (mRows > (tilesCount + mColumns - 1) / mColumns) {
81             mRows = (tilesCount + mColumns - 1) / mColumns
82         }
83         return previousRows != mRows
84     }
85 }