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;
|
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.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
2015-04-08 19:01:34 -07:00
|
|
|
import com.android.launcher3.DeviceProfile;
|
|
|
|
|
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-05-15 17:01:14 -07:00
|
|
|
* Represents the individual cell of the widget inside the widget tray. The preview is drawn
|
|
|
|
|
* horizontally centered, and scaled down if needed.
|
|
|
|
|
*
|
|
|
|
|
* This view does not support padding. Since the image is scaled down to fit the view, padding will
|
|
|
|
|
* further decrease the scaling factor. Drag-n-drop uses the view bounds for showing a smooth
|
|
|
|
|
* transition from the view to drag view, so when adding padding support, DnD would need to
|
|
|
|
|
* consider the appropriate scaling factor.
|
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. */
|
2015-05-12 15:36:20 -07:00
|
|
|
private static final float WIDTH_SCALE = 2.6f;
|
2015-05-11 14:55:07 -07:00
|
|
|
|
|
|
|
|
/** Widget preview width is calculated by multiplying this factor to the widget cell width. */
|
2015-05-12 15:36:20 -07:00
|
|
|
private static final float PREVIEW_SCALE = 0.8f;
|
2015-05-11 14:55:07 -07:00
|
|
|
|
2015-05-12 15:36:20 -07:00
|
|
|
private int mPresetPreviewSize;
|
|
|
|
|
int cellSize;
|
2015-04-08 19:01:34 -07:00
|
|
|
|
2015-05-15 17:01:14 -07:00
|
|
|
private WidgetImageView mWidgetImage;
|
2015-04-23 15:17:50 -07:00
|
|
|
private TextView mWidgetName;
|
|
|
|
|
private TextView mWidgetDims;
|
2015-03-19 14:31:19 -07:00
|
|
|
|
|
|
|
|
private String mDimensionsFormatString;
|
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() {
|
|
|
|
|
DeviceProfile profile = LauncherAppState.getInstance().getDynamicGrid().getDeviceProfile();
|
2015-05-12 15:36:20 -07:00
|
|
|
cellSize = (int) (profile.cellWidthPx * WIDTH_SCALE);
|
2015-05-11 14:55:07 -07:00
|
|
|
mPresetPreviewSize = (int) (profile.cellWidthPx * WIDTH_SCALE * PREVIEW_SCALE);
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|
|
|
|
|
|
2012-07-23 20:48:26 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
|
2015-05-15 17:01:14 -07:00
|
|
|
mWidgetImage = (WidgetImageView) findViewById(R.id.widget_preview);
|
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-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-05-15 15:15:11 -07:00
|
|
|
mWidgetImage.animate().cancel();
|
2015-05-15 17:01:14 -07:00
|
|
|
mWidgetImage.setBitmap(null);
|
2015-04-23 15:17:50 -07:00
|
|
|
mWidgetName.setText(null);
|
|
|
|
|
mWidgetDims.setText(null);
|
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,
|
2015-05-15 17:01:14 -07:00
|
|
|
WidgetPreviewLoader loader) {
|
2013-08-22 16:15:50 -07:00
|
|
|
LauncherAppState app = LauncherAppState.getInstance();
|
|
|
|
|
DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
|
|
|
|
|
|
2013-01-23 12:39:24 +01:00
|
|
|
mInfo = info;
|
2015-04-23 15:17:50 -07:00
|
|
|
// TODO(hyunyoungs): setup a cache for these labels.
|
|
|
|
|
mWidgetName.setText(AppWidgetManagerCompat.getInstance(getContext()).loadLabel(info));
|
2015-05-15 14:12:04 -07:00
|
|
|
int hSpan = Math.min(info.spanX, grid.numColumns);
|
|
|
|
|
int vSpan = Math.min(info.spanY, grid.numRows);
|
2015-04-23 15:17:50 -07:00
|
|
|
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) {
|
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-05-14 17:08:45 -07:00
|
|
|
|
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) {
|
2015-05-15 17:01:14 -07:00
|
|
|
if (bitmap != null) {
|
|
|
|
|
mWidgetImage.setBitmap(bitmap);
|
2015-04-23 15:17:50 -07:00
|
|
|
mWidgetImage.setAlpha(0f);
|
|
|
|
|
mWidgetImage.animate().alpha(1.0f).setDuration(FADE_IN_DURATION_MS);
|
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-05-15 14:12:04 -07:00
|
|
|
mActiveRequest = mWidgetPreviewLoader.getPreview(mInfo, size[0], size[1], this);
|
2015-03-19 14:31:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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() {
|
2015-05-08 17:00:10 -07:00
|
|
|
if (getTag() instanceof PendingAddWidgetInfo ||
|
|
|
|
|
getTag() instanceof PendingAddShortcutInfo) {
|
|
|
|
|
return getTag().toString();
|
2015-04-08 19:01:34 -07:00
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2010-12-01 15:47:31 -08:00
|
|
|
}
|