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