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 | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 29 | import java.util.ArrayList; |
| 30 | |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 31 | /** |
| 32 | * Layout class representing the Global Actions menu which appears when the power button is held. |
| 33 | */ |
| 34 | public abstract class MultiListLayout extends LinearLayout { |
Aaron Heuckroth | 166392f | 2019-01-17 16:50:59 -0500 | [diff] [blame] | 35 | protected boolean mHasOutsideTouch; |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 36 | protected boolean mSeparated; |
| 37 | protected MultiListAdapter mAdapter; |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 38 | |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 39 | protected int mRotation; |
| 40 | protected RotationListener mRotationListener; |
| 41 | |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 42 | public MultiListLayout(Context context, AttributeSet attrs) { |
| 43 | super(context, attrs); |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 44 | mRotation = RotationUtils.getRotation(context); |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | protected abstract ViewGroup getSeparatedView(); |
| 48 | |
| 49 | protected abstract ViewGroup getListView(); |
| 50 | |
| 51 | /** |
| 52 | * Removes all child items from the separated and list views, if they exist. |
| 53 | */ |
| 54 | public abstract void removeAllItems(); |
| 55 | |
| 56 | /** |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 57 | * Sets the divided view, which may have a differently-colored background. |
| 58 | */ |
| 59 | public abstract void setDivisionView(View v); |
| 60 | |
| 61 | /** |
| 62 | * Set the view accessibility delegate for the list view container. |
| 63 | */ |
| 64 | public void setListViewAccessibilityDelegate(View.AccessibilityDelegate delegate) { |
| 65 | getListView().setAccessibilityDelegate(delegate); |
| 66 | } |
| 67 | |
| 68 | protected void setSeparatedViewVisibility(boolean visible) { |
| 69 | getSeparatedView().setVisibility(visible ? View.VISIBLE : View.GONE); |
| 70 | } |
| 71 | |
| 72 | /** |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 73 | * Sets whether the separated view should be shown, and handles updating visibility on |
| 74 | * that view. |
| 75 | */ |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 76 | public void setSeparated(boolean separated) { |
| 77 | mSeparated = separated; |
| 78 | setSeparatedViewVisibility(separated); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Sets the adapter used to inflate items. |
| 83 | */ |
| 84 | public void setAdapter(MultiListAdapter adapter) { |
| 85 | mAdapter = adapter; |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Sets this layout to respond to an outside touch listener. |
| 90 | */ |
| 91 | public void setOutsideTouchListener(OnClickListener onClickListener) { |
| 92 | mHasOutsideTouch = true; |
| 93 | requestLayout(); |
| 94 | setOnClickListener(onClickListener); |
| 95 | setClickable(true); |
| 96 | setFocusable(true); |
| 97 | } |
| 98 | |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 99 | @Override |
| 100 | protected void onConfigurationChanged(Configuration newConfig) { |
| 101 | super.onConfigurationChanged(newConfig); |
| 102 | int newRotation = RotationUtils.getRotation(mContext); |
| 103 | if (newRotation != mRotation) { |
| 104 | rotate(mRotation, newRotation); |
| 105 | mRotation = newRotation; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | protected void rotate(int from, int to) { |
| 110 | if (mRotationListener != null) { |
| 111 | mRotationListener.onRotate(from, to); |
| 112 | } |
| 113 | } |
| 114 | |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 115 | /** |
| 116 | * Update the list of items in both the separated and list views. |
| 117 | * For this to work, mAdapter must already have been set. |
| 118 | */ |
| 119 | public void updateList() { |
| 120 | if (mAdapter == null) { |
| 121 | throw new IllegalStateException("mAdapter must be set before calling updateList"); |
| 122 | } |
| 123 | onUpdateList(); |
| 124 | } |
| 125 | |
| 126 | protected abstract void onUpdateList(); |
| 127 | |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 128 | public void setRotationListener(RotationListener listener) { |
| 129 | mRotationListener = listener; |
| 130 | } |
| 131 | |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 132 | /** |
| 133 | * Retrieve the MultiListLayout associated with the given view. |
| 134 | */ |
| 135 | public static MultiListLayout get(View v) { |
| 136 | if (v instanceof MultiListLayout) return (MultiListLayout) v; |
| 137 | if (v.getParent() instanceof View) { |
| 138 | return get((View) v.getParent()); |
| 139 | } |
| 140 | return null; |
| 141 | } |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 142 | |
Aaron Heuckroth | 4ea2fdb | 2019-02-14 16:28:35 -0500 | [diff] [blame] | 143 | /** |
| 144 | * Interface to provide callbacks which trigger when this list detects a rotation. |
| 145 | */ |
| 146 | public interface RotationListener { |
Aaron Heuckroth | 75e249f | 2019-02-01 15:59:57 -0500 | [diff] [blame] | 147 | void onRotate(int from, int to); |
| 148 | } |
Aaron Heuckroth | 57d60d2 | 2019-03-05 14:00:12 -0500 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * Adapter class for converting items into child views for MultiListLayout and handling |
| 152 | * callbacks for input events. |
| 153 | */ |
| 154 | public abstract static class MultiListAdapter extends BaseAdapter { |
| 155 | /** |
| 156 | * Creates an ArrayList of items which should be rendered in the separated view. |
| 157 | * @param useSeparatedView is true if the separated view will be used, false otherwise. |
| 158 | */ |
| 159 | public abstract ArrayList getSeparatedItems(boolean useSeparatedView); |
| 160 | |
| 161 | /** |
| 162 | * Creates an ArrayList of items which should be rendered in the list view. |
| 163 | * @param useSeparatedView True if the separated view will be used, false otherwise. |
| 164 | */ |
| 165 | public abstract ArrayList getListItems(boolean useSeparatedView); |
| 166 | |
| 167 | /** |
| 168 | * Callback to run when an individual item is clicked or pressed. |
| 169 | * @param position The index of the item which was clicked. |
| 170 | */ |
| 171 | public abstract void onClickItem(int position); |
| 172 | |
| 173 | /** |
| 174 | * Callback to run when an individual item is long-clicked or long-pressed. |
| 175 | * @param position The index of the item which was long-clicked. |
| 176 | * @return True if the long-click was handled, false otherwise. |
| 177 | */ |
| 178 | public abstract boolean onLongClickItem(int position); |
| 179 | } |
Aaron Heuckroth | f708d47 | 2019-01-10 16:54:51 -0500 | [diff] [blame] | 180 | } |