summaryrefslogtreecommitdiffstats
path: root/quickstep/src/com/android/quickstep/views/ShelfScrimView.java
blob: aca8351a06ac7c6a61b7f31417c8e2ccd8e4f84f (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
/*
 * Copyright (C) 2018 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.quickstep.views;

import static android.support.v4.graphics.ColorUtils.compositeColors;
import static android.support.v4.graphics.ColorUtils.setAlphaComponent;

import static com.android.launcher3.LauncherState.OVERVIEW;
import static com.android.launcher3.anim.Interpolators.ACCEL_2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Path.Op;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.util.AttributeSet;

import com.android.launcher3.DeviceProfile;
import com.android.launcher3.R;
import com.android.launcher3.uioverrides.OverviewState;
import com.android.launcher3.util.Themes;
import com.android.launcher3.views.ScrimView;

/**
 * Scrim used for all-apps and shelf in Overview
 * In transposed layout, it behaves as a simple color scrim.
 * In portrait layout, it draws a rounded rect such that
 *    From normal state to overview state, the shelf just fades in and does not move
 *    From overview state to all-apps state the shelf moves up and fades in to cover the screen
 */
public class ShelfScrimView extends ScrimView {

    private static final int THRESHOLD_ALPHA_DARK = 102;
    private static final int THRESHOLD_ALPHA_LIGHT = 46;
    private static final int THRESHOLD_ALPHA_SUPER_LIGHT = 128;
    private static final int CLEAR_ALL_TASKS = R.string.recents_clear_all;

    // In transposed layout, we simply draw a flat color.
    private boolean mDrawingFlatColor;

    // For shelf mode
    private final int mEndAlpha;
    private final int mThresholdAlpha;
    private final float mRadius;
    private final float mMaxScrimAlpha;
    private final Paint mPaint;

    // Max vertical progress after which the scrim stops moving.
    private float mMoveThreshold;
    // Minimum visible size of the scrim.
    private int mMinSize;

    private float mScrimMoveFactor = 0;
    private int mShelfColor;
    private int mRemainingScreenColor;

    private final Path mTempPath = new Path();
    private final Path mRemainingScreenPath = new Path();
    private boolean mRemainingScreenPathValid = false;

    public ShelfScrimView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMaxScrimAlpha = OVERVIEW.getWorkspaceScrimAlpha(mLauncher);

        mEndAlpha = Color.alpha(mEndScrim);
        if (Themes.getAttrBoolean(mLauncher, R.attr.isMainColorDark)) {
            mThresholdAlpha = THRESHOLD_ALPHA_DARK;
        } else if (Themes.getAttrBoolean(mLauncher, R.attr.isWorkspaceDarkText)) {
            mThresholdAlpha = THRESHOLD_ALPHA_SUPER_LIGHT;
        } else {
            mThresholdAlpha = THRESHOLD_ALPHA_LIGHT;
        }
        mRadius = mLauncher.getResources().getDimension(R.dimen.shelf_surface_radius);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        // Just assume the easiest UI for now, until we have the proper layout information.
        mDrawingFlatColor = true;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRemainingScreenPathValid = false;
    }

    @Override
    public void reInitUi() {
        DeviceProfile dp = mLauncher.getDeviceProfile();
        mDrawingFlatColor = dp.isVerticalBarLayout();

        if (!mDrawingFlatColor) {
            float swipeLength = OverviewState.getDefaultSwipeHeight(mLauncher);
            mMoveThreshold = 1 - swipeLength / mLauncher.getAllAppsController().getShiftRange();
            mMinSize = dp.hotseatBarSizePx + dp.getInsets().bottom;
            mRemainingScreenPathValid = false;
            updateColors();
        }
        updateDragHandleAlpha();
        invalidate();
    }

    @Override
    public void updateColors() {
        super.updateColors();
        if (mDrawingFlatColor) {
            return;
        }

        if (mProgress >= mMoveThreshold) {
            mScrimMoveFactor = 1;

            if (mProgress >= 1) {
                mShelfColor = 0;
            } else {
                int alpha = Math.round(mThresholdAlpha * ACCEL_2.getInterpolation(
                        (1 - mProgress) / (1 - mMoveThreshold)));
                mShelfColor = setAlphaComponent(mEndScrim, alpha);
            }

            mRemainingScreenColor = 0;
        } else if (mProgress <= 0) {
            mScrimMoveFactor = 0;
            mShelfColor = mCurrentFlatColor;
            mRemainingScreenColor = 0;

        } else {
            mScrimMoveFactor = mProgress / mMoveThreshold;
            mRemainingScreenColor = setAlphaComponent(mScrimColor,
                    Math.round((1 - mScrimMoveFactor) * mMaxScrimAlpha * 255));

            // Merge the remainingScreenColor and shelfColor in one to avoid overdraw.
            int alpha = mEndAlpha - Math.round((mEndAlpha - mThresholdAlpha) * mScrimMoveFactor);
            mShelfColor = compositeColors(setAlphaComponent(mEndScrim, alpha),
                    mRemainingScreenColor);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float translate = drawBackground(canvas);

        if (mDragHandle != null) {
            canvas.translate(0, -translate);
            mDragHandle.draw(canvas);
            canvas.translate(0, translate);
        }
    }

    private float drawBackground(Canvas canvas) {
        if (mDrawingFlatColor) {
            if (mCurrentFlatColor != 0) {
                canvas.drawColor(mCurrentFlatColor);
            }
            return 0;
        }

        if (mShelfColor == 0) {
            return 0;
        } else if (mScrimMoveFactor <= 0) {
            canvas.drawColor(mShelfColor);
            return getHeight();
        }

        float minTop = getHeight() - mMinSize;
        float top = minTop * mScrimMoveFactor - mDragHandleSize;

        // Draw the scrim over the remaining screen if needed.
        if (mRemainingScreenColor != 0) {
            if (!mRemainingScreenPathValid) {
                mTempPath.reset();
                // Using a arbitrary '+10' in the bottom to avoid any left-overs at the
                // corners due to rounding issues.
                mTempPath.addRoundRect(0, minTop, getWidth(), getHeight() + mRadius + 10,
                        mRadius, mRadius, Direction.CW);

                mRemainingScreenPath.reset();
                mRemainingScreenPath.addRect(0, 0, getWidth(), getHeight(), Direction.CW);
                mRemainingScreenPath.op(mTempPath, Op.DIFFERENCE);
            }

            float offset = minTop - top;
            canvas.translate(0, -offset);
            mPaint.setColor(mRemainingScreenColor);
            canvas.drawPath(mRemainingScreenPath, mPaint);
            canvas.translate(0, offset);
        }

        mPaint.setColor(mShelfColor);
        canvas.drawRoundRect(0, top, getWidth(), getHeight() + mRadius,
                mRadius, mRadius, mPaint);
        return minTop - mDragHandleSize - top;
    }

    @NonNull
    @Override
    protected AccessibilityHelper createAccessibilityHelper() {
        return new ShelfScrimAccessibilityHelper();
    }

    protected class ShelfScrimAccessibilityHelper extends AccessibilityHelper {
        @Override
        protected void onPopulateNodeForVirtualView(int virtualViewId,
                AccessibilityNodeInfoCompat node) {
            super.onPopulateNodeForVirtualView(virtualViewId, node);

            if (mLauncher.isInState(OVERVIEW)) {
                final RecentsView overviewPanel = mLauncher.getOverviewPanel();
                if (overviewPanel.getChildCount() != 0) {
                    node.addAction(
                            new AccessibilityNodeInfoCompat.AccessibilityActionCompat(
                                    CLEAR_ALL_TASKS,
                                    getContext().getText(CLEAR_ALL_TASKS)));
                }
            }
        }

        @Override
        protected boolean onPerformActionForVirtualView(
                int virtualViewId, int action, Bundle arguments) {
            if (super.onPerformActionForVirtualView(virtualViewId, action, arguments)) return true;

            if (action == CLEAR_ALL_TASKS) {
                if (mLauncher.isInState(OVERVIEW)) {
                    mLauncher.<RecentsView>getOverviewPanel().dismissAllTasks();
                }
                return true;
            }

            return false;
        }
    }
}