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 import org.jspecify.annotations.NonNull; 34 35 /** 36 * Example of PlaybackFragment working with a PlaybackControlGlue. 37 */ 38 public class PlaybackTransportControlFragment 39 extends androidx.leanback.app.PlaybackFragment 40 implements PlaybackTransportControlActivity.PictureInPictureListener { 41 private static final String TAG = "TransportFragment"; 42 43 /** 44 * Change this to choose a different overlay background. 45 */ 46 private static final int BACKGROUND_TYPE = BG_DARK; 47 48 /** 49 * Change the number of related content rows. 50 */ 51 private static final int RELATED_CONTENT_ROWS = 3; 52 53 private static final int ROW_CONTROLS = 0; 54 55 private PlaybackTransportControlGlueSample mGlue; 56 57 @Override getAdapter()58 public SparseArrayObjectAdapter getAdapter() { 59 return (SparseArrayObjectAdapter) super.getAdapter(); 60 } 61 62 @Override onCreate(Bundle savedInstanceState)63 public void onCreate(Bundle savedInstanceState) { 64 Log.i(TAG, "onCreate"); 65 super.onCreate(savedInstanceState); 66 67 setBackgroundType(BACKGROUND_TYPE); 68 69 createComponents(getActivity()); 70 } 71 createComponents(Context context)72 private void createComponents(Context context) { 73 mGlue = new PlaybackTransportControlGlueSample(context, new PlayerAdapter()) { 74 @Override 75 public void onActionClicked(@NonNull Action action) { 76 if (action.getId() == androidx.leanback.R.id.lb_control_picture_in_picture) { 77 if (Build.VERSION.SDK_INT >= 24) { 78 getActivity().enterPictureInPictureMode(); 79 } 80 return; 81 } 82 super.onActionClicked(action); 83 } 84 85 }; 86 87 mGlue.setTitle("Title"); 88 mGlue.setSubtitle("Android developer"); 89 mGlue.setHost(new PlaybackFragmentGlueHost(this)); 90 mGlue.setSeekProvider(new PlaybackSeekDataProviderSample( 91 PlayerAdapter.FAUX_DURATION, 100)); 92 93 ClassPresenterSelector selector = new ClassPresenterSelector(); 94 selector.addClassPresenter(ListRow.class, new ListRowPresenter()); 95 setAdapter(new SparseArrayObjectAdapter(selector)); 96 97 // Add related content rows 98 for (int i = 0; i < RELATED_CONTENT_ROWS; ++i) { 99 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new StringPresenter()); 100 listRowAdapter.add("Some related content"); 101 listRowAdapter.add("Other related content"); 102 HeaderItem header = new HeaderItem(i, "Row " + i); 103 getAdapter().set(ROW_CONTROLS + 1 + i, new ListRow(header, listRowAdapter)); 104 } 105 } 106 107 @Override onStart()108 public void onStart() { 109 super.onStart(); 110 ((PlaybackTransportControlActivity) getActivity()).registerPictureInPictureListener(this); 111 } 112 113 @Override onStop()114 public void onStop() { 115 ((PlaybackTransportControlActivity) getActivity()).unregisterPictureInPictureListener(this); 116 super.onStop(); 117 } 118 119 @Override onPictureInPictureModeChanged(boolean isInPictureInPictureMode)120 public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) { 121 if (isInPictureInPictureMode) { 122 // Hide the controls in picture-in-picture mode. 123 setControlsOverlayAutoHideEnabled(true); 124 hideControlsOverlay(true); 125 } else { 126 setControlsOverlayAutoHideEnabled(mGlue.isPlaying()); 127 } 128 } 129 } 130