1 // CHECKSTYLE:OFF Generated code
2 /* This file is auto-generated from PlaybackFragment.java.  DO NOT MODIFY. */
3 
4 /*
5  * Copyright (C) 2016 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 package com.example.android.leanback;
21 
22 import android.content.Context;
23 import android.os.Build;
24 import android.os.Bundle;
25 import android.util.Log;
26 
27 import androidx.leanback.app.PlaybackSupportFragmentGlueHost;
28 import androidx.leanback.widget.Action;
29 import androidx.leanback.widget.ArrayObjectAdapter;
30 import androidx.leanback.widget.ClassPresenterSelector;
31 import androidx.leanback.widget.HeaderItem;
32 import androidx.leanback.widget.ListRow;
33 import androidx.leanback.widget.ListRowPresenter;
34 import androidx.leanback.widget.SparseArrayObjectAdapter;
35 
36 /**
37  * Example of PlaybackSupportFragment working with a PlaybackControlGlue.
38  */
39 public class PlaybackSupportFragment
40         extends androidx.leanback.app.PlaybackSupportFragment
41         implements PlaybackSupportActivity.PictureInPictureListener {
42     private static final String TAG = "leanback.PlaybackControlsFragment";
43 
44     /**
45      * Change this to choose a different overlay background.
46      */
47     private static final int BACKGROUND_TYPE = PlaybackSupportFragment.BG_LIGHT;
48 
49     /**
50      * Change the number of related content rows.
51      */
52     private static final int RELATED_CONTENT_ROWS = 3;
53 
54     /**
55      * Change this to select hidden
56      */
57     private static final boolean SECONDARY_HIDDEN = false;
58 
59     private static final int ROW_CONTROLS = 0;
60 
61     private PlaybackControlGlue mGlue;
62 
63     @Override
getAdapter()64     public SparseArrayObjectAdapter getAdapter() {
65         return (SparseArrayObjectAdapter) super.getAdapter();
66     }
67 
68     @Override
onCreate(Bundle savedInstanceState)69     public void onCreate(Bundle savedInstanceState) {
70         Log.i(TAG, "onCreate");
71         super.onCreate(savedInstanceState);
72 
73         setBackgroundType(BACKGROUND_TYPE);
74 
75         createComponents(getActivity());
76     }
77 
createComponents(Context context)78     private void createComponents(Context context) {
79         mGlue = new PlaybackControlGlue(context) {
80             @Override
81             public int getUpdatePeriod() {
82                 long totalTime = getControlsRow().getDuration();
83                 if (getView() == null || getView().getWidth() == 0 || totalTime <= 0) {
84                     return 1000;
85                 }
86                 return 16;
87             }
88 
89             @Override
90             public void onActionClicked(Action action) {
91                 if (action.getId() == R.id.lb_control_picture_in_picture) {
92                     if (Build.VERSION.SDK_INT >= 24) {
93                         getActivity().enterPictureInPictureMode();
94                     }
95                     return;
96                 }
97                 super.onActionClicked(action);
98             }
99 
100             @Override
101             protected void onCreateControlsRowAndPresenter() {
102                 super.onCreateControlsRowAndPresenter();
103                 getControlsRowPresenter().setSecondaryActionsHidden(SECONDARY_HIDDEN);
104             }
105         };
106 
107         mGlue.setHost(new PlaybackSupportFragmentGlueHost(this));
108         ClassPresenterSelector classPresenterSelector = new ClassPresenterSelector();
109         classPresenterSelector.addClassPresenter(ListRow.class, new ListRowPresenter());
110 
111         setAdapter(new SparseArrayObjectAdapter(classPresenterSelector));
112 
113         // Add related content rows
114         for (int i = 0; i < RELATED_CONTENT_ROWS; ++i) {
115             ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new StringPresenter());
116             listRowAdapter.add("Some related content");
117             listRowAdapter.add("Other related content");
118             HeaderItem header = new HeaderItem(i, "Row " + i);
119             getAdapter().set(ROW_CONTROLS + 1 + i, new ListRow(header, listRowAdapter));
120         }
121     }
122 
123     @Override
onStart()124     public void onStart() {
125         super.onStart();
126         ((PlaybackSupportActivity) getActivity()).registerPictureInPictureListener(this);
127     }
128 
129     @Override
onStop()130     public void onStop() {
131         ((PlaybackSupportActivity) getActivity()).unregisterPictureInPictureListener(this);
132         super.onStop();
133     }
134 
135     @Override
onPictureInPictureModeChanged(boolean isInPictureInPictureMode)136     public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
137         if (isInPictureInPictureMode) {
138             // Hide the controls in picture-in-picture mode.
139             setFadingEnabled(true);
140             fadeOut();
141         } else {
142             setFadingEnabled(mGlue.isPlaying());
143         }
144     }
145 }
146