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