blob: add6da51d9d25ef54454700fdc46117f77b9d638 [file] [log] [blame]
Winson Chung82f55532011-08-09 14:14:23 -07001/*
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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung82f55532011-08-09 14:14:23 -070018
19import android.content.Context;
20import android.content.res.Resources;
21import android.content.res.TypedArray;
Winson Chung7d7541e2011-09-16 20:14:36 -070022import android.graphics.Bitmap;
Winson Chung82f55532011-08-09 14:14:23 -070023import android.graphics.Canvas;
24import android.graphics.Paint;
Winson Chung7d7541e2011-09-16 20:14:36 -070025import android.graphics.PorterDuff;
26import android.graphics.PorterDuffXfermode;
27import android.graphics.Rect;
Winson Chung82f55532011-08-09 14:14:23 -070028import android.graphics.drawable.Drawable;
29import android.util.AttributeSet;
30import android.util.DisplayMetrics;
Svetoslav Ganov55d225d2012-05-22 15:19:14 -070031import android.view.FocusFinder;
32import android.view.MotionEvent;
33import android.view.View;
Winson Chung82f55532011-08-09 14:14:23 -070034import android.widget.FrameLayout;
35
Winson Chung82f55532011-08-09 14:14:23 -070036public class Cling extends FrameLayout {
37
Winson Chung7d7541e2011-09-16 20:14:36 -070038 static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
39 static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
40 static final String FOLDER_CLING_DISMISSED_KEY = "cling.folder.dismissed";
41
Winson Chung82f55532011-08-09 14:14:23 -070042 private static String WORKSPACE_PORTRAIT = "workspace_portrait";
43 private static String WORKSPACE_LANDSCAPE = "workspace_landscape";
Andrew Flynn2f5f9452012-05-12 17:00:35 -070044 private static String WORKSPACE_LARGE = "workspace_large";
45 private static String WORKSPACE_CUSTOM = "workspace_custom";
46
Winson Chung82f55532011-08-09 14:14:23 -070047 private static String ALLAPPS_PORTRAIT = "all_apps_portrait";
48 private static String ALLAPPS_LANDSCAPE = "all_apps_landscape";
Andrew Flynn2f5f9452012-05-12 17:00:35 -070049 private static String ALLAPPS_LARGE = "all_apps_large";
50
Winson Chung7d7541e2011-09-16 20:14:36 -070051 private static String FOLDER_PORTRAIT = "folder_portrait";
52 private static String FOLDER_LANDSCAPE = "folder_landscape";
Winson Chungd0160152011-11-15 15:04:42 -080053 private static String FOLDER_LARGE = "folder_large";
Winson Chung82f55532011-08-09 14:14:23 -070054
55 private Launcher mLauncher;
56 private boolean mIsInitialized;
57 private String mDrawIdentifier;
Winson Chung7d7541e2011-09-16 20:14:36 -070058 private Drawable mBackground;
Winson Chung82f55532011-08-09 14:14:23 -070059 private Drawable mPunchThroughGraphic;
Winson Chung7d7541e2011-09-16 20:14:36 -070060 private Drawable mHandTouchGraphic;
Winson Chung82f55532011-08-09 14:14:23 -070061 private int mPunchThroughGraphicCenterRadius;
62 private int mAppIconSize;
Winson Chung7d7541e2011-09-16 20:14:36 -070063 private int mButtonBarHeight;
64 private float mRevealRadius;
65 private int[] mPositionData;
Winson Chung82f55532011-08-09 14:14:23 -070066
Winson Chung7d7541e2011-09-16 20:14:36 -070067 private Paint mErasePaint;
68
Winson Chung82f55532011-08-09 14:14:23 -070069 public Cling(Context context) {
70 this(context, null, 0);
71 }
72
73 public Cling(Context context, AttributeSet attrs) {
74 this(context, attrs, 0);
75 }
76
77 public Cling(Context context, AttributeSet attrs, int defStyle) {
78 super(context, attrs, defStyle);
79
80 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cling, defStyle, 0);
81 mDrawIdentifier = a.getString(R.styleable.Cling_drawIdentifier);
82 a.recycle();
Michael Jurka9bc8eba2012-05-21 20:36:44 -070083
84 setClickable(true);
Winson Chung82f55532011-08-09 14:14:23 -070085 }
86
Winson Chung7d7541e2011-09-16 20:14:36 -070087 void init(Launcher l, int[] positionData) {
Winson Chung82f55532011-08-09 14:14:23 -070088 if (!mIsInitialized) {
89 mLauncher = l;
Winson Chung7d7541e2011-09-16 20:14:36 -070090 mPositionData = positionData;
Winson Chung82f55532011-08-09 14:14:23 -070091
92 Resources r = getContext().getResources();
Winson Chung5f8afe62013-08-12 16:19:28 -070093 LauncherAppState app = LauncherAppState.getInstance();
94 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
Andrew Flynn2f5f9452012-05-12 17:00:35 -070095
Winson Chung82f55532011-08-09 14:14:23 -070096 mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
97 mPunchThroughGraphicCenterRadius =
98 r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
Winson Chung5f8afe62013-08-12 16:19:28 -070099 mAppIconSize = grid.iconSizePx;
100 mRevealRadius = grid.iconSizePx * 1f;
101 mButtonBarHeight = grid.hotseatBarHeightPx;
Winson Chung82f55532011-08-09 14:14:23 -0700102
Winson Chung7d7541e2011-09-16 20:14:36 -0700103 mErasePaint = new Paint();
104 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
105 mErasePaint.setColor(0xFFFFFF);
106 mErasePaint.setAlpha(0);
107
Winson Chung82f55532011-08-09 14:14:23 -0700108 mIsInitialized = true;
109 }
110 }
111
112 void cleanup() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700113 mBackground = null;
Winson Chung82f55532011-08-09 14:14:23 -0700114 mPunchThroughGraphic = null;
Winson Chung7d7541e2011-09-16 20:14:36 -0700115 mHandTouchGraphic = null;
116 mIsInitialized = false;
117 }
118
Michael Jurka974c3862012-05-22 22:00:31 -0700119 public String getDrawIdentifier() {
120 return mDrawIdentifier;
121 }
122
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700123 private int[] getPunchThroughPositions() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700124 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
125 return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)};
126 } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
127 return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2};
Winson Chungd0160152011-11-15 15:04:42 -0800128 } else if (mDrawIdentifier.equals(WORKSPACE_LARGE)) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400129 final float scale = LauncherAppState.getInstance().getScreenDensity();
Winson Chungd0160152011-11-15 15:04:42 -0800130 final int cornerXOffset = (int) (scale * 15);
131 final int cornerYOffset = (int) (scale * 10);
132 return new int[]{getMeasuredWidth() - cornerXOffset, cornerYOffset};
Winson Chung7d7541e2011-09-16 20:14:36 -0700133 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800134 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
135 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700136 return mPositionData;
137 }
138 return new int[]{-1, -1};
Winson Chung82f55532011-08-09 14:14:23 -0700139 }
140
141 @Override
Svetoslav Ganov55d225d2012-05-22 15:19:14 -0700142 public View focusSearch(int direction) {
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700143 return this.focusSearch(this, direction);
Svetoslav Ganov55d225d2012-05-22 15:19:14 -0700144 }
145
146 @Override
147 public View focusSearch(View focused, int direction) {
148 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
149 }
150
151 @Override
152 public boolean onHoverEvent(MotionEvent event) {
153 return (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)
154 || mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)
155 || mDrawIdentifier.equals(WORKSPACE_LARGE)
156 || mDrawIdentifier.equals(ALLAPPS_PORTRAIT)
157 || mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)
158 || mDrawIdentifier.equals(ALLAPPS_LARGE)
159 || mDrawIdentifier.equals(WORKSPACE_CUSTOM));
160 }
161
162 @Override
Winson Chung82f55532011-08-09 14:14:23 -0700163 public boolean onTouchEvent(android.view.MotionEvent event) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700164 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
165 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Winson Chungd0160152011-11-15 15:04:42 -0800166 mDrawIdentifier.equals(WORKSPACE_LARGE) ||
Winson Chung7d7541e2011-09-16 20:14:36 -0700167 mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800168 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
169 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700170
171 int[] positions = getPunchThroughPositions();
172 for (int i = 0; i < positions.length; i += 2) {
173 double diff = Math.sqrt(Math.pow(event.getX() - positions[i], 2) +
174 Math.pow(event.getY() - positions[i + 1], 2));
175 if (diff < mRevealRadius) {
176 return false;
177 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700178 }
179 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800180 mDrawIdentifier.equals(FOLDER_LANDSCAPE) ||
181 mDrawIdentifier.equals(FOLDER_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700182 Folder f = mLauncher.getWorkspace().getOpenFolder();
183 if (f != null) {
184 Rect r = new Rect();
185 f.getHitRect(r);
186 if (r.contains((int) event.getX(), (int) event.getY())) {
187 return false;
188 }
189 }
190 }
Winson Chung82f55532011-08-09 14:14:23 -0700191 return true;
192 };
193
194 @Override
195 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700196 if (mIsInitialized) {
197 DisplayMetrics metrics = new DisplayMetrics();
198 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Winson Chung82f55532011-08-09 14:14:23 -0700199
Winson Chung9d9d74f2011-09-19 11:49:12 -0700200 // Initialize the draw buffer (to allow punching through)
Winson Chung82f55532011-08-09 14:14:23 -0700201 Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
202 Bitmap.Config.ARGB_8888);
203 Canvas c = new Canvas(b);
Winson Chung7d7541e2011-09-16 20:14:36 -0700204
205 // Draw the background
206 if (mBackground == null) {
Winson Chung5966da22011-09-28 16:49:47 -0700207 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700208 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Andrew Flynnd5e97342012-05-17 13:37:52 -0700209 mDrawIdentifier.equals(WORKSPACE_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700210 mBackground = getResources().getDrawable(R.drawable.bg_cling1);
Winson Chung5966da22011-09-28 16:49:47 -0700211 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800212 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
213 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700214 mBackground = getResources().getDrawable(R.drawable.bg_cling2);
Winson Chung5966da22011-09-28 16:49:47 -0700215 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Andrew Flynna743d352012-05-18 13:38:52 -0700216 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700217 mBackground = getResources().getDrawable(R.drawable.bg_cling3);
Winson Chungd0160152011-11-15 15:04:42 -0800218 } else if (mDrawIdentifier.equals(FOLDER_LARGE)) {
219 mBackground = getResources().getDrawable(R.drawable.bg_cling4);
Andrew Flynna743d352012-05-18 13:38:52 -0700220 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
221 mBackground = getResources().getDrawable(R.drawable.bg_cling5);
Winson Chung7d7541e2011-09-16 20:14:36 -0700222 }
223 }
224 if (mBackground != null) {
225 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
226 mBackground.draw(c);
227 } else {
228 c.drawColor(0x99000000);
229 }
Winson Chung82f55532011-08-09 14:14:23 -0700230
231 int cx = -1;
232 int cy = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700233 float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
Winson Chung82f55532011-08-09 14:14:23 -0700234 int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
235 int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
236
Winson Chung7d7541e2011-09-16 20:14:36 -0700237 // Determine where to draw the punch through graphic
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700238 int[] positions = getPunchThroughPositions();
239 for (int i = 0; i < positions.length; i += 2) {
240 cx = positions[i];
241 cy = positions[i + 1];
242 if (cx > -1 && cy > -1) {
243 c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
244 mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
245 mPunchThroughGraphic.draw(c);
246 }
Winson Chung82f55532011-08-09 14:14:23 -0700247 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700248
249 // Draw the hand graphic in All Apps
250 if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800251 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
252 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700253 if (mHandTouchGraphic == null) {
254 mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
255 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700256 int offset = mAppIconSize / 4;
Winson Chung7d7541e2011-09-16 20:14:36 -0700257 mHandTouchGraphic.setBounds(cx + offset, cy + offset,
258 cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
259 cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
260 mHandTouchGraphic.draw(c);
261 }
262
Winson Chung82f55532011-08-09 14:14:23 -0700263 canvas.drawBitmap(b, 0, 0, null);
264 c.setBitmap(null);
265 b = null;
Winson Chung82f55532011-08-09 14:14:23 -0700266 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700267
268 // Draw the rest of the cling
269 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700270 };
271}