Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui; |
| 18 | |
| 19 | import android.content.Context; |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 20 | import android.content.res.Configuration; |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 24 | import android.widget.BaseAdapter; |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 25 | import android.widget.LinearLayout; |
| 26 | |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 27 | import com.android.systemui.util.leak.RotationUtils; |
| 28 | |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 29 | /** |
| 30 | * Layout class representing the Global Actions menu which appears when the power button is held. |
| 31 | */ |
| 32 | public abstract class MultiListLayout extends LinearLayout { |
Aaron Heuckroth | 166392f | 2019-01-17 16:50:59 -0500 | [diff] [blame] | 33 | protected boolean mHasOutsideTouch; |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 34 | protected MultiListAdapter mAdapter; |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 35 | protected int mRotation; |
| 36 | protected RotationListener mRotationListener; |
| 37 | |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 38 | public MultiListLayout(Context context, AttributeSet attrs) { |
| 39 | super(context, attrs); |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 40 | mRotation = RotationUtils.getRotation(context); |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | protected abstract ViewGroup getSeparatedView(); |
| 44 | |
| 45 | protected abstract ViewGroup getListView(); |
| 46 | |
| 47 | /** |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 48 | * Sets the divided view, which may have a differently-colored background. |
| 49 | */ |
| 50 | public abstract void setDivisionView(View v); |
| 51 | |
| 52 | /** |
| 53 | * Set the view accessibility delegate for the list view container. |
| 54 | */ |
| 55 | public void setListViewAccessibilityDelegate(View.AccessibilityDelegate delegate) { |
| 56 | getListView().setAccessibilityDelegate(delegate); |
| 57 | } |
| 58 | |
| 59 | protected void setSeparatedViewVisibility(boolean visible) { |
Aran Ink | 780d450 | 2020-02-14 10:39:58 -0500 | [diff] [blame] | 60 | ViewGroup separatedView = getSeparatedView(); |
| 61 | if (separatedView != null) { |
| 62 | separatedView.setVisibility(visible ? View.VISIBLE : View.GONE); |
| 63 | } |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 67 | * Sets the adapter used to inflate items. |
| 68 | */ |
| 69 | public void setAdapter(MultiListAdapter adapter) { |
| 70 | mAdapter = adapter; |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Sets this layout to respond to an outside touch listener. |
| 75 | */ |
| 76 | public void setOutsideTouchListener(OnClickListener onClickListener) { |
| 77 | mHasOutsideTouch = true; |
| 78 | requestLayout(); |
| 79 | setOnClickListener(onClickListener); |
| 80 | setClickable(true); |
| 81 | setFocusable(true); |
| 82 | } |
| 83 | |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 84 | @Override |
| 85 | protected void onConfigurationChanged(Configuration newConfig) { |
| 86 | super.onConfigurationChanged(newConfig); |
| 87 | int newRotation = RotationUtils.getRotation(mContext); |
| 88 | if (newRotation != mRotation) { |
| 89 | rotate(mRotation, newRotation); |
| 90 | mRotation = newRotation; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | protected void rotate(int from, int to) { |
| 95 | if (mRotationListener != null) { |
| 96 | mRotationListener.onRotate(from, to); |
| 97 | } |
| 98 | } |
| 99 | |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 100 | /** |
| 101 | * Update the list of items in both the separated and list views. |
| 102 | * For this to work, mAdapter must already have been set. |
| 103 | */ |
| 104 | public void updateList() { |
| 105 | if (mAdapter == null) { |
| 106 | throw new IllegalStateException("mAdapter must be set before calling updateList"); |
| 107 | } |
| 108 | onUpdateList(); |
| 109 | } |
| 110 | |
Aaron Heuckroth | e2d92ac | 2019-05-01 10:44:59 -0400 | [diff] [blame] | 111 | protected void removeAllSeparatedViews() { |
| 112 | ViewGroup separated = getSeparatedView(); |
| 113 | if (separated != null) { |
| 114 | separated.removeAllViews(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | protected void removeAllListViews() { |
| 119 | ViewGroup list = getListView(); |
| 120 | if (list != null) { |
| 121 | list.removeAllViews(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | protected void removeAllItems() { |
| 126 | removeAllListViews(); |
| 127 | removeAllSeparatedViews(); |
| 128 | } |
| 129 | |
Aaron Heuckroth | f19d272 | 2019-03-11 17:06:02 -0400 | [diff] [blame] | 130 | protected void onUpdateList() { |
Aaron Heuckroth | 5e28f8c8 | 2019-04-24 13:26:12 -0400 | [diff] [blame] | 131 | removeAllItems(); |
Aaron Heuckroth | f19d272 | 2019-03-11 17:06:02 -0400 | [diff] [blame] | 132 | setSeparatedViewVisibility(mAdapter.hasSeparatedItems()); |
| 133 | } |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 134 | |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 135 | public void setRotationListener(RotationListener listener) { |
| 136 | mRotationListener = listener; |
| 137 | } |
| 138 | |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 139 | /** |
| 140 | * Retrieve the MultiListLayout associated with the given view. |
| 141 | */ |
| 142 | public static MultiListLayout get(View v) { |
| 143 | if (v instanceof MultiListLayout) return (MultiListLayout) v; |
| 144 | if (v.getParent() instanceof View) { |
| 145 | return get((View) v.getParent()); |
| 146 | } |
| 147 | return null; |
| 148 | } |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 149 | |
Aaron Heuckroth | 4ea2fdb | 2019-02-14 16:28:35 -0500 | [diff] [blame] | 150 | /** |
| 151 | * Interface to provide callbacks which trigger when this list detects a rotation. |
| 152 | */ |
| 153 | public interface RotationListener { |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 154 | void onRotate(int from, int to); |
| 155 | } |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 156 | |
| 157 | /** |
Aaron Heuckroth | c792661 | 2019-04-02 14:21:57 -0400 | [diff] [blame] | 158 | * Get the X offset in pixels for use when animating the view onto or off of the screen. |
| 159 | */ |
| 160 | public abstract float getAnimationOffsetX(); |
| 161 | |
| 162 | /** |
| 163 | * Get the Y offset in pixels for use when animating the view onto or off of the screen. |
| 164 | */ |
| 165 | public abstract float getAnimationOffsetY(); |
| 166 | |
| 167 | /** |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 168 | * Adapter class for converting items into child views for MultiListLayout and handling |
| 169 | * callbacks for input events. |
| 170 | */ |
| 171 | public abstract static class MultiListAdapter extends BaseAdapter { |
| 172 | /** |
Aaron Heuckroth | 5e28f8c8 | 2019-04-24 13:26:12 -0400 | [diff] [blame] | 173 | * Counts the number of items to be rendered in the separated view. |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 174 | */ |
Aaron Heuckroth | 5e28f8c8 | 2019-04-24 13:26:12 -0400 | [diff] [blame] | 175 | public abstract int countSeparatedItems(); |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 176 | |
| 177 | /** |
Aaron Heuckroth | 5e28f8c8 | 2019-04-24 13:26:12 -0400 | [diff] [blame] | 178 | * Counts the number of items be rendered in the list view. |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 179 | */ |
Aaron Heuckroth | 5e28f8c8 | 2019-04-24 13:26:12 -0400 | [diff] [blame] | 180 | public abstract int countListItems(); |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * Callback to run when an individual item is clicked or pressed. |
| 184 | * @param position The index of the item which was clicked. |
| 185 | */ |
| 186 | public abstract void onClickItem(int position); |
| 187 | |
| 188 | /** |
| 189 | * Callback to run when an individual item is long-clicked or long-pressed. |
| 190 | * @param position The index of the item which was long-clicked. |
| 191 | * @return True if the long-click was handled, false otherwise. |
| 192 | */ |
| 193 | public abstract boolean onLongClickItem(int position); |
Aaron Heuckroth | f19d272 | 2019-03-11 17:06:02 -0400 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * Determines whether the mAdapter contains any separated items, used to determine whether |
| 197 | * or not to hide the separated list from view. |
| 198 | */ |
| 199 | public boolean hasSeparatedItems() { |
Aaron Heuckroth | 5e28f8c8 | 2019-04-24 13:26:12 -0400 | [diff] [blame] | 200 | return countSeparatedItems() > 0; |
Aaron Heuckroth | f19d272 | 2019-03-11 17:06:02 -0400 | [diff] [blame] | 201 | } |
Aaron Heuckroth | 5e28f8c8 | 2019-04-24 13:26:12 -0400 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * Determines whether the item at the given index should be rendered in the separarted view. |
| 205 | * @param position The index of the item. |
| 206 | */ |
| 207 | public abstract boolean shouldBeSeparated(int position); |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 208 | } |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 209 | } |