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.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
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;
|
2012-07-23 20:48:26 -07:00
|
|
|
import android.graphics.Rect;
|
2010-12-01 15:47:31 -08:00
|
|
|
import android.util.AttributeSet;
|
2013-09-03 17:48:37 -07:00
|
|
|
import android.util.TypedValue;
|
2010-12-01 15:47:31 -08:00
|
|
|
import android.view.MotionEvent;
|
2012-01-23 17:28:51 -08:00
|
|
|
import android.view.View;
|
2010-12-01 15:47:31 -08:00
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
import com.android.launcher3.R;
|
2010-12-01 15:47:31 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The linear layout used strictly for the widget/wallpaper tab of the customization tray
|
|
|
|
|
*/
|
2012-01-12 08:08:38 -08:00
|
|
|
public class PagedViewWidget extends LinearLayout {
|
2010-12-01 15:47:31 -08:00
|
|
|
static final String TAG = "PagedViewWidgetLayout";
|
|
|
|
|
|
2011-11-03 13:50:45 -07:00
|
|
|
private static boolean sDeletePreviewsWhenDetachedFromWindow = true;
|
2013-02-07 13:27:06 +01:00
|
|
|
private static boolean sRecyclePreviewsWhenDetachedFromWindow = true;
|
2011-09-30 14:41:25 -07:00
|
|
|
|
2011-05-05 17:06:13 -07:00
|
|
|
private String mDimensionsFormatString;
|
2012-01-23 17:28:51 -08:00
|
|
|
CheckForShortPress mPendingCheckForShortPress = null;
|
|
|
|
|
ShortPressListener mShortPressListener = null;
|
|
|
|
|
boolean mShortPressTriggered = false;
|
2012-05-11 15:57:05 -07:00
|
|
|
static PagedViewWidget sShortpressTarget = null;
|
2012-05-18 15:04:49 -07:00
|
|
|
boolean mIsAppWidget;
|
2012-07-23 20:48:26 -07:00
|
|
|
private final Rect mOriginalImagePadding = new Rect();
|
2013-01-23 12:39:24 +01:00
|
|
|
private Object mInfo;
|
2013-02-05 11:21:28 +01:00
|
|
|
private WidgetPreviewLoader mWidgetPreviewLoader;
|
2011-01-05 13:54:43 -08:00
|
|
|
|
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
|
|
|
final Resources r = context.getResources();
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-23 20:48:26 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
|
|
|
|
|
final ImageView image = (ImageView) findViewById(R.id.widget_preview);
|
|
|
|
|
mOriginalImagePadding.left = image.getPaddingLeft();
|
|
|
|
|
mOriginalImagePadding.top = image.getPaddingTop();
|
|
|
|
|
mOriginalImagePadding.right = image.getPaddingRight();
|
|
|
|
|
mOriginalImagePadding.bottom = image.getPaddingBottom();
|
2013-09-03 17:48:37 -07:00
|
|
|
|
|
|
|
|
// Ensure we are using the right text size
|
|
|
|
|
LauncherAppState app = LauncherAppState.getInstance();
|
|
|
|
|
DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
|
|
|
|
|
TextView name = (TextView) findViewById(R.id.widget_name);
|
|
|
|
|
if (name != null) {
|
|
|
|
|
name.setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.iconTextSize);
|
|
|
|
|
}
|
|
|
|
|
TextView dims = (TextView) findViewById(R.id.widget_dims);
|
|
|
|
|
if (dims != null) {
|
|
|
|
|
dims.setTextSize(TypedValue.COMPLEX_UNIT_SP, grid.iconTextSize);
|
|
|
|
|
}
|
2012-07-23 20:48:26 -07:00
|
|
|
}
|
|
|
|
|
|
2011-11-03 13:50:45 -07:00
|
|
|
public static void setDeletePreviewsWhenDetachedFromWindow(boolean value) {
|
|
|
|
|
sDeletePreviewsWhenDetachedFromWindow = value;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 13:27:06 +01:00
|
|
|
public static void setRecyclePreviewsWhenDetachedFromWindow(boolean value) {
|
|
|
|
|
sRecyclePreviewsWhenDetachedFromWindow = value;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-20 14:22:40 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onDetachedFromWindow() {
|
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
|
|
2011-11-03 13:50:45 -07:00
|
|
|
if (sDeletePreviewsWhenDetachedFromWindow) {
|
|
|
|
|
final ImageView image = (ImageView) findViewById(R.id.widget_preview);
|
|
|
|
|
if (image != null) {
|
|
|
|
|
FastBitmapDrawable preview = (FastBitmapDrawable) image.getDrawable();
|
2013-02-07 13:27:06 +01:00
|
|
|
if (sRecyclePreviewsWhenDetachedFromWindow &&
|
|
|
|
|
mInfo != null && preview != null && preview.getBitmap() != null) {
|
|
|
|
|
mWidgetPreviewLoader.recycleBitmap(mInfo, preview.getBitmap());
|
2011-11-03 13:50:45 -07:00
|
|
|
}
|
|
|
|
|
image.setImageDrawable(null);
|
2012-07-23 20:48:26 -07:00
|
|
|
}
|
2011-09-20 14:22:40 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
public void applyFromAppWidgetProviderInfo(AppWidgetProviderInfo info,
|
2013-02-05 11:21:28 +01:00
|
|
|
int maxWidth, int[] cellSpan, WidgetPreviewLoader loader) {
|
2013-08-22 16:15:50 -07:00
|
|
|
LauncherAppState app = LauncherAppState.getInstance();
|
|
|
|
|
DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
|
|
|
|
|
|
2012-05-18 15:04:49 -07:00
|
|
|
mIsAppWidget = true;
|
2013-01-23 12:39:24 +01:00
|
|
|
mInfo = info;
|
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);
|
|
|
|
|
}
|
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) {
|
2013-08-22 16:15:50 -07:00
|
|
|
int hSpan = Math.min(cellSpan[0], (int) grid.numColumns);
|
|
|
|
|
int vSpan = Math.min(cellSpan[1], (int) grid.numRows);
|
2012-05-01 15:10:11 -07:00
|
|
|
dims.setText(String.format(mDimensionsFormatString, hSpan, vSpan));
|
2011-08-11 15:12:11 -07:00
|
|
|
}
|
2013-02-05 11:21:28 +01:00
|
|
|
mWidgetPreviewLoader = loader;
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|
|
|
|
|
|
2013-02-05 11:21:28 +01:00
|
|
|
public void applyFromResolveInfo(
|
|
|
|
|
PackageManager pm, ResolveInfo info, WidgetPreviewLoader loader) {
|
2012-05-18 15:04:49 -07:00
|
|
|
mIsAppWidget = false;
|
2013-01-23 12:39:24 +01:00
|
|
|
mInfo = info;
|
2011-06-29 20:10:49 -07:00
|
|
|
CharSequence label = info.loadLabel(pm);
|
2011-05-03 16:18:34 -07:00
|
|
|
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));
|
|
|
|
|
}
|
2013-02-05 11:21:28 +01:00
|
|
|
mWidgetPreviewLoader = loader;
|
2011-06-13 11:32:14 -07:00
|
|
|
}
|
2011-01-05 13:54:43 -08:00
|
|
|
|
2011-11-03 13:50:45 -07:00
|
|
|
public int[] getPreviewSize() {
|
|
|
|
|
final ImageView i = (ImageView) findViewById(R.id.widget_preview);
|
|
|
|
|
int[] maxSize = new int[2];
|
2012-07-23 20:48:26 -07:00
|
|
|
maxSize[0] = i.getWidth() - mOriginalImagePadding.left - mOriginalImagePadding.right;
|
|
|
|
|
maxSize[1] = i.getHeight() - mOriginalImagePadding.top;
|
2011-11-03 13:50:45 -07:00
|
|
|
return maxSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void applyPreview(FastBitmapDrawable preview, int index) {
|
2011-11-03 13:50:45 -07:00
|
|
|
final PagedViewWidgetImageView image =
|
2012-05-18 15:04:49 -07:00
|
|
|
(PagedViewWidgetImageView) findViewById(R.id.widget_preview);
|
2011-09-30 14:41:25 -07:00
|
|
|
if (preview != null) {
|
2011-11-03 13:50:45 -07:00
|
|
|
image.mAllowRequestLayout = false;
|
2011-09-30 14:41:25 -07:00
|
|
|
image.setImageDrawable(preview);
|
2012-05-18 15:04:49 -07:00
|
|
|
if (mIsAppWidget) {
|
|
|
|
|
// center horizontally
|
|
|
|
|
int[] imageSize = getPreviewSize();
|
|
|
|
|
int centerAmount = (imageSize[0] - preview.getIntrinsicWidth()) / 2;
|
2012-07-23 20:48:26 -07:00
|
|
|
image.setPadding(mOriginalImagePadding.left + centerAmount,
|
|
|
|
|
mOriginalImagePadding.top,
|
|
|
|
|
mOriginalImagePadding.right,
|
|
|
|
|
mOriginalImagePadding.bottom);
|
2012-05-18 15:04:49 -07:00
|
|
|
}
|
2011-11-10 15:48:25 -08:00
|
|
|
image.setAlpha(1f);
|
2011-11-03 13:50:45 -07:00
|
|
|
image.mAllowRequestLayout = true;
|
2011-09-30 14:41:25 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-23 17:28:51 -08:00
|
|
|
void setShortPressListener(ShortPressListener listener) {
|
|
|
|
|
mShortPressListener = listener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ShortPressListener {
|
|
|
|
|
void onShortPress(View v);
|
|
|
|
|
void cleanUpShortPress(View v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CheckForShortPress implements Runnable {
|
|
|
|
|
public void run() {
|
2012-05-16 17:15:26 -07:00
|
|
|
if (sShortpressTarget != null) return;
|
2012-01-23 17:28:51 -08:00
|
|
|
if (mShortPressListener != null) {
|
|
|
|
|
mShortPressListener.onShortPress(PagedViewWidget.this);
|
2012-05-17 12:24:40 -07:00
|
|
|
sShortpressTarget = PagedViewWidget.this;
|
2012-01-23 17:28:51 -08:00
|
|
|
}
|
|
|
|
|
mShortPressTriggered = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void checkForShortPress() {
|
2012-05-11 15:57:05 -07:00
|
|
|
if (sShortpressTarget != null) return;
|
2012-01-23 17:28:51 -08:00
|
|
|
if (mPendingCheckForShortPress == null) {
|
|
|
|
|
mPendingCheckForShortPress = new CheckForShortPress();
|
|
|
|
|
}
|
|
|
|
|
postDelayed(mPendingCheckForShortPress, 120);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the longpress detection timer.
|
|
|
|
|
*/
|
|
|
|
|
private void removeShortPressCallback() {
|
|
|
|
|
if (mPendingCheckForShortPress != null) {
|
|
|
|
|
removeCallbacks(mPendingCheckForShortPress);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cleanUpShortPress() {
|
|
|
|
|
removeShortPressCallback();
|
|
|
|
|
if (mShortPressTriggered) {
|
|
|
|
|
if (mShortPressListener != null) {
|
|
|
|
|
mShortPressListener.cleanUpShortPress(PagedViewWidget.this);
|
|
|
|
|
}
|
|
|
|
|
mShortPressTriggered = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-11 15:57:05 -07:00
|
|
|
static void resetShortPressTarget() {
|
|
|
|
|
sShortpressTarget = null;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
@Override
|
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
2012-01-23 17:28:51 -08:00
|
|
|
super.onTouchEvent(event);
|
|
|
|
|
|
|
|
|
|
switch (event.getAction()) {
|
|
|
|
|
case MotionEvent.ACTION_UP:
|
|
|
|
|
cleanUpShortPress();
|
|
|
|
|
break;
|
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
|
checkForShortPress();
|
|
|
|
|
break;
|
|
|
|
|
case MotionEvent.ACTION_CANCEL:
|
|
|
|
|
cleanUpShortPress();
|
|
|
|
|
break;
|
|
|
|
|
case MotionEvent.ACTION_MOVE:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-05-11 15:57:05 -07:00
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
// 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.
|
2012-01-23 17:28:51 -08:00
|
|
|
return true;
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|
|
|
|
|
}
|