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