blob: 2656ad6a8d6fdd2083be2c95d0a65665d53d60b9 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung82f55532011-08-09 14:14:23 -070018
Winson Chungaf40f202013-09-18 18:26:31 -070019import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
Winson Chung82f55532011-08-09 14:14:23 -070022import android.content.Context;
23import android.content.res.Resources;
24import android.content.res.TypedArray;
Winson Chung7d7541e2011-09-16 20:14:36 -070025import android.graphics.Bitmap;
Winson Chung82f55532011-08-09 14:14:23 -070026import android.graphics.Canvas;
Winson Chunge6eabff2013-09-24 11:01:23 -070027import android.graphics.Color;
Winson Chung82f55532011-08-09 14:14:23 -070028import android.graphics.Paint;
Winson Chung7d7541e2011-09-16 20:14:36 -070029import android.graphics.PorterDuff;
30import android.graphics.PorterDuffXfermode;
31import android.graphics.Rect;
Winson Chung82f55532011-08-09 14:14:23 -070032import android.graphics.drawable.Drawable;
33import android.util.AttributeSet;
34import android.util.DisplayMetrics;
Svetoslav Ganov55d225d2012-05-22 15:19:14 -070035import android.view.FocusFinder;
36import android.view.MotionEvent;
37import android.view.View;
Winson Chungaf40f202013-09-18 18:26:31 -070038import android.view.animation.AccelerateInterpolator;
Winson Chung82f55532011-08-09 14:14:23 -070039import android.widget.FrameLayout;
40
Winson Chungaf40f202013-09-18 18:26:31 -070041public class Cling extends FrameLayout implements Insettable, View.OnLongClickListener {
Winson Chung82f55532011-08-09 14:14:23 -070042
Winson Chungaf40f202013-09-18 18:26:31 -070043 static final String FIRST_RUN_CLING_DISMISSED_KEY = "cling_gel.first_run.dismissed";
44 static final String WORKSPACE_CLING_DISMISSED_KEY = "cling_gel.workspace.dismissed";
45 static final String FOLDER_CLING_DISMISSED_KEY = "cling_gel.folder.dismissed";
46
47 private static String FIRST_RUN_PORTRAIT = "first_run_portrait";
48 private static String FIRST_RUN_LANDSCAPE = "first_run_landscape";
Winson Chung7d7541e2011-09-16 20:14:36 -070049
Winson Chung82f55532011-08-09 14:14:23 -070050 private static String WORKSPACE_PORTRAIT = "workspace_portrait";
51 private static String WORKSPACE_LANDSCAPE = "workspace_landscape";
Andrew Flynn2f5f9452012-05-12 17:00:35 -070052 private static String WORKSPACE_LARGE = "workspace_large";
53 private static String WORKSPACE_CUSTOM = "workspace_custom";
54
Winson Chung7d7541e2011-09-16 20:14:36 -070055 private static String FOLDER_PORTRAIT = "folder_portrait";
56 private static String FOLDER_LANDSCAPE = "folder_landscape";
Winson Chungd0160152011-11-15 15:04:42 -080057 private static String FOLDER_LARGE = "folder_large";
Winson Chung82f55532011-08-09 14:14:23 -070058
Winson Chunge6eabff2013-09-24 11:01:23 -070059 private static float FIRST_RUN_CIRCLE_BUFFER_DPS = 60;
Winson Chungaf40f202013-09-18 18:26:31 -070060 private static float WORKSPACE_INNER_CIRCLE_RADIUS_DPS = 50;
61 private static float WORKSPACE_OUTER_CIRCLE_RADIUS_DPS = 60;
62 private static float WORKSPACE_CIRCLE_Y_OFFSET_DPS = 30;
63
Winson Chung82f55532011-08-09 14:14:23 -070064 private Launcher mLauncher;
65 private boolean mIsInitialized;
66 private String mDrawIdentifier;
Winson Chung7d7541e2011-09-16 20:14:36 -070067 private Drawable mBackground;
Winson Chung82f55532011-08-09 14:14:23 -070068
Winson Chung7d7541e2011-09-16 20:14:36 -070069 private Paint mErasePaint;
Winson Chungaf40f202013-09-18 18:26:31 -070070 private Paint mBubblePaint;
71 private Paint mDotPaint;
72
Winson Chungfa545132013-09-26 17:45:32 -070073 private View mScrimView;
74 private int mBackgroundColor;
75
Winson Chungaf40f202013-09-18 18:26:31 -070076 private final Rect mInsets = new Rect();
Winson Chung7d7541e2011-09-16 20:14:36 -070077
Winson Chung82f55532011-08-09 14:14:23 -070078 public Cling(Context context) {
79 this(context, null, 0);
80 }
81
82 public Cling(Context context, AttributeSet attrs) {
83 this(context, attrs, 0);
84 }
85
86 public Cling(Context context, AttributeSet attrs, int defStyle) {
87 super(context, attrs, defStyle);
88
89 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cling, defStyle, 0);
90 mDrawIdentifier = a.getString(R.styleable.Cling_drawIdentifier);
91 a.recycle();
Michael Jurka9bc8eba2012-05-21 20:36:44 -070092
93 setClickable(true);
Winson Chungaf40f202013-09-18 18:26:31 -070094
Winson Chung82f55532011-08-09 14:14:23 -070095 }
96
Winson Chungfa545132013-09-26 17:45:32 -070097 void init(Launcher l, View scrim) {
Winson Chung82f55532011-08-09 14:14:23 -070098 if (!mIsInitialized) {
99 mLauncher = l;
Winson Chungfa545132013-09-26 17:45:32 -0700100 mScrimView = scrim;
101 mBackgroundColor = 0xdd000000;
Winson Chungaf40f202013-09-18 18:26:31 -0700102 setOnLongClickListener(this);
Winson Chung82f55532011-08-09 14:14:23 -0700103
Winson Chung7d7541e2011-09-16 20:14:36 -0700104 mErasePaint = new Paint();
105 mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
106 mErasePaint.setColor(0xFFFFFF);
107 mErasePaint.setAlpha(0);
Winson Chungaf40f202013-09-18 18:26:31 -0700108 mErasePaint.setAntiAlias(true);
109
Winson Chunge6eabff2013-09-24 11:01:23 -0700110 int circleColor = getResources().getColor(
111 R.color.first_run_cling_circle_background_color);
Winson Chungaf40f202013-09-18 18:26:31 -0700112 mBubblePaint = new Paint();
Winson Chunge6eabff2013-09-24 11:01:23 -0700113 mBubblePaint.setColor(circleColor);
Winson Chungaf40f202013-09-18 18:26:31 -0700114 mBubblePaint.setAntiAlias(true);
115
116 mDotPaint = new Paint();
117 mDotPaint.setColor(0x72BBED);
118 mDotPaint.setAntiAlias(true);
Winson Chung7d7541e2011-09-16 20:14:36 -0700119
Winson Chung82f55532011-08-09 14:14:23 -0700120 mIsInitialized = true;
121 }
122 }
123
Winson Chungaf40f202013-09-18 18:26:31 -0700124 void show(boolean animate, int duration) {
125 setVisibility(View.VISIBLE);
126 setLayerType(View.LAYER_TYPE_HARDWARE, null);
127 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
128 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
129 mDrawIdentifier.equals(WORKSPACE_LARGE) ||
130 mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
131 View content = getContent();
132 content.setAlpha(0f);
133 content.animate()
134 .alpha(1f)
135 .setDuration(duration)
Winson Chungfa545132013-09-26 17:45:32 -0700136 .setListener(null)
Winson Chungaf40f202013-09-18 18:26:31 -0700137 .start();
138 setAlpha(1f);
139 } else {
140 if (animate) {
141 buildLayer();
142 setAlpha(0f);
143 animate()
144 .alpha(1f)
145 .setInterpolator(new AccelerateInterpolator())
146 .setDuration(duration)
Winson Chungfa545132013-09-26 17:45:32 -0700147 .setListener(null)
Winson Chungaf40f202013-09-18 18:26:31 -0700148 .start();
149 } else {
150 setAlpha(1f);
151 }
152 }
Winson Chungfa545132013-09-26 17:45:32 -0700153
154 // Show the scrim if necessary
155 if (mScrimView != null) {
156 mScrimView.setVisibility(View.VISIBLE);
157 mScrimView.setAlpha(0f);
158 mScrimView.animate()
159 .alpha(1f)
160 .setDuration(duration)
161 .setListener(null)
162 .start();
163 }
164
Winson Chungaf40f202013-09-18 18:26:31 -0700165 setFocusableInTouchMode(true);
166 post(new Runnable() {
167 public void run() {
168 setFocusable(true);
169 requestFocus();
170 }
171 });
172 }
173
174 void hide(final int duration, final Runnable postCb) {
175 if (mDrawIdentifier.equals(FIRST_RUN_PORTRAIT) ||
176 mDrawIdentifier.equals(FIRST_RUN_LANDSCAPE)) {
177 View content = getContent();
Winson Chungfa545132013-09-26 17:45:32 -0700178 content.animate()
179 .alpha(0f)
180 .setDuration(duration)
181 .setListener(new AnimatorListenerAdapter() {
182 public void onAnimationEnd(Animator animation) {
183 // We are about to trigger the workspace cling, so don't do anything else
184 setVisibility(View.GONE);
185 postCb.run();
186 };
187 })
188 .start();
Winson Chungaf40f202013-09-18 18:26:31 -0700189 } else {
Winson Chungfa545132013-09-26 17:45:32 -0700190 animate()
191 .alpha(0f)
192 .setDuration(duration)
193 .setListener(new AnimatorListenerAdapter() {
194 public void onAnimationEnd(Animator animation) {
195 // We are about to trigger the workspace cling, so don't do anything else
196 setVisibility(View.GONE);
197 postCb.run();
198 };
199 })
200 .start();
201 }
202
203 // Show the scrim if necessary
204 if (mScrimView != null) {
205 mScrimView.animate()
206 .alpha(0f)
207 .setDuration(duration)
208 .setListener(new AnimatorListenerAdapter() {
209 public void onAnimationEnd(Animator animation) {
210 mScrimView.setVisibility(View.GONE);
211 };
212 })
213 .start();
Winson Chungaf40f202013-09-18 18:26:31 -0700214 }
215 }
216
Winson Chung82f55532011-08-09 14:14:23 -0700217 void cleanup() {
Winson Chung7d7541e2011-09-16 20:14:36 -0700218 mBackground = null;
Winson Chung7d7541e2011-09-16 20:14:36 -0700219 mIsInitialized = false;
220 }
221
Winson Chungfa545132013-09-26 17:45:32 -0700222 void bringScrimToFront() {
223 if (mScrimView != null) {
224 mScrimView.bringToFront();
225 }
226 }
227
Winson Chungaf40f202013-09-18 18:26:31 -0700228 @Override
229 public void setInsets(Rect insets) {
230 mInsets.set(insets);
231 setPadding(insets.left, insets.top, insets.right, insets.bottom);
Michael Jurka974c3862012-05-22 22:00:31 -0700232 }
233
Winson Chungaf40f202013-09-18 18:26:31 -0700234 View getContent() {
235 return findViewById(R.id.content);
236 }
237
238 String getDrawIdentifier() {
239 return mDrawIdentifier;
Winson Chung82f55532011-08-09 14:14:23 -0700240 }
241
242 @Override
Svetoslav Ganov55d225d2012-05-22 15:19:14 -0700243 public View focusSearch(int direction) {
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700244 return this.focusSearch(this, direction);
Svetoslav Ganov55d225d2012-05-22 15:19:14 -0700245 }
246
247 @Override
248 public View focusSearch(View focused, int direction) {
249 return FocusFinder.getInstance().findNextFocus(this, focused, direction);
250 }
251
252 @Override
253 public boolean onHoverEvent(MotionEvent event) {
254 return (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)
255 || mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)
256 || mDrawIdentifier.equals(WORKSPACE_LARGE)
Svetoslav Ganov55d225d2012-05-22 15:19:14 -0700257 || mDrawIdentifier.equals(WORKSPACE_CUSTOM));
258 }
259
260 @Override
Winson Chung82f55532011-08-09 14:14:23 -0700261 public boolean onTouchEvent(android.view.MotionEvent event) {
Winson Chungaf40f202013-09-18 18:26:31 -0700262 if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
Winson Chungd0160152011-11-15 15:04:42 -0800263 mDrawIdentifier.equals(FOLDER_LANDSCAPE) ||
264 mDrawIdentifier.equals(FOLDER_LARGE)) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700265 Folder f = mLauncher.getWorkspace().getOpenFolder();
266 if (f != null) {
267 Rect r = new Rect();
268 f.getHitRect(r);
269 if (r.contains((int) event.getX(), (int) event.getY())) {
270 return false;
271 }
272 }
273 }
Winson Chungaf40f202013-09-18 18:26:31 -0700274 return super.onTouchEvent(event);
Winson Chung82f55532011-08-09 14:14:23 -0700275 };
276
277 @Override
Winson Chungaf40f202013-09-18 18:26:31 -0700278 public boolean onLongClick(View v) {
279 if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
280 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
281 mDrawIdentifier.equals(WORKSPACE_LARGE)) {
282 mLauncher.dismissWorkspaceCling(null);
283 return true;
284 }
285 return false;
286 }
287
288 @Override
Winson Chung82f55532011-08-09 14:14:23 -0700289 protected void dispatchDraw(Canvas canvas) {
Winson Chung82f55532011-08-09 14:14:23 -0700290 if (mIsInitialized) {
Winson Chungaf40f202013-09-18 18:26:31 -0700291 canvas.save();
Winson Chung82f55532011-08-09 14:14:23 -0700292
Winson Chungaf40f202013-09-18 18:26:31 -0700293 // Get the background override if there is one
Winson Chung7d7541e2011-09-16 20:14:36 -0700294 if (mBackground == null) {
Winson Chungaf40f202013-09-18 18:26:31 -0700295 if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) {
Andrew Flynna743d352012-05-18 13:38:52 -0700296 mBackground = getResources().getDrawable(R.drawable.bg_cling5);
Winson Chung7d7541e2011-09-16 20:14:36 -0700297 }
298 }
Winson Chungaf40f202013-09-18 18:26:31 -0700299 // Draw the background
300 Bitmap eraseBg = null;
301 Canvas eraseCanvas = null;
Winson Chungfa545132013-09-26 17:45:32 -0700302 if (mScrimView != null) {
303 // Skip drawing the background
304 mScrimView.setBackgroundColor(mBackgroundColor);
305 } else if (mBackground != null) {
Winson Chung7d7541e2011-09-16 20:14:36 -0700306 mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
Winson Chungaf40f202013-09-18 18:26:31 -0700307 mBackground.draw(canvas);
Winson Chungaf40f202013-09-18 18:26:31 -0700308 } else if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
309 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
310 mDrawIdentifier.equals(WORKSPACE_LARGE)) {
311 // Initialize the draw buffer (to allow punching through)
312 eraseBg = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
313 Bitmap.Config.ARGB_8888);
314 eraseCanvas = new Canvas(eraseBg);
Winson Chungfa545132013-09-26 17:45:32 -0700315 eraseCanvas.drawColor(mBackgroundColor);
Winson Chung7d7541e2011-09-16 20:14:36 -0700316 } else {
Winson Chungfa545132013-09-26 17:45:32 -0700317 canvas.drawColor(mBackgroundColor);
Winson Chung7d7541e2011-09-16 20:14:36 -0700318 }
Winson Chung82f55532011-08-09 14:14:23 -0700319
Winson Chungaf40f202013-09-18 18:26:31 -0700320 // Draw everything else
321 DisplayMetrics metrics = new DisplayMetrics();
322 mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
323 float alpha = getAlpha();
324 View content = getContent();
325 if (content != null) {
326 alpha *= content.getAlpha();
327 }
328 if (mDrawIdentifier.equals(FIRST_RUN_PORTRAIT) ||
329 mDrawIdentifier.equals(FIRST_RUN_LANDSCAPE)) {
330 // Draw the white circle
331 View bubbleContent = findViewById(R.id.bubble_content);
332 Rect bubbleRect = new Rect();
333 bubbleContent.getGlobalVisibleRect(bubbleRect);
334 mBubblePaint.setAlpha((int) (255 * alpha));
335 float buffer = DynamicGrid.pxFromDp(FIRST_RUN_CIRCLE_BUFFER_DPS, metrics);
336 canvas.drawCircle(metrics.widthPixels / 2,
337 bubbleRect.centerY(),
338 (bubbleContent.getMeasuredWidth() + buffer) / 2,
339 mBubblePaint);
340 } else if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
341 mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
342 mDrawIdentifier.equals(WORKSPACE_LARGE)) {
343 int offset = DynamicGrid.pxFromDp(WORKSPACE_CIRCLE_Y_OFFSET_DPS, metrics);
344 mErasePaint.setAlpha((int) (128));
345 eraseCanvas.drawCircle(metrics.widthPixels / 2,
346 metrics.heightPixels / 2 - offset,
347 DynamicGrid.pxFromDp(WORKSPACE_OUTER_CIRCLE_RADIUS_DPS, metrics),
348 mErasePaint);
349 mErasePaint.setAlpha(0);
350 eraseCanvas.drawCircle(metrics.widthPixels / 2,
351 metrics.heightPixels / 2 - offset,
352 DynamicGrid.pxFromDp(WORKSPACE_INNER_CIRCLE_RADIUS_DPS, metrics),
353 mErasePaint);
354 canvas.drawBitmap(eraseBg, 0, 0, null);
355 eraseCanvas.setBitmap(null);
356 eraseBg = null;
Winson Chung82f55532011-08-09 14:14:23 -0700357 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700358
Winson Chung7d7541e2011-09-16 20:14:36 -0700359
Winson Chungaf40f202013-09-18 18:26:31 -0700360 canvas.restore();
Winson Chung82f55532011-08-09 14:14:23 -0700361 }
Winson Chung7d7541e2011-09-16 20:14:36 -0700362
363 // Draw the rest of the cling
364 super.dispatchDraw(canvas);
Winson Chung82f55532011-08-09 14:14:23 -0700365 };
366}