blob: 32b1de4eddfb4fcd17182a98878c20f634afd4cb [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";
43 private static String ALLAPPS_PORTRAIT = "all_apps_portrait";
44 private static String ALLAPPS_LANDSCAPE = "all_apps_landscape";
Winson Chung7d7541e2011-09-16 20:14:36 -070045 private static String FOLDER_PORTRAIT = "folder_portrait";
46 private static String FOLDER_LANDSCAPE = "folder_landscape";
Winson Chung82f55532011-08-09 14:14:23 -070047
48 private Launcher mLauncher;
49 private boolean mIsInitialized;
50 private String mDrawIdentifier;
Winson Chung7d7541e2011-09-16 20:14:36 -070051 private Drawable mBackground;
Winson Chung82f55532011-08-09 14:14:23 -070052 private Drawable mPunchThroughGraphic;
Winson Chung7d7541e2011-09-16 20:14:36 -070053 private Drawable mHandTouchGraphic;
Winson Chung82f55532011-08-09 14:14:23 -070054 private int mPunchThroughGraphicCenterRadius;
55 private int mAppIconSize;
Winson Chung7d7541e2011-09-16 20:14:36 -070056 private int mButtonBarHeight;
57 private float mRevealRadius;
58 private int[] mPositionData;
Winson Chung82f55532011-08-09 14:14:23 -070059
Winson Chung7d7541e2011-09-16 20:14:36 -070060 private Paint mErasePaint;
61
Winson Chung82f55532011-08-09 14:14:23 -070062 public Cling(Context context) {
63 this(context, null, 0);
64 }
65
66 public Cling(Context context, AttributeSet attrs) {
67 this(context, attrs, 0);
68 }
69
70 public Cling(Context context, AttributeSet attrs, int defStyle) {
71 super(context, attrs, defStyle);
72
73 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cling, defStyle, 0);
74 mDrawIdentifier = a.getString(R.styleable.Cling_drawIdentifier);
75 a.recycle();
76 }
77
Winson Chung7d7541e2011-09-16 20:14:36 -070078 void init(Launcher l, int[] positionData) {
Winson Chung82f55532011-08-09 14:14:23 -070079 if (!mIsInitialized) {
80 mLauncher = l;
Winson Chung7d7541e2011-09-16 20:14:36 -070081 mPositionData = positionData;
Winson Chung82f55532011-08-09 14:14:23 -070082
83 Resources r = getContext().getResources();
84 mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
85 mPunchThroughGraphicCenterRadius =
86 r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
87 mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung7d7541e2011-09-16 20:14:36 -070088 mRevealRadius = mAppIconSize * 1f;
Winson Chung7d7541e2011-09-16 20:14:36 -070089 mButtonBarHeight = r.getDimensionPixelSize(R.dimen.button_bar_height);
Winson Chung82f55532011-08-09 14:14:23 -070090
Winson Chung7d7541e2011-09-16 20:14:36 -070091 mErasePaint = new Paint();
92 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
93 mErasePaint.setColor(0xFFFFFF);
94 mErasePaint.setAlpha(0);
95
Winson Chung82f55532011-08-09 14:14:23 -070096 mIsInitialized = true;
97 }
98 }
99
100 void cleanup() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700101 mBackground = null;
Winson Chung82f55532011-08-09 14:14:23 -0700102 mPunchThroughGraphic = null;
Winson Chung7d7541e2011-09-16 20:14:36 -0700103 mHandTouchGraphic = null;
104 mIsInitialized = false;
105 }
106
107 private int[] getPunchThroughPosition() {
108 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
109 return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)};
110 } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
111 return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2};
112 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
113 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
114 return mPositionData;
115 }
116 return new int[]{-1, -1};
Winson Chung82f55532011-08-09 14:14:23 -0700117 }
118
119 @Override
120 public boolean onTouchEvent(android.view.MotionEvent event) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700121 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
122 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
123 mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
124 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
125 int[] pos = getPunchThroughPosition();
126 double diff = Math.sqrt(Math.pow(event.getX() - pos[0], 2) +
127 Math.pow(event.getY() - pos[1], 2));
128 if (diff < mRevealRadius) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700129 return false;
130 }
131 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
132 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
133 Folder f = mLauncher.getWorkspace().getOpenFolder();
134 if (f != null) {
135 Rect r = new Rect();
136 f.getHitRect(r);
137 if (r.contains((int) event.getX(), (int) event.getY())) {
138 return false;
139 }
140 }
141 }
Winson Chung82f55532011-08-09 14:14:23 -0700142 return true;
143 };
144
145 @Override
146 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700147 if (mIsInitialized) {
148 DisplayMetrics metrics = new DisplayMetrics();
149 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Winson Chung82f55532011-08-09 14:14:23 -0700150
Winson Chung9d9d74f2011-09-19 11:49:12 -0700151 // Initialize the draw buffer (to allow punching through)
Winson Chung82f55532011-08-09 14:14:23 -0700152 Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
153 Bitmap.Config.ARGB_8888);
154 Canvas c = new Canvas(b);
Winson Chung7d7541e2011-09-16 20:14:36 -0700155
156 // Draw the background
157 if (mBackground == null) {
Winson Chung5966da22011-09-28 16:49:47 -0700158 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
159 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700160 mBackground = getResources().getDrawable(R.drawable.bg_cling1);
Winson Chung5966da22011-09-28 16:49:47 -0700161 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
162 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700163 mBackground = getResources().getDrawable(R.drawable.bg_cling2);
Winson Chung5966da22011-09-28 16:49:47 -0700164 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
165 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700166 mBackground = getResources().getDrawable(R.drawable.bg_cling3);
167 }
168 }
169 if (mBackground != null) {
170 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
171 mBackground.draw(c);
172 } else {
173 c.drawColor(0x99000000);
174 }
Winson Chung82f55532011-08-09 14:14:23 -0700175
176 int cx = -1;
177 int cy = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700178 float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
Winson Chung82f55532011-08-09 14:14:23 -0700179 int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
180 int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
181
Winson Chung7d7541e2011-09-16 20:14:36 -0700182 // Determine where to draw the punch through graphic
183 int[] pos = getPunchThroughPosition();
184 cx = pos[0];
185 cy = pos[1];
Winson Chung82f55532011-08-09 14:14:23 -0700186 if (cx > -1 && cy > -1) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700187 c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
Winson Chung82f55532011-08-09 14:14:23 -0700188 mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
189 mPunchThroughGraphic.draw(c);
190 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700191
192 // Draw the hand graphic in All Apps
193 if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
194 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
195 if (mHandTouchGraphic == null) {
196 mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
197 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700198 int offset = mAppIconSize / 4;
Winson Chung7d7541e2011-09-16 20:14:36 -0700199 mHandTouchGraphic.setBounds(cx + offset, cy + offset,
200 cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
201 cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
202 mHandTouchGraphic.draw(c);
203 }
204
Winson Chung82f55532011-08-09 14:14:23 -0700205 canvas.drawBitmap(b, 0, 0, null);
206 c.setBitmap(null);
207 b = null;
Winson Chung82f55532011-08-09 14:14:23 -0700208 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700209
210 // Draw the rest of the cling
211 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700212 };
213}