2011-08-09 14:14:23 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher2;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.res.Resources;
|
|
|
|
|
import android.content.res.TypedArray;
|
2011-09-16 20:14:36 -07:00
|
|
|
import android.graphics.Bitmap;
|
2011-08-09 14:14:23 -07:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Paint;
|
2011-09-16 20:14:36 -07:00
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
|
import android.graphics.PorterDuffXfermode;
|
|
|
|
|
import android.graphics.Rect;
|
2011-08-09 14:14:23 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
import android.util.DisplayMetrics;
|
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher.R;
|
|
|
|
|
|
|
|
|
|
public class Cling extends FrameLayout {
|
|
|
|
|
|
2011-09-16 20:14:36 -07:00
|
|
|
static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
|
|
|
|
|
static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
|
|
|
|
|
static final String FOLDER_CLING_DISMISSED_KEY = "cling.folder.dismissed";
|
|
|
|
|
|
2011-08-09 14:14:23 -07:00
|
|
|
private static String WORKSPACE_PORTRAIT = "workspace_portrait";
|
|
|
|
|
private static String WORKSPACE_LANDSCAPE = "workspace_landscape";
|
|
|
|
|
private static String ALLAPPS_PORTRAIT = "all_apps_portrait";
|
|
|
|
|
private static String ALLAPPS_LANDSCAPE = "all_apps_landscape";
|
2011-09-16 20:14:36 -07:00
|
|
|
private static String FOLDER_PORTRAIT = "folder_portrait";
|
|
|
|
|
private static String FOLDER_LANDSCAPE = "folder_landscape";
|
2011-08-09 14:14:23 -07:00
|
|
|
|
|
|
|
|
private Launcher mLauncher;
|
|
|
|
|
private boolean mIsInitialized;
|
|
|
|
|
private String mDrawIdentifier;
|
2011-09-16 20:14:36 -07:00
|
|
|
private Drawable mBackground;
|
2011-08-09 14:14:23 -07:00
|
|
|
private Drawable mPunchThroughGraphic;
|
2011-09-16 20:14:36 -07:00
|
|
|
private Drawable mHandTouchGraphic;
|
2011-08-09 14:14:23 -07:00
|
|
|
private int mPunchThroughGraphicCenterRadius;
|
|
|
|
|
private int mAppIconSize;
|
2011-09-16 20:14:36 -07:00
|
|
|
private int mButtonBarHeight;
|
|
|
|
|
private float mRevealRadius;
|
|
|
|
|
private int[] mPositionData;
|
|
|
|
|
|
|
|
|
|
private Paint mErasePaint;
|
2011-08-09 14:14:23 -07:00
|
|
|
|
|
|
|
|
public Cling(Context context) {
|
|
|
|
|
this(context, null, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Cling(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Cling(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
|
|
|
|
|
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cling, defStyle, 0);
|
|
|
|
|
mDrawIdentifier = a.getString(R.styleable.Cling_drawIdentifier);
|
|
|
|
|
a.recycle();
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-16 20:14:36 -07:00
|
|
|
void init(Launcher l, int[] positionData) {
|
2011-08-09 14:14:23 -07:00
|
|
|
if (!mIsInitialized) {
|
|
|
|
|
mLauncher = l;
|
2011-09-16 20:14:36 -07:00
|
|
|
mPositionData = positionData;
|
2011-08-09 14:14:23 -07:00
|
|
|
|
|
|
|
|
Resources r = getContext().getResources();
|
|
|
|
|
mPunchThroughGraphic = r.getDrawable(R.drawable.cling);
|
|
|
|
|
mPunchThroughGraphicCenterRadius =
|
|
|
|
|
r.getDimensionPixelSize(R.dimen.clingPunchThroughGraphicCenterRadius);
|
|
|
|
|
mAppIconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
|
2011-09-16 20:14:36 -07:00
|
|
|
mRevealRadius = mAppIconSize * 1f;
|
|
|
|
|
mButtonBarHeight = r.getDimensionPixelSize(R.dimen.button_bar_height);
|
2011-08-09 14:14:23 -07:00
|
|
|
|
2011-09-16 20:14:36 -07:00
|
|
|
mErasePaint = new Paint();
|
|
|
|
|
mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
|
|
|
|
|
mErasePaint.setColor(0xFFFFFF);
|
|
|
|
|
mErasePaint.setAlpha(0);
|
|
|
|
|
|
2011-08-09 14:14:23 -07:00
|
|
|
mIsInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void cleanup() {
|
2011-09-16 20:14:36 -07:00
|
|
|
mBackground = null;
|
2011-08-09 14:14:23 -07:00
|
|
|
mPunchThroughGraphic = null;
|
2011-09-16 20:14:36 -07:00
|
|
|
mHandTouchGraphic = null;
|
|
|
|
|
mIsInitialized = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int[] getPunchThroughPosition() {
|
|
|
|
|
if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT)) {
|
|
|
|
|
return new int[]{getMeasuredWidth() / 2, getMeasuredHeight() - (mButtonBarHeight / 2)};
|
|
|
|
|
} else if (mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
|
|
|
|
|
return new int[]{getMeasuredWidth() - (mButtonBarHeight / 2), getMeasuredHeight() / 2};
|
|
|
|
|
} else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
|
|
|
|
|
mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
|
|
|
|
|
return mPositionData;
|
|
|
|
|
}
|
|
|
|
|
return new int[]{-1, -1};
|
2011-08-09 14:14:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onTouchEvent(android.view.MotionEvent event) {
|
2011-09-16 20:14:36 -07:00
|
|
|
if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
|
|
|
|
|
mDrawIdentifier.equals(WORKSPACE_LANDSCAPE) ||
|
|
|
|
|
mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
|
|
|
|
|
mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
|
|
|
|
|
int[] pos = getPunchThroughPosition();
|
|
|
|
|
double diff = Math.sqrt(Math.pow(event.getX() - pos[0], 2) +
|
|
|
|
|
Math.pow(event.getY() - pos[1], 2));
|
|
|
|
|
if (diff < mRevealRadius) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
|
|
|
|
|
mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
|
|
|
|
|
Folder f = mLauncher.getWorkspace().getOpenFolder();
|
|
|
|
|
if (f != null) {
|
|
|
|
|
Rect r = new Rect();
|
|
|
|
|
f.getHitRect(r);
|
|
|
|
|
if (r.contains((int) event.getX(), (int) event.getY())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-08-09 14:14:23 -07:00
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void dispatchDraw(Canvas canvas) {
|
|
|
|
|
if (mIsInitialized) {
|
|
|
|
|
DisplayMetrics metrics = new DisplayMetrics();
|
|
|
|
|
mLauncher.getWindowManager().getDefaultDisplay().getMetrics(metrics);
|
|
|
|
|
|
2011-09-19 11:49:12 -07:00
|
|
|
// Initialize the draw buffer (to allow punching through)
|
2011-08-09 14:14:23 -07:00
|
|
|
Bitmap b = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(),
|
|
|
|
|
Bitmap.Config.ARGB_8888);
|
|
|
|
|
Canvas c = new Canvas(b);
|
2011-09-16 20:14:36 -07:00
|
|
|
|
|
|
|
|
// Draw the background
|
|
|
|
|
if (mBackground == null) {
|
2011-09-28 16:49:47 -07:00
|
|
|
if (mDrawIdentifier.equals(WORKSPACE_PORTRAIT) ||
|
|
|
|
|
mDrawIdentifier.equals(WORKSPACE_LANDSCAPE)) {
|
2011-09-16 20:14:36 -07:00
|
|
|
mBackground = getResources().getDrawable(R.drawable.bg_cling1);
|
2011-09-28 16:49:47 -07:00
|
|
|
} else if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
|
|
|
|
|
mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
|
2011-09-16 20:14:36 -07:00
|
|
|
mBackground = getResources().getDrawable(R.drawable.bg_cling2);
|
2011-09-28 16:49:47 -07:00
|
|
|
} else if (mDrawIdentifier.equals(FOLDER_PORTRAIT) ||
|
|
|
|
|
mDrawIdentifier.equals(FOLDER_LANDSCAPE)) {
|
2011-09-16 20:14:36 -07:00
|
|
|
mBackground = getResources().getDrawable(R.drawable.bg_cling3);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (mBackground != null) {
|
|
|
|
|
mBackground.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight());
|
|
|
|
|
mBackground.draw(c);
|
|
|
|
|
} else {
|
|
|
|
|
c.drawColor(0x99000000);
|
|
|
|
|
}
|
2011-08-09 14:14:23 -07:00
|
|
|
|
|
|
|
|
int cx = -1;
|
|
|
|
|
int cy = -1;
|
2011-09-16 20:14:36 -07:00
|
|
|
float scale = mRevealRadius / mPunchThroughGraphicCenterRadius;
|
2011-08-09 14:14:23 -07:00
|
|
|
int dw = (int) (scale * mPunchThroughGraphic.getIntrinsicWidth());
|
|
|
|
|
int dh = (int) (scale * mPunchThroughGraphic.getIntrinsicHeight());
|
|
|
|
|
|
2011-09-16 20:14:36 -07:00
|
|
|
// Determine where to draw the punch through graphic
|
|
|
|
|
int[] pos = getPunchThroughPosition();
|
|
|
|
|
cx = pos[0];
|
|
|
|
|
cy = pos[1];
|
2011-08-09 14:14:23 -07:00
|
|
|
if (cx > -1 && cy > -1) {
|
2011-09-16 20:14:36 -07:00
|
|
|
c.drawCircle(cx, cy, mRevealRadius, mErasePaint);
|
2011-08-09 14:14:23 -07:00
|
|
|
mPunchThroughGraphic.setBounds(cx - dw/2, cy - dh/2, cx + dw/2, cy + dh/2);
|
|
|
|
|
mPunchThroughGraphic.draw(c);
|
|
|
|
|
}
|
2011-09-16 20:14:36 -07:00
|
|
|
|
|
|
|
|
// Draw the hand graphic in All Apps
|
|
|
|
|
if (mDrawIdentifier.equals(ALLAPPS_PORTRAIT) ||
|
|
|
|
|
mDrawIdentifier.equals(ALLAPPS_LANDSCAPE)) {
|
|
|
|
|
if (mHandTouchGraphic == null) {
|
|
|
|
|
mHandTouchGraphic = getResources().getDrawable(R.drawable.hand);
|
|
|
|
|
}
|
2011-09-20 17:43:51 -07:00
|
|
|
int offset = mAppIconSize / 4;
|
2011-09-16 20:14:36 -07:00
|
|
|
mHandTouchGraphic.setBounds(cx + offset, cy + offset,
|
|
|
|
|
cx + mHandTouchGraphic.getIntrinsicWidth() + offset,
|
|
|
|
|
cy + mHandTouchGraphic.getIntrinsicHeight() + offset);
|
|
|
|
|
mHandTouchGraphic.draw(c);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-09 14:14:23 -07:00
|
|
|
canvas.drawBitmap(b, 0, 0, null);
|
|
|
|
|
c.setBitmap(null);
|
|
|
|
|
b = null;
|
|
|
|
|
}
|
2011-09-16 20:14:36 -07:00
|
|
|
|
|
|
|
|
// Draw the rest of the cling
|
|
|
|
|
super.dispatchDraw(canvas);
|
2011-08-09 14:14:23 -07:00
|
|
|
};
|
|
|
|
|
}
|