1 /*
2  * Copyright (C) 2022 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.launcher3.taskbar.navbutton
18 
19 import android.content.res.Resources
20 import android.view.Gravity
21 import android.view.ViewGroup
22 import android.view.ViewGroup.LayoutParams.MATCH_PARENT
23 import android.widget.FrameLayout
24 import android.widget.ImageView
25 import android.widget.LinearLayout
26 import android.widget.Space
27 import com.android.launcher3.R
28 import com.android.launcher3.taskbar.TaskbarActivityContext
29 
30 class PhonePortraitNavLayoutter(
31     resources: Resources,
32     navBarContainer: LinearLayout,
33     endContextualContainer: ViewGroup,
34     startContextualContainer: ViewGroup,
35     imeSwitcher: ImageView?,
36     a11yButton: ImageView?,
37     space: Space?
38 ) :
39     AbstractNavButtonLayoutter(
40         resources,
41         navBarContainer,
42         endContextualContainer,
43         startContextualContainer,
44         imeSwitcher,
45         a11yButton,
46         space
47     ) {
48 
layoutButtonsnull49     override fun layoutButtons(context: TaskbarActivityContext, isA11yButtonPersistent: Boolean) {
50         val totalWidth = context.deviceProfile.widthPx
51         val homeButtonWidth =
52             resources.getDimensionPixelSize(R.dimen.taskbar_phone_home_button_size)
53         val roundedCornerContentMargin =
54             resources.getDimensionPixelSize(R.dimen.taskbar_phone_rounded_corner_content_margin)
55         val contentPadding = resources.getDimensionPixelSize(R.dimen.taskbar_phone_content_padding)
56         val contentWidth = totalWidth - roundedCornerContentMargin * 2 - contentPadding * 2
57 
58         // left:back:space(reserved for home):overview:right = 0.25:0.5:1:0.5:0.25
59         val contextualButtonWidth = contentWidth / (0.25f + 0.5f + 1f + 0.5f + 0.25f) * 0.25f
60         val sideButtonWidth = contextualButtonWidth * 2
61         val navButtonContainerWidth = contentWidth - contextualButtonWidth * 2
62 
63         val navContainerParams =
64             FrameLayout.LayoutParams(
65                 navButtonContainerWidth.toInt(),
66                 ViewGroup.LayoutParams.MATCH_PARENT
67             )
68         navContainerParams.apply {
69             topMargin = 0
70             bottomMargin = 0
71             marginEnd =
72                 (contextualButtonWidth + contentPadding + roundedCornerContentMargin).toInt()
73             marginStart =
74                 (contextualButtonWidth + contentPadding + roundedCornerContentMargin).toInt()
75         }
76 
77         // Ensure order of buttons is correct
78         navButtonContainer.removeAllViews()
79         navButtonContainer.orientation = LinearLayout.HORIZONTAL
80 
81         navButtonContainer.addView(backButton)
82         navButtonContainer.addView(homeButton)
83         navButtonContainer.addView(recentsButton)
84 
85         navButtonContainer.layoutParams = navContainerParams
86         navButtonContainer.gravity = Gravity.CENTER
87 
88         // Add the spaces in between the nav buttons
89         val spaceInBetween =
90             (navButtonContainerWidth - homeButtonWidth - sideButtonWidth * 2) / 2.0f
91         for (i in 0 until navButtonContainer.childCount) {
92             val navButton = navButtonContainer.getChildAt(i)
93             val buttonLayoutParams = navButton.layoutParams as LinearLayout.LayoutParams
94             val margin = (spaceInBetween / 2).toInt()
95             when (i) {
96                 0 -> {
97                     // First button
98                     buttonLayoutParams.marginEnd = margin
99                     buttonLayoutParams.width = sideButtonWidth.toInt()
100                 }
101                 navButtonContainer.childCount - 1 -> {
102                     // Last button
103                     buttonLayoutParams.marginStart = margin
104                     buttonLayoutParams.width = sideButtonWidth.toInt()
105                 }
106                 else -> {
107                     // other buttons
108                     buttonLayoutParams.marginStart = margin
109                     buttonLayoutParams.marginEnd = margin
110                     buttonLayoutParams.width = homeButtonWidth
111                 }
112             }
113         }
114 
115         endContextualContainer.removeAllViews()
116         startContextualContainer.removeAllViews()
117 
118         repositionContextualContainer(
119             startContextualContainer,
120             contextualButtonWidth.toInt(),
121             roundedCornerContentMargin + contentPadding,
122             0,
123             Gravity.START
124         )
125         repositionContextualContainer(
126             endContextualContainer,
127             contextualButtonWidth.toInt(),
128             0,
129             roundedCornerContentMargin + contentPadding,
130             Gravity.END
131         )
132 
133         startContextualContainer.addView(space, MATCH_PARENT, MATCH_PARENT)
134         if (imeSwitcher != null) {
135             endContextualContainer.addView(imeSwitcher)
136             imeSwitcher.layoutParams = getParamsToCenterView()
137         }
138         if (a11yButton != null) {
139             endContextualContainer.addView(a11yButton)
140             a11yButton.layoutParams = getParamsToCenterView()
141         }
142     }
143 }
144