2010-12-01 15:47:31 -08:00
|
|
|
/*
|
2015-04-08 19:01:34 -07:00
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
2010-12-01 15:47:31 -08:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-04-08 19:01:34 -07:00
|
|
|
package com.android.launcher3.widget;
|
2010-12-01 15:47:31 -08:00
|
|
|
|
|
|
|
|
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;
|
2015-03-19 14:31:19 -07:00
|
|
|
import android.graphics.Bitmap;
|
2012-07-23 20:48:26 -07:00
|
|
|
import android.graphics.Rect;
|
2010-12-01 15:47:31 -08:00
|
|
|
import android.util.AttributeSet;
|
2015-04-08 19:01:34 -07:00
|
|
|
import android.util.Log;
|
2012-01-23 17:28:51 -08:00
|
|
|
import android.view.View;
|
2015-03-19 14:31:19 -07:00
|
|
|
import android.view.View.OnLayoutChangeListener;
|
2010-12-01 15:47:31 -08:00
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
2015-04-08 19:01:34 -07:00
|
|
|
import com.android.launcher3.DeviceProfile;
|
|
|
|
|
import com.android.launcher3.FastBitmapDrawable;
|
|
|
|
|
import com.android.launcher3.ItemInfo;
|
|
|
|
|
import com.android.launcher3.LauncherAppState;
|
|
|
|
|
import com.android.launcher3.LauncherAppWidgetProviderInfo;
|
|
|
|
|
import com.android.launcher3.R;
|
|
|
|
|
import com.android.launcher3.WidgetPreviewLoader;
|
2015-03-19 14:31:19 -07:00
|
|
|
import com.android.launcher3.WidgetPreviewLoader.PreviewLoadRequest;
|
2014-08-14 17:39:34 -07:00
|
|
|
import com.android.launcher3.compat.AppWidgetManagerCompat;
|
|
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
/**
|
2015-04-23 15:17:50 -07:00
|
|
|
* Represents the individual cell of the widget inside the widget tray.
|
2010-12-01 15:47:31 -08:00
|
|
|
*/
|
2015-04-08 19:01:34 -07:00
|
|
|
public class WidgetCell extends LinearLayout implements OnLayoutChangeListener {
|
2010-12-01 15:47:31 -08:00
|
|
|
|
2015-04-11 15:44:32 -07:00
|
|
|
private static final String TAG = "WidgetCell";
|
2015-04-08 19:01:34 -07:00
|
|
|
private static final boolean DEBUG = false;
|
|
|
|
|
|
2015-04-27 17:01:32 -07:00
|
|
|
private static final int FADE_IN_DURATION_MS = 90;
|
2015-05-11 14:55:07 -07:00
|
|
|
|
|
|
|
|
/** Widget cell width is calculated by multiplying this factor to grid cell width. */
|
|
|
|
|
private static final float WIDTH_SCALE = 2.8f;
|
|
|
|
|
|
|
|
|
|
/** Widget preview width is calculated by multiplying this factor to the widget cell width. */
|
|
|
|
|
private static final float PREVIEW_SCALE = 0.9f;
|
|
|
|
|
|
|
|
|
|
private static int mPresetPreviewSize;
|
|
|
|
|
private static int mSize;
|
|
|
|
|
private static int mDividerWidth;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
2015-04-23 15:17:50 -07:00
|
|
|
private ImageView mWidgetImage;
|
|
|
|
|
private TextView mWidgetName;
|
|
|
|
|
private TextView mWidgetDims;
|
2012-07-23 20:48:26 -07:00
|
|
|
private final Rect mOriginalImagePadding = new Rect();
|
2015-03-19 14:31:19 -07:00
|
|
|
|
|
|
|
|
private String mDimensionsFormatString;
|
|
|
|
|
private boolean mIsAppWidget;
|
2013-01-23 12:39:24 +01:00
|
|
|
private Object mInfo;
|
2015-03-19 14:31:19 -07:00
|
|
|
|
2013-02-05 11:21:28 +01:00
|
|
|
private WidgetPreviewLoader mWidgetPreviewLoader;
|
2015-03-19 14:31:19 -07:00
|
|
|
private PreviewLoadRequest mActiveRequest;
|
2011-01-05 13:54:43 -08:00
|
|
|
|
2015-04-08 19:01:34 -07:00
|
|
|
public WidgetCell(Context context) {
|
2010-12-01 15:47:31 -08:00
|
|
|
this(context, null);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 19:01:34 -07:00
|
|
|
public WidgetCell(Context context, AttributeSet attrs) {
|
2010-12-01 15:47:31 -08:00
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 19:01:34 -07:00
|
|
|
public WidgetCell(Context context, AttributeSet attrs, int defStyle) {
|
2010-12-01 15:47:31 -08:00
|
|
|
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
|
|
|
|
2015-05-11 14:55:07 -07:00
|
|
|
setContainerWidth();
|
2010-12-01 15:47:31 -08:00
|
|
|
setWillNotDraw(false);
|
|
|
|
|
setClipToPadding(false);
|
2015-01-08 16:59:04 -08:00
|
|
|
setAccessibilityDelegate(LauncherAppState.getInstance().getAccessibilityDelegate());
|
2015-05-11 14:55:07 -07:00
|
|
|
}
|
2015-04-08 19:01:34 -07:00
|
|
|
|
2015-05-11 14:55:07 -07:00
|
|
|
private void setContainerWidth() {
|
|
|
|
|
// Do nothing if already set
|
|
|
|
|
if (mSize > 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DeviceProfile profile = LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile();
|
|
|
|
|
mSize = (int) (profile.cellWidthPx * WIDTH_SCALE);
|
|
|
|
|
mPresetPreviewSize = (int) (profile.cellWidthPx * WIDTH_SCALE * PREVIEW_SCALE);
|
|
|
|
|
mDividerWidth = getResources().getDimensionPixelSize(R.dimen.widget_row_divider);
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|
|
|
|
|
|
2012-07-23 20:48:26 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
|
2015-04-23 15:17:50 -07:00
|
|
|
mWidgetImage = (ImageView) findViewById(R.id.widget_preview);
|
|
|
|
|
mOriginalImagePadding.left = mWidgetImage.getPaddingLeft();
|
|
|
|
|
mOriginalImagePadding.top = mWidgetImage.getPaddingTop();
|
|
|
|
|
mOriginalImagePadding.right = mWidgetImage.getPaddingRight();
|
|
|
|
|
mOriginalImagePadding.bottom = mWidgetImage.getPaddingBottom();
|
2013-09-03 17:48:37 -07:00
|
|
|
|
2015-04-23 15:17:50 -07:00
|
|
|
mWidgetName = ((TextView) findViewById(R.id.widget_name));
|
|
|
|
|
mWidgetDims = ((TextView) findViewById(R.id.widget_dims));
|
2015-04-11 15:44:32 -07:00
|
|
|
}
|
|
|
|
|
|
2015-05-11 14:55:07 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
|
super.onMeasure(MeasureSpec.makeMeasureSpec(mSize, MeasureSpec.EXACTLY),
|
|
|
|
|
MeasureSpec.makeMeasureSpec(mSize, MeasureSpec.EXACTLY));
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-28 15:06:45 -07:00
|
|
|
/**
|
|
|
|
|
* Called to clear the view and free attached resources. (e.g., {@link Bitmap}
|
|
|
|
|
*/
|
|
|
|
|
public void clear() {
|
|
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, "reset called on:" + mWidgetName.getText());
|
|
|
|
|
}
|
2015-04-23 15:17:50 -07:00
|
|
|
mWidgetImage.setImageDrawable(null);
|
|
|
|
|
mWidgetName.setText(null);
|
|
|
|
|
mWidgetDims.setText(null);
|
2015-05-11 14:55:07 -07:00
|
|
|
setSeparator(true);
|
2015-04-27 17:01:32 -07:00
|
|
|
|
2015-04-28 15:06:45 -07:00
|
|
|
if (mActiveRequest != null) {
|
|
|
|
|
mActiveRequest.cleanup();
|
|
|
|
|
mActiveRequest = null;
|
|
|
|
|
}
|
2011-09-20 14:22:40 -07:00
|
|
|
}
|
|
|
|
|
|
2015-04-23 15:17:50 -07:00
|
|
|
/**
|
|
|
|
|
* Apply the widget provider info to the view.
|
|
|
|
|
*/
|
2014-03-05 18:07:04 -08:00
|
|
|
public void applyFromAppWidgetProviderInfo(LauncherAppWidgetProviderInfo info,
|
|
|
|
|
int maxWidth, 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;
|
2011-04-27 17:40:20 -07:00
|
|
|
if (maxWidth > -1) {
|
2015-04-23 15:17:50 -07:00
|
|
|
mWidgetImage.setMaxWidth(maxWidth);
|
2011-08-11 15:12:11 -07:00
|
|
|
}
|
2015-04-23 15:17:50 -07:00
|
|
|
// TODO(hyunyoungs): setup a cache for these labels.
|
|
|
|
|
mWidgetName.setText(AppWidgetManagerCompat.getInstance(getContext()).loadLabel(info));
|
|
|
|
|
int hSpan = Math.min(info.spanX, (int) grid.numColumns);
|
|
|
|
|
int vSpan = Math.min(info.spanY, (int) grid.numRows);
|
|
|
|
|
mWidgetDims.setText(String.format(mDimensionsFormatString, hSpan, vSpan));
|
2013-02-05 11:21:28 +01:00
|
|
|
mWidgetPreviewLoader = loader;
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|
|
|
|
|
|
2015-04-23 15:17:50 -07:00
|
|
|
/**
|
|
|
|
|
* Apply the resolve info to the view.
|
|
|
|
|
*/
|
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);
|
2015-04-23 15:17:50 -07:00
|
|
|
mWidgetName.setText(label);
|
|
|
|
|
mWidgetDims.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() {
|
|
|
|
|
int[] maxSize = new int[2];
|
2015-04-08 19:01:34 -07:00
|
|
|
maxSize[0] = mPresetPreviewSize;
|
|
|
|
|
maxSize[1] = mPresetPreviewSize;
|
2011-11-03 13:50:45 -07:00
|
|
|
return maxSize;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 14:31:19 -07:00
|
|
|
public void applyPreview(Bitmap bitmap) {
|
|
|
|
|
FastBitmapDrawable preview = new FastBitmapDrawable(bitmap);
|
2015-04-08 19:01:34 -07:00
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, String.format("[tag=%s] applyPreview preview: %s",
|
|
|
|
|
getTagToString(), preview));
|
|
|
|
|
}
|
2011-09-30 14:41:25 -07:00
|
|
|
if (preview != null) {
|
2015-04-23 15:17:50 -07:00
|
|
|
mWidgetImage.setImageDrawable(preview);
|
2012-05-18 15:04:49 -07:00
|
|
|
if (mIsAppWidget) {
|
|
|
|
|
// center horizontally
|
|
|
|
|
int[] imageSize = getPreviewSize();
|
|
|
|
|
int centerAmount = (imageSize[0] - preview.getIntrinsicWidth()) / 2;
|
2015-04-23 15:17:50 -07:00
|
|
|
mWidgetImage.setPadding(mOriginalImagePadding.left + centerAmount,
|
2012-07-23 20:48:26 -07:00
|
|
|
mOriginalImagePadding.top,
|
|
|
|
|
mOriginalImagePadding.right,
|
|
|
|
|
mOriginalImagePadding.bottom);
|
2012-05-18 15:04:49 -07:00
|
|
|
}
|
2015-04-23 15:17:50 -07:00
|
|
|
mWidgetImage.setAlpha(0f);
|
|
|
|
|
mWidgetImage.animate().alpha(1.0f).setDuration(FADE_IN_DURATION_MS);
|
|
|
|
|
// TODO(hyunyoungs): figure out why this has to be called explicitly.
|
|
|
|
|
mWidgetImage.requestLayout();
|
2012-01-23 17:28:51 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 14:31:19 -07:00
|
|
|
public void ensurePreview() {
|
|
|
|
|
if (mActiveRequest != null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int[] size = getPreviewSize();
|
2015-04-08 19:01:34 -07:00
|
|
|
if (DEBUG) {
|
|
|
|
|
Log.d(TAG, String.format("[tag=%s] ensurePreview (%d, %d):",
|
|
|
|
|
getTagToString(), size[0], size[1]));
|
|
|
|
|
}
|
2015-03-19 14:31:19 -07:00
|
|
|
|
|
|
|
|
if (size[0] <= 0 || size[1] <= 0) {
|
|
|
|
|
addOnLayoutChangeListener(this);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Bitmap[] immediateResult = new Bitmap[1];
|
|
|
|
|
mActiveRequest = mWidgetPreviewLoader.getPreview(mInfo, size[0], size[1], this,
|
|
|
|
|
immediateResult);
|
|
|
|
|
if (immediateResult[0] != null) {
|
|
|
|
|
applyPreview(immediateResult[0]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
|
|
|
|
|
int oldTop, int oldRight, int oldBottom) {
|
|
|
|
|
removeOnLayoutChangeListener(this);
|
|
|
|
|
ensurePreview();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getActualItemWidth() {
|
|
|
|
|
ItemInfo info = (ItemInfo) getTag();
|
|
|
|
|
int[] size = getPreviewSize();
|
|
|
|
|
int cellWidth = LauncherAppState.getInstance()
|
|
|
|
|
.getDynamicGrid().getDeviceProfile().cellWidthPx;
|
|
|
|
|
|
|
|
|
|
return Math.min(size[0], info.spanX * cellWidth);
|
|
|
|
|
}
|
2015-04-08 19:01:34 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper method to get the string info of the tag.
|
|
|
|
|
*/
|
|
|
|
|
private String getTagToString() {
|
|
|
|
|
if (getTag() instanceof PendingAddWidgetInfo) {
|
|
|
|
|
return ((PendingAddWidgetInfo)getTag()).toString();
|
|
|
|
|
} else if (getTag() instanceof PendingAddShortcutInfo) {
|
|
|
|
|
return ((PendingAddShortcutInfo)getTag()).toString();
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2015-05-11 14:55:07 -07:00
|
|
|
|
|
|
|
|
public void setSeparator(boolean enable) {
|
|
|
|
|
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
|
|
|
|
|
lp.setMarginEnd(enable? mDividerWidth : 0);
|
|
|
|
|
setLayoutParams(lp);
|
|
|
|
|
requestLayout();
|
|
|
|
|
}
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|