| 1 |
/* |
| 2 |
* Copyright (C) 2014 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 |
* in compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 |
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 |
* or implied. See the License for the specific language governing permissions and limitations under |
| 12 |
* the License. |
| 13 |
*/ |
| 14 |
|
| 15 |
package ${packageName}; |
| 16 |
|
| 17 |
<#if minApiLevel lt 23>import android.app.Activity; |
| 18 |
<#else>import android.content.Context;</#if> |
| 19 |
import android.media.MediaMetadataRetriever; |
| 20 |
import android.os.Build; |
| 21 |
import android.os.Bundle; |
| 22 |
import android.os.Handler; |
| 23 |
import android.support.v17.leanback.widget.AbstractDetailsDescriptionPresenter; |
| 24 |
import android.support.v17.leanback.widget.Action; |
| 25 |
import android.support.v17.leanback.widget.ArrayObjectAdapter; |
| 26 |
import android.support.v17.leanback.widget.ClassPresenterSelector; |
| 27 |
import android.support.v17.leanback.widget.ControlButtonPresenterSelector; |
| 28 |
import android.support.v17.leanback.widget.HeaderItem; |
| 29 |
import android.support.v17.leanback.widget.ListRow; |
| 30 |
import android.support.v17.leanback.widget.ListRowPresenter; |
| 31 |
import android.support.v17.leanback.widget.OnActionClickedListener; |
| 32 |
import android.support.v17.leanback.widget.OnItemViewClickedListener; |
| 33 |
import android.support.v17.leanback.widget.OnItemViewSelectedListener; |
| 34 |
import android.support.v17.leanback.widget.PlaybackControlsRow; |
| 35 |
import android.support.v17.leanback.widget.PlaybackControlsRow.FastForwardAction; |
| 36 |
import android.support.v17.leanback.widget.PlaybackControlsRow.PlayPauseAction; |
| 37 |
import android.support.v17.leanback.widget.PlaybackControlsRow.RepeatAction; |
| 38 |
import android.support.v17.leanback.widget.PlaybackControlsRow.RewindAction; |
| 39 |
import android.support.v17.leanback.widget.PlaybackControlsRow.ShuffleAction; |
| 40 |
import android.support.v17.leanback.widget.PlaybackControlsRow.SkipNextAction; |
| 41 |
import android.support.v17.leanback.widget.PlaybackControlsRow.SkipPreviousAction; |
| 42 |
import android.support.v17.leanback.widget.PlaybackControlsRow.ThumbsDownAction; |
| 43 |
import android.support.v17.leanback.widget.PlaybackControlsRow.ThumbsUpAction; |
| 44 |
import android.support.v17.leanback.widget.PlaybackControlsRowPresenter; |
| 45 |
import android.support.v17.leanback.widget.Presenter; |
| 46 |
import android.support.v17.leanback.widget.Row; |
| 47 |
import android.support.v17.leanback.widget.RowPresenter; |
| 48 |
import android.util.Log; |
| 49 |
import android.widget.Toast; |
| 50 |
|
| 51 |
import com.bumptech.glide.Glide; |
| 52 |
import com.bumptech.glide.load.resource.drawable.GlideDrawable; |
| 53 |
import com.bumptech.glide.request.animation.GlideAnimation; |
| 54 |
import com.bumptech.glide.request.target.SimpleTarget; |
| 55 |
|
| 56 |
import java.util.ArrayList; |
| 57 |
import java.util.HashMap; |
| 58 |
import java.util.List; |
| 59 |
|
| 60 |
/* |
| 61 |
* Class for video playback with media control |
| 62 |
*/ |
| 63 |
public class PlaybackOverlayFragment extends android.support.v17.leanback.app.PlaybackOverlayFragment { |
| 64 |
private static final String TAG = "PlaybackControlsFragmnt"; |
| 65 |
|
| 66 |
private static final boolean SHOW_DETAIL = true; |
| 67 |
private static final boolean HIDE_MORE_ACTIONS = false; |
| 68 |
private static final int PRIMARY_CONTROLS = 5; |
| 69 |
private static final boolean SHOW_IMAGE = PRIMARY_CONTROLS <= 5; |
| 70 |
private static final int BACKGROUND_TYPE = PlaybackOverlayFragment.BG_LIGHT; |
| 71 |
private static final int CARD_WIDTH = 200; |
| 72 |
private static final int CARD_HEIGHT = 240; |
| 73 |
private static final int DEFAULT_UPDATE_PERIOD = 1000; |
| 74 |
private static final int UPDATE_PERIOD = 16; |
| 75 |
private static final int SIMULATED_BUFFERED_TIME = 10000; |
| 76 |
|
| 77 |
private ArrayObjectAdapter mRowsAdapter; |
| 78 |
private ArrayObjectAdapter mPrimaryActionsAdapter; |
| 79 |
private ArrayObjectAdapter mSecondaryActionsAdapter; |
| 80 |
private PlayPauseAction mPlayPauseAction; |
| 81 |
private RepeatAction mRepeatAction; |
| 82 |
private ThumbsUpAction mThumbsUpAction; |
| 83 |
private ThumbsDownAction mThumbsDownAction; |
| 84 |
private ShuffleAction mShuffleAction; |
| 85 |
private FastForwardAction mFastForwardAction; |
| 86 |
private RewindAction mRewindAction; |
| 87 |
private SkipNextAction mSkipNextAction; |
| 88 |
private SkipPreviousAction mSkipPreviousAction; |
| 89 |
private PlaybackControlsRow mPlaybackControlsRow; |
| 90 |
private ArrayList<Movie> mItems = new ArrayList<Movie>(); |
| 91 |
private int mCurrentItem; |
| 92 |
private Handler mHandler; |
| 93 |
private Runnable mRunnable; |
| 94 |
private Movie mSelectedMovie; |
| 95 |
|
| 96 |
private OnPlayPauseClickedListener mCallback; |
| 97 |
|
| 98 |
// Container Activity must implement this interface |
| 99 |
public interface OnPlayPauseClickedListener { |
| 100 |
void onFragmentPlayPause(Movie movie, int position, Boolean playPause); |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public void onCreate(Bundle savedInstanceState) { |
| 105 |
super.onCreate(savedInstanceState); |
| 106 |
|
| 107 |
mItems = new ArrayList<Movie>(); |
| 108 |
mSelectedMovie = (Movie) getActivity() |
| 109 |
.getIntent().getSerializableExtra(${detailsActivity}.MOVIE); |
| 110 |
|
| 111 |
List<Movie> movies = MovieList.list; |
| 112 |
|
| 113 |
for (int j = 0; j < movies.size(); j++) { |
| 114 |
mItems.add(movies.get(j)); |
| 115 |
if (mSelectedMovie.getTitle().contentEquals(movies.get(j).getTitle())) { |
| 116 |
mCurrentItem = j; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
mHandler = new Handler(); |
| 121 |
|
| 122 |
setBackgroundType(BACKGROUND_TYPE); |
| 123 |
setFadingEnabled(false); |
| 124 |
|
| 125 |
setupRows(); |
| 126 |
|
| 127 |
setOnItemViewSelectedListener(new OnItemViewSelectedListener() { |
| 128 |
@Override |
| 129 |
public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, |
| 130 |
RowPresenter.ViewHolder rowViewHolder, Row row) { |
| 131 |
Log.i(TAG, "onItemSelected: " + item + " row " + row); |
| 132 |
} |
| 133 |
}); |
| 134 |
setOnItemViewClickedListener(new OnItemViewClickedListener() { |
| 135 |
@Override |
| 136 |
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, |
| 137 |
RowPresenter.ViewHolder rowViewHolder, Row row) { |
| 138 |
Log.i(TAG, "onItemClicked: " + item + " row " + row); |
| 139 |
} |
| 140 |
}); |
| 141 |
} |
| 142 |
|
| 143 |
<#if minApiLevel lt 23>@SuppressWarnings("deprecation")</#if> |
| 144 |
@Override |
| 145 |
public void onAttach(<#if minApiLevel lt 23>Activity<#else>Context</#if> context) { |
| 146 |
super.onAttach(context); |
| 147 |
if (context instanceof OnPlayPauseClickedListener) { |
| 148 |
mCallback = (OnPlayPauseClickedListener) context; |
| 149 |
} else { |
| 150 |
throw new RuntimeException(context.toString() |
| 151 |
+ " must implement OnPlayPauseClickedListener"); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
private void setupRows() { |
| 156 |
|
| 157 |
ClassPresenterSelector ps = new ClassPresenterSelector(); |
| 158 |
|
| 159 |
PlaybackControlsRowPresenter playbackControlsRowPresenter; |
| 160 |
if (SHOW_DETAIL) { |
| 161 |
playbackControlsRowPresenter = new PlaybackControlsRowPresenter( |
| 162 |
new DescriptionPresenter()); |
| 163 |
} else { |
| 164 |
playbackControlsRowPresenter = new PlaybackControlsRowPresenter(); |
| 165 |
} |
| 166 |
playbackControlsRowPresenter.setOnActionClickedListener(new OnActionClickedListener() { |
| 167 |
public void onActionClicked(Action action) { |
| 168 |
if (action.getId() == mPlayPauseAction.getId()) { |
| 169 |
togglePlayback(mPlayPauseAction.getIndex() == PlayPauseAction.PLAY); |
| 170 |
} else if (action.getId() == mSkipNextAction.getId()) { |
| 171 |
next(); |
| 172 |
} else if (action.getId() == mSkipPreviousAction.getId()) { |
| 173 |
prev(); |
| 174 |
} else if (action.getId() == mFastForwardAction.getId()) { |
| 175 |
Toast.makeText(getActivity(), "TODO: Fast Forward", Toast.LENGTH_SHORT).show(); |
| 176 |
} else if (action.getId() == mRewindAction.getId()) { |
| 177 |
Toast.makeText(getActivity(), "TODO: Rewind", Toast.LENGTH_SHORT).show(); |
| 178 |
} |
| 179 |
if (action instanceof PlaybackControlsRow.MultiAction) { |
| 180 |
((PlaybackControlsRow.MultiAction) action).nextIndex(); |
| 181 |
notifyChanged(action); |
| 182 |
} |
| 183 |
} |
| 184 |
}); |
| 185 |
playbackControlsRowPresenter.setSecondaryActionsHidden(HIDE_MORE_ACTIONS); |
| 186 |
|
| 187 |
ps.addClassPresenter(PlaybackControlsRow.class, playbackControlsRowPresenter); |
| 188 |
ps.addClassPresenter(ListRow.class, new ListRowPresenter()); |
| 189 |
mRowsAdapter = new ArrayObjectAdapter(ps); |
| 190 |
|
| 191 |
addPlaybackControlsRow(); |
| 192 |
addOtherRows(); |
| 193 |
|
| 194 |
setAdapter(mRowsAdapter); |
| 195 |
} |
| 196 |
|
| 197 |
public void togglePlayback(boolean playPause) { |
| 198 |
if (playPause) { |
| 199 |
startProgressAutomation(); |
| 200 |
setFadingEnabled(true); |
| 201 |
mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), |
| 202 |
mPlaybackControlsRow.getCurrentTime(), true); |
| 203 |
mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlayPauseAction.PAUSE)); |
| 204 |
} else { |
| 205 |
stopProgressAutomation(); |
| 206 |
setFadingEnabled(false); |
| 207 |
mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), |
| 208 |
mPlaybackControlsRow.getCurrentTime(), false); |
| 209 |
mPlayPauseAction.setIcon(mPlayPauseAction.getDrawable(PlayPauseAction.PLAY)); |
| 210 |
} |
| 211 |
notifyChanged(mPlayPauseAction); |
| 212 |
} |
| 213 |
|
| 214 |
private int getDuration() { |
| 215 |
Movie movie = mItems.get(mCurrentItem); |
| 216 |
MediaMetadataRetriever mmr = new MediaMetadataRetriever(); |
| 217 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { |
| 218 |
mmr.setDataSource(movie.getVideoUrl(), new HashMap<String, String>()); |
| 219 |
} else { |
| 220 |
mmr.setDataSource(movie.getVideoUrl()); |
| 221 |
} |
| 222 |
String time = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); |
| 223 |
long duration = Long.parseLong(time); |
| 224 |
return (int) duration; |
| 225 |
} |
| 226 |
|
| 227 |
private void addPlaybackControlsRow() { |
| 228 |
if (SHOW_DETAIL) { |
| 229 |
mPlaybackControlsRow = new PlaybackControlsRow(mSelectedMovie); |
| 230 |
} else { |
| 231 |
mPlaybackControlsRow = new PlaybackControlsRow(); |
| 232 |
} |
| 233 |
mRowsAdapter.add(mPlaybackControlsRow); |
| 234 |
|
| 235 |
updatePlaybackRow(mCurrentItem); |
| 236 |
|
| 237 |
ControlButtonPresenterSelector presenterSelector = new ControlButtonPresenterSelector(); |
| 238 |
mPrimaryActionsAdapter = new ArrayObjectAdapter(presenterSelector); |
| 239 |
mSecondaryActionsAdapter = new ArrayObjectAdapter(presenterSelector); |
| 240 |
mPlaybackControlsRow.setPrimaryActionsAdapter(mPrimaryActionsAdapter); |
| 241 |
mPlaybackControlsRow.setSecondaryActionsAdapter(mSecondaryActionsAdapter); |
| 242 |
|
| 243 |
mPlayPauseAction = new PlayPauseAction(getActivity()); |
| 244 |
mRepeatAction = new RepeatAction(getActivity()); |
| 245 |
mThumbsUpAction = new ThumbsUpAction(getActivity()); |
| 246 |
mThumbsDownAction = new ThumbsDownAction(getActivity()); |
| 247 |
mShuffleAction = new ShuffleAction(getActivity()); |
| 248 |
mSkipNextAction = new PlaybackControlsRow.SkipNextAction(getActivity()); |
| 249 |
mSkipPreviousAction = new PlaybackControlsRow.SkipPreviousAction(getActivity()); |
| 250 |
mFastForwardAction = new PlaybackControlsRow.FastForwardAction(getActivity()); |
| 251 |
mRewindAction = new PlaybackControlsRow.RewindAction(getActivity()); |
| 252 |
|
| 253 |
if (PRIMARY_CONTROLS > 5) { |
| 254 |
mPrimaryActionsAdapter.add(mThumbsUpAction); |
| 255 |
} else { |
| 256 |
mSecondaryActionsAdapter.add(mThumbsUpAction); |
| 257 |
} |
| 258 |
mPrimaryActionsAdapter.add(mSkipPreviousAction); |
| 259 |
if (PRIMARY_CONTROLS > 3) { |
| 260 |
mPrimaryActionsAdapter.add(new PlaybackControlsRow.RewindAction(getActivity())); |
| 261 |
} |
| 262 |
mPrimaryActionsAdapter.add(mPlayPauseAction); |
| 263 |
if (PRIMARY_CONTROLS > 3) { |
| 264 |
mPrimaryActionsAdapter.add(new PlaybackControlsRow.FastForwardAction(getActivity())); |
| 265 |
} |
| 266 |
mPrimaryActionsAdapter.add(mSkipNextAction); |
| 267 |
|
| 268 |
mSecondaryActionsAdapter.add(mRepeatAction); |
| 269 |
mSecondaryActionsAdapter.add(mShuffleAction); |
| 270 |
if (PRIMARY_CONTROLS > 5) { |
| 271 |
mPrimaryActionsAdapter.add(mThumbsDownAction); |
| 272 |
} else { |
| 273 |
mSecondaryActionsAdapter.add(mThumbsDownAction); |
| 274 |
} |
| 275 |
mSecondaryActionsAdapter.add(new PlaybackControlsRow.HighQualityAction(getActivity())); |
| 276 |
mSecondaryActionsAdapter.add(new PlaybackControlsRow.ClosedCaptioningAction(getActivity())); |
| 277 |
} |
| 278 |
|
| 279 |
private void notifyChanged(Action action) { |
| 280 |
ArrayObjectAdapter adapter = mPrimaryActionsAdapter; |
| 281 |
if (adapter.indexOf(action) >= 0) { |
| 282 |
adapter.notifyArrayItemRangeChanged(adapter.indexOf(action), 1); |
| 283 |
return; |
| 284 |
} |
| 285 |
adapter = mSecondaryActionsAdapter; |
| 286 |
if (adapter.indexOf(action) >= 0) { |
| 287 |
adapter.notifyArrayItemRangeChanged(adapter.indexOf(action), 1); |
| 288 |
return; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
private void updatePlaybackRow(int index) { |
| 293 |
if (mPlaybackControlsRow.getItem() != null) { |
| 294 |
Movie item = (Movie) mPlaybackControlsRow.getItem(); |
| 295 |
item.setTitle(mItems.get(mCurrentItem).getTitle()); |
| 296 |
item.setStudio(mItems.get(mCurrentItem).getStudio()); |
| 297 |
} |
| 298 |
if (SHOW_IMAGE) { |
| 299 |
updateVideoImage(mItems.get(mCurrentItem).getCardImageURI().toString()); |
| 300 |
} |
| 301 |
mRowsAdapter.notifyArrayItemRangeChanged(0, 1); |
| 302 |
mPlaybackControlsRow.setTotalTime(getDuration()); |
| 303 |
mPlaybackControlsRow.setCurrentTime(0); |
| 304 |
mPlaybackControlsRow.setBufferedProgress(0); |
| 305 |
} |
| 306 |
|
| 307 |
private void addOtherRows() { |
| 308 |
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter()); |
| 309 |
for (Movie movie : mItems) { |
| 310 |
listRowAdapter.add(movie); |
| 311 |
} |
| 312 |
<#if buildApi gte 22> |
| 313 |
HeaderItem header = new HeaderItem(0, getString(R.string.related_movies)); |
| 314 |
<#else> |
| 315 |
HeaderItem header = new HeaderItem(0, getString(R.string.related_movies), null); |
| 316 |
</#if> |
| 317 |
mRowsAdapter.add(new ListRow(header, listRowAdapter)); |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
private int getUpdatePeriod() { |
| 322 |
if (getView() == null || mPlaybackControlsRow.getTotalTime() <= 0) { |
| 323 |
return DEFAULT_UPDATE_PERIOD; |
| 324 |
} |
| 325 |
return Math.max(UPDATE_PERIOD, mPlaybackControlsRow.getTotalTime() / getView().getWidth()); |
| 326 |
} |
| 327 |
|
| 328 |
private void startProgressAutomation() { |
| 329 |
mRunnable = new Runnable() { |
| 330 |
@Override |
| 331 |
public void run() { |
| 332 |
int updatePeriod = getUpdatePeriod(); |
| 333 |
int currentTime = mPlaybackControlsRow.getCurrentTime() + updatePeriod; |
| 334 |
int totalTime = mPlaybackControlsRow.getTotalTime(); |
| 335 |
mPlaybackControlsRow.setCurrentTime(currentTime); |
| 336 |
mPlaybackControlsRow.setBufferedProgress(currentTime + SIMULATED_BUFFERED_TIME); |
| 337 |
|
| 338 |
if (totalTime > 0 && totalTime <= currentTime) { |
| 339 |
next(); |
| 340 |
} |
| 341 |
mHandler.postDelayed(this, updatePeriod); |
| 342 |
} |
| 343 |
}; |
| 344 |
mHandler.postDelayed(mRunnable, getUpdatePeriod()); |
| 345 |
} |
| 346 |
|
| 347 |
private void next() { |
| 348 |
if (++mCurrentItem >= mItems.size()) { |
| 349 |
mCurrentItem = 0; |
| 350 |
} |
| 351 |
|
| 352 |
if (mPlayPauseAction.getIndex() == PlayPauseAction.PLAY) { |
| 353 |
mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), 0, false); |
| 354 |
} else { |
| 355 |
mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), 0, true); |
| 356 |
} |
| 357 |
updatePlaybackRow(mCurrentItem); |
| 358 |
} |
| 359 |
|
| 360 |
private void prev() { |
| 361 |
if (--mCurrentItem < 0) { |
| 362 |
mCurrentItem = mItems.size() - 1; |
| 363 |
} |
| 364 |
if (mPlayPauseAction.getIndex() == PlayPauseAction.PLAY) { |
| 365 |
mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), 0, false); |
| 366 |
} else { |
| 367 |
mCallback.onFragmentPlayPause(mItems.get(mCurrentItem), 0, true); |
| 368 |
} |
| 369 |
updatePlaybackRow(mCurrentItem); |
| 370 |
} |
| 371 |
|
| 372 |
private void stopProgressAutomation() { |
| 373 |
if (mHandler != null && mRunnable != null) { |
| 374 |
mHandler.removeCallbacks(mRunnable); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
@Override |
| 379 |
public void onStop() { |
| 380 |
stopProgressAutomation(); |
| 381 |
super.onStop(); |
| 382 |
} |
| 383 |
|
| 384 |
static class DescriptionPresenter extends AbstractDetailsDescriptionPresenter { |
| 385 |
@Override |
| 386 |
protected void onBindDescription(ViewHolder viewHolder, Object item) { |
| 387 |
viewHolder.getTitle().setText(((Movie) item).getTitle()); |
| 388 |
viewHolder.getSubtitle().setText(((Movie) item).getStudio()); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
protected void updateVideoImage(String uri) { |
| 393 |
Glide.with(getActivity()) |
| 394 |
.load(uri) |
| 395 |
.centerCrop() |
| 396 |
.into(new SimpleTarget<GlideDrawable>(CARD_WIDTH, CARD_HEIGHT) { |
| 397 |
@Override |
| 398 |
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) { |
| 399 |
mPlaybackControlsRow.setImageDrawable(resource); |
| 400 |
mRowsAdapter.notifyArrayItemRangeChanged(0, mRowsAdapter.size()); |
| 401 |
} |
| 402 |
}); |
| 403 |
} |
| 404 |
} |