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.globalactions;
18 
19 import static com.android.systemui.util.leak.RotationUtils.ROTATION_LANDSCAPE;
20 import static com.android.systemui.util.leak.RotationUtils.ROTATION_NONE;
21 import static com.android.systemui.util.leak.RotationUtils.ROTATION_SEASCAPE;
22 
23 import android.content.Context;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.view.ViewGroup;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.systemui.HardwareBgDrawable;
30 import com.android.systemui.res.R;
31 
32 /**
33  * Flat, single-row implementation of the button layout created by the global actions dialog.
34  */
35 public class GlobalActionsFlatLayout extends GlobalActionsLayout {
GlobalActionsFlatLayout(Context context, AttributeSet attrs)36     public GlobalActionsFlatLayout(Context context, AttributeSet attrs) {
37         super(context, attrs);
38     }
39 
40     @VisibleForTesting
shouldReverseListItems()41     protected boolean shouldReverseListItems() {
42         int rotation = getCurrentRotation();
43         if (rotation == ROTATION_NONE) {
44             return false;
45         }
46         if (getCurrentLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
47             return rotation == ROTATION_LANDSCAPE;
48         }
49         return rotation == ROTATION_SEASCAPE;
50     }
51 
52     @Override
getBackgroundDrawable(int backgroundColor)53     protected HardwareBgDrawable getBackgroundDrawable(int backgroundColor) {
54         return null;
55     }
56 
getOverflowButton()57     private View getOverflowButton() {
58         return findViewById(com.android.systemui.res.R.id.global_actions_overflow_button);
59     }
60 
61     @Override
addToListView(View v, boolean reverse)62     protected void addToListView(View v, boolean reverse) {
63         super.addToListView(v, reverse);
64         View overflowButton = getOverflowButton();
65         // if there's an overflow button, make sure it stays at the end
66         if (overflowButton != null) {
67             getListView().removeView(overflowButton);
68             super.addToListView(overflowButton, reverse);
69         }
70     }
71 
72     @Override
removeAllListViews()73     protected void removeAllListViews() {
74         View overflowButton = getOverflowButton();
75         super.removeAllListViews();
76         // if there's an overflow button, add it back after clearing the list views
77         if (overflowButton != null) {
78             super.addToListView(overflowButton, false);
79         }
80     }
81 
82     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)83     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
84         super.onLayout(changed, left, top, right, bottom);
85         boolean anyTruncated = false;
86         ViewGroup listView = getListView();
87         // Check to see if any of the GlobalActionsItems have had their messages truncated
88         for (int i = 0; i < listView.getChildCount(); i++) {
89             View child = listView.getChildAt(i);
90             if (child instanceof GlobalActionsItem) {
91                 GlobalActionsItem item = (GlobalActionsItem) child;
92                 anyTruncated = anyTruncated || item.isTruncated();
93             }
94         }
95         // If any of the items have been truncated, set the all to single-line marquee
96         if (anyTruncated) {
97             for (int i = 0; i < listView.getChildCount(); i++) {
98                 View child = listView.getChildAt(i);
99                 if (child instanceof GlobalActionsItem) {
100                     GlobalActionsItem item = (GlobalActionsItem) child;
101                     item.setMarquee(true);
102                 }
103             }
104         }
105     }
106 
107     @VisibleForTesting
getGridItemSize()108     protected float getGridItemSize() {
109         return getContext().getResources().getDimension(R.dimen.global_actions_grid_item_height);
110     }
111 
112     @VisibleForTesting
getAnimationDistance()113     protected float getAnimationDistance() {
114         return getGridItemSize() / 2;
115     }
116 
117     @Override
getAnimationOffsetX()118     public float getAnimationOffsetX() {
119         return getAnimationDistance();
120     }
121 
122     @Override
getAnimationOffsetY()123     public float getAnimationOffsetY() {
124         return 0f;
125     }
126 }
127