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