blob: 33cb26f0a3018f835d87ffd0c94ec716b6d92016 [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
17package com.android.launcher2;
18
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
36import com.android.launcher.R;
37
38public class Cling extends FrameLayout {
39
Winson Chung7d7541e2011-09-16 20:14:36 -070040 static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
41 static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
42 static final String FOLDER_CLING_DISMISSED_KEY = "cling.folder.dismissed";
43
Winson Chung82f55532011-08-09 14:14:23 -070044 private static String WORKSPACE_PORTRAIT = "workspace_portrait";
45 private static String WORKSPACE_LANDSCAPE = "workspace_landscape";
Andrew Flynn2f5f9452012-05-12 17:00:35 -070046 private static String WORKSPACE_LARGE = "workspace_large";
47 private static String WORKSPACE_CUSTOM = "workspace_custom";
48
Winson Chung82f55532011-08-09 14:14:23 -070049 private static String ALLAPPS_PORTRAIT = "all_apps_portrait";
50 private static String ALLAPPS_LANDSCAPE = "all_apps_landscape";
Andrew Flynn2f5f9452012-05-12 17:00:35 -070051 private static String ALLAPPS_LARGE = "all_apps_large";
52
Winson Chung7d7541e2011-09-16 20:14:36 -070053 private static String FOLDER_PORTRAIT = "folder_portrait";
54 private static String FOLDER_LANDSCAPE = "folder_landscape";
Winson Chungd0160152011-11-15 15:04:42 -080055 private static String FOLDER_LARGE = "folder_large";
Winson Chung82f55532011-08-09 14:14:23 -070056
57 private Launcher mLauncher;
58 private boolean mIsInitialized;
59 private String mDrawIdentifier;
Winson Chung7d7541e2011-09-16 20:14:36 -070060 private Drawable mBackground;
Winson Chung82f55532011-08-09 14:14:23 -070061 private Drawable mPunchThroughGraphic;
Winson Chung7d7541e2011-09-16 20:14:36 -070062 private Drawable mHandTouchGraphic;
Winson Chung82f55532011-08-09 14:14:23 -070063 private int mPunchThroughGraphicCenterRadius;
64 private int mAppIconSize;
Winson Chung7d7541e2011-09-16 20:14:36 -070065 private int mButtonBarHeight;
66 private float mRevealRadius;
67 private int[] mPositionData;
Winson Chung82f55532011-08-09 14:14:23 -070068
Winson Chung7d7541e2011-09-16 20:14:36 -070069 private Paint mErasePaint;
70
Winson Chung82f55532011-08-09 14:14:23 -070071 public Cling(Context context) {
72 this(context, null, 0);
73 }
74
75 public Cling(Context context, AttributeSet attrs) {
76 this(context, attrs, 0);
77 }
78
79 public Cling(Context context, AttributeSet attrs, int defStyle) {
80 super(context, attrs, defStyle);
81
82 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cling, defStyle, 0);
83 mDrawIdentifier = a.getString(R.styleable.Cling_drawIdentifier);
84 a.recycle();
Michael Jurka9bc8eba2012-05-21 20:36:44 -070085
86 setClickable(true);
Winson Chung82f55532011-08-09 14:14:23 -070087 }
88
Winson Chung7d7541e2011-09-16 20:14:36 -070089 void init(Launcher l, int[] positionData) {
Winson Chung82f55532011-08-09 14:14:23 -070090 if (!mIsInitialized) {
91 mLauncher = l;
Winson Chung7d7541e2011-09-16 20:14:36 -070092 mPositionData = positionData;
Winson Chung82f55532011-08-09 14:14:23 -070093
94 Resources r = getContext().getResources();
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);
99 mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700100 mRevealRadius = r.getDimensionPixelSize(R.dimen.reveal_radius) * 1f;
Winson Chung7d7541e2011-09-16 20:14:36 -0700101 mButtonBarHeight = r.getDimensionPixelSize(R.dimen.button_bar_height);
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)) {
129 final float scale = LauncherApplication.getScreenDensity();
130 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 }
Andrew Flynnd5e97342012-05-17 13:37:52 -0700190 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
191 // Let all touch events fall through
192 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -0700193 }
Winson Chung82f55532011-08-09 14:14:23 -0700194 return true;
195 };
196
197 @Override
198 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700199 if (mIsInitialized) {
200 DisplayMetrics metrics = new DisplayMetrics();
201 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Winson Chung82f55532011-08-09 14:14:23 -0700202
Winson Chung9d9d74f2011-09-19 11:49:12 -0700203 // Initialize the draw buffer (to allow punching through)
Winson Chung82f55532011-08-09 14:14:23 -0700204 Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
205 Bitmap.Config.ARGB_8888);
206 Canvas c = new Canvas(b);
Winson Chung7d7541e2011-09-16 20:14:36 -0700207
208 // Draw the background
209 if (mBackground == null) {
Winson Chung5966da22011-09-28 16:49:47 -0700210 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700211 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Andrew Flynnd5e97342012-05-17 13:37:52 -0700212 mDrawIdentifier.equals(WORKSPACE_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700213 mBackground = getResources().getDrawable(R.drawable.bg_cling1);
Winson Chung5966da22011-09-28 16:49:47 -0700214 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800215 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
216 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700217 mBackground = getResources().getDrawable(R.drawable.bg_cling2);
Winson Chung5966da22011-09-28 16:49:47 -0700218 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Andrew Flynna743d352012-05-18 13:38:52 -0700219 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700220 mBackground = getResources().getDrawable(R.drawable.bg_cling3);
Winson Chungd0160152011-11-15 15:04:42 -0800221 } else if (mDrawIdentifier.equals(FOLDER_LARGE)) {
222 mBackground = getResources().getDrawable(R.drawable.bg_cling4);
Andrew Flynna743d352012-05-18 13:38:52 -0700223 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
224 mBackground = getResources().getDrawable(R.drawable.bg_cling5);
Winson Chung7d7541e2011-09-16 20:14:36 -0700225 }
226 }
227 if (mBackground != null) {
228 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
229 mBackground.draw(c);
230 } else {
231 c.drawColor(0x99000000);
232 }
Winson Chung82f55532011-08-09 14:14:23 -0700233
234 int cx = -1;
235 int cy = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700236 float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
Winson Chung82f55532011-08-09 14:14:23 -0700237 int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
238 int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
239
Winson Chung7d7541e2011-09-16 20:14:36 -0700240 // Determine where to draw the punch through graphic
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700241 int[] positions = getPunchThroughPositions();
242 for (int i = 0; i < positions.length; i += 2) {
243 cx = positions[i];
244 cy = positions[i + 1];
245 if (cx > -1 && cy > -1) {
246 c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
247 mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
248 mPunchThroughGraphic.draw(c);
249 }
Winson Chung82f55532011-08-09 14:14:23 -0700250 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700251
252 // Draw the hand graphic in All Apps
253 if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800254 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
255 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700256 if (mHandTouchGraphic == null) {
257 mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
258 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700259 int offset = mAppIconSize / 4;
Winson Chung7d7541e2011-09-16 20:14:36 -0700260 mHandTouchGraphic.setBounds(cx + offset, cy + offset,
261 cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
262 cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
263 mHandTouchGraphic.draw(c);
264 }
265
Winson Chung82f55532011-08-09 14:14:23 -0700266 canvas.drawBitmap(b, 0, 0, null);
267 c.setBitmap(null);
268 b = null;
Winson Chung82f55532011-08-09 14:14:23 -0700269 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700270
271 // Draw the rest of the cling
272 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700273 };
274}