1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14package android.support.v17.leanback.widget; 15 16import android.app.Activity; 17import android.os.Handler; 18import android.support.v17.leanback.transition.TransitionHelper; 19import android.support.v17.leanback.transition.TransitionListener; 20import android.support.v17.leanback.widget.FullWidthDetailsOverviewRowPresenter.ViewHolder; 21import android.support.v4.app.ActivityCompat; 22import android.support.v4.view.ViewCompat; 23import android.text.TextUtils; 24import android.util.Log; 25 26import java.lang.ref.WeakReference; 27 28/** 29 * Helper class to assist delayed shared element activity transition for view created by 30 * {@link FullWidthDetailsOverviewRowPresenter}. User must call 31 * {@link #setSharedElementEnterTransition(Activity, String, long)} during activity onCreate() and 32 * call {@link FullWidthDetailsOverviewRowPresenter#setListener(FullWidthDetailsOverviewRowPresenter.Listener)}. 33 * The helper implements {@link FullWidthDetailsOverviewRowPresenter.Listener} and starts delayed 34 * activity transition once {@link FullWidthDetailsOverviewRowPresenter.Listener#onBindLogo(ViewHolder)} 35 * is called. 36 */ 37public class FullWidthDetailsOverviewSharedElementHelper extends 38 FullWidthDetailsOverviewRowPresenter.Listener { 39 40 static final String TAG = "DetailsTransitionHelper"; 41 static final boolean DEBUG = false; 42 43 private static final long DEFAULT_TIMEOUT = 5000; 44 45 static class TransitionTimeOutRunnable implements Runnable { 46 WeakReference<FullWidthDetailsOverviewSharedElementHelper> mHelperRef; 47 48 TransitionTimeOutRunnable(FullWidthDetailsOverviewSharedElementHelper helper) { 49 mHelperRef = new WeakReference<FullWidthDetailsOverviewSharedElementHelper>(helper); 50 } 51 52 @Override 53 public void run() { 54 FullWidthDetailsOverviewSharedElementHelper helper = mHelperRef.get(); 55 if (helper == null) { 56 return; 57 } 58 if (DEBUG) { 59 Log.d(TAG, "timeout " + helper.mActivityToRunTransition); 60 } 61 helper.startPostponedEnterTransition(); 62 } 63 } 64 65 ViewHolder mViewHolder; 66 Activity mActivityToRunTransition; 67 private boolean mStartedPostpone; 68 String mSharedElementName; 69 private boolean mAutoStartSharedElementTransition = true; 70 71 public void setSharedElementEnterTransition(Activity activity, String sharedElementName) { 72 setSharedElementEnterTransition(activity, sharedElementName, DEFAULT_TIMEOUT); 73 } 74 75 public void setSharedElementEnterTransition(Activity activity, String sharedElementName, 76 long timeoutMs) { 77 if ((activity == null && !TextUtils.isEmpty(sharedElementName)) 78 || (activity != null && TextUtils.isEmpty(sharedElementName))) { 79 throw new IllegalArgumentException(); 80 } 81 if (activity == mActivityToRunTransition 82 && TextUtils.equals(sharedElementName, mSharedElementName)) { 83 return; 84 } 85 mActivityToRunTransition = activity; 86 mSharedElementName = sharedElementName; 87 if (DEBUG) { 88 Log.d(TAG, "postponeEnterTransition " + mActivityToRunTransition); 89 } 90 Object transition = TransitionHelper.getSharedElementEnterTransition(activity.getWindow()); 91 setAutoStartSharedElementTransition(transition != null); 92 ActivityCompat.postponeEnterTransition(mActivityToRunTransition); 93 if (timeoutMs > 0) { 94 new Handler().postDelayed(new TransitionTimeOutRunnable(this), timeoutMs); 95 } 96 } 97 98 /** 99 * Enable or disable auto startPostponedEnterTransition() when bound to logo. When it's 100 * disabled, app must call {@link #startPostponedEnterTransition()} to kick off 101 * windowEnterTransition. By default, it is disabled when there is no 102 * windowEnterSharedElementTransition set on the activity. 103 */ 104 public void setAutoStartSharedElementTransition(boolean enabled) { 105 mAutoStartSharedElementTransition = enabled; 106 } 107 108 /** 109 * Returns true if auto startPostponedEnterTransition() when bound to logo. When it's 110 * disabled, app must call {@link #startPostponedEnterTransition()} to kick off 111 * windowEnterTransition. By default, it is disabled when there is no 112 * windowEnterSharedElementTransition set on the activity. 113 */ 114 public boolean getAutoStartSharedElementTransition() { 115 return mAutoStartSharedElementTransition; 116 } 117 118 @Override 119 public void onBindLogo(ViewHolder vh) { 120 if (DEBUG) { 121 Log.d(TAG, "onBindLogo, could start transition of " + mActivityToRunTransition); 122 } 123 mViewHolder = vh; 124 if (!mAutoStartSharedElementTransition) { 125 return; 126 } 127 if (mViewHolder != null) { 128 if (DEBUG) { 129 Log.d(TAG, "rebind? clear transitionName on current viewHolder " 130 + mViewHolder.getOverviewView()); 131 } 132 ViewCompat.setTransitionName(mViewHolder.getLogoViewHolder().view, null); 133 } 134 // After we got a image drawable, we can determine size of right panel. 135 // We want right panel to have fixed size so that the right panel don't change size 136 // when the overview is layout as a small bounds in transition. 137 mViewHolder.getDetailsDescriptionFrame().postOnAnimation(new Runnable() { 138 @Override 139 public void run() { 140 if (DEBUG) { 141 Log.d(TAG, "setTransitionName "+mViewHolder.getOverviewView()); 142 } 143 ViewCompat.setTransitionName(mViewHolder.getLogoViewHolder().view, 144 mSharedElementName); 145 Object transition = TransitionHelper.getSharedElementEnterTransition( 146 mActivityToRunTransition.getWindow()); 147 if (transition != null) { 148 TransitionHelper.addTransitionListener(transition, new TransitionListener() { 149 @Override 150 public void onTransitionEnd(Object transition) { 151 if (DEBUG) { 152 Log.d(TAG, "onTransitionEnd " + mActivityToRunTransition); 153 } 154 // after transition if the action row still focused, transfer 155 // focus to its children 156 if (mViewHolder.getActionsRow().isFocused()) { 157 mViewHolder.getActionsRow().requestFocus(); 158 } 159 TransitionHelper.removeTransitionListener(transition, this); 160 } 161 }); 162 } 163 startPostponedEnterTransitionInternal(); 164 } 165 }); 166 } 167 168 /** 169 * Manually start postponed enter transition. 170 */ 171 public void startPostponedEnterTransition() { 172 new Handler().post(new Runnable(){ 173 @Override 174 public void run() { 175 startPostponedEnterTransitionInternal(); 176 } 177 }); 178 } 179 180 void startPostponedEnterTransitionInternal() { 181 if (!mStartedPostpone && mViewHolder != null) { 182 if (DEBUG) { 183 Log.d(TAG, "startPostponedEnterTransition " + mActivityToRunTransition); 184 } 185 ActivityCompat.startPostponedEnterTransition(mActivityToRunTransition); 186 mStartedPostpone = true; 187 } 188 } 189} 190