summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/WidgetPreviewLoader.java
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2017-02-07 15:06:32 -0800
committerSunny Goyal <sunnygoyal@google.com>2017-02-08 15:28:02 -0800
commit92bbfa19c379b99017e8c00418c3a461d4a9cac8 (patch)
tree737c1087c94d42bbc8ddab90cbff5eb36def4742 /src/com/android/launcher3/WidgetPreviewLoader.java
parentdb7b82960a54b85510aaeba2f543b37b4dc59fc8 (diff)
downloadandroid_packages_apps_Trebuchet-92bbfa19c379b99017e8c00418c3a461d4a9cac8.tar.gz
android_packages_apps_Trebuchet-92bbfa19c379b99017e8c00418c3a461d4a9cac8.tar.bz2
android_packages_apps_Trebuchet-92bbfa19c379b99017e8c00418c3a461d4a9cac8.zip
Updating the default widget preview generation logic
We draw a while round rect box which is divided into the cells to represent the widget size. The app icon is drawn in the center for this white box. Bug: 34819119 Change-Id: Ie4d83712577f21b276c6744a55993f71da368398
Diffstat (limited to 'src/com/android/launcher3/WidgetPreviewLoader.java')
-rw-r--r--src/com/android/launcher3/WidgetPreviewLoader.java97
1 files changed, 54 insertions, 43 deletions
diff --git a/src/com/android/launcher3/WidgetPreviewLoader.java b/src/com/android/launcher3/WidgetPreviewLoader.java
index 091ae9903..3512210c7 100644
--- a/src/com/android/launcher3/WidgetPreviewLoader.java
+++ b/src/com/android/launcher3/WidgetPreviewLoader.java
@@ -18,9 +18,8 @@ import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
-import android.graphics.Rect;
+import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
-import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Handler;
@@ -31,6 +30,7 @@ import android.util.LongSparseArray;
import com.android.launcher3.compat.AppWidgetManagerCompat;
import com.android.launcher3.compat.ShortcutConfigActivityInfo;
import com.android.launcher3.compat.UserManagerCompat;
+import com.android.launcher3.graphics.ShadowGenerator;
import com.android.launcher3.model.WidgetItem;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.Preconditions;
@@ -52,8 +52,6 @@ public class WidgetPreviewLoader {
private static final String TAG = "WidgetPreviewLoader";
private static final boolean DEBUG = false;
- private static final float WIDGET_PREVIEW_ICON_PADDING_PERCENTAGE = 0.25f;
-
private final HashMap<String, long[]> mPackageVersions = new HashMap<>();
/**
@@ -104,7 +102,7 @@ public class WidgetPreviewLoader {
* sizes (landscape vs portrait).
*/
private static class CacheDb extends SQLiteCacheHelper {
- private static final int DB_VERSION = 5;
+ private static final int DB_VERSION = 6;
private static final String TABLE_NAME = "shortcut_and_widget_previews";
private static final String COLUMN_COMPONENT = "componentName";
@@ -299,7 +297,7 @@ public class WidgetPreviewLoader {
Drawable drawable = null;
if (info.previewImage != 0) {
try {
- drawable = info.loadPreviewImage(launcher.getApplicationContext(), 0);
+ drawable = info.loadPreviewImage(mContext, 0);
} catch (OutOfMemoryError e) {
Log.w(TAG, "Error loading widget preview for: " + info.provider, e);
// During OutOfMemoryError, the previous heap stack is not affected. Catching
@@ -321,17 +319,14 @@ public class WidgetPreviewLoader {
int previewWidth;
int previewHeight;
- Bitmap tileBitmap = null;
-
if (widgetPreviewExists) {
previewWidth = drawable.getIntrinsicWidth();
previewHeight = drawable.getIntrinsicHeight();
} else {
- // Generate a preview image if we couldn't load one
- tileBitmap = ((BitmapDrawable) mContext.getResources().getDrawable(
- R.drawable.widget_tile)).getBitmap();
- previewWidth = tileBitmap.getWidth() * spanX;
- previewHeight = tileBitmap.getHeight() * spanY;
+ DeviceProfile dp = launcher.getDeviceProfile();
+ int tileSize = Math.min(dp.cellWidthPx, dp.cellHeightPx);
+ previewWidth = tileSize * spanX;
+ previewHeight = tileSize * spanY;
}
// Scale to fit width only - let the widget preview be clipped in the
@@ -371,45 +366,61 @@ public class WidgetPreviewLoader {
drawable.setBounds(x, 0, x + previewWidth, previewHeight);
drawable.draw(c);
} else {
- final Paint p = new Paint();
- p.setFilterBitmap(true);
- int appIconSize = launcher.getDeviceProfile().iconSizePx;
-
- // draw the spanX x spanY tiles
- final Rect src = new Rect(0, 0, tileBitmap.getWidth(), tileBitmap.getHeight());
-
- float tileW = scale * tileBitmap.getWidth();
- float tileH = scale * tileBitmap.getHeight();
- final RectF dst = new RectF(0, 0, tileW, tileH);
-
- float tx = x;
- for (int i = 0; i < spanX; i++, tx += tileW) {
- float ty = 0;
- for (int j = 0; j < spanY; j++, ty += tileH) {
- dst.offsetTo(tx, ty);
- c.drawBitmap(tileBitmap, src, dst, p);
- }
+ Resources res = mContext.getResources();
+ float shadowBlur = res.getDimension(R.dimen.widget_preview_shadow_blur);
+ float keyShadowDistance = res.getDimension(R.dimen.widget_preview_key_shadow_distance);
+ float corner = res.getDimension(R.dimen.widget_preview_corner_radius);
+
+ RectF boxRect = new RectF(shadowBlur, shadowBlur,
+ previewWidth - shadowBlur, previewHeight - shadowBlur - keyShadowDistance);
+
+ final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
+ p.setColor(0xFFFFFFFF);
+
+ // Key shadow
+ p.setShadowLayer(shadowBlur, 0, keyShadowDistance,
+ ShadowGenerator.KEY_SHADOW_ALPHA << 24);
+ c.drawRoundRect(boxRect, corner, corner, p);
+
+ // Ambient shadow
+ p.setShadowLayer(shadowBlur, 0, 0, ShadowGenerator.AMBIENT_SHADOW_ALPHA << 24);
+ c.drawRoundRect(boxRect, corner, corner, p);
+
+ // Draw horizontal and vertical lines to represent individual columns.
+ p.clearShadowLayer();
+ p.setStyle(Paint.Style.STROKE);
+ p.setStrokeWidth(res.getDimension(R.dimen.widget_preview_cell_divider_width));
+ p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
+
+ float t = boxRect.left;
+ float tileSize = boxRect.width() / spanX;
+ for (int i = 1; i < spanX; i++) {
+ t += tileSize;
+ c.drawLine(t, 0, t, previewHeight, p);
}
- // Draw the icon in the top left corner
- // TODO: use top right for RTL
- int minOffset = (int) (appIconSize * WIDGET_PREVIEW_ICON_PADDING_PERCENTAGE);
- int smallestSide = Math.min(previewWidth, previewHeight);
- float iconScale = Math.min((float) smallestSide / (appIconSize + 2 * minOffset), scale);
+ t = boxRect.top;
+ tileSize = boxRect.height() / spanY;
+ for (int i = 1; i < spanY; i++) {
+ t += tileSize;
+ c.drawLine(0, t, previewWidth, t, p);
+ }
+ // Draw icon in the center.
try {
Drawable icon = info.getIcon(launcher, mIconCache);
if (icon != null) {
+ int appIconSize = launcher.getDeviceProfile().iconSizePx;
+ int iconSize = (int) Math.min(appIconSize * scale,
+ Math.min(boxRect.width(), boxRect.height()));
+
icon = mutateOnMainThread(icon);
- int hoffset = (int) ((tileW - appIconSize * iconScale) / 2) + x;
- int yoffset = (int) ((tileH - appIconSize * iconScale) / 2);
- icon.setBounds(hoffset, yoffset,
- hoffset + (int) (appIconSize * iconScale),
- yoffset + (int) (appIconSize * iconScale));
+ int hoffset = (previewWidth - iconSize) / 2;
+ int yoffset = (previewHeight - iconSize) / 2;
+ icon.setBounds(hoffset, yoffset, hoffset + iconSize, yoffset + iconSize);
icon.draw(c);
}
- } catch (Resources.NotFoundException e) {
- }
+ } catch (Resources.NotFoundException e) { }
c.setBitmap(null);
}
return preview;