blob: 87c154b472dd0ebeacd6071a9c6d5835a9939e50 [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 Chungb44b5242011-06-13 11:32:14 -070020import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.content.Context;
23import android.content.res.Resources;
24import android.util.AttributeSet;
25import android.view.LayoutInflater;
Winson Chung4179b4e2011-05-31 17:28:12 -070026import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070027import android.view.View;
28import android.view.ViewGroup;
Winson Chungb44b5242011-06-13 11:32:14 -070029import android.view.ViewPropertyAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.widget.TabHost;
Winson Chungfaa13252011-06-13 18:15:54 -070031import android.widget.TabWidget;
Winson Chung785d2eb2011-04-14 16:08:02 -070032import android.widget.TextView;
33
34import com.android.launcher.R;
35
36public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable,
37 TabHost.OnTabChangeListener {
38 static final String LOG_TAG = "AppsCustomizeTabHost";
39
40 private static final String APPS_TAB_TAG = "APPS";
41 private static final String WIDGETS_TAB_TAG = "WIDGETS";
42
43 private final LayoutInflater mLayoutInflater;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044 private ViewGroup mTabs;
Winson Chung4179b4e2011-05-31 17:28:12 -070045 private AppsCustomizePagedView mAppsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070046
47 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
48 super(context, attrs);
49 mLayoutInflater = LayoutInflater.from(context);
50 }
51
Winson Chungf0ea4d32011-06-06 14:27:16 -070052 /**
53 * Convenience methods to select specific tabs
54 */
Winson Chung55b65502011-05-26 12:03:43 -070055 void selectAppsTab() {
56 setCurrentTabByTag(APPS_TAB_TAG);
57 }
58 void selectWidgetsTab() {
59 setCurrentTabByTag(WIDGETS_TAB_TAG);
60 }
61
Winson Chung785d2eb2011-04-14 16:08:02 -070062 /**
63 * Setup the tab host and create all necessary tabs.
64 */
65 @Override
66 protected void onFinishInflate() {
67 // Setup the tab host
68 setup();
69
Winson Chungfaa13252011-06-13 18:15:54 -070070 final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs);
Winson Chung4179b4e2011-05-31 17:28:12 -070071 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
Winson Chung785d2eb2011-04-14 16:08:02 -070072 findViewById(R.id.apps_customize_pane_content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070073 mTabs = tabs;
Winson Chung4179b4e2011-05-31 17:28:12 -070074 mAppsCustomizePane = appsCustomizePane;
75 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
Winson Chung785d2eb2011-04-14 16:08:02 -070076
77 // Configure the tabs content factory to return the same paged view (that we change the
78 // content filter on)
79 TabContentFactory contentFactory = new TabContentFactory() {
80 public View createTabContent(String tag) {
Winson Chung4179b4e2011-05-31 17:28:12 -070081 return appsCustomizePane;
Winson Chung785d2eb2011-04-14 16:08:02 -070082 }
83 };
84
85 // Create the tabs
86 TextView tabView;
87 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
88 tabView.setText(mContext.getString(R.string.all_apps_button_label));
89 addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -070090 tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
91 tabView.setText(mContext.getString(R.string.widgets_tab_label));
92 addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
Winson Chung785d2eb2011-04-14 16:08:02 -070093 setOnTabChangedListener(this);
Winson Chungfaa13252011-06-13 18:15:54 -070094
95 // Setup the key listener to jump between the last tab view and the market icon
96 AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
97 View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
98 lastTab.setOnKeyListener(keyListener);
99 View shopButton = findViewById(R.id.market_button);
100 shopButton.setOnKeyListener(keyListener);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700101 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700102
Winson Chungf0ea4d32011-06-06 14:27:16 -0700103 @Override
104 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
105 boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
106 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
107
108 // Set the width of the tab list to the content width
109 if (remeasureTabWidth) {
110 mTabs.getLayoutParams().width = mAppsCustomizePane.getPageContentWidth();
111 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
112 }
Winson Chung4179b4e2011-05-31 17:28:12 -0700113 }
114
115 @Override
116 public boolean onTouchEvent(MotionEvent event) {
117 // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
118 // through to the workspace and trigger showWorkspace()
119 if (event.getY() < mAppsCustomizePane.getBottom()) {
120 return true;
121 }
122 return super.onTouchEvent(event);
Winson Chung785d2eb2011-04-14 16:08:02 -0700123 }
124
125 @Override
126 public void onTabChanged(String tabId) {
Winson Chungb44b5242011-06-13 11:32:14 -0700127 final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId);
128 if (!mAppsCustomizePane.isContentType(type)) {
129 // Animate the changing of the tab content by fading pages in and out
130 final Resources res = getResources();
131 final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
132
133 ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 0f);
134 anim.setDuration(duration);
135 anim.addListener(new AnimatorListenerAdapter() {
136 @Override
137 public void onAnimationEnd(android.animation.Animator animation) {
138 mAppsCustomizePane.setContentType(type);
139
140 ObjectAnimator anim = ObjectAnimator.ofFloat(mAppsCustomizePane, "alpha", 1f);
141 anim.setDuration(duration);
142 anim.start();
143 }
144 });
145 anim.start();
146 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700147 }
148
149 /**
150 * Returns the content type for the specified tab tag.
151 */
152 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
153 if (tag.equals(APPS_TAB_TAG)) {
154 return AppsCustomizePagedView.ContentType.Applications;
155 } else if (tag.equals(WIDGETS_TAB_TAG)) {
156 return AppsCustomizePagedView.ContentType.Widgets;
157 }
158 return AppsCustomizePagedView.ContentType.Applications;
159 }
160
161 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700162 * Returns the tab tag for a given content type.
163 */
164 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
165 if (type == AppsCustomizePagedView.ContentType.Applications) {
166 return APPS_TAB_TAG;
167 } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
168 return WIDGETS_TAB_TAG;
169 }
170 return APPS_TAB_TAG;
171 }
172
173 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700174 * Disable focus on anything under this view in the hierarchy if we are not visible.
175 */
176 @Override
177 public int getDescendantFocusability() {
178 if (getVisibility() != View.VISIBLE) {
179 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
180 }
181 return super.getDescendantFocusability();
182 }
183
184 /* LauncherTransitionable overrides */
185 @Override
Winson Chungf0ea4d32011-06-06 14:27:16 -0700186 public void onLauncherTransitionStart(Animator animation) {
187 if (animation != null) {
188 // Turn on hardware layers for performance
189 setLayerType(LAYER_TYPE_HARDWARE, null);
190
191 // force building the layer at the beginning of the animation, so you don't get a
192 // blip early in the animation
193 buildLayer();
194 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700195 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700196
Winson Chung785d2eb2011-04-14 16:08:02 -0700197 @Override
Winson Chungf0ea4d32011-06-06 14:27:16 -0700198 public void onLauncherTransitionEnd(Animator animation) {
199 if (animation != null) {
200 setLayerType(LAYER_TYPE_NONE, null);
201 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700202 }
203}