From 61a5b0160d9f2e53ef4d4b451212a63032dad32d Mon Sep 17 00:00:00 2001 From: Michael Jurka Date: Fri, 13 Apr 2012 10:39:45 -0700 Subject: New API to allow third-party apps to bind widgets Change-Id: I1a3761c1a0f557a32d4d3bdd0207567fec918ba7 --- core/java/android/appwidget/AppWidgetManager.java | 115 +++++++++++++++++++++- 1 file changed, 113 insertions(+), 2 deletions(-) (limited to 'core/java/android/appwidget/AppWidgetManager.java') diff --git a/core/java/android/appwidget/AppWidgetManager.java b/core/java/android/appwidget/AppWidgetManager.java index 83ab81720e49..f2e909ee6217 100644 --- a/core/java/android/appwidget/AppWidgetManager.java +++ b/core/java/android/appwidget/AppWidgetManager.java @@ -79,6 +79,46 @@ public class AppWidgetManager { */ public static final String ACTION_APPWIDGET_PICK = "android.appwidget.action.APPWIDGET_PICK"; + /** + * Send this from your {@link AppWidgetHost} activity when you want to bind an AppWidget to + * display and bindAppWidgetIdIfAllowed returns false. + *

+ * You must supply the following extras: + * + * + * + * + * + * + * + * + * + *
{@link #EXTRA_APPWIDGET_ID}A newly allocated appWidgetId, which will be bound to the AppWidget provider + * you provide.
{@link #EXTRA_APPWIDGET_PROVIDER}The BroadcastReceiver that will be the AppWidget provider for this AppWidget. + *
+ * + *

+ * The system will respond with an onActivityResult call with the following extras in + * the intent: + * + * + * + * + * + *
{@link #EXTRA_APPWIDGET_ID}The appWidgetId that you supplied in the original intent.
+ *

+ * When you receive the result from the AppWidget bind activity, if the resultCode is + * {@link android.app.Activity#RESULT_OK}, the AppWidget has been bound. You should then + * check the AppWidgetProviderInfo for the returned AppWidget, and if it has one, launch its + * configuration activity. If {@link android.app.Activity#RESULT_CANCELED} is returned, you + * should delete + * the appWidgetId. + * + * @see #ACTION_APPWIDGET_CONFIGURE + * + */ + public static final String ACTION_APPWIDGET_BIND = "android.appwidget.action.APPWIDGET_BIND"; + /** * Sent when it is time to configure your AppWidget while it is being added to a host. * This action is not sent as a broadcast to the AppWidget provider, but as a startActivity @@ -143,6 +183,13 @@ public class AppWidgetManager { */ public static final String EXTRA_APPWIDGET_IDS = "appWidgetIds"; + /** + * An intent extra that contains the component name of a AppWidget provider. + *

+ * The value will be an ComponentName. + */ + public static final String EXTRA_APPWIDGET_PROVIDER = "appWidgetProvider"; + /** * An intent extra to pass to the AppWidget picker containing a {@link java.util.List} of * {@link AppWidgetProviderInfo} objects to mix in to the list of AppWidgets that are @@ -501,12 +548,14 @@ public class AppWidgetManager { /** * Set the component for a given appWidgetId. * - *

You need the APPWIDGET_LIST permission. This method is to be used by the - * AppWidget picker. + *

You need the BIND_APPWIDGET permission or the user must have enabled binding + * widgets always for your component. This method is used by the AppWidget picker and + * should not be used by other apps. * * @param appWidgetId The AppWidget instance for which to set the RemoteViews. * @param provider The {@link android.content.BroadcastReceiver} that will be the AppWidget * provider for this AppWidget. + * @hide */ public void bindAppWidgetId(int appWidgetId, ComponentName provider) { try { @@ -517,6 +566,68 @@ public class AppWidgetManager { } } + /** + * Set the component for a given appWidgetId. + * + *

You need the BIND_APPWIDGET permission or the user must have enabled binding + * widgets always for your component. Should be used by apps that host widgets; if this + * method returns false, call {@link #ACTION_APPWIDGET_BIND} to request permission to + * bind + * + * @param appWidgetId The AppWidget instance for which to set the RemoteViews. + * @param provider The {@link android.content.BroadcastReceiver} that will be the AppWidget + * provider for this AppWidget. + * @return true if this component has permission to bind the AppWidget + */ + public boolean bindAppWidgetIdIfAllowed(int appWidgetId, ComponentName provider) { + if (mContext == null) { + return false; + } + try { + return sService.bindAppWidgetIdIfAllowed( + mContext.getPackageName(), appWidgetId, provider); + } + catch (RemoteException e) { + throw new RuntimeException("system server dead?", e); + } + } + + /** + * Query if a given package was granted permission by the user to bind app widgets + * + *

You need the MODIFY_APPWIDGET_BIND_PERMISSIONS permission + * + * @param packageName The package for which the permission is being queried + * @return true if the package was granted permission by the user to bind app widgets + * @hide + */ + public boolean hasBindAppWidgetPermission(String packageName) { + try { + return sService.hasBindAppWidgetPermission(packageName); + } + catch (RemoteException e) { + throw new RuntimeException("system server dead?", e); + } + } + + /** + * Changes any user-granted permission for the given package to bind app widgets + * + *

You need the MODIFY_APPWIDGET_BIND_PERMISSIONS permission + * + * @param provider The package whose permission is being changed + * @param permission Whether to give the package permission to bind widgets + * @hide + */ + public void setBindAppWidgetPermission(String packageName, boolean permission) { + try { + sService.setBindAppWidgetPermission(packageName, permission); + } + catch (RemoteException e) { + throw new RuntimeException("system server dead?", e); + } + } + /** * Binds the RemoteViewsService for a given appWidgetId and intent. * -- cgit v1.2.3