This repository was archived by the owner on Sep 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathVideoPlayerGlue.java
More file actions
182 lines (157 loc) · 6.92 KB
/
VideoPlayerGlue.java
File metadata and controls
182 lines (157 loc) · 6.92 KB
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
/*
* Copyright (C) 2017 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.example.android.tvleanback.player;
import android.content.Context;
import androidx.leanback.media.PlaybackTransportControlGlue;
import androidx.leanback.widget.Action;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.leanback.widget.PlaybackControlsRow;
import com.google.android.exoplayer2.ext.leanback.LeanbackPlayerAdapter;
import java.util.concurrent.TimeUnit;
/**
* Manages customizing the actions in the {@link PlaybackControlsRow}. Adds and manages the
* following actions to the primary and secondary controls:
*
* <ul>
* <li>{@link androidx.leanback.widget.PlaybackControlsRow.RepeatAction}
* <li>{@link androidx.leanback.widget.PlaybackControlsRow.ThumbsDownAction}
* <li>{@link androidx.leanback.widget.PlaybackControlsRow.ThumbsUpAction}
* <li>{@link androidx.leanback.widget.PlaybackControlsRow.SkipPreviousAction}
* <li>{@link androidx.leanback.widget.PlaybackControlsRow.SkipNextAction}
* <li>{@link androidx.leanback.widget.PlaybackControlsRow.FastForwardAction}
* <li>{@link androidx.leanback.widget.PlaybackControlsRow.RewindAction}
* </ul>
*
* Note that the superclass, {@link PlaybackTransportControlGlue}, manages the playback controls
* row.
*/
public class VideoPlayerGlue extends PlaybackTransportControlGlue<LeanbackPlayerAdapter> {
private static final long TEN_SECONDS = TimeUnit.SECONDS.toMillis(10);
/** Listens for when skip to next and previous actions have been dispatched. */
public interface OnActionClickedListener {
/** Skip to the previous item in the queue. */
void onPrevious();
/** Skip to the next item in the queue. */
void onNext();
}
private final OnActionClickedListener mActionListener;
private PlaybackControlsRow.RepeatAction mRepeatAction;
private PlaybackControlsRow.ThumbsUpAction mThumbsUpAction;
private PlaybackControlsRow.ThumbsDownAction mThumbsDownAction;
private PlaybackControlsRow.SkipPreviousAction mSkipPreviousAction;
private PlaybackControlsRow.SkipNextAction mSkipNextAction;
private PlaybackControlsRow.FastForwardAction mFastForwardAction;
private PlaybackControlsRow.RewindAction mRewindAction;
public VideoPlayerGlue(
Context context,
LeanbackPlayerAdapter playerAdapter,
OnActionClickedListener actionListener) {
super(context, playerAdapter);
mActionListener = actionListener;
mSkipPreviousAction = new PlaybackControlsRow.SkipPreviousAction(context);
mSkipNextAction = new PlaybackControlsRow.SkipNextAction(context);
mFastForwardAction = new PlaybackControlsRow.FastForwardAction(context);
mRewindAction = new PlaybackControlsRow.RewindAction(context);
mThumbsUpAction = new PlaybackControlsRow.ThumbsUpAction(context);
mThumbsUpAction.setIndex(PlaybackControlsRow.ThumbsUpAction.INDEX_OUTLINE);
mThumbsDownAction = new PlaybackControlsRow.ThumbsDownAction(context);
mThumbsDownAction.setIndex(PlaybackControlsRow.ThumbsDownAction.INDEX_OUTLINE);
mRepeatAction = new PlaybackControlsRow.RepeatAction(context);
}
@Override
protected void onCreatePrimaryActions(ArrayObjectAdapter adapter) {
// Order matters, super.onCreatePrimaryActions() will create the play / pause action.
// Will display as follows:
// play/pause, previous, rewind, fast forward, next
// > /|| |< << >> >|
super.onCreatePrimaryActions(adapter);
adapter.add(mSkipPreviousAction);
adapter.add(mRewindAction);
adapter.add(mFastForwardAction);
adapter.add(mSkipNextAction);
}
@Override
protected void onCreateSecondaryActions(ArrayObjectAdapter adapter) {
super.onCreateSecondaryActions(adapter);
adapter.add(mThumbsDownAction);
adapter.add(mThumbsUpAction);
adapter.add(mRepeatAction);
}
@Override
public void onActionClicked(Action action) {
if (shouldDispatchAction(action)) {
dispatchAction(action);
return;
}
// Super class handles play/pause and delegates to abstract methods next()/previous().
super.onActionClicked(action);
}
// Should dispatch actions that the super class does not supply callbacks for.
private boolean shouldDispatchAction(Action action) {
return action == mRewindAction
|| action == mFastForwardAction
|| action == mThumbsDownAction
|| action == mThumbsUpAction
|| action == mRepeatAction;
}
private void dispatchAction(Action action) {
// Primary actions are handled manually.
if (action == mRewindAction) {
rewind();
} else if (action == mFastForwardAction) {
fastForward();
} else if (action instanceof PlaybackControlsRow.MultiAction) {
PlaybackControlsRow.MultiAction multiAction = (PlaybackControlsRow.MultiAction) action;
multiAction.nextIndex();
// Notify adapter of action changes to handle secondary actions, such as, thumbs up/down
// and repeat.
notifyActionChanged(
multiAction,
(ArrayObjectAdapter) getControlsRow().getSecondaryActionsAdapter());
}
}
private void notifyActionChanged(
PlaybackControlsRow.MultiAction action, ArrayObjectAdapter adapter) {
if (adapter != null) {
int index = adapter.indexOf(action);
if (index >= 0) {
adapter.notifyArrayItemRangeChanged(index, 1);
}
}
}
@Override
public void next() {
mActionListener.onNext();
}
@Override
public void previous() {
mActionListener.onPrevious();
}
/** Skips backwards 10 seconds. */
public void rewind() {
long newPosition = getCurrentPosition() - TEN_SECONDS;
newPosition = (newPosition < 0) ? 0 : newPosition;
getPlayerAdapter().seekTo(newPosition);
}
/** Skips forward 10 seconds. */
public void fastForward() {
if (getDuration() > -1) {
long newPosition = getCurrentPosition() + TEN_SECONDS;
newPosition = (newPosition > getDuration()) ? getDuration() : newPosition;
getPlayerAdapter().seekTo(newPosition);
}
}
}