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