blob: 0a9bc7fa375dc69414a5eb00c6b0a242e4d4e658 [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;
31import android.view.View;
32import android.widget.FrameLayout;
33
34import com.android.launcher.R;
35
36public class Cling extends FrameLayout {
37
Winson Chung7d7541e2011-09-16 20:14:36 -070038 static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
39 static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
40 static final String FOLDER_CLING_DISMISSED_KEY = "cling.folder.dismissed";
41
Winson Chung82f55532011-08-09 14:14:23 -070042 private static String WORKSPACE_PORTRAIT = "workspace_portrait";
43 private static String WORKSPACE_LANDSCAPE = "workspace_landscape";
44 private static String ALLAPPS_PORTRAIT = "all_apps_portrait";
45 private static String ALLAPPS_LANDSCAPE = "all_apps_landscape";
Winson Chung7d7541e2011-09-16 20:14:36 -070046 private static String FOLDER_PORTRAIT = "folder_portrait";
47 private static String FOLDER_LANDSCAPE = "folder_landscape";
Winson Chung82f55532011-08-09 14:14:23 -070048
49 private Launcher mLauncher;
50 private boolean mIsInitialized;
51 private String mDrawIdentifier;
Winson Chung7d7541e2011-09-16 20:14:36 -070052 private Drawable mBackground;
Winson Chung82f55532011-08-09 14:14:23 -070053 private Drawable mPunchThroughGraphic;
Winson Chung7d7541e2011-09-16 20:14:36 -070054 private Drawable mHandTouchGraphic;
Winson Chung82f55532011-08-09 14:14:23 -070055 private int mPunchThroughGraphicCenterRadius;
56 private int mAppIconSize;
57 private int mTabBarHeight;
58 private int mTabBarHorizontalPadding;
Winson Chung7d7541e2011-09-16 20:14:36 -070059 private int mButtonBarHeight;
60 private float mRevealRadius;
61 private int[] mPositionData;
Winson Chung82f55532011-08-09 14:14:23 -070062
Winson Chung7d7541e2011-09-16 20:14:36 -070063 private Paint mErasePaint;
64
Winson Chung82f55532011-08-09 14:14:23 -070065 public Cling(Context context) {
66 this(context, null, 0);
67 }
68
69 public Cling(Context context, AttributeSet attrs) {
70 this(context, attrs, 0);
71 }
72
73 public Cling(Context context, AttributeSet attrs, int defStyle) {
74 super(context, attrs, defStyle);
75
76 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cling, defStyle, 0);
77 mDrawIdentifier = a.getString(R.styleable.Cling_drawIdentifier);
78 a.recycle();
79 }
80
Winson Chung7d7541e2011-09-16 20:14:36 -070081 void init(Launcher l, int[] positionData) {
Winson Chung82f55532011-08-09 14:14:23 -070082 if (!mIsInitialized) {
83 mLauncher = l;
Winson Chung7d7541e2011-09-16 20:14:36 -070084 mPositionData = positionData;
Winson Chung82f55532011-08-09 14:14:23 -070085
86 Resources r = getContext().getResources();
87 mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
88 mPunchThroughGraphicCenterRadius =
89 r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
90 mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung7d7541e2011-09-16 20:14:36 -070091 mRevealRadius = mAppIconSize * 1f;
Winson Chung82f55532011-08-09 14:14:23 -070092 mTabBarHeight = r.getDimensionPixelSize(R.dimen.apps_customize_tab_bar_height);
93 mTabBarHorizontalPadding =
94 r.getDimensionPixelSize(R.dimen.toolbar_button_horizontal_padding);
Winson Chung7d7541e2011-09-16 20:14:36 -070095 mButtonBarHeight = r.getDimensionPixelSize(R.dimen.button_bar_height);
Winson Chung82f55532011-08-09 14:14:23 -070096
Winson Chung7d7541e2011-09-16 20:14:36 -070097 mErasePaint = new Paint();
98 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
99 mErasePaint.setColor(0xFFFFFF);
100 mErasePaint.setAlpha(0);
101
Winson Chung82f55532011-08-09 14:14:23 -0700102 mIsInitialized = true;
103 }
104 }
105
106 void cleanup() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700107 mBackground = null;
Winson Chung82f55532011-08-09 14:14:23 -0700108 mPunchThroughGraphic = null;
Winson Chung7d7541e2011-09-16 20:14:36 -0700109 mHandTouchGraphic = null;
110 mIsInitialized = false;
111 }
112
113 private int[] getPunchThroughPosition() {
114 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
115 return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)};
116 } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
117 return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2};
118 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
119 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
120 return mPositionData;
121 }
122 return new int[]{-1, -1};
Winson Chung82f55532011-08-09 14:14:23 -0700123 }
124
125 @Override
126 public boolean onTouchEvent(android.view.MotionEvent event) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700127 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
128 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
129 mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
130 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
131 int[] pos = getPunchThroughPosition();
132 double diff = Math.sqrt(Math.pow(event.getX() - pos[0], 2) +
133 Math.pow(event.getY() - pos[1], 2));
134 if (diff < mRevealRadius) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700135 return false;
136 }
137 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
138 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
139 Folder f = mLauncher.getWorkspace().getOpenFolder();
140 if (f != null) {
141 Rect r = new Rect();
142 f.getHitRect(r);
143 if (r.contains((int) event.getX(), (int) event.getY())) {
144 return false;
145 }
146 }
147 }
Winson Chung82f55532011-08-09 14:14:23 -0700148 return true;
149 };
150
151 @Override
152 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700153 if (mIsInitialized) {
154 DisplayMetrics metrics = new DisplayMetrics();
155 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Winson Chung82f55532011-08-09 14:14:23 -0700156
Winson Chung9d9d74f2011-09-19 11:49:12 -0700157 // Initialize the draw buffer (to allow punching through)
Winson Chung82f55532011-08-09 14:14:23 -0700158 Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
159 Bitmap.Config.ARGB_8888);
160 Canvas c = new Canvas(b);
Winson Chung7d7541e2011-09-16 20:14:36 -0700161
162 // Draw the background
163 if (mBackground == null) {
164 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
165 mBackground = getResources().getDrawable(R.drawable.bg_cling1);
166 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT)) {
167 mBackground = getResources().getDrawable(R.drawable.bg_cling2);
168 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT)) {
169 mBackground = getResources().getDrawable(R.drawable.bg_cling3);
170 }
171 }
172 if (mBackground != null) {
173 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
174 mBackground.draw(c);
175 } else {
176 c.drawColor(0x99000000);
177 }
Winson Chung82f55532011-08-09 14:14:23 -0700178
179 int cx = -1;
180 int cy = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700181 float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
Winson Chung82f55532011-08-09 14:14:23 -0700182 int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
183 int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
184
Winson Chung7d7541e2011-09-16 20:14:36 -0700185 // Determine where to draw the punch through graphic
186 int[] pos = getPunchThroughPosition();
187 cx = pos[0];
188 cy = pos[1];
Winson Chung82f55532011-08-09 14:14:23 -0700189 if (cx > -1 && cy > -1) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700190 c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
Winson Chung82f55532011-08-09 14:14:23 -0700191 mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
192 mPunchThroughGraphic.draw(c);
193 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700194
195 // Draw the hand graphic in All Apps
196 if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
197 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
198 if (mHandTouchGraphic == null) {
199 mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
200 }
Winson Chung7a74ac92011-09-20 17:43:51 -0700201 int offset = mAppIconSize / 4;
Winson Chung7d7541e2011-09-16 20:14:36 -0700202 mHandTouchGraphic.setBounds(cx + offset, cy + offset,
203 cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
204 cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
205 mHandTouchGraphic.draw(c);
206 }
207
Winson Chung82f55532011-08-09 14:14:23 -0700208 canvas.drawBitmap(b, 0, 0, null);
209 c.setBitmap(null);
210 b = null;
Winson Chung82f55532011-08-09 14:14:23 -0700211 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700212
213 // Draw the rest of the cling
214 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700215 };
216}