blob: 5e2f05c613e969a07fa695cf580cf328b735580b [file] [log] [blame]
Winson Chung785d2eb2011-04-14 16:08:02 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung785d2eb2011-04-14 16:08:02 -070018
19import android.content.Context;
John Spurlock77e1f472013-09-11 10:09:51 -040020import android.graphics.Rect;
Winson Chung785d2eb2011-04-14 16:08:02 -070021import android.util.AttributeSet;
Winson Chung785d2eb2011-04-14 16:08:02 -070022import android.view.View;
23import android.view.ViewGroup;
Jason Monked05f092014-04-24 10:13:05 -040024import android.view.accessibility.AccessibilityManager;
Michael Jurka141dbd02011-11-03 13:50:45 -070025import android.widget.FrameLayout;
Michael Jurka141dbd02011-11-03 13:50:45 -070026
Adam Cohen6c5891a2014-07-09 23:53:15 -070027public class AppsCustomizeTabHost extends FrameLayout implements LauncherTransitionable, Insettable {
Winson Chung785d2eb2011-04-14 16:08:02 -070028 static final String LOG_TAG = "AppsCustomizeTabHost";
29
Winson Chung785d2eb2011-04-14 16:08:02 -070030 private static final String WIDGETS_TAB_TAG = "WIDGETS";
31
Adam Cohen6c5891a2014-07-09 23:53:15 -070032 private AppsCustomizePagedView mPagedView;
33 private View mContent;
34 private boolean mInTransition = false;
Winson Chung785d2eb2011-04-14 16:08:02 -070035
John Spurlock77e1f472013-09-11 10:09:51 -040036 private final Rect mInsets = new Rect();
Winson Chungc100e8e2011-08-09 16:02:43 -070037
Winson Chung785d2eb2011-04-14 16:08:02 -070038 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
39 super(context, attrs);
Winson Chung785d2eb2011-04-14 16:08:02 -070040 }
41
Winson Chungf0ea4d32011-06-06 14:27:16 -070042 /**
Winson Chung5a808352011-06-27 19:08:49 -070043 * Convenience methods to select specific tabs. We want to set the content type immediately
44 * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view
45 * reflects the new content (but doesn't do the animation and logic associated with changing
46 * tabs manually).
Winson Chungf0ea4d32011-06-06 14:27:16 -070047 */
Winson Chungc93e5ae2012-07-23 20:48:26 -070048 void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) {
Adam Cohen6c5891a2014-07-09 23:53:15 -070049 mPagedView.setContentType(type);
50 }
51
John Spurlock77e1f472013-09-11 10:09:51 -040052 @Override
53 public void setInsets(Rect insets) {
54 mInsets.set(insets);
Adam Cohen6c5891a2014-07-09 23:53:15 -070055 LayoutParams flp = (LayoutParams) mContent.getLayoutParams();
John Spurlock77e1f472013-09-11 10:09:51 -040056 flp.topMargin = insets.top;
57 flp.bottomMargin = insets.bottom;
58 flp.leftMargin = insets.left;
59 flp.rightMargin = insets.right;
60 mContent.setLayoutParams(flp);
61 }
62
Winson Chung785d2eb2011-04-14 16:08:02 -070063 /**
64 * Setup the tab host and create all necessary tabs.
65 */
66 @Override
67 protected void onFinishInflate() {
Adam Cohen6c5891a2014-07-09 23:53:15 -070068 mPagedView = (AppsCustomizePagedView) findViewById(R.id.apps_customize_pane_content);
69 mContent = findViewById(R.id.content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070070 }
Winson Chung785d2eb2011-04-14 16:08:02 -070071
Adam Cohen6c5891a2014-07-09 23:53:15 -070072 public String getContentTag() {
73 return getTabTagForContentType(mPagedView.getContentType());
Winson Chung785d2eb2011-04-14 16:08:02 -070074 }
75
76 /**
Winson Chungb745afb2015-03-02 11:51:23 -080077 * Returns the content view used for the launcher transitions.
78 */
79 public View getContentView() {
80 return findViewById(R.id.apps_customize_pane_content);
81 }
82
83 /**
84 * Returns the reveal view used for the launcher transitions.
85 */
86 public View getRevealView() {
87 return findViewById(R.id.fake_page);
88 }
89
90 /**
91 * Returns the page indicators view.
92 */
93 public View getPageIndicators() {
94 return findViewById(R.id.apps_customize_page_indicator);
95 }
96
97 /**
Winson Chung785d2eb2011-04-14 16:08:02 -070098 * Returns the content type for the specified tab tag.
99 */
100 public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
Winson Chungb745afb2015-03-02 11:51:23 -0800101 return AppsCustomizePagedView.ContentType.Widgets;
Winson Chung785d2eb2011-04-14 16:08:02 -0700102 }
103
104 /**
Winson Chungfc79c802011-05-02 13:35:34 -0700105 * Returns the tab tag for a given content type.
106 */
107 public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
Winson Chungb745afb2015-03-02 11:51:23 -0800108 return WIDGETS_TAB_TAG;
Winson Chungfc79c802011-05-02 13:35:34 -0700109 }
110
111 /**
Winson Chung785d2eb2011-04-14 16:08:02 -0700112 * Disable focus on anything under this view in the hierarchy if we are not visible.
113 */
114 @Override
115 public int getDescendantFocusability() {
116 if (getVisibility() != View.VISIBLE) {
117 return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
118 }
119 return super.getDescendantFocusability();
120 }
121
Winson Chungc100e8e2011-08-09 16:02:43 -0700122 void reset() {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700123 // Reset immediately
124 mPagedView.reset();
125 }
126
Adam Cohenc8f4e1b2014-11-19 16:03:20 -0800127 void trimMemory() {
128 mPagedView.trimMemory();
129 }
130
Adam Cohen6c5891a2014-07-09 23:53:15 -0700131 public void onWindowVisible() {
132 if (getVisibility() == VISIBLE) {
133 mContent.setVisibility(VISIBLE);
134 // We unload the widget previews when the UI is hidden, so need to reload pages
135 // Load the current page synchronously, and the neighboring pages asynchronously
136 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage(), true);
137 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage());
Winson Chungc100e8e2011-08-09 16:02:43 -0700138 }
139 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -0800140 @Override
Adam Cohenc956cba2014-07-24 09:17:37 -0700141 public ViewGroup getContent() {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700142 return mPagedView;
143 }
144
145 public boolean isInTransition() {
146 return mInTransition;
Michael Jurka1899a362011-11-03 13:50:45 -0700147 }
148
149 /* LauncherTransitionable overrides */
150 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700151 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700152 mPagedView.onLauncherTransitionPrepare(l, animated, toWorkspace);
Michael Jurka1899a362011-11-03 13:50:45 -0700153 mInTransition = true;
Michael Jurka1899a362011-11-03 13:50:45 -0700154
Michael Jurkabed61d22012-02-14 22:51:29 -0800155 if (toWorkspace) {
156 // Going from All Apps -> Workspace
157 setVisibilityOfSiblingsWithLowerZOrder(VISIBLE);
158 } else {
159 // Going from Workspace -> All Apps
160 mContent.setVisibility(VISIBLE);
Michael Jurka3d845e82011-11-15 17:04:31 -0800161
Michael Jurkae326f182011-11-21 14:05:46 -0800162 // Make sure the current page is loaded (we start loading the side pages after the
163 // transition to prevent slowing down the animation)
Adam Cohen6c5891a2014-07-09 23:53:15 -0700164 // TODO: revisit this
Adam Cohen63f1ec02014-08-12 09:23:13 -0700165 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage());
Winson Chungc100e8e2011-08-09 16:02:43 -0700166 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700167 }
Michael Jurkabed61d22012-02-14 22:51:29 -0800168
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700169 @Override
170 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700171 mPagedView.onLauncherTransitionStart(l, animated, toWorkspace);
Winson Chung785d2eb2011-04-14 16:08:02 -0700172 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700173
Winson Chung785d2eb2011-04-14 16:08:02 -0700174 @Override
Winson Chung70442722012-02-10 15:43:22 -0800175 public void onLauncherTransitionStep(Launcher l, float t) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700176 mPagedView.onLauncherTransitionStep(l, t);
Winson Chung70442722012-02-10 15:43:22 -0800177 }
178
179 @Override
Michael Jurkabed61d22012-02-14 22:51:29 -0800180 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700181 mPagedView.onLauncherTransitionEnd(l, animated, toWorkspace);
Winson Chungc100e8e2011-08-09 16:02:43 -0700182 mInTransition = false;
Winson Chung3ac74c52011-06-30 17:39:37 -0700183
Winson Chung7d7541e2011-09-16 20:14:36 -0700184 if (!toWorkspace) {
Michael Jurkae326f182011-11-21 14:05:46 -0800185 // Make sure adjacent pages are loaded (we wait until after the transition to
186 // prevent slowing down the animation)
Adam Cohen6c5891a2014-07-09 23:53:15 -0700187 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage());
Winson Chung7d7541e2011-09-16 20:14:36 -0700188
Jason Monked05f092014-04-24 10:13:05 -0400189 // Opening apps, need to announce what page we are on.
190 AccessibilityManager am = (AccessibilityManager)
191 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
192 if (am.isEnabled()) {
193 // Notify the user when the page changes
Adam Cohen6c5891a2014-07-09 23:53:15 -0700194 announceForAccessibility(mPagedView.getCurrentPageDescription());
Jason Monked05f092014-04-24 10:13:05 -0400195 }
196
Winson Chung7819abd2012-11-29 14:29:38 -0800197 // Going from Workspace -> All Apps
198 // NOTE: We should do this at the end since we check visibility state in some of the
199 // cling initialization/dismiss code above.
200 setVisibilityOfSiblingsWithLowerZOrder(INVISIBLE);
Winson Chung5a808352011-06-27 19:08:49 -0700201 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700202 }
Winson Chung70d72102011-08-12 11:24:35 -0700203
Michael Jurkabed61d22012-02-14 22:51:29 -0800204 private void setVisibilityOfSiblingsWithLowerZOrder(int visibility) {
205 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaaea1ce52012-04-12 11:23:21 -0700206 if (parent == null) return;
207
Winson Chungb745afb2015-03-02 11:51:23 -0800208 View appsView = ((Launcher) getContext()).getAppsView();
Adam Cohen3d411982013-09-24 17:02:06 -0700209 View overviewPanel = ((Launcher) getContext()).getOverviewPanel();
Michael Jurkabed61d22012-02-14 22:51:29 -0800210 final int count = parent.getChildCount();
211 if (!isChildrenDrawingOrderEnabled()) {
212 for (int i = 0; i < count; i++) {
213 final View child = parent.getChildAt(i);
214 if (child == this) {
215 break;
216 } else {
Winson Chungb745afb2015-03-02 11:51:23 -0800217 if (child.getVisibility() == GONE || child == overviewPanel ||
218 child == appsView) {
Michael Jurkabed61d22012-02-14 22:51:29 -0800219 continue;
220 }
221 child.setVisibility(visibility);
222 }
223 }
224 } else {
225 throw new RuntimeException("Failed; can't get z-order of views");
226 }
227 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700228}