summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/statusbar/QueueView.java
blob: 1da2e5e50180ff4c8e2e52d371b86e0d77fce99b (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * Copyright (C) 2015 The CyanogenMod 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.statusbar;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.media.MediaDescription;
import android.media.MediaMetadata;
import android.media.session.MediaController;
import android.media.session.MediaSession;
import android.media.session.PlaybackState;
import android.os.Handler;
import android.provider.Settings;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.android.systemui.R;
import com.android.systemui.cm.UserContentObserver;

import java.util.ArrayList;
import java.util.List;

public class QueueView extends LinearLayout implements
        QueueViewRow.UserRowInteractionListener, AdapterView.OnItemClickListener,
        AdapterView.OnItemLongClickListener {

    private static final String TAG = QueueView.class.getSimpleName();
    private static final boolean DEBUG = MediaExpandableNotificationRow.DEBUG;

    private MediaController mController;

    private List<MediaSession.QueueItem> mQueue = new ArrayList<>(getMaxQueueRowCount());

    private QueueItemAdapter mAdapter;
    private ListView mList;
    private boolean mQueueEnabled;

    long mLastUserInteraction = -1;

    private MediaController.Callback mCallback = new MediaController.Callback() {
        @Override
        public void onPlaybackStateChanged(@NonNull PlaybackState state) {
            super.onPlaybackStateChanged(state);

            if (getParent() != null && updateQueue(mController.getQueue())) {
                getParent().requestLayout();
            }
        }

        @Override
        public void onSessionDestroyed() {
            if (DEBUG) Log.d(TAG, "onSessionDestroyed() called with " + "");
            super.onSessionDestroyed();
            setController(null);
        }
    };

    public QueueView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mAdapter = new QueueItemAdapter(context);
        setClipToOutline(false);
        setClipToPadding(false);
    }

    public void setQueueEnabled(boolean enabled) {
        mQueueEnabled = enabled;
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mList = (ListView) findViewById(R.id.queue_list);
        mList.setItemsCanFocus(true);
        mList.setDrawSelectorOnTop(true);
        mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        mList.setAdapter(mAdapter);
        mList.setOnItemLongClickListener(this);
        mList.setOnItemClickListener(this);
        mList.setVerticalScrollBarEnabled(false);
    }

    private class QueueItemAdapter extends ArrayAdapter<MediaSession.QueueItem> {

        public QueueItemAdapter(Context context) {
            super(context, R.layout.queue_adapter_row, mQueue);
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public long getItemId(int position) {
            if (position > getCount() - 1) {
                return -1;
            }
            return getItem(position).getQueueId();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final MediaSession.QueueItem queueItem = getItem(position);

            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.queue_adapter_row, parent, false);
            }

            QueueViewRow row = (QueueViewRow) convertView;
            row.setHotSpotChangeListener(QueueView.this);

            row.setQueueItem(queueItem);

            return convertView;
        }

        @Override
        public int getCount() {
            if (!mQueueEnabled) {
                return 0;
            }
            return super.getCount();
        }
    }

    public boolean isUserSelectingRow() {
        final long delta = System.currentTimeMillis() - mLastUserInteraction;
        if (DEBUG) Log.i(TAG, "isUserSelectingRow() delta=" + delta);

        if (mLastUserInteraction > 0 && delta < 500) {
            if (DEBUG) Log.w(TAG, "user selecting row bc of hotspot change.");
            return true;
        }

        return false;
    }

    public int getMaxQueueRowCount() {
        return MediaExpandableNotificationRow.MAX_QUEUE_ENTRIES;
    }

    public int getCurrentQueueRowCount() {
        if (mAdapter == null) {
            return 0;
        }
        return mAdapter.getCount();
    }

    @Override
    public void onHotSpotChanged(float x, float y) {
        mLastUserInteraction = System.currentTimeMillis();
    }

    @Override
    public void onDrawableStateChanged() {
        mLastUserInteraction = System.currentTimeMillis();
    }

    /**
     * @param queue
     * @return whether the queue size has changed
     */
    public boolean updateQueue(List<MediaSession.QueueItem> queue) {
        int queueSizeBefore = mAdapter.getCount();

        mQueue.clear();

        if (queue != null) {
            // add everything *after* the currently playing item
            boolean foundNowPlaying = false;

            final PlaybackState playbackState = mController.getPlaybackState();

            long activeQueueId = -1;
            if (playbackState != null) {
                activeQueueId = playbackState.getActiveQueueItemId();
            }

            for (int i = 0; i < queue.size() && mQueue.size() < getMaxQueueRowCount(); i++) {
                final MediaSession.QueueItem item = queue.get(i);
                if (!foundNowPlaying
                        && activeQueueId != -1
                        && activeQueueId == item.getQueueId()) {
                    foundNowPlaying = true;
                    continue;
                }
                if (foundNowPlaying) {
                    mQueue.add(item);
                }
            }

            // add everything
            if (!foundNowPlaying) {
                for(int i = 0; i < getMaxQueueRowCount() && i < queue.size(); i++) {
                    mQueue.add(queue.get(i));
                }
            }
        }
        mAdapter.notifyDataSetChanged();

        return mAdapter.getCount() != queueSizeBefore;
    }

    public boolean setController(MediaController controller) {
        if (mController != null) {
            mController.unregisterCallback(mCallback);
        }
        mController = controller;
        if (mController != null) {
            mController.registerCallback(mCallback);
        }

        return updateQueue(mController != null
                ? mController.getQueue() : null);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final MediaSession.QueueItem itemAtPosition = (MediaSession.QueueItem)
                parent.getItemAtPosition(position);
        if (itemAtPosition != null && mController != null) {
            mController.getTransportControls().skipToQueueItem(itemAtPosition.getQueueId());
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return true;
    }

}