summaryrefslogtreecommitdiffstats
path: root/quickstep/src/com/android/launcher3/uioverrides/WorkspaceCard.java
blob: ed60b84e19d09e158780e1e30414c2717cfdc4c1 (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
/*
 * Copyright (C) 2017 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.uioverrides;

import static com.android.launcher3.LauncherState.NORMAL;
import static com.android.launcher3.uioverrides.OverviewState.WORKSPACE_SCALE_ON_SCROLL;
import static com.android.quickstep.RecentsView.SCROLL_TYPE_WORKSPACE;

import android.animation.FloatArrayEvaluator;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;

import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.Workspace;
import com.android.launcher3.widget.WidgetsFullSheet;
import com.android.quickstep.RecentsView;
import com.android.quickstep.RecentsView.PageCallbacks;
import com.android.quickstep.RecentsView.ScrollState;

public class WorkspaceCard extends FrameLayout implements PageCallbacks, OnClickListener {

    private final Rect mTempRect = new Rect();
    private final float[] mEvaluatedFloats = new float[3];
    private final FloatArrayEvaluator mEvaluator = new FloatArrayEvaluator(mEvaluatedFloats);

    // UI related information
    private float[] mScaleAndTranslatePage0, mScaleAndTranslatePage1;
    private boolean mUIDataValid = false;

    private Launcher mLauncher;
    private Workspace mWorkspace;

    private boolean mIsWorkspaceScrollingEnabled;

    private View mWorkspaceClickTarget;
    private View mWidgetsButton;

    private boolean mLayoutHorizontal;

    public WorkspaceCard(Context context) {
        super(context);
    }

    public WorkspaceCard(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public WorkspaceCard(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mWorkspaceClickTarget = findViewById(R.id.workspace_click_target);
        mWidgetsButton = findViewById(R.id.widget_button);

        mWorkspaceClickTarget.setOnClickListener(this);
        mWidgetsButton.setOnClickListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // We measure the dimensions of the PagedView to be larger than the pages so that when we
        // zoom out (and scale down), the view is still contained in the parent
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }

        // Return early if we aren't given a proper dimension
        if (widthSize <= 0 || heightSize <= 0) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }

        float workspaceWidth = mWorkspace.getNormalChildWidth();
        float workspaceHeight = mWorkspace.getNormalChildHeight();

        int availableWidth = widthSize - getPaddingLeft() - getPaddingRight();
        float scaleX = availableWidth / workspaceWidth;

        int availableHeight = heightSize - getPaddingTop() - getPaddingBottom();
        float scaleY = availableHeight / workspaceHeight;

        if (scaleX < scaleY) {
            mLayoutHorizontal = false;
            int childWidthSpec = MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.EXACTLY);

            int pageHeight = Math.round(workspaceHeight * scaleX);
            mWorkspaceClickTarget.measure(childWidthSpec,
                    MeasureSpec.makeMeasureSpec(pageHeight, MeasureSpec.EXACTLY));

            int buttonHeight = availableHeight - pageHeight;
            mWidgetsButton.measure(childWidthSpec,
                    MeasureSpec.makeMeasureSpec(buttonHeight, MeasureSpec.EXACTLY));
        } else {
            mLayoutHorizontal = true;
            int childHeightSpec = MeasureSpec.makeMeasureSpec(availableHeight, MeasureSpec.EXACTLY);

            int pageWidth = Math.round(workspaceWidth * scaleY);
            mWorkspaceClickTarget.measure(
                    MeasureSpec.makeMeasureSpec(pageWidth, MeasureSpec.EXACTLY), childHeightSpec);

            int buttonWidth = availableWidth - pageWidth;
            mWidgetsButton.measure(
                    MeasureSpec.makeMeasureSpec(buttonWidth, MeasureSpec.EXACTLY), childHeightSpec);
        }
        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int x = getPaddingLeft();
        int y = getPaddingTop();

        if (mLayoutHorizontal) {
            final View first, second;
            if (Utilities.isRtl(getResources())) {
                first = mWidgetsButton;
                second = mWorkspaceClickTarget;
            } else {
                first = mWorkspaceClickTarget;
                second = mWidgetsButton;
            }
            int x2 = x + first.getMeasuredWidth();
            first.layout(x, y,
                    x2, y + first.getMeasuredHeight());
            second.layout(x2, y,
                    x2 + second.getMeasuredWidth(),
                    y + second.getMeasuredHeight());
        } else {
            int y2 = y + mWorkspaceClickTarget.getMeasuredHeight();
            mWorkspaceClickTarget.layout(x, y,
                    x + mWorkspaceClickTarget.getMeasuredWidth(), y2);
            mWidgetsButton.layout(x, y2,
                    x + mWidgetsButton.getMeasuredWidth(),
                    y2 + mWidgetsButton.getMeasuredHeight());
        }

        mUIDataValid = false;
    }

    @Override
    public void onClick(View view) {
        if (view == mWorkspaceClickTarget) {
            mLauncher.getStateManager().goToState(NORMAL);
        } else if (view == mWidgetsButton) {
            WidgetsFullSheet.show(mLauncher, true);
        }
    }

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

    public void setup(Launcher launcher) {
        mLauncher = launcher;
        mWorkspace = mLauncher.getWorkspace();
    }

    public void setWorkspaceScrollingEnabled(boolean isEnabled) {
        mIsWorkspaceScrollingEnabled = isEnabled;
    }

    @Override
    public int onPageScroll(ScrollState scrollState) {
        float factor = scrollState.linearInterpolation;
        float scale = factor * WORKSPACE_SCALE_ON_SCROLL + (1 - factor);
        setScaleX(scale);
        setScaleY(scale);

        float translateX = scrollState.distanceFromScreenCenter;
        if (mIsWorkspaceScrollingEnabled) {
            initUiData();

            mEvaluator.evaluate(factor, mScaleAndTranslatePage0, mScaleAndTranslatePage1);
            mWorkspace.setScaleX(mEvaluatedFloats[0]);
            mWorkspace.setScaleY(mEvaluatedFloats[0]);
            mWorkspace.setTranslationX(mEvaluatedFloats[1]);
            mWorkspace.setTranslationY(mEvaluatedFloats[2]);
            translateX += mEvaluatedFloats[1] - mScaleAndTranslatePage0[1];
        }

        setTranslationX(translateX);

        return SCROLL_TYPE_WORKSPACE;
    }

    private void initUiData() {
        if (mUIDataValid && mScaleAndTranslatePage0 != null) {
            return;
        }

        float overlap = getResources().getDimension(R.dimen.workspace_overview_offset_x);

        RecentsView.getPageRect(mLauncher, mTempRect);
        mScaleAndTranslatePage0 = OverviewState
                .getScaleAndTranslationForPageRect(mLauncher, 0, mTempRect);
        Rect scaledDown = new Rect(mTempRect);
        Utilities.scaleRectAboutCenter(scaledDown, WORKSPACE_SCALE_ON_SCROLL);
        mScaleAndTranslatePage1 = OverviewState
                .getScaleAndTranslationForPageRect(mLauncher, overlap, scaledDown);
        mUIDataValid = true;
    }
}