Debian Codegraph @

core/java/android/appwidget/AppWidgetProvider.java

raw symbols SCIP
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 */
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)
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_ENABLE_AND_UPDATE.equals(action)) {
62 this.onReceive(context, new Intent(intent)
63 .setAction(AppWidgetManager.ACTION_APPWIDGET_ENABLED));
64 this.onReceive(context, new Intent(intent)
65 .setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE));
66 } else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
67 Bundle extras = intent.getExtras();
68 if (extras != null) {
69 int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
70 if (appWidgetIds != null && appWidgetIds.length > 0) {
71 this.onUpdate(context, AppWidgetManager.getInstance(context), appWidgetIds);
72 }
73 }
74 } else if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
75 Bundle extras = intent.getExtras();
76 if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)) {
77 final int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
78 this.onDeleted(context, new int[] { appWidgetId });
79 }
80 } else if (AppWidgetManager.ACTION_APPWIDGET_OPTIONS_CHANGED.equals(action)) {
81 Bundle extras = intent.getExtras();
82 if (extras != null && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_ID)
83 && extras.containsKey(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS)) {
84 int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID);
85 Bundle widgetExtras = extras.getBundle(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS);
86 this.onAppWidgetOptionsChanged(context, AppWidgetManager.getInstance(context),
87 appWidgetId, widgetExtras);
88 }
89 } else if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
90 this.onEnabled(context);
91 } else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
92 this.onDisabled(context);
93 } else if (AppWidgetManager.ACTION_APPWIDGET_RESTORED.equals(action)) {
94 Bundle extras = intent.getExtras();
95 if (extras != null) {
96 int[] oldIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_OLD_IDS);
97 int[] newIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
98 if (oldIds != null && oldIds.length > 0) {
99 this.onRestored(context, oldIds, newIds);
100 this.onUpdate(context, AppWidgetManager.getInstance(context), newIds);
101 }
102 }
103 }
104 }
105 // END_INCLUDE(onReceive)
106
107 /**
108 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_UPDATE} and
109 * {@link AppWidgetManager#ACTION_APPWIDGET_RESTORED} broadcasts when this AppWidget
110 * provider is being asked to provide {@link android.widget.RemoteViews RemoteViews}
111 * for a set of AppWidgets. Override this method to implement your own AppWidget functionality.
112 *
113 * {@more}
114 *
115 * @param context The {@link android.content.Context Context} in which this receiver is
116 * running.
117 * @param appWidgetManager A {@link AppWidgetManager} object you can call {@link
118 * AppWidgetManager#updateAppWidget} on.
119 * @param appWidgetIds The appWidgetIds for which an update is needed. Note that this
120 * may be all of the AppWidget instances for this provider, or just
121 * a subset of them.
122 *
123 * @see AppWidgetManager#ACTION_APPWIDGET_UPDATE
124 */
125 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
126 }
127
128 /**
129 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_OPTIONS_CHANGED}
130 * broadcast when this widget has been layed out at a new size.
131 *
132 * {@more}
133 *
134 * @param context The {@link android.content.Context Context} in which this receiver is
135 * running.
136 * @param appWidgetManager A {@link AppWidgetManager} object you can call {@link
137 * AppWidgetManager#updateAppWidget} on.
138 * @param appWidgetId The appWidgetId of the widget whose size changed.
139 * @param newOptions The appWidgetId of the widget whose size changed.
140 *
141 * @see AppWidgetManager#ACTION_APPWIDGET_OPTIONS_CHANGED
142 */
143 public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
144 int appWidgetId, Bundle newOptions) {
145 }
146
147 /**
148 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_DELETED} broadcast when
149 * one or more AppWidget instances have been deleted. Override this method to implement
150 * your own AppWidget functionality.
151 *
152 * {@more}
153 *
154 * @param context The {@link android.content.Context Context} in which this receiver is
155 * running.
156 * @param appWidgetIds The appWidgetIds that have been deleted from their host.
157 *
158 * @see AppWidgetManager#ACTION_APPWIDGET_DELETED
159 */
160 public void onDeleted(Context context, int[] appWidgetIds) {
161 }
162
163 /**
164 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_ENABLED} broadcast when
165 * the a AppWidget for this provider is instantiated. Override this method to implement your
166 * own AppWidget functionality.
167 *
168 * {@more}
169 * When the last AppWidget for this provider is deleted,
170 * {@link AppWidgetManager#ACTION_APPWIDGET_DISABLED} is sent by the AppWidget manager, and
171 * {@link #onDisabled} is called. If after that, an AppWidget for this provider is created
172 * again, onEnabled() will be called again.
173 *
174 * @param context The {@link android.content.Context Context} in which this receiver is
175 * running.
176 *
177 * @see AppWidgetManager#ACTION_APPWIDGET_ENABLED
178 */
179 public void onEnabled(Context context) {
180 }
181
182 /**
183 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_DISABLED} broadcast, which
184 * is sent when the last AppWidget instance for this provider is deleted. Override this method
185 * to implement your own AppWidget functionality.
186 *
187 * {@more}
188 *
189 * @param context The {@link android.content.Context Context} in which this receiver is
190 * running.
191 *
192 * @see AppWidgetManager#ACTION_APPWIDGET_DISABLED
193 */
194 public void onDisabled(Context context) {
195 }
196
197 /**
198 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_RESTORED} broadcast
199 * when instances of this AppWidget provider have been restored from backup. If your
200 * provider maintains any persistent data about its widget instances, override this method
201 * to remap the old AppWidgetIds to the new values and update any other app state that may
202 * be relevant.
203 *
204 * <p>This callback will be followed immediately by a call to {@link #onUpdate} so your
205 * provider can immediately generate new RemoteViews suitable for its newly-restored set
206 * of instances.
207 *
208 * <p>In addition, you should set {@link AppWidgetManager#OPTION_APPWIDGET_RESTORE_COMPLETED}
209 * to true indicate if a widget has been restored successfully from the provider's side.
210 *
211 * {@more}
212 *
213 * @param context
214 * @param oldWidgetIds
215 * @param newWidgetIds
216 */
217 public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {
218 }
219 }