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