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.graphics.drawable.Drawable; 18 import android.os.Bundle; 19 import android.os.Handler; 20 import android.support.v17.leanback.app.PlaybackControlGlue; 21 import android.support.v17.leanback.widget.Action; 22 import android.support.v17.leanback.widget.ArrayObjectAdapter; 23 import android.support.v17.leanback.widget.PlaybackControlsRow; 24 import android.support.v17.leanback.widget.PlaybackControlsRow.RepeatAction; 25 import android.support.v17.leanback.widget.PlaybackControlsRow.ThumbsUpAction; 26 import android.support.v17.leanback.widget.PlaybackControlsRow.ThumbsDownAction; 27 import android.support.v17.leanback.widget.PlaybackControlsRowPresenter; 28 import android.support.v17.leanback.widget.HeaderItem; 29 import android.support.v17.leanback.widget.PresenterSelector; 30 import android.support.v17.leanback.widget.Row; 31 import android.support.v17.leanback.widget.ListRow; 32 import android.support.v17.leanback.widget.Presenter; 33 import android.support.v17.leanback.widget.RowPresenter; 34 import android.support.v17.leanback.widget.ListRowPresenter; 35 import android.support.v17.leanback.widget.OnItemViewSelectedListener; 36 import android.support.v17.leanback.widget.OnItemViewClickedListener; 37 import android.support.v17.leanback.widget.ControlButtonPresenterSelector; 38 import android.support.v17.leanback.widget.SparseArrayObjectAdapter; 39 import android.util.Log; 40 import android.view.LayoutInflater; 41 import android.view.View; 42 import android.view.ViewGroup; 43 import android.widget.Toast; 44 45 public class PlaybackOverlayFragment 46 extends android.support.v17.leanback.app.PlaybackOverlayFragment 47 implements PlaybackOverlayActivity.PictureInPictureListener { 48 private static final String TAG = "leanback.PlaybackControlsFragment"; 49 50 /** 51 * Change this to choose a different overlay background. 52 */ 53 private static final int BACKGROUND_TYPE = PlaybackOverlayFragment.BG_LIGHT; 54 55 /** 56 * Change the number of related content rows. 57 */ 58 private static final int RELATED_CONTENT_ROWS = 3; 59 60 /** 61 * Change this to select hidden 62 */ 63 private static final boolean SECONDARY_HIDDEN = false; 64 65 private static final int ROW_CONTROLS = 0; 66 67 private PlaybackControlHelper mGlue; 68 private PlaybackControlsRowPresenter mPlaybackControlsRowPresenter; 69 private ListRowPresenter mListRowPresenter; 70 71 private OnItemViewClickedListener mOnItemViewClickedListener = new OnItemViewClickedListener() { 72 @Override 73 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 74 RowPresenter.ViewHolder rowViewHolder, Row row) { 75 Log.i(TAG, "onItemClicked: " + item + " row " + row); 76 if (item instanceof Action) { 77 mGlue.onActionClicked((Action) item); 78 } 79 } 80 }; 81 82 private OnItemViewSelectedListener mOnItemViewSelectedListener = new OnItemViewSelectedListener() { 83 @Override 84 public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item, 85 RowPresenter.ViewHolder rowViewHolder, Row row) { 86 Log.i(TAG, "onItemSelected: " + item + " row " + row); 87 } 88 }; 89 90 public SparseArrayObjectAdapter getAdapter() { 91 return (SparseArrayObjectAdapter) super.getAdapter(); 92 } 93 94 @Override 95 public void onCreate(Bundle savedInstanceState) { 96 Log.i(TAG, "onCreate"); 97 super.onCreate(savedInstanceState); 98 99 setBackgroundType(BACKGROUND_TYPE); 100 setOnItemViewSelectedListener(mOnItemViewSelectedListener); 101 102 createComponents(getActivity()); 103 } 104 105 private void createComponents(Context context) { 106 mGlue = new PlaybackControlHelper(context, this) { 107 @Override 108 public int getUpdatePeriod() { 109 int totalTime = getControlsRow().getTotalTime(); 110 if (getView() == null || getView().getWidth() == 0 || totalTime <= 0) { 111 return 1000; 112 } 113 return Math.max(16, totalTime / getView().getWidth()); 114 } 115 116 @Override 117 protected void onRowChanged(PlaybackControlsRow row) { 118 if (getAdapter() == null) { 119 return; 120 } 121 int index = getAdapter().indexOf(row); 122 if (index >= 0) { 123 getAdapter().notifyArrayItemRangeChanged(index, 1); 124 } 125 } 126 127 @Override 128 public void onActionClicked(Action action) { 129 if (action.getId() == R.id.lb_control_picture_in_picture) { 130 getActivity().enterPictureInPictureMode(); 131 return; 132 } 133 super.onActionClicked(action); 134 } 135 }; 136 137 mGlue.setOnItemViewClickedListener(mOnItemViewClickedListener); 138 139 mPlaybackControlsRowPresenter = mGlue.createControlsRowAndPresenter(); 140 mPlaybackControlsRowPresenter.setSecondaryActionsHidden(SECONDARY_HIDDEN); 141 mListRowPresenter = new ListRowPresenter(); 142 143 setAdapter(new SparseArrayObjectAdapter(new PresenterSelector() { 144 @Override 145 public Presenter getPresenter(Object object) { 146 if (object instanceof PlaybackControlsRow) { 147 return mPlaybackControlsRowPresenter; 148 } else if (object instanceof ListRow) { 149 return mListRowPresenter; 150 } 151 throw new IllegalArgumentException("Unhandled object: " + object); 152 } 153 })); 154 155 // Add the controls row 156 getAdapter().set(ROW_CONTROLS, mGlue.getControlsRow()); 157 158 // Add related content rows 159 for (int i = 0; i < RELATED_CONTENT_ROWS; ++i) { 160 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new StringPresenter()); 161 listRowAdapter.add("Some related content"); 162 listRowAdapter.add("Other related content"); 163 HeaderItem header = new HeaderItem(i, "Row " + i); 164 getAdapter().set(ROW_CONTROLS + 1 + i, new ListRow(header, listRowAdapter)); 165 } 166 167 } 168 169 @Override 170 public void onStart() { 171 super.onStart(); 172 mGlue.setFadingEnabled(true); 173 mGlue.enableProgressUpdating(mGlue.hasValidMedia() && mGlue.isMediaPlaying()); 174 ((PlaybackOverlayActivity) getActivity()).registerPictureInPictureListener(this); 175 } 176 177 @Override 178 public void onStop() { 179 mGlue.enableProgressUpdating(false); 180 ((PlaybackOverlayActivity) getActivity()).unregisterPictureInPictureListener(this); 181 super.onStop(); 182 } 183 184 @Override 185 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) { 186 if (isInPictureInPictureMode) { 187 // Hide the controls in picture-in-picture mode. 188 setFadingEnabled(true); 189 fadeOut(); 190 } else { 191 setFadingEnabled(mGlue.isMediaPlaying()); 192 } 193 } 194 } 195