Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame^] | 17 | package com.android.launcher3; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
| 21 | import android.content.res.TypedArray; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.Paint; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 25 | import android.graphics.PorterDuff; |
| 26 | import android.graphics.PorterDuffXfermode; |
| 27 | import android.graphics.Rect; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.util.AttributeSet; |
| 30 | import android.util.DisplayMetrics; |
Svetoslav Ganov | 55d225d | 2012-05-22 15:19:14 -0700 | [diff] [blame] | 31 | import android.view.FocusFinder; |
| 32 | import android.view.MotionEvent; |
| 33 | import android.view.View; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 34 | import android.widget.FrameLayout; |
| 35 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame^] | 36 | import com.android.launcher3.R; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 37 | |
| 38 | public class Cling extends FrameLayout { |
| 39 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 40 | static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed"; |
| 41 | static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed"; |
| 42 | static final String FOLDER_CLING_DISMISSED_KEY = "cling.folder.dismissed"; |
| 43 | |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 44 | private static String WORKSPACE_PORTRAIT = "workspace_portrait"; |
| 45 | private static String WORKSPACE_LANDSCAPE = "workspace_landscape"; |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 46 | private static String WORKSPACE_LARGE = "workspace_large"; |
| 47 | private static String WORKSPACE_CUSTOM = "workspace_custom"; |
| 48 | |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 49 | private static String ALLAPPS_PORTRAIT = "all_apps_portrait"; |
| 50 | private static String ALLAPPS_LANDSCAPE = "all_apps_landscape"; |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 51 | private static String ALLAPPS_LARGE = "all_apps_large"; |
| 52 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 53 | private static String FOLDER_PORTRAIT = "folder_portrait"; |
| 54 | private static String FOLDER_LANDSCAPE = "folder_landscape"; |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 55 | private static String FOLDER_LARGE = "folder_large"; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 56 | |
| 57 | private Launcher mLauncher; |
| 58 | private boolean mIsInitialized; |
| 59 | private String mDrawIdentifier; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 60 | private Drawable mBackground; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 61 | private Drawable mPunchThroughGraphic; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 62 | private Drawable mHandTouchGraphic; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 63 | private int mPunchThroughGraphicCenterRadius; |
| 64 | private int mAppIconSize; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 65 | private int mButtonBarHeight; |
| 66 | private float mRevealRadius; |
| 67 | private int[] mPositionData; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 68 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 69 | private Paint mErasePaint; |
| 70 | |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 71 | public Cling(Context context) { |
| 72 | this(context, null, 0); |
| 73 | } |
| 74 | |
| 75 | public Cling(Context context, AttributeSet attrs) { |
| 76 | this(context, attrs, 0); |
| 77 | } |
| 78 | |
| 79 | public Cling(Context context, AttributeSet attrs, int defStyle) { |
| 80 | super(context, attrs, defStyle); |
| 81 | |
| 82 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cling, defStyle, 0); |
| 83 | mDrawIdentifier = a.getString(R.styleable.Cling_drawIdentifier); |
| 84 | a.recycle(); |
Michael Jurka | 9bc8eba | 2012-05-21 20:36:44 -0700 | [diff] [blame] | 85 | |
| 86 | setClickable(true); |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 89 | void init(Launcher l, int[] positionData) { |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 90 | if (!mIsInitialized) { |
| 91 | mLauncher = l; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 92 | mPositionData = positionData; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 93 | |
| 94 | Resources r = getContext().getResources(); |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 95 | |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 96 | mPunchThroughGraphic = r.getDrawable(R.drawable.cling); |
| 97 | mPunchThroughGraphicCenterRadius = |
| 98 | r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius); |
| 99 | mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size); |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 100 | mRevealRadius = r.getDimensionPixelSize(R.dimen.reveal_radius) * 1f; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 101 | mButtonBarHeight = r.getDimensionPixelSize(R.dimen.button_bar_height); |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 102 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 103 | mErasePaint = new Paint(); |
| 104 | mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY)); |
| 105 | mErasePaint.setColor(0xFFFFFF); |
| 106 | mErasePaint.setAlpha(0); |
| 107 | |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 108 | mIsInitialized = true; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void cleanup() { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 113 | mBackground = null; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 114 | mPunchThroughGraphic = null; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 115 | mHandTouchGraphic = null; |
| 116 | mIsInitialized = false; |
| 117 | } |
| 118 | |
Michael Jurka | 974c386 | 2012-05-22 22:00:31 -0700 | [diff] [blame] | 119 | public String getDrawIdentifier() { |
| 120 | return mDrawIdentifier; |
| 121 | } |
| 122 | |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 123 | private int[] getPunchThroughPositions() { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 124 | if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) { |
| 125 | return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)}; |
| 126 | } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) { |
| 127 | return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2}; |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 128 | } else if (mDrawIdentifier.equals(WORKSPACE_LARGE)) { |
| 129 | final float scale = LauncherApplication.getScreenDensity(); |
| 130 | final int cornerXOffset = (int) (scale * 15); |
| 131 | final int cornerYOffset = (int) (scale * 10); |
| 132 | return new int[]{getMeasuredWidth() - cornerXOffset, cornerYOffset}; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 133 | } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 134 | mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) || |
| 135 | mDrawIdentifier.equals(ALLAPPS_LARGE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 136 | return mPositionData; |
| 137 | } |
| 138 | return new int[]{-1, -1}; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
Svetoslav Ganov | 55d225d | 2012-05-22 15:19:14 -0700 | [diff] [blame] | 142 | public View focusSearch(int direction) { |
Michael Jurka | 9bc8eba | 2012-05-21 20:36:44 -0700 | [diff] [blame] | 143 | return this.focusSearch(this, direction); |
Svetoslav Ganov | 55d225d | 2012-05-22 15:19:14 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public View focusSearch(View focused, int direction) { |
| 148 | return FocusFinder.getInstance().findNextFocus(this, focused, direction); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public boolean onHoverEvent(MotionEvent event) { |
| 153 | return (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) |
| 154 | || mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) |
| 155 | || mDrawIdentifier.equals(WORKSPACE_LARGE) |
| 156 | || mDrawIdentifier.equals(ALLAPPS_PORTRAIT) |
| 157 | || mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) |
| 158 | || mDrawIdentifier.equals(ALLAPPS_LARGE) |
| 159 | || mDrawIdentifier.equals(WORKSPACE_CUSTOM)); |
| 160 | } |
| 161 | |
| 162 | @Override |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 163 | public boolean onTouchEvent(android.view.MotionEvent event) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 164 | if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) || |
| 165 | mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 166 | mDrawIdentifier.equals(WORKSPACE_LARGE) || |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 167 | mDrawIdentifier.equals(ALLAPPS_PORTRAIT) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 168 | mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) || |
| 169 | mDrawIdentifier.equals(ALLAPPS_LARGE)) { |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 170 | |
| 171 | int[] positions = getPunchThroughPositions(); |
| 172 | for (int i = 0; i < positions.length; i += 2) { |
| 173 | double diff = Math.sqrt(Math.pow(event.getX() - positions[i], 2) + |
| 174 | Math.pow(event.getY() - positions[i + 1], 2)); |
| 175 | if (diff < mRevealRadius) { |
| 176 | return false; |
| 177 | } |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 178 | } |
| 179 | } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 180 | mDrawIdentifier.equals(FOLDER_LANDSCAPE) || |
| 181 | mDrawIdentifier.equals(FOLDER_LARGE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 182 | Folder f = mLauncher.getWorkspace().getOpenFolder(); |
| 183 | if (f != null) { |
| 184 | Rect r = new Rect(); |
| 185 | f.getHitRect(r); |
| 186 | if (r.contains((int) event.getX(), (int) event.getY())) { |
| 187 | return false; |
| 188 | } |
| 189 | } |
| 190 | } |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 191 | return true; |
| 192 | }; |
| 193 | |
| 194 | @Override |
| 195 | protected void dispatchDraw(Canvas canvas) { |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 196 | if (mIsInitialized) { |
| 197 | DisplayMetrics metrics = new DisplayMetrics(); |
| 198 | mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics); |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 199 | |
Winson Chung | 9d9d74f | 2011-09-19 11:49:12 -0700 | [diff] [blame] | 200 | // Initialize the draw buffer (to allow punching through) |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 201 | Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), |
| 202 | Bitmap.Config.ARGB_8888); |
| 203 | Canvas c = new Canvas(b); |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 204 | |
| 205 | // Draw the background |
| 206 | if (mBackground == null) { |
Winson Chung | 5966da2 | 2011-09-28 16:49:47 -0700 | [diff] [blame] | 207 | if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) || |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 208 | mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) || |
Andrew Flynn | d5e9734 | 2012-05-17 13:37:52 -0700 | [diff] [blame] | 209 | mDrawIdentifier.equals(WORKSPACE_LARGE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 210 | mBackground = getResources().getDrawable(R.drawable.bg_cling1); |
Winson Chung | 5966da2 | 2011-09-28 16:49:47 -0700 | [diff] [blame] | 211 | } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 212 | mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) || |
| 213 | mDrawIdentifier.equals(ALLAPPS_LARGE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 214 | mBackground = getResources().getDrawable(R.drawable.bg_cling2); |
Winson Chung | 5966da2 | 2011-09-28 16:49:47 -0700 | [diff] [blame] | 215 | } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) || |
Andrew Flynn | a743d35 | 2012-05-18 13:38:52 -0700 | [diff] [blame] | 216 | mDrawIdentifier.equals(FOLDER_LANDSCAPE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 217 | mBackground = getResources().getDrawable(R.drawable.bg_cling3); |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 218 | } else if (mDrawIdentifier.equals(FOLDER_LARGE)) { |
| 219 | mBackground = getResources().getDrawable(R.drawable.bg_cling4); |
Andrew Flynn | a743d35 | 2012-05-18 13:38:52 -0700 | [diff] [blame] | 220 | } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) { |
| 221 | mBackground = getResources().getDrawable(R.drawable.bg_cling5); |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | if (mBackground != null) { |
| 225 | mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight()); |
| 226 | mBackground.draw(c); |
| 227 | } else { |
| 228 | c.drawColor(0x99000000); |
| 229 | } |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 230 | |
| 231 | int cx = -1; |
| 232 | int cy = -1; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 233 | float scale = mRevealRadius / mPunchThroughGraphicCenterRadius; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 234 | int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth()); |
| 235 | int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight()); |
| 236 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 237 | // Determine where to draw the punch through graphic |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 238 | int[] positions = getPunchThroughPositions(); |
| 239 | for (int i = 0; i < positions.length; i += 2) { |
| 240 | cx = positions[i]; |
| 241 | cy = positions[i + 1]; |
| 242 | if (cx > -1 && cy > -1) { |
| 243 | c.drawCircle(cx, cy, mRevealRadius, mErasePaint); |
| 244 | mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2); |
| 245 | mPunchThroughGraphic.draw(c); |
| 246 | } |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 247 | } |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 248 | |
| 249 | // Draw the hand graphic in All Apps |
| 250 | if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 251 | mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) || |
| 252 | mDrawIdentifier.equals(ALLAPPS_LARGE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 253 | if (mHandTouchGraphic == null) { |
| 254 | mHandTouchGraphic = getResources().getDrawable(R.drawable.hand); |
| 255 | } |
Winson Chung | 7a74ac9 | 2011-09-20 17:43:51 -0700 | [diff] [blame] | 256 | int offset = mAppIconSize / 4; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 257 | mHandTouchGraphic.setBounds(cx + offset, cy + offset, |
| 258 | cx + mHandTouchGraphic.getIntrinsicWidth() + offset, |
| 259 | cy + mHandTouchGraphic.getIntrinsicHeight() + offset); |
| 260 | mHandTouchGraphic.draw(c); |
| 261 | } |
| 262 | |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 263 | canvas.drawBitmap(b, 0, 0, null); |
| 264 | c.setBitmap(null); |
| 265 | b = null; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 266 | } |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 267 | |
| 268 | // Draw the rest of the cling |
| 269 | super.dispatchDraw(canvas); |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 270 | }; |
| 271 | } |