package ${packageName};

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.widget.RemoteViews;
<#if applicationPackage??>import ${applicationPackage}.R;</#if>

/**
 * Implementation of App Widget functionality.
<#if configurable>
 * App Widget Configuration implemented in {@link ${className}ConfigureActivity ${className}ConfigureActivity}
</#if>
 */
public class ${className} extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // There may be multiple widgets active, so update all of them
        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
        }
    }

<#if configurable>
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        // When the user deletes the widget, delete the preference associated with it.
        final int N = appWidgetIds.length;
        for (int i=0; i<N; i++) {
            ${className}ConfigureActivity.deleteTitlePref(context, appWidgetIds[i]);
        }
    }
</#if>

    @Override
    public void onEnabled(Context context) {
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        // Enter relevant functionality for when the last widget is disabled
    }

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
            int appWidgetId) {

<#if configurable>
        CharSequence widgetText = ${className}ConfigureActivity.loadTitlePref(context, appWidgetId);
<#else>
        CharSequence widgetText = context.getString(R.string.appwidget_text);
</#if>
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.${class_name});
        views.setTextViewText(R.id.appwidget_text, widgetText);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

