blob: 51db66c73330ac20e098c4a8a79c4fec3b10bec7 [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
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
Winson Chungf0ea4d32011-06-06 14:27:16 -070019import android.animation.Animator;
Winson Chung785d2eb2011-04-14 16:08:02 -070020import android.content.Context;
21import android.content.res.Resources;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
Winson Chung4179b4e2011-05-31 17:28:12 -070024import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070025import android.view.View;
26import android.view.ViewGroup;
27import android.widget.TabHost;
Winson Chungfaa13252011-06-13 18:15:54 -070028import android.widget.TabWidget;
Winson Chung785d2eb2011-04-14 16:08:02 -070029import android.widget.TextView;
30
31import com.android.launcher.R;
32
33public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable,
34 TabHost.OnTabChangeListener {
35 static final String LOG_TAG = "AppsCustomizeTabHost";
36
37 private static final String APPS_TAB_TAG = "APPS";
38 private static final String WIDGETS_TAB_TAG = "WIDGETS";
39
40 private final LayoutInflater mLayoutInflater;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041 private ViewGroup mTabs;
Winson Chung4179b4e2011-05-31 17:28:12 -070042 private AppsCustomizePagedView mAppsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070043
44 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
45 super(context, attrs);
46 mLayoutInflater = LayoutInflater.from(context);
47 }
48
Winson Chungf0ea4d32011-06-06 14:27:16 -070049 /**
50 * Convenience methods to select specific tabs
51 */
Winson Chung55b65502011-05-26 12:03:43 -070052 void selectAppsTab() {
53 setCurrentTabByTag(APPS_TAB_TAG);
54 }
55 void selectWidgetsTab() {
56 setCurrentTabByTag(WIDGETS_TAB_TAG);
57 }
58
Winson Chung785d2eb2011-04-14 16:08:02 -070059 /**
60 * Setup the tab host and create all necessary tabs.
61 */
62 @Override
63 protected void onFinishInflate() {
64 // Setup the tab host
65 setup();
66
Winson Chungfaa13252011-06-13 18:15:54 -070067 final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070068 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070069 findViewById(R.id.apps_customize_pane_content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070070 mTabs = tabs;
Winson Chung4179b4e2011-05-31 17:28:12 -070071 mAppsCustomizePane = appsCustomizePane;
72 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -070073
74 // Configure the tabs content factory to return the same paged view (that we change the
75 // content filter on)
76 TabContentFactory contentFactory = new TabContentFactory() {
77 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -070078 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070079 }
80 };
81
82 // Create the tabs
83 TextView tabView;
84 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
85 tabView.setText(mContext.getString(R.string.all_apps_button_label));
86 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -070087 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
88 tabView.setText(mContext.getString(R.string.widgets_tab_label));
89 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -070090 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -070091
92 // Setup the key listener to jump between the last tab view and the market icon
93 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
94 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
95 lastTab.setOnKeyListener(keyListener);
96 View shopButton = findViewById(R.id.market_button);
97 shopButton.setOnKeyListener(keyListener);
Winson Chungf0ea4d32011-06-06 14:27:16 -070098 }
Winson Chung785d2eb2011-04-14 16:08:02 -070099
Winson Chungf0ea4d32011-06-06 14:27:16 -0700100 @Override
101 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
102 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
103 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
104
105 // Set the width of the tab list to the content width
106 if (remeasureTabWidth) {
107 mTabs.getLayoutParams().width = mAppsCustomizePane.getPageContentWidth();
108 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
109 }
Winson Chung4179b4e2011-05-31 17:28:12 -0700110 }
111
112 @Override
113 public boolean onTouchEvent(MotionEvent event) {
114 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
115 // through to the workspace and trigger showWorkspace()
116 if (event.getY() < mAppsCustomizePane.getBottom()) {
117 return true;
118 }
119 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700120 }
121
122 @Override
123 public void onTabChanged(String tabId) {
Winson Chung4179b4e2011-05-31 17:28:12 -0700124 mAppsCustomizePane.setContentType(getContentTypeForTabTag(tabId));
Winson Chung785d2eb2011-04-14 16:08:02 -0700125 }
126
127 /**
128 * Returns the content type for the specified tab tag.
129 */
130 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
131 if (tag.equals(APPS_TAB_TAG)) {
132 return AppsCustomizePagedView.ContentType.Applications;
133 } else if (tag.equals(WIDGETS_TAB_TAG)) {
134 return AppsCustomizePagedView.ContentType.Widgets;
135 }
136 return AppsCustomizePagedView.ContentType.Applications;
137 }
138
139 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700140 * Returns the tab tag for a given content type.
141 */
142 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
143 if (type == AppsCustomizePagedView.ContentType.Applications) {
144 return APPS_TAB_TAG;
145 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
146 return WIDGETS_TAB_TAG;
147 }
148 return APPS_TAB_TAG;
149 }
150
151 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700152 * Disable focus on anything under this view in the hierarchy if we are not visible.
153 */
154 @Override
155 public int getDescendantFocusability() {
156 if (getVisibility() != View.VISIBLE) {
157 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
158 }
159 return super.getDescendantFocusability();
160 }
161
162 /* LauncherTransitionable overrides */
163 @Override
Winson Chungf0ea4d32011-06-06 14:27:16 -0700164 public void onLauncherTransitionStart(Animator animation) {
165 if (animation != null) {
166 // Turn on hardware layers for performance
167 setLayerType(LAYER_TYPE_HARDWARE, null);
168
169 // force building the layer at the beginning of the animation, so you don't get a
170 // blip early in the animation
171 buildLayer();
172 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700173 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700174
Winson Chung785d2eb2011-04-14 16:08:02 -0700175 @Override
Winson Chungf0ea4d32011-06-06 14:27:16 -0700176 public void onLauncherTransitionEnd(Animator animation) {
177 if (animation != null) {
178 setLayerType(LAYER_TYPE_NONE, null);
179 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700180 }
181}