16
16
import android .content .Context ;
17
17
import android .content .res .Resources ;
18
18
import android .os .Bundle ;
19
+ import android .os .Handler ;
19
20
import android .support .v17 .leanback .widget .Action ;
20
21
import android .support .v17 .leanback .widget .ArrayObjectAdapter ;
21
22
import android .support .v17 .leanback .widget .ClassPresenterSelector ;
32
33
public class PlaybackOverlayFragment extends android .support .v17 .leanback .app .PlaybackOverlayFragment {
33
34
private static final String TAG = "leanback.PlaybackControlsFragment" ;
34
35
35
- private static final int NUM_ROWS = 3 ;
36
- private static final boolean SHOW_ITEM_DETAIL = true ;
36
+ private static final boolean SHOW_DETAIL = true ;
37
+ private static final boolean SHOW_IMAGE = true ;
37
38
private static final boolean HIDE_MORE_ACTIONS = false ;
39
+ private static final int TOTAL_TIME_MS = 120 * 1000 ;
40
+ private static final int NUM_ROWS = 3 ;
38
41
39
42
private ArrayObjectAdapter mRowsAdapter ;
40
43
private ArrayObjectAdapter mPrimaryActionsAdapter ;
41
44
private ArrayObjectAdapter mSecondaryActionsAdapter ;
42
45
private PlaybackControlsRow .PlayPauseAction mPlayPauseAction ;
43
46
private PlaybackControlsRow .RepeatAction mRepeatAction ;
44
47
private PlaybackControlsRow mPlaybackControlsRow ;
48
+ private Handler mHandler ;
49
+ private Runnable mRunnable ;
45
50
46
51
@ Override
47
52
public void onCreate (Bundle savedInstanceState ) {
48
53
Log .i (TAG , "onCreate" );
49
54
super .onCreate (savedInstanceState );
50
55
56
+ mHandler = new Handler ();
57
+
51
58
setupRows ();
52
59
}
53
60
@@ -59,7 +66,7 @@ private void setupRows() {
59
66
ClassPresenterSelector ps = new ClassPresenterSelector ();
60
67
61
68
PlaybackControlsRowPresenter playbackControlsRowPresenter ;
62
- if (SHOW_ITEM_DETAIL ) {
69
+ if (SHOW_DETAIL ) {
63
70
playbackControlsRowPresenter = new PlaybackControlsRowPresenter (
64
71
new DetailsDescriptionPresenter ());
65
72
} else {
@@ -69,6 +76,15 @@ private void setupRows() {
69
76
public void onActionClicked (Action action ) {
70
77
Toast .makeText (getActivity (), action .toString (), Toast .LENGTH_SHORT ).show ();
71
78
if (action .getId () == mPlayPauseAction .getId ()) {
79
+ if (mPlayPauseAction .isPlayIconShown ()) {
80
+ int totalTime = mPlaybackControlsRow .getTotalTime ();
81
+ if (totalTime > 0 && mPlaybackControlsRow .getCurrentTime () >= totalTime ) {
82
+ mPlaybackControlsRow .setCurrentTime (0 );
83
+ }
84
+ startProgressAutomation ();
85
+ } else {
86
+ stopProgressAutomation ();
87
+ }
72
88
mPlayPauseAction .toggle ();
73
89
notifyChanged (mPrimaryActionsAdapter , mPlayPauseAction );
74
90
} else if (action .getId () == mRepeatAction .getId ()) {
@@ -95,15 +111,21 @@ private void addPlaybackControlsRow() {
95
111
mPrimaryActionsAdapter = new ArrayObjectAdapter (presenterSelector );
96
112
mSecondaryActionsAdapter = new ArrayObjectAdapter (presenterSelector );
97
113
98
- if (SHOW_ITEM_DETAIL ) {
114
+ if (SHOW_DETAIL ) {
99
115
mPlaybackControlsRow = new PlaybackControlsRow ("Playback Controls Title" );
100
- mPlaybackControlsRow .setImageDrawable (context .getResources ().getDrawable (
101
- R .drawable .details_img ));
102
116
} else {
103
117
mPlaybackControlsRow = new PlaybackControlsRow ();
104
118
}
119
+ if (SHOW_IMAGE ) {
120
+ mPlaybackControlsRow .setImageDrawable (context .getResources ().getDrawable (
121
+ R .drawable .details_img ));
122
+ }
105
123
mPlaybackControlsRow .setPrimaryActionsAdapter (mPrimaryActionsAdapter );
106
124
mPlaybackControlsRow .setSecondaryActionsAdapter (mSecondaryActionsAdapter );
125
+ mPlaybackControlsRow .setTotalTime (TOTAL_TIME_MS );
126
+ mPlaybackControlsRow .setCurrentTime (10 * 1000 );
127
+ mPlaybackControlsRow .setBufferedProgress (75 * 1000 );
128
+
107
129
mRowsAdapter .add (mPlaybackControlsRow );
108
130
109
131
mPlayPauseAction = new PlaybackControlsRow .PlayPauseAction (context );
@@ -128,4 +150,28 @@ private void addPlaybackControlsRow() {
128
150
mRowsAdapter .add (new ListRow (header , listRowAdapter ));
129
151
}
130
152
}
153
+
154
+ private void startProgressAutomation () {
155
+ int width = getView ().getWidth ();
156
+ final int totalTime = mPlaybackControlsRow .getTotalTime ();
157
+ final int updateFreq = totalTime <= 0 ? 1000 :
158
+ Math .max (16 , totalTime / width );
159
+ mRunnable = new Runnable () {
160
+ @ Override
161
+ public void run () {
162
+ int currentTime = mPlaybackControlsRow .getCurrentTime () + updateFreq ;
163
+ mPlaybackControlsRow .setCurrentTime (currentTime );
164
+ if (totalTime <= 0 || totalTime > currentTime ) {
165
+ mHandler .postDelayed (this , updateFreq );
166
+ }
167
+ }
168
+ };
169
+ mHandler .postDelayed (mRunnable , updateFreq );
170
+ }
171
+
172
+ private void stopProgressAutomation () {
173
+ if (mHandler != null && mRunnable != null ) {
174
+ mHandler .removeCallbacks (mRunnable );
175
+ }
176
+ }
131
177
}
0 commit comments