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; |
Jason Monk | ed05f09 | 2014-04-24 10:13:05 -0400 | [diff] [blame] | 32 | import android.view.accessibility.AccessibilityManager; |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 33 | import android.widget.FrameLayout; |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 34 | import android.widget.LinearLayout; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 35 | import android.widget.TabHost; |
Winson Chung | faa1325 | 2011-06-13 18:15:54 -0700 | [diff] [blame] | 36 | import android.widget.TabWidget; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 37 | import android.widget.TextView; |
| 38 | |
Michael Jurka | 141dbd0 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 39 | import java.util.ArrayList; |
| 40 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 41 | public class AppsCustomizeTabHost extends FrameLayout implements LauncherTransitionable, 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 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 47 | private AppsCustomizePagedView mPagedView; |
| 48 | private View mContent; |
| 49 | private boolean mInTransition = false; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 50 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 51 | private final Rect mInsets = new Rect(); |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 52 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 53 | public AppsCustomizeTabHost(Context context, AttributeSet attrs) { |
| 54 | super(context, attrs); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 57 | /** |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 58 | * Convenience methods to select specific tabs. We want to set the content type immediately |
| 59 | * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view |
| 60 | * reflects the new content (but doesn't do the animation and logic associated with changing |
| 61 | * tabs manually). |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 62 | */ |
Winson Chung | c93e5ae | 2012-07-23 20:48:26 -0700 | [diff] [blame] | 63 | void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 64 | mPagedView.setContentType(type); |
| 65 | } |
| 66 | |
| 67 | public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) { |
| 68 | setContentTypeImmediate(type); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 69 | } |
Adam Cohen | 947dc54 | 2013-06-06 22:43:33 -0700 | [diff] [blame] | 70 | |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 71 | @Override |
| 72 | public void setInsets(Rect insets) { |
| 73 | mInsets.set(insets); |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 74 | LayoutParams flp = (LayoutParams) mContent.getLayoutParams(); |
John Spurlock | 77e1f47 | 2013-09-11 10:09:51 -0400 | [diff] [blame] | 75 | flp.topMargin = insets.top; |
| 76 | flp.bottomMargin = insets.bottom; |
| 77 | flp.leftMargin = insets.left; |
| 78 | flp.rightMargin = insets.right; |
| 79 | mContent.setLayoutParams(flp); |
| 80 | } |
| 81 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Setup the tab host and create all necessary tabs. |
| 84 | */ |
| 85 | @Override |
| 86 | protected void onFinishInflate() { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 87 | mPagedView = (AppsCustomizePagedView) findViewById(R.id.apps_customize_pane_content); |
| 88 | mContent = findViewById(R.id.content); |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 89 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 90 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 91 | public String getContentTag() { |
| 92 | return getTabTagForContentType(mPagedView.getContentType()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the content type for the specified tab tag. |
| 97 | */ |
| 98 | public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) { |
| 99 | if (tag.equals(APPS_TAB_TAG)) { |
| 100 | return AppsCustomizePagedView.ContentType.Applications; |
| 101 | } else if (tag.equals(WIDGETS_TAB_TAG)) { |
| 102 | return AppsCustomizePagedView.ContentType.Widgets; |
| 103 | } |
| 104 | return AppsCustomizePagedView.ContentType.Applications; |
| 105 | } |
| 106 | |
| 107 | /** |
Winson Chung | fc79c80 | 2011-05-02 13:35:34 -0700 | [diff] [blame] | 108 | * Returns the tab tag for a given content type. |
| 109 | */ |
| 110 | public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) { |
| 111 | if (type == AppsCustomizePagedView.ContentType.Applications) { |
| 112 | return APPS_TAB_TAG; |
| 113 | } else if (type == AppsCustomizePagedView.ContentType.Widgets) { |
| 114 | return WIDGETS_TAB_TAG; |
| 115 | } |
| 116 | return APPS_TAB_TAG; |
| 117 | } |
| 118 | |
| 119 | /** |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 120 | * Disable focus on anything under this view in the hierarchy if we are not visible. |
| 121 | */ |
| 122 | @Override |
| 123 | public int getDescendantFocusability() { |
| 124 | if (getVisibility() != View.VISIBLE) { |
| 125 | return ViewGroup.FOCUS_BLOCK_DESCENDANTS; |
| 126 | } |
| 127 | return super.getDescendantFocusability(); |
| 128 | } |
| 129 | |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 130 | void reset() { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 131 | // Reset immediately |
| 132 | mPagedView.reset(); |
| 133 | } |
| 134 | |
| 135 | public void onWindowVisible() { |
| 136 | if (getVisibility() == VISIBLE) { |
| 137 | mContent.setVisibility(VISIBLE); |
| 138 | // We unload the widget previews when the UI is hidden, so need to reload pages |
| 139 | // Load the current page synchronously, and the neighboring pages asynchronously |
| 140 | mPagedView.loadAssociatedPages(mPagedView.getCurrentPage(), true); |
| 141 | mPagedView.loadAssociatedPages(mPagedView.getCurrentPage()); |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 145 | public void onTrimMemory() { |
| 146 | mContent.setVisibility(GONE); |
| 147 | // Clear the widget pages of all their subviews - this will trigger the widget previews |
| 148 | // to delete their bitmaps |
| 149 | mPagedView.clearAllWidgetPages(); |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 152 | @Override |
| 153 | public View getContent() { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 154 | return mPagedView; |
| 155 | } |
| 156 | |
| 157 | public boolean isInTransition() { |
| 158 | return mInTransition; |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /* LauncherTransitionable overrides */ |
| 162 | @Override |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 163 | public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 164 | mPagedView.onLauncherTransitionPrepare(l, animated, toWorkspace); |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 165 | mInTransition = true; |
Michael Jurka | 1899a36 | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 166 | |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 167 | if (toWorkspace) { |
| 168 | // Going from All Apps -> Workspace |
| 169 | setVisibilityOfSiblingsWithLowerZOrder(VISIBLE); |
| 170 | } else { |
| 171 | // Going from Workspace -> All Apps |
| 172 | mContent.setVisibility(VISIBLE); |
Michael Jurka | 3d845e8 | 2011-11-15 17:04:31 -0800 | [diff] [blame] | 173 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 174 | // Make sure the current page is loaded (we start loading the side pages after the |
| 175 | // transition to prevent slowing down the animation) |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 176 | // TODO: revisit this |
| 177 | mPagedView.loadAssociatedPages(mPagedView.getCurrentPage(), true); |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 178 | } |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 179 | } |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 180 | |
Michael Jurka | a35e35a | 2012-04-26 15:04:28 -0700 | [diff] [blame] | 181 | @Override |
| 182 | public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 183 | mPagedView.onLauncherTransitionStart(l, animated, toWorkspace); |
| 184 | if (animated && !Utilities.isLmp()) { |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 185 | enableAndBuildHardwareLayer(); |
| 186 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 187 | } |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 188 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 189 | @Override |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 190 | public void onLauncherTransitionStep(Launcher l, float t) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 191 | mPagedView.onLauncherTransitionStep(l, t); |
Winson Chung | 7044272 | 2012-02-10 15:43:22 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 195 | public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) { |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 196 | mPagedView.onLauncherTransitionEnd(l, animated, toWorkspace); |
Winson Chung | c100e8e | 2011-08-09 16:02:43 -0700 | [diff] [blame] | 197 | mInTransition = false; |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 198 | if (animated && !Utilities.isLmp()) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 199 | setLayerType(LAYER_TYPE_NONE, null); |
| 200 | } |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 201 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 202 | if (!toWorkspace) { |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 203 | // Make sure adjacent pages are loaded (we wait until after the transition to |
| 204 | // prevent slowing down the animation) |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 205 | mPagedView.loadAssociatedPages(mPagedView.getCurrentPage()); |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 206 | |
Jason Monk | ed05f09 | 2014-04-24 10:13:05 -0400 | [diff] [blame] | 207 | // Opening apps, need to announce what page we are on. |
| 208 | AccessibilityManager am = (AccessibilityManager) |
| 209 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 210 | if (am.isEnabled()) { |
| 211 | // Notify the user when the page changes |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 212 | announceForAccessibility(mPagedView.getCurrentPageDescription()); |
Jason Monk | ed05f09 | 2014-04-24 10:13:05 -0400 | [diff] [blame] | 213 | } |
| 214 | |
Winson Chung | 7819abd | 2012-11-29 14:29:38 -0800 | [diff] [blame] | 215 | // Going from Workspace -> All Apps |
| 216 | // NOTE: We should do this at the end since we check visibility state in some of the |
| 217 | // cling initialization/dismiss code above. |
| 218 | setVisibilityOfSiblingsWithLowerZOrder(INVISIBLE); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 219 | } |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 220 | } |
Winson Chung | 70d7210 | 2011-08-12 11:24:35 -0700 | [diff] [blame] | 221 | |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 222 | private void setVisibilityOfSiblingsWithLowerZOrder(int visibility) { |
| 223 | ViewGroup parent = (ViewGroup) getParent(); |
Michael Jurka | aea1ce5 | 2012-04-12 11:23:21 -0700 | [diff] [blame] | 224 | if (parent == null) return; |
| 225 | |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 226 | View overviewPanel = ((Launcher) getContext()).getOverviewPanel(); |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 227 | final int count = parent.getChildCount(); |
| 228 | if (!isChildrenDrawingOrderEnabled()) { |
| 229 | for (int i = 0; i < count; i++) { |
| 230 | final View child = parent.getChildAt(i); |
| 231 | if (child == this) { |
| 232 | break; |
| 233 | } else { |
Adam Cohen | 3d41198 | 2013-09-24 17:02:06 -0700 | [diff] [blame] | 234 | if (child.getVisibility() == GONE || child == overviewPanel) { |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 235 | continue; |
| 236 | } |
| 237 | child.setVisibility(visibility); |
| 238 | } |
| 239 | } |
| 240 | } else { |
| 241 | throw new RuntimeException("Failed; can't get z-order of views"); |
| 242 | } |
| 243 | } |
| 244 | |
Adam Cohen | 6c5891a | 2014-07-09 23:53:15 -0700 | [diff] [blame] | 245 | private void enableAndBuildHardwareLayer() { |
| 246 | // isHardwareAccelerated() checks if we're attached to a window and if that |
| 247 | // window is HW accelerated-- we were sometimes not attached to a window |
| 248 | // and buildLayer was throwing an IllegalStateException |
| 249 | if (isHardwareAccelerated()) { |
| 250 | // Turn on hardware layers for performance |
| 251 | setLayerType(LAYER_TYPE_HARDWARE, null); |
| 252 | |
| 253 | // force building the layer, so you don't get a blip early in an animation |
| 254 | // when the layer is created layer |
| 255 | buildLayer(); |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 259 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 260 | } |