blob: 9a516fd4115ea62e7296944f55e234bff19a4691 [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
Winson Chungf0ea4d32011-06-06 14:27:16 -070019import android.animation.Animator;
Winson Chungb44b5242011-06-13 11:32:14 -070020import android.animation.AnimatorListenerAdapter;
Winson Chungf314b0e2011-08-16 11:54:27 -070021import android.animation.AnimatorSet;
Winson Chungb44b5242011-06-13 11:32:14 -070022import android.animation.ObjectAnimator;
Winson Chung785d2eb2011-04-14 16:08:02 -070023import android.content.Context;
24import android.content.res.Resources;
Winson Chung2d75f122013-09-23 16:53:31 -070025import android.graphics.Color;
John Spurlock77e1f472013-09-11 10:09:51 -040026import android.graphics.Rect;
Winson Chung785d2eb2011-04-14 16:08:02 -070027import android.util.AttributeSet;
28import android.view.LayoutInflater;
Winson Chung4179b4e2011-05-31 17:28:12 -070029import android.view.MotionEvent;
Winson Chung785d2eb2011-04-14 16:08:02 -070030import android.view.View;
31import android.view.ViewGroup;
Jason Monked05f092014-04-24 10:13:05 -040032import android.view.accessibility.AccessibilityManager;
Michael Jurka141dbd02011-11-03 13:50:45 -070033import android.widget.FrameLayout;
Michael Jurka1899a362011-11-03 13:50:45 -070034import android.widget.LinearLayout;
Winson Chung785d2eb2011-04-14 16:08:02 -070035import android.widget.TabHost;
Winson Chungfaa13252011-06-13 18:15:54 -070036import android.widget.TabWidget;
Winson Chung785d2eb2011-04-14 16:08:02 -070037import android.widget.TextView;
38
Michael Jurka141dbd02011-11-03 13:50:45 -070039import java.util.ArrayList;
40
Adam Cohen6c5891a2014-07-09 23:53:15 -070041public class AppsCustomizeTabHost extends FrameLayout implements LauncherTransitionable, Insettable {
Winson Chung785d2eb2011-04-14 16:08:02 -070042 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 Cohen6c5891a2014-07-09 23:53:15 -070047 private AppsCustomizePagedView mPagedView;
48 private View mContent;
49 private boolean mInTransition = false;
Winson Chung785d2eb2011-04-14 16:08:02 -070050
John Spurlock77e1f472013-09-11 10:09:51 -040051 private final Rect mInsets = new Rect();
Winson Chungc100e8e2011-08-09 16:02:43 -070052
Winson Chung785d2eb2011-04-14 16:08:02 -070053 public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
54 super(context, attrs);
Winson Chung785d2eb2011-04-14 16:08:02 -070055 }
56
Winson Chungf0ea4d32011-06-06 14:27:16 -070057 /**
Winson Chung5a808352011-06-27 19:08:49 -070058 * 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 Chungf0ea4d32011-06-06 14:27:16 -070062 */
Winson Chungc93e5ae2012-07-23 20:48:26 -070063 void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) {
Adam Cohen6c5891a2014-07-09 23:53:15 -070064 mPagedView.setContentType(type);
65 }
66
67 public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) {
68 setContentTypeImmediate(type);
Winson Chung5a808352011-06-27 19:08:49 -070069 }
Adam Cohen947dc542013-06-06 22:43:33 -070070
John Spurlock77e1f472013-09-11 10:09:51 -040071 @Override
72 public void setInsets(Rect insets) {
73 mInsets.set(insets);
Adam Cohen6c5891a2014-07-09 23:53:15 -070074 LayoutParams flp = (LayoutParams) mContent.getLayoutParams();
John Spurlock77e1f472013-09-11 10:09:51 -040075 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 Chung785d2eb2011-04-14 16:08:02 -070082 /**
83 * Setup the tab host and create all necessary tabs.
84 */
85 @Override
86 protected void onFinishInflate() {
Adam Cohen6c5891a2014-07-09 23:53:15 -070087 mPagedView = (AppsCustomizePagedView) findViewById(R.id.apps_customize_pane_content);
88 mContent = findViewById(R.id.content);
Winson Chungf0ea4d32011-06-06 14:27:16 -070089 }
Winson Chung785d2eb2011-04-14 16:08:02 -070090
Adam Cohen6c5891a2014-07-09 23:53:15 -070091 public String getContentTag() {
92 return getTabTagForContentType(mPagedView.getContentType());
Winson Chung785d2eb2011-04-14 16:08:02 -070093 }
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 Chungfc79c802011-05-02 13:35:34 -0700108 * 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 Chung785d2eb2011-04-14 16:08:02 -0700120 * 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 Chungc100e8e2011-08-09 16:02:43 -0700130 void reset() {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700131 // 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 Chungc100e8e2011-08-09 16:02:43 -0700142 }
143 }
144
Adam Cohen6c5891a2014-07-09 23:53:15 -0700145 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 Jurka1899a362011-11-03 13:50:45 -0700150 }
151
Michael Jurka2a4b1a82011-12-07 14:00:02 -0800152 @Override
Adam Cohenc956cba2014-07-24 09:17:37 -0700153 public ViewGroup getContent() {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700154 return mPagedView;
155 }
156
157 public boolean isInTransition() {
158 return mInTransition;
Michael Jurka1899a362011-11-03 13:50:45 -0700159 }
160
161 /* LauncherTransitionable overrides */
162 @Override
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700163 public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700164 mPagedView.onLauncherTransitionPrepare(l, animated, toWorkspace);
Michael Jurka1899a362011-11-03 13:50:45 -0700165 mInTransition = true;
Michael Jurka1899a362011-11-03 13:50:45 -0700166
Michael Jurkabed61d22012-02-14 22:51:29 -0800167 if (toWorkspace) {
168 // Going from All Apps -> Workspace
169 setVisibilityOfSiblingsWithLowerZOrder(VISIBLE);
170 } else {
171 // Going from Workspace -> All Apps
172 mContent.setVisibility(VISIBLE);
Michael Jurka3d845e82011-11-15 17:04:31 -0800173
Michael Jurkae326f182011-11-21 14:05:46 -0800174 // Make sure the current page is loaded (we start loading the side pages after the
175 // transition to prevent slowing down the animation)
Adam Cohen6c5891a2014-07-09 23:53:15 -0700176 // TODO: revisit this
Adam Cohen63f1ec02014-08-12 09:23:13 -0700177 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage());
Winson Chungc100e8e2011-08-09 16:02:43 -0700178 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700179 }
Michael Jurkabed61d22012-02-14 22:51:29 -0800180
Michael Jurkaa35e35a2012-04-26 15:04:28 -0700181 @Override
182 public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700183 mPagedView.onLauncherTransitionStart(l, animated, toWorkspace);
Winson Chung785d2eb2011-04-14 16:08:02 -0700184 }
Winson Chungf0ea4d32011-06-06 14:27:16 -0700185
Winson Chung785d2eb2011-04-14 16:08:02 -0700186 @Override
Winson Chung70442722012-02-10 15:43:22 -0800187 public void onLauncherTransitionStep(Launcher l, float t) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700188 mPagedView.onLauncherTransitionStep(l, t);
Winson Chung70442722012-02-10 15:43:22 -0800189 }
190
191 @Override
Michael Jurkabed61d22012-02-14 22:51:29 -0800192 public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
Adam Cohen6c5891a2014-07-09 23:53:15 -0700193 mPagedView.onLauncherTransitionEnd(l, animated, toWorkspace);
Winson Chungc100e8e2011-08-09 16:02:43 -0700194 mInTransition = false;
Winson Chung3ac74c52011-06-30 17:39:37 -0700195
Winson Chung7d7541e2011-09-16 20:14:36 -0700196 if (!toWorkspace) {
Michael Jurkae326f182011-11-21 14:05:46 -0800197 // Make sure adjacent pages are loaded (we wait until after the transition to
198 // prevent slowing down the animation)
Adam Cohen6c5891a2014-07-09 23:53:15 -0700199 mPagedView.loadAssociatedPages(mPagedView.getCurrentPage());
Winson Chung7d7541e2011-09-16 20:14:36 -0700200
Jason Monked05f092014-04-24 10:13:05 -0400201 // Opening apps, need to announce what page we are on.
202 AccessibilityManager am = (AccessibilityManager)
203 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
204 if (am.isEnabled()) {
205 // Notify the user when the page changes
Adam Cohen6c5891a2014-07-09 23:53:15 -0700206 announceForAccessibility(mPagedView.getCurrentPageDescription());
Jason Monked05f092014-04-24 10:13:05 -0400207 }
208
Winson Chung7819abd2012-11-29 14:29:38 -0800209 // Going from Workspace -> All Apps
210 // NOTE: We should do this at the end since we check visibility state in some of the
211 // cling initialization/dismiss code above.
212 setVisibilityOfSiblingsWithLowerZOrder(INVISIBLE);
Winson Chung5a808352011-06-27 19:08:49 -0700213 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700214 }
Winson Chung70d72102011-08-12 11:24:35 -0700215
Michael Jurkabed61d22012-02-14 22:51:29 -0800216 private void setVisibilityOfSiblingsWithLowerZOrder(int visibility) {
217 ViewGroup parent = (ViewGroup) getParent();
Michael Jurkaaea1ce52012-04-12 11:23:21 -0700218 if (parent == null) return;
219
Adam Cohen3d411982013-09-24 17:02:06 -0700220 View overviewPanel = ((Launcher) getContext()).getOverviewPanel();
Michael Jurkabed61d22012-02-14 22:51:29 -0800221 final int count = parent.getChildCount();
222 if (!isChildrenDrawingOrderEnabled()) {
223 for (int i = 0; i < count; i++) {
224 final View child = parent.getChildAt(i);
225 if (child == this) {
226 break;
227 } else {
Adam Cohen3d411982013-09-24 17:02:06 -0700228 if (child.getVisibility() == GONE || child == overviewPanel) {
Michael Jurkabed61d22012-02-14 22:51:29 -0800229 continue;
230 }
231 child.setVisibility(visibility);
232 }
233 }
234 } else {
235 throw new RuntimeException("Failed; can't get z-order of views");
236 }
237 }
Winson Chung785d2eb2011-04-14 16:08:02 -0700238}