Debian Codegraph @

templates/other/AppWidget/root/src/app_package/AppWidget.java.ftl

raw
1 package ${packageName};
2
3 import android.appwidget.AppWidgetManager;
4 import android.appwidget.AppWidgetProvider;
5 import android.content.Context;
6 import android.widget.RemoteViews;
7 <#if applicationPackage??>
8 import ${applicationPackage}.R;
9 </#if>
10
11 /**
12 * Implementation of App Widget functionality.
13 <#if configurable>
14 * App Widget Configuration implemented in {@link ${className}ConfigureActivity ${className}ConfigureActivity}
15 </#if>
16 */
17 public class ${className} extends AppWidgetProvider {
18
19 @Override
20 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
21 // There may be multiple widgets active, so update all of them
22 for (int appWidgetId : appWidgetIds) {
23 updateAppWidget(context, appWidgetManager, appWidgetId);
24 }
25 }
26
27 <#if configurable>
28 @Override
29 public void onDeleted(Context context, int[] appWidgetIds) {
30 // When the user deletes the widget, delete the preference associated with it.
31 for (int appWidgetId : appWidgetIds) {
32 ${className}ConfigureActivity.deleteTitlePref(context, appWidgetId);
33 }
34 }
35 </#if>
36
37 @Override
38 public void onEnabled(Context context) {
39 // Enter relevant functionality for when the first widget is created
40 }
41
42 @Override
43 public void onDisabled(Context context) {
44 // Enter relevant functionality for when the last widget is disabled
45 }
46
47 static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
48 int appWidgetId) {
49
50 <#if configurable>
51 CharSequence widgetText = ${className}ConfigureActivity.loadTitlePref(context, appWidgetId);
52 <#else>
53 CharSequence widgetText = context.getString(R.string.appwidget_text);
54 </#if>
55 // Construct the RemoteViews object
56 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.${class_name});
57 views.setTextViewText(R.id.appwidget_text, widgetText);
58
59 // Instruct the widget manager to update the widget
60 appWidgetManager.updateAppWidget(appWidgetId, views);
61 }
62 }
63