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
17package com.android.launcher3;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
23import android.content.Context;
24import android.content.res.Resources;
25import android.graphics.Color;
26import android.graphics.Rect;
27import android.util.AttributeSet;
28import android.view.LayoutInflater;
29import android.view.MotionEvent;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.FrameLayout;
33import android.widget.LinearLayout;
34import android.widget.TabHost;
35import android.widget.TabWidget;
36import android.widget.TextView;
37
38import java.util.ArrayList;
39
40public class AppsCustomizeTabHost extends TabHost implements LauncherTransitionable,
41        TabHost.OnTabChangeListener, Insettable  {
42    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
47    private final LayoutInflater mLayoutInflater;
48    private ViewGroup mTabs;
49    private ViewGroup mTabsContainer;
50    private AppsCustomizePagedView mAppsCustomizePane;
51    private FrameLayout mAnimationBuffer;
52    private LinearLayout mContent;
53
54    private boolean mInTransition;
55    private boolean mTransitioningToWorkspace;
56    private boolean mResetAfterTransition;
57    private Runnable mRelayoutAndMakeVisible;
58    private final Rect mInsets = new Rect();
59
60    public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
61        super(context, attrs);
62        mLayoutInflater = LayoutInflater.from(context);
63        mRelayoutAndMakeVisible = new Runnable() {
64                public void run() {
65                    mTabs.requestLayout();
66                    mTabsContainer.setAlpha(1f);
67                }
68            };
69    }
70
71    /**
72     * Convenience methods to select specific tabs.  We want to set the content type immediately
73     * in these cases, but we note that we still call setCurrentTabByTag() so that the tab view
74     * reflects the new content (but doesn't do the animation and logic associated with changing
75     * tabs manually).
76     */
77    void setContentTypeImmediate(AppsCustomizePagedView.ContentType type) {
78        setOnTabChangedListener(null);
79        onTabChangedStart();
80        onTabChangedEnd(type);
81        setCurrentTabByTag(getTabTagForContentType(type));
82        setOnTabChangedListener(this);
83    }
84
85    @Override
86    public void setInsets(Rect insets) {
87        mInsets.set(insets);
88        FrameLayout.LayoutParams flp = (LayoutParams) mContent.getLayoutParams();
89        flp.topMargin = insets.top;
90        flp.bottomMargin = insets.bottom;
91        flp.leftMargin = insets.left;
92        flp.rightMargin = insets.right;
93        mContent.setLayoutParams(flp);
94    }
95
96    /**
97     * Setup the tab host and create all necessary tabs.
98     */
99    @Override
100    protected void onFinishInflate() {
101        // Setup the tab host
102        setup();
103
104        final ViewGroup tabsContainer = (ViewGroup) findViewById(R.id.tabs_container);
105        final TabWidget tabs = getTabWidget();
106        final AppsCustomizePagedView appsCustomizePane = (AppsCustomizePagedView)
107                findViewById(R.id.apps_customize_pane_content);
108        mTabs = tabs;
109        mTabsContainer = tabsContainer;
110        mAppsCustomizePane = appsCustomizePane;
111        mAnimationBuffer = (FrameLayout) findViewById(R.id.animation_buffer);
112        mContent = (LinearLayout) findViewById(R.id.apps_customize_content);
113        if (tabs == null || mAppsCustomizePane == null) throw new Resources.NotFoundException();
114
115        // Configure the tabs content factory to return the same paged view (that we change the
116        // content filter on)
117        TabContentFactory contentFactory = new TabContentFactory() {
118            public View createTabContent(String tag) {
119                return appsCustomizePane;
120            }
121        };
122
123        // Create the tabs
124        TextView tabView;
125        String label;
126        label = getContext().getString(R.string.all_apps_button_label);
127        tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
128        tabView.setText(label);
129        tabView.setContentDescription(label);
130        addTab(newTabSpec(APPS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
131        label = getContext().getString(R.string.widgets_tab_label);
132        tabView = (TextView) mLayoutInflater.inflate(R.layout.tab_widget_indicator, tabs, false);
133        tabView.setText(label);
134        tabView.setContentDescription(label);
135        addTab(newTabSpec(WIDGETS_TAB_TAG).setIndicator(tabView).setContent(contentFactory));
136        setOnTabChangedListener(this);
137
138        // Setup the key listener to jump between the last tab view and the market icon
139        AppsCustomizeTabKeyEventListener keyListener = new AppsCustomizeTabKeyEventListener();
140        View lastTab = tabs.getChildTabViewAt(tabs.getTabCount() - 1);
141        lastTab.setOnKeyListener(keyListener);
142        View shopButton = findViewById(R.id.market_button);
143        shopButton.setOnKeyListener(keyListener);
144
145        // Hide the tab bar until we measure
146        mTabsContainer.setAlpha(0f);
147    }
148
149    @Override
150    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
151        boolean remeasureTabWidth = (mTabs.getLayoutParams().width <= 0);
152        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
153
154        // Set the width of the tab list to the content width
155        if (remeasureTabWidth) {
156            int contentWidth = mAppsCustomizePane.getPageContentWidth();
157            if (contentWidth > 0 && mTabs.getLayoutParams().width != contentWidth) {
158                // Set the width and show the tab bar
159                mTabs.getLayoutParams().width = contentWidth;
160                mRelayoutAndMakeVisible.run();
161            }
162
163            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
164        }
165    }
166
167     public boolean onInterceptTouchEvent(MotionEvent ev) {
168         // If we are mid transitioning to the workspace, then intercept touch events here so we
169         // can ignore them, otherwise we just let all apps handle the touch events.
170         if (mInTransition && mTransitioningToWorkspace) {
171             return true;
172         }
173         return super.onInterceptTouchEvent(ev);
174     };
175
176    @Override
177    public boolean onTouchEvent(MotionEvent event) {
178        // Allow touch events to fall through to the workspace if we are transitioning there
179        if (mInTransition && mTransitioningToWorkspace) {
180            return super.onTouchEvent(event);
181        }
182
183        // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
184        // through to the workspace and trigger showWorkspace()
185        if (event.getY() < mAppsCustomizePane.getBottom()) {
186            return true;
187        }
188        return super.onTouchEvent(event);
189    }
190
191    private void onTabChangedStart() {
192    }
193
194    private void reloadCurrentPage() {
195        mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage());
196        mAppsCustomizePane.requestFocus();
197    }
198
199    private void onTabChangedEnd(AppsCustomizePagedView.ContentType type) {
200        int bgAlpha = (int) (255 * (getResources().getInteger(
201            R.integer.config_appsCustomizeSpringLoadedBgAlpha) / 100f));
202        setBackgroundColor(Color.argb(bgAlpha, 0, 0, 0));
203        mAppsCustomizePane.setContentType(type);
204    }
205
206    @Override
207    public void onTabChanged(String tabId) {
208        final AppsCustomizePagedView.ContentType type = getContentTypeForTabTag(tabId);
209
210        // Animate the changing of the tab content by fading pages in and out
211        final Resources res = getResources();
212        final int duration = res.getInteger(R.integer.config_tabTransitionDuration);
213
214        // We post a runnable here because there is a delay while the first page is loading and
215        // the feedback from having changed the tab almost feels better than having it stick
216        post(new Runnable() {
217            @Override
218            public void run() {
219                if (mAppsCustomizePane.getMeasuredWidth() <= 0 ||
220                        mAppsCustomizePane.getMeasuredHeight() <= 0) {
221                    reloadCurrentPage();
222                    return;
223                }
224
225                // Take the visible pages and re-parent them temporarily to mAnimatorBuffer
226                // and then cross fade to the new pages
227                int[] visiblePageRange = new int[2];
228                mAppsCustomizePane.getVisiblePages(visiblePageRange);
229                if (visiblePageRange[0] == -1 && visiblePageRange[1] == -1) {
230                    // If we can't get the visible page ranges, then just skip the animation
231                    reloadCurrentPage();
232                    return;
233                }
234                ArrayList<View> visiblePages = new ArrayList<View>();
235                for (int i = visiblePageRange[0]; i <= visiblePageRange[1]; i++) {
236                    visiblePages.add(mAppsCustomizePane.getPageAt(i));
237                }
238
239                // We want the pages to be rendered in exactly the same way as they were when
240                // their parent was mAppsCustomizePane -- so set the scroll on mAnimationBuffer
241                // to be exactly the same as mAppsCustomizePane, and below, set the left/top
242                // parameters to be correct for each of the pages
243                mAnimationBuffer.scrollTo(mAppsCustomizePane.getScrollX(), 0);
244
245                // mAppsCustomizePane renders its children in reverse order, so
246                // add the pages to mAnimationBuffer in reverse order to match that behavior
247                for (int i = visiblePages.size() - 1; i >= 0; i--) {
248                    View child = visiblePages.get(i);
249                    if (child instanceof AppsCustomizeCellLayout) {
250                        ((AppsCustomizeCellLayout) child).resetChildrenOnKeyListeners();
251                    } else if (child instanceof PagedViewGridLayout) {
252                        ((PagedViewGridLayout) child).resetChildrenOnKeyListeners();
253                    }
254                    PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(false);
255                    mAppsCustomizePane.removeView(child);
256                    PagedViewWidget.setDeletePreviewsWhenDetachedFromWindow(true);
257                    mAnimationBuffer.setAlpha(1f);
258                    mAnimationBuffer.setVisibility(View.VISIBLE);
259                    LayoutParams p = new FrameLayout.LayoutParams(child.getMeasuredWidth(),
260                            child.getMeasuredHeight());
261                    p.setMargins((int) child.getLeft(), (int) child.getTop(), 0, 0);
262                    mAnimationBuffer.addView(child, p);
263                }
264
265                // Toggle the new content
266                onTabChangedStart();
267                onTabChangedEnd(type);
268
269                // Animate the transition
270                ObjectAnimator outAnim = LauncherAnimUtils.ofFloat(mAnimationBuffer, "alpha", 0f);
271                outAnim.addListener(new AnimatorListenerAdapter() {
272                    private void clearAnimationBuffer() {
273                        mAnimationBuffer.setVisibility(View.GONE);
274                        PagedViewWidget.setRecyclePreviewsWhenDetachedFromWindow(false);
275                        mAnimationBuffer.removeAllViews();
276                        PagedViewWidget.setRecyclePreviewsWhenDetachedFromWindow(true);
277                    }
278                    @Override
279                    public void onAnimationEnd(Animator animation) {
280                        clearAnimationBuffer();
281                    }
282                    @Override
283                    public void onAnimationCancel(Animator animation) {
284                        clearAnimationBuffer();
285                    }
286                });
287                ObjectAnimator inAnim = LauncherAnimUtils.ofFloat(mAppsCustomizePane, "alpha", 1f);
288                inAnim.addListener(new AnimatorListenerAdapter() {
289                    @Override
290                    public void onAnimationEnd(Animator animation) {
291                        reloadCurrentPage();
292                    }
293                });
294
295                final AnimatorSet animSet = LauncherAnimUtils.createAnimatorSet();
296                animSet.playTogether(outAnim, inAnim);
297                animSet.setDuration(duration);
298                animSet.start();
299            }
300        });
301    }
302
303    public void setCurrentTabFromContent(AppsCustomizePagedView.ContentType type) {
304        setOnTabChangedListener(null);
305        setCurrentTabByTag(getTabTagForContentType(type));
306        setOnTabChangedListener(this);
307    }
308
309    /**
310     * Returns the content type for the specified tab tag.
311     */
312    public AppsCustomizePagedView.ContentType getContentTypeForTabTag(String tag) {
313        if (tag.equals(APPS_TAB_TAG)) {
314            return AppsCustomizePagedView.ContentType.Applications;
315        } else if (tag.equals(WIDGETS_TAB_TAG)) {
316            return AppsCustomizePagedView.ContentType.Widgets;
317        }
318        return AppsCustomizePagedView.ContentType.Applications;
319    }
320
321    /**
322     * Returns the tab tag for a given content type.
323     */
324    public String getTabTagForContentType(AppsCustomizePagedView.ContentType type) {
325        if (type == AppsCustomizePagedView.ContentType.Applications) {
326            return APPS_TAB_TAG;
327        } else if (type == AppsCustomizePagedView.ContentType.Widgets) {
328            return WIDGETS_TAB_TAG;
329        }
330        return APPS_TAB_TAG;
331    }
332
333    /**
334     * Disable focus on anything under this view in the hierarchy if we are not visible.
335     */
336    @Override
337    public int getDescendantFocusability() {
338        if (getVisibility() != View.VISIBLE) {
339            return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
340        }
341        return super.getDescendantFocusability();
342    }
343
344    void reset() {
345        if (mInTransition) {
346            // Defer to after the transition to reset
347            mResetAfterTransition = true;
348        } else {
349            // Reset immediately
350            mAppsCustomizePane.reset();
351        }
352    }
353
354    private void enableAndBuildHardwareLayer() {
355        // isHardwareAccelerated() checks if we're attached to a window and if that
356        // window is HW accelerated-- we were sometimes not attached to a window
357        // and buildLayer was throwing an IllegalStateException
358        if (isHardwareAccelerated()) {
359            // Turn on hardware layers for performance
360            setLayerType(LAYER_TYPE_HARDWARE, null);
361
362            // force building the layer, so you don't get a blip early in an animation
363            // when the layer is created layer
364            buildLayer();
365        }
366    }
367
368    @Override
369    public View getContent() {
370        View appsCustomizeContent = mAppsCustomizePane.getContent();
371        if (appsCustomizeContent != null) {
372            return appsCustomizeContent;
373        }
374        return mContent;
375    }
376
377    /* LauncherTransitionable overrides */
378    @Override
379    public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
380        mAppsCustomizePane.onLauncherTransitionPrepare(l, animated, toWorkspace);
381        mInTransition = true;
382        mTransitioningToWorkspace = toWorkspace;
383
384        if (toWorkspace) {
385            // Going from All Apps -> Workspace
386            setVisibilityOfSiblingsWithLowerZOrder(VISIBLE);
387        } else {
388            // Going from Workspace -> All Apps
389            mContent.setVisibility(VISIBLE);
390
391            // Make sure the current page is loaded (we start loading the side pages after the
392            // transition to prevent slowing down the animation)
393            mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage(), true);
394        }
395
396        if (mResetAfterTransition) {
397            mAppsCustomizePane.reset();
398            mResetAfterTransition = false;
399        }
400    }
401
402    @Override
403    public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
404        mAppsCustomizePane.onLauncherTransitionStart(l, animated, toWorkspace);
405        if (animated) {
406            enableAndBuildHardwareLayer();
407        }
408
409        // Dismiss the workspace cling
410        l.getLauncherClings().dismissWorkspaceCling(null);
411    }
412
413    @Override
414    public void onLauncherTransitionStep(Launcher l, float t) {
415        mAppsCustomizePane.onLauncherTransitionStep(l, t);
416    }
417
418    @Override
419    public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
420        mAppsCustomizePane.onLauncherTransitionEnd(l, animated, toWorkspace);
421        mInTransition = false;
422        if (animated) {
423            setLayerType(LAYER_TYPE_NONE, null);
424        }
425
426        if (!toWorkspace) {
427            // Show the all apps cling (if not already shown)
428            mAppsCustomizePane.showAllAppsCling();
429            // Make sure adjacent pages are loaded (we wait until after the transition to
430            // prevent slowing down the animation)
431            mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage());
432
433            // Going from Workspace -> All Apps
434            // NOTE: We should do this at the end since we check visibility state in some of the
435            // cling initialization/dismiss code above.
436            setVisibilityOfSiblingsWithLowerZOrder(INVISIBLE);
437        }
438    }
439
440    private void setVisibilityOfSiblingsWithLowerZOrder(int visibility) {
441        ViewGroup parent = (ViewGroup) getParent();
442        if (parent == null) return;
443
444        View overviewPanel = ((Launcher) getContext()).getOverviewPanel();
445        final int count = parent.getChildCount();
446        if (!isChildrenDrawingOrderEnabled()) {
447            for (int i = 0; i < count; i++) {
448                final View child = parent.getChildAt(i);
449                if (child == this) {
450                    break;
451                } else {
452                    if (child.getVisibility() == GONE || child == overviewPanel) {
453                        continue;
454                    }
455                    child.setVisibility(visibility);
456                }
457            }
458        } else {
459            throw new RuntimeException("Failed; can't get z-order of views");
460        }
461    }
462
463    public void onWindowVisible() {
464        if (getVisibility() == VISIBLE) {
465            mContent.setVisibility(VISIBLE);
466            // We unload the widget previews when the UI is hidden, so need to reload pages
467            // Load the current page synchronously, and the neighboring pages asynchronously
468            mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage(), true);
469            mAppsCustomizePane.loadAssociatedPages(mAppsCustomizePane.getCurrentPage());
470        }
471    }
472
473    public void onTrimMemory() {
474        mContent.setVisibility(GONE);
475        // Clear the widget pages of all their subviews - this will trigger the widget previews
476        // to delete their bitmaps
477        mAppsCustomizePane.clearAllWidgetPages();
478    }
479
480    boolean isTransitioning() {
481        return mInTransition;
482    }
483}
484