<commit_before>package com.ianhanniballake.contractiontimer.notification;

import android.app.IntentService;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.preview.support.v4.app.NotificationManagerCompat;
import android.preview.support.wearable.notifications.RemoteInput;
import android.preview.support.wearable.notifications.WearableNotifications;
import android.provider.BaseColumns;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.text.format.DateUtils;

import com.ianhanniballake.contractiontimer.R;
import com.ianhanniballake.contractiontimer.appwidget.AppWidgetToggleService;
import com.ianhanniballake.contractiontimer.provider.ContractionContract;
import com.ianhanniballake.contractiontimer.ui.MainActivity;
import com.ianhanniballake.contractiontimer.ui.Preferences;

/**
 * IntentService which updates the ongoing notification
 */
public class NotificationUpdateService extends IntentService {
    private static final int NOTIFICATION_ID = 0;

    public NotificationUpdateService() {
        super(NotificationUpdateService.class.getSimpleName());
    }

    public static void updateNotification(Context context) {
        context.startService(new Intent(context, NotificationUpdateService.class));
    }

    @Override
    protected void onHandleIntent(final Intent intent) {
        NoteTransparentActivity.checkServiceState(this);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final boolean notificationsEnabled = preferences.getBoolean(Preferences.NOTIFICATION_ENABLE_PREFERENCE_KEY,
                getResources().getBoolean(R.bool.pref_notification_enable_default));
        if (!notificationsEnabled) {
            notificationManager.cancel(NOTIFICATION_ID);
            return;
        }
        final String[] projection = {BaseColumns._ID,
                ContractionContract.Contractions.COLUMN_NAME_START_TIME,
                ContractionContract.Contractions.COLUMN_NAME_END_TIME,
                ContractionContract.Contractions.COLUMN_NAME_NOTE};
        final String selection = ContractionContract.Contractions.COLUMN_NAME_START_TIME + ">?";
        final long averagesTimeFrame = Long.parseLong(preferences.getString(
                Preferences.AVERAGE_TIME_FRAME_PREFERENCE_KEY,
                getString(R.string.pref_settings_average_time_frame_default)));
        final long timeCutoff = System.currentTimeMillis() - averagesTimeFrame;
        final String[] selectionArgs = {Long.toString(timeCutoff)};
        final Cursor data = getContentResolver().query(ContractionContract.Contractions.CONTENT_URI, projection,
                selection, selectionArgs, null);
        if (data == null || !data.moveToFirst()) {
            notificationManager.cancel(NOTIFICATION_ID);
            if (data != null) {
                data.close();
            }
            return;
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.ic_notification);
        Intent contentIntent = new Intent(this, MainActivity.class);
        contentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_CLEAR_TASK |
                Intent.FLAG_ACTIVITY_TASK_ON_HOME);
        contentIntent.putExtra(MainActivity.LAUNCHED_FROM_NOTIFICATION_EXTRA, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, contentIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        // Determine whether a contraction is currently ongoing
        final int startTimeColumnIndex = data.getColumnIndex(ContractionContract.Contractions.COLUMN_NAME_START_TIME);
        final int endTimeColumnIndex = data.getColumnIndex(ContractionContract.Contractions.COLUMN_NAME_END_TIME);
        final boolean contractionOngoing = data.isNull(endTimeColumnIndex);
        Intent startStopIntent = new Intent(this, AppWidgetToggleService.class);
        startStopIntent.putExtra(AppWidgetToggleService.WIDGET_NAME_EXTRA, "NotificationAction");
        PendingIntent startStopPendingIntent = PendingIntent.getService(this, 0, startStopIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        if (contractionOngoing) {
            builder.setContentTitle(getString(R.string.notification_timing));<commit_msg>Set an alarm to update the notification after first start time + average time frame amount of time<commit_after>package com.ianhanniballake.contractiontimer.notification;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.preview.support.v4.app.NotificationManagerCompat;
import android.preview.support.wearable.notifications.RemoteInput;
import android.preview.support.wearable.notifications.WearableNotifications;
import android.provider.BaseColumns;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.text.format.DateUtils;

import com.ianhanniballake.contractiontimer.R;
import com.ianhanniballake.contractiontimer.appwidget.AppWidgetToggleService;
import com.ianhanniballake.contractiontimer.provider.ContractionContract;
import com.ianhanniballake.contractiontimer.ui.MainActivity;
import com.ianhanniballake.contractiontimer.ui.Preferences;

/**
 * IntentService which updates the ongoing notification
 */
public class NotificationUpdateService extends IntentService {
    private static final int NOTIFICATION_ID = 0;

    public NotificationUpdateService() {
        super(NotificationUpdateService.class.getSimpleName());
    }

    public static void updateNotification(Context context) {
        context.startService(new Intent(context, NotificationUpdateService.class));
    }

    @Override
    protected void onHandleIntent(final Intent intent) {
        NoteTransparentActivity.checkServiceState(this);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final boolean notificationsEnabled = preferences.getBoolean(Preferences.NOTIFICATION_ENABLE_PREFERENCE_KEY,
                getResources().getBoolean(R.bool.pref_notification_enable_default));
        if (!notificationsEnabled) {
            notificationManager.cancel(NOTIFICATION_ID);
            return;
        }
        final String[] projection = {BaseColumns._ID,
                ContractionContract.Contractions.COLUMN_NAME_START_TIME,
                ContractionContract.Contractions.COLUMN_NAME_END_TIME,
                ContractionContract.Contractions.COLUMN_NAME_NOTE};
        final String selection = ContractionContract.Contractions.COLUMN_NAME_START_TIME + ">?";
        final long averagesTimeFrame = Long.parseLong(preferences.getString(
                Preferences.AVERAGE_TIME_FRAME_PREFERENCE_KEY,
                getString(R.string.pref_settings_average_time_frame_default)));
        final long timeCutoff = System.currentTimeMillis() - averagesTimeFrame;
        final String[] selectionArgs = {Long.toString(timeCutoff)};
        final Cursor data = getContentResolver().query(ContractionContract.Contractions.CONTENT_URI, projection,
                selection, selectionArgs, null);
        if (data == null || !data.moveToFirst()) {
            notificationManager.cancel(NOTIFICATION_ID);
            if (data != null) {
                data.close();
            }
            return;
        }
        // Set an alarm to update the notification after first start time + average time frame amount of time
        // This ensures that if no contraction has started since then (likely, otherwise we would have been called in
        // the mean time) we will fail the above check as there will be no contractions within the average time period
        // and the notification will be cancelled
        final int startTimeColumnIndex = data.getColumnIndex(ContractionContract.Contractions.COLUMN_NAME_START_TIME);
        final long lastStartTime = data.getLong(startTimeColumnIndex);
        Intent autoCancelIntent = new Intent(this, NotificationUpdateService.class);
        PendingIntent autoCancelPendingIntent = PendingIntent.getService(this, 0, autoCancelIntent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.cancel(autoCancelPendingIntent);
        // We don't need to wake up the device as it doesn't matter if the notification is cancelled until the device
        // is woken up
        alarmManager.set(AlarmManager.RTC, lastStartTime + averagesTimeFrame, autoCancelPendingIntent);
        // Build the notification
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.ic_notification);
        Intent contentIntent = new Intent(this, MainActivity.class);
        contentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_CLEAR_TASK |
                Intent.FLAG_ACTIVITY_TASK_ON_HOME);
        contentIntent.putExtra(MainActivity.LAUNCHED_FROM_NOTIFICATION_EXTRA, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, contentIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);
        // Determine whether a contraction is currently ongoing
        final int endTimeColumnIndex = data.getColumnIndex(ContractionContract.Contractions.COLUMN_NAME_END_TIME);
        final boolean contractionOngoing = data.isNull(endTimeColumnIndex);
        Intent startStopIntent = new Intent(this, AppWidgetToggleService.class);
        startStopIntent.putExtra(AppWidgetToggleService.WIDGET_NAME_EXTRA, "NotificationAction");
        PendingIntent startStopPendingIntent = PendingIntent.getService(this, 0, startStopIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        if (contractionOngoing) {
            builder.setContentTitle(getString(R.string.notification_timing));
{'fraction_non_alphanumeric': 0.07296177744304275, 'fraction_numerical': 0.001129730747505178, 'mean_word_length': 3.9873239436619716, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5223157', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2651, 'n_words': 516}