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