<commit_before>package com.mareksebera.simpledilbert;

import org.joda.time.DateMidnight;
import org.joda.time.DateTimeZone;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class WidgetProvider extends AppWidgetProvider {

	public static final String TAG = "Dilbert Widget";
	static {
		/**
		 * Set default time-zone, because strips are published in New York
		 * timezone on midnight
		 * */
		DateTimeZone.setDefault(DilbertPreferences.TIME_ZONE);
	}
	private static final String INTENT_PREVIOUS = "com.mareksebera.simpledilbert.widget.PREVIOUS";
	private static final String INTENT_NEXT = "com.mareksebera.simpledilbert.widget.NEXT";
	private static final String INTENT_LATEST = "com.mareksebera.simpledilbert.widget.LATEST";
	private static final String INTENT_RANDOM = "com.mareksebera.simpledilbert.widget.RANDOM";
	private static final String INTENT_REFRESH = "com.mareksebera.simpledilbert.widget.REFRESH";
	private static final String INTENT_DISPLAY = "com.mareksebera.simpledilbert.widget.DISPLAY";

	private static Toast currentToast = null;

	private static PendingIntent getPendingIntent(String INTENT,
			Context context, int appWidgetId) {
		Intent intent = new Intent(INTENT);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		return PendingIntent.getBroadcast(context, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
	}

	static void updateAppWidget(final Context context,
			final AppWidgetManager appWidgetManager, final int appWidgetId) {
		final RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);
		views.setOnClickPendingIntent(R.id.widget_previous,
				getPendingIntent(INTENT_PREVIOUS, context, appWidgetId));
		views.setOnClickPendingIntent(R.id.widget_next,
				getPendingIntent(INTENT_NEXT, context, appWidgetId));
		views.setOnClickPendingIntent(R.id.widget_latest,
				getPendingIntent(INTENT_LATEST, context, appWidgetId));
		views.setOnClickPendingIntent(R.id.widget_random,
				getPendingIntent(INTENT_RANDOM, context, appWidgetId));
		views.setOnClickPendingIntent(R.id.widget_image,
				getPendingIntent(INTENT_DISPLAY, context, appWidgetId));
		views.setOnClickPendingIntent(R.id.widget_refresh,
				getPendingIntent(INTENT_REFRESH, context, appWidgetId));

		final DilbertPreferences prefs = new DilbertPreferences(context);
		final DateMidnight currentDate = prefs.getDateForWidgetId(appWidgetId);
		final String cachedUrl = prefs.getCachedUrl(currentDate);
		views.setViewVisibility(R.id.widget_progress, View.VISIBLE);
		appWidgetManager.updateAppWidget(appWidgetId, views);
		if (cachedUrl == null) {
			new GetStripUrl(new GetStripUrlInterface() {

				@Override
				public void imageLoadFailed(String url, FailReason reason) {
					Toast.makeText(context, "Image Loading failed",
							Toast.LENGTH_SHORT).show();
					views.setImageViewResource(R.id.widget_image,
							R.drawable.cancel);
					views.setViewVisibility(R.id.widget_progress, View.GONE);
					appWidgetManager.updateAppWidget(appWidgetId, views);
				}

				@Override
				public void displayImage(String url) {
					updateAppWidget(context, appWidgetManager, appWidgetId);
				}
			}, prefs, currentDate).execute();
		} else {
			ImageLoader.getInstance().loadImage(cachedUrl,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
							if (imageUri.equals(prefs.getCachedUrl(prefs
									.getDateForWidgetId(appWidgetId)))) {
								views.setViewVisibility(R.id.widget_progress,
										View.GONE);
								views.setImageViewBitmap(R.id.widget_image,
										loadedImage);
								views.setTextViewText(
										R.id.widget_title,
										prefs.getDateForWidgetId(appWidgetId)
												.toString(
														DilbertPreferences.DATE_FORMATTER));
								appWidgetManager.updateAppWidget(appWidgetId,
										views);
							}
						}
					});
		}

	}

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		AppController.configureImageLoader(context);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		final String action = intent.getAction();
		final int appWidgetId = intent
				.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID) ? intent
				.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID) : -1;
		final DilbertPreferences preferences = new DilbertPreferences(context);
		if (action == null || appWidgetId == -1) {
			super.onReceive(context, intent);
			return;
		}
		if (currentToast != null)
			currentToast.cancel();
		if (action.equals(INTENT_PREVIOUS)) {
			preferences.saveDateForWidgetId(appWidgetId, preferences
					.getDateForWidgetId(appWidgetId).minusDays(1));
		} else if (action.equals(INTENT_NEXT)) {
			preferences.saveDateForWidgetId(appWidgetId, preferences
					.getDateForWidgetId(appWidgetId).plusDays(1));
		} else if (action.equals(INTENT_LATEST)) {
			preferences.saveDateForWidgetId(appWidgetId, DateMidnight.now());
		} else if (action.equals(INTENT_RANDOM)) {
			preferences.saveDateForWidgetId(appWidgetId,
					DilbertPreferences.getRandomDateMidnight());
		} else if (action.equals(INTENT_REFRESH)) {
			preferences
					.removeCache(preferences.getDateForWidgetId(appWidgetId));
		} else if (action.equals(INTENT_DISPLAY)) {
			preferences.saveCurrentDate(preferences
					.getDateForWidgetId(appWidgetId));
			Intent display = new Intent(context, DilbertFragmentActivity.class);<commit_msg>Fixed time on click through widget<commit_after>package com.mareksebera.simpledilbert;

import org.joda.time.DateMidnight;
import org.joda.time.DateTimeZone;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class WidgetProvider extends AppWidgetProvider {

	public static final String TAG = "Dilbert Widget";
	static {
		/**
		 * Set default time-zone, because strips are published in New York
		 * timezone on midnight
		 * */
		DateTimeZone.setDefault(DilbertPreferences.TIME_ZONE);
	}
	private static final String INTENT_PREVIOUS = "com.mareksebera.simpledilbert.widget.PREVIOUS";
	private static final String INTENT_NEXT = "com.mareksebera.simpledilbert.widget.NEXT";
	private static final String INTENT_LATEST = "com.mareksebera.simpledilbert.widget.LATEST";
	private static final String INTENT_RANDOM = "com.mareksebera.simpledilbert.widget.RANDOM";
	private static final String INTENT_REFRESH = "com.mareksebera.simpledilbert.widget.REFRESH";
	private static final String INTENT_DISPLAY = "com.mareksebera.simpledilbert.widget.DISPLAY";

	private static Toast currentToast = null;

	private static PendingIntent getPendingIntent(String INTENT,
			Context context, int appWidgetId) {
		Intent intent = new Intent(INTENT);
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
		return PendingIntent.getBroadcast(context, 0, intent,
				PendingIntent.FLAG_UPDATE_CURRENT);
	}

	static void updateAppWidget(final Context context,
			final AppWidgetManager appWidgetManager, final int appWidgetId) {
		final RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.widget_layout);
		views.setOnClickPendingIntent(R.id.widget_previous,
				getPendingIntent(INTENT_PREVIOUS, context, appWidgetId));
		views.setOnClickPendingIntent(R.id.widget_next,
				getPendingIntent(INTENT_NEXT, context, appWidgetId));
		views.setOnClickPendingIntent(R.id.widget_latest,
				getPendingIntent(INTENT_LATEST, context, appWidgetId));
		views.setOnClickPendingIntent(R.id.widget_random,
				getPendingIntent(INTENT_RANDOM, context, appWidgetId));
		views.setOnClickPendingIntent(R.id.widget_image,
				getPendingIntent(INTENT_DISPLAY, context, appWidgetId));
		views.setOnClickPendingIntent(R.id.widget_refresh,
				getPendingIntent(INTENT_REFRESH, context, appWidgetId));

		final DilbertPreferences prefs = new DilbertPreferences(context);
		final DateMidnight currentDate = prefs.getDateForWidgetId(appWidgetId);
		final String cachedUrl = prefs.getCachedUrl(currentDate);
		views.setViewVisibility(R.id.widget_progress, View.VISIBLE);
		appWidgetManager.updateAppWidget(appWidgetId, views);
		if (cachedUrl == null) {
			new GetStripUrl(new GetStripUrlInterface() {

				@Override
				public void imageLoadFailed(String url, FailReason reason) {
					Toast.makeText(context, "Image Loading failed",
							Toast.LENGTH_SHORT).show();
					views.setImageViewResource(R.id.widget_image,
							R.drawable.cancel);
					views.setViewVisibility(R.id.widget_progress, View.GONE);
					appWidgetManager.updateAppWidget(appWidgetId, views);
				}

				@Override
				public void displayImage(String url) {
					updateAppWidget(context, appWidgetManager, appWidgetId);
				}
			}, prefs, currentDate).execute();
		} else {
			ImageLoader.getInstance().loadImage(cachedUrl,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
							if (imageUri.equals(prefs.getCachedUrl(prefs
									.getDateForWidgetId(appWidgetId)))) {
								views.setViewVisibility(R.id.widget_progress,
										View.GONE);
								views.setImageViewBitmap(R.id.widget_image,
										loadedImage);
								views.setTextViewText(
										R.id.widget_title,
										prefs.getDateForWidgetId(appWidgetId)
												.toString(
														DilbertPreferences.DATE_FORMATTER));
								appWidgetManager.updateAppWidget(appWidgetId,
										views);
							}
						}
					});
		}

	}

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		AppController.configureImageLoader(context);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		final String action = intent.getAction();
		final int appWidgetId = intent
				.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID) ? intent
				.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID) : -1;
		final DilbertPreferences preferences = new DilbertPreferences(context);
		if (action == null || appWidgetId == -1) {
			super.onReceive(context, intent);
			return;
		}
		if (currentToast != null)
			currentToast.cancel();
		if (action.equals(INTENT_PREVIOUS)) {
			preferences.saveDateForWidgetId(appWidgetId, preferences
					.getDateForWidgetId(appWidgetId).minusDays(1));
		} else if (action.equals(INTENT_NEXT)) {
			preferences.saveDateForWidgetId(appWidgetId, preferences
					.getDateForWidgetId(appWidgetId).plusDays(1));
		} else if (action.equals(INTENT_LATEST)) {
			preferences.saveDateForWidgetId(appWidgetId, DateMidnight.now());
		} else if (action.equals(INTENT_RANDOM)) {
			preferences.saveDateForWidgetId(appWidgetId,
					DilbertPreferences.getRandomDateMidnight());
		} else if (action.equals(INTENT_REFRESH)) {
			preferences
					.removeCache(preferences.getDateForWidgetId(appWidgetId));
		} else if (action.equals(INTENT_DISPLAY)) {
			preferences.saveCurrentDate(preferences
					.getDateForWidgetId(appWidgetId).minusDays(1));
			Intent display = new Intent(context, DilbertFragmentActivity.class);
{'fraction_non_alphanumeric': 0.09517021633406003, 'fraction_numerical': 0.0019285594499413047, 'mean_word_length': 4.942700548081714, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3430634', 'n_tokens_mistral': 4326, 'n_tokens_neox': 3666, 'n_words': 636}