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