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 import name.gumartinm.weather.information.widget.service.WidgetIntentService;
14 public class WidgetProvider extends AppWidgetProvider {
17 public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
18 // For each widget that needs an update, get the text that we should display:
19 // - Create a RemoteViews object for it
20 // - Set the text in the RemoteViews object
21 // - Tell the AppWidgetManager to show that views object for the widget.
22 final int N = appWidgetIds.length;
23 for (int i=0; i<N; i++) {
24 int appWidgetId = appWidgetIds[i];
25 // To prevent any ANR timeouts, we perform the update in a service
26 final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
27 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
28 intent.putExtra("refreshAppWidget", true);
29 context.startService(intent);
34 public void onDeleted(final Context context, final int[] appWidgetIds) {
35 // When the user deletes the widget, delete the preference associated with it.
36 final int N = appWidgetIds.length;
37 for (int i=0; i<N; i++) {
38 WidgetConfigure.deletePreference(context, appWidgetIds[i]);
39 WidgetIntentService.deleteWidgetCurrentData(context, appWidgetIds[i]);
43 public static void updateAppWidget(final Context context, final int appWidgetId) {
44 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
46 updateAppWidget(context, appWidgetManager, appWidgetId);
49 public static void refreshAppWidget(final Context context, final int appWidgetId) {
50 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
52 refreshAppWidget(context.getApplicationContext(), appWidgetManager, appWidgetId);
55 public static void refreshAllAppWidgets(final Context context) {
56 final ComponentName widgets = new ComponentName(context.getApplicationContext(), WidgetProvider.class);
57 final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context.getApplicationContext());
59 final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widgets);
60 for (final int appWidgetId : appWidgetIds) {
61 refreshAppWidget(context.getApplicationContext(), appWidgetManager, appWidgetId);
65 private static void refreshAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
68 Bundle myOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
70 // Get the value of OPTION_APPWIDGET_HOST_CATEGORY
71 int category = myOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
73 // If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
74 boolean isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
76 // Once you know the widget's category, you can optionally load a different base layout, set different
77 // properties, and so on. For example:
78 //int baseLayout = isKeyguard ? R.layout.keyguard_widget_layout : R.layout.widget_layout;
80 // Construct the RemoteViews object. It takes the package name (in our case, it's our
81 // package, but it needs this because on the other side it's the widget host inflating
82 // the layout from our package).
83 //final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
85 final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
86 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
87 intent.putExtra("refreshAppWidget", true);
88 context.startService(intent);
91 private static void updateAppWidget(final Context context, final AppWidgetManager appWidgetManager, final int appWidgetId) {
94 Bundle myOptions = appWidgetManager.getAppWidgetOptions(appWidgetId);
96 // Get the value of OPTION_APPWIDGET_HOST_CATEGORY
97 int category = myOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY, -1);
99 // If the value is WIDGET_CATEGORY_KEYGUARD, it's a lockscreen widget
100 boolean isKeyguard = category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD;
102 // Once you know the widget's category, you can optionally load a different base layout, set different
103 // properties, and so on. For example:
104 //int baseLayout = isKeyguard ? R.layout.keyguard_widget_layout : R.layout.widget_layout;
106 // Construct the RemoteViews object. It takes the package name (in our case, it's our
107 // package, but it needs this because on the other side it's the widget host inflating
108 // the layout from our package).
109 //final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
111 final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
112 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
113 intent.putExtra("refreshAppWidget", false);
114 context.startService(intent);