blob: 4f37cb9e0ec500f0ceaff1b5ddbefd26df08def3 [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
65 private View mWorkspaceDesc1;
66 private View mWorkspaceDesc2;
67 private View mAllAppsDesc;
Winson Chung82f55532011-08-09 14:14:23 -070068
69 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();
91 mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
92 mPunchThroughGraphicCenterRadius =
93 r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
94 mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
Winson Chung7d7541e2011-09-16 20:14:36 -070095 mRevealRadius = mAppIconSize * 1f;
Winson Chung82f55532011-08-09 14:14:23 -070096 mTabBarHeight = r.getDimensionPixelSize(R.dimen.apps_customize_tab_bar_height);
97 mTabBarHorizontalPadding =
98 r.getDimensionPixelSize(R.dimen.toolbar_button_horizontal_padding);
Winson Chung7d7541e2011-09-16 20:14:36 -070099 mButtonBarHeight = r.getDimensionPixelSize(R.dimen.button_bar_height);
Winson Chung82f55532011-08-09 14:14:23 -0700100
101 mWorkspaceDesc1 = findViewById(R.id.workspace_cling_move_item);
102 mWorkspaceDesc2 = findViewById(R.id.workspace_cling_open_all_apps);
103 mAllAppsDesc = findViewById(R.id.all_apps_cling_add_item);
Winson Chung7d7541e2011-09-16 20:14:36 -0700104
105 mErasePaint = new Paint();
106 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
107 mErasePaint.setColor(0xFFFFFF);
108 mErasePaint.setAlpha(0);
109
Winson Chung82f55532011-08-09 14:14:23 -0700110 mIsInitialized = true;
111 }
112 }
113
114 void cleanup() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700115 mBackground = null;
Winson Chung82f55532011-08-09 14:14:23 -0700116 mPunchThroughGraphic = null;
Winson Chung7d7541e2011-09-16 20:14:36 -0700117 mHandTouchGraphic = null;
118 mIsInitialized = false;
119 }
120
121 private int[] getPunchThroughPosition() {
122 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
123 return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)};
124 } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
125 return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2};
126 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
127 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
128 return mPositionData;
129 }
130 return new int[]{-1, -1};
Winson Chung82f55532011-08-09 14:14:23 -0700131 }
132
133 @Override
134 public boolean onTouchEvent(android.view.MotionEvent event) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700135 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
136 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
137 mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
138 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
139 int[] pos = getPunchThroughPosition();
140 double diff = Math.sqrt(Math.pow(event.getX() - pos[0], 2) +
141 Math.pow(event.getY() - pos[1], 2));
142 if (diff < mRevealRadius) {
143 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
144 // Do nothing
145 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT)) {
146 // Do nothing
147 }
148 return false;
149 }
150 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
151 mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
152 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 }
160 }
Winson Chung82f55532011-08-09 14:14:23 -0700161 return true;
162 };
163
164 @Override
165 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700166 if (mIsInitialized) {
167 DisplayMetrics metrics = new DisplayMetrics();
168 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Winson Chung82f55532011-08-09 14:14:23 -0700169
Winson Chung82f55532011-08-09 14:14:23 -0700170 // Draw the background
171 Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
172 Bitmap.Config.ARGB_8888);
173 Canvas c = new Canvas(b);
Winson Chung7d7541e2011-09-16 20:14:36 -0700174
175 // Draw the background
176 if (mBackground == null) {
177 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
178 mBackground = getResources().getDrawable(R.drawable.bg_cling1);
179 } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT)) {
180 mBackground = getResources().getDrawable(R.drawable.bg_cling2);
181 } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT)) {
182 mBackground = getResources().getDrawable(R.drawable.bg_cling3);
183 }
184 }
185 if (mBackground != null) {
186 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
187 mBackground.draw(c);
188 } else {
189 c.drawColor(0x99000000);
190 }
Winson Chung82f55532011-08-09 14:14:23 -0700191
192 int cx = -1;
193 int cy = -1;
Winson Chung7d7541e2011-09-16 20:14:36 -0700194 float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
Winson Chung82f55532011-08-09 14:14:23 -0700195 int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
196 int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
197
Winson Chung7d7541e2011-09-16 20:14:36 -0700198 // Determine where to draw the punch through graphic
199 int[] pos = getPunchThroughPosition();
200 cx = pos[0];
201 cy = pos[1];
Winson Chung82f55532011-08-09 14:14:23 -0700202 if (cx > -1 && cy > -1) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700203 c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
Winson Chung82f55532011-08-09 14:14:23 -0700204 mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
205 mPunchThroughGraphic.draw(c);
206 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700207
208 // Draw the hand graphic in All Apps
209 if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
210 mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
211 if (mHandTouchGraphic == null) {
212 mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
213 }
214 int offset = -mAppIconSize / 4;
215 mHandTouchGraphic.setBounds(cx + offset, cy + offset,
216 cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
217 cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
218 mHandTouchGraphic.draw(c);
219 }
220
Winson Chung82f55532011-08-09 14:14:23 -0700221 canvas.drawBitmap(b, 0, 0, null);
222 c.setBitmap(null);
223 b = null;
Winson Chung82f55532011-08-09 14:14:23 -0700224 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700225
226 // Draw the rest of the cling
227 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700228 };
229}