1/* 2 * Copyright (C) 2017 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 17package android.support.v17.leanback.widget; 18 19import android.content.Context; 20import android.support.v17.leanback.media.PlaybackGlueHostImpl; 21import android.view.KeyEvent; 22import android.view.View; 23import android.view.ViewGroup; 24import android.widget.FrameLayout; 25 26/** 27 * Example to create a ViewHolder and rebind when notifyPlaybackRowChanged. 28 */ 29public class PlaybackGlueHostImplWithViewHolder extends PlaybackGlueHostImpl 30 implements PlaybackSeekUi { 31 protected Context mContext; 32 protected PlaybackRowPresenter.ViewHolder mViewHolder; 33 protected ViewGroup mRootView; 34 35 protected int mLayoutWidth = 1920; 36 protected int mLayoutHeight = ViewGroup.LayoutParams.WRAP_CONTENT; 37 Client mSeekClient; 38 39 public PlaybackGlueHostImplWithViewHolder(Context context) { 40 mContext = context; 41 } 42 43 @Override 44 public void setPlaybackRow(Row row) { 45 super.setPlaybackRow(row); 46 createViewHolderIfNeeded(); 47 } 48 49 @Override 50 public void setPlaybackRowPresenter(PlaybackRowPresenter presenter) { 51 super.setPlaybackRowPresenter(presenter); 52 createViewHolderIfNeeded(); 53 } 54 55 void createViewHolderIfNeeded() { 56 if (mViewHolder == null && mPlaybackRowPresenter != null && mRow != null) { 57 mViewHolder = (PlaybackRowPresenter.ViewHolder) 58 mPlaybackRowPresenter.onCreateViewHolder(mRootView = new FrameLayout(mContext)); 59 mRootView.addView(mViewHolder.view, mLayoutWidth, mLayoutHeight); 60 mRootView.measure( 61 View.MeasureSpec.makeMeasureSpec(1920, View.MeasureSpec.AT_MOST), 62 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 63 mRootView.layout(0, 0, mRootView.getMeasuredWidth(), mRootView.getMeasuredHeight()); 64 mPlaybackRowPresenter.onBindViewHolder(mViewHolder, mRow); 65 if (mViewHolder instanceof PlaybackSeekUi) { 66 ((PlaybackSeekUi) mViewHolder).setPlaybackSeekUiClient(mChainedClient); 67 } 68 } 69 } 70 71 @Override 72 public void notifyPlaybackRowChanged() { 73 if (mViewHolder != null) { 74 mPlaybackRowPresenter.onUnbindRowViewHolder(mViewHolder); 75 mPlaybackRowPresenter.onBindViewHolder(mViewHolder, mRow); 76 } 77 } 78 79 public void sendKeyEvent(KeyEvent event) { 80 mRootView.dispatchKeyEvent(event); 81 } 82 83 public void sendKeyDownUp(int keyCode) { 84 sendKeyDownUp(keyCode, 1); 85 } 86 87 public void sendKeyDownUp(int keyCode, int repeat) { 88 for (int i = 0; i < repeat; i++) { 89 mRootView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode)); 90 } 91 mRootView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyCode)); 92 } 93 94 @Override 95 public void setPlaybackSeekUiClient(Client client) { 96 mSeekClient = client; 97 } 98 99 protected Client mChainedClient = new Client() { 100 @Override 101 public boolean isSeekEnabled() { 102 return mSeekClient == null ? false : mSeekClient.isSeekEnabled(); 103 } 104 105 @Override 106 public void onSeekStarted() { 107 mSeekClient.onSeekStarted(); 108 } 109 110 @Override 111 public PlaybackSeekDataProvider getPlaybackSeekDataProvider() { 112 return mSeekClient.getPlaybackSeekDataProvider(); 113 } 114 115 @Override 116 public void onSeekPositionChanged(long pos) { 117 mSeekClient.onSeekPositionChanged(pos); 118 } 119 120 @Override 121 public void onSeekFinished(boolean cancelled) { 122 mSeekClient.onSeekFinished(cancelled); 123 } 124 }; 125} 126