AppsCustomizeTabHost.java revision 4179b4e048dd019daca053dc3a7eddf6c464b08b
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 17package com.android.launcher2; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.util.AttributeSet; 22import android.view.LayoutInflater; 23import android.view.MotionEvent; 24import android.view.View; 25import android.view.ViewGroup; 26import android.widget.TabHost; 27import android.widget.TextView; 28 29import com.android.launcher.R; 30 31public 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"; 37 private static final String WALLPAPERS_TAB_TAG = "WALLPAPERS"; 38 39 private final LayoutInflater mLayoutInflater; 40 private AppsCustomizePagedView mAppsCustomizePane; 41 42 public AppsCustomizeTabHost(Context context, AttributeSet attrs) { 43 super(context, attrs); 44 mLayoutInflater = LayoutInflater.from(context); 45 } 46 47 void selectAppsTab() { 48 setCurrentTabByTag(APPS_TAB_TAG); 49 } 50 void selectWidgetsTab() { 51 setCurrentTabByTag(WIDGETS_TAB_TAG); 52 } 53 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); 63 final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView) 64 findViewById(R.id.apps_customize_pane_content); 65 mAppsCustomizePane = appsCustomizePane; 66 if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException(); 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) { 72 return appsCustomizePane; 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)); 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)); 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)); 87 setOnTabChangedListener(this); 88 89 // Set the width of the tab bar to match the content (for now) 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); 101 } 102 103 @Override 104 public void onTabChanged(String tabId) { 105 mAppsCustomizePane.setContentType(getContentTypeForTabTag(tabId)); 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; 116 } else if (tag.equals(WALLPAPERS_TAB_TAG)) { 117 return AppsCustomizePagedView.ContentType.Wallpapers; 118 } 119 return AppsCustomizePagedView.ContentType.Applications; 120 } 121 122 /** 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; 130 } else if (type == AppsCustomizePagedView.ContentType.Wallpapers) { 131 return WALLPAPERS_TAB_TAG; 132 } 133 return APPS_TAB_TAG; 134 } 135 136 /** 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} 157