<commit_before>package co.sfng.calendarwidget;

import java.util.Calendar;

import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.RemoteViews;


public class CalendarWidgetProvider extends AppWidgetProvider {

	private static final String LOG_TAG = CalendarWidgetProvider.class.toString();
	private static final String APP_WIDGET_ID = "co.sfng.calendarwidget.APP_WIDGET_ID";

	private static final String ACTION_PREVIOUS_MONTH =
			"co.sfng.calendarwidget.ACTION_PREVIOUS_MONTH";
	private static final String ACTION_NEXT_MONTH =
			"co.sfng.calendarwidget.ACTION_NEXT_MONTH";
	private static final String ACTION_TODAY =
			"co.sfng.calendarwidget.ACTION_TODAY";

	private static final String PREFERENCE_FILE =
			"co.sfng.calendarwidget.PREFERENCE_WIDGET_";
	private static final String SELECTED_TIME = "selected_time";
	private static final String IS_WIDE = "is_wide";

	private static final int WEEKS = 6;

	@Override
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
			int appWidgetId, Bundle newOptions) {
		Log.i(LOG_TAG, "onAppWidgetOptionsChanged()");
		super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,	newOptions);
		Bundle bundle = appWidgetManager.getAppWidgetOptions(appWidgetId);

		if (bundle != null) {
			Resources res = context.getResources();
			int minWidth = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
			boolean isWide = minWidth > res.getInteger(R.integer.minimum_wide_width);

			SharedPreferences pref = context.getSharedPreferences(
					getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
			pref.edit().putBoolean(IS_WIDE, isWide).apply();
		}
		render(context, appWidgetId);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		Log.i(LOG_TAG, "onUpdate()");
		for (int appWidgetId: appWidgetIds) {
			render(context, appWidgetId);
		}
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);
		Log.i(LOG_TAG, "onDeleted()");
		for (int appWidgetId: appWidgetIds) {
			Log.i(LOG_TAG, "Clear all preferences for widget ID " + appWidgetId);
			SharedPreferences pref = context.getSharedPreferences(
					getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
			pref.edit().clear().apply();
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		String action = intent.getAction();
		int appWidgetId = intent.getIntExtra(APP_WIDGET_ID, -1);
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

		if (ACTION_PREVIOUS_MONTH.equals(action)) {
			previousMonth(context, appWidgetManager, appWidgetId);
		} else if (ACTION_NEXT_MONTH.equals(action)) {
			nextMonth(context, appWidgetManager, appWidgetId);
		} else if (ACTION_TODAY.equals(action)) {
			today(context, appWidgetManager, appWidgetId);
		}
	}

	private void previousMonth(Context context, AppWidgetManager appWidgetManager,
			int appWidgetId) {
		Calendar cal = Calendar.getInstance();
		SharedPreferences pref = context.getSharedPreferences(
				getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
		cal.setTimeInMillis(pref.getLong(SELECTED_TIME, cal.getTimeInMillis()));
		cal.add(Calendar.MONTH, -1);
		pref.edit().putLong(SELECTED_TIME, cal.getTimeInMillis()).apply();
		render(context, appWidgetId);
	}

	private void nextMonth(Context context, AppWidgetManager appWidgetManager,
			int appWidgetId) {
		Calendar cal = Calendar.getInstance();
		SharedPreferences pref = context.getSharedPreferences(
				getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
		cal.setTimeInMillis(pref.getLong(SELECTED_TIME, cal.getTimeInMillis()));
		cal.add(Calendar.MONTH, 1);
		pref.edit().putLong(SELECTED_TIME, cal.getTimeInMillis()).apply();
		render(context, appWidgetId);
	}

	private void today(Context context, AppWidgetManager appWidgetManager,
			int appWidgetId) {
		SharedPreferences pref = context.getSharedPreferences(
				getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
		pref.edit().clear().apply();
		render(context, appWidgetId);
	}

	private String getPreferenceFileName(int appWidgetId) {
		return PREFERENCE_FILE + appWidgetId;
	}

	private void render(Context context, int appWidgetId) {
		RemoteViews widgetView = new RemoteViews(context.getPackageName(), R.layout.widget);

		// Obtain last selected time from SharedPreferences.
		Calendar cal = Calendar.getInstance();
		int today = cal.get(Calendar.DAY_OF_YEAR);
		int todayYear = cal.get(Calendar.YEAR);

		SharedPreferences pref = context.getSharedPreferences(
				getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
		long selectedTime = pref.getLong(SELECTED_TIME, cal.getTimeInMillis());
		cal.setTimeInMillis(selectedTime);

		if (pref.getBoolean(IS_WIDE, false)) {
			widgetView.setTextViewText(R.id.month_year_label, DateFormat.format("MMMM yyyy", cal));
		} else {
			widgetView.setTextViewText(R.id.month_year_label, DateFormat.format("MMM yyyy", cal));
		}

		widgetView.removeAllViews(R.id.calendar);

		// Remember what the selected month is.
		int selectedMonth = cal.get(Calendar.MONTH);

		// Set date to the first Sunday or Sunday of the first week of previous month.
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.DATE, 1 - cal.get(Calendar.DAY_OF_WEEK));

		for (int i = 0; i < WEEKS; i++) {
			RemoteViews rowView = new RemoteViews(context.getPackageName(), R.layout.row_week);

			for (int y = 0; y < 7; y++) {
				boolean isToday = cal.get(Calendar.DAY_OF_YEAR) == today &&
							cal.get(Calendar.YEAR) == todayYear;
				boolean inSelectedMonth = cal.get(Calendar.MONTH) == selectedMonth;

				int layoutId = R.layout.cell_day;
				if (isToday) {
					layoutId = R.layout.cell_today;
				} else if (inSelectedMonth) {
					layoutId = R.layout.cell_current_month_day;
				}

				RemoteViews dateView = new RemoteViews(context.getPackageName(), layoutId);
				dateView.setTextViewText(
						android.R.id.text1, Integer.toString(cal.get(Calendar.DATE)));
				rowView.addView(R.id.row_week, dateView);
				cal.add(Calendar.DATE, 1);
			}
			widgetView.addView(R.id.calendar, rowView);
		}

		widgetView.setOnClickPendingIntent(R.id.previous_month_button,
				createPendingIntent(context, appWidgetId, ACTION_PREVIOUS_MONTH));
		widgetView.setOnClickPendingIntent(R.id.next_month_button,
				createPendingIntent(context, appWidgetId, ACTION_NEXT_MONTH));
		widgetView.setOnClickPendingIntent(R.id.month_year_label,
				createPendingIntent(context, appWidgetId, ACTION_TODAY));

		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		appWidgetManager.updateAppWidget(appWidgetId, widgetView);
	}

    private PendingIntent createPendingIntent(Context context, int appWidgetId, String action) {
    	Intent intent = new Intent(context, CalendarWidgetProvider.class);
    	intent.setAction(action);
    	intent.putExtra(APP_WIDGET_ID, appWidgetId);
    	return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
}<commit_msg>Use app widget ID as request code for PendingIntent to differentiate widgets.<commit_after>package co.sfng.calendarwidget;

import java.util.Calendar;

import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.widget.RemoteViews;


public class CalendarWidgetProvider extends AppWidgetProvider {

	private static final String LOG_TAG = CalendarWidgetProvider.class.toString();

	private static final String ACTION_PREVIOUS_MONTH =
			"co.sfng.calendarwidget.ACTION_PREVIOUS_MONTH";
	private static final String ACTION_NEXT_MONTH =
			"co.sfng.calendarwidget.ACTION_NEXT_MONTH";
	private static final String ACTION_TODAY =
			"co.sfng.calendarwidget.ACTION_TODAY";

	private static final String PREFERENCE_FILE =
			"co.sfng.calendarwidget.PREFERENCE_WIDGET_";
	private static final String SELECTED_TIME = "selected_time";
	private static final String IS_WIDE = "is_wide";

	private static final int WEEKS = 6;

	@Override
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager,
			int appWidgetId, Bundle newOptions) {
		super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId,	newOptions);

		// Obtain widget width and set flag in SharePreferences.
		Bundle bundle = appWidgetManager.getAppWidgetOptions(appWidgetId);

		if (bundle != null) {
			int minWidth = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
			Resources res = context.getResources();
			boolean isWide = minWidth > res.getInteger(R.integer.minimum_wide_width);

			SharedPreferences pref = context.getSharedPreferences(
					getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
			pref.edit().putBoolean(IS_WIDE, isWide).apply();
		}

		render(context, appWidgetId);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);

		for (int appWidgetId: appWidgetIds) {
			render(context, appWidgetId);
		}
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		super.onDeleted(context, appWidgetIds);

		for (int appWidgetId: appWidgetIds) {
			SharedPreferences pref = context.getSharedPreferences(
					getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
			pref.edit().clear().apply();
		}
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);

		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		String action = intent.getAction();
		int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);

		if (ACTION_PREVIOUS_MONTH.equals(action)) {
			previousMonth(context, appWidgetManager, appWidgetId);
		} else if (ACTION_NEXT_MONTH.equals(action)) {
			nextMonth(context, appWidgetManager, appWidgetId);
		} else if (ACTION_TODAY.equals(action)) {
			today(context, appWidgetManager, appWidgetId);
		}
	}

	private void previousMonth(Context context, AppWidgetManager appWidgetManager,
			int appWidgetId) {
		Calendar cal = Calendar.getInstance();
		SharedPreferences pref = context.getSharedPreferences(
				getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
		cal.setTimeInMillis(pref.getLong(SELECTED_TIME, cal.getTimeInMillis()));
		cal.add(Calendar.MONTH, -1);
		pref.edit().putLong(SELECTED_TIME, cal.getTimeInMillis()).apply();
		render(context, appWidgetId);
	}

	private void nextMonth(Context context, AppWidgetManager appWidgetManager,
			int appWidgetId) {
		Calendar cal = Calendar.getInstance();
		SharedPreferences pref = context.getSharedPreferences(
				getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
		cal.setTimeInMillis(pref.getLong(SELECTED_TIME, cal.getTimeInMillis()));
		cal.add(Calendar.MONTH, 1);
		pref.edit().putLong(SELECTED_TIME, cal.getTimeInMillis()).apply();
		render(context, appWidgetId);
	}

	private void today(Context context, AppWidgetManager appWidgetManager,
			int appWidgetId) {
		SharedPreferences pref = context.getSharedPreferences(
				getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
		pref.edit().remove(SELECTED_TIME).apply();
		render(context, appWidgetId);
	}

	private String getPreferenceFileName(int appWidgetId) {
		return PREFERENCE_FILE + appWidgetId;
	}

	private void render(Context context, int appWidgetId) {
		Calendar cal = Calendar.getInstance();
		int today = cal.get(Calendar.DAY_OF_YEAR);
		int todayYear = cal.get(Calendar.YEAR);

		// Obtain last selected time from SharedPreferences.
		SharedPreferences pref = context.getSharedPreferences(
				getPreferenceFileName(appWidgetId), Context.MODE_PRIVATE);
		long selectedTime = pref.getLong(SELECTED_TIME, cal.getTimeInMillis());
		cal.setTimeInMillis(selectedTime);

		RemoteViews widgetView = new RemoteViews(context.getPackageName(), R.layout.widget);
		widgetView.removeAllViews(R.id.calendar);

		if (pref.getBoolean(IS_WIDE, false)) {
			widgetView.setTextViewText(R.id.month_year_label, DateFormat.format("MMMM yyyy", cal));
		} else {
			widgetView.setTextViewText(R.id.month_year_label, DateFormat.format("MMM yyyy", cal));
		}

		// Keep track of what the selected month is.
		int selectedMonth = cal.get(Calendar.MONTH);

		// Set date to the first Sunday or Sunday of the first week of previous month.
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.DATE, 1 - cal.get(Calendar.DAY_OF_WEEK));

		for (int i = 0; i < WEEKS; i++) {
			RemoteViews rowView = new RemoteViews(context.getPackageName(), R.layout.row_week);

			for (int y = 0; y < 7; y++) {
				boolean isToday = cal.get(Calendar.DAY_OF_YEAR) == today &&
							cal.get(Calendar.YEAR) == todayYear;
				boolean inSelectedMonth = cal.get(Calendar.MONTH) == selectedMonth;

				int layoutId = R.layout.cell_day;
				if (isToday) {
					layoutId = R.layout.cell_today;
				} else if (inSelectedMonth) {
					layoutId = R.layout.cell_current_month_day;
				}

				RemoteViews dateView = new RemoteViews(context.getPackageName(), layoutId);
				dateView.setTextViewText(
						android.R.id.text1, Integer.toString(cal.get(Calendar.DATE)));
				rowView.addView(R.id.row_week, dateView);
				cal.add(Calendar.DATE, 1);
			}
			widgetView.addView(R.id.calendar, rowView);
		}

		// Attach action to buttons.
		widgetView.setOnClickPendingIntent(R.id.previous_month_button,
				createPendingIntent(context, appWidgetId, ACTION_PREVIOUS_MONTH));
		widgetView.setOnClickPendingIntent(R.id.next_month_button,
				createPendingIntent(context, appWidgetId, ACTION_NEXT_MONTH));
		widgetView.setOnClickPendingIntent(R.id.month_year_label,
				createPendingIntent(context, appWidgetId, ACTION_TODAY));

		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		appWidgetManager.updateAppWidget(appWidgetId, widgetView);
	}

    private PendingIntent createPendingIntent(Context context, int appWidgetId, String action) {
    	Intent intent = new Intent(context, CalendarWidgetProvider.class);
    	intent.setAction(action);
    	intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    	return PendingIntent.getBroadcast(context, appWidgetId, intent,
    			PendingIntent.FLAG_UPDATE_CURRENT);
    }
}
{'fraction_non_alphanumeric': 0.10367915147497514, 'fraction_numerical': 0.0015246934040437522, 'mean_word_length': 5.485812553740327, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '2491741', 'n_tokens_mistral': 5071, 'n_tokens_neox': 4627, 'n_words': 921}