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 | |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 118 | private int[] getPunchThroughPositions() { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 119 | if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) { |
| 120 | return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)}; |
| 121 | } else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) { |
| 122 | return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2}; |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 123 | } else if (mDrawIdentifier.equals(WORKSPACE_LARGE)) { |
| 124 | final float scale = LauncherApplication.getScreenDensity(); |
| 125 | final int cornerXOffset = (int) (scale * 15); |
| 126 | final int cornerYOffset = (int) (scale * 10); |
| 127 | return new int[]{getMeasuredWidth() - cornerXOffset, cornerYOffset}; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 128 | } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 129 | mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) || |
| 130 | mDrawIdentifier.equals(ALLAPPS_LARGE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 131 | return mPositionData; |
| 132 | } |
| 133 | return new int[]{-1, -1}; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
Svetoslav Ganov | 55d225d | 2012-05-22 15:19:14 -0700 | [diff] [blame^] | 137 | public View findViewToTakeAccessibilityFocusFromHover(View child, View descendant) { |
| 138 | if (descendant.includeForAccessibility()) { |
| 139 | return descendant; |
| 140 | } |
| 141 | return null; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public View focusSearch(int direction) { |
| 146 | return this.focusSearch(null, direction); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public View focusSearch(View focused, int direction) { |
| 151 | return FocusFinder.getInstance().findNextFocus(this, focused, direction); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public boolean onHoverEvent(MotionEvent event) { |
| 156 | return (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) |
| 157 | || mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) |
| 158 | || mDrawIdentifier.equals(WORKSPACE_LARGE) |
| 159 | || mDrawIdentifier.equals(ALLAPPS_PORTRAIT) |
| 160 | || mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) |
| 161 | || mDrawIdentifier.equals(ALLAPPS_LARGE) |
| 162 | || mDrawIdentifier.equals(WORKSPACE_CUSTOM)); |
| 163 | } |
| 164 | |
| 165 | @Override |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 166 | public boolean onTouchEvent(android.view.MotionEvent event) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 167 | if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) || |
| 168 | mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 169 | mDrawIdentifier.equals(WORKSPACE_LARGE) || |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 170 | mDrawIdentifier.equals(ALLAPPS_PORTRAIT) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 171 | mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) || |
| 172 | mDrawIdentifier.equals(ALLAPPS_LARGE)) { |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 173 | |
| 174 | int[] positions = getPunchThroughPositions(); |
| 175 | for (int i = 0; i < positions.length; i += 2) { |
| 176 | double diff = Math.sqrt(Math.pow(event.getX() - positions[i], 2) + |
| 177 | Math.pow(event.getY() - positions[i + 1], 2)); |
| 178 | if (diff < mRevealRadius) { |
| 179 | return false; |
| 180 | } |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 181 | } |
| 182 | } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 183 | mDrawIdentifier.equals(FOLDER_LANDSCAPE) || |
| 184 | mDrawIdentifier.equals(FOLDER_LARGE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 185 | Folder f = mLauncher.getWorkspace().getOpenFolder(); |
| 186 | if (f != null) { |
| 187 | Rect r = new Rect(); |
| 188 | f.getHitRect(r); |
| 189 | if (r.contains((int) event.getX(), (int) event.getY())) { |
| 190 | return false; |
| 191 | } |
| 192 | } |
Andrew Flynn | d5e9734 | 2012-05-17 13:37:52 -0700 | [diff] [blame] | 193 | } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) { |
| 194 | // Let all touch events fall through |
| 195 | return false; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 196 | } |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 197 | return true; |
| 198 | }; |
| 199 | |
| 200 | @Override |
| 201 | protected void dispatchDraw(Canvas canvas) { |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 202 | if (mIsInitialized) { |
| 203 | DisplayMetrics metrics = new DisplayMetrics(); |
| 204 | mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics); |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 205 | |
Winson Chung | 9d9d74f | 2011-09-19 11:49:12 -0700 | [diff] [blame] | 206 | // Initialize the draw buffer (to allow punching through) |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 207 | Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), |
| 208 | Bitmap.Config.ARGB_8888); |
| 209 | Canvas c = new Canvas(b); |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 210 | |
| 211 | // Draw the background |
| 212 | if (mBackground == null) { |
Winson Chung | 5966da2 | 2011-09-28 16:49:47 -0700 | [diff] [blame] | 213 | if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) || |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 214 | mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) || |
Andrew Flynn | d5e9734 | 2012-05-17 13:37:52 -0700 | [diff] [blame] | 215 | mDrawIdentifier.equals(WORKSPACE_LARGE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 216 | mBackground = getResources().getDrawable(R.drawable.bg_cling1); |
Winson Chung | 5966da2 | 2011-09-28 16:49:47 -0700 | [diff] [blame] | 217 | } else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 218 | mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) || |
| 219 | mDrawIdentifier.equals(ALLAPPS_LARGE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 220 | mBackground = getResources().getDrawable(R.drawable.bg_cling2); |
Winson Chung | 5966da2 | 2011-09-28 16:49:47 -0700 | [diff] [blame] | 221 | } else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) || |
Andrew Flynn | a743d35 | 2012-05-18 13:38:52 -0700 | [diff] [blame] | 222 | mDrawIdentifier.equals(FOLDER_LANDSCAPE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 223 | mBackground = getResources().getDrawable(R.drawable.bg_cling3); |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 224 | } else if (mDrawIdentifier.equals(FOLDER_LARGE)) { |
| 225 | mBackground = getResources().getDrawable(R.drawable.bg_cling4); |
Andrew Flynn | a743d35 | 2012-05-18 13:38:52 -0700 | [diff] [blame] | 226 | } else if (mDrawIdentifier.equals(WORKSPACE_CUSTOM)) { |
| 227 | mBackground = getResources().getDrawable(R.drawable.bg_cling5); |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | if (mBackground != null) { |
| 231 | mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight()); |
| 232 | mBackground.draw(c); |
| 233 | } else { |
| 234 | c.drawColor(0x99000000); |
| 235 | } |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 236 | |
| 237 | int cx = -1; |
| 238 | int cy = -1; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 239 | float scale = mRevealRadius / mPunchThroughGraphicCenterRadius; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 240 | int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth()); |
| 241 | int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight()); |
| 242 | |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 243 | // Determine where to draw the punch through graphic |
Andrew Flynn | 2f5f945 | 2012-05-12 17:00:35 -0700 | [diff] [blame] | 244 | int[] positions = getPunchThroughPositions(); |
| 245 | for (int i = 0; i < positions.length; i += 2) { |
| 246 | cx = positions[i]; |
| 247 | cy = positions[i + 1]; |
| 248 | if (cx > -1 && cy > -1) { |
| 249 | c.drawCircle(cx, cy, mRevealRadius, mErasePaint); |
| 250 | mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2); |
| 251 | mPunchThroughGraphic.draw(c); |
| 252 | } |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 253 | } |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 254 | |
| 255 | // Draw the hand graphic in All Apps |
| 256 | if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) || |
Winson Chung | d016015 | 2011-11-15 15:04:42 -0800 | [diff] [blame] | 257 | mDrawIdentifier.equals(ALLAPPS_LANDSCAPE) || |
| 258 | mDrawIdentifier.equals(ALLAPPS_LARGE)) { |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 259 | if (mHandTouchGraphic == null) { |
| 260 | mHandTouchGraphic = getResources().getDrawable(R.drawable.hand); |
| 261 | } |
Winson Chung | 7a74ac9 | 2011-09-20 17:43:51 -0700 | [diff] [blame] | 262 | int offset = mAppIconSize / 4; |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 263 | mHandTouchGraphic.setBounds(cx + offset, cy + offset, |
| 264 | cx + mHandTouchGraphic.getIntrinsicWidth() + offset, |
| 265 | cy + mHandTouchGraphic.getIntrinsicHeight() + offset); |
| 266 | mHandTouchGraphic.draw(c); |
| 267 | } |
| 268 | |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 269 | canvas.drawBitmap(b, 0, 0, null); |
| 270 | c.setBitmap(null); |
| 271 | b = null; |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 272 | } |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 273 | |
| 274 | // Draw the rest of the cling |
| 275 | super.dispatchDraw(canvas); |
Winson Chung | 82f5553 | 2011-08-09 14:14:23 -0700 | [diff] [blame] | 276 | }; |
| 277 | } |