blob: 77c41b799f5b5b1ec2c7a482da68c05d9b3edc42 [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();
Andrew Flynn2f5f9452012-05-12 17:00:35 -070093
Winson Chung82f55532011-08-09 14:14:23 -070094 mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
95 mPunchThroughGraphicCenterRadius =
96 r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
97 mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
Andrew Flynn2f5f9452012-05-12 17:00:35 -070098 mRevealRadius = r.getDimensionPixelSize(R.dimen.reveal_radius) * 1f;
Winson Chung7d7541e2011-09-16 20:14:36 -070099 mButtonBarHeight = r.getDimensionPixelSize(R.dimen.button_bar_height);
Winson Chung82f55532011-08-09 14:14:23 -0700100
Winson Chung7d7541e2011-09-16 20:14:36 -0700101 mErasePaint = new Paint();
102 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
103 mErasePaint.setColor(0xFFFFFF);
104 mErasePaint.setAlpha(0);
105
Winson Chung82f55532011-08-09 14:14:23 -0700106 mIsInitialized = true;
107 }
108 }
109
110 void cleanup() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700111 mBackground = null;
Winson Chung82f55532011-08-09 14:14:23 -0700112 mPunchThroughGraphic = null;
Winson Chung7d7541e2011-09-16 20:14:36 -0700113 mHandTouchGraphic = null;
114 mIsInitialized = false;
115 }
116
Michael Jurka974c3862012-05-22 22:00:31 -0700117 public String getDrawIdentifier() {
118 return mDrawIdentifier;
119 }
120
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700121 private int[] getPunchThroughPositions() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700122 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
123 return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)};
124 } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
125 return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2};
Winson Chungd0160152011-11-15 15:04:42 -0800126 } else if (mDrawIdentifier.equals(WORKSPACE_LARGE)) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400127 final float scale = LauncherAppState.getInstance().getScreenDensity();
Winson Chungd0160152011-11-15 15:04:42 -0800128 final int cornerXOffset = (int) (scale * 15);
129 final int cornerYOffset = (int) (scale * 10);
130 return new int[]{getMeasuredWidth() - cornerXOffset, cornerYOffset};
Winson Chung7d7541e2011-09-16 20:14:36 -0700131 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800132 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
133 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700134 return mPositionData;
135 }
136 return new int[]{-1, -1};
Winson Chung82f55532011-08-09 14:14:23 -0700137 }
138
139 @Override
Svetoslav Ganov55d225d2012-05-22 15:19:14 -0700140 public View focusSearch(int direction) {
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700141 return this.focusSearch(this, direction);
Svetoslav Ganov55d225d2012-05-22 15:19:14 -0700142 }
143
144 @Override
145 public View focusSearch(View focused, int direction) {
146 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
147 }
148
149 @Override
150 public boolean onHoverEvent(MotionEvent event) {
151 return (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)
152 || mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)
153 || mDrawIdentifier.equals(WORKSPACE_LARGE)
154 || mDrawIdentifier.equals(ALLAPPS_PORTRAIT)
155 || mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)
156 || mDrawIdentifier.equals(ALLAPPS_LARGE)
157 || mDrawIdentifier.equals(WORKSPACE_CUSTOM));
158 }
159
160 @Override
Winson Chung82f55532011-08-09 14:14:23 -0700161 public boolean onTouchEvent(android.view.MotionEvent event) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700162 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
163 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Winson Chungd0160152011-11-15 15:04:42 -0800164 mDrawIdentifier.equals(WORKSPACE_LARGE) ||
Winson Chung7d7541e2011-09-16 20:14:36 -0700165 mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800166 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
167 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700168
169 int[] positions = getPunchThroughPositions();
170 for (int i = 0; i < positions.length; i += 2) {
171 double diff = Math.sqrt(Math.pow(event.getX() - positions[i], 2) +
172 Math.pow(event.getY() - positions[i + 1], 2));
173 if (diff < mRevealRadius) {
174 return false;
175 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 }
177 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800178 mDrawIdentifier.equals(FOLDER_LANDSCAPE) ||
179 mDrawIdentifier.equals(FOLDER_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700180 Folder f = mLauncher.getWorkspace().getOpenFolder();
181 if (f != null) {
182 Rect r = new Rect();
183 f.getHitRect(r);
184 if (r.contains((int) event.getX(), (int) event.getY())) {
185 return false;
186 }
187 }
188 }
Winson Chung82f55532011-08-09 14:14:23 -0700189 return true;
190 };
191
192 @Override
193 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700194 if (mIsInitialized) {
195 DisplayMetrics metrics = new DisplayMetrics();
196 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Winson Chung82f55532011-08-09 14:14:23 -0700197
Winson Chung9d9d74f2011-09-19 11:49:12 -0700198 // Initialize the draw buffer (to allow punching through)
Winson Chung82f55532011-08-09 14:14:23 -0700199 Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
200 Bitmap.Config.ARGB_8888);
201 Canvas c = new Canvas(b);
Winson Chung7d7541e2011-09-16 20:14:36 -0700202
203 // Draw the background
204 if (mBackground == null) {
Winson Chung5966da22011-09-28 16:49:47 -0700205 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700206 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Andrew Flynnd5e97342012-05-17 13:37:52 -0700207 mDrawIdentifier.equals(WORKSPACE_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700208 mBackground = getResources().getDrawable(R.drawable.bg_cling1);
Winson Chung5966da22011-09-28 16:49:47 -0700209 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800210 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
211 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700212 mBackground = getResources().getDrawable(R.drawable.bg_cling2);
Winson Chung5966da22011-09-28 16:49:47 -0700213 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Andrew Flynna743d352012-05-18 13:38:52 -0700214 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700215 mBackground = getResources().getDrawable(R.drawable.bg_cling3);
Winson Chungd0160152011-11-15 15:04:42 -0800216 } else if (mDrawIdentifier.equals(FOLDER_LARGE)) {
217 mBackground = getResources().getDrawable(R.drawable.bg_cling4);
Andrew Flynna743d352012-05-18 13:38:52 -0700218 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
219 mBackground = getResources().getDrawable(R.drawable.bg_cling5);
Winson Chung7d7541e2011-09-16 20:14:36 -0700220 }
221 }
222 if (mBackground != null) {
223 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
224 mBackground.draw(c);
225 } else {
226 c.drawColor(0x99000000);
227 }
Winson Chung82f55532011-08-09 14:14:23 -0700228
229 int cx = -1;
230 int cy = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700231 float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
Winson Chung82f55532011-08-09 14:14:23 -0700232 int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
233 int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
234
Winson Chung7d7541e2011-09-16 20:14:36 -0700235 // Determine where to draw the punch through graphic
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700236 int[] positions = getPunchThroughPositions();
237 for (int i = 0; i < positions.length; i += 2) {
238 cx = positions[i];
239 cy = positions[i + 1];
240 if (cx > -1 && cy > -1) {
241 c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
242 mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
243 mPunchThroughGraphic.draw(c);
244 }
Winson Chung82f55532011-08-09 14:14:23 -0700245 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700246
247 // Draw the hand graphic in All Apps
248 if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800249 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
250 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700251 if (mHandTouchGraphic == null) {
252 mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
253 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700254 int offset = mAppIconSize / 4;
Winson Chung7d7541e2011-09-16 20:14:36 -0700255 mHandTouchGraphic.setBounds(cx + offset, cy + offset,
256 cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
257 cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
258 mHandTouchGraphic.draw(c);
259 }
260
Winson Chung82f55532011-08-09 14:14:23 -0700261 canvas.drawBitmap(b, 0, 0, null);
262 c.setBitmap(null);
263 b = null;
Winson Chung82f55532011-08-09 14:14:23 -0700264 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700265
266 // Draw the rest of the cling
267 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700268 };
269}