summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher2/Cling.java
blob: ae8dd4319d1b0187e45aa7e95ae62cd4f531309a (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
/*
 * Copyright (C) 2011 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.launcher2;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;

import com.android.launcher.R;

public class Cling extends FrameLayout {

    private static String WORKSPACE_PORTRAIT = "workspace_portrait";
    private static String WORKSPACE_LANDSCAPE = "workspace_landscape";
    private static String ALLAPPS_PORTRAIT = "all_apps_portrait";
    private static String ALLAPPS_LANDSCAPE = "all_apps_landscape";

    private Launcher mLauncher;
    private boolean mIsInitialized;
    private String mDrawIdentifier;
    private Drawable mPunchThroughGraphic;
    private int mPunchThroughGraphicCenterRadius;
    private int mAppIconSize;
    private int mTabBarHeight;
    private int mTabBarHorizontalPadding;

    View mWorkspaceDesc1;
    View mWorkspaceDesc2;
    View mAllAppsDesc;

    public Cling(Context context) {
        this(context, null, 0);
    }

    public Cling(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Cling(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cling, defStyle, 0);
        mDrawIdentifier = a.getString(R.styleable.Cling_drawIdentifier);
        a.recycle();
    }

    void init(Launcher l) {
        if (!mIsInitialized) {
            mLauncher = l;

            Resources r = getContext().getResources();
            mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
            mPunchThroughGraphicCenterRadius =
                r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
            mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
            mTabBarHeight = r.getDimensionPixelSize(R.dimen.apps_customize_tab_bar_height);
            mTabBarHorizontalPadding =
                r.getDimensionPixelSize(R.dimen.toolbar_button_horizontal_padding);

            mWorkspaceDesc1 = findViewById(R.id.workspace_cling_move_item);
            mWorkspaceDesc2 = findViewById(R.id.workspace_cling_open_all_apps);
            mAllAppsDesc = findViewById(R.id.all_apps_cling_add_item);
            mIsInitialized = true;
        }
    }

    void cleanup() {
        mPunchThroughGraphic = null;
    }

    @Override
    public boolean onTouchEvent(android.view.MotionEvent event) {
        // Do nothing
        return true;
    };

    @Override
    protected void dispatchDraw(Canvas canvas) {
        // Draw the rest of the cling
        super.dispatchDraw(canvas);

        if (mIsInitialized) {
            DisplayMetrics metrics = new DisplayMetrics();
            mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int dotRadius = (int) (6f * metrics.density);

            Paint p = new Paint();
            p.setAntiAlias(true);
            p.setColor(0xFF49C0EC);

            if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
                /* Draw the all apps line */ {
                    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
                            mWorkspaceDesc2.getLayoutParams();
                    int[] loc = new int[2];
                    mWorkspaceDesc2.getLocationInWindow(loc);
                    int x = loc[0];
                    int xOffset = (int) (10f * metrics.density);
                    int y = loc[1];
                    int yOffset = (int) (30f * metrics.density);
                    int w = mWorkspaceDesc2.getWidth();
                    int h = mWorkspaceDesc2.getHeight();

                    Point p1 = new Point(x + w + xOffset, y - (2 * dotRadius));
                    Point p2 = new Point(getMeasuredWidth() / 2, getMeasuredHeight() -
                            mAppIconSize / 2 - yOffset);
                    canvas.drawCircle(p1.x, p1.y, dotRadius, p);
                    canvas.drawCircle(p2.x, p2.y, dotRadius, p);

                    Point p3 = new Point(p1.x, (int) (p1.y + (p2.y - p1.y) * 0.30f));
                    Point p4 = new Point(p2.x, (int) (p1.y + (p2.y - p1.y) * 0.55f));
                    canvas.drawLine(p1.x, p1.y, p3.x, p3.y, p);
                    canvas.drawLine(p3.x, p3.y, p4.x, p4.y, p);
                    canvas.drawLine(p4.x, p4.y, p2.x, p2.y, p);
                }

                /* Draw the move line */ {
                    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
                            mWorkspaceDesc1.getLayoutParams();
                    int[] loc = new int[2];
                    mWorkspaceDesc1.getLocationInWindow(loc);
                    int x = loc[0];
                    int y = loc[1];
                    int w = mWorkspaceDesc1.getWidth();
                    int h = mWorkspaceDesc1.getHeight();

                    Point p1 = new Point(x + w, y - (2 * dotRadius));
                    Point p2 = new Point(x + w, getMeasuredHeight() - (4 * mAppIconSize));
                    canvas.drawCircle(p1.x, p1.y, dotRadius, p);
                    canvas.drawCircle(p2.x, p2.y, dotRadius, p);
                    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, p);
                }
            } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
                int xOffset = (int) (1.5f * mAppIconSize);
                /* Draw the all apps line */ {
                    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
                            mWorkspaceDesc2.getLayoutParams();
                    int[] loc = new int[2];
                    mWorkspaceDesc2.getLocationInWindow(loc);
                    int x = loc[0];
                    int y = loc[1];
                    int w = mWorkspaceDesc2.getWidth();
                    int h = mWorkspaceDesc2.getHeight();

                    Point p1 = new Point(x + w, y - (2 * dotRadius));
                    Point p2 = new Point(getMeasuredWidth() - xOffset,
                            getMeasuredHeight() / 2);
                    canvas.drawCircle(p1.x, p1.y, dotRadius, p);
                    canvas.drawCircle(p2.x, p2.y, dotRadius, p);

                    Point p3 = new Point((int) (p1.x + (p2.x - p1.x) * 0.6f), p1.y);
                    Point p4 = new Point((int) (p1.x + (p2.x - p1.x) * 0.75f), p2.y);
                    canvas.drawLine(p1.x, p1.y, p3.x, p3.y, p);
                    canvas.drawLine(p3.x, p3.y, p4.x, p4.y, p);
                    canvas.drawLine(p4.x, p4.y, p2.x, p2.y, p);
                }

                /* Draw the move line */ {
                    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
                            mWorkspaceDesc1.getLayoutParams();
                    int[] loc = new int[2];
                    mWorkspaceDesc1.getLocationInWindow(loc);
                    int x = loc[0];
                    int y = loc[1];
                    int w = mWorkspaceDesc1.getWidth();
                    int h = mWorkspaceDesc1.getHeight();

                    Point p1 = new Point(x + w, y - (2 * dotRadius));
                    Point p2 = new Point(getMeasuredWidth() - xOffset, y - (2 * dotRadius));
                    canvas.drawCircle(p1.x, p1.y, dotRadius, p);
                    canvas.drawCircle(p2.x, p2.y, dotRadius, p);
                    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, p);
                }
            } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT)) {
                float r = mAppIconSize * 1.1f;
                float scale = r / mPunchThroughGraphicCenterRadius;
                int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
                int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
                int cx = getMeasuredWidth() / 2;
                int cy = mTabBarHeight + ((getMeasuredHeight() - mTabBarHeight) / 2);
                mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
                mPunchThroughGraphic.draw(canvas);

                /* Draw the line */ {
                    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
                            mAllAppsDesc.getLayoutParams();
                    int[] loc = new int[2];
                    mAllAppsDesc.getLocationInWindow(loc);
                    int x = loc[0];
                    int y = loc[1];
                    int yOffset = (int) (2.5f * metrics.density);
                    int w = mAllAppsDesc.getWidth();
                    int h = mAllAppsDesc.getHeight();

                    Point p1 = new Point(getMeasuredWidth() / 2, y + h + yOffset);
                    Point p2 = new Point(cx, cy);
                    canvas.drawCircle(p1.x, p1.y, dotRadius, p);
                    canvas.drawCircle(p2.x, p2.y, dotRadius, p);
                    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, p);
                }
            } else if (mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
                float r = mAppIconSize * 1.1f;
                float scale = r / mPunchThroughGraphicCenterRadius;
                int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
                int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
                int cx = getMeasuredWidth() / 2 + getMeasuredWidth() / 4;
                int cy = mTabBarHeight + ((getMeasuredHeight() - mTabBarHeight) / 2);
                mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
                mPunchThroughGraphic.draw(canvas);

                /* Draw the line */ {
                    FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)
                            mAllAppsDesc.getLayoutParams();
                    int[] loc = new int[2];
                    mAllAppsDesc.getLocationInWindow(loc);
                    int x = loc[0];
                    int y = loc[1];
                    int w = mAllAppsDesc.getWidth();
                    int h = mAllAppsDesc.getHeight();

                    Point p1 = new Point(x + w, y);
                    Point p2 = new Point(cx, cy);
                    canvas.drawCircle(p1.x, p1.y, dotRadius, p);
                    canvas.drawCircle(p2.x, p2.y, dotRadius, p);
                    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, p);
                }
            }

            /*
            // Draw the background
            Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
                    Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);
            c.drawColor(0xD4000000);
            Paint p = new Paint();
            p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
            p.setColor(0xFFFFFF);
            p.setAlpha(0);

            int cx = -1;
            int cy = -1;
            float r = mAppIconSize * 1.4f;
            float scale = r / mPunchThroughGraphicCenterRadius;
            int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
            int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());

            if (mDrawIdentifier.equals("workspace_portrait")) {
                cx = getMeasuredWidth() / 2;
                cy = getMeasuredHeight() - mAppIconSize / 2;
            } else if (mDrawIdentifier.equals("workspace_landscape")) {
                cx = getMeasuredWidth() - mAppIconSize / 2;
                cy = getMeasuredHeight() / 2;
            } else if (mDrawIdentifier.equals("large_workspace_landscape") ||
                       mDrawIdentifier.equals("large_workspace_portrait")) {
                cx = getMeasuredWidth() - mTabBarHorizontalPadding;
                cy = 0;
            } else if (mDrawIdentifier.equals("all_apps_portrait")) {
                cx = getMeasuredWidth() / 2;
                cy = mTabBarHeight + ((getMeasuredHeight() - mTabBarHeight) / 2);
            } else if (mDrawIdentifier.equals("all_apps_landscape")) {
                cx = getMeasuredWidth() / 2 + getMeasuredWidth() / 4;
                cy = mTabBarHeight + ((getMeasuredHeight() - mTabBarHeight) / 2);
            } else if (mDrawIdentifier.equals("large_all_apps_portrait")) {
                cx = getMeasuredWidth() / 2;
                cy = mTabBarHeight + (int) ((getMeasuredHeight() - mTabBarHeight) * 2f / 5f);
            } else if (mDrawIdentifier.equals("large_all_apps_landscape")) {
                cx = getMeasuredWidth() / 2 + getMeasuredWidth() / 6;
                cy = mTabBarHeight + (int) ((getMeasuredHeight() - mTabBarHeight) * 2f / 5f);
            }
            if (cx > -1 && cy > -1) {
                c.drawCircle(cx, cy, r, p);
                mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
                mPunchThroughGraphic.draw(c);
            }
            canvas.drawBitmap(b, 0, 0, null);
            c.setBitmap(null);
            b = null;
            */
        }
    };
}