Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2015 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 package com.android.messaging.ui;
     17 
     18 import android.os.Parcelable;
     19 import android.view.View;
     20 import android.view.ViewGroup;
     21 
     22 /**
     23  * The base pager view holder implementation that handles basic view creation/destruction logic,
     24  * as well as logic to save/restore instance state that's persisted not only for activity
     25  * reconstruction (e.g. during a configuration change), but also cases where the individual
     26  * page view gets destroyed and recreated.
     27  *
     28  * To opt into saving/restoring instance state behavior for a particular page view, let the
     29  * PageView implement PersistentInstanceState to save and restore instance states to/from a
     30  * Parcelable.
     31  */
     32 public abstract class BasePagerViewHolder implements PagerViewHolder {
     33     protected View mView;
     34     protected Parcelable mSavedState;
     35 
     36     /**
     37      * This is called when the entire view pager is being torn down (due to configuration change
     38      * for example) that will be restored later.
     39      */
     40     @Override
     41     public Parcelable saveState() {
     42         savePendingState();
     43         return mSavedState;
     44     }
     45 
     46     /**
     47      * This is called when the view pager is being restored.
     48      */
     49     @Override
     50     public void restoreState(final Parcelable restoredState) {
     51         if (restoredState != null) {
     52             mSavedState = restoredState;
     53             // If the view is already there, let it restore the state. Otherwise, it will be
     54             // restored the next time the view gets created.
     55             restorePendingState();
     56         }
     57     }
     58 
     59     @Override
     60     public void resetState() {
     61         mSavedState = null;
     62         if (mView != null && (mView instanceof PersistentInstanceState)) {
     63             ((PersistentInstanceState) mView).resetState();
     64         }
     65     }
     66 
     67     /**
     68      * This is called when the view itself is being torn down. This may happen when the user
     69      * has flipped to another page in the view pager, so we want to save the current state if
     70      * possible.
     71      */
     72     @Override
     73     public View destroyView() {
     74         savePendingState();
     75         final View retView = mView;
     76         mView = null;
     77         return retView;
     78     }
     79 
     80     @Override
     81     public View getView(ViewGroup container) {
     82         if (mView == null) {
     83             mView = createView(container);
     84             // When initially created, check if the view has any saved state. If so restore it.
     85             restorePendingState();
     86         }
     87         return mView;
     88     }
     89 
     90     private void savePendingState() {
     91         if (mView != null && (mView instanceof PersistentInstanceState)) {
     92             mSavedState = ((PersistentInstanceState) mView).saveState();
     93         }
     94     }
     95 
     96     private void restorePendingState() {
     97         if (mView != null && (mView instanceof PersistentInstanceState) && (mSavedState != null)) {
     98             ((PersistentInstanceState) mView).restoreState(mSavedState);
     99         }
    100     }
    101 
    102     /**
    103      * Create and initialize a new page view.
    104      */
    105     protected abstract View createView(ViewGroup container);
    106 }
    107