diff options
author | Sunny Goyal <sunnygoyal@google.com> | 2016-08-10 15:03:22 -0700 |
---|---|---|
committer | Sunny Goyal <sunnygoyal@google.com> | 2016-08-16 12:37:29 -0700 |
commit | 86df138d9f5b32d947f3497314694a2a7adac70c (patch) | |
tree | b4440a3ef424b7f9c0b7d58d01e32b25b4cba155 /src/com/android/launcher3/DefaultLayoutParser.java | |
parent | eea721266f0a7c2c60097cba8363cfca12de9e46 (diff) | |
download | android_packages_apps_Trebuchet-86df138d9f5b32d947f3497314694a2a7adac70c.tar.gz android_packages_apps_Trebuchet-86df138d9f5b32d947f3497314694a2a7adac70c.tar.bz2 android_packages_apps_Trebuchet-86df138d9f5b32d947f3497314694a2a7adac70c.zip |
Adding support for pending widgets in AutoInstall layout
> Pending widgets whill show a loading progress while the app
is being installed.
> Extra bind options can be defined using the tub tags
<extra key="key-name" value="key-value" />
These are sent as widget options when the widget is bound.
> If the widget has any config activity, it is not shown
> Required attributes:
className, packageName, x, y, spanY, spanY & screen
Bug: 30279609
Change-Id: I1338618bfa5d86967339dffb68c12b1add6eb5d7
Diffstat (limited to 'src/com/android/launcher3/DefaultLayoutParser.java')
-rw-r--r-- | src/com/android/launcher3/DefaultLayoutParser.java | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/com/android/launcher3/DefaultLayoutParser.java b/src/com/android/launcher3/DefaultLayoutParser.java index e6f34d952..ef28d1e8c 100644 --- a/src/com/android/launcher3/DefaultLayoutParser.java +++ b/src/com/android/launcher3/DefaultLayoutParser.java @@ -1,6 +1,8 @@ package com.android.launcher3; import android.appwidget.AppWidgetHost; +import android.appwidget.AppWidgetManager; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; @@ -9,6 +11,7 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.content.res.XmlResourceParser; +import android.os.Bundle; import android.text.TextUtils; import android.util.Log; @@ -42,6 +45,10 @@ public class DefaultLayoutParser extends AutoInstallsLayout { private static final String ATTR_SCREEN = "screen"; private static final String ATTR_FOLDER_ITEMS = "folderItems"; + // TODO: Remove support for this broadcast, instead use widget options to send bind time options + private static final String ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE = + "com.android.launcher.action.APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE"; + public DefaultLayoutParser(Context context, AppWidgetHost appWidgetHost, LayoutParserCallback callback, Resources sourceRes, int layoutId) { super(context, appWidgetHost, callback, sourceRes, layoutId, TAG_FAVORITES); @@ -270,4 +277,61 @@ public class DefaultLayoutParser extends AutoInstallsLayout { return super.parseAndAdd(parser); } } + + + /** + * AppWidget parser which enforces that the app is already installed when the layout is parsed. + */ + protected class AppWidgetParser extends PendingWidgetParser { + + @Override + protected long verifyAndInsert(ComponentName cn, Bundle extras) { + try { + mPackageManager.getReceiverInfo(cn, 0); + } catch (Exception e) { + String[] packages = mPackageManager.currentToCanonicalPackageNames( + new String[] { cn.getPackageName() }); + cn = new ComponentName(packages[0], cn.getClassName()); + try { + mPackageManager.getReceiverInfo(cn, 0); + } catch (Exception e1) { + Log.d(TAG, "Can't find widget provider: " + cn.getClassName()); + return -1; + } + } + + final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext); + long insertedId = -1; + try { + int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); + + if (!appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, cn)) { + Log.e(TAG, "Unable to bind app widget id " + cn); + mAppWidgetHost.deleteAppWidgetId(appWidgetId); + return -1; + } + + mValues.put(Favorites.APPWIDGET_ID, appWidgetId); + mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString()); + mValues.put(Favorites._ID, mCallback.generateNewItemId()); + insertedId = mCallback.insertAndCheck(mDb, mValues); + if (insertedId < 0) { + mAppWidgetHost.deleteAppWidgetId(appWidgetId); + return insertedId; + } + + // Send a broadcast to configure the widget + if (!extras.isEmpty()) { + Intent intent = new Intent(ACTION_APPWIDGET_DEFAULT_WORKSPACE_CONFIGURE); + intent.setComponent(cn); + intent.putExtras(extras); + intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); + mContext.sendBroadcast(intent); + } + } catch (RuntimeException ex) { + Log.e(TAG, "Problem allocating appWidgetId", ex); + } + return insertedId; + } + } } |