AppWidgetHostpublic 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 |
Methods Summary |
---|
public int | allocateAppWidgetId()Get a appWidgetId for a host in the calling process.
try {
return sService.allocateAppWidgetId(mContextOpPackageName, mHostId);
}
catch (RemoteException e) {
throw new RuntimeException("system server dead?", e);
}
| private static void | bindService()
synchronized (sServiceLock) {
if (sService == null) {
IBinder b = ServiceManager.getService(Context.APPWIDGET_SERVICE);
sService = IAppWidgetService.Stub.asInterface(b);
}
}
| protected void | clearViews()Clear the list of Views that have been created by this AppWidgetHost.
mViews.clear();
| public final AppWidgetHostView | createView(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 void | deleteAllHosts()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 void | deleteAppWidgetId(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 void | deleteHost()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
try {
if (sService == null) {
bindService();
}
return sService.getAppWidgetIdsForHost(mContextOpPackageName, mHostId);
} catch (RemoteException e) {
throw new RuntimeException("system server dead?", e);
}
| private boolean | isLocalBinder()
return Process.myPid() == Binder.getCallingPid();
| protected AppWidgetHostView | onCreateView(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 void | onProviderChanged(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 void | onProvidersChanged()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 void | startAppWidgetConfigureActivityForResult(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.
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 void | startListening()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 void | stopListening()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();
| void | updateAppWidgetView(int appWidgetId, android.widget.RemoteViews views)
AppWidgetHostView v;
synchronized (mViews) {
v = mViews.get(appWidgetId);
}
if (v != null) {
v.updateAppWidget(views);
}
| void | viewDataChanged(int appWidgetId, int viewId)
AppWidgetHostView v;
synchronized (mViews) {
v = mViews.get(appWidgetId);
}
if (v != null) {
v.viewDataChanged(viewId);
}
|
|