<commit_before>package com.apps.adrcotfas.goodtime;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import java.util.concurrent.TimeUnit;

import static com.apps.adrcotfas.goodtime.TimerActivity.NOTIFICATION_TAG;
import static com.apps.adrcotfas.goodtime.Notifications.createCompletionNotification;
import static com.apps.adrcotfas.goodtime.Notifications.createForegroundNotification;
import static com.apps.adrcotfas.goodtime.Preferences.PREFERENCES_NAME;
import static com.apps.adrcotfas.goodtime.SessionType.WORK;
import static com.apps.adrcotfas.goodtime.TimerState.ACTIVE;
import static com.apps.adrcotfas.goodtime.TimerState.INACTIVE;
import static com.apps.adrcotfas.goodtime.TimerState.PAUSED;

public class TimerService extends Service {

    private static final int NOTIFICATION_ID = 1;
    private static final String TAG = "TimerService";
    public final static String ACTION_TIMERSERVICE = "com.apps.adrcotfas.goodtime.TIMERSERVICE";

    private long mCountDownFinishedTime;
    private int mRemainingTimePaused;
    private int mCurrentSessionStreak;
    private TimerState mTimerState;
    private TimerState mTimerBroughtToForegroundState;
    private final IBinder mBinder = new TimerBinder();
    private LocalBroadcastManager mBroadcastManager;
    private int mPreviousRingerMode;
    private boolean mPreviousWifiMode;
    private boolean mIsOnForeground;
    private boolean mIsTimerRunning;
    private Preferences mPref;
    private SessionType mCurrentSession;

    @Override
    public void onCreate() {
        super.onCreate();

        SharedPreferences preferences = getApplicationContext().getSharedPreferences(
                PREFERENCES_NAME,
                Context.MODE_PRIVATE
        );
        mPref = new Preferences(preferences);
        mTimerState = INACTIVE;
        mCurrentSession = WORK;

        mBroadcastManager = LocalBroadcastManager.getInstance(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void startSession(SessionType sessionType) {
        mIsTimerRunning = true;
        mCountDownFinishedTime = calculateSessionDurationFor(sessionType);
        Log.i(TAG, "Starting new timer for " + sessionType + ", ending in "
                + getRemainingTime() + " seconds.");

        if (mPref.getDisableSoundAndVibration() && sessionType == WORK) {
            saveCurrentStateOfSound();
            disableSound();
        }
        if (mPref.getDisableWifi() && sessionType == WORK) {
            saveCurrentStateOfWifi();
            disableWifi();
        }

        mTimerState = ACTIVE;
        mCurrentSession = sessionType;
    }

    public void stopSession() {
        mIsTimerRunning = false;
        Log.d(TAG, "Session stopped");

        sendToBackground();

        if (mPref.getDisableSoundAndVibration()) {
            restoreSound();
        }
        if (mPref.getDisableWifi()) {
            restoreWifi();
        }

        mTimerState = INACTIVE;
    }

    private long calculateSessionDurationFor(SessionType sessionType) {
        long currentTime = System.currentTimeMillis();
        switch (sessionType) {
            case WORK:
                return currentTime + TimeUnit.MINUTES.toMillis(mPref.getSessionDuration());
            case BREAK:
                return currentTime + TimeUnit.MINUTES.toMillis(mPref.getBreakDuration());
            case LONG_BREAK:
                return currentTime + TimeUnit.MINUTES.toMillis(mPref.getLongBreakDuration());
            default:
                throw new IllegalStateException("This cannot happen");
        }
    }

    public void pauseTimer() {
        mTimerState = PAUSED;
        mIsTimerRunning = false;
        mRemainingTimePaused = getRemainingTime();
    }

    public void unPauseTimer() {
        mTimerState = ACTIVE;
        mIsTimerRunning = true;
        mCountDownFinishedTime = System.currentTimeMillis() +
                TimeUnit.SECONDS.toMillis(mRemainingTimePaused);
        Log.i(TAG, "Resuming countdown for " + getSessionType() + ", ending in "
                + getRemainingTime() + " seconds.");
    }

    private void onCountdownFinished() {
        Log.d(TAG, "Countdown finished");

        if (mPref.getDisableSoundAndVibration()) {
            restoreSound();
        }
        if (mPref.getDisableWifi()) {
            restoreWifi();
        }

        sendFinishedNotification();
        mTimerState = INACTIVE;
        sendToBackground();
    }

    private void saveCurrentStateOfSound() {
        AudioManager aManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        mPreviousRingerMode = aManager.getRingerMode();
    }

    private void saveCurrentStateOfWifi() {
        WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
        mPreviousWifiMode = wifiManager.isWifiEnabled();
    }

    private void disableSound() {
        Log.d(TAG, "Disabling sound");
        AudioManager aManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        aManager.setRingerMode(RINGER_MODE_SILENT);
    }

    private void disableWifi() {
        Log.d(TAG, "Disabling Wifi");
        WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
        wifiManager.setWifiEnabled(false);
    }

    private void restoreSound() {
        Log.d(TAG, "Restoring sound mode");
        AudioManager aManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        aManager.setRingerMode(mPreviousRingerMode);
    }

    private void sendFinishedNotification() {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(
                NOTIFICATION_TAG,
                createCompletionNotification(
                        this,
                        mCurrentSession,
                        mPref.getNotificationSound(),
                        mPref.getNotificationVibrate()
                )
        );
    }

    public boolean isTimerRunning() {
        return mIsTimerRunning;
    }

    public class TimerBinder extends Binder {
        TimerService getService() {
            return TimerService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public TimerState getTimerState() {<commit_msg>additional fix after master merge<commit_after>package com.apps.adrcotfas.goodtime;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.os.Binder;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import java.util.concurrent.TimeUnit;

import static android.media.AudioManager.RINGER_MODE_SILENT;
import static com.apps.adrcotfas.goodtime.TimerActivity.NOTIFICATION_TAG;
import static com.apps.adrcotfas.goodtime.Notifications.createCompletionNotification;
import static com.apps.adrcotfas.goodtime.Notifications.createForegroundNotification;
import static com.apps.adrcotfas.goodtime.Preferences.PREFERENCES_NAME;
import static com.apps.adrcotfas.goodtime.SessionType.WORK;
import static com.apps.adrcotfas.goodtime.TimerState.ACTIVE;
import static com.apps.adrcotfas.goodtime.TimerState.INACTIVE;
import static com.apps.adrcotfas.goodtime.TimerState.PAUSED;

public class TimerService extends Service {

    private static final int NOTIFICATION_ID = 1;
    private static final String TAG = "TimerService";
    public final static String ACTION_TIMERSERVICE = "com.apps.adrcotfas.goodtime.TIMERSERVICE";

    private long mCountDownFinishedTime;
    private int mRemainingTimePaused;
    private int mCurrentSessionStreak;
    private TimerState mTimerState;
    private TimerState mTimerBroughtToForegroundState;
    private final IBinder mBinder = new TimerBinder();
    private LocalBroadcastManager mBroadcastManager;
    private int mPreviousRingerMode;
    private boolean mPreviousWifiMode;
    private boolean mIsOnForeground;
    private boolean mIsTimerRunning;
    private Preferences mPref;
    private SessionType mCurrentSession;

    @Override
    public void onCreate() {
        super.onCreate();

        SharedPreferences preferences = getApplicationContext().getSharedPreferences(
                PREFERENCES_NAME,
                Context.MODE_PRIVATE
        );
        mPref = new Preferences(preferences);
        mTimerState = INACTIVE;
        mCurrentSession = WORK;

        mBroadcastManager = LocalBroadcastManager.getInstance(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void startSession(SessionType sessionType) {
        mIsTimerRunning = true;
        mCountDownFinishedTime = calculateSessionDurationFor(sessionType);
        Log.i(TAG, "Starting new timer for " + sessionType + ", ending in "
                + getRemainingTime() + " seconds.");

        if (mPref.getDisableSoundAndVibration() && sessionType == WORK) {
            saveCurrentStateOfSound();
            disableSound();
        }
        if (mPref.getDisableWifi() && sessionType == WORK) {
            saveCurrentStateOfWifi();
            disableWifi();
        }

        mTimerState = ACTIVE;
        mCurrentSession = sessionType;
    }

    public void stopSession() {
        mIsTimerRunning = false;
        Log.d(TAG, "Session stopped");

        sendToBackground();

        if (mPref.getDisableSoundAndVibration()) {
            restoreSound();
        }
        if (mPref.getDisableWifi()) {
            restoreWifi();
        }

        mTimerState = INACTIVE;
    }

    private long calculateSessionDurationFor(SessionType sessionType) {
        long currentTime = System.currentTimeMillis();
        switch (sessionType) {
            case WORK:
                return currentTime + TimeUnit.MINUTES.toMillis(mPref.getSessionDuration());
            case BREAK:
                return currentTime + TimeUnit.MINUTES.toMillis(mPref.getBreakDuration());
            case LONG_BREAK:
                return currentTime + TimeUnit.MINUTES.toMillis(mPref.getLongBreakDuration());
            default:
                throw new IllegalStateException("This cannot happen");
        }
    }

    public void pauseTimer() {
        mTimerState = PAUSED;
        mIsTimerRunning = false;
        mRemainingTimePaused = getRemainingTime();
    }

    public void unPauseTimer() {
        mTimerState = ACTIVE;
        mIsTimerRunning = true;
        mCountDownFinishedTime = System.currentTimeMillis() +
                TimeUnit.SECONDS.toMillis(mRemainingTimePaused);
        Log.i(TAG, "Resuming countdown for " + getSessionType() + ", ending in "
                + getRemainingTime() + " seconds.");
    }

    private void onCountdownFinished() {
        Log.d(TAG, "Countdown finished");

        if (mPref.getDisableSoundAndVibration()) {
            restoreSound();
        }
        if (mPref.getDisableWifi()) {
            restoreWifi();
        }

        sendFinishedNotification();
        mTimerState = INACTIVE;
        sendToBackground();
    }

    private void saveCurrentStateOfSound() {
        AudioManager aManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        mPreviousRingerMode = aManager.getRingerMode();
    }

    private void saveCurrentStateOfWifi() {
        WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
        mPreviousWifiMode = wifiManager.isWifiEnabled();
    }

    private void disableSound() {
        Log.d(TAG, "Disabling sound");
        AudioManager aManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        aManager.setRingerMode(RINGER_MODE_SILENT);
    }

    private void disableWifi() {
        Log.d(TAG, "Disabling Wifi");
        WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
        wifiManager.setWifiEnabled(false);
    }

    private void restoreSound() {
        Log.d(TAG, "Restoring sound mode");
        AudioManager aManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        aManager.setRingerMode(mPreviousRingerMode);
    }

    private void restoreWifi() {
        Log.d(TAG, "Restoring Wifi mode");
        WifiManager wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);
        wifiManager.setWifiEnabled(mPreviousWifiMode);
    }

    private void sendFinishedNotification() {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(
                NOTIFICATION_TAG,
                createCompletionNotification(
                        this,
                        mCurrentSession,
                        mPref.getNotificationSound(),
                        mPref.getNotificationVibrate()
                )
        );
    }

    public boolean isTimerRunning() {
        return mIsTimerRunning;
    }

    public class TimerBinder extends Binder {
        TimerService getService() {
            return TimerService.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public TimerState getTimerState() {
{'fraction_non_alphanumeric': 0.08278240873814315, 'fraction_numerical': 0.0002874389192296637, 'mean_word_length': 2.7715447154471544, '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': '3632300', 'n_tokens_mistral': 3846, 'n_tokens_neox': 3762, 'n_words': 751}