Home | History | Annotate | Download | only in app
      1 // CHECKSTYLE:OFF Generated code
      2 /* This file is auto-generated from BaseSupportFragment.java.  DO NOT MODIFY. */
      3 
      4 /*
      5  * Copyright (C) 2014 The Android Open Source Project
      6  *
      7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      8  * in compliance with the License. You may obtain a copy of the License at
      9  *
     10  * http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software distributed under the License
     13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
     14  * or implied. See the License for the specific language governing permissions and limitations under
     15  * the License.
     16  */
     17 package androidx.leanback.app;
     18 
     19 import android.annotation.SuppressLint;
     20 import android.os.Bundle;
     21 import android.view.View;
     22 import android.view.ViewTreeObserver;
     23 
     24 import androidx.annotation.NonNull;
     25 import androidx.annotation.Nullable;
     26 import androidx.leanback.transition.TransitionHelper;
     27 import androidx.leanback.transition.TransitionListener;
     28 import androidx.leanback.util.StateMachine;
     29 import androidx.leanback.util.StateMachine.Condition;
     30 import androidx.leanback.util.StateMachine.Event;
     31 import androidx.leanback.util.StateMachine.State;
     32 
     33 /**
     34  * Base class for leanback Fragments. This class is not intended to be subclassed by apps.
     35  * @deprecated use {@link BaseSupportFragment}
     36  */
     37 @Deprecated
     38 @SuppressWarnings("FragmentNotInstantiable")
     39 public class BaseFragment extends BrandedFragment {
     40 
     41     /**
     42      * The start state for all
     43      */
     44     final State STATE_START = new State("START", true, false);
     45 
     46     /**
     47      * Initial State for ENTRNACE transition.
     48      */
     49     final State STATE_ENTRANCE_INIT = new State("ENTRANCE_INIT");
     50 
     51     /**
     52      * prepareEntranceTransition is just called, but view not ready yet. We can enable the
     53      * busy spinner.
     54      */
     55     final State STATE_ENTRANCE_ON_PREPARED = new State("ENTRANCE_ON_PREPARED", true, false) {
     56         @Override
     57         public void run() {
     58             mProgressBarManager.show();
     59         }
     60     };
     61 
     62     /**
     63      * prepareEntranceTransition is called and main content view to slide in was created, so we can
     64      * call {@link #onEntranceTransitionPrepare}. Note that we dont set initial content to invisible
     65      * in this State, the process is very different in subclass, e.g. BrowseFragment hide header
     66      * views and hide main fragment view in two steps.
     67      */
     68     final State STATE_ENTRANCE_ON_PREPARED_ON_CREATEVIEW = new State(
     69             "ENTRANCE_ON_PREPARED_ON_CREATEVIEW") {
     70         @Override
     71         public void run() {
     72             onEntranceTransitionPrepare();
     73         }
     74     };
     75 
     76     /**
     77      * execute the entrance transition.
     78      */
     79     final State STATE_ENTRANCE_PERFORM = new State("STATE_ENTRANCE_PERFORM") {
     80         @Override
     81         public void run() {
     82             mProgressBarManager.hide();
     83             onExecuteEntranceTransition();
     84         }
     85     };
     86 
     87     /**
     88      * execute onEntranceTransitionEnd.
     89      */
     90     final State STATE_ENTRANCE_ON_ENDED = new State("ENTRANCE_ON_ENDED") {
     91         @Override
     92         public void run() {
     93             onEntranceTransitionEnd();
     94         }
     95     };
     96 
     97     /**
     98      * either entrance transition completed or skipped
     99      */
    100     final State STATE_ENTRANCE_COMPLETE = new State("ENTRANCE_COMPLETE", true, false);
    101 
    102     /**
    103      * Event fragment.onCreate()
    104      */
    105     final Event EVT_ON_CREATE = new Event("onCreate");
    106 
    107     /**
    108      * Event fragment.onViewCreated()
    109      */
    110     final Event EVT_ON_CREATEVIEW = new Event("onCreateView");
    111 
    112     /**
    113      * Event for {@link #prepareEntranceTransition()} is called.
    114      */
    115     final Event EVT_PREPARE_ENTRANCE = new Event("prepareEntranceTransition");
    116 
    117     /**
    118      * Event for {@link #startEntranceTransition()} is called.
    119      */
    120     final Event EVT_START_ENTRANCE = new Event("startEntranceTransition");
    121 
    122     /**
    123      * Event for entrance transition is ended through Transition listener.
    124      */
    125     final Event EVT_ENTRANCE_END = new Event("onEntranceTransitionEnd");
    126 
    127     /**
    128      * Event for skipping entrance transition if not supported.
    129      */
    130     final Condition COND_TRANSITION_NOT_SUPPORTED = new Condition("EntranceTransitionNotSupport") {
    131         @Override
    132         public boolean canProceed() {
    133             return !TransitionHelper.systemSupportsEntranceTransitions();
    134         }
    135     };
    136 
    137     final StateMachine mStateMachine = new StateMachine();
    138 
    139     Object mEntranceTransition;
    140     final ProgressBarManager mProgressBarManager = new ProgressBarManager();
    141 
    142     @SuppressLint("ValidFragment")
    143     BaseFragment() {
    144     }
    145 
    146     @Override
    147     public void onCreate(Bundle savedInstanceState) {
    148         createStateMachineStates();
    149         createStateMachineTransitions();
    150         mStateMachine.start();
    151         super.onCreate(savedInstanceState);
    152         mStateMachine.fireEvent(EVT_ON_CREATE);
    153     }
    154 
    155     void createStateMachineStates() {
    156         mStateMachine.addState(STATE_START);
    157         mStateMachine.addState(STATE_ENTRANCE_INIT);
    158         mStateMachine.addState(STATE_ENTRANCE_ON_PREPARED);
    159         mStateMachine.addState(STATE_ENTRANCE_ON_PREPARED_ON_CREATEVIEW);
    160         mStateMachine.addState(STATE_ENTRANCE_PERFORM);
    161         mStateMachine.addState(STATE_ENTRANCE_ON_ENDED);
    162         mStateMachine.addState(STATE_ENTRANCE_COMPLETE);
    163     }
    164 
    165     void createStateMachineTransitions() {
    166         mStateMachine.addTransition(STATE_START, STATE_ENTRANCE_INIT, EVT_ON_CREATE);
    167         mStateMachine.addTransition(STATE_ENTRANCE_INIT, STATE_ENTRANCE_COMPLETE,
    168                 COND_TRANSITION_NOT_SUPPORTED);
    169         mStateMachine.addTransition(STATE_ENTRANCE_INIT, STATE_ENTRANCE_COMPLETE,
    170                 EVT_ON_CREATEVIEW);
    171         mStateMachine.addTransition(STATE_ENTRANCE_INIT, STATE_ENTRANCE_ON_PREPARED,
    172                 EVT_PREPARE_ENTRANCE);
    173         mStateMachine.addTransition(STATE_ENTRANCE_ON_PREPARED,
    174                 STATE_ENTRANCE_ON_PREPARED_ON_CREATEVIEW,
    175                 EVT_ON_CREATEVIEW);
    176         mStateMachine.addTransition(STATE_ENTRANCE_ON_PREPARED,
    177                 STATE_ENTRANCE_PERFORM,
    178                 EVT_START_ENTRANCE);
    179         mStateMachine.addTransition(STATE_ENTRANCE_ON_PREPARED_ON_CREATEVIEW,
    180                 STATE_ENTRANCE_PERFORM);
    181         mStateMachine.addTransition(STATE_ENTRANCE_PERFORM,
    182                 STATE_ENTRANCE_ON_ENDED,
    183                 EVT_ENTRANCE_END);
    184         mStateMachine.addTransition(STATE_ENTRANCE_ON_ENDED, STATE_ENTRANCE_COMPLETE);
    185     }
    186 
    187     @Override
    188     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    189         super.onViewCreated(view, savedInstanceState);
    190         mStateMachine.fireEvent(EVT_ON_CREATEVIEW);
    191     }
    192 
    193     /**
    194      * Enables entrance transition.<p>
    195      * Entrance transition is the standard slide-in transition that shows rows of data in
    196      * browse screen and details screen.
    197      * <p>
    198      * The method is ignored before LOLLIPOP (API21).
    199      * <p>
    200      * This method must be called in or
    201      * before onCreate().  Typically entrance transition should be enabled when savedInstance is
    202      * null so that fragment restored from instanceState does not run an extra entrance transition.
    203      * When the entrance transition is enabled, the fragment will make headers and content
    204      * hidden initially.
    205      * When data of rows are ready, app must call {@link #startEntranceTransition()} to kick off
    206      * the transition, otherwise the rows will be invisible forever.
    207      * <p>
    208      * It is similar to android:windowsEnterTransition and can be considered a late-executed
    209      * android:windowsEnterTransition controlled by app.  There are two reasons that app needs it:
    210      * <li> Workaround the problem that activity transition is not available between launcher and
    211      * app.  Browse activity must programmatically start the slide-in transition.</li>
    212      * <li> Separates DetailsOverviewRow transition from other rows transition.  So that
    213      * the DetailsOverviewRow transition can be executed earlier without waiting for all rows
    214      * to be loaded.</li>
    215      * <p>
    216      * Transition object is returned by createEntranceTransition().  Typically the app does not need
    217      * override the default transition that browse and details provides.
    218      */
    219     public void prepareEntranceTransition() {
    220         mStateMachine.fireEvent(EVT_PREPARE_ENTRANCE);
    221     }
    222 
    223     /**
    224      * Create entrance transition.  Subclass can override to load transition from
    225      * resource or construct manually.  Typically app does not need to
    226      * override the default transition that browse and details provides.
    227      */
    228     protected Object createEntranceTransition() {
    229         return null;
    230     }
    231 
    232     /**
    233      * Run entrance transition.  Subclass may use TransitionManager to perform
    234      * go(Scene) or beginDelayedTransition().  App should not override the default
    235      * implementation of browse and details fragment.
    236      */
    237     protected void runEntranceTransition(Object entranceTransition) {
    238     }
    239 
    240     /**
    241      * Callback when entrance transition is prepared.  This is when fragment should
    242      * stop user input and animations.
    243      */
    244     protected void onEntranceTransitionPrepare() {
    245     }
    246 
    247     /**
    248      * Callback when entrance transition is started.  This is when fragment should
    249      * stop processing layout.
    250      */
    251     protected void onEntranceTransitionStart() {
    252     }
    253 
    254     /**
    255      * Callback when entrance transition is ended.
    256      */
    257     protected void onEntranceTransitionEnd() {
    258     }
    259 
    260     /**
    261      * When fragment finishes loading data, it should call startEntranceTransition()
    262      * to execute the entrance transition.
    263      * startEntranceTransition() will start transition only if both two conditions
    264      * are satisfied:
    265      * <li> prepareEntranceTransition() was called.</li>
    266      * <li> has not executed entrance transition yet.</li>
    267      * <p>
    268      * If startEntranceTransition() is called before onViewCreated(), it will be pending
    269      * and executed when view is created.
    270      */
    271     public void startEntranceTransition() {
    272         mStateMachine.fireEvent(EVT_START_ENTRANCE);
    273     }
    274 
    275     void onExecuteEntranceTransition() {
    276         // wait till views get their initial position before start transition
    277         final View view = getView();
    278         if (view == null) {
    279             // fragment view destroyed, transition not needed
    280             return;
    281         }
    282         view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    283             @Override
    284             public boolean onPreDraw() {
    285                 view.getViewTreeObserver().removeOnPreDrawListener(this);
    286                 if (FragmentUtil.getContext(BaseFragment.this) == null || getView() == null) {
    287                     // bail out if fragment is destroyed immediately after startEntranceTransition
    288                     return true;
    289                 }
    290                 internalCreateEntranceTransition();
    291                 onEntranceTransitionStart();
    292                 if (mEntranceTransition != null) {
    293                     runEntranceTransition(mEntranceTransition);
    294                 } else {
    295                     mStateMachine.fireEvent(EVT_ENTRANCE_END);
    296                 }
    297                 return false;
    298             }
    299         });
    300         view.invalidate();
    301     }
    302 
    303     void internalCreateEntranceTransition() {
    304         mEntranceTransition = createEntranceTransition();
    305         if (mEntranceTransition == null) {
    306             return;
    307         }
    308         TransitionHelper.addTransitionListener(mEntranceTransition, new TransitionListener() {
    309             @Override
    310             public void onTransitionEnd(Object transition) {
    311                 mEntranceTransition = null;
    312                 mStateMachine.fireEvent(EVT_ENTRANCE_END);
    313             }
    314         });
    315     }
    316 
    317     /**
    318      * Returns the {@link ProgressBarManager}.
    319      * @return The {@link ProgressBarManager}.
    320      */
    321     public final ProgressBarManager getProgressBarManager() {
    322         return mProgressBarManager;
    323     }
    324 }
    325