1/*
2 * Copyright (C) 2011 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.v4.app;
18
19import java.util.ArrayList;
20
21import android.os.Bundle;
22import android.os.Parcelable;
23import android.support.v4.view.PagerAdapter;
24import android.util.Log;
25import android.view.View;
26import android.view.ViewGroup;
27
28/**
29 * Implementation of {@link android.support.v4.view.PagerAdapter} that
30 * uses a {@link Fragment} to manage each page. This class also handles
31 * saving and restoring of fragment's state.
32 *
33 * <p>This version of the pager is more useful when there are a large number
34 * of pages, working more like a list view.  When pages are not visible to
35 * the user, their entire fragment may be destroyed, only keeping the saved
36 * state of that fragment.  This allows the pager to hold on to much less
37 * memory associated with each visited page as compared to
38 * {@link FragmentPagerAdapter} at the cost of potentially more overhead when
39 * switching between pages.
40 *
41 * <p>When using FragmentPagerAdapter the host ViewPager must have a
42 * valid ID set.</p>
43 *
44 * <p>Subclasses only need to implement {@link #getItem(int)}
45 * and {@link #getCount()} to have a working adapter.
46 *
47 * <p>Here is an example implementation of a pager containing fragments of
48 * lists:
49 *
50 * {@sample development/samples/Support13Demos/src/com/example/android/supportv13/app/FragmentStatePagerSupport.java
51 *      complete}
52 *
53 * <p>The <code>R.layout.fragment_pager</code> resource of the top-level fragment is:
54 *
55 * {@sample development/samples/Support13Demos/res/layout/fragment_pager.xml
56 *      complete}
57 *
58 * <p>The <code>R.layout.fragment_pager_list</code> resource containing each
59 * individual fragment's layout is:
60 *
61 * {@sample development/samples/Support13Demos/res/layout/fragment_pager_list.xml
62 *      complete}
63 */
64public abstract class FragmentStatePagerAdapter extends PagerAdapter {
65    private static final String TAG = "FragmentStatePagerAdapter";
66    private static final boolean DEBUG = false;
67
68    private final FragmentManager mFragmentManager;
69    private FragmentTransaction mCurTransaction = null;
70
71    private ArrayList<Fragment.SavedState> mSavedState = new ArrayList<Fragment.SavedState>();
72    private ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
73    private Fragment mCurrentPrimaryItem = null;
74
75    public FragmentStatePagerAdapter(FragmentManager fm) {
76        mFragmentManager = fm;
77    }
78
79    /**
80     * Return the Fragment associated with a specified position.
81     */
82    public abstract Fragment getItem(int position);
83
84    @Override
85    public void startUpdate(ViewGroup container) {
86    }
87
88    @Override
89    public Object instantiateItem(ViewGroup container, int position) {
90        // If we already have this item instantiated, there is nothing
91        // to do.  This can happen when we are restoring the entire pager
92        // from its saved state, where the fragment manager has already
93        // taken care of restoring the fragments we previously had instantiated.
94        if (mFragments.size() > position) {
95            Fragment f = mFragments.get(position);
96            if (f != null) {
97                return f;
98            }
99        }
100
101        if (mCurTransaction == null) {
102            mCurTransaction = mFragmentManager.beginTransaction();
103        }
104
105        Fragment fragment = getItem(position);
106        if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
107        if (mSavedState.size() > position) {
108            Fragment.SavedState fss = mSavedState.get(position);
109            if (fss != null) {
110                fragment.setInitialSavedState(fss);
111            }
112        }
113        while (mFragments.size() <= position) {
114            mFragments.add(null);
115        }
116        fragment.setMenuVisibility(false);
117        fragment.setUserVisibleHint(false);
118        mFragments.set(position, fragment);
119        mCurTransaction.add(container.getId(), fragment);
120
121        return fragment;
122    }
123
124    @Override
125    public void destroyItem(ViewGroup container, int position, Object object) {
126        Fragment fragment = (Fragment)object;
127
128        if (mCurTransaction == null) {
129            mCurTransaction = mFragmentManager.beginTransaction();
130        }
131        if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object
132                + " v=" + ((Fragment)object).getView());
133        while (mSavedState.size() <= position) {
134            mSavedState.add(null);
135        }
136        mSavedState.set(position, mFragmentManager.saveFragmentInstanceState(fragment));
137        mFragments.set(position, null);
138
139        mCurTransaction.remove(fragment);
140    }
141
142    @Override
143    public void setPrimaryItem(ViewGroup container, int position, Object object) {
144        Fragment fragment = (Fragment)object;
145        if (fragment != mCurrentPrimaryItem) {
146            if (mCurrentPrimaryItem != null) {
147                mCurrentPrimaryItem.setMenuVisibility(false);
148                mCurrentPrimaryItem.setUserVisibleHint(false);
149            }
150            if (fragment != null) {
151                fragment.setMenuVisibility(true);
152                fragment.setUserVisibleHint(true);
153            }
154            mCurrentPrimaryItem = fragment;
155        }
156    }
157
158    @Override
159    public void finishUpdate(ViewGroup container) {
160        if (mCurTransaction != null) {
161            mCurTransaction.commitAllowingStateLoss();
162            mCurTransaction = null;
163            mFragmentManager.executePendingTransactions();
164        }
165    }
166
167    @Override
168    public boolean isViewFromObject(View view, Object object) {
169        return ((Fragment)object).getView() == view;
170    }
171
172    @Override
173    public Parcelable saveState() {
174        Bundle state = null;
175        if (mSavedState.size() > 0) {
176            state = new Bundle();
177            Fragment.SavedState[] fss = new Fragment.SavedState[mSavedState.size()];
178            mSavedState.toArray(fss);
179            state.putParcelableArray("states", fss);
180        }
181        for (int i=0; i<mFragments.size(); i++) {
182            Fragment f = mFragments.get(i);
183            if (f != null) {
184                if (state == null) {
185                    state = new Bundle();
186                }
187                String key = "f" + i;
188                mFragmentManager.putFragment(state, key, f);
189            }
190        }
191        return state;
192    }
193
194    @Override
195    public void restoreState(Parcelable state, ClassLoader loader) {
196        if (state != null) {
197            Bundle bundle = (Bundle)state;
198            bundle.setClassLoader(loader);
199            Parcelable[] fss = bundle.getParcelableArray("states");
200            mSavedState.clear();
201            mFragments.clear();
202            if (fss != null) {
203                for (int i=0; i<fss.length; i++) {
204                    mSavedState.add((Fragment.SavedState)fss[i]);
205                }
206            }
207            Iterable<String> keys = bundle.keySet();
208            for (String key: keys) {
209                if (key.startsWith("f")) {
210                    int index = Integer.parseInt(key.substring(1));
211                    Fragment f = mFragmentManager.getFragment(bundle, key);
212                    if (f != null) {
213                        while (mFragments.size() <= index) {
214                            mFragments.add(null);
215                        }
216                        f.setMenuVisibility(false);
217                        mFragments.set(index, f);
218                    } else {
219                        Log.w(TAG, "Bad fragment at key " + key);
220                    }
221                }
222            }
223        }
224    }
225}
226