1 /* This file is auto-generated from PlaybackOverlayFragment.java.  DO NOT MODIFY. */
2 
3 /*
4  * Copyright (C) 2014 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  * in compliance with the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software distributed under the License
12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 package com.example.android.leanback;
17 
18 import android.content.Context;
19 import android.graphics.drawable.Drawable;
20 import android.os.Bundle;
21 import android.os.Handler;
22 import android.support.v17.leanback.app.PlaybackControlGlue;
23 import android.support.v17.leanback.widget.Action;
24 import android.support.v17.leanback.widget.ArrayObjectAdapter;
25 import android.support.v17.leanback.widget.PlaybackControlsRow;
26 import android.support.v17.leanback.widget.PlaybackControlsRow.RepeatAction;
27 import android.support.v17.leanback.widget.PlaybackControlsRow.ThumbsUpAction;
28 import android.support.v17.leanback.widget.PlaybackControlsRow.ThumbsDownAction;
29 import android.support.v17.leanback.widget.PlaybackControlsRowPresenter;
30 import android.support.v17.leanback.widget.HeaderItem;
31 import android.support.v17.leanback.widget.PresenterSelector;
32 import android.support.v17.leanback.widget.Row;
33 import android.support.v17.leanback.widget.ListRow;
34 import android.support.v17.leanback.widget.Presenter;
35 import android.support.v17.leanback.widget.RowPresenter;
36 import android.support.v17.leanback.widget.ListRowPresenter;
37 import android.support.v17.leanback.widget.OnItemViewSelectedListener;
38 import android.support.v17.leanback.widget.OnItemViewClickedListener;
39 import android.support.v17.leanback.widget.ControlButtonPresenterSelector;
40 import android.support.v17.leanback.widget.SparseArrayObjectAdapter;
41 import android.util.Log;
42 import android.view.LayoutInflater;
43 import android.view.View;
44 import android.view.ViewGroup;
45 import android.widget.Toast;
46 
47 public class PlaybackOverlaySupportFragment
48         extends android.support.v17.leanback.app.PlaybackOverlaySupportFragment
49         implements PlaybackOverlaySupportActivity.PictureInPictureListener {
50     private static final String TAG = "leanback.PlaybackControlsFragment";
51 
52     /**
53      * Change this to choose a different overlay background.
54      */
55     private static final int BACKGROUND_TYPE = PlaybackOverlaySupportFragment.BG_LIGHT;
56 
57     /**
58      * Change the number of related content rows.
59      */
60     private static final int RELATED_CONTENT_ROWS = 3;
61 
62     /**
63      * Change this to select hidden
64      */
65     private static final boolean SECONDARY_HIDDEN = false;
66 
67     private static final int ROW_CONTROLS = 0;
68 
69     private PlaybackControlSupportHelper mGlue;
70     private PlaybackControlsRowPresenter mPlaybackControlsRowPresenter;
71     private ListRowPresenter mListRowPresenter;
72 
73     private OnItemViewClickedListener mOnItemViewClickedListener = new OnItemViewClickedListener() {
74         @Override
75         public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
76                                   RowPresenter.ViewHolder rowViewHolder, Row row) {
77             Log.i(TAG, "onItemClicked: " + item + " row " + row);
78             if (item instanceof Action) {
79                 mGlue.onActionClicked((Action) item);
80             }
81         }
82     };
83 
84     private OnItemViewSelectedListener mOnItemViewSelectedListener = new OnItemViewSelectedListener() {
85         @Override
86         public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
87                                    RowPresenter.ViewHolder rowViewHolder, Row row) {
88             Log.i(TAG, "onItemSelected: " + item + " row " + row);
89         }
90     };
91 
getAdapter()92     public SparseArrayObjectAdapter getAdapter() {
93         return (SparseArrayObjectAdapter) super.getAdapter();
94     }
95 
96     @Override
onCreate(Bundle savedInstanceState)97     public void onCreate(Bundle savedInstanceState) {
98         Log.i(TAG, "onCreate");
99         super.onCreate(savedInstanceState);
100 
101         setBackgroundType(BACKGROUND_TYPE);
102         setOnItemViewSelectedListener(mOnItemViewSelectedListener);
103 
104         createComponents(getActivity());
105     }
106 
createComponents(Context context)107     private void createComponents(Context context) {
108         mGlue = new PlaybackControlSupportHelper(context, this) {
109             @Override
110             public int getUpdatePeriod() {
111                 int totalTime = getControlsRow().getTotalTime();
112                 if (getView() == null || getView().getWidth() == 0 || totalTime <= 0) {
113                     return 1000;
114                 }
115                 return Math.max(16, totalTime / getView().getWidth());
116             }
117 
118             @Override
119             protected void onRowChanged(PlaybackControlsRow row) {
120                 if (getAdapter() == null) {
121                     return;
122                 }
123                 int index = getAdapter().indexOf(row);
124                 if (index >= 0) {
125                     getAdapter().notifyArrayItemRangeChanged(index, 1);
126                 }
127             }
128 
129             @Override
130             public void onActionClicked(Action action) {
131                 if (action.getId() == R.id.lb_control_picture_in_picture) {
132                     getActivity().enterPictureInPictureMode();
133                     return;
134                 }
135                 super.onActionClicked(action);
136             }
137         };
138 
139         mGlue.setOnItemViewClickedListener(mOnItemViewClickedListener);
140 
141         mPlaybackControlsRowPresenter = mGlue.createControlsRowAndPresenter();
142         mPlaybackControlsRowPresenter.setSecondaryActionsHidden(SECONDARY_HIDDEN);
143         mListRowPresenter = new ListRowPresenter();
144 
145         setAdapter(new SparseArrayObjectAdapter(new PresenterSelector() {
146             @Override
147             public Presenter getPresenter(Object object) {
148                 if (object instanceof PlaybackControlsRow) {
149                     return mPlaybackControlsRowPresenter;
150                 } else if (object instanceof ListRow) {
151                     return mListRowPresenter;
152                 }
153                 throw new IllegalArgumentException("Unhandled object: " + object);
154             }
155         }));
156 
157         // Add the controls row
158         getAdapter().set(ROW_CONTROLS, mGlue.getControlsRow());
159 
160         // Add related content rows
161         for (int i = 0; i < RELATED_CONTENT_ROWS; ++i) {
162             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new StringPresenter());
163             listRowAdapter.add("Some related content");
164             listRowAdapter.add("Other related content");
165             HeaderItem header = new HeaderItem(i, "Row " + i);
166             getAdapter().set(ROW_CONTROLS + 1 + i, new ListRow(header, listRowAdapter));
167         }
168 
169     }
170 
171     @Override
onStart()172     public void onStart() {
173         super.onStart();
174         mGlue.setFadingEnabled(true);
175         mGlue.enableProgressUpdating(mGlue.hasValidMedia() && mGlue.isMediaPlaying());
176         ((PlaybackOverlaySupportActivity) getActivity()).registerPictureInPictureListener(this);
177     }
178 
179     @Override
onStop()180     public void onStop() {
181         mGlue.enableProgressUpdating(false);
182         ((PlaybackOverlaySupportActivity) getActivity()).unregisterPictureInPictureListener(this);
183         super.onStop();
184     }
185 
186     @Override
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)187     public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
188         if (isInPictureInPictureMode) {
189             // Hide the controls in picture-in-picture mode.
190             setFadingEnabled(true);
191             fadeOut();
192         } else {
193             setFadingEnabled(mGlue.isMediaPlaying());
194         }
195     }
196 }
197