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