2010-12-01 15:47:31 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010 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;
|
|
|
|
|
|
2010-12-21 11:31:54 -08:00
|
|
|
import android.animation.ObjectAnimator;
|
2010-12-01 15:47:31 -08:00
|
|
|
import android.appwidget.AppWidgetProviderInfo;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.content.pm.ResolveInfo;
|
2010-12-21 11:31:54 -08:00
|
|
|
import android.content.res.Resources;
|
2010-12-01 15:47:31 -08:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Paint;
|
2011-02-24 18:09:44 -08:00
|
|
|
import android.graphics.RectF;
|
2010-12-01 15:47:31 -08:00
|
|
|
import android.util.AttributeSet;
|
2011-04-13 11:27:36 -07:00
|
|
|
import android.view.KeyEvent;
|
2010-12-01 15:47:31 -08:00
|
|
|
import android.view.MotionEvent;
|
2010-12-21 11:31:54 -08:00
|
|
|
import android.widget.Checkable;
|
2010-12-01 15:47:31 -08:00
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher.R;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The linear layout used strictly for the widget/wallpaper tab of the customization tray
|
|
|
|
|
*/
|
2010-12-21 11:31:54 -08:00
|
|
|
public class PagedViewWidget extends LinearLayout implements Checkable {
|
2010-12-01 15:47:31 -08:00
|
|
|
static final String TAG = "PagedViewWidgetLayout";
|
|
|
|
|
|
2011-09-30 14:41:25 -07:00
|
|
|
private static final int sPreviewFadeInDuration = 80;
|
|
|
|
|
private static final int sPreviewFadeInStaggerDuration = 20;
|
|
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
private final Paint mPaint = new Paint();
|
|
|
|
|
private Bitmap mHolographicOutline;
|
2011-06-13 11:32:14 -07:00
|
|
|
private HolographicOutlineHelper mHolographicOutlineHelper;
|
2011-02-24 18:09:44 -08:00
|
|
|
private ImageView mPreviewImageView;
|
|
|
|
|
private final RectF mTmpScaleRect = new RectF();
|
2010-12-01 15:47:31 -08:00
|
|
|
|
2011-05-05 17:06:13 -07:00
|
|
|
private String mDimensionsFormatString;
|
2011-01-05 13:54:43 -08:00
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
private int mAlpha = 255;
|
|
|
|
|
private int mHolographicAlpha;
|
|
|
|
|
|
2010-12-21 11:31:54 -08:00
|
|
|
private boolean mIsChecked;
|
|
|
|
|
private ObjectAnimator mCheckedAlphaAnimator;
|
|
|
|
|
private float mCheckedAlpha = 1.0f;
|
|
|
|
|
private int mCheckedFadeInDuration;
|
|
|
|
|
private int mCheckedFadeOutDuration;
|
|
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
public PagedViewWidget(Context context) {
|
|
|
|
|
this(context, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PagedViewWidget(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PagedViewWidget(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
|
2010-12-21 11:31:54 -08:00
|
|
|
// Set up fade in/out constants
|
|
|
|
|
final Resources r = context.getResources();
|
2011-04-14 16:08:02 -07:00
|
|
|
final int alpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha);
|
2010-12-21 11:31:54 -08:00
|
|
|
if (alpha > 0) {
|
2011-04-14 16:08:02 -07:00
|
|
|
mCheckedAlpha = r.getInteger(R.integer.config_dragAppsCustomizeIconFadeAlpha) / 256.0f;
|
|
|
|
|
mCheckedFadeInDuration =
|
|
|
|
|
r.getInteger(R.integer.config_dragAppsCustomizeIconFadeInDuration);
|
|
|
|
|
mCheckedFadeOutDuration =
|
|
|
|
|
r.getInteger(R.integer.config_dragAppsCustomizeIconFadeOutDuration);
|
2010-12-21 11:31:54 -08:00
|
|
|
}
|
2011-05-05 17:06:13 -07:00
|
|
|
mDimensionsFormatString = r.getString(R.string.widget_dims_format);
|
2010-12-21 11:31:54 -08:00
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
setWillNotDraw(false);
|
|
|
|
|
setClipToPadding(false);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-20 14:22:40 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onDetachedFromWindow() {
|
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
|
|
|
|
|
|
final ImageView image = (ImageView) findViewById(R.id.widget_preview);
|
|
|
|
|
if (image != null) {
|
|
|
|
|
FastBitmapDrawable preview = (FastBitmapDrawable) image.getDrawable();
|
|
|
|
|
if (preview != null && preview.getBitmap() != null) {
|
|
|
|
|
preview.getBitmap().recycle();
|
|
|
|
|
}
|
|
|
|
|
image.setImageDrawable(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
public void applyFromAppWidgetProviderInfo(AppWidgetProviderInfo info,
|
2011-09-30 14:41:25 -07:00
|
|
|
int maxWidth, int[] cellSpan, HolographicOutlineHelper holoOutlineHelper) {
|
2011-06-13 11:32:14 -07:00
|
|
|
mHolographicOutlineHelper = holoOutlineHelper;
|
2010-12-01 15:47:31 -08:00
|
|
|
final ImageView image = (ImageView) findViewById(R.id.widget_preview);
|
2011-04-27 17:40:20 -07:00
|
|
|
if (maxWidth > -1) {
|
|
|
|
|
image.setMaxWidth(maxWidth);
|
|
|
|
|
}
|
2011-06-29 20:10:49 -07:00
|
|
|
image.setContentDescription(info.label);
|
2011-02-24 18:09:44 -08:00
|
|
|
mPreviewImageView = image;
|
2010-12-01 15:47:31 -08:00
|
|
|
final TextView name = (TextView) findViewById(R.id.widget_name);
|
|
|
|
|
name.setText(info.label);
|
|
|
|
|
final TextView dims = (TextView) findViewById(R.id.widget_dims);
|
2011-08-11 15:12:11 -07:00
|
|
|
if (dims != null) {
|
|
|
|
|
dims.setText(String.format(mDimensionsFormatString, cellSpan[0], cellSpan[1]));
|
|
|
|
|
}
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|
|
|
|
|
|
2011-05-03 16:18:34 -07:00
|
|
|
public void applyFromResolveInfo(PackageManager pm, ResolveInfo info,
|
2011-09-30 14:41:25 -07:00
|
|
|
HolographicOutlineHelper holoOutlineHelper) {
|
2011-06-13 11:32:14 -07:00
|
|
|
mHolographicOutlineHelper = holoOutlineHelper;
|
2011-06-29 20:10:49 -07:00
|
|
|
CharSequence label = info.loadLabel(pm);
|
2011-05-03 16:18:34 -07:00
|
|
|
final ImageView image = (ImageView) findViewById(R.id.widget_preview);
|
2011-06-29 20:10:49 -07:00
|
|
|
image.setContentDescription(label);
|
2011-05-03 16:18:34 -07:00
|
|
|
mPreviewImageView = image;
|
|
|
|
|
final TextView name = (TextView) findViewById(R.id.widget_name);
|
2011-06-29 20:10:49 -07:00
|
|
|
name.setText(label);
|
2011-05-03 16:18:34 -07:00
|
|
|
final TextView dims = (TextView) findViewById(R.id.widget_dims);
|
2011-05-09 16:00:53 -07:00
|
|
|
if (dims != null) {
|
|
|
|
|
dims.setText(String.format(mDimensionsFormatString, 1, 1));
|
|
|
|
|
}
|
2011-06-13 11:32:14 -07:00
|
|
|
}
|
2011-01-05 13:54:43 -08:00
|
|
|
|
2011-10-11 17:53:58 -07:00
|
|
|
void applyPreview(FastBitmapDrawable preview, int index, boolean scale) {
|
2011-09-30 14:41:25 -07:00
|
|
|
final ImageView image = (ImageView) findViewById(R.id.widget_preview);
|
|
|
|
|
if (preview != null) {
|
|
|
|
|
image.setImageDrawable(preview);
|
2011-10-11 17:53:58 -07:00
|
|
|
image.setScaleType(scale ? ImageView.ScaleType.FIT_START : ImageView.ScaleType.MATRIX);
|
2011-09-30 14:41:25 -07:00
|
|
|
image.setAlpha(0f);
|
|
|
|
|
image.animate()
|
|
|
|
|
.alpha(1f)
|
|
|
|
|
.setDuration(sPreviewFadeInDuration + (index * sPreviewFadeInStaggerDuration))
|
|
|
|
|
.start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-13 11:32:14 -07:00
|
|
|
public void setHolographicOutline(Bitmap holoOutline) {
|
|
|
|
|
mHolographicOutline = holoOutline;
|
|
|
|
|
invalidate();
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
|
|
// We eat up the touch events here, since the PagedView (which uses the same swiping
|
|
|
|
|
// touch code as Workspace previously) uses onInterceptTouchEvent() to determine when
|
|
|
|
|
// the user is scrolling between pages. This means that if the pages themselves don't
|
|
|
|
|
// handle touch events, it gets forwarded up to PagedView itself, and it's own
|
|
|
|
|
// onTouchEvent() handling will prevent further intercept touch events from being called
|
|
|
|
|
// (it's the same view in that case). This is not ideal, but to prevent more changes,
|
|
|
|
|
// we just always mark the touch event as handled.
|
|
|
|
|
return super.onTouchEvent(event) || true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-13 11:27:36 -07:00
|
|
|
@Override
|
|
|
|
|
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
2011-10-20 00:15:42 -07:00
|
|
|
return FocusHelper.handleAppsCustomizeKeyEvent(this, keyCode, event)
|
2011-06-13 18:15:54 -07:00
|
|
|
|| super.onKeyDown(keyCode, event);
|
2011-04-13 11:27:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onKeyUp(int keyCode, KeyEvent event) {
|
2011-10-20 00:15:42 -07:00
|
|
|
return FocusHelper.handleAppsCustomizeKeyEvent(this, keyCode, event)
|
2011-06-13 18:15:54 -07:00
|
|
|
|| super.onKeyUp(keyCode, event);
|
2011-04-13 11:27:36 -07:00
|
|
|
}
|
|
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
@Override
|
|
|
|
|
protected void onDraw(Canvas canvas) {
|
|
|
|
|
if (mAlpha > 0) {
|
|
|
|
|
super.onDraw(canvas);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// draw any blended overlays
|
|
|
|
|
if (mHolographicOutline != null && mHolographicAlpha > 0) {
|
2011-02-24 18:09:44 -08:00
|
|
|
// Calculate how much to scale the holographic preview
|
|
|
|
|
mTmpScaleRect.set(0,0,1,1);
|
|
|
|
|
mPreviewImageView.getImageMatrix().mapRect(mTmpScaleRect);
|
|
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
mPaint.setAlpha(mHolographicAlpha);
|
2011-02-24 18:09:44 -08:00
|
|
|
canvas.save();
|
|
|
|
|
canvas.scale(mTmpScaleRect.right, mTmpScaleRect.bottom);
|
2011-06-15 19:51:24 -07:00
|
|
|
canvas.drawBitmap(mHolographicOutline, mPreviewImageView.getLeft(),
|
|
|
|
|
mPreviewImageView.getTop(), mPaint);
|
2011-02-24 18:09:44 -08:00
|
|
|
canvas.restore();
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected boolean onSetAlpha(int alpha) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-13 11:32:14 -07:00
|
|
|
private void setChildrenAlpha(float alpha) {
|
|
|
|
|
final int childCount = getChildCount();
|
|
|
|
|
for (int i = 0; i < childCount; i++) {
|
|
|
|
|
getChildAt(i).setAlpha(alpha);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-01 15:47:31 -08:00
|
|
|
@Override
|
|
|
|
|
public void setAlpha(float alpha) {
|
2011-06-13 11:32:14 -07:00
|
|
|
final float viewAlpha = mHolographicOutlineHelper.viewAlphaInterpolator(alpha);
|
|
|
|
|
final float holographicAlpha = mHolographicOutlineHelper.highlightAlphaInterpolator(alpha);
|
2010-12-01 15:47:31 -08:00
|
|
|
int newViewAlpha = (int) (viewAlpha * 255);
|
|
|
|
|
int newHolographicAlpha = (int) (holographicAlpha * 255);
|
|
|
|
|
if ((mAlpha != newViewAlpha) || (mHolographicAlpha != newHolographicAlpha)) {
|
|
|
|
|
mAlpha = newViewAlpha;
|
|
|
|
|
mHolographicAlpha = newHolographicAlpha;
|
|
|
|
|
setChildrenAlpha(viewAlpha);
|
|
|
|
|
super.setAlpha(viewAlpha);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 14:35:21 -08:00
|
|
|
void setChecked(boolean checked, boolean animate) {
|
2010-12-21 11:31:54 -08:00
|
|
|
if (mIsChecked != checked) {
|
|
|
|
|
mIsChecked = checked;
|
|
|
|
|
|
|
|
|
|
float alpha;
|
|
|
|
|
int duration;
|
|
|
|
|
if (mIsChecked) {
|
|
|
|
|
alpha = mCheckedAlpha;
|
|
|
|
|
duration = mCheckedFadeInDuration;
|
|
|
|
|
} else {
|
|
|
|
|
alpha = 1.0f;
|
|
|
|
|
duration = mCheckedFadeOutDuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize the animator
|
|
|
|
|
if (mCheckedAlphaAnimator != null) {
|
|
|
|
|
mCheckedAlphaAnimator.cancel();
|
|
|
|
|
}
|
2011-02-18 14:35:21 -08:00
|
|
|
if (animate) {
|
|
|
|
|
mCheckedAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", getAlpha(), alpha);
|
|
|
|
|
mCheckedAlphaAnimator.setDuration(duration);
|
|
|
|
|
mCheckedAlphaAnimator.start();
|
|
|
|
|
} else {
|
|
|
|
|
setAlpha(alpha);
|
|
|
|
|
}
|
2010-12-21 11:31:54 -08:00
|
|
|
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-18 14:35:21 -08:00
|
|
|
@Override
|
|
|
|
|
public void setChecked(boolean checked) {
|
|
|
|
|
setChecked(checked, true);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-21 11:31:54 -08:00
|
|
|
@Override
|
|
|
|
|
public boolean isChecked() {
|
|
|
|
|
return mIsChecked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void toggle() {
|
|
|
|
|
setChecked(!mIsChecked);
|
|
|
|
|
}
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|