• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.appwidget;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 
24 /**
25  * A convenience class to aid in implementing an AppWidget provider.
26  * Everything you can do with AppWidgetProvider, you can do with a regular {@link BroadcastReceiver}.
27  * AppWidgetProvider merely parses the relevant fields out of the Intent that is received in
28  * {@link #onReceive(Context,Intent) onReceive(Context,Intent)}, and calls hook methods
29  * with the received extras.
30  *
31  * <p>Extend this class and override one or more of the {@link #onUpdate}, {@link #onDeleted},
32  * {@link #onEnabled} or {@link #onDisabled} methods to implement your own AppWidget functionality.
33  * </p>
34  *
35  * <div class="special reference">
36  * <h3>Developer Guides</h3>
37  * <p>For more information about how to write an app widget provider, read the
38  * <a href="{@docRoot}guide/topics/appwidgets/index.html#AppWidgetProvider">App Widgets</a>
39  * developer guide.</p>
40  * </div>
41  */
42 public class AppWidgetProvider extends BroadcastReceiver {
43     /**
44      * Constructor to initialize AppWidgetProvider.
45      */
AppWidgetProvider()46     public AppWidgetProvider() {
47     }
48 
49     /**
50      * Implements {@link BroadcastReceiver#onReceive} to dispatch calls to the various
51      * other methods on AppWidgetProvider.
52      *
53      * @param context The Context in which the receiver is running.
54      * @param intent The Intent being received.
55      */
56     // BEGIN_INCLUDE(onReceive)
onReceive(Context context, Intent intent)57     public void onReceive(Context context, Intent intent) {
58         // Protect against rogue update broadcasts (not really a security issue,
59         // just filter bad broacasts out so subclasses are less likely to crash).
60         String action = intent.getAction();
61         if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
62             Bundle extras = intent.getExtras();
63             if (extras != null) {
64                 int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
65                 if (appWidgetIds != null && appWidgetIds.length > 0) {
66                     this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
67                 }
68             }
69         }
70         else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
71             Bundle extras = intent.getExtras();
72             if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
73                 final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
74                 this.onDeleted(context, new int[] { appWidgetId });
75             }
76         }
77         else if (AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED.equals(action)) {
78             Bundle extras = intent.getExtras();
79             if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)
80                     && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS)) {
81                 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
82                 Bundle widgetExtras = extras.getBundle(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS);
83                 this.onAppWidgetOptionsChanged(context, AppWidgetManager.getInstance(context),
84                         appWidgetId, widgetExtras);
85             }
86         }
87         else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
88             this.onEnabled(context);
89         }
90         else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
91             this.onDisabled(context);
92         }
93     }
94     // END_INCLUDE(onReceive)
95 
96     /**
97      * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_UPDATE} broadcast when
98      * this AppWidget provider is being asked to provide {@link android.widget.RemoteViews RemoteViews}
99      * for a set of AppWidgets.  Override this method to implement your own AppWidget functionality.
100      *
101      * {@more}
102      *
103      * @param context   The {@link android.content.Context Context} in which this receiver is
104      *                  running.
105      * @param appWidgetManager A {@link AppWidgetManager} object you can call {@link
106      *                  AppWidgetManager#updateAppWidget} on.
107      * @param appWidgetIds The appWidgetIds for which an update is needed.  Note that this
108      *                  may be all of the AppWidget instances for this provider, or just
109      *                  a subset of them.
110      *
111      * @see AppWidgetManager#ACTION_APPWIDGET_UPDATE
112      */
onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)113     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
114     }
115 
116     /**
117      * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_OPTIONS_CHANGED}
118      * broadcast when this widget has been layed out at a new size.
119      *
120      * {@more}
121      *
122      * @param context   The {@link android.content.Context Context} in which this receiver is
123      *                  running.
124      * @param appWidgetManager A {@link AppWidgetManager} object you can call {@link
125      *                  AppWidgetManager#updateAppWidget} on.
126      * @param appWidgetId The appWidgetId of the widget who's size changed.
127      * @param newOptions The appWidgetId of the widget who's size changed.
128      *
129      * @see AppWidgetManager#ACTION_APPWIDGET_OPTIONS_CHANGED
130      */
onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)131     public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
132             int appWidgetId, Bundle newOptions) {
133     }
134 
135     /**
136      * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_DELETED} broadcast when
137      * one or more AppWidget instances have been deleted.  Override this method to implement
138      * your own AppWidget functionality.
139      *
140      * {@more}
141      *
142      * @param context   The {@link android.content.Context Context} in which this receiver is
143      *                  running.
144      * @param appWidgetIds The appWidgetIds that have been deleted from their host.
145      *
146      * @see AppWidgetManager#ACTION_APPWIDGET_DELETED
147      */
onDeleted(Context context, int[] appWidgetIds)148     public void onDeleted(Context context, int[] appWidgetIds) {
149     }
150 
151     /**
152      * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_ENABLED} broadcast when
153      * the a AppWidget for this provider is instantiated.  Override this method to implement your
154      * own AppWidget functionality.
155      *
156      * {@more}
157      * When the last AppWidget for this provider is deleted,
158      * {@link AppWidgetManager#ACTION_APPWIDGET_DISABLED} is sent by the AppWidget manager, and
159      * {@link #onDisabled} is called.  If after that, an AppWidget for this provider is created
160      * again, onEnabled() will be called again.
161      *
162      * @param context   The {@link android.content.Context Context} in which this receiver is
163      *                  running.
164      *
165      * @see AppWidgetManager#ACTION_APPWIDGET_ENABLED
166      */
onEnabled(Context context)167     public void onEnabled(Context context) {
168     }
169 
170     /**
171      * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_DISABLED} broadcast, which
172      * is sent when the last AppWidget instance for this provider is deleted.  Override this method
173      * to implement your own AppWidget functionality.
174      *
175      * {@more}
176      *
177      * @param context   The {@link android.content.Context Context} in which this receiver is
178      *                  running.
179      *
180      * @see AppWidgetManager#ACTION_APPWIDGET_DISABLED
181      */
onDisabled(Context context)182     public void onDisabled(Context context) {
183     }
184 }
185