AppsCustomizeTabHost.java revision 6a26e5bed48bf64996aef3ea1db4767ee147640e
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 com.android.launcher2;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.util.AttributeSet;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.TabHost;
26import android.widget.TextView;
27
28import com.android.launcher.R;
29
30public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable,
31        TabHost.OnTabChangeListener  {
32    static final String LOG_TAG = "AppsCustomizeTabHost";
33
34    private static final String APPS_TAB_TAG = "APPS";
35    private static final String WIDGETS_TAB_TAG = "WIDGETS";
36
37    private final LayoutInflater mLayoutInflater;
38
39    public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
40        super(context, attrs);
41        mLayoutInflater = LayoutInflater.from(context);
42    }
43
44    void selectAppsTab() {
45        setCurrentTabByTag(APPS_TAB_TAG);
46    }
47    void selectWidgetsTab() {
48        setCurrentTabByTag(WIDGETS_TAB_TAG);
49    }
50
51    /**
52     * Setup the tab host and create all necessary tabs.
53     */
54    @Override
55    protected void onFinishInflate() {
56        // Setup the tab host
57        setup();
58
59        final ViewGroup tabs = (ViewGroup) findViewById(com.android.internal.R.id.tabs);
60        final AppsCustomizePagedView content = (AppsCustomizePagedView)
61                findViewById(R.id.apps_customize_pane_content);
62        if (tabs == null || content == null) throw new Resources.NotFoundException();
63
64        // Configure the tabs content factory to return the same paged view (that we change the
65        // content filter on)
66        TabContentFactory contentFactory = new TabContentFactory() {
67            public View createTabContent(String tag) {
68                return content;
69            }
70        };
71
72        // Create the tabs
73        TextView tabView;
74        tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
75        tabView.setText(mContext.getString(R.string.all_apps_button_label));
76        addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
77        tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
78        tabView.setText(mContext.getString(R.string.widgets_tab_label));
79        addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
80        setOnTabChangedListener(this);
81
82        // Set the width of the tab bar to match the content (for now)
83        tabs.getLayoutParams().width = content.getPageContentWidth();
84    }
85
86    @Override
87    public void onTabChanged(String tabId) {
88        final AppsCustomizePagedView content = (AppsCustomizePagedView)
89                findViewById(R.id.apps_customize_pane_content);
90        content.setContentType(getContentTypeForTabTag(tabId));
91    }
92
93    /**
94     * Returns the content type for the specified tab tag.
95     */
96    public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
97        if (tag.equals(APPS_TAB_TAG)) {
98            return AppsCustomizePagedView.ContentType.Applications;
99        } else if (tag.equals(WIDGETS_TAB_TAG)) {
100            return AppsCustomizePagedView.ContentType.Widgets;
101        }
102        return AppsCustomizePagedView.ContentType.Applications;
103    }
104
105    /**
106     * Returns the tab tag for a given content type.
107     */
108    public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
109        if (type == AppsCustomizePagedView.ContentType.Applications) {
110            return APPS_TAB_TAG;
111        } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
112            return WIDGETS_TAB_TAG;
113        }
114        return APPS_TAB_TAG;
115    }
116
117    /**
118     * Disable focus on anything under this view in the hierarchy if we are not visible.
119     */
120    @Override
121    public int getDescendantFocusability() {
122        if (getVisibility() != View.VISIBLE) {
123            return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
124        }
125        return super.getDescendantFocusability();
126    }
127
128    /* LauncherTransitionable overrides */
129    @Override
130    public void onLauncherTransitionStart(android.animation.Animator animation) {
131        // TODO-APPS_CUSTOMIZE: see AllAppsTabbed.onLauncherTransitionStart();
132    }
133    @Override
134    public void onLauncherTransitionEnd(android.animation.Animator animation) {
135        // TODO-APPS_CUSTOMIZE: see AllAppsTabbed.onLauncherTransitionEnd();
136    }
137}
138