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 | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame^] | 20 | import android.animation.AnimatorListenerAdapter; |
| 21 | import android.animation.ObjectAnimator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.res.Resources; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.view.LayoutInflater; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 26 | import android.view.MotionEvent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame^] | 29 | import android.view.ViewPropertyAnimator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 30 | import android.widget.TabHost; |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 31 | import android.widget.TabWidget; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 32 | import android.widget.TextView; |
| 33 | |
| 34 | import com.android.launcher.R; |
| 35 | |
| 36 | public 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 Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 44 | private ViewGroup mTabs; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 45 | private AppsCustomizePagedView mAppsCustomizePane; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 46 | |
| 47 | public AppsCustomizeTabHost(Context context, AttributeSet attrs) { |
| 48 | super(context, attrs); |
| 49 | mLayoutInflater = LayoutInflater.from(context); |
| 50 | } |
| 51 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Convenience methods to select specific tabs |
| 54 | */ |
Winson Chung | 55b6550 | 2011-05-26 12:03:43 -0700 | [diff] [blame] | 55 | void selectAppsTab() { |
| 56 | setCurrentTabByTag(APPS_TAB_TAG); |
| 57 | } |
| 58 | void selectWidgetsTab() { |
| 59 | setCurrentTabByTag(WIDGETS_TAB_TAG); |
| 60 | } |
| 61 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 62 | /** |
| 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 Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 70 | final TabWidget tabs = (TabWidget) findViewById(com.android.internal.R.id.tabs); |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 71 | final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView) |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 72 | findViewById(R.id.apps_customize_pane_content); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 73 | mTabs = tabs; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 74 | mAppsCustomizePane = appsCustomizePane; |
| 75 | if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 76 | |
| 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 Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 81 | return appsCustomizePane; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 82 | } |
| 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 Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 90 | 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 Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 93 | setOnTabChangedListener(this); |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 94 | |
| 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 Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 101 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 102 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 103 | @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 Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 113 | } |
| 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 Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onTabChanged(String tabId) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame^] | 127 | 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 Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 147 | } |
| 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 Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 162 | * 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 Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 174 | * 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 Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 186 | 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 Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 195 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 196 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 197 | @Override |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 198 | public void onLauncherTransitionEnd(Animator animation) { |
| 199 | if (animation != null) { |
| 200 | setLayerType(LAYER_TYPE_NONE, null); |
| 201 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 202 | } |
| 203 | } |