FileDocCategorySizeDatePackage
AppWidgetHost.javaAPI DocAndroid 5.1 API14002Thu Mar 12 22:22:10 GMT 2015android.appwidget

AppWidgetHost

public class AppWidgetHost extends Object
AppWidgetHost provides the interaction with the AppWidget service for apps, like the home screen, that want to embed AppWidgets in their UI.

Fields Summary
static final int
HANDLE_UPDATE
static final int
HANDLE_PROVIDER_CHANGED
static final int
HANDLE_PROVIDERS_CHANGED
static final int
HANDLE_VIEW_DATA_CHANGED
static final Object
sServiceLock
static com.android.internal.appwidget.IAppWidgetService
sService
private android.util.DisplayMetrics
mDisplayMetrics
private String
mContextOpPackageName
android.os.Handler
mHandler
int
mHostId
Callbacks
mCallbacks
final HashMap
mViews
private android.widget.RemoteViews.OnClickHandler
mOnClickHandler
Constructors Summary
public AppWidgetHost(android.content.Context context, int hostId)

        this(context, hostId, null, context.getMainLooper());
    
public AppWidgetHost(android.content.Context context, int hostId, android.widget.RemoteViews.OnClickHandler handler, android.os.Looper looper)

hide

        mContextOpPackageName = context.getOpPackageName();
        mHostId = hostId;
        mOnClickHandler = handler;
        mHandler = new UpdateHandler(looper);
        mDisplayMetrics = context.getResources().getDisplayMetrics();
        bindService();
    
Methods Summary
public intallocateAppWidgetId()
Get a appWidgetId for a host in the calling process.

return
a appWidgetId

        try {
            return sService.allocateAppWidgetId(mContextOpPackageName, mHostId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    
private static voidbindService()

        synchronized (sServiceLock) {
            if (sService == null) {
                IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE);
                sService = IAppWidgetService.Stub.asInterface(b);
            }
        }
    
protected voidclearViews()
Clear the list of Views that have been created by this AppWidgetHost.

        mViews.clear();
    
public final AppWidgetHostViewcreateView(android.content.Context context, int appWidgetId, AppWidgetProviderInfo appWidget)
Create the AppWidgetHostView for the given widget. The AppWidgetHost retains a pointer to the newly-created View.

        AppWidgetHostView view = onCreateView(context, appWidgetId, appWidget);
        view.setOnClickHandler(mOnClickHandler);
        view.setAppWidget(appWidgetId, appWidget);
        synchronized (mViews) {
            mViews.put(appWidgetId, view);
        }
        RemoteViews views;
        try {
            views = sService.getAppWidgetViews(mContextOpPackageName, appWidgetId);
        } catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
        view.updateAppWidget(views);

        return view;
    
public static voiddeleteAllHosts()
Remove all records about all hosts for your package.
  • Call this when initializing your database, as it might be because of a data wipe.
  • Call this to have the AppWidget manager release all resources associated with your host. Any future calls about this host will cause the records to be re-allocated.

        try {
            sService.deleteAllHosts();
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    
public voiddeleteAppWidgetId(int appWidgetId)
Stop listening to changes for this AppWidget.

        synchronized (mViews) {
            mViews.remove(appWidgetId);
            try {
                sService.deleteAppWidgetId(mContextOpPackageName, appWidgetId);
            }
            catch (RemoteException e) {
                throw new RuntimeException("system server dead?", e);
            }
        }
    
public voiddeleteHost()
Remove all records about this host from the AppWidget manager.
  • Call this when initializing your database, as it might be because of a data wipe.
  • Call this to have the AppWidget manager release all resources associated with your host. Any future calls about this host will cause the records to be re-allocated.

        try {
            sService.deleteHost(mContextOpPackageName, mHostId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    
public int[]getAppWidgetIds()
Gets a list of all the appWidgetIds that are bound to the current host

hide

        try {
            if (sService == null) {
                bindService();
            }
            return sService.getAppWidgetIdsForHost(mContextOpPackageName, mHostId);
        } catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    
private booleanisLocalBinder()

        return Process.myPid() == Binder.getCallingPid();
    
protected AppWidgetHostViewonCreateView(android.content.Context context, int appWidgetId, AppWidgetProviderInfo appWidget)
Called to create the AppWidgetHostView. Override to return a custom subclass if you need it. {@more}

        return new AppWidgetHostView(context, mOnClickHandler);
    
protected voidonProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget)
Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.

        AppWidgetHostView v;

        // Convert complex to dp -- we are getting the AppWidgetProviderInfo from the
        // AppWidgetService, which doesn't have our context, hence we need to do the
        // conversion here.
        appWidget.minWidth =
            TypedValue.complexToDimensionPixelSize(appWidget.minWidth, mDisplayMetrics);
        appWidget.minHeight =
            TypedValue.complexToDimensionPixelSize(appWidget.minHeight, mDisplayMetrics);
        appWidget.minResizeWidth =
            TypedValue.complexToDimensionPixelSize(appWidget.minResizeWidth, mDisplayMetrics);
        appWidget.minResizeHeight =
            TypedValue.complexToDimensionPixelSize(appWidget.minResizeHeight, mDisplayMetrics);

        synchronized (mViews) {
            v = mViews.get(appWidgetId);
        }
        if (v != null) {
            v.resetAppWidget(appWidget);
        }
    
protected voidonProvidersChanged()
Called when the set of available widgets changes (ie. widget containing packages are added, updated or removed, or widget components are enabled or disabled.)

        // Does nothing
    
public final voidstartAppWidgetConfigureActivityForResult(android.app.Activity activity, int appWidgetId, int intentFlags, int requestCode, android.os.Bundle options)
Starts an app widget provider configure activity for result on behalf of the caller. Use this method if the provider is in another profile as you are not allowed to start an activity in another profile. You can optionally provide a request code that is returned in {@link Activity#onActivityResult(int, int, android.content.Intent)} and an options bundle to be passed to the started activity.

Note that the provided app widget has to be bound for this method to work.

param
activity The activity from which to start the configure one.
param
appWidgetId The bound app widget whose provider's config activity to start.
param
requestCode Optional request code retuned with the result.
param
intentFlags Optional intent flags.
throws
android.content.ActivityNotFoundException If the activity is not found.
see
AppWidgetProviderInfo#getProfile()

        try {
            IntentSender intentSender = sService.createAppWidgetConfigIntentSender(
                    mContextOpPackageName, appWidgetId, intentFlags);
            if (intentSender != null) {
                activity.startIntentSenderForResult(intentSender, requestCode, null, 0, 0, 0,
                        options);
            } else {
                throw new ActivityNotFoundException();
            }
        } catch (IntentSender.SendIntentException e) {
            throw new ActivityNotFoundException();
        } catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }
    
public voidstartListening()
Start receiving onAppWidgetChanged calls for your AppWidgets. Call this when your activity becomes visible, i.e. from onStart() in your Activity.

        int[] updatedIds;
        ArrayList<RemoteViews> updatedViews = new ArrayList<RemoteViews>();
        try {
            updatedIds = sService.startListening(mCallbacks, mContextOpPackageName, mHostId,
                    updatedViews);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }

        final int N = updatedIds.length;
        for (int i = 0; i < N; i++) {
            updateAppWidgetView(updatedIds[i], updatedViews.get(i));
        }
    
public voidstopListening()
Stop receiving onAppWidgetChanged calls for your AppWidgets. Call this when your activity is no longer visible, i.e. from onStop() in your Activity.

        try {
            sService.stopListening(mContextOpPackageName, mHostId);
        }
        catch (RemoteException e) {
            throw new RuntimeException("system server dead?", e);
        }

        // This is here because keyguard needs it since it'll be switching users after this call.
        // If it turns out other apps need to call this often, we should re-think how this works.
        clearViews();
    
voidupdateAppWidgetView(int appWidgetId, android.widget.RemoteViews views)

        AppWidgetHostView v;
        synchronized (mViews) {
            v = mViews.get(appWidgetId);
        }
        if (v != null) {
            v.updateAppWidget(views);
        }
    
voidviewDataChanged(int appWidgetId, int viewId)

        AppWidgetHostView v;
        synchronized (mViews) {
            v = mViews.get(appWidgetId);
        }
        if (v != null) {
            v.viewDataChanged(viewId);
        }