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