1/*
2 * Copyright (C) 2012 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.content.Context;
22import android.content.res.TypedArray;
23import android.os.Bundle;
24import android.os.Parcel;
25import android.os.Parcelable;
26import android.util.AttributeSet;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.FrameLayout;
30import android.widget.LinearLayout;
31import android.widget.TabHost;
32import android.widget.TabWidget;
33
34/**
35 * Special TabHost that allows the use of {@link Fragment} objects for
36 * its tab content.  When placing this in a view hierarchy, after inflating
37 * the hierarchy you must call {@link #setup(Context, FragmentManager, int)}
38 * to complete the initialization of the tab host.
39 *
40 * <p>Here is a simple example of using a FragmentTabHost in an Activity:
41 *
42 * {@sample development/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.java
43 *      complete}
44 *
45 * <p>This can also be used inside of a fragment through fragment nesting:
46 *
47 * {@sample development/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabsFragmentSupport.java
48 *      complete}
49 */
50public class FragmentTabHost extends TabHost
51        implements TabHost.OnTabChangeListener {
52    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
53    private FrameLayout mRealTabContent;
54    private Context mContext;
55    private FragmentManager mFragmentManager;
56    private int mContainerId;
57    private TabHost.OnTabChangeListener mOnTabChangeListener;
58    private TabInfo mLastTab;
59    private boolean mAttached;
60
61    static final class TabInfo {
62        private final String tag;
63        private final Class<?> clss;
64        private final Bundle args;
65        private Fragment fragment;
66
67        TabInfo(String _tag, Class<?> _class, Bundle _args) {
68            tag = _tag;
69            clss = _class;
70            args = _args;
71        }
72    }
73
74    static class DummyTabFactory implements TabHost.TabContentFactory {
75        private final Context mContext;
76
77        public DummyTabFactory(Context context) {
78            mContext = context;
79        }
80
81        @Override
82        public View createTabContent(String tag) {
83            View v = new View(mContext);
84            v.setMinimumWidth(0);
85            v.setMinimumHeight(0);
86            return v;
87        }
88    }
89
90    static class SavedState extends BaseSavedState {
91        String curTab;
92
93        SavedState(Parcelable superState) {
94            super(superState);
95        }
96
97        private SavedState(Parcel in) {
98            super(in);
99            curTab = in.readString();
100        }
101
102        @Override
103        public void writeToParcel(Parcel out, int flags) {
104            super.writeToParcel(out, flags);
105            out.writeString(curTab);
106        }
107
108        @Override
109        public String toString() {
110            return "FragmentTabHost.SavedState{"
111                    + Integer.toHexString(System.identityHashCode(this))
112                    + " curTab=" + curTab + "}";
113        }
114
115        public static final Parcelable.Creator<SavedState> CREATOR
116                = new Parcelable.Creator<SavedState>() {
117            public SavedState createFromParcel(Parcel in) {
118                return new SavedState(in);
119            }
120
121            public SavedState[] newArray(int size) {
122                return new SavedState[size];
123            }
124        };
125    }
126
127    public FragmentTabHost(Context context) {
128        // Note that we call through to the version that takes an AttributeSet,
129        // because the simple Context construct can result in a broken object!
130        super(context, null);
131        initFragmentTabHost(context, null);
132    }
133
134    public FragmentTabHost(Context context, AttributeSet attrs) {
135        super(context, attrs);
136        initFragmentTabHost(context, attrs);
137    }
138
139    private void initFragmentTabHost(Context context, AttributeSet attrs) {
140        TypedArray a = context.obtainStyledAttributes(attrs,
141                new int[] { android.R.attr.inflatedId }, 0, 0);
142        mContainerId = a.getResourceId(0, 0);
143        a.recycle();
144
145        super.setOnTabChangedListener(this);
146    }
147
148    private void ensureHierarchy(Context context) {
149        // If owner hasn't made its own view hierarchy, then as a convenience
150        // we will construct a standard one here.
151        if (findViewById(android.R.id.tabs) == null) {
152            LinearLayout ll = new LinearLayout(context);
153            ll.setOrientation(LinearLayout.VERTICAL);
154            addView(ll, new FrameLayout.LayoutParams(
155                    ViewGroup.LayoutParams.FILL_PARENT,
156                    ViewGroup.LayoutParams.FILL_PARENT));
157
158            TabWidget tw = new TabWidget(context);
159            tw.setId(android.R.id.tabs);
160            tw.setOrientation(TabWidget.HORIZONTAL);
161            ll.addView(tw, new LinearLayout.LayoutParams(
162                    ViewGroup.LayoutParams.FILL_PARENT,
163                    ViewGroup.LayoutParams.WRAP_CONTENT, 0));
164
165            FrameLayout fl = new FrameLayout(context);
166            fl.setId(android.R.id.tabcontent);
167            ll.addView(fl, new LinearLayout.LayoutParams(0, 0, 0));
168
169            mRealTabContent = fl = new FrameLayout(context);
170            mRealTabContent.setId(mContainerId);
171            ll.addView(fl, new LinearLayout.LayoutParams(
172                    LinearLayout.LayoutParams.FILL_PARENT, 0, 1));
173        }
174    }
175
176    /**
177     * @deprecated Don't call the original TabHost setup, you must instead
178     * call {@link #setup(Context, FragmentManager)} or
179     * {@link #setup(Context, FragmentManager, int)}.
180     */
181    @Override @Deprecated
182    public void setup() {
183        throw new IllegalStateException(
184                "Must call setup() that takes a Context and FragmentManager");
185    }
186
187    public void setup(Context context, FragmentManager manager) {
188        ensureHierarchy(context);  // Ensure views required by super.setup()
189        super.setup();
190        mContext = context;
191        mFragmentManager = manager;
192        ensureContent();
193    }
194
195    public void setup(Context context, FragmentManager manager, int containerId) {
196        ensureHierarchy(context);  // Ensure views required by super.setup()
197        super.setup();
198        mContext = context;
199        mFragmentManager = manager;
200        mContainerId = containerId;
201        ensureContent();
202        mRealTabContent.setId(containerId);
203
204        // We must have an ID to be able to save/restore our state.  If
205        // the owner hasn't set one at this point, we will set it ourself.
206        if (getId() == View.NO_ID) {
207            setId(android.R.id.tabhost);
208        }
209    }
210
211    private void ensureContent() {
212        if (mRealTabContent == null) {
213            mRealTabContent = (FrameLayout)findViewById(mContainerId);
214            if (mRealTabContent == null) {
215                throw new IllegalStateException(
216                        "No tab content FrameLayout found for id " + mContainerId);
217            }
218        }
219    }
220
221    @Override
222    public void setOnTabChangedListener(OnTabChangeListener l) {
223        mOnTabChangeListener = l;
224    }
225
226    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
227        tabSpec.setContent(new DummyTabFactory(mContext));
228        String tag = tabSpec.getTag();
229
230        TabInfo info = new TabInfo(tag, clss, args);
231
232        if (mAttached) {
233            // If we are already attached to the window, then check to make
234            // sure this tab's fragment is inactive if it exists.  This shouldn't
235            // normally happen.
236            info.fragment = mFragmentManager.findFragmentByTag(tag);
237            if (info.fragment != null && !info.fragment.isDetached()) {
238                FragmentTransaction ft = mFragmentManager.beginTransaction();
239                ft.detach(info.fragment);
240                ft.commit();
241            }
242        }
243
244        mTabs.add(info);
245        addTab(tabSpec);
246    }
247
248    @Override
249    protected void onAttachedToWindow() {
250        super.onAttachedToWindow();
251
252        String currentTab = getCurrentTabTag();
253
254        // Go through all tabs and make sure their fragments match
255        // the correct state.
256        FragmentTransaction ft = null;
257        for (int i=0; i<mTabs.size(); i++) {
258            TabInfo tab = mTabs.get(i);
259            tab.fragment = mFragmentManager.findFragmentByTag(tab.tag);
260            if (tab.fragment != null && !tab.fragment.isDetached()) {
261                if (tab.tag.equals(currentTab)) {
262                    // The fragment for this tab is already there and
263                    // active, and it is what we really want to have
264                    // as the current tab.  Nothing to do.
265                    mLastTab = tab;
266                } else {
267                    // This fragment was restored in the active state,
268                    // but is not the current tab.  Deactivate it.
269                    if (ft == null) {
270                        ft = mFragmentManager.beginTransaction();
271                    }
272                    ft.detach(tab.fragment);
273                }
274            }
275        }
276
277        // We are now ready to go.  Make sure we are switched to the
278        // correct tab.
279        mAttached = true;
280        ft = doTabChanged(currentTab, ft);
281        if (ft != null) {
282            ft.commit();
283            mFragmentManager.executePendingTransactions();
284        }
285    }
286
287    @Override
288    protected void onDetachedFromWindow() {
289        super.onDetachedFromWindow();
290        mAttached = false;
291    }
292
293    @Override
294    protected Parcelable onSaveInstanceState() {
295        Parcelable superState = super.onSaveInstanceState();
296        SavedState ss = new SavedState(superState);
297        ss.curTab = getCurrentTabTag();
298        return ss;
299    }
300
301    @Override
302    protected void onRestoreInstanceState(Parcelable state) {
303        if (!(state instanceof SavedState)) {
304            super.onRestoreInstanceState(state);
305            return;
306        }
307        SavedState ss = (SavedState) state;
308        super.onRestoreInstanceState(ss.getSuperState());
309        setCurrentTabByTag(ss.curTab);
310    }
311
312    @Override
313    public void onTabChanged(String tabId) {
314        if (mAttached) {
315            FragmentTransaction ft = doTabChanged(tabId, null);
316            if (ft != null) {
317                ft.commit();
318            }
319        }
320        if (mOnTabChangeListener != null) {
321            mOnTabChangeListener.onTabChanged(tabId);
322        }
323    }
324
325    private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
326        TabInfo newTab = null;
327        for (int i=0; i<mTabs.size(); i++) {
328            TabInfo tab = mTabs.get(i);
329            if (tab.tag.equals(tabId)) {
330                newTab = tab;
331            }
332        }
333        if (newTab == null) {
334            throw new IllegalStateException("No tab known for tag " + tabId);
335        }
336        if (mLastTab != newTab) {
337            if (ft == null) {
338                ft = mFragmentManager.beginTransaction();
339            }
340            if (mLastTab != null) {
341                if (mLastTab.fragment != null) {
342                    ft.detach(mLastTab.fragment);
343                }
344            }
345            if (newTab != null) {
346                if (newTab.fragment == null) {
347                    newTab.fragment = Fragment.instantiate(mContext,
348                            newTab.clss.getName(), newTab.args);
349                    ft.add(mContainerId, newTab.fragment, newTab.tag);
350                } else {
351                    ft.attach(newTab.fragment);
352                }
353            }
354
355            mLastTab = newTab;
356        }
357        return ft;
358    }
359}
360