blob: 183ee458acb4dd8b839f668ebae089e688e6533d [file] [log] [blame]
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -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.globalactions;
18
19import android.content.Context;
20import android.text.TextUtils;
21import android.util.AttributeSet;
22import android.view.View;
23import android.view.ViewGroup;
24
25import com.android.internal.annotations.VisibleForTesting;
26import com.android.systemui.HardwareBgDrawable;
27import com.android.systemui.MultiListLayout;
28import com.android.systemui.R;
29import com.android.systemui.util.leak.RotationUtils;
30
31import java.util.Locale;
32
33/**
34 * Grid-based implementation of the button layout created by the global actions dialog.
35 */
36public abstract class GlobalActionsLayout extends MultiListLayout {
37
38 boolean mBackgroundsSet;
39
40 public GlobalActionsLayout(Context context, AttributeSet attrs) {
41 super(context, attrs);
42 }
43
44 private void setBackgrounds() {
Aran Ink780d4502020-02-14 10:39:58 -050045 ViewGroup listView = getListView();
46 int listBgColor = getResources().getColor(
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -040047 R.color.global_actions_grid_background, null);
Aran Ink780d4502020-02-14 10:39:58 -050048 HardwareBgDrawable listBackground = getBackgroundDrawable(listBgColor);
49 if (listBackground != null) {
50 listView.setBackground(listBackground);
51 }
52
53 ViewGroup separatedView = getSeparatedView();
54
55 if (separatedView != null) {
56 int separatedBgColor = getResources().getColor(
57 R.color.global_actions_separated_background, null);
58 HardwareBgDrawable separatedBackground = getBackgroundDrawable(separatedBgColor);
59 if (separatedBackground != null) {
60 getSeparatedView().setBackground(separatedBackground);
61 }
62 }
63 }
64
65 protected HardwareBgDrawable getBackgroundDrawable(int backgroundColor) {
66 HardwareBgDrawable background = new HardwareBgDrawable(true, true, getContext());
67 background.setTint(backgroundColor);
68 return background;
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -040069 }
70
71 @Override
72 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
73 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
74
75 // backgrounds set only once, the first time onMeasure is called after inflation
76 if (getListView() != null && !mBackgroundsSet) {
77 setBackgrounds();
78 mBackgroundsSet = true;
79 }
80 }
81
82 protected void addToListView(View v, boolean reverse) {
83 if (reverse) {
84 getListView().addView(v, 0);
85 } else {
86 getListView().addView(v);
87 }
88 }
89
90 protected void addToSeparatedView(View v, boolean reverse) {
Aran Ink780d4502020-02-14 10:39:58 -050091 ViewGroup separated = getSeparatedView();
92 if (separated != null) {
93 if (reverse) {
94 separated.addView(v, 0);
95 } else {
96 separated.addView(v);
97 }
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -040098 } else {
Aran Ink780d4502020-02-14 10:39:58 -050099 // if no separated view exists, just use the list view
100 addToListView(v, reverse);
Aaron Heuckrothe2d92ac2019-05-01 10:44:59 -0400101 }
102 }
103
104 @VisibleForTesting
105 protected int getCurrentLayoutDirection() {
106 return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault());
107 }
108
109 @VisibleForTesting
110 protected int getCurrentRotation() {
111 return RotationUtils.getRotation(mContext);
112 }
113
114 /**
115 * Determines whether the ListGridLayout should reverse the ordering of items within sublists.
116 * Used for RTL languages to ensure that items appear in the same positions, without having to
117 * override layoutDirection, which breaks Talkback ordering.
118 */
119 protected abstract boolean shouldReverseListItems();
120
121 @Override
122 public void onUpdateList() {
123 super.onUpdateList();
124
125 ViewGroup separatedView = getSeparatedView();
126 ViewGroup listView = getListView();
127
128 for (int i = 0; i < mAdapter.getCount(); i++) {
129 // generate the view item
130 View v;
131 boolean separated = mAdapter.shouldBeSeparated(i);
132 if (separated) {
133 v = mAdapter.getView(i, null, separatedView);
134 } else {
135 v = mAdapter.getView(i, null, listView);
136 }
137 if (separated) {
138 addToSeparatedView(v, false);
139 } else {
140 addToListView(v, shouldReverseListItems());
141 }
142 }
143 }
144
145 @Override
146 protected ViewGroup getSeparatedView() {
147 return findViewById(R.id.separated_button);
148 }
149
150 @Override
151 protected ViewGroup getListView() {
152 return findViewById(android.R.id.list);
153 }
154
155 protected View getWrapper() {
156 return getChildAt(0);
157 }
158
159 /**
160 * Not used in this implementation of the Global Actions Menu, but necessary for some others.
161 */
162 @Override
163 public void setDivisionView(View v) {
164 // do nothing
165 }
166}