blob: 6f2d0211608841b7bfe8907eec3693f60dc03da3 [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;
Andrew Flynn2f5f9452012-05-12 17:00:35 -070065 private int[] mCustomPositionData;
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();
83 }
84
Winson Chung7d7541e2011-09-16 20:14:36 -070085 void init(Launcher l, int[] positionData) {
Winson Chung82f55532011-08-09 14:14:23 -070086 if (!mIsInitialized) {
87 mLauncher = l;
Winson Chung7d7541e2011-09-16 20:14:36 -070088 mPositionData = positionData;
Winson Chung82f55532011-08-09 14:14:23 -070089
90 Resources r = getContext().getResources();
Andrew Flynn2f5f9452012-05-12 17:00:35 -070091
92 // If we have custom punch through data from resources
93 TypedArray punchThroughCoords = r.obtainTypedArray(R.array.punch_through_coords);
94
95 if (punchThroughCoords != null) {
96 int len = punchThroughCoords.length();
97 mCustomPositionData = new int[len];
98 for (int i = 0; i < len; i++) {
99 mCustomPositionData[i] = punchThroughCoords.getDimensionPixelSize(i, 0);
100 }
101 }
102
Winson Chung82f55532011-08-09 14:14:23 -0700103 mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
104 mPunchThroughGraphicCenterRadius =
105 r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
106 mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700107 mRevealRadius = r.getDimensionPixelSize(R.dimen.reveal_radius) * 1f;
Winson Chung7d7541e2011-09-16 20:14:36 -0700108 mButtonBarHeight = r.getDimensionPixelSize(R.dimen.button_bar_height);
Winson Chung82f55532011-08-09 14:14:23 -0700109
Winson Chung7d7541e2011-09-16 20:14:36 -0700110 mErasePaint = new Paint();
111 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
112 mErasePaint.setColor(0xFFFFFF);
113 mErasePaint.setAlpha(0);
114
Winson Chung82f55532011-08-09 14:14:23 -0700115 mIsInitialized = true;
116 }
117 }
118
119 void cleanup() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700120 mBackground = null;
Winson Chung82f55532011-08-09 14:14:23 -0700121 mPunchThroughGraphic = null;
Winson Chung7d7541e2011-09-16 20:14:36 -0700122 mHandTouchGraphic = null;
123 mIsInitialized = false;
124 }
125
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700126 private int[] getPunchThroughPositions() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700127 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
128 return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)};
129 } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
130 return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2};
Winson Chungd0160152011-11-15 15:04:42 -0800131 } else if (mDrawIdentifier.equals(WORKSPACE_LARGE)) {
132 final float scale = LauncherApplication.getScreenDensity();
133 final int cornerXOffset = (int) (scale * 15);
134 final int cornerYOffset = (int) (scale * 10);
135 return new int[]{getMeasuredWidth() - cornerXOffset, cornerYOffset};
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700136 } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
137 return mCustomPositionData;
Winson Chung7d7541e2011-09-16 20:14:36 -0700138 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800139 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
140 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700141 return mPositionData;
142 }
143 return new int[]{-1, -1};
Winson Chung82f55532011-08-09 14:14:23 -0700144 }
145
146 @Override
147 public boolean onTouchEvent(android.view.MotionEvent event) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700148 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
149 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
Winson Chungd0160152011-11-15 15:04:42 -0800150 mDrawIdentifier.equals(WORKSPACE_LARGE) ||
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700151 mDrawIdentifier.equals(WORKSPACE_CUSTOM) ||
Winson Chung7d7541e2011-09-16 20:14:36 -0700152 mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800153 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
154 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700155
156 int[] positions = getPunchThroughPositions();
157 for (int i = 0; i < positions.length; i += 2) {
158 double diff = Math.sqrt(Math.pow(event.getX() - positions[i], 2) +
159 Math.pow(event.getY() - positions[i + 1], 2));
160 if (diff < mRevealRadius) {
161 return false;
162 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700163 }
164 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800165 mDrawIdentifier.equals(FOLDER_LANDSCAPE) ||
166 mDrawIdentifier.equals(FOLDER_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700167 Folder f = mLauncher.getWorkspace().getOpenFolder();
168 if (f != null) {
169 Rect r = new Rect();
170 f.getHitRect(r);
171 if (r.contains((int) event.getX(), (int) event.getY())) {
172 return false;
173 }
174 }
175 }
Winson Chung82f55532011-08-09 14:14:23 -0700176 return true;
177 };
178
179 @Override
180 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700181 if (mIsInitialized) {
182 DisplayMetrics metrics = new DisplayMetrics();
183 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Winson Chung82f55532011-08-09 14:14:23 -0700184
Winson Chung9d9d74f2011-09-19 11:49:12 -0700185 // Initialize the draw buffer (to allow punching through)
Winson Chung82f55532011-08-09 14:14:23 -0700186 Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
187 Bitmap.Config.ARGB_8888);
188 Canvas c = new Canvas(b);
Winson Chung7d7541e2011-09-16 20:14:36 -0700189
190 // Draw the background
191 if (mBackground == null) {
Winson Chung5966da22011-09-28 16:49:47 -0700192 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700193 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
194 mDrawIdentifier.equals(WORKSPACE_LARGE) ||
195 mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700196 mBackground = getResources().getDrawable(R.drawable.bg_cling1);
Winson Chung5966da22011-09-28 16:49:47 -0700197 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800198 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
199 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700200 mBackground = getResources().getDrawable(R.drawable.bg_cling2);
Winson Chung5966da22011-09-28 16:49:47 -0700201 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
202 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700203 mBackground = getResources().getDrawable(R.drawable.bg_cling3);
Winson Chungd0160152011-11-15 15:04:42 -0800204 } else if (mDrawIdentifier.equals(FOLDER_LARGE)) {
205 mBackground = getResources().getDrawable(R.drawable.bg_cling4);
Winson Chung7d7541e2011-09-16 20:14:36 -0700206 }
207 }
208 if (mBackground != null) {
209 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
210 mBackground.draw(c);
211 } else {
212 c.drawColor(0x99000000);
213 }
Winson Chung82f55532011-08-09 14:14:23 -0700214
215 int cx = -1;
216 int cy = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700217 float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
Winson Chung82f55532011-08-09 14:14:23 -0700218 int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
219 int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
220
Winson Chung7d7541e2011-09-16 20:14:36 -0700221 // Determine where to draw the punch through graphic
Andrew Flynn2f5f9452012-05-12 17:00:35 -0700222 int[] positions = getPunchThroughPositions();
223 for (int i = 0; i < positions.length; i += 2) {
224 cx = positions[i];
225 cy = positions[i + 1];
226 if (cx > -1 && cy > -1) {
227 c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
228 mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
229 mPunchThroughGraphic.draw(c);
230 }
Winson Chung82f55532011-08-09 14:14:23 -0700231 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700232
233 // Draw the hand graphic in All Apps
234 if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800235 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) ||
236 mDrawIdentifier.equals(ALLAPPS_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700237 if (mHandTouchGraphic == null) {
238 mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
239 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700240 int offset = mAppIconSize / 4;
Winson Chung7d7541e2011-09-16 20:14:36 -0700241 mHandTouchGraphic.setBounds(cx + offset, cy + offset,
242 cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
243 cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
244 mHandTouchGraphic.draw(c);
245 }
246
Winson Chung82f55532011-08-09 14:14:23 -0700247 canvas.drawBitmap(b, 0, 0, null);
248 c.setBitmap(null);
249 b = null;
Winson Chung82f55532011-08-09 14:14:23 -0700250 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700251
252 // Draw the rest of the cling
253 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700254 };
255}