blob: c83ee2fde568d3c3d9b2e6b0360684a6cd6ba2c7 [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;
34import android.view.accessibility.AccessibilityManager;
Winson Chung82f55532011-08-09 14:14:23 -070035import android.widget.FrameLayout;
36
37import com.android.launcher.R;
38
39public class Cling extends FrameLayout {
40
Winson Chung7d7541e2011-09-16 20:14:36 -070041 static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
42 static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
43 static final String FOLDER_CLING_DISMISSED_KEY = "cling.folder.dismissed";
44
Winson Chung82f55532011-08-09 14:14:23 -070045 private static String WORKSPACE_PORTRAIT = "workspace_portrait";
46 private static String WORKSPACE_LANDSCAPE = "workspace_landscape";
Andrew Flynn2f5f9452012-05-12 17:00:35 -070047 private static String WORKSPACE_LARGE = "workspace_large";
48 private static String WORKSPACE_CUSTOM = "workspace_custom";
49
Winson Chung82f55532011-08-09 14:14:23 -070050 private static String ALLAPPS_PORTRAIT = "all_apps_portrait";
51 private static String ALLAPPS_LANDSCAPE = "all_apps_landscape";
Andrew Flynn2f5f9452012-05-12 17:00:35 -070052 private static String ALLAPPS_LARGE = "all_apps_large";
53
Winson Chung7d7541e2011-09-16 20:14:36 -070054 private static String FOLDER_PORTRAIT = "folder_portrait";
55 private static String FOLDER_LANDSCAPE = "folder_landscape";
Winson Chungd0160152011-11-15 15:04:42 -080056 private static String FOLDER_LARGE = "folder_large";
Winson Chung82f55532011-08-09 14:14:23 -070057
58 private Launcher mLauncher;
59 private boolean mIsInitialized;
60 private String mDrawIdentifier;
Winson Chung7d7541e2011-09-16 20:14:36 -070061 private Drawable mBackground;
Winson Chung82f55532011-08-09 14:14:23 -070062 private Drawable mPunchThroughGraphic;
Winson Chung7d7541e2011-09-16 20:14:36 -070063 private Drawable mHandTouchGraphic;
Winson Chung82f55532011-08-09 14:14:23 -070064 private int mPunchThroughGraphicCenterRadius;
65 private int mAppIconSize;
Winson Chung7d7541e2011-09-16 20:14:36 -070066 private int mButtonBarHeight;
67 private float mRevealRadius;
68 private int[] mPositionData;
Winson Chung82f55532011-08-09 14:14:23 -070069
Winson Chung7d7541e2011-09-16 20:14:36 -070070 private Paint mErasePaint;
71
Winson Chung82f55532011-08-09 14:14:23 -070072 public Cling(Context context) {
73 this(context, null, 0);
74 }
75
76 public Cling(Context context, AttributeSet attrs) {
77 this(context, attrs, 0);
78 }
79
80 public Cling(Context context, AttributeSet attrs, int defStyle) {
81 super(context, attrs, defStyle);
82
83 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cling, defStyle, 0);
84 mDrawIdentifier = a.getString(R.styleable.Cling_drawIdentifier);
85 a.recycle();
86 }
87
Winson Chung7d7541e2011-09-16 20:14:36 -070088 void init(Launcher l, int[] positionData) {
Winson Chung82f55532011-08-09 14:14:23 -070089 if (!mIsInitialized) {
90 mLauncher = l;
Winson Chung7d7541e2011-09-16 20:14:36 -070091 mPositionData = positionData;
Winson Chung82f55532011-08-09 14:14:23 -070092
93 Resources r = getContext().getResources();
Andrew Flynn2f5f9452012-05-12 17:00:35 -070094
Winson Chung82f55532011-08-09 14:14:23 -070095 mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
96 mPunchThroughGraphicCenterRadius =
97 r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
98 mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
Andrew Flynn2f5f9452012-05-12 17:00:35 -070099 mRevealRadius = r.getDimensionPixelSize(R.dimen.reveal_radius) * 1f;
Winson Chung7d7541e2011-09-16 20:14:36 -0700100 mButtonBarHeight = r.getDimensionPixelSize(R.dimen.button_bar_height);
Winson Chung82f55532011-08-09 14:14:23 -0700101
Winson Chung7d7541e2011-09-16 20:14:36 -0700102 mErasePaint = new Paint();
103 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
104 mErasePaint.setColor(0xFFFFFF);
105 mErasePaint.setAlpha(0);
106
Winson Chung82f55532011-08-09 14:14:23 -0700107 mIsInitialized = true;
108 }
109 }
110
111 void cleanup() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700112 mBackground = null;
Winson Chung82f55532011-08-09 14:14:23 -0700113 mPunchThroughGraphic = null;
Winson Chung7d7541e2011-09-16 20:14:36 -0700114 mHandTouchGraphic = null;
115 mIsInitialized = false;
116 }
117
Michael Jurka974c3862012-05-22 22:00:31 -0700118 public String getDrawIdentifier() {
119 return mDrawIdentifier;
120 }
121
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700122 private int[] getPunchThroughPositions() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700123 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
124 return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)};
125 } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
126 return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2};
Winson Chungd0160152011-11-15 15:04:42 -0800127 } else if (mDrawIdentifier.equals(WORKSPACE_LARGE)) {
128 final float scale = LauncherApplication.getScreenDensity();
129 final int cornerXOffset = (int) (scale * 15);
130 final int cornerYOffset = (int) (scale * 10);
131 return new int[]{getMeasuredWidth() - cornerXOffset, cornerYOffset};
Winson Chung7d7541e2011-09-16 20:14:36 -0700132 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800133 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
134 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700135 return mPositionData;
136 }
137 return new int[]{-1, -1};
Winson Chung82f55532011-08-09 14:14:23 -0700138 }
139
140 @Override
Svetoslav Ganov55d225d2012-05-22 15:19:14 -0700141 public View findViewToTakeAccessibilityFocusFromHover(View child, View descendant) {
142 if (descendant.includeForAccessibility()) {
143 return descendant;
144 }
145 return null;
146 }
147
148 @Override
149 public View focusSearch(int direction) {
150 return this.focusSearch(null, direction);
151 }
152
153 @Override
154 public View focusSearch(View focused, int direction) {
155 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
156 }
157
158 @Override
159 public boolean onHoverEvent(MotionEvent event) {
160 return (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)
161 || mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)
162 || mDrawIdentifier.equals(WORKSPACE_LARGE)
163 || mDrawIdentifier.equals(ALLAPPS_PORTRAIT)
164 || mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)
165 || mDrawIdentifier.equals(ALLAPPS_LARGE)
166 || mDrawIdentifier.equals(WORKSPACE_CUSTOM));
167 }
168
169 @Override
Winson Chung82f55532011-08-09 14:14:23 -0700170 public boolean onTouchEvent(android.view.MotionEvent event) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700171 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
172 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Winson Chungd0160152011-11-15 15:04:42 -0800173 mDrawIdentifier.equals(WORKSPACE_LARGE) ||
Winson Chung7d7541e2011-09-16 20:14:36 -0700174 mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800175 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
176 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700177
178 int[] positions = getPunchThroughPositions();
179 for (int i = 0; i < positions.length; i += 2) {
180 double diff = Math.sqrt(Math.pow(event.getX() - positions[i], 2) +
181 Math.pow(event.getY() - positions[i + 1], 2));
182 if (diff < mRevealRadius) {
183 return false;
184 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700185 }
186 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800187 mDrawIdentifier.equals(FOLDER_LANDSCAPE) ||
188 mDrawIdentifier.equals(FOLDER_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700189 Folder f = mLauncher.getWorkspace().getOpenFolder();
190 if (f != null) {
191 Rect r = new Rect();
192 f.getHitRect(r);
193 if (r.contains((int) event.getX(), (int) event.getY())) {
194 return false;
195 }
196 }
Andrew Flynnd5e97342012-05-17 13:37:52 -0700197 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
198 // Let all touch events fall through
199 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -0700200 }
Winson Chung82f55532011-08-09 14:14:23 -0700201 return true;
202 };
203
204 @Override
205 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700206 if (mIsInitialized) {
207 DisplayMetrics metrics = new DisplayMetrics();
208 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Winson Chung82f55532011-08-09 14:14:23 -0700209
Winson Chung9d9d74f2011-09-19 11:49:12 -0700210 // Initialize the draw buffer (to allow punching through)
Winson Chung82f55532011-08-09 14:14:23 -0700211 Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
212 Bitmap.Config.ARGB_8888);
213 Canvas c = new Canvas(b);
Winson Chung7d7541e2011-09-16 20:14:36 -0700214
215 // Draw the background
216 if (mBackground == null) {
Winson Chung5966da22011-09-28 16:49:47 -0700217 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700218 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Andrew Flynnd5e97342012-05-17 13:37:52 -0700219 mDrawIdentifier.equals(WORKSPACE_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700220 mBackground = getResources().getDrawable(R.drawable.bg_cling1);
Winson Chung5966da22011-09-28 16:49:47 -0700221 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800222 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
223 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700224 mBackground = getResources().getDrawable(R.drawable.bg_cling2);
Winson Chung5966da22011-09-28 16:49:47 -0700225 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Andrew Flynna743d352012-05-18 13:38:52 -0700226 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700227 mBackground = getResources().getDrawable(R.drawable.bg_cling3);
Winson Chungd0160152011-11-15 15:04:42 -0800228 } else if (mDrawIdentifier.equals(FOLDER_LARGE)) {
229 mBackground = getResources().getDrawable(R.drawable.bg_cling4);
Andrew Flynna743d352012-05-18 13:38:52 -0700230 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
231 mBackground = getResources().getDrawable(R.drawable.bg_cling5);
Winson Chung7d7541e2011-09-16 20:14:36 -0700232 }
233 }
234 if (mBackground != null) {
235 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
236 mBackground.draw(c);
237 } else {
238 c.drawColor(0x99000000);
239 }
Winson Chung82f55532011-08-09 14:14:23 -0700240
241 int cx = -1;
242 int cy = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700243 float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
Winson Chung82f55532011-08-09 14:14:23 -0700244 int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
245 int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
246
Winson Chung7d7541e2011-09-16 20:14:36 -0700247 // Determine where to draw the punch through graphic
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700248 int[] positions = getPunchThroughPositions();
249 for (int i = 0; i < positions.length; i += 2) {
250 cx = positions[i];
251 cy = positions[i + 1];
252 if (cx > -1 && cy > -1) {
253 c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
254 mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
255 mPunchThroughGraphic.draw(c);
256 }
Winson Chung82f55532011-08-09 14:14:23 -0700257 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700258
259 // Draw the hand graphic in All Apps
260 if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800261 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
262 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700263 if (mHandTouchGraphic == null) {
264 mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
265 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700266 int offset = mAppIconSize / 4;
Winson Chung7d7541e2011-09-16 20:14:36 -0700267 mHandTouchGraphic.setBounds(cx + offset, cy + offset,
268 cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
269 cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
270 mHandTouchGraphic.draw(c);
271 }
272
Winson Chung82f55532011-08-09 14:14:23 -0700273 canvas.drawBitmap(b, 0, 0, null);
274 c.setBitmap(null);
275 b = null;
Winson Chung82f55532011-08-09 14:14:23 -0700276 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700277
278 // Draw the rest of the cling
279 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700280 };
281}