blob: a2717126d1f71071af0d90d01fb29b30e1203a17 [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
30 private static final String APPS_TAB_TAG = "APPS";
31 private static final String WIDGETS_TAB_TAG = "WIDGETS";
32
Adam Cohen6c5891a2014-07-09 23:53:15 -070033 private AppsCustomizePagedView mPagedView;
34 private View mContent;
35 private boolean mInTransition = false;
Winson Chung785d2eb2011-04-14 16:08:02 -070036
John Spurlock77e1f472013-09-11 10:09:51 -040037 private final Rect mInsets = new Rect();
Winson Chungc100e8e2011-08-09 16:02:43 -070038
Winson Chung785d2eb2011-04-14 16:08:02 -070039 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
40 super(context, attrs);
Winson Chung785d2eb2011-04-14 16:08:02 -070041 }
42
Winson Chungf0ea4d32011-06-06 14:27:16 -070043 /**
Winson Chung5a808352011-06-27 19:08:49 -070044 * 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 Chungf0ea4d32011-06-06 14:27:16 -070048 */
Winson Chungc93e5ae2012-07-23 20:48:26 -070049 void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) {
Adam Cohen6c5891a2014-07-09 23:53:15 -070050 mPagedView.setContentType(type);
51 }
52
53 public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) {
54 setContentTypeImmediate(type);
Winson Chung5a808352011-06-27 19:08:49 -070055 }
Adam Cohen947dc542013-06-06 22:43:33 -070056
John Spurlock77e1f472013-09-11 10:09:51 -040057 @Override
58 public void setInsets(Rect insets) {
59 mInsets.set(insets);
Adam Cohen6c5891a2014-07-09 23:53:15 -070060 LayoutParams flp = (LayoutParams) mContent.getLayoutParams();
John Spurlock77e1f472013-09-11 10:09:51 -040061 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 Chung785d2eb2011-04-14 16:08:02 -070068 /**
69 * Setup the tab host and create all necessary tabs.
70 */
71 @Override
72 protected void onFinishInflate() {
Adam Cohen6c5891a2014-07-09 23:53:15 -070073 mPagedView = (AppsCustomizePagedView) findViewById(R.id.apps_customize_pane_content);
74 mContent = findViewById(R.id.content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070075 }
Winson Chung785d2eb2011-04-14 16:08:02 -070076
Adam Cohen6c5891a2014-07-09 23:53:15 -070077 public String getContentTag() {
78 return getTabTagForContentType(mPagedView.getContentType());
Winson Chung785d2eb2011-04-14 16:08:02 -070079 }
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 Chungfc79c802011-05-02 13:35:34 -070094 * 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 Chung785d2eb2011-04-14 16:08:02 -0700106 * 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 Chungc100e8e2011-08-09 16:02:43 -0700116 void reset() {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700117 // Reset immediately
118 mPagedView.reset();
119 }
120
Adam Cohenc8f4e1b2014-11-19 16:03:20 -0800121 void trimMemory() {
122 mPagedView.trimMemory();
123 }
124
Adam Cohen6c5891a2014-07-09 23:53:15 -0700125 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 Chungc100e8e2011-08-09 16:02:43 -0700132 }
133 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -0800134 @Override
Adam Cohenc956cba2014-07-24 09:17:37 -0700135 public ViewGroup getContent() {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700136 return mPagedView;
137 }
138
139 public boolean isInTransition() {
140 return mInTransition;
Michael Jurka1899a362011-11-03 13:50:45 -0700141 }
142
143 /* LauncherTransitionable overrides */
144 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700145 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700146 mPagedView.onLauncherTransitionPrepare(l, animated, toWorkspace);
Michael Jurka1899a362011-11-03 13:50:45 -0700147 mInTransition = true;
Michael Jurka1899a362011-11-03 13:50:45 -0700148
Michael Jurkabed61d22012-02-14 22:51:29 -0800149 if (toWorkspace) {
150 // Going from All Apps -> Workspace
151 setVisibilityOfSiblingsWithLowerZOrder(VISIBLE);
152 } else {
153 // Going from Workspace -> All Apps
154 mContent.setVisibility(VISIBLE);
Michael Jurka3d845e82011-11-15 17:04:31 -0800155
Michael Jurkae326f182011-11-21 14:05:46 -0800156 // Make sure the current page is loaded (we start loading the side pages after the
157 // transition to prevent slowing down the animation)
Adam Cohen6c5891a2014-07-09 23:53:15 -0700158 // TODO: revisit this
Adam Cohen63f1ec02014-08-12 09:23:13 -0700159 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage());
Winson Chungc100e8e2011-08-09 16:02:43 -0700160 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700161 }
Michael Jurkabed61d22012-02-14 22:51:29 -0800162
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700163 @Override
164 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700165 mPagedView.onLauncherTransitionStart(l, animated, toWorkspace);
Winson Chung785d2eb2011-04-14 16:08:02 -0700166 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700167
Winson Chung785d2eb2011-04-14 16:08:02 -0700168 @Override
Winson Chung70442722012-02-10 15:43:22 -0800169 public void onLauncherTransitionStep(Launcher l, float t) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700170 mPagedView.onLauncherTransitionStep(l, t);
Winson Chung70442722012-02-10 15:43:22 -0800171 }
172
173 @Override
Michael Jurkabed61d22012-02-14 22:51:29 -0800174 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700175 mPagedView.onLauncherTransitionEnd(l, animated, toWorkspace);
Winson Chungc100e8e2011-08-09 16:02:43 -0700176 mInTransition = false;
Winson Chung3ac74c52011-06-30 17:39:37 -0700177
Winson Chung7d7541e2011-09-16 20:14:36 -0700178 if (!toWorkspace) {
Michael Jurkae326f182011-11-21 14:05:46 -0800179 // Make sure adjacent pages are loaded (we wait until after the transition to
180 // prevent slowing down the animation)
Adam Cohen6c5891a2014-07-09 23:53:15 -0700181 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage());
Winson Chung7d7541e2011-09-16 20:14:36 -0700182
Jason Monked05f092014-04-24 10:13:05 -0400183 // 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 Cohen6c5891a2014-07-09 23:53:15 -0700188 announceForAccessibility(mPagedView.getCurrentPageDescription());
Jason Monked05f092014-04-24 10:13:05 -0400189 }
190
Winson Chung7819abd2012-11-29 14:29:38 -0800191 // 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 Chung5a808352011-06-27 19:08:49 -0700195 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700196 }
Winson Chung70d72102011-08-12 11:24:35 -0700197
Michael Jurkabed61d22012-02-14 22:51:29 -0800198 private void setVisibilityOfSiblingsWithLowerZOrder(int visibility) {
199 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaaea1ce52012-04-12 11:23:21 -0700200 if (parent == null) return;
201
Adam Cohen3d411982013-09-24 17:02:06 -0700202 View overviewPanel = ((Launcher) getContext()).getOverviewPanel();
Michael Jurkabed61d22012-02-14 22:51:29 -0800203 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 Cohen3d411982013-09-24 17:02:06 -0700210 if (child.getVisibility() == GONE || child == overviewPanel) {
Michael Jurkabed61d22012-02-14 22:51:29 -0800211 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 Chung785d2eb2011-04-14 16:08:02 -0700220}