blob: 2bc4720656768cfdb1c716836401cee42002cfd8 [file] [log] [blame]
Aaron Heuckrothf708d472019-01-10 16:54:51 -05001/*
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;
18
19import android.content.Context;
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050020import android.content.res.Configuration;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050021import android.util.AttributeSet;
22import android.view.View;
23import android.view.ViewGroup;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050024import android.widget.BaseAdapter;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050025import android.widget.LinearLayout;
26
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050027import com.android.systemui.util.leak.RotationUtils;
28
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050029import java.util.ArrayList;
30
Aaron Heuckrothf708d472019-01-10 16:54:51 -050031/**
32 * Layout class representing the Global Actions menu which appears when the power button is held.
33 */
34public abstract class MultiListLayout extends LinearLayout {
Aaron Heuckroth166392f2019-01-17 16:50:59 -050035 protected boolean mHasOutsideTouch;
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050036 protected boolean mSeparated;
37 protected MultiListAdapter mAdapter;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050038
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050039 protected int mRotation;
40 protected RotationListener mRotationListener;
41
Aaron Heuckrothf708d472019-01-10 16:54:51 -050042 public MultiListLayout(Context context, AttributeSet attrs) {
43 super(context, attrs);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050044 mRotation = RotationUtils.getRotation(context);
Aaron Heuckrothf708d472019-01-10 16:54:51 -050045 }
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 Heuckrothf708d472019-01-10 16:54:51 -050057 * 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 Heuckrothf708d472019-01-10 16:54:51 -050073 * Sets whether the separated view should be shown, and handles updating visibility on
74 * that view.
75 */
Aaron Heuckroth57d60d22019-03-05 14:00:12 -050076 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 Heuckrothf708d472019-01-10 16:54:51 -050086 }
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 Heuckroth75e249f2019-02-01 15:59:57 -050099 @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 Heuckroth57d60d22019-03-05 14:00:12 -0500115 /**
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 Heuckroth75e249f2019-02-01 15:59:57 -0500128 public void setRotationListener(RotationListener listener) {
129 mRotationListener = listener;
130 }
131
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500132 /**
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 Heuckroth75e249f2019-02-01 15:59:57 -0500142
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -0500143 /**
144 * Interface to provide callbacks which trigger when this list detects a rotation.
145 */
146 public interface RotationListener {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500147 void onRotate(int from, int to);
148 }
Aaron Heuckroth57d60d22019-03-05 14:00:12 -0500149
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 Heuckrothf708d472019-01-10 16:54:51 -0500180}