|
| 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 | +package com.example.android.leanback; |
| 15 | + |
| 16 | +import android.content.Context; |
| 17 | +import android.content.res.Resources; |
| 18 | +import android.os.Bundle; |
| 19 | +import android.support.v17.leanback.widget.Action; |
| 20 | +import android.support.v17.leanback.widget.ArrayObjectAdapter; |
| 21 | +import android.support.v17.leanback.widget.ClassPresenterSelector; |
| 22 | +import android.support.v17.leanback.widget.PlaybackControlsRow; |
| 23 | +import android.support.v17.leanback.widget.PlaybackControlsRowPresenter; |
| 24 | +import android.support.v17.leanback.widget.HeaderItem; |
| 25 | +import android.support.v17.leanback.widget.ListRow; |
| 26 | +import android.support.v17.leanback.widget.ListRowPresenter; |
| 27 | +import android.support.v17.leanback.widget.OnActionClickedListener; |
| 28 | +import android.support.v17.leanback.widget.ControlButtonPresenterSelector; |
| 29 | +import android.util.Log; |
| 30 | +import android.widget.Toast; |
| 31 | + |
| 32 | +public class PlaybackOverlayFragment extends android.support.v17.leanback.app.PlaybackOverlayFragment { |
| 33 | + private static final String TAG = "leanback.PlaybackControlsFragment"; |
| 34 | + |
| 35 | + private static final int NUM_ROWS = 3; |
| 36 | + private static final boolean SHOW_ITEM_DETAIL = true; |
| 37 | + private static final boolean HIDE_MORE_ACTIONS = false; |
| 38 | + |
| 39 | + private ArrayObjectAdapter mRowsAdapter; |
| 40 | + private ArrayObjectAdapter mPrimaryActionsAdapter; |
| 41 | + private ArrayObjectAdapter mSecondaryActionsAdapter; |
| 42 | + private PlaybackControlsRow.PlayPauseAction mPlayPauseAction; |
| 43 | + private PlaybackControlsRow.RepeatAction mRepeatAction; |
| 44 | + private PlaybackControlsRow mPlaybackControlsRow; |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onCreate(Bundle savedInstanceState) { |
| 48 | + Log.i(TAG, "onCreate"); |
| 49 | + super.onCreate(savedInstanceState); |
| 50 | + |
| 51 | + setupRows(); |
| 52 | + } |
| 53 | + |
| 54 | + private static void notifyChanged(ArrayObjectAdapter adapter, Action action) { |
| 55 | + adapter.notifyArrayItemRangeChanged(adapter.indexOf(action), 1); |
| 56 | + } |
| 57 | + |
| 58 | + private void setupRows() { |
| 59 | + ClassPresenterSelector ps = new ClassPresenterSelector(); |
| 60 | + |
| 61 | + PlaybackControlsRowPresenter playbackControlsRowPresenter; |
| 62 | + if (SHOW_ITEM_DETAIL) { |
| 63 | + playbackControlsRowPresenter = new PlaybackControlsRowPresenter( |
| 64 | + new DetailsDescriptionPresenter()); |
| 65 | + } else { |
| 66 | + playbackControlsRowPresenter = new PlaybackControlsRowPresenter(); |
| 67 | + } |
| 68 | + playbackControlsRowPresenter.setOnActionClickedListener(new OnActionClickedListener() { |
| 69 | + public void onActionClicked(Action action) { |
| 70 | + Toast.makeText(getActivity(), action.toString(), Toast.LENGTH_SHORT).show(); |
| 71 | + if (action.getId() == mPlayPauseAction.getId()) { |
| 72 | + mPlayPauseAction.toggle(); |
| 73 | + notifyChanged(mPrimaryActionsAdapter, mPlayPauseAction); |
| 74 | + } else if (action.getId() == mRepeatAction.getId()) { |
| 75 | + mRepeatAction.next(); |
| 76 | + notifyChanged(mSecondaryActionsAdapter, mRepeatAction); |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
| 80 | + playbackControlsRowPresenter.setSecondaryActionsHidden(HIDE_MORE_ACTIONS); |
| 81 | + |
| 82 | + ps.addClassPresenter(PlaybackControlsRow.class, playbackControlsRowPresenter); |
| 83 | + ps.addClassPresenter(ListRow.class, new ListRowPresenter()); |
| 84 | + mRowsAdapter = new ArrayObjectAdapter(ps); |
| 85 | + |
| 86 | + addPlaybackControlsRow(); |
| 87 | + |
| 88 | + setAdapter(mRowsAdapter); |
| 89 | + } |
| 90 | + |
| 91 | + private void addPlaybackControlsRow() { |
| 92 | + Context context = getActivity(); |
| 93 | + |
| 94 | + ControlButtonPresenterSelector presenterSelector = new ControlButtonPresenterSelector(); |
| 95 | + mPrimaryActionsAdapter = new ArrayObjectAdapter(presenterSelector); |
| 96 | + mSecondaryActionsAdapter = new ArrayObjectAdapter(presenterSelector); |
| 97 | + |
| 98 | + if (SHOW_ITEM_DETAIL) { |
| 99 | + mPlaybackControlsRow = new PlaybackControlsRow("Playback Controls Title"); |
| 100 | + mPlaybackControlsRow.setImageDrawable(context.getResources().getDrawable( |
| 101 | + R.drawable.details_img)); |
| 102 | + } else { |
| 103 | + mPlaybackControlsRow = new PlaybackControlsRow(); |
| 104 | + } |
| 105 | + mPlaybackControlsRow.setPrimaryActionsAdapter(mPrimaryActionsAdapter); |
| 106 | + mPlaybackControlsRow.setSecondaryActionsAdapter(mSecondaryActionsAdapter); |
| 107 | + mRowsAdapter.add(mPlaybackControlsRow); |
| 108 | + |
| 109 | + mPlayPauseAction = new PlaybackControlsRow.PlayPauseAction(context); |
| 110 | + mRepeatAction = new PlaybackControlsRow.RepeatAction(context); |
| 111 | + |
| 112 | + mPrimaryActionsAdapter.add(new PlaybackControlsRow.SkipPreviousAction(context)); |
| 113 | + mPrimaryActionsAdapter.add(new PlaybackControlsRow.RewindAction(context)); |
| 114 | + mPrimaryActionsAdapter.add(mPlayPauseAction); |
| 115 | + mPrimaryActionsAdapter.add(new PlaybackControlsRow.FastForwardAction(context)); |
| 116 | + mPrimaryActionsAdapter.add(new PlaybackControlsRow.SkipNextAction(context)); |
| 117 | + |
| 118 | + mSecondaryActionsAdapter.add(new PlaybackControlsRow.ThumbsUpAction(context)); |
| 119 | + mSecondaryActionsAdapter.add(mRepeatAction); |
| 120 | + mSecondaryActionsAdapter.add(new PlaybackControlsRow.ShuffleAction(context)); |
| 121 | + mSecondaryActionsAdapter.add(new PlaybackControlsRow.ThumbsDownAction(context)); |
| 122 | + |
| 123 | + for (int i = 0; i < NUM_ROWS; ++i) { |
| 124 | + ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new StringPresenter()); |
| 125 | + listRowAdapter.add("Some related content"); |
| 126 | + listRowAdapter.add("Other related content"); |
| 127 | + HeaderItem header = new HeaderItem(i, "Row " + i, null); |
| 128 | + mRowsAdapter.add(new ListRow(header, listRowAdapter)); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments