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_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 */ onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)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 or its options changed via 131 * {@link AppWidgetManager#updateAppWidgetOptions}. 132 * 133 * {@more} 134 * 135 * @param context The {@link android.content.Context Context} in which this receiver is 136 * running. 137 * @param appWidgetManager A {@link AppWidgetManager} object you can call {@link 138 * AppWidgetManager#updateAppWidget} on. 139 * @param appWidgetId The appWidgetId of the widget whose size changed. 140 * @param newOptions The new options of the changed widget. 141 * 142 * @see AppWidgetManager#ACTION_APPWIDGET_OPTIONS_CHANGED 143 */ onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)144 public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, 145 int appWidgetId, Bundle newOptions) { 146 } 147 148 /** 149 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_DELETED} broadcast when 150 * one or more AppWidget instances have been deleted. Override this method to implement 151 * your own AppWidget functionality. 152 * 153 * {@more} 154 * 155 * @param context The {@link android.content.Context Context} in which this receiver is 156 * running. 157 * @param appWidgetIds The appWidgetIds that have been deleted from their host. 158 * 159 * @see AppWidgetManager#ACTION_APPWIDGET_DELETED 160 */ onDeleted(Context context, int[] appWidgetIds)161 public void onDeleted(Context context, int[] appWidgetIds) { 162 } 163 164 /** 165 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_ENABLED} broadcast when 166 * the a AppWidget for this provider is instantiated. Override this method to implement your 167 * own AppWidget functionality. 168 * 169 * {@more} 170 * When the last AppWidget for this provider is deleted, 171 * {@link AppWidgetManager#ACTION_APPWIDGET_DISABLED} is sent by the AppWidget manager, and 172 * {@link #onDisabled} is called. If after that, an AppWidget for this provider is created 173 * again, onEnabled() will be called again. 174 * 175 * @param context The {@link android.content.Context Context} in which this receiver is 176 * running. 177 * 178 * @see AppWidgetManager#ACTION_APPWIDGET_ENABLED 179 */ onEnabled(Context context)180 public void onEnabled(Context context) { 181 } 182 183 /** 184 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_DISABLED} broadcast, which 185 * is sent when the last AppWidget instance for this provider is deleted. Override this method 186 * to implement your own AppWidget functionality. 187 * 188 * {@more} 189 * 190 * @param context The {@link android.content.Context Context} in which this receiver is 191 * running. 192 * 193 * @see AppWidgetManager#ACTION_APPWIDGET_DISABLED 194 */ onDisabled(Context context)195 public void onDisabled(Context context) { 196 } 197 198 /** 199 * Called in response to the {@link AppWidgetManager#ACTION_APPWIDGET_RESTORED} broadcast 200 * when instances of this AppWidget provider have been restored from backup. If your 201 * provider maintains any persistent data about its widget instances, override this method 202 * to remap the old AppWidgetIds to the new values and update any other app state that may 203 * be relevant. 204 * 205 * <p>This callback will be followed immediately by a call to {@link #onUpdate} so your 206 * provider can immediately generate new RemoteViews suitable for its newly-restored set 207 * of instances. 208 * 209 * <p>In addition, you should set {@link AppWidgetManager#OPTION_APPWIDGET_RESTORE_COMPLETED} 210 * to true indicate if a widget has been restored successfully from the provider's side. 211 * 212 * {@more} 213 * 214 * @param context 215 * @param oldWidgetIds 216 * @param newWidgetIds 217 */ onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds)218 public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) { 219 } 220 } 221