<commit_before>package ca.nmsasaki.silenttouch;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import java.text.DateFormat;

public class MyWidgetProvider extends AppWidgetProvider {

	private static final long SILENT_DURATION_MILLISECONDS = 1 * 60 * 1000;
	private static final String TAG = "SilentTouch";
	private static final int MY_NOTIFICATION_ID = 1;

	private static PendingIntent mAlarmIntent = null;
	private static Toast mToast = null;

	private static final String INTENT_ACTION_WIDGET_CLICK = "ca.nmsasaki.silenttouch.INTENT_ACTION_WIDGET_CLICK";
	private static final String INTENT_ACTION_NOTIFICATION_CANCEL_CLICK = "ca.nmsasaki.silenttouch.INTENT_ACTION_NOTIFICATION_CANCEL_CLICK";
	private static final String INTENT_ACTION_TIMER_EXPIRED = "ca.nmsasaki.silenttouch.INTENT_ACTION_TIMER_EXPIRED";

	private static long mAlarmExpire = 0;

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

		Log.d(TAG, "MyWidgetProvider::onEnabled");
	}

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

		Log.i(TAG, "MyWidgetProvider::onReceive - enter");

		final String curIntentAction = intent.getAction();
		Log.i(TAG, "onReceive Intent=" + curIntentAction);

		if (curIntentAction == INTENT_ACTION_WIDGET_CLICK) {
			// User clicked on widget
			// Perform actions before notifying user
			// this will expose performance delays and show bugs
			
			Log.i(TAG, "INTENT_ACTION_WIDGET_CLICK - enter");
			
			// ----------------------------------------------------
			// check current ringermode
			AudioManager audioManager = (AudioManager) context
					.getSystemService(Context.AUDIO_SERVICE);
			final int curAudioMode = audioManager.getRingerMode();
			String curModeString = "UNKNOWN";

			switch (curAudioMode) {
			case AudioManager.RINGER_MODE_NORMAL:
				curModeString = "NORMAL";
				break;
			case AudioManager.RINGER_MODE_SILENT:
				curModeString = "SILENT";
				break;
			case AudioManager.RINGER_MODE_VIBRATE:
				curModeString = "VIBRATE";
				break;
			default:
				break;
			}

			Log.i(TAG, "AudioMode=" + curModeString);

			// --------------------------------------------------
			// set timer to re-enable original ringer mode
			AlarmManager alarmMgr = (AlarmManager) context
					.getSystemService(Context.ALARM_SERVICE);

			if (mAlarmIntent == null) {
				// Create a NEW timer for Canceling Silent Mode
				// -----------------------------------------
				Intent intentAlarmReceiver = new Intent(context,
						MyWidgetProvider.class);
				intentAlarmReceiver.setAction(INTENT_ACTION_TIMER_EXPIRED);

				mAlarmIntent = PendingIntent.getBroadcast(context, 0,
						intentAlarmReceiver, 0);

				mAlarmExpire = System.currentTimeMillis()
						+ SILENT_DURATION_MILLISECONDS;
				
				alarmMgr.set(AlarmManager.RTC_WAKEUP, mAlarmExpire,
						mAlarmIntent);
				
				Log.i(TAG, "AudioMode=" + curModeString);

			} else {
				// Update existing timer
				// ------------------------------------------

				mAlarmExpire = mAlarmExpire + SILENT_DURATION_MILLISECONDS; 
				alarmMgr.set(AlarmManager.RTC_WAKEUP, mAlarmExpire,<commit_msg>Truncated seconds from timer so timer always expires on the minute.<commit_after>package ca.nmsasaki.silenttouch;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.DateFormat;

public class MyWidgetProvider extends AppWidgetProvider {

	// when testing minimum is 2 minutes because I round seconds down to 00 to timer expires on minute change
	//private static final long SILENT_DURATION_MILLISECONDS = 30 * 60 * 1000;
	private static final long SILENT_DURATION_MILLISECONDS = 2 * 60 * 1000;
	private static final String TAG = "SilentTouch";
	private static final int MY_NOTIFICATION_ID = 1;

	private static PendingIntent mAlarmIntent = null;
	private static Toast mToast = null;

	private static final String INTENT_ACTION_WIDGET_CLICK = "ca.nmsasaki.silenttouch.INTENT_ACTION_WIDGET_CLICK";
	private static final String INTENT_ACTION_NOTIFICATION_CANCEL_CLICK = "ca.nmsasaki.silenttouch.INTENT_ACTION_NOTIFICATION_CANCEL_CLICK";
	private static final String INTENT_ACTION_TIMER_EXPIRED = "ca.nmsasaki.silenttouch.INTENT_ACTION_TIMER_EXPIRED";

	private static long mAlarmExpire = 0;

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

		Log.d(TAG, "MyWidgetProvider::onEnabled");
	}

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

		Log.i(TAG, "MyWidgetProvider::onReceive - enter");

		final String curIntentAction = intent.getAction();
		Log.i(TAG, "onReceive Intent=" + curIntentAction);

		if (curIntentAction == INTENT_ACTION_WIDGET_CLICK) {
			// User clicked on widget
			// Perform actions before notifying user
			// this will expose performance delays and show bugs
			
			Log.i(TAG, "INTENT_ACTION_WIDGET_CLICK - enter");
			
			// ----------------------------------------------------
			// check current ringermode
			AudioManager audioManager = (AudioManager) context
					.getSystemService(Context.AUDIO_SERVICE);
			final int curAudioMode = audioManager.getRingerMode();
			String curModeString = "UNKNOWN";

			switch (curAudioMode) {
			case AudioManager.RINGER_MODE_NORMAL:
				curModeString = "NORMAL";
				break;
			case AudioManager.RINGER_MODE_SILENT:
				curModeString = "SILENT";
				break;
			case AudioManager.RINGER_MODE_VIBRATE:
				curModeString = "VIBRATE";
				break;
			default:
				break;
			}

			Log.i(TAG, "AudioMode=" + curModeString);

			// --------------------------------------------------
			// set timer to re-enable original ringer mode
			AlarmManager alarmMgr = (AlarmManager) context
					.getSystemService(Context.ALARM_SERVICE);

			Calendar calendar = Calendar.getInstance();
			
			if (mAlarmIntent == null) {
				// Create a NEW timer for Canceling Silent Mode
				// -----------------------------------------
				Intent intentAlarmReceiver = new Intent(context,
						MyWidgetProvider.class);
				intentAlarmReceiver.setAction(INTENT_ACTION_TIMER_EXPIRED);

				mAlarmIntent = PendingIntent.getBroadcast(context, 0,
						intentAlarmReceiver, 0);

				mAlarmExpire = System.currentTimeMillis()
						+ SILENT_DURATION_MILLISECONDS;
				// truncate seconds
				calendar.setTimeInMillis(mAlarmExpire);
				calendar.set(Calendar.SECOND,0);
				mAlarmExpire = calendar.getTimeInMillis();
				
				alarmMgr.set(AlarmManager.RTC_WAKEUP, mAlarmExpire,
						mAlarmIntent);
				
				Log.i(TAG, "AudioMode=" + curModeString);

			} else {
				// Update existing timer
				// ------------------------------------------

				mAlarmExpire = mAlarmExpire + SILENT_DURATION_MILLISECONDS;
				// truncate seconds
				calendar.setTimeInMillis(mAlarmExpire);
				calendar.set(Calendar.SECOND,0);
				mAlarmExpire = calendar.getTimeInMillis();
				
				alarmMgr.set(AlarmManager.RTC_WAKEUP, mAlarmExpire,
{'fraction_non_alphanumeric': 0.12962962962962962, 'fraction_numerical': 0.0045010288065843625, 'mean_word_length': 3.4798387096774195, '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': '3742705', 'n_tokens_mistral': 2745, 'n_tokens_neox': 2343, 'n_words': 511}