Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.Resources; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.LayoutInflater; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 24 | import android.view.MotionEvent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.view.ViewGroup; |
| 27 | import android.widget.TabHost; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 28 | import android.widget.TextView; |
| 29 | |
| 30 | import com.android.launcher.R; |
| 31 | |
| 32 | public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable, |
| 33 | TabHost.OnTabChangeListener { |
| 34 | static final String LOG_TAG = "AppsCustomizeTabHost"; |
| 35 | |
| 36 | private static final String APPS_TAB_TAG = "APPS"; |
| 37 | private static final String WIDGETS_TAB_TAG = "WIDGETS"; |
| 38 | |
| 39 | private final LayoutInflater mLayoutInflater; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 40 | private ViewGroup mTabs; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 41 | private AppsCustomizePagedView mAppsCustomizePane; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 42 | |
| 43 | public AppsCustomizeTabHost(Context context, AttributeSet attrs) { |
| 44 | super(context, attrs); |
| 45 | mLayoutInflater = LayoutInflater.from(context); |
| 46 | } |
| 47 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Convenience methods to select specific tabs |
| 50 | */ |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 51 | void selectAppsTab() { |
| 52 | setCurrentTabByTag(APPS_TAB_TAG); |
| 53 | } |
| 54 | void selectWidgetsTab() { |
| 55 | setCurrentTabByTag(WIDGETS_TAB_TAG); |
| 56 | } |
| 57 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Setup the tab host and create all necessary tabs. |
| 60 | */ |
| 61 | @Override |
| 62 | protected void onFinishInflate() { |
| 63 | // Setup the tab host |
| 64 | setup(); |
| 65 | |
| 66 | final ViewGroup tabs = (ViewGroup) findViewById(com.android.internal.R.id.tabs); |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 67 | final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView) |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 68 | findViewById(R.id.apps_customize_pane_content); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 69 | mTabs = tabs; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 70 | mAppsCustomizePane = appsCustomizePane; |
| 71 | if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 72 | |
| 73 | // Configure the tabs content factory to return the same paged view (that we change the |
| 74 | // content filter on) |
| 75 | TabContentFactory contentFactory = new TabContentFactory() { |
| 76 | public View createTabContent(String tag) { |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 77 | return appsCustomizePane; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 78 | } |
| 79 | }; |
| 80 | |
| 81 | // Create the tabs |
| 82 | TextView tabView; |
| 83 | tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false); |
| 84 | tabView.setText(mContext.getString(R.string.all_apps_button_label)); |
| 85 | addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory)); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 86 | tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false); |
| 87 | tabView.setText(mContext.getString(R.string.widgets_tab_label)); |
| 88 | addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory)); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 89 | setOnTabChangedListener(this); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 90 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 91 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 92 | @Override |
| 93 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 94 | boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0); |
| 95 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 96 | |
| 97 | // Set the width of the tab list to the content width |
| 98 | if (remeasureTabWidth) { |
| 99 | mTabs.getLayoutParams().width = mAppsCustomizePane.getPageContentWidth(); |
| 100 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 101 | } |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean onTouchEvent(MotionEvent event) { |
| 106 | // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall |
| 107 | // through to the workspace and trigger showWorkspace() |
| 108 | if (event.getY() < mAppsCustomizePane.getBottom()) { |
| 109 | return true; |
| 110 | } |
| 111 | return super.onTouchEvent(event); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void onTabChanged(String tabId) { |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 116 | mAppsCustomizePane.setContentType(getContentTypeForTabTag(tabId)); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns the content type for the specified tab tag. |
| 121 | */ |
| 122 | public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) { |
| 123 | if (tag.equals(APPS_TAB_TAG)) { |
| 124 | return AppsCustomizePagedView.ContentType.Applications; |
| 125 | } else if (tag.equals(WIDGETS_TAB_TAG)) { |
| 126 | return AppsCustomizePagedView.ContentType.Widgets; |
| 127 | } |
| 128 | return AppsCustomizePagedView.ContentType.Applications; |
| 129 | } |
| 130 | |
| 131 | /** |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 132 | * Returns the tab tag for a given content type. |
| 133 | */ |
| 134 | public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) { |
| 135 | if (type == AppsCustomizePagedView.ContentType.Applications) { |
| 136 | return APPS_TAB_TAG; |
| 137 | } else if (type == AppsCustomizePagedView.ContentType.Widgets) { |
| 138 | return WIDGETS_TAB_TAG; |
| 139 | } |
| 140 | return APPS_TAB_TAG; |
| 141 | } |
| 142 | |
| 143 | /** |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 144 | * Disable focus on anything under this view in the hierarchy if we are not visible. |
| 145 | */ |
| 146 | @Override |
| 147 | public int getDescendantFocusability() { |
| 148 | if (getVisibility() != View.VISIBLE) { |
| 149 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 150 | } |
| 151 | return super.getDescendantFocusability(); |
| 152 | } |
| 153 | |
| 154 | /* LauncherTransitionable overrides */ |
| 155 | @Override |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 156 | public void onLauncherTransitionStart(Animator animation) { |
| 157 | if (animation != null) { |
| 158 | // Turn on hardware layers for performance |
| 159 | setLayerType(LAYER_TYPE_HARDWARE, null); |
| 160 | |
| 161 | // force building the layer at the beginning of the animation, so you don't get a |
| 162 | // blip early in the animation |
| 163 | buildLayer(); |
| 164 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 165 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 166 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 167 | @Override |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 168 | public void onLauncherTransitionEnd(Animator animation) { |
| 169 | if (animation != null) { |
| 170 | setLayerType(LAYER_TYPE_NONE, null); |
| 171 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 172 | } |
| 173 | } |