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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 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; |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorSet; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.res.Resources; |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 25 | import android.graphics.Color; |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 26 | import android.graphics.Rect; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 27 | import android.util.AttributeSet; |
| 28 | import android.view.LayoutInflater; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 29 | import android.view.MotionEvent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 30 | import android.view.View; |
| 31 | import android.view.ViewGroup; |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 33 | import android.widget.LinearLayout; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 34 | import android.widget.TabHost; |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 35 | import android.widget.TabWidget; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 36 | import android.widget.TextView; |
| 37 | |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
| 39 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 40 | public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable, |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 41 | TabHost.OnTabChangeListener, Insettable { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 42 | static final String LOG_TAG = "AppsCustomizeTabHost"; |
| 43 | |
| 44 | private static final String APPS_TAB_TAG = "APPS"; |
| 45 | private static final String WIDGETS_TAB_TAG = "WIDGETS"; |
| 46 | |
| 47 | private final LayoutInflater mLayoutInflater; |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 48 | private ViewGroup mTabs; |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 49 | private ViewGroup mTabsContainer; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 50 | private AppsCustomizePagedView mAppsCustomizePane; |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 51 | private FrameLayout mAnimationBuffer; |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 52 | private LinearLayout mContent; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 53 | |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 54 | private boolean mInTransition; |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 55 | private boolean mTransitioningToWorkspace; |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 56 | private boolean mResetAfterTransition; |
Michael Jurka | 6cfafb9 | 2012-02-23 18:25:43 -0800 | [diff] [blame] | 57 | private Runnable mRelayoutAndMakeVisible; |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 58 | private final Rect mInsets = new Rect(); |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 59 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 60 | public AppsCustomizeTabHost(Context context, AttributeSet attrs) { |
| 61 | super(context, attrs); |
| 62 | mLayoutInflater = LayoutInflater.from(context); |
Michael Jurka | 6cfafb9 | 2012-02-23 18:25:43 -0800 | [diff] [blame] | 63 | mRelayoutAndMakeVisible = new Runnable() { |
| 64 | public void run() { |
| 65 | mTabs.requestLayout(); |
| 66 | mTabsContainer.setAlpha(1f); |
| 67 | } |
| 68 | }; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 71 | /** |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 72 | * Convenience methods to select specific tabs. We want to set the content type immediately |
| 73 | * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view |
| 74 | * reflects the new content (but doesn't do the animation and logic associated with changing |
| 75 | * tabs manually). |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 76 | */ |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 77 | void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) { |
Winson Chung | fa4086d | 2012-07-25 16:18:49 -0700 | [diff] [blame] | 78 | setOnTabChangedListener(null); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 79 | onTabChangedStart(); |
| 80 | onTabChangedEnd(type); |
Winson Chung | fa4086d | 2012-07-25 16:18:49 -0700 | [diff] [blame] | 81 | setCurrentTabByTag(getTabTagForContentType(type)); |
| 82 | setOnTabChangedListener(this); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 83 | } |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 84 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 85 | @Override |
| 86 | public void setInsets(Rect insets) { |
| 87 | mInsets.set(insets); |
| 88 | FrameLayout.LayoutParams flp = (LayoutParams) mContent.getLayoutParams(); |
| 89 | flp.topMargin = insets.top; |
| 90 | flp.bottomMargin = insets.bottom; |
| 91 | flp.leftMargin = insets.left; |
| 92 | flp.rightMargin = insets.right; |
| 93 | mContent.setLayoutParams(flp); |
| 94 | } |
| 95 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Setup the tab host and create all necessary tabs. |
| 98 | */ |
| 99 | @Override |
| 100 | protected void onFinishInflate() { |
| 101 | // Setup the tab host |
| 102 | setup(); |
| 103 | |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 104 | final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 105 | final TabWidget tabs = getTabWidget(); |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 106 | final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView) |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 107 | findViewById(R.id.apps_customize_pane_content); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 108 | mTabs = tabs; |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 109 | mTabsContainer = tabsContainer; |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 110 | mAppsCustomizePane = appsCustomizePane; |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 111 | mAnimationBuffer = (FrameLayout) findViewById(R.id.animation_buffer); |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 112 | mContent = (LinearLayout) findViewById(R.id.apps_customize_content); |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 113 | if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException(); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 114 | |
| 115 | // Configure the tabs content factory to return the same paged view (that we change the |
| 116 | // content filter on) |
| 117 | TabContentFactory contentFactory = new TabContentFactory() { |
| 118 | public View createTabContent(String tag) { |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 119 | return appsCustomizePane; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 120 | } |
| 121 | }; |
| 122 | |
| 123 | // Create the tabs |
| 124 | TextView tabView; |
Winson Chung | d2c1f80 | 2011-10-14 12:26:54 -0700 | [diff] [blame] | 125 | String label; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 126 | label = getContext().getString(R.string.all_apps_button_label); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 127 | tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false); |
Winson Chung | d2c1f80 | 2011-10-14 12:26:54 -0700 | [diff] [blame] | 128 | tabView.setText(label); |
| 129 | tabView.setContentDescription(label); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 130 | addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory)); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 131 | label = getContext().getString(R.string.widgets_tab_label); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 132 | tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false); |
Winson Chung | d2c1f80 | 2011-10-14 12:26:54 -0700 | [diff] [blame] | 133 | tabView.setText(label); |
| 134 | tabView.setContentDescription(label); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 135 | addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory)); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 136 | setOnTabChangedListener(this); |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 137 | |
| 138 | // Setup the key listener to jump between the last tab view and the market icon |
| 139 | AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener(); |
| 140 | View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1); |
| 141 | lastTab.setOnKeyListener(keyListener); |
| 142 | View shopButton = findViewById(R.id.market_button); |
| 143 | shopButton.setOnKeyListener(keyListener); |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 144 | |
| 145 | // Hide the tab bar until we measure |
| 146 | mTabsContainer.setAlpha(0f); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 147 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 148 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 149 | @Override |
| 150 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 151 | boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0); |
| 152 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 153 | |
| 154 | // Set the width of the tab list to the content width |
| 155 | if (remeasureTabWidth) { |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 156 | int contentWidth = mAppsCustomizePane.getPageContentWidth(); |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 157 | if (contentWidth > 0 && mTabs.getLayoutParams().width != contentWidth) { |
| 158 | // Set the width and show the tab bar |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 159 | mTabs.getLayoutParams().width = contentWidth; |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 160 | mRelayoutAndMakeVisible.run(); |
Winson Chung | fd3385f | 2011-06-15 19:51:24 -0700 | [diff] [blame] | 161 | } |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 162 | |
| 163 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 164 | } |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 167 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Winson Chung | ac08f4b | 2012-04-27 15:41:00 -0700 | [diff] [blame] | 168 | // If we are mid transitioning to the workspace, then intercept touch events here so we |
| 169 | // can ignore them, otherwise we just let all apps handle the touch events. |
| 170 | if (mInTransition && mTransitioningToWorkspace) { |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | return super.onInterceptTouchEvent(ev); |
| 174 | }; |
| 175 | |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 176 | @Override |
| 177 | public boolean onTouchEvent(MotionEvent event) { |
Winson Chung | ac08f4b | 2012-04-27 15:41:00 -0700 | [diff] [blame] | 178 | // Allow touch events to fall through to the workspace if we are transitioning there |
| 179 | if (mInTransition && mTransitioningToWorkspace) { |
Winson Chung | 21fadea | 2012-04-27 15:59:55 -0700 | [diff] [blame] | 180 | return super.onTouchEvent(event); |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Winson Chung | 4179b4e | 2011-05-31 17:28:12 -0700 | [diff] [blame] | 183 | // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall |
| 184 | // through to the workspace and trigger showWorkspace() |
| 185 | if (event.getY() < mAppsCustomizePane.getBottom()) { |
| 186 | return true; |
| 187 | } |
| 188 | return super.onTouchEvent(event); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 191 | private void onTabChangedStart() { |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 192 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 193 | |
Winson Chung | 097e6c7 | 2011-10-20 13:58:30 -0700 | [diff] [blame] | 194 | private void reloadCurrentPage() { |
Winson Chung | 097e6c7 | 2011-10-20 13:58:30 -0700 | [diff] [blame] | 195 | mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage()); |
| 196 | mAppsCustomizePane.requestFocus(); |
| 197 | } |
| 198 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 199 | private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) { |
Winson Chung | 2d75f12 | 2013-09-23 16:53:31 -0700 | [diff] [blame] | 200 | int bgAlpha = (int) (255 * (getResources().getInteger( |
| 201 | R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f)); |
| 202 | setBackgroundColor(Color.argb(bgAlpha, 0, 0, 0)); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 203 | mAppsCustomizePane.setContentType(type); |
| 204 | } |
| 205 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 206 | @Override |
| 207 | public void onTabChanged(String tabId) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 208 | final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 209 | |
| 210 | // Animate the changing of the tab content by fading pages in and out |
| 211 | final Resources res = getResources(); |
| 212 | final int duration = res.getInteger(R.integer.config_tabTransitionDuration); |
| 213 | |
| 214 | // We post a runnable here because there is a delay while the first page is loading and |
| 215 | // the feedback from having changed the tab almost feels better than having it stick |
| 216 | post(new Runnable() { |
| 217 | @Override |
| 218 | public void run() { |
Winson Chung | 097e6c7 | 2011-10-20 13:58:30 -0700 | [diff] [blame] | 219 | if (mAppsCustomizePane.getMeasuredWidth() <= 0 || |
| 220 | mAppsCustomizePane.getMeasuredHeight() <= 0) { |
| 221 | reloadCurrentPage(); |
| 222 | return; |
| 223 | } |
| 224 | |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 225 | // Take the visible pages and re-parent them temporarily to mAnimatorBuffer |
| 226 | // and then cross fade to the new pages |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 227 | int[] visiblePageRange = new int[2]; |
| 228 | mAppsCustomizePane.getVisiblePages(visiblePageRange); |
| 229 | if (visiblePageRange[0] == -1 && visiblePageRange[1] == -1) { |
| 230 | // If we can't get the visible page ranges, then just skip the animation |
| 231 | reloadCurrentPage(); |
| 232 | return; |
| 233 | } |
| 234 | ArrayList<View> visiblePages = new ArrayList<View>(); |
| 235 | for (int i = visiblePageRange[0]; i <= visiblePageRange[1]; i++) { |
| 236 | visiblePages.add(mAppsCustomizePane.getPageAt(i)); |
| 237 | } |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 238 | |
| 239 | // We want the pages to be rendered in exactly the same way as they were when |
| 240 | // their parent was mAppsCustomizePane -- so set the scroll on mAnimationBuffer |
| 241 | // to be exactly the same as mAppsCustomizePane, and below, set the left/top |
| 242 | // parameters to be correct for each of the pages |
| 243 | mAnimationBuffer.scrollTo(mAppsCustomizePane.getScrollX(), 0); |
| 244 | |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 245 | // mAppsCustomizePane renders its children in reverse order, so |
| 246 | // add the pages to mAnimationBuffer in reverse order to match that behavior |
| 247 | for (int i = visiblePages.size() - 1; i >= 0; i--) { |
| 248 | View child = visiblePages.get(i); |
Winson Chung | c58497e | 2013-09-03 17:48:37 -0700 | [diff] [blame] | 249 | if (child instanceof AppsCustomizeCellLayout) { |
| 250 | ((AppsCustomizeCellLayout) child).resetChildrenOnKeyListeners(); |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 251 | } else if (child instanceof PagedViewGridLayout) { |
| 252 | ((PagedViewGridLayout) child).resetChildrenOnKeyListeners(); |
| 253 | } |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 254 | PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(false); |
| 255 | mAppsCustomizePane.removeView(child); |
| 256 | PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(true); |
| 257 | mAnimationBuffer.setAlpha(1f); |
| 258 | mAnimationBuffer.setVisibility(View.VISIBLE); |
Winson Chung | 6e3cf90 | 2012-03-30 16:36:53 -0700 | [diff] [blame] | 259 | LayoutParams p = new FrameLayout.LayoutParams(child.getMeasuredWidth(), |
| 260 | child.getMeasuredHeight()); |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 261 | p.setMargins((int) child.getLeft(), (int) child.getTop(), 0, 0); |
| 262 | mAnimationBuffer.addView(child, p); |
| 263 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 264 | |
| 265 | // Toggle the new content |
| 266 | onTabChangedStart(); |
| 267 | onTabChangedEnd(type); |
| 268 | |
| 269 | // Animate the transition |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 270 | ObjectAnimator outAnim = LauncherAnimUtils.ofFloat(mAnimationBuffer, "alpha", 0f); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 271 | outAnim.addListener(new AnimatorListenerAdapter() { |
Michael Jurka | ee8e99f | 2013-02-07 13:27:06 +0100 | [diff] [blame] | 272 | private void clearAnimationBuffer() { |
| 273 | mAnimationBuffer.setVisibility(View.GONE); |
| 274 | PagedViewWidget.setRecyclePreviewsWhenDetachedFromWindow(false); |
| 275 | mAnimationBuffer.removeAllViews(); |
| 276 | PagedViewWidget.setRecyclePreviewsWhenDetachedFromWindow(true); |
| 277 | } |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 278 | @Override |
| 279 | public void onAnimationEnd(Animator animation) { |
Michael Jurka | ee8e99f | 2013-02-07 13:27:06 +0100 | [diff] [blame] | 280 | clearAnimationBuffer(); |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 281 | } |
| 282 | @Override |
| 283 | public void onAnimationCancel(Animator animation) { |
Michael Jurka | ee8e99f | 2013-02-07 13:27:06 +0100 | [diff] [blame] | 284 | clearAnimationBuffer(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 285 | } |
| 286 | }); |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 287 | ObjectAnimator inAnim = LauncherAnimUtils.ofFloat(mAppsCustomizePane, "alpha", 1f); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 288 | inAnim.addListener(new AnimatorListenerAdapter() { |
| 289 | @Override |
| 290 | public void onAnimationEnd(Animator animation) { |
Winson Chung | 097e6c7 | 2011-10-20 13:58:30 -0700 | [diff] [blame] | 291 | reloadCurrentPage(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 292 | } |
| 293 | }); |
Michael Jurka | 7f9ddd8 | 2012-11-19 16:15:24 -0800 | [diff] [blame] | 294 | |
| 295 | final AnimatorSet animSet = LauncherAnimUtils.createAnimatorSet(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 296 | animSet.playTogether(outAnim, inAnim); |
| 297 | animSet.setDuration(duration); |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 298 | animSet.start(); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 299 | } |
| 300 | }); |
| 301 | } |
| 302 | |
| 303 | public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) { |
Winson Chung | fa4086d | 2012-07-25 16:18:49 -0700 | [diff] [blame] | 304 | setOnTabChangedListener(null); |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 305 | setCurrentTabByTag(getTabTagForContentType(type)); |
Winson Chung | fa4086d | 2012-07-25 16:18:49 -0700 | [diff] [blame] | 306 | setOnTabChangedListener(this); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Returns the content type for the specified tab tag. |
| 311 | */ |
| 312 | public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) { |
| 313 | if (tag.equals(APPS_TAB_TAG)) { |
| 314 | return AppsCustomizePagedView.ContentType.Applications; |
| 315 | } else if (tag.equals(WIDGETS_TAB_TAG)) { |
| 316 | return AppsCustomizePagedView.ContentType.Widgets; |
| 317 | } |
| 318 | return AppsCustomizePagedView.ContentType.Applications; |
| 319 | } |
| 320 | |
| 321 | /** |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 322 | * Returns the tab tag for a given content type. |
| 323 | */ |
| 324 | public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) { |
| 325 | if (type == AppsCustomizePagedView.ContentType.Applications) { |
| 326 | return APPS_TAB_TAG; |
| 327 | } else if (type == AppsCustomizePagedView.ContentType.Widgets) { |
| 328 | return WIDGETS_TAB_TAG; |
| 329 | } |
| 330 | return APPS_TAB_TAG; |
| 331 | } |
| 332 | |
| 333 | /** |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 334 | * Disable focus on anything under this view in the hierarchy if we are not visible. |
| 335 | */ |
| 336 | @Override |
| 337 | public int getDescendantFocusability() { |
| 338 | if (getVisibility() != View.VISIBLE) { |
| 339 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 340 | } |
| 341 | return super.getDescendantFocusability(); |
| 342 | } |
| 343 | |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 344 | void reset() { |
| 345 | if (mInTransition) { |
| 346 | // Defer to after the transition to reset |
| 347 | mResetAfterTransition = true; |
| 348 | } else { |
| 349 | // Reset immediately |
| 350 | mAppsCustomizePane.reset(); |
| 351 | } |
| 352 | } |
| 353 | |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 354 | private void enableAndBuildHardwareLayer() { |
Michael Jurka | 19e0fc5 | 2011-07-22 18:00:21 -0700 | [diff] [blame] | 355 | // isHardwareAccelerated() checks if we're attached to a window and if that |
| 356 | // window is HW accelerated-- we were sometimes not attached to a window |
| 357 | // and buildLayer was throwing an IllegalStateException |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 358 | if (isHardwareAccelerated()) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 359 | // Turn on hardware layers for performance |
| 360 | setLayerType(LAYER_TYPE_HARDWARE, null); |
| 361 | |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 362 | // force building the layer, so you don't get a blip early in an animation |
| 363 | // when the layer is created layer |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 364 | buildLayer(); |
| 365 | } |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 368 | @Override |
| 369 | public View getContent() { |
Winson Chung | 7bb3752 | 2013-10-28 11:07:57 -0700 | [diff] [blame] | 370 | View appsCustomizeContent = mAppsCustomizePane.getContent(); |
| 371 | if (appsCustomizeContent != null) { |
| 372 | return appsCustomizeContent; |
| 373 | } |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 374 | return mContent; |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /* LauncherTransitionable overrides */ |
| 378 | @Override |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 379 | public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) { |
| 380 | mAppsCustomizePane.onLauncherTransitionPrepare(l, animated, toWorkspace); |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 381 | mInTransition = true; |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 382 | mTransitioningToWorkspace = toWorkspace; |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 383 | |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 384 | if (toWorkspace) { |
| 385 | // Going from All Apps -> Workspace |
| 386 | setVisibilityOfSiblingsWithLowerZOrder(VISIBLE); |
| 387 | } else { |
| 388 | // Going from Workspace -> All Apps |
| 389 | mContent.setVisibility(VISIBLE); |
Michael Jurka | 3d845e8 | 2011-11-15 17:04:31 -0800 | [diff] [blame] | 390 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 391 | // Make sure the current page is loaded (we start loading the side pages after the |
| 392 | // transition to prevent slowing down the animation) |
| 393 | mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage(), true); |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 396 | if (mResetAfterTransition) { |
| 397 | mAppsCustomizePane.reset(); |
| 398 | mResetAfterTransition = false; |
| 399 | } |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 400 | } |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 401 | |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 402 | @Override |
| 403 | public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) { |
Winson Chung | 7bb3752 | 2013-10-28 11:07:57 -0700 | [diff] [blame] | 404 | mAppsCustomizePane.onLauncherTransitionStart(l, animated, toWorkspace); |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 405 | if (animated) { |
| 406 | enableAndBuildHardwareLayer(); |
| 407 | } |
Winson Chung | af40f20 | 2013-09-18 18:26:31 -0700 | [diff] [blame] | 408 | |
| 409 | // Dismiss the workspace cling |
Winson Chung | a694524 | 2014-01-08 14:04:34 -0800 | [diff] [blame^] | 410 | l.getLauncherClings().dismissWorkspaceCling(null); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 411 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 412 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 413 | @Override |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 414 | public void onLauncherTransitionStep(Launcher l, float t) { |
Winson Chung | 7bb3752 | 2013-10-28 11:07:57 -0700 | [diff] [blame] | 415 | mAppsCustomizePane.onLauncherTransitionStep(l, t); |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | @Override |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 419 | public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) { |
Michael Jurka | 39e5d17 | 2012-03-12 18:36:12 -0700 | [diff] [blame] | 420 | mAppsCustomizePane.onLauncherTransitionEnd(l, animated, toWorkspace); |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 421 | mInTransition = false; |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 422 | if (animated) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 423 | setLayerType(LAYER_TYPE_NONE, null); |
| 424 | } |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 425 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 426 | if (!toWorkspace) { |
Winson Chung | 7819abd | 2012-11-29 14:29:38 -0800 | [diff] [blame] | 427 | // Show the all apps cling (if not already shown) |
Winson Chung | 3f4e142 | 2011-11-17 14:58:51 -0800 | [diff] [blame] | 428 | mAppsCustomizePane.showAllAppsCling(); |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 429 | // Make sure adjacent pages are loaded (we wait until after the transition to |
| 430 | // prevent slowing down the animation) |
| 431 | mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage()); |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 432 | |
Winson Chung | 7819abd | 2012-11-29 14:29:38 -0800 | [diff] [blame] | 433 | // Going from Workspace -> All Apps |
| 434 | // NOTE: We should do this at the end since we check visibility state in some of the |
| 435 | // cling initialization/dismiss code above. |
| 436 | setVisibilityOfSiblingsWithLowerZOrder(INVISIBLE); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 437 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 438 | } |
Winson Chung | 70d7210 | 2011-08-12 11:24:35 -0700 | [diff] [blame] | 439 | |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 440 | private void setVisibilityOfSiblingsWithLowerZOrder(int visibility) { |
| 441 | ViewGroup parent = (ViewGroup) getParent(); |
Michael Jurka | aea1ce5 | 2012-04-12 11:23:21 -0700 | [diff] [blame] | 442 | if (parent == null) return; |
| 443 | |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 444 | View overviewPanel = ((Launcher) getContext()).getOverviewPanel(); |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 445 | final int count = parent.getChildCount(); |
| 446 | if (!isChildrenDrawingOrderEnabled()) { |
| 447 | for (int i = 0; i < count; i++) { |
| 448 | final View child = parent.getChildAt(i); |
| 449 | if (child == this) { |
| 450 | break; |
| 451 | } else { |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 452 | if (child.getVisibility() == GONE || child == overviewPanel) { |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 453 | continue; |
| 454 | } |
| 455 | child.setVisibility(visibility); |
| 456 | } |
| 457 | } |
| 458 | } else { |
| 459 | throw new RuntimeException("Failed; can't get z-order of views"); |
| 460 | } |
| 461 | } |
| 462 | |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 463 | public void onWindowVisible() { |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 464 | if (getVisibility() == VISIBLE) { |
| 465 | mContent.setVisibility(VISIBLE); |
| 466 | // We unload the widget previews when the UI is hidden, so need to reload pages |
| 467 | // Load the current page synchronously, and the neighboring pages asynchronously |
| 468 | mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage(), true); |
| 469 | mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage()); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | public void onTrimMemory() { |
| 474 | mContent.setVisibility(GONE); |
| 475 | // Clear the widget pages of all their subviews - this will trigger the widget previews |
| 476 | // to delete their bitmaps |
| 477 | mAppsCustomizePane.clearAllWidgetPages(); |
| 478 | } |
| 479 | |
Winson Chung | 70d7210 | 2011-08-12 11:24:35 -0700 | [diff] [blame] | 480 | boolean isTransitioning() { |
| 481 | return mInTransition; |
| 482 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 483 | } |