blob: 8c49d56ae34883234fc35af255354c74b78535ea [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;
24import android.widget.LinearLayout;
25
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050026import com.android.systemui.util.leak.RotationUtils;
27
Aaron Heuckrothf708d472019-01-10 16:54:51 -050028/**
29 * Layout class representing the Global Actions menu which appears when the power button is held.
30 */
31public abstract class MultiListLayout extends LinearLayout {
Aaron Heuckroth166392f2019-01-17 16:50:59 -050032 protected boolean mHasOutsideTouch;
33 protected boolean mHasSeparatedView;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050034
Aaron Heuckroth166392f2019-01-17 16:50:59 -050035 protected int mExpectedSeparatedItemCount;
36 protected int mExpectedListItemCount;
Aaron Heuckrothf708d472019-01-10 16:54:51 -050037
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050038 protected int mRotation;
39 protected RotationListener mRotationListener;
40
Aaron Heuckrothf708d472019-01-10 16:54:51 -050041 public MultiListLayout(Context context, AttributeSet attrs) {
42 super(context, attrs);
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050043 mRotation = RotationUtils.getRotation(context);
Aaron Heuckrothf708d472019-01-10 16:54:51 -050044 }
45
46 protected abstract ViewGroup getSeparatedView();
47
48 protected abstract ViewGroup getListView();
49
50 /**
51 * Removes all child items from the separated and list views, if they exist.
52 */
53 public abstract void removeAllItems();
54
55 /**
56 * Get the parent view which will be used to contain the item at the specified index.
57 * @param separated Whether or not this index refers to a position in the separated or list
58 * container.
59 * @param index The index of the item within the container.
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050060 * @param reverse If the MultiListLayout contains sub-lists within the list container, reverse
61 * the order that they are filled.
Aaron Heuckrothf708d472019-01-10 16:54:51 -050062 * @return The parent ViewGroup which will be used to contain the specified item
63 * after it has been added to the layout.
64 */
Aaron Heuckroth75e249f2019-02-01 15:59:57 -050065 public abstract ViewGroup getParentView(boolean separated, int index, boolean reverse);
Aaron Heuckrothf708d472019-01-10 16:54:51 -050066
67 /**
68 * Sets the divided view, which may have a differently-colored background.
69 */
70 public abstract void setDivisionView(View v);
71
72 /**
73 * Set the view accessibility delegate for the list view container.
74 */
75 public void setListViewAccessibilityDelegate(View.AccessibilityDelegate delegate) {
76 getListView().setAccessibilityDelegate(delegate);
77 }
78
79 protected void setSeparatedViewVisibility(boolean visible) {
80 getSeparatedView().setVisibility(visible ? View.VISIBLE : View.GONE);
81 }
82
83 /**
84 * Sets the number of items expected to be rendered in the separated container. This allows the
85 * layout to correctly determine which parent containers will be used for items before they have
86 * beenadded to the layout.
87 * @param count The number of items expected.
88 */
89 public void setExpectedSeparatedItemCount(int count) {
90 mExpectedSeparatedItemCount = count;
91 }
92
93 /**
94 * Sets the number of items expected to be rendered in the list container. This allows the
95 * layout to correctly determine which parent containers will be used for items before they have
96 * beenadded to the layout.
97 * @param count The number of items expected.
98 */
99 public void setExpectedListItemCount(int count) {
100 mExpectedListItemCount = count;
101 }
102
103 /**
104 * Sets whether the separated view should be shown, and handles updating visibility on
105 * that view.
106 */
107 public void setHasSeparatedView(boolean hasSeparatedView) {
108 mHasSeparatedView = hasSeparatedView;
109 setSeparatedViewVisibility(hasSeparatedView);
110 }
111
112 /**
113 * Sets this layout to respond to an outside touch listener.
114 */
115 public void setOutsideTouchListener(OnClickListener onClickListener) {
116 mHasOutsideTouch = true;
117 requestLayout();
118 setOnClickListener(onClickListener);
119 setClickable(true);
120 setFocusable(true);
121 }
122
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500123 @Override
124 protected void onConfigurationChanged(Configuration newConfig) {
125 super.onConfigurationChanged(newConfig);
126 int newRotation = RotationUtils.getRotation(mContext);
127 if (newRotation != mRotation) {
128 rotate(mRotation, newRotation);
129 mRotation = newRotation;
130 }
131 }
132
133 protected void rotate(int from, int to) {
134 if (mRotationListener != null) {
135 mRotationListener.onRotate(from, to);
136 }
137 }
138
139 public void setRotationListener(RotationListener listener) {
140 mRotationListener = listener;
141 }
142
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500143 /**
144 * Retrieve the MultiListLayout associated with the given view.
145 */
146 public static MultiListLayout get(View v) {
147 if (v instanceof MultiListLayout) return (MultiListLayout) v;
148 if (v.getParent() instanceof View) {
149 return get((View) v.getParent());
150 }
151 return null;
152 }
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500153
Aaron Heuckroth4ea2fdb2019-02-14 16:28:35 -0500154 /**
155 * Interface to provide callbacks which trigger when this list detects a rotation.
156 */
157 public interface RotationListener {
Aaron Heuckroth75e249f2019-02-01 15:59:57 -0500158 void onRotate(int from, int to);
159 }
Aaron Heuckrothf708d472019-01-10 16:54:51 -0500160}