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