From ffe83f13319cdb833a25596d47cf1af098cc4c60 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Thu, 14 Aug 2014 17:39:34 -0700 Subject: Adding support for managed profiles in widget issue: 16892811 Change-Id: If8c81f2a5a3bc846b02a5d225c5a8f27b1382dab --- .../android/launcher3/AppsCustomizePagedView.java | 37 +--- src/com/android/launcher3/IconCache.java | 4 + src/com/android/launcher3/Launcher.java | 41 ++-- .../android/launcher3/LauncherBackupHelper.java | 47 +++-- src/com/android/launcher3/LauncherModel.java | 80 ++------ src/com/android/launcher3/PagedViewWidget.java | 4 +- src/com/android/launcher3/Utilities.java | 2 +- src/com/android/launcher3/WidgetPreviewLoader.java | 207 ++++++++++----------- .../launcher3/compat/AppWidgetManagerCompat.java | 83 +++++++++ .../compat/AppWidgetManagerCompatV16.java | 90 +++++++++ .../launcher3/compat/AppWidgetManagerCompatVL.java | 142 ++++++++++++++ 11 files changed, 480 insertions(+), 257 deletions(-) create mode 100644 src/com/android/launcher3/compat/AppWidgetManagerCompat.java create mode 100644 src/com/android/launcher3/compat/AppWidgetManagerCompatV16.java create mode 100644 src/com/android/launcher3/compat/AppWidgetManagerCompatVL.java (limited to 'src/com') diff --git a/src/com/android/launcher3/AppsCustomizePagedView.java b/src/com/android/launcher3/AppsCustomizePagedView.java index 1ab11ee8a..9f9c34bf4 100644 --- a/src/com/android/launcher3/AppsCustomizePagedView.java +++ b/src/com/android/launcher3/AppsCustomizePagedView.java @@ -28,7 +28,6 @@ import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Bitmap; -import android.graphics.Canvas; import android.graphics.Point; import android.graphics.Rect; import android.graphics.drawable.Drawable; @@ -49,6 +48,7 @@ import android.widget.ImageView; import android.widget.Toast; import com.android.launcher3.DropTarget.DragObject; +import com.android.launcher3.compat.AppWidgetManagerCompat; import java.util.ArrayList; import java.util.Collections; @@ -176,7 +176,6 @@ public class AppsCustomizePagedView extends PagedViewWithDraggableItems implemen private int mClingFocusedY; // Caching - private Canvas mCanvas; private IconCache mIconCache; // Dimens @@ -224,13 +223,10 @@ public class AppsCustomizePagedView extends PagedViewWithDraggableItems implemen mApps = new ArrayList(); mWidgets = new ArrayList(); mIconCache = (LauncherAppState.getInstance()).getIconCache(); - mCanvas = new Canvas(); mRunningTasks = new ArrayList(); // Save the default widget preview background TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppsCustomizePagedView, 0, 0); - LauncherAppState app = LauncherAppState.getInstance(); - DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); mWidgetCountX = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountX, 2); mWidgetCountY = a.getInt(R.styleable.AppsCustomizePagedView_widgetCountY, 2); mClingFocusedX = a.getInt(R.styleable.AppsCustomizePagedView_clingFocusedX, 0); @@ -391,7 +387,6 @@ public class AppsCustomizePagedView extends PagedViewWithDraggableItems implemen if (!app.shouldShowAppOrWidgetProvider(widget.provider)) { continue; } - widget.label = widget.label.trim(); if (widget.minWidth > 0 && widget.minHeight > 0) { // Ensure that all widgets we show can be added on a workspace of this size int[] spanXY = Launcher.getSpanForWidget(mLauncher, widget); @@ -520,18 +515,9 @@ public class AppsCustomizePagedView extends PagedViewWithDraggableItems implemen @Override public void run() { mWidgetLoadingId = mLauncher.getAppWidgetHost().allocateAppWidgetId(); - // Options will be null for platforms with JB or lower, so this serves as an - // SDK level check. - if (options == null) { - if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed( - mWidgetLoadingId, info.componentName)) { - mWidgetCleanupState = WIDGET_BOUND; - } - } else { - if (AppWidgetManager.getInstance(mLauncher).bindAppWidgetIdIfAllowed( - mWidgetLoadingId, info.componentName, options)) { - mWidgetCleanupState = WIDGET_BOUND; - } + if(AppWidgetManagerCompat.getInstance(mLauncher).bindAppWidgetIdIfAllowed( + mWidgetLoadingId, pInfo, options)) { + mWidgetCleanupState = WIDGET_BOUND; } } }; @@ -659,9 +645,8 @@ public class AppsCustomizePagedView extends PagedViewWithDraggableItems implemen int[] previewSizeBeforeScale = new int[1]; - preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.componentName, - createWidgetInfo.previewImage, createWidgetInfo.icon, spanX, spanY, - maxWidth, maxHeight, null, previewSizeBeforeScale); + preview = getWidgetPreviewLoader().generateWidgetPreview(createWidgetInfo.info, + spanX, spanY, maxWidth, maxHeight, null, previewSizeBeforeScale); // Compare the size of the drag preview to the preview in the AppsCustomize tray int previewWidthInAppsCustomize = Math.min(previewSizeBeforeScale[0], @@ -678,15 +663,7 @@ public class AppsCustomizePagedView extends PagedViewWithDraggableItems implemen } else { PendingAddShortcutInfo createShortcutInfo = (PendingAddShortcutInfo) v.getTag(); Drawable icon = mIconCache.getFullResIcon(createShortcutInfo.shortcutActivityInfo); - preview = Bitmap.createBitmap(icon.getIntrinsicWidth(), - icon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); - - mCanvas.setBitmap(preview); - mCanvas.save(); - WidgetPreviewLoader.renderDrawableToBitmap(icon, preview, 0, 0, - icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); - mCanvas.restore(); - mCanvas.setBitmap(null); + preview = Utilities.createIconBitmap(icon, mLauncher); createItemInfo.spanX = createItemInfo.spanY = 1; } diff --git a/src/com/android/launcher3/IconCache.java b/src/com/android/launcher3/IconCache.java index 06b77756d..76a85caae 100644 --- a/src/com/android/launcher3/IconCache.java +++ b/src/com/android/launcher3/IconCache.java @@ -146,6 +146,10 @@ public class IconCache { return getFullResDefaultActivityIcon(); } + public int getFullResIconDpi() { + return mIconDpi; + } + public Drawable getFullResIcon(ResolveInfo info) { return getFullResIcon(info.activityInfo); } diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index b8571008b..5d9ff1d55 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -99,6 +99,7 @@ import android.widget.Toast; import com.android.launcher3.DropTarget.DragObject; import com.android.launcher3.PagedView.PageSwitchListener; +import com.android.launcher3.compat.AppWidgetManagerCompat; import com.android.launcher3.compat.LauncherActivityInfoCompat; import com.android.launcher3.compat.LauncherAppsCompat; import com.android.launcher3.compat.PackageInstallerCompat; @@ -257,7 +258,7 @@ public class Launcher extends Activity private View mWeightWatcher; private LauncherClings mLauncherClings; - private AppWidgetManager mAppWidgetManager; + private AppWidgetManagerCompat mAppWidgetManager; private LauncherAppWidgetHost mAppWidgetHost; private ItemInfo mPendingAddInfo = new ItemInfo(); @@ -438,7 +439,7 @@ public class Launcher extends Activity mStats = new Stats(this); - mAppWidgetManager = AppWidgetManager.getInstance(this); + mAppWidgetManager = AppWidgetManagerCompat.getInstance(this); mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID); mAppWidgetHost.startListening(); @@ -1569,6 +1570,7 @@ public class Launcher extends Activity launcherInfo.spanY = spanXY[1]; launcherInfo.minSpanX = mPendingAddInfo.minSpanX; launcherInfo.minSpanY = mPendingAddInfo.minSpanY; + launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo); LauncherModel.addItemToDatabase(this, launcherInfo, container, screenId, cellXY[0], cellXY[1], false); @@ -2194,10 +2196,9 @@ public class Launcher extends Activity mPendingAddWidgetId = appWidgetId; // Launch over to configure widget, if needed - Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); - intent.setComponent(appWidgetInfo.configure); - intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); - Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET); + mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this, + mAppWidgetHost, REQUEST_CREATE_APPWIDGET); + } else { // Otherwise just add it Runnable onComplete = new Runnable() { @@ -2281,14 +2282,8 @@ public class Launcher extends Activity appWidgetId = getAppWidgetHost().allocateAppWidgetId(); Bundle options = info.bindOptions; - boolean success = false; - if (options != null) { - success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, - info.componentName, options); - } else { - success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, - info.componentName); - } + boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed( + appWidgetId, info.info, options); if (success) { addAppWidgetImpl(appWidgetId, info, null, info.info); } else { @@ -2296,6 +2291,8 @@ public class Launcher extends Activity Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName); + mAppWidgetManager.getUser(mPendingAddWidgetInfo) + .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE); // TODO: we need to make sure that this accounts for the options bundle. // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options); startActivityForResult(intent, REQUEST_BIND_APPWIDGET); @@ -2476,10 +2473,8 @@ public class Launcher extends Activity mPendingAddInfo.copyFrom(info); mPendingAddWidgetId = widgetId; - Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); - intent.setComponent(appWidgetInfo.configure); - intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, info.appWidgetId); - Utilities.startActivityForResultSafely(this, intent, REQUEST_RECONFIGURE_APPWIDGET); + AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo, + info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET); } } } @@ -4378,15 +4373,9 @@ public class Launcher extends Activity Bundle options = AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo); - boolean success = false; int newWidgetId = mAppWidgetHost.allocateAppWidgetId(); - if (options != null) { - success = mAppWidgetManager.bindAppWidgetIdIfAllowed(newWidgetId, - appWidgetInfo.provider, options); - } else { - success = mAppWidgetManager.bindAppWidgetIdIfAllowed(newWidgetId, - appWidgetInfo.provider); - } + boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed( + newWidgetId, appWidgetInfo, options); // TODO consider showing a permission dialog when the widget is clicked. if (!success) { diff --git a/src/com/android/launcher3/LauncherBackupHelper.java b/src/com/android/launcher3/LauncherBackupHelper.java index 64e82c754..1ea562b3f 100644 --- a/src/com/android/launcher3/LauncherBackupHelper.java +++ b/src/com/android/launcher3/LauncherBackupHelper.java @@ -299,36 +299,26 @@ public class LauncherBackupHelper implements BackupHelper { Set savedIds = getSavedIdsByType(Key.FAVORITE, in); if (DEBUG) Log.d(TAG, "favorite savedIds.size()=" + savedIds.size()); - // Don't backup apps in other profiles for now. - UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle(); - long userSerialNumber = - UserManagerCompat.getInstance(mContext).getSerialNumberForUser(myUserHandle); - // persist things that have changed since the last backup ContentResolver cr = mContext.getContentResolver(); + // Don't backup apps in other profiles for now. Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, - null, null, null); + getUserSelectionArg(), null, null); Set currentIds = new HashSet(cursor.getCount()); try { cursor.moveToPosition(-1); while(cursor.moveToNext()) { final long id = cursor.getLong(ID_INDEX); - final long profileId = cursor.getLong(PROFILE_ID_INDEX); - if (userSerialNumber == profileId) { - final long updateTime = cursor.getLong(ID_MODIFIED); - Key key = getKey(Key.FAVORITE, id); - keys.add(key); - final String backupKey = keyToBackupKey(key); - currentIds.add(backupKey); - if (!savedIds.contains(backupKey) || updateTime >= in.t) { - byte[] blob = packFavorite(cursor); - writeRowToBackup(key, blob, out, data); - } else { - if (VERBOSE) Log.v(TAG, "favorite " + id + " was too old: " + updateTime); - } + final long updateTime = cursor.getLong(ID_MODIFIED); + Key key = getKey(Key.FAVORITE, id); + keys.add(key); + final String backupKey = keyToBackupKey(key); + currentIds.add(backupKey); + if (!savedIds.contains(backupKey) || updateTime >= in.t) { + byte[] blob = packFavorite(cursor); + writeRowToBackup(key, blob, out, data); } else { - if (VERBOSE) Log.v(TAG, "favorite " + id + " is for other profile: " - + profileId); + if (VERBOSE) Log.v(TAG, "favorite " + id + " was too old: " + updateTime); } } } finally { @@ -469,20 +459,19 @@ public class LauncherBackupHelper implements BackupHelper { } final ContentResolver cr = mContext.getContentResolver(); final int dpi = mContext.getResources().getDisplayMetrics().densityDpi; + final UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle(); // read the old ID set Set savedIds = getSavedIdsByType(Key.ICON, in); if (DEBUG) Log.d(TAG, "icon savedIds.size()=" + savedIds.size()); // Don't backup apps in other profiles for now. - UserHandleCompat myUserHandle = UserHandleCompat.myUserHandle(); - long userSerialNumber = - UserManagerCompat.getInstance(mContext).getSerialNumberForUser(myUserHandle); int startRows = out.rows; if (DEBUG) Log.d(TAG, "starting here: " + startRows); + String where = "(" + Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPLICATION + " OR " + Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_SHORTCUT + ") AND " + - Favorites.PROFILE_ID + "=" + userSerialNumber; + getUserSelectionArg(); Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, where, null, null); Set currentIds = new HashSet(cursor.getCount()); @@ -617,7 +606,8 @@ public class LauncherBackupHelper implements BackupHelper { int startRows = out.rows; if (DEBUG) Log.d(TAG, "starting here: " + startRows); - String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET; + String where = Favorites.ITEM_TYPE + "=" + Favorites.ITEM_TYPE_APPWIDGET + " AND " + + getUserSelectionArg(); Cursor cursor = cr.query(Favorites.CONTENT_URI, FAVORITE_PROJECTION, where, null, null); Set currentIds = new HashSet(cursor.getCount()); @@ -1192,6 +1182,11 @@ public class LauncherBackupHelper implements BackupHelper { return true; } + private String getUserSelectionArg() { + return Favorites.PROFILE_ID + '=' + UserManagerCompat.getInstance(mContext) + .getSerialNumberForUser(UserHandleCompat.myUserHandle()); + } + private class KeyParsingException extends Throwable { private KeyParsingException(Throwable cause) { super(cause); diff --git a/src/com/android/launcher3/LauncherModel.java b/src/com/android/launcher3/LauncherModel.java index 4c9d1a700..bcb45011e 100644 --- a/src/com/android/launcher3/LauncherModel.java +++ b/src/com/android/launcher3/LauncherModel.java @@ -30,7 +30,6 @@ import android.content.Intent; import android.content.Intent.ShortcutIconResource; import android.content.IntentFilter; import android.content.SharedPreferences; -import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.pm.ProviderInfo; import android.content.pm.ResolveInfo; @@ -52,7 +51,7 @@ import android.text.TextUtils; import android.util.Log; import android.util.Pair; -import com.android.launcher3.InstallWidgetReceiver.WidgetMimeTypeHandlerData; +import com.android.launcher3.compat.AppWidgetManagerCompat; import com.android.launcher3.compat.LauncherActivityInfoCompat; import com.android.launcher3.compat.LauncherAppsCompat; import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo; @@ -3040,11 +3039,11 @@ public class LauncherModel extends BroadcastReceiver public static ArrayList getSortedWidgetsAndShortcuts(Context context) { PackageManager packageManager = context.getPackageManager(); final ArrayList widgetsAndShortcuts = new ArrayList(); - widgetsAndShortcuts.addAll(AppWidgetManager.getInstance(context).getInstalledProviders()); + widgetsAndShortcuts.addAll(AppWidgetManagerCompat.getInstance(context).getAllProviders()); + Intent shortcutsIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT); widgetsAndShortcuts.addAll(packageManager.queryIntentActivities(shortcutsIntent, 0)); - Collections.sort(widgetsAndShortcuts, - new LauncherModel.WidgetAndShortcutNameComparator(packageManager)); + Collections.sort(widgetsAndShortcuts, new WidgetAndShortcutNameComparator(context)); return widgetsAndShortcuts; } @@ -3390,44 +3389,6 @@ public class LauncherModel extends BroadcastReceiver return null; } - /** - * Returns a list of all the widgets that can handle configuration with a particular mimeType. - */ - List resolveWidgetsForMimeType(Context context, String mimeType) { - final PackageManager packageManager = context.getPackageManager(); - final List supportedConfigurationActivities = - new ArrayList(); - - final Intent supportsIntent = - new Intent(InstallWidgetReceiver.ACTION_SUPPORTS_CLIPDATA_MIMETYPE); - supportsIntent.setType(mimeType); - - // Create a set of widget configuration components that we can test against - final List widgets = - AppWidgetManager.getInstance(context).getInstalledProviders(); - final HashMap configurationComponentToWidget = - new HashMap(); - for (AppWidgetProviderInfo info : widgets) { - configurationComponentToWidget.put(info.configure, info); - } - - // Run through each of the intents that can handle this type of clip data, and cross - // reference them with the components that are actual configuration components - final List activities = packageManager.queryIntentActivities(supportsIntent, - PackageManager.MATCH_DEFAULT_ONLY); - for (ResolveInfo info : activities) { - final ActivityInfo activityInfo = info.activityInfo; - final ComponentName infoComponent = new ComponentName(activityInfo.packageName, - activityInfo.name); - if (configurationComponentToWidget.containsKey(infoComponent)) { - supportedConfigurationActivities.add( - new InstallWidgetReceiver.WidgetMimeTypeHandlerData(info, - configurationComponentToWidget.get(infoComponent))); - } - } - return supportedConfigurationActivities; - } - ShortcutInfo infoFromShortcutIntent(Context context, Intent data, Bitmap fallbackIcon) { Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); @@ -3570,14 +3531,6 @@ public class LauncherModel extends BroadcastReceiver return 0; } }; - public static final Comparator getWidgetNameComparator() { - final Collator collator = Collator.getInstance(); - return new Comparator() { - public final int compare(AppWidgetProviderInfo a, AppWidgetProviderInfo b) { - return collator.compare(a.label.toString().trim(), b.label.toString().trim()); - } - }; - } static ComponentName getComponentNameFromResolveInfo(ResolveInfo info) { if (info.activityInfo != null) { return new ComponentName(info.activityInfo.packageName, info.activityInfo.name); @@ -3618,11 +3571,14 @@ public class LauncherModel extends BroadcastReceiver } }; public static class WidgetAndShortcutNameComparator implements Comparator { - private Collator mCollator; - private PackageManager mPackageManager; - private HashMap mLabelCache; - WidgetAndShortcutNameComparator(PackageManager pm) { - mPackageManager = pm; + private final AppWidgetManagerCompat mManager; + private final PackageManager mPackageManager; + private final HashMap mLabelCache; + private final Collator mCollator; + + WidgetAndShortcutNameComparator(Context context) { + mManager = AppWidgetManagerCompat.getInstance(context); + mPackageManager = context.getPackageManager(); mLabelCache = new HashMap(); mCollator = Collator.getInstance(); } @@ -3631,17 +3587,17 @@ public class LauncherModel extends BroadcastReceiver if (mLabelCache.containsKey(a)) { labelA = mLabelCache.get(a); } else { - labelA = (a instanceof AppWidgetProviderInfo) ? - ((AppWidgetProviderInfo) a).label : - ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim(); + labelA = (a instanceof AppWidgetProviderInfo) + ? mManager.loadLabel((AppWidgetProviderInfo) a) + : ((ResolveInfo) a).loadLabel(mPackageManager).toString().trim(); mLabelCache.put(a, labelA); } if (mLabelCache.containsKey(b)) { labelB = mLabelCache.get(b); } else { - labelB = (b instanceof AppWidgetProviderInfo) ? - ((AppWidgetProviderInfo) b).label : - ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim(); + labelB = (b instanceof AppWidgetProviderInfo) + ? mManager.loadLabel((AppWidgetProviderInfo) b) + : ((ResolveInfo) b).loadLabel(mPackageManager).toString().trim(); mLabelCache.put(b, labelB); } return mCollator.compare(labelA, labelB); diff --git a/src/com/android/launcher3/PagedViewWidget.java b/src/com/android/launcher3/PagedViewWidget.java index db4aeb940..e6e11a312 100644 --- a/src/com/android/launcher3/PagedViewWidget.java +++ b/src/com/android/launcher3/PagedViewWidget.java @@ -30,6 +30,8 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; +import com.android.launcher3.compat.AppWidgetManagerCompat; + /** * The linear layout used strictly for the widget/wallpaper tab of the customization tray */ @@ -127,7 +129,7 @@ public class PagedViewWidget extends LinearLayout { image.setMaxWidth(maxWidth); } final TextView name = (TextView) findViewById(R.id.widget_name); - name.setText(info.label); + name.setText(AppWidgetManagerCompat.getInstance(getContext()).loadLabel(info)); final TextView dims = (TextView) findViewById(R.id.widget_dims); if (dims != null) { int hSpan = Math.min(cellSpan[0], (int) grid.numColumns); diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java index f70fc4d86..f20f261f9 100644 --- a/src/com/android/launcher3/Utilities.java +++ b/src/com/android/launcher3/Utilities.java @@ -78,7 +78,7 @@ public final class Utilities { /** * Returns a FastBitmapDrawable with the icon, accurately sized. */ - static FastBitmapDrawable createIconDrawable(Bitmap icon) { + public static FastBitmapDrawable createIconDrawable(Bitmap icon) { FastBitmapDrawable d = new FastBitmapDrawable(icon); d.setFilterBitmap(true); resizeIconDrawable(d); diff --git a/src/com/android/launcher3/WidgetPreviewLoader.java b/src/com/android/launcher3/WidgetPreviewLoader.java index 1b37700c6..5aa719027 100644 --- a/src/com/android/launcher3/WidgetPreviewLoader.java +++ b/src/com/android/launcher3/WidgetPreviewLoader.java @@ -5,7 +5,6 @@ import android.content.ComponentName; import android.content.ContentValues; import android.content.Context; import android.content.SharedPreferences; -import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.database.Cursor; @@ -29,6 +28,8 @@ import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.util.Log; +import com.android.launcher3.compat.AppWidgetManagerCompat; + import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -42,124 +43,123 @@ import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; -abstract class SoftReferenceThreadLocal { - private ThreadLocal> mThreadLocal; - public SoftReferenceThreadLocal() { - mThreadLocal = new ThreadLocal>(); - } +public class WidgetPreviewLoader { - abstract T initialValue(); + private static abstract class SoftReferenceThreadLocal { + private ThreadLocal> mThreadLocal; + public SoftReferenceThreadLocal() { + mThreadLocal = new ThreadLocal>(); + } - public void set(T t) { - mThreadLocal.set(new SoftReference(t)); - } + abstract T initialValue(); - public T get() { - SoftReference reference = mThreadLocal.get(); - T obj; - if (reference == null) { - obj = initialValue(); - mThreadLocal.set(new SoftReference(obj)); - return obj; - } else { - obj = reference.get(); - if (obj == null) { + public void set(T t) { + mThreadLocal.set(new SoftReference(t)); + } + + public T get() { + SoftReference reference = mThreadLocal.get(); + T obj; + if (reference == null) { obj = initialValue(); mThreadLocal.set(new SoftReference(obj)); + return obj; + } else { + obj = reference.get(); + if (obj == null) { + obj = initialValue(); + mThreadLocal.set(new SoftReference(obj)); + } + return obj; } - return obj; } } -} -class CanvasCache extends SoftReferenceThreadLocal { - @Override - protected Canvas initialValue() { - return new Canvas(); + private static class CanvasCache extends SoftReferenceThreadLocal { + @Override + protected Canvas initialValue() { + return new Canvas(); + } } -} -class PaintCache extends SoftReferenceThreadLocal { - @Override - protected Paint initialValue() { - return null; + private static class PaintCache extends SoftReferenceThreadLocal { + @Override + protected Paint initialValue() { + return null; + } } -} -class BitmapCache extends SoftReferenceThreadLocal { - @Override - protected Bitmap initialValue() { - return null; + private static class BitmapCache extends SoftReferenceThreadLocal { + @Override + protected Bitmap initialValue() { + return null; + } } -} -class RectCache extends SoftReferenceThreadLocal { - @Override - protected Rect initialValue() { - return new Rect(); + private static class RectCache extends SoftReferenceThreadLocal { + @Override + protected Rect initialValue() { + return new Rect(); + } } -} -class BitmapFactoryOptionsCache extends SoftReferenceThreadLocal { - @Override - protected BitmapFactory.Options initialValue() { - return new BitmapFactory.Options(); + private static class BitmapFactoryOptionsCache extends + SoftReferenceThreadLocal { + @Override + protected BitmapFactory.Options initialValue() { + return new BitmapFactory.Options(); + } } -} -public class WidgetPreviewLoader { - static final String TAG = "WidgetPreviewLoader"; - static final String ANDROID_INCREMENTAL_VERSION_NAME_KEY = "android.incremental.version"; + private static final String TAG = "WidgetPreviewLoader"; + private static final String ANDROID_INCREMENTAL_VERSION_NAME_KEY = "android.incremental.version"; + + private static final float WIDGET_PREVIEW_ICON_PADDING_PERCENTAGE = 0.25f; + private static final HashSet sInvalidPackages = new HashSet(); + + // Used for drawing shortcut previews + private final BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache(); + private final PaintCache mCachedShortcutPreviewPaint = new PaintCache(); + private final CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache(); + + // Used for drawing widget previews + private final CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache(); + private final RectCache mCachedAppWidgetPreviewSrcRect = new RectCache(); + private final RectCache mCachedAppWidgetPreviewDestRect = new RectCache(); + private final PaintCache mCachedAppWidgetPreviewPaint = new PaintCache(); + private final PaintCache mDefaultAppWidgetPreviewPaint = new PaintCache(); + private final BitmapFactoryOptionsCache mCachedBitmapFactoryOptions = new BitmapFactoryOptionsCache(); + + private final HashMap> mLoadedPreviews = new HashMap<>(); + private final ArrayList> mUnusedBitmaps = new ArrayList<>(); + + private final Context mContext; + private final int mAppIconSize; + private final IconCache mIconCache; + private final AppWidgetManagerCompat mManager; private int mPreviewBitmapWidth; private int mPreviewBitmapHeight; private String mSize; - private Context mContext; - private PackageManager mPackageManager; private PagedViewCellLayout mWidgetSpacingLayout; - // Used for drawing shortcut previews - private BitmapCache mCachedShortcutPreviewBitmap = new BitmapCache(); - private PaintCache mCachedShortcutPreviewPaint = new PaintCache(); - private CanvasCache mCachedShortcutPreviewCanvas = new CanvasCache(); - - // Used for drawing widget previews - private CanvasCache mCachedAppWidgetPreviewCanvas = new CanvasCache(); - private RectCache mCachedAppWidgetPreviewSrcRect = new RectCache(); - private RectCache mCachedAppWidgetPreviewDestRect = new RectCache(); - private PaintCache mCachedAppWidgetPreviewPaint = new PaintCache(); - private PaintCache mDefaultAppWidgetPreviewPaint = new PaintCache(); private String mCachedSelectQuery; - private BitmapFactoryOptionsCache mCachedBitmapFactoryOptions = new BitmapFactoryOptionsCache(); - private int mAppIconSize; - private IconCache mIconCache; - - private static final float sWidgetPreviewIconPaddingPercentage = 0.25f; private CacheDb mDb; - private final HashMap> mLoadedPreviews; - private final ArrayList> mUnusedBitmaps; - private final static HashSet sInvalidPackages; - private final MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor(); - static { - sInvalidPackages = new HashSet(); - } - public WidgetPreviewLoader(Context context) { LauncherAppState app = LauncherAppState.getInstance(); DeviceProfile grid = app.getDynamicGrid().getDeviceProfile(); mContext = context; - mPackageManager = mContext.getPackageManager(); mAppIconSize = grid.iconSizePx; mIconCache = app.getIconCache(); + mManager = AppWidgetManagerCompat.getInstance(context); + mDb = app.getWidgetPreviewCacheDb(); - mLoadedPreviews = new HashMap>(); - mUnusedBitmaps = new ArrayList>(); SharedPreferences sp = context.getSharedPreferences( LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE); @@ -175,7 +175,7 @@ public class WidgetPreviewLoader { editor.commit(); } } - + public void recreateDb() { LauncherAppState app = LauncherAppState.getInstance(); app.recreateWidgetPreviewDb(); @@ -328,7 +328,7 @@ public class WidgetPreviewLoader { String output; if (o instanceof AppWidgetProviderInfo) { sb.append(WIDGET_PREFIX); - sb.append(((AppWidgetProviderInfo) o).provider.flattenToString()); + sb.append(((AppWidgetProviderInfo) o).toString()); output = sb.toString(); sb.setLength(0); } else { @@ -413,7 +413,7 @@ public class WidgetPreviewLoader { }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null); } - public static void removeItemFromDb(final CacheDb cacheDb, final String objectName) { + private static void removeItemFromDb(final CacheDb cacheDb, final String objectName) { new AsyncTask() { public Void doInBackground(Void ... args) { SQLiteDatabase db = cacheDb.getWritableDatabase(); @@ -473,7 +473,7 @@ public class WidgetPreviewLoader { } } - public Bitmap generatePreview(Object info, Bitmap preview) { + private Bitmap generatePreview(Object info, Bitmap preview) { if (preview != null && (preview.getWidth() != mPreviewBitmapWidth || preview.getHeight() != mPreviewBitmapHeight)) { @@ -491,8 +491,8 @@ public class WidgetPreviewLoader { int[] cellSpans = Launcher.getSpanForWidget(mContext, info); int maxWidth = maxWidthForWidgetPreview(cellSpans[0]); int maxHeight = maxHeightForWidgetPreview(cellSpans[1]); - return generateWidgetPreview(info.provider, info.previewImage, info.icon, - cellSpans[0], cellSpans[1], maxWidth, maxHeight, preview, null); + return generateWidgetPreview(info, cellSpans[0], cellSpans[1], + maxWidth, maxHeight, preview, null); } public int maxWidthForWidgetPreview(int spanX) { @@ -505,22 +505,20 @@ public class WidgetPreviewLoader { mWidgetSpacingLayout.estimateCellHeight(spanY)); } - public Bitmap generateWidgetPreview(ComponentName provider, int previewImage, - int iconId, int cellHSpan, int cellVSpan, int maxPreviewWidth, int maxPreviewHeight, - Bitmap preview, int[] preScaledWidthOut) { + public Bitmap generateWidgetPreview(AppWidgetProviderInfo info, int cellHSpan, int cellVSpan, + int maxPreviewWidth, int maxPreviewHeight, Bitmap preview, int[] preScaledWidthOut) { // Load the preview image if possible - String packageName = provider.getPackageName(); if (maxPreviewWidth < 0) maxPreviewWidth = Integer.MAX_VALUE; if (maxPreviewHeight < 0) maxPreviewHeight = Integer.MAX_VALUE; Drawable drawable = null; - if (previewImage != 0) { - drawable = mPackageManager.getDrawable(packageName, previewImage, null); + if (info.previewImage != 0) { + drawable = mManager.loadPreview(info); if (drawable != null) { drawable = mutateOnMainThread(drawable); } else { Log.w(TAG, "Can't load widget preview drawable 0x" + - Integer.toHexString(previewImage) + " for provider: " + provider); + Integer.toHexString(info.previewImage) + " for provider: " + info.provider); } } @@ -562,21 +560,16 @@ public class WidgetPreviewLoader { c.setBitmap(null); // Draw the icon in the top left corner - int minOffset = (int) (mAppIconSize * sWidgetPreviewIconPaddingPercentage); + int minOffset = (int) (mAppIconSize * WIDGET_PREVIEW_ICON_PADDING_PERCENTAGE); int smallestSide = Math.min(previewWidth, previewHeight); float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f); try { - Drawable icon = null; - int hoffset = - (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2); - int yoffset = - (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2); - if (iconId > 0) { - icon = mIconCache.getFullResIcon(packageName, iconId); - } + Drawable icon = mManager.loadIcon(info, mIconCache); if (icon != null) { + int hoffset = (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2); + int yoffset = (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2); icon = mutateOnMainThread(icon); renderDrawableToBitmap(icon, defaultPreview, hoffset, yoffset, (int) (mAppIconSize * iconScale), @@ -627,7 +620,7 @@ public class WidgetPreviewLoader { c.drawBitmap(defaultPreview, src, dest, p); c.setBitmap(null); } - return preview; + return mManager.getBadgeBitmap(info, preview); } private Bitmap generateShortcutPreview( @@ -685,18 +678,10 @@ public class WidgetPreviewLoader { return preview; } - - public static void renderDrawableToBitmap( - Drawable d, Bitmap bitmap, int x, int y, int w, int h) { - renderDrawableToBitmap(d, bitmap, x, y, w, h, 1f); - } - private static void renderDrawableToBitmap( - Drawable d, Bitmap bitmap, int x, int y, int w, int h, - float scale) { + Drawable d, Bitmap bitmap, int x, int y, int w, int h) { if (bitmap != null) { Canvas c = new Canvas(bitmap); - c.scale(scale, scale); Rect oldBounds = d.copyBounds(); d.setBounds(x, y, x + w, y + h); d.draw(c); diff --git a/src/com/android/launcher3/compat/AppWidgetManagerCompat.java b/src/com/android/launcher3/compat/AppWidgetManagerCompat.java new file mode 100644 index 000000000..57fac7f8f --- /dev/null +++ b/src/com/android/launcher3/compat/AppWidgetManagerCompat.java @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2014 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.launcher3.compat; + +import android.app.Activity; +import android.appwidget.AppWidgetHost; +import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProviderInfo; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; +import android.os.Bundle; + +import com.android.launcher3.IconCache; +import com.android.launcher3.Utilities; + +import java.util.List; + +public abstract class AppWidgetManagerCompat { + + private static final Object sInstanceLock = new Object(); + private static AppWidgetManagerCompat sInstance; + + + public static AppWidgetManagerCompat getInstance(Context context) { + synchronized (sInstanceLock) { + // TODO change this to use api version once L gets an API number. + if (sInstance == null) { + if (Utilities.isLmp()) { + sInstance = new AppWidgetManagerCompatVL(context); + } else { + sInstance = new AppWidgetManagerCompatV16(context); + } + } + return sInstance; + } + } + + final AppWidgetManager mAppWidgetManager; + final Context mContext; + + AppWidgetManagerCompat(Context context) { + mContext = context; + mAppWidgetManager = AppWidgetManager.getInstance(context); + } + + public AppWidgetProviderInfo getAppWidgetInfo(int appWidgetId) { + return mAppWidgetManager.getAppWidgetInfo(appWidgetId); + } + + public abstract List getAllProviders(); + + public abstract String loadLabel(AppWidgetProviderInfo info); + + public abstract boolean bindAppWidgetIdIfAllowed( + int appWidgetId, AppWidgetProviderInfo info, Bundle options); + + public abstract UserHandleCompat getUser(AppWidgetProviderInfo info); + + public abstract void startConfigActivity(AppWidgetProviderInfo info, int widgetId, + Activity activity, AppWidgetHost host, int requestCode); + + public abstract Drawable loadPreview(AppWidgetProviderInfo info); + + public abstract Drawable loadIcon(AppWidgetProviderInfo info, IconCache cache); + + public abstract Bitmap getBadgeBitmap(AppWidgetProviderInfo info, Bitmap bitmap); + +} diff --git a/src/com/android/launcher3/compat/AppWidgetManagerCompatV16.java b/src/com/android/launcher3/compat/AppWidgetManagerCompatV16.java new file mode 100644 index 000000000..f599f4303 --- /dev/null +++ b/src/com/android/launcher3/compat/AppWidgetManagerCompatV16.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2014 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.launcher3.compat; + +import android.app.Activity; +import android.appwidget.AppWidgetHost; +import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProviderInfo; +import android.content.Context; +import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.os.Bundle; + +import com.android.launcher3.IconCache; +import com.android.launcher3.Utilities; + +import java.util.List; + +class AppWidgetManagerCompatV16 extends AppWidgetManagerCompat { + + AppWidgetManagerCompatV16(Context context) { + super(context); + } + + @Override + public List getAllProviders() { + return mAppWidgetManager.getInstalledProviders(); + } + + @Override + public String loadLabel(AppWidgetProviderInfo info) { + return info.label.trim(); + } + + @Override + public boolean bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info, + Bundle options) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { + return mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.provider); + } else { + return mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.provider, options); + } + } + + @Override + public UserHandleCompat getUser(AppWidgetProviderInfo info) { + return UserHandleCompat.myUserHandle(); + } + + @Override + public void startConfigActivity(AppWidgetProviderInfo info, int widgetId, Activity activity, + AppWidgetHost host, int requestCode) { + Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); + intent.setComponent(info.configure); + intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId); + Utilities.startActivityForResultSafely(activity, intent, requestCode); + } + + @Override + public Drawable loadPreview(AppWidgetProviderInfo info) { + return mContext.getPackageManager().getDrawable( + info.provider.getPackageName(), info.previewImage, null); + } + + @Override + public Drawable loadIcon(AppWidgetProviderInfo info, IconCache cache) { + return cache.getFullResIcon(info.provider.getPackageName(), info.icon); + } + + @Override + public Bitmap getBadgeBitmap(AppWidgetProviderInfo info, Bitmap bitmap) { + return bitmap; + } +} diff --git a/src/com/android/launcher3/compat/AppWidgetManagerCompatVL.java b/src/com/android/launcher3/compat/AppWidgetManagerCompatVL.java new file mode 100644 index 000000000..535c74bbd --- /dev/null +++ b/src/com/android/launcher3/compat/AppWidgetManagerCompatVL.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2014 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.launcher3.compat; + +import android.annotation.TargetApi; +import android.app.Activity; +import android.appwidget.AppWidgetHost; +import android.appwidget.AppWidgetProviderInfo; +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.pm.PackageManager; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Rect; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.os.Bundle; +import android.os.UserHandle; +import android.os.UserManager; +import android.view.View; +import android.widget.Toast; + +import com.android.launcher3.IconCache; +import com.android.launcher3.R; + +import java.util.ArrayList; +import java.util.List; + +@TargetApi(Build.VERSION_CODES.L) +class AppWidgetManagerCompatVL extends AppWidgetManagerCompat { + + private final UserManager mUserManager; + private final PackageManager mPm; + + AppWidgetManagerCompatVL(Context context) { + super(context); + mPm = context.getPackageManager(); + mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); + } + + @Override + public List getAllProviders() { + ArrayList providers = new ArrayList(); + for (UserHandle user : mUserManager.getUserProfiles()) { + providers.addAll(mAppWidgetManager.getInstalledProvidersForProfile(user)); + } + return providers; + } + + @Override + public String loadLabel(AppWidgetProviderInfo info) { + return info.loadLabel(mPm); + } + + @Override + public boolean bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info, + Bundle options) { + return mAppWidgetManager.bindAppWidgetIdIfAllowed( + appWidgetId, info.getProfile(), info.provider, options); + } + + @Override + public UserHandleCompat getUser(AppWidgetProviderInfo info) { + return UserHandleCompat.fromUser(info.getProfile()); + } + + @Override + public void startConfigActivity(AppWidgetProviderInfo info, int widgetId, Activity activity, + AppWidgetHost host, int requestCode) { + try { + host.startAppWidgetConfigureActivityForResult(activity, widgetId, 0, requestCode, null); + } catch (ActivityNotFoundException e) { + Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); + } catch (SecurityException e) { + Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); + } + } + + @Override + public Drawable loadPreview(AppWidgetProviderInfo info) { + return info.loadPreviewImage(mContext, 0); + } + + @Override + public Drawable loadIcon(AppWidgetProviderInfo info, IconCache cache) { + return info.loadIcon(mContext, cache.getFullResIconDpi()); + } + + @Override + public Bitmap getBadgeBitmap(AppWidgetProviderInfo info, Bitmap bitmap) { + if (info.getProfile().equals(android.os.Process.myUserHandle())) { + return bitmap; + } + + // Add a user badge in the bottom right of the image. + final Resources res = mContext.getResources(); + final int badgeSize = res.getDimensionPixelSize(R.dimen.profile_badge_size); + final int badgeMargin = res.getDimensionPixelSize(R.dimen.profile_badge_margin); + final Rect badgeLocation = new Rect(0, 0, badgeSize, badgeSize); + + final int top = bitmap.getHeight() - badgeSize - badgeMargin; + if (res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) { + badgeLocation.offset(badgeMargin, top); + } else { + badgeLocation.offset(bitmap.getWidth() - badgeSize - badgeMargin, top); + } + + UserManager userManager = (UserManager) mContext.getSystemService( + Context.USER_SERVICE); + + Drawable drawable = userManager.getBadgedDrawableForUser(new BitmapDrawable(res, bitmap), + info.getProfile(), badgeLocation, 0); + + if (drawable instanceof BitmapDrawable) { + return ((BitmapDrawable) drawable).getBitmap(); + } + + bitmap.eraseColor(Color.TRANSPARENT); + Canvas c = new Canvas(bitmap); + drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight()); + drawable.draw(c); + c.setBitmap(null); + return bitmap; + } +} -- cgit v1.2.3