blob: d61b9883a408c793685ded6f51c57d758c18bd88 [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;
Winson Chung82f55532011-08-09 14:14:23 -070031import android.widget.FrameLayout;
32
33import com.android.launcher.R;
34
35public class Cling extends FrameLayout {
36
Winson Chung7d7541e2011-09-16 20:14:36 -070037 static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
38 static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
39 static final String FOLDER_CLING_DISMISSED_KEY = "cling.folder.dismissed";
40
Winson Chung82f55532011-08-09 14:14:23 -070041 private static String WORKSPACE_PORTRAIT = "workspace_portrait";
42 private static String WORKSPACE_LANDSCAPE = "workspace_landscape";
Andrew Flynn2f5f9452012-05-12 17:00:35 -070043 private static String WORKSPACE_LARGE = "workspace_large";
44 private static String WORKSPACE_CUSTOM = "workspace_custom";
45
Winson Chung82f55532011-08-09 14:14:23 -070046 private static String ALLAPPS_PORTRAIT = "all_apps_portrait";
47 private static String ALLAPPS_LANDSCAPE = "all_apps_landscape";
Andrew Flynn2f5f9452012-05-12 17:00:35 -070048 private static String ALLAPPS_LARGE = "all_apps_large";
49
Winson Chung7d7541e2011-09-16 20:14:36 -070050 private static String FOLDER_PORTRAIT = "folder_portrait";
51 private static String FOLDER_LANDSCAPE = "folder_landscape";
Winson Chungd0160152011-11-15 15:04:42 -080052 private static String FOLDER_LARGE = "folder_large";
Winson Chung82f55532011-08-09 14:14:23 -070053
54 private Launcher mLauncher;
55 private boolean mIsInitialized;
56 private String mDrawIdentifier;
Winson Chung7d7541e2011-09-16 20:14:36 -070057 private Drawable mBackground;
Winson Chung82f55532011-08-09 14:14:23 -070058 private Drawable mPunchThroughGraphic;
Winson Chung7d7541e2011-09-16 20:14:36 -070059 private Drawable mHandTouchGraphic;
Winson Chung82f55532011-08-09 14:14:23 -070060 private int mPunchThroughGraphicCenterRadius;
61 private int mAppIconSize;
Winson Chung7d7541e2011-09-16 20:14:36 -070062 private int mButtonBarHeight;
63 private float mRevealRadius;
64 private int[] mPositionData;
Winson Chung82f55532011-08-09 14:14:23 -070065
Winson Chung7d7541e2011-09-16 20:14:36 -070066 private Paint mErasePaint;
67
Winson Chung82f55532011-08-09 14:14:23 -070068 public Cling(Context context) {
69 this(context, null, 0);
70 }
71
72 public Cling(Context context, AttributeSet attrs) {
73 this(context, attrs, 0);
74 }
75
76 public Cling(Context context, AttributeSet attrs, int defStyle) {
77 super(context, attrs, defStyle);
78
79 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cling, defStyle, 0);
80 mDrawIdentifier = a.getString(R.styleable.Cling_drawIdentifier);
81 a.recycle();
82 }
83
Winson Chung7d7541e2011-09-16 20:14:36 -070084 void init(Launcher l, int[] positionData) {
Winson Chung82f55532011-08-09 14:14:23 -070085 if (!mIsInitialized) {
86 mLauncher = l;
Winson Chung7d7541e2011-09-16 20:14:36 -070087 mPositionData = positionData;
Winson Chung82f55532011-08-09 14:14:23 -070088
89 Resources r = getContext().getResources();
Andrew Flynn2f5f9452012-05-12 17:00:35 -070090
Winson Chung82f55532011-08-09 14:14:23 -070091 mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
92 mPunchThroughGraphicCenterRadius =
93 r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
94 mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
Andrew Flynn2f5f9452012-05-12 17:00:35 -070095 mRevealRadius = r.getDimensionPixelSize(R.dimen.reveal_radius) * 1f;
Winson Chung7d7541e2011-09-16 20:14:36 -070096 mButtonBarHeight = r.getDimensionPixelSize(R.dimen.button_bar_height);
Winson Chung82f55532011-08-09 14:14:23 -070097
Winson Chung7d7541e2011-09-16 20:14:36 -070098 mErasePaint = new Paint();
99 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
100 mErasePaint.setColor(0xFFFFFF);
101 mErasePaint.setAlpha(0);
102
Winson Chung82f55532011-08-09 14:14:23 -0700103 mIsInitialized = true;
104 }
105 }
106
107 void cleanup() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700108 mBackground = null;
Winson Chung82f55532011-08-09 14:14:23 -0700109 mPunchThroughGraphic = null;
Winson Chung7d7541e2011-09-16 20:14:36 -0700110 mHandTouchGraphic = null;
111 mIsInitialized = false;
112 }
113
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700114 private int[] getPunchThroughPositions() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700115 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
116 return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)};
117 } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
118 return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2};
Winson Chungd0160152011-11-15 15:04:42 -0800119 } else if (mDrawIdentifier.equals(WORKSPACE_LARGE)) {
120 final float scale = LauncherApplication.getScreenDensity();
121 final int cornerXOffset = (int) (scale * 15);
122 final int cornerYOffset = (int) (scale * 10);
123 return new int[]{getMeasuredWidth() - cornerXOffset, cornerYOffset};
Winson Chung7d7541e2011-09-16 20:14:36 -0700124 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800125 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
126 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700127 return mPositionData;
128 }
129 return new int[]{-1, -1};
Winson Chung82f55532011-08-09 14:14:23 -0700130 }
131
132 @Override
133 public boolean onTouchEvent(android.view.MotionEvent event) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700134 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
135 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Winson Chungd0160152011-11-15 15:04:42 -0800136 mDrawIdentifier.equals(WORKSPACE_LARGE) ||
Winson Chung7d7541e2011-09-16 20:14:36 -0700137 mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800138 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
139 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700140
141 int[] positions = getPunchThroughPositions();
142 for (int i = 0; i < positions.length; i += 2) {
143 double diff = Math.sqrt(Math.pow(event.getX() - positions[i], 2) +
144 Math.pow(event.getY() - positions[i + 1], 2));
145 if (diff < mRevealRadius) {
146 return false;
147 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700148 }
149 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800150 mDrawIdentifier.equals(FOLDER_LANDSCAPE) ||
151 mDrawIdentifier.equals(FOLDER_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700152 Folder f = mLauncher.getWorkspace().getOpenFolder();
153 if (f != null) {
154 Rect r = new Rect();
155 f.getHitRect(r);
156 if (r.contains((int) event.getX(), (int) event.getY())) {
157 return false;
158 }
159 }
Andrew Flynnd5e97342012-05-17 13:37:52 -0700160 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
161 // Let all touch events fall through
162 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -0700163 }
Winson Chung82f55532011-08-09 14:14:23 -0700164 return true;
165 };
166
167 @Override
168 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700169 if (mIsInitialized) {
170 DisplayMetrics metrics = new DisplayMetrics();
171 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Winson Chung82f55532011-08-09 14:14:23 -0700172
Winson Chung9d9d74f2011-09-19 11:49:12 -0700173 // Initialize the draw buffer (to allow punching through)
Winson Chung82f55532011-08-09 14:14:23 -0700174 Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
175 Bitmap.Config.ARGB_8888);
176 Canvas c = new Canvas(b);
Winson Chung7d7541e2011-09-16 20:14:36 -0700177
178 // Draw the background
179 if (mBackground == null) {
Winson Chung5966da22011-09-28 16:49:47 -0700180 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700181 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Andrew Flynnd5e97342012-05-17 13:37:52 -0700182 mDrawIdentifier.equals(WORKSPACE_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700183 mBackground = getResources().getDrawable(R.drawable.bg_cling1);
Winson Chung5966da22011-09-28 16:49:47 -0700184 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800185 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
186 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700187 mBackground = getResources().getDrawable(R.drawable.bg_cling2);
Winson Chung5966da22011-09-28 16:49:47 -0700188 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Andrew Flynna743d352012-05-18 13:38:52 -0700189 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700190 mBackground = getResources().getDrawable(R.drawable.bg_cling3);
Winson Chungd0160152011-11-15 15:04:42 -0800191 } else if (mDrawIdentifier.equals(FOLDER_LARGE)) {
192 mBackground = getResources().getDrawable(R.drawable.bg_cling4);
Andrew Flynna743d352012-05-18 13:38:52 -0700193 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
194 mBackground = getResources().getDrawable(R.drawable.bg_cling5);
Winson Chung7d7541e2011-09-16 20:14:36 -0700195 }
196 }
197 if (mBackground != null) {
198 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
199 mBackground.draw(c);
200 } else {
201 c.drawColor(0x99000000);
202 }
Winson Chung82f55532011-08-09 14:14:23 -0700203
204 int cx = -1;
205 int cy = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700206 float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
Winson Chung82f55532011-08-09 14:14:23 -0700207 int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
208 int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
209
Winson Chung7d7541e2011-09-16 20:14:36 -0700210 // Determine where to draw the punch through graphic
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700211 int[] positions = getPunchThroughPositions();
212 for (int i = 0; i < positions.length; i += 2) {
213 cx = positions[i];
214 cy = positions[i + 1];
215 if (cx > -1 && cy > -1) {
216 c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
217 mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
218 mPunchThroughGraphic.draw(c);
219 }
Winson Chung82f55532011-08-09 14:14:23 -0700220 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700221
222 // Draw the hand graphic in All Apps
223 if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800224 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
225 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700226 if (mHandTouchGraphic == null) {
227 mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
228 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700229 int offset = mAppIconSize / 4;
Winson Chung7d7541e2011-09-16 20:14:36 -0700230 mHandTouchGraphic.setBounds(cx + offset, cy + offset,
231 cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
232 cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
233 mHandTouchGraphic.draw(c);
234 }
235
Winson Chung82f55532011-08-09 14:14:23 -0700236 canvas.drawBitmap(b, 0, 0, null);
237 c.setBitmap(null);
238 b = null;
Winson Chung82f55532011-08-09 14:14:23 -0700239 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700240
241 // Draw the rest of the cling
242 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700243 };
244}