1 package name.gumartinm.weather.information.widget;
4 import android.appwidget.AppWidgetManager;
5 import android.appwidget.AppWidgetProvider;
6 import android.appwidget.AppWidgetProviderInfo;
7 import android.content.ComponentName;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.os.Bundle;
12 public class WidgetProvider extends AppWidgetProvider {
15 public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
16 // For each widget that needs an update, get the text that we should display:
17 // - Create a RemoteViews object for it
18 // - Set the text in the RemoteViews object
19 // - Tell the AppWidgetManager to show that views object for the widget.
20 final int N = appWidgetIds.length;
21 for (int i=0; i<N; i++) {
22 int appWidgetId = appWidgetIds[i];
23 // To prevent any ANR timeouts, we perform the update in a service
24 final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
25 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
26 intent.putExtra("refreshAppWidget", true);
27 context.startService(intent);
32 public void onDeleted(final Context context, final int[] appWidgetIds) {
33 // When the user deletes the widget, delete the preference associated with it.
34 final int N = appWidgetIds.length;
35 for (int i=0; i<N; i++) {
36 WidgetConfigure.deletePreference(context, appWidgetIds[i]);
37 WidgetIntentService.deleteWidgetCurrentData(context, appWidgetIds[i]);
41 public static void updateAppWidget(final Context context, final int appWidgetId) {
42 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
44 updateAppWidget(context, appWidgetManager, appWidgetId);
47 public static void refreshAppWidget(final Context context, final int appWidgetId) {
48 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
50 refreshAppWidget(context.getApplicationContext(), appWidgetManager, appWidgetId);
53 public static void refreshAllAppWidgets(final Context context) {
54 final ComponentName widgets = new ComponentName(context.getApplicationContext(), WidgetProvider.class);
55 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
57 final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widgets);
58 for (final int appWidgetId : appWidgetIds) {
59 refreshAppWidget(context.getApplicationContext(), appWidgetManager, appWidgetId);
63 private static void refreshAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
66 Bundle myOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
68 // Get the value of OPTION_APPWIDGET_HOST_CATEGORY
69 int category = myOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
71 // If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
72 boolean isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
74 // Once you know the widget's category, you can optionally load a different base layout, set different
75 // properties, and so on. For example:
76 //int baseLayout = isKeyguard ? R.layout.keyguard_widget_layout : R.layout.widget_layout;
78 // Construct the RemoteViews object. It takes the package name (in our case, it's our
79 // package, but it needs this because on the other side it's the widget host inflating
80 // the layout from our package).
81 //final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
83 final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
84 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
85 intent.putExtra("refreshAppWidget", true);
86 context.startService(intent);
89 private static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
92 Bundle myOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
94 // Get the value of OPTION_APPWIDGET_HOST_CATEGORY
95 int category = myOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
97 // If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
98 boolean isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
100 // Once you know the widget's category, you can optionally load a different base layout, set different
101 // properties, and so on. For example:
102 //int baseLayout = isKeyguard ? R.layout.keyguard_widget_layout : R.layout.widget_layout;
104 // Construct the RemoteViews object. It takes the package name (in our case, it's our
105 // package, but it needs this because on the other side it's the widget host inflating
106 // the layout from our package).
107 //final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
109 final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
110 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
111 intent.putExtra("refreshAppWidget", false);
112 context.startService(intent);