summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/MultiListLayout.java
blob: d153fb039802041d950433e504428cdee107c9a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;

import com.android.systemui.util.leak.RotationUtils;

/**
 * Layout class representing the Global Actions menu which appears when the power button is held.
 */
public abstract class MultiListLayout extends LinearLayout {
    protected boolean mHasOutsideTouch;
    protected MultiListAdapter mAdapter;
    protected int mRotation;
    protected RotationListener mRotationListener;

    public MultiListLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mRotation = RotationUtils.getRotation(context);
    }

    protected abstract ViewGroup getSeparatedView();

    protected abstract ViewGroup getListView();

    /**
     * Sets the divided view, which may have a differently-colored background.
     */
    public abstract void setDivisionView(View v);

    /**
     * Set the view accessibility delegate for the list view container.
     */
    public void setListViewAccessibilityDelegate(View.AccessibilityDelegate delegate) {
        getListView().setAccessibilityDelegate(delegate);
    }

    protected void setSeparatedViewVisibility(boolean visible) {
        getSeparatedView().setVisibility(visible ? View.VISIBLE : View.GONE);
    }

    /**
     * Sets the adapter used to inflate items.
     */
    public void setAdapter(MultiListAdapter adapter) {
        mAdapter = adapter;
    }

    /**
     * Sets this layout to respond to an outside touch listener.
     */
    public void setOutsideTouchListener(OnClickListener onClickListener) {
        mHasOutsideTouch = true;
        requestLayout();
        setOnClickListener(onClickListener);
        setClickable(true);
        setFocusable(true);
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        int newRotation = RotationUtils.getRotation(mContext);
        if (newRotation != mRotation) {
            rotate(mRotation, newRotation);
            mRotation = newRotation;
        }
    }

    protected void rotate(int from, int to) {
        if (mRotationListener != null) {
            mRotationListener.onRotate(from, to);
        }
    }

    /**
     * Update the list of items in both the separated and list views.
     * For this to work, mAdapter must already have been set.
     */
    public void updateList() {
        if (mAdapter == null) {
            throw new IllegalStateException("mAdapter must be set before calling updateList");
        }
        onUpdateList();
    }

    protected void removeAllSeparatedViews() {
        ViewGroup separated = getSeparatedView();
        if (separated != null) {
            separated.removeAllViews();
        }
    }

    protected void removeAllListViews() {
        ViewGroup list = getListView();
        if (list != null) {
            list.removeAllViews();
        }
    }

    protected void removeAllItems() {
        removeAllListViews();
        removeAllSeparatedViews();
    }

    protected void onUpdateList() {
        removeAllItems();
        setSeparatedViewVisibility(mAdapter.hasSeparatedItems());
    }

    public void setRotationListener(RotationListener listener) {
        mRotationListener = listener;
    }

    /**
     * Retrieve the MultiListLayout associated with the given view.
     */
    public static MultiListLayout get(View v) {
        if (v instanceof MultiListLayout) return (MultiListLayout) v;
        if (v.getParent() instanceof View) {
            return get((View) v.getParent());
        }
        return null;
    }

    /**
     * Interface to provide callbacks which trigger when this list detects a rotation.
     */
    public interface RotationListener {
        void onRotate(int from, int to);
    }

    /**
     * Get the X offset in pixels for use when animating the view onto or off of the screen.
     */
    public abstract float getAnimationOffsetX();

    /**
     * Get the Y offset in pixels for use when animating the view onto or off of the screen.
     */
    public abstract float getAnimationOffsetY();

    /**
     * Adapter class for converting items into child views for MultiListLayout and handling
     * callbacks for input events.
     */
    public abstract static class MultiListAdapter extends BaseAdapter {
        /**
         * Counts the number of items to be rendered in the separated view.
         */
        public abstract int countSeparatedItems();

        /**
         * Counts the number of items be rendered in the list view.
         */
        public abstract int countListItems();

        /**
         * Callback to run when an individual item is clicked or pressed.
         * @param position The index of the item which was clicked.
         */
        public abstract void onClickItem(int position);

        /**
         * Callback to run when an individual item is long-clicked or long-pressed.
         * @param position The index of the item which was long-clicked.
         * @return True if the long-click was handled, false otherwise.
         */
        public abstract boolean onLongClickItem(int position);

        /**
         * Determines whether the mAdapter contains any separated items, used to determine whether
         * or not to hide the separated list from view.
         */
        public boolean hasSeparatedItems() {
            return countSeparatedItems() > 0;
        }

        /**
         * Determines whether the item at the given index should be rendered in the separarted view.
         * @param position The index of the item.
         */
        public abstract boolean shouldBeSeparated(int position);
    }
}