1 /*
2  * Copyright (C) 2021 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.globalactions;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.View;
22 import android.view.ViewGroup;
23 
24 import androidx.constraintlayout.helper.widget.Flow;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 import com.android.systemui.HardwareBgDrawable;
28 import com.android.systemui.res.R;
29 
30 /**
31  * ConstraintLayout implementation of the button layout created by the global actions dialog.
32  */
33 public class GlobalActionsLayoutLite extends GlobalActionsLayout {
34 
GlobalActionsLayoutLite(Context context, AttributeSet attrs)35     public GlobalActionsLayoutLite(Context context, AttributeSet attrs) {
36         super(context, attrs);
37         setOnClickListener(v -> { }); // Prevent parent onClickListener from triggering
38     }
39 
40     @VisibleForTesting
41     @Override
shouldReverseListItems()42     protected boolean shouldReverseListItems() {
43         // Handled in XML
44         return false;
45     }
46 
47     @Override
getBackgroundDrawable(int backgroundColor)48     protected HardwareBgDrawable getBackgroundDrawable(int backgroundColor) {
49         return null;
50     }
51 
52     @Override
onUpdateList()53     public void onUpdateList() {
54         super.onUpdateList();
55         int nElementsWrap = getResources().getInteger(
56                 com.android.systemui.res.R.integer.power_menu_lite_max_columns);
57         int nChildren = getListView().getChildCount() - 1; // don't count flow element
58 
59         // Avoid having just one action on the last row if there are more than 2 columns because
60         // it looks unbalanced. Instead, bring the column size down to balance better.
61         if (nChildren == nElementsWrap + 1 && nElementsWrap > 2) {
62             nElementsWrap -= 1;
63         }
64         Flow flow = findViewById(R.id.list_flow);
65         flow.setMaxElementsWrap(nElementsWrap);
66     }
67 
68     @Override
addToListView(View v, boolean reverse)69     protected void addToListView(View v, boolean reverse) {
70         super.addToListView(v, reverse);
71         Flow flow = findViewById(R.id.list_flow);
72         flow.addView(v);
73     }
74 
75     @Override
removeAllListViews()76     protected void removeAllListViews() {
77         View flow = findViewById(R.id.list_flow);
78         super.removeAllListViews();
79 
80         // Add flow element back after clearing the list view
81         super.addToListView(flow, false);
82     }
83 
84     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)85     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
86         super.onLayout(changed, left, top, right, bottom);
87         boolean anyTruncated = false;
88         ViewGroup listView = getListView();
89 
90         // Check to see if any of the GlobalActionsItems have had their messages truncated
91         for (int i = 0; i < listView.getChildCount(); i++) {
92             View child = listView.getChildAt(i);
93             if (child instanceof GlobalActionsItem) {
94                 GlobalActionsItem item = (GlobalActionsItem) child;
95                 anyTruncated = anyTruncated || item.isTruncated();
96             }
97         }
98         // If any of the items have been truncated, set the all to single-line marquee
99         if (anyTruncated) {
100             for (int i = 0; i < listView.getChildCount(); i++) {
101                 View child = listView.getChildAt(i);
102                 if (child instanceof GlobalActionsItem) {
103                     GlobalActionsItem item = (GlobalActionsItem) child;
104                     item.setMarquee(true);
105                 }
106             }
107         }
108     }
109 
110     @VisibleForTesting
getGridItemSize()111     protected float getGridItemSize() {
112         return getContext().getResources().getDimension(R.dimen.global_actions_grid_item_height);
113     }
114 
115     @VisibleForTesting
getAnimationDistance()116     protected float getAnimationDistance() {
117         return getGridItemSize() / 2;
118     }
119 
120     @Override
getAnimationOffsetX()121     public float getAnimationOffsetX() {
122         return getAnimationDistance();
123     }
124 
125     @Override
getAnimationOffsetY()126     public float getAnimationOffsetY() {
127         return 0f;
128     }
129 }
130