blob: 5cde0b447a0d7332ca954c90458ee5af6045cbe1 [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
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700118 private int[] getPunchThroughPositions() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700119 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
120 return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)};
121 } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
122 return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2};
Winson Chungd0160152011-11-15 15:04:42 -0800123 } else if (mDrawIdentifier.equals(WORKSPACE_LARGE)) {
124 final float scale = LauncherApplication.getScreenDensity();
125 final int cornerXOffset = (int) (scale * 15);
126 final int cornerYOffset = (int) (scale * 10);
127 return new int[]{getMeasuredWidth() - cornerXOffset, cornerYOffset};
Winson Chung7d7541e2011-09-16 20:14:36 -0700128 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800129 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
130 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700131 return mPositionData;
132 }
133 return new int[]{-1, -1};
Winson Chung82f55532011-08-09 14:14:23 -0700134 }
135
136 @Override
Svetoslav Ganov55d225d2012-05-22 15:19:14 -0700137 public View findViewToTakeAccessibilityFocusFromHover(View child, View descendant) {
138 if (descendant.includeForAccessibility()) {
139 return descendant;
140 }
141 return null;
142 }
143
144 @Override
145 public View focusSearch(int direction) {
146 return this.focusSearch(null, direction);
147 }
148
149 @Override
150 public View focusSearch(View focused, int direction) {
151 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
152 }
153
154 @Override
155 public boolean onHoverEvent(MotionEvent event) {
156 return (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)
157 || mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)
158 || mDrawIdentifier.equals(WORKSPACE_LARGE)
159 || mDrawIdentifier.equals(ALLAPPS_PORTRAIT)
160 || mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)
161 || mDrawIdentifier.equals(ALLAPPS_LARGE)
162 || mDrawIdentifier.equals(WORKSPACE_CUSTOM));
163 }
164
165 @Override
Winson Chung82f55532011-08-09 14:14:23 -0700166 public boolean onTouchEvent(android.view.MotionEvent event) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700167 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
168 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Winson Chungd0160152011-11-15 15:04:42 -0800169 mDrawIdentifier.equals(WORKSPACE_LARGE) ||
Winson Chung7d7541e2011-09-16 20:14:36 -0700170 mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800171 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
172 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700173
174 int[] positions = getPunchThroughPositions();
175 for (int i = 0; i < positions.length; i += 2) {
176 double diff = Math.sqrt(Math.pow(event.getX() - positions[i], 2) +
177 Math.pow(event.getY() - positions[i + 1], 2));
178 if (diff < mRevealRadius) {
179 return false;
180 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700181 }
182 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800183 mDrawIdentifier.equals(FOLDER_LANDSCAPE) ||
184 mDrawIdentifier.equals(FOLDER_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700185 Folder f = mLauncher.getWorkspace().getOpenFolder();
186 if (f != null) {
187 Rect r = new Rect();
188 f.getHitRect(r);
189 if (r.contains((int) event.getX(), (int) event.getY())) {
190 return false;
191 }
192 }
Andrew Flynnd5e97342012-05-17 13:37:52 -0700193 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
194 // Let all touch events fall through
195 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -0700196 }
Winson Chung82f55532011-08-09 14:14:23 -0700197 return true;
198 };
199
200 @Override
201 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700202 if (mIsInitialized) {
203 DisplayMetrics metrics = new DisplayMetrics();
204 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Winson Chung82f55532011-08-09 14:14:23 -0700205
Winson Chung9d9d74f2011-09-19 11:49:12 -0700206 // Initialize the draw buffer (to allow punching through)
Winson Chung82f55532011-08-09 14:14:23 -0700207 Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
208 Bitmap.Config.ARGB_8888);
209 Canvas c = new Canvas(b);
Winson Chung7d7541e2011-09-16 20:14:36 -0700210
211 // Draw the background
212 if (mBackground == null) {
Winson Chung5966da22011-09-28 16:49:47 -0700213 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700214 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Andrew Flynnd5e97342012-05-17 13:37:52 -0700215 mDrawIdentifier.equals(WORKSPACE_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700216 mBackground = getResources().getDrawable(R.drawable.bg_cling1);
Winson Chung5966da22011-09-28 16:49:47 -0700217 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800218 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
219 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700220 mBackground = getResources().getDrawable(R.drawable.bg_cling2);
Winson Chung5966da22011-09-28 16:49:47 -0700221 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Andrew Flynna743d352012-05-18 13:38:52 -0700222 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700223 mBackground = getResources().getDrawable(R.drawable.bg_cling3);
Winson Chungd0160152011-11-15 15:04:42 -0800224 } else if (mDrawIdentifier.equals(FOLDER_LARGE)) {
225 mBackground = getResources().getDrawable(R.drawable.bg_cling4);
Andrew Flynna743d352012-05-18 13:38:52 -0700226 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
227 mBackground = getResources().getDrawable(R.drawable.bg_cling5);
Winson Chung7d7541e2011-09-16 20:14:36 -0700228 }
229 }
230 if (mBackground != null) {
231 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
232 mBackground.draw(c);
233 } else {
234 c.drawColor(0x99000000);
235 }
Winson Chung82f55532011-08-09 14:14:23 -0700236
237 int cx = -1;
238 int cy = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700239 float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
Winson Chung82f55532011-08-09 14:14:23 -0700240 int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
241 int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
242
Winson Chung7d7541e2011-09-16 20:14:36 -0700243 // Determine where to draw the punch through graphic
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700244 int[] positions = getPunchThroughPositions();
245 for (int i = 0; i < positions.length; i += 2) {
246 cx = positions[i];
247 cy = positions[i + 1];
248 if (cx > -1 && cy > -1) {
249 c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
250 mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
251 mPunchThroughGraphic.draw(c);
252 }
Winson Chung82f55532011-08-09 14:14:23 -0700253 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700254
255 // Draw the hand graphic in All Apps
256 if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800257 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
258 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700259 if (mHandTouchGraphic == null) {
260 mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
261 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700262 int offset = mAppIconSize / 4;
Winson Chung7d7541e2011-09-16 20:14:36 -0700263 mHandTouchGraphic.setBounds(cx + offset, cy + offset,
264 cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
265 cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
266 mHandTouchGraphic.draw(c);
267 }
268
Winson Chung82f55532011-08-09 14:14:23 -0700269 canvas.drawBitmap(b, 0, 0, null);
270 c.setBitmap(null);
271 b = null;
Winson Chung82f55532011-08-09 14:14:23 -0700272 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700273
274 // Draw the rest of the cling
275 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700276 };
277}