blob: 69e3ac17fe147b916b04d5c1fa7aea65aa8623a1 [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
121 public void onWindowVisible() {
122 if (getVisibility() == VISIBLE) {
123 mContent.setVisibility(VISIBLE);
124 // We unload the widget previews when the UI is hidden, so need to reload pages
125 // Load the current page synchronously, and the neighboring pages asynchronously
126 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage(), true);
127 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage());
Winson Chungc100e8e2011-08-09 16:02:43 -0700128 }
129 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -0800130 @Override
Adam Cohenc956cba2014-07-24 09:17:37 -0700131 public ViewGroup getContent() {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700132 return mPagedView;
133 }
134
135 public boolean isInTransition() {
136 return mInTransition;
Michael Jurka1899a362011-11-03 13:50:45 -0700137 }
138
139 /* LauncherTransitionable overrides */
140 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700141 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700142 mPagedView.onLauncherTransitionPrepare(l, animated, toWorkspace);
Michael Jurka1899a362011-11-03 13:50:45 -0700143 mInTransition = true;
Michael Jurka1899a362011-11-03 13:50:45 -0700144
Michael Jurkabed61d22012-02-14 22:51:29 -0800145 if (toWorkspace) {
146 // Going from All Apps -> Workspace
147 setVisibilityOfSiblingsWithLowerZOrder(VISIBLE);
148 } else {
149 // Going from Workspace -> All Apps
150 mContent.setVisibility(VISIBLE);
Michael Jurka3d845e82011-11-15 17:04:31 -0800151
Michael Jurkae326f182011-11-21 14:05:46 -0800152 // Make sure the current page is loaded (we start loading the side pages after the
153 // transition to prevent slowing down the animation)
Adam Cohen6c5891a2014-07-09 23:53:15 -0700154 // TODO: revisit this
Adam Cohen63f1ec02014-08-12 09:23:13 -0700155 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage());
Winson Chungc100e8e2011-08-09 16:02:43 -0700156 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700157 }
Michael Jurkabed61d22012-02-14 22:51:29 -0800158
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700159 @Override
160 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700161 mPagedView.onLauncherTransitionStart(l, animated, toWorkspace);
Winson Chung785d2eb2011-04-14 16:08:02 -0700162 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700163
Winson Chung785d2eb2011-04-14 16:08:02 -0700164 @Override
Winson Chung70442722012-02-10 15:43:22 -0800165 public void onLauncherTransitionStep(Launcher l, float t) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700166 mPagedView.onLauncherTransitionStep(l, t);
Winson Chung70442722012-02-10 15:43:22 -0800167 }
168
169 @Override
Michael Jurkabed61d22012-02-14 22:51:29 -0800170 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700171 mPagedView.onLauncherTransitionEnd(l, animated, toWorkspace);
Winson Chungc100e8e2011-08-09 16:02:43 -0700172 mInTransition = false;
Winson Chung3ac74c52011-06-30 17:39:37 -0700173
Winson Chung7d7541e2011-09-16 20:14:36 -0700174 if (!toWorkspace) {
Michael Jurkae326f182011-11-21 14:05:46 -0800175 // Make sure adjacent pages are loaded (we wait until after the transition to
176 // prevent slowing down the animation)
Adam Cohen6c5891a2014-07-09 23:53:15 -0700177 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage());
Winson Chung7d7541e2011-09-16 20:14:36 -0700178
Jason Monked05f092014-04-24 10:13:05 -0400179 // Opening apps, need to announce what page we are on.
180 AccessibilityManager am = (AccessibilityManager)
181 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
182 if (am.isEnabled()) {
183 // Notify the user when the page changes
Adam Cohen6c5891a2014-07-09 23:53:15 -0700184 announceForAccessibility(mPagedView.getCurrentPageDescription());
Jason Monked05f092014-04-24 10:13:05 -0400185 }
186
Winson Chung7819abd2012-11-29 14:29:38 -0800187 // Going from Workspace -> All Apps
188 // NOTE: We should do this at the end since we check visibility state in some of the
189 // cling initialization/dismiss code above.
190 setVisibilityOfSiblingsWithLowerZOrder(INVISIBLE);
Winson Chung5a808352011-06-27 19:08:49 -0700191 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700192 }
Winson Chung70d72102011-08-12 11:24:35 -0700193
Michael Jurkabed61d22012-02-14 22:51:29 -0800194 private void setVisibilityOfSiblingsWithLowerZOrder(int visibility) {
195 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaaea1ce52012-04-12 11:23:21 -0700196 if (parent == null) return;
197
Adam Cohen3d411982013-09-24 17:02:06 -0700198 View overviewPanel = ((Launcher) getContext()).getOverviewPanel();
Michael Jurkabed61d22012-02-14 22:51:29 -0800199 final int count = parent.getChildCount();
200 if (!isChildrenDrawingOrderEnabled()) {
201 for (int i = 0; i < count; i++) {
202 final View child = parent.getChildAt(i);
203 if (child == this) {
204 break;
205 } else {
Adam Cohen3d411982013-09-24 17:02:06 -0700206 if (child.getVisibility() == GONE || child == overviewPanel) {
Michael Jurkabed61d22012-02-14 22:51:29 -0800207 continue;
208 }
209 child.setVisibility(visibility);
210 }
211 }
212 } else {
213 throw new RuntimeException("Failed; can't get z-order of views");
214 }
215 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700216}