summaryrefslogtreecommitdiff
path: root/src/com/android/launcher3/allapps/SearchTransitionController.java
blob: eb1bc0a4be55ae6e39d14dc613d71d4aec2810e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher3.allapps;

import static android.view.View.VISIBLE;

import static androidx.recyclerview.widget.RecyclerView.NO_POSITION;

import static com.android.app.animation.Interpolators.DECELERATE_1_7;
import static com.android.app.animation.Interpolators.INSTANT;
import static com.android.app.animation.Interpolators.clampToProgress;
import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
import static com.android.launcher3.anim.AnimatorListeners.forEndCallback;
import static com.android.launcher3.anim.AnimatorListeners.forSuccessCallback;

import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.graphics.drawable.Drawable;
import android.util.FloatProperty;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;

import com.android.launcher3.BubbleTextView;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.model.data.ItemInfo;

/** Coordinates the transition between Search and A-Z in All Apps. */
public class SearchTransitionController {

    private static final String LOG_TAG = "SearchTransitionCtrl";

    // Interpolator when the user taps the QSB while already in All Apps.
    private static final Interpolator INTERPOLATOR_WITHIN_ALL_APPS = DECELERATE_1_7;
    // Interpolator when the user taps the QSB from home screen, so transition to all apps is
    // happening simultaneously.
    private static final Interpolator INTERPOLATOR_TRANSITIONING_TO_ALL_APPS = INSTANT;

    /**
     * These values represent points on the [0, 1] animation progress spectrum. They are used to
     * animate items in the {@link SearchRecyclerView}.
     */
    private static final float TOP_CONTENT_FADE_PROGRESS_START = 0.133f;
    private static final float CONTENT_FADE_PROGRESS_DURATION = 0.083f;
    private static final float TOP_BACKGROUND_FADE_PROGRESS_START = 0.633f;
    private static final float BACKGROUND_FADE_PROGRESS_DURATION = 0.15f;
    private static final float CONTENT_STAGGER = 0.01f;  // Progress before next item starts fading.

    private static final FloatProperty<SearchTransitionController> SEARCH_TO_AZ_PROGRESS =
            new FloatProperty<SearchTransitionController>("searchToAzProgress") {
                @Override
                public Float get(SearchTransitionController controller) {
                    return controller.getSearchToAzProgress();
                }

                @Override
                public void setValue(SearchTransitionController controller, float progress) {
                    controller.setSearchToAzProgress(progress);
                }
            };

    private final ActivityAllAppsContainerView<?> mAllAppsContainerView;

    private ObjectAnimator mSearchToAzAnimator = null;
    private float mSearchToAzProgress = 1f;
    private boolean mSkipNextAnimationWithinAllApps;

    public SearchTransitionController(ActivityAllAppsContainerView<?> allAppsContainerView) {
        mAllAppsContainerView = allAppsContainerView;
    }

    /** Returns true if a transition animation is currently in progress. */
    public boolean isRunning() {
        return mSearchToAzAnimator != null;
    }

    /**
     * Starts the transition to or from search state. If a transition is already in progress, the
     * animation will start from that point with the new duration, and the previous onEndRunnable
     * will not be called.
     *
     * @param goingToSearch true if will be showing search results, otherwise will be showing a-z
     * @param duration time in ms for the animation to run
     * @param onEndRunnable will be called when the animation finishes, unless another animation is
     *                      scheduled in the meantime
     */
    public void animateToSearchState(boolean goingToSearch, long duration, Runnable onEndRunnable) {
        float targetProgress = goingToSearch ? 0 : 1;

        if (mSearchToAzAnimator != null) {
            mSearchToAzAnimator.cancel();
        }

        mSearchToAzAnimator = ObjectAnimator.ofFloat(this, SEARCH_TO_AZ_PROGRESS, targetProgress);
        boolean inAllApps = mAllAppsContainerView.isInAllApps();
        TimeInterpolator timeInterpolator =
                inAllApps ? INTERPOLATOR_WITHIN_ALL_APPS : INTERPOLATOR_TRANSITIONING_TO_ALL_APPS;
        if (mSkipNextAnimationWithinAllApps) {
            timeInterpolator = INSTANT;
            mSkipNextAnimationWithinAllApps = false;
        }
        if (timeInterpolator == INSTANT) {
            duration = 0;  // Don't want to animate when coming from QSB.
        }
        mSearchToAzAnimator.setDuration(duration).setInterpolator(timeInterpolator);
        mSearchToAzAnimator.addListener(forEndCallback(() -> mSearchToAzAnimator = null));
        if (!goingToSearch) {
            mSearchToAzAnimator.addListener(forSuccessCallback(() -> {
                mAllAppsContainerView.getFloatingHeaderView().setFloatingRowsCollapsed(false);
                mAllAppsContainerView.getFloatingHeaderView().reset(false /* animate */);
                mAllAppsContainerView.getAppsRecyclerViewContainer().setTranslationY(0);
            }));
        }
        mSearchToAzAnimator.addListener(forSuccessCallback(onEndRunnable));

        mAllAppsContainerView.getFloatingHeaderView().setFloatingRowsCollapsed(true);
        mAllAppsContainerView.getFloatingHeaderView().setVisibility(VISIBLE);
        mAllAppsContainerView.getFloatingHeaderView().maybeSetTabVisibility(VISIBLE);
        mAllAppsContainerView.getAppsRecyclerViewContainer().setVisibility(VISIBLE);
        getSearchRecyclerView().setVisibility(VISIBLE);
        getSearchRecyclerView().setChildAttachedConsumer(this::onSearchChildAttached);
        mSearchToAzAnimator.start();
    }

    private SearchRecyclerView getSearchRecyclerView() {
        return mAllAppsContainerView.getSearchRecyclerView();
    }

    private void setSearchToAzProgress(float searchToAzProgress) {
        mSearchToAzProgress = searchToAzProgress;
        int searchHeight = updateSearchRecyclerViewProgress();

        FloatingHeaderView headerView = mAllAppsContainerView.getFloatingHeaderView();

        // Add predictions + app divider height to account for predicted apps which will now be in
        // the Search RV instead of the floating header view. Note `getFloatingRowsHeight` returns 0
        // when predictions are not shown.
        int appsTranslationY = searchHeight + headerView.getFloatingRowsHeight();

        if (headerView.usingTabs()) {
            // Move tabs below the search results, and fade them out in 20% of the animation.
            headerView.setTranslationY(searchHeight);
            headerView.setAlpha(clampToProgress(searchToAzProgress, 0.8f, 1f));

            // Account for the additional padding added for the tabs.
            appsTranslationY +=
                    headerView.getTabsAdditionalPaddingBottom()
                            + mAllAppsContainerView.getResources().getDimensionPixelOffset(
                                    R.dimen.all_apps_tabs_margin_top)
                            - headerView.getPaddingTop();
        }

        View appsContainer = mAllAppsContainerView.getAppsRecyclerViewContainer();
        appsContainer.setTranslationY(appsTranslationY);
        // Fade apps out with tabs (in 20% of the total animation).
        appsContainer.setAlpha(clampToProgress(searchToAzProgress, 0.8f, 1f));
    }

    /**
     * Updates the children views of SearchRecyclerView based on the current animation progress.
     *
     * @return the total height of animating views (excluding at most one row of app icons).
     */
    private int updateSearchRecyclerViewProgress() {
        int numSearchResultsAnimated = 0;
        int totalHeight = 0;
        int appRowHeight = 0;
        boolean appRowComplete = false;
        Integer top = null;
        SearchRecyclerView searchRecyclerView = getSearchRecyclerView();

        for (int i = 0; i < searchRecyclerView.getChildCount(); i++) {
            View searchResultView = searchRecyclerView.getChildAt(i);
            if (searchResultView == null) {
                continue;
            }

            if (top == null) {
                top = searchResultView.getTop();
            }

            int adapterPosition = searchRecyclerView.getChildAdapterPosition(searchResultView);
            int spanIndex = getSpanIndex(searchRecyclerView, adapterPosition);
            appRowComplete |= appRowHeight > 0 && spanIndex == 0;
            // We don't animate the first (currently only) app row we see, as that is assumed to be
            // predicted/prefix-matched apps.
            boolean shouldAnimate = !isAppIcon(searchResultView) || appRowComplete;

            float contentAlpha = 1f;
            float backgroundAlpha = 1f;
            if (shouldAnimate) {
                if (spanIndex > 0) {
                    // Animate this item with the previous item on the same row.
                    numSearchResultsAnimated--;
                }

                // Adjust content alpha based on start progress and stagger.
                float startContentFadeProgress = Math.max(0,
                        TOP_CONTENT_FADE_PROGRESS_START
                                - CONTENT_STAGGER * numSearchResultsAnimated);
                float endContentFadeProgress = Math.min(1,
                        startContentFadeProgress + CONTENT_FADE_PROGRESS_DURATION);
                contentAlpha = 1 - clampToProgress(mSearchToAzProgress,
                        startContentFadeProgress, endContentFadeProgress);

                // Adjust background (or decorator) alpha based on start progress and stagger.
                float startBackgroundFadeProgress = Math.max(0,
                        TOP_BACKGROUND_FADE_PROGRESS_START
                                - CONTENT_STAGGER * numSearchResultsAnimated);
                float endBackgroundFadeProgress = Math.min(1,
                        startBackgroundFadeProgress + BACKGROUND_FADE_PROGRESS_DURATION);
                backgroundAlpha = 1 - clampToProgress(mSearchToAzProgress,
                        startBackgroundFadeProgress, endBackgroundFadeProgress);

                numSearchResultsAnimated++;
            }

            Drawable background = searchResultView.getBackground();
            if (background != null
                    && searchResultView instanceof ViewGroup
                    && FeatureFlags.ENABLE_SEARCH_RESULT_BACKGROUND_DRAWABLES.get()) {
                searchResultView.setAlpha(1f);

                // Apply content alpha to each child, since the view needs to be fully opaque for
                // the background to show properly.
                ViewGroup searchResultViewGroup = (ViewGroup) searchResultView;
                for (int j = 0; j < searchResultViewGroup.getChildCount(); j++) {
                    searchResultViewGroup.getChildAt(j).setAlpha(contentAlpha);
                }

                // Apply background alpha to the background drawable directly.
                background.setAlpha((int) (255 * backgroundAlpha));
            } else {
                searchResultView.setAlpha(contentAlpha);

                // Apply background alpha to decorator if possible.
                if (adapterPosition != NO_POSITION) {
                    searchRecyclerView.getApps().getAdapterItems().get(adapterPosition)
                            .setDecorationFillAlpha((int) (255 * backgroundAlpha));
                }

                // Apply background alpha to view's background (e.g. for Search Edu card).
                if (background != null) {
                    background.setAlpha((int) (255 * backgroundAlpha));
                }
            }

            float scaleY = 1;
            if (shouldAnimate) {
                scaleY = 1 - mSearchToAzProgress;
            }
            int scaledHeight = (int) (searchResultView.getHeight() * scaleY);
            searchResultView.setScaleY(scaleY);

            // For rows with multiple elements, only count the height once and translate elements to
            // the same y position.
            int y = top + totalHeight;
            if (spanIndex > 0) {
                // Continuation of an existing row; move this item into the row.
                y -= scaledHeight;
            } else {
                // Start of a new row contributes to total height.
                totalHeight += scaledHeight;
                if (!shouldAnimate) {
                    appRowHeight = scaledHeight;
                }
            }
            searchResultView.setY(y);
        }

        return totalHeight - appRowHeight;
    }

    /** @return the column that the view at this position is found (0 assumed if indeterminate). */
    private int getSpanIndex(SearchRecyclerView searchRecyclerView, int adapterPosition) {
        if (adapterPosition == NO_POSITION) {
            Log.w(LOG_TAG, "Can't determine span index - child not found in adapter");
            return 0;
        }
        if (!(searchRecyclerView.getAdapter() instanceof AllAppsGridAdapter<?>)) {
            Log.e(LOG_TAG, "Search RV doesn't have an AllAppsGridAdapter?");
            // This case shouldn't happen, but for debug devices we will continue to create a more
            // visible crash.
            if (!Utilities.IS_DEBUG_DEVICE) {
                return 0;
            }
        }
        AllAppsGridAdapter<?> adapter = (AllAppsGridAdapter<?>) searchRecyclerView.getAdapter();
        return adapter.getSpanIndex(adapterPosition);
    }

    private boolean isAppIcon(View item) {
        return item instanceof BubbleTextView && item.getTag() instanceof ItemInfo
                && ((ItemInfo) item.getTag()).itemType == ITEM_TYPE_APPLICATION;
    }

    /** Called just before a child is attached to the SearchRecyclerView. */
    private void onSearchChildAttached(View child) {
        // Avoid allocating hardware layers for alpha changes.
        child.forceHasOverlappingRendering(false);
        child.setPivotY(0);
        if (mSearchToAzProgress > 0) {
            // Before the child is rendered, apply the animation including it to avoid flicker.
            updateSearchRecyclerViewProgress();
        } else {
            // Apply default states without processing the full layout.
            child.setAlpha(1);
            child.setScaleY(1);
            child.setTranslationY(0);
            int adapterPosition = getSearchRecyclerView().getChildAdapterPosition(child);
            if (adapterPosition != NO_POSITION) {
                getSearchRecyclerView().getApps().getAdapterItems().get(adapterPosition)
                        .setDecorationFillAlpha(255);
            }
            if (child instanceof ViewGroup
                    && FeatureFlags.ENABLE_SEARCH_RESULT_BACKGROUND_DRAWABLES.get()) {
                ViewGroup childGroup = (ViewGroup) child;
                for (int i = 0; i < childGroup.getChildCount(); i++) {
                    childGroup.getChildAt(i).setAlpha(1f);
                }
            }
            if (child.getBackground() != null) {
                child.getBackground().setAlpha(255);
            }
        }
    }

    private float getSearchToAzProgress() {
        return mSearchToAzProgress;
    }

    /**
     * This should only be called from {@code LauncherSearchSessionManager} when app restarts due to
     * theme changes.
     */
    public void setSkipAnimationWithinAllApps(boolean skip) {
        mSkipNextAnimationWithinAllApps = skip;
    }
}