<commit_before>/*
 * Copyright (C) 2010-2014 The MPDroid Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.namelessdev.mpdroid;

import com.namelessdev.mpdroid.helpers.UpdateTrackInfo;
import com.namelessdev.mpdroid.helpers.MPDAsyncHelper;
import com.namelessdev.mpdroid.helpers.MPDAsyncHelper.ConnectionListener;
import com.namelessdev.mpdroid.tools.SettingsHelper;

import org.a0z.mpd.MPD;
import org.a0z.mpd.MPDStatus;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager.BadTokenException;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

import static android.util.Log.w;

public class MPDApplication extends Application implements ConnectionListener {

    public class ApplicationState {
        public boolean streamingMode = false;
        public boolean notificationMode = false;
        public boolean persistentNotification = false;
        public boolean settingsShown = false;
        public boolean warningShown = false;
        public MPDStatus currentMpdStatus = null;
    }

    class DialogClickListener implements OnClickListener {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case AlertDialog.BUTTON_NEUTRAL:
                    // Show Settings
                    currentActivity.startActivityForResult(new Intent(currentActivity,
                            WifiConnectionSettings.class), SETTINGS);
                    break;
                case AlertDialog.BUTTON_NEGATIVE:
                    currentActivity.finish();
                    break;
                case AlertDialog.BUTTON_POSITIVE:
                    connectMPD();
                    break;

            }
        }
    }

    private static MPDApplication instance;

    public static final String TAG = "MPDroid";
    private static final long DISCONNECT_TIMER = 15000;
    public MPDAsyncHelper oMPDAsyncHelper = null;
    public UpdateTrackInfo updateTrackInfo = null;

    private SettingsHelper settingsHelper = null;
    private ApplicationState state = new ApplicationState();
    private Collection<Object> connectionLocks = new LinkedList<Object>();
    private AlertDialog ad;

    private Activity currentActivity;

    private Timer disconnectSheduler;

    public static final int SETTINGS = 5;

    public static MPDApplication getInstance() {
        return instance;
    }

    private void cancelDisconnectSheduler() {
        disconnectSheduler.cancel();
        disconnectSheduler.purge();
        disconnectSheduler = new Timer();
    }

    private void checkConnectionNeeded() {
        if (connectionLocks.size() > 0) {
            if (!oMPDAsyncHelper.isMonitorAlive()) {
                oMPDAsyncHelper.startMonitor();
            }
            if (!oMPDAsyncHelper.oMPD.isConnected()
                    && (currentActivity == null || !currentActivity.getClass().equals(
                            WifiConnectionSettings.class))) {
                connect();
            }
        } else {
            disconnect();
        }
    }

    public void connect() {
        if (!settingsHelper.updateSettings()) {
            // Absolutely no settings defined! Open Settings!
            if (currentActivity != null && !state.settingsShown) {
                currentActivity.startActivityForResult(new Intent(currentActivity,
                        WifiConnectionSettings.class), SETTINGS);
                state.settingsShown = true;
            }
        }

        if (currentActivity != null && !settingsHelper.warningShown() && !state.warningShown) {
            currentActivity.startActivity(new Intent(currentActivity, WarningActivity.class));
            state.warningShown = true;
        }
        connectMPD();
    }

    public synchronized void connectionFailed(String message) {

        if (ad != null && !(ad instanceof ProgressDialog) && ad.isShowing()) {
            return;<commit_msg>MPDApplication: Move settings/warnings shown into the outer class.<commit_after>/*
 * Copyright (C) 2010-2014 The MPDroid Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.namelessdev.mpdroid;

import com.namelessdev.mpdroid.helpers.MPDAsyncHelper;
import com.namelessdev.mpdroid.helpers.MPDAsyncHelper.ConnectionListener;
import com.namelessdev.mpdroid.helpers.UpdateTrackInfo;
import com.namelessdev.mpdroid.tools.SettingsHelper;

import org.a0z.mpd.MPD;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager.BadTokenException;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

import static android.util.Log.w;

public class MPDApplication extends Application implements ConnectionListener {

    public class ApplicationState {
        public boolean streamingMode = false;
        public boolean notificationMode = false;
        public boolean persistentNotification = false;
    }

    class DialogClickListener implements OnClickListener {
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case AlertDialog.BUTTON_NEUTRAL:
                    // Show Settings
                    currentActivity.startActivityForResult(new Intent(currentActivity,
                            WifiConnectionSettings.class), SETTINGS);
                    break;
                case AlertDialog.BUTTON_NEGATIVE:
                    currentActivity.finish();
                    break;
                case AlertDialog.BUTTON_POSITIVE:
                    connectMPD();
                    break;

            }
        }
    }

    private static MPDApplication instance;

    public static final String TAG = "MPDroid";
    private static final long DISCONNECT_TIMER = 15000;
    public MPDAsyncHelper oMPDAsyncHelper = null;
    public UpdateTrackInfo updateTrackInfo = null;

    private SettingsHelper settingsHelper = null;
    private ApplicationState state = new ApplicationState();
    private Collection<Object> connectionLocks = new LinkedList<Object>();
    private AlertDialog ad;

    private boolean settingsShown = false;
    private boolean warningShown = false;

    private Activity currentActivity;

    private Timer disconnectSheduler;

    public static final int SETTINGS = 5;

    public static MPDApplication getInstance() {
        return instance;
    }

    private void cancelDisconnectSheduler() {
        disconnectSheduler.cancel();
        disconnectSheduler.purge();
        disconnectSheduler = new Timer();
    }

    private void checkConnectionNeeded() {
        if (connectionLocks.size() > 0) {
            if (!oMPDAsyncHelper.isMonitorAlive()) {
                oMPDAsyncHelper.startMonitor();
            }
            if (!oMPDAsyncHelper.oMPD.isConnected()
                    && (currentActivity == null || !currentActivity.getClass().equals(
                            WifiConnectionSettings.class))) {
                connect();
            }
        } else {
            disconnect();
        }
    }

    public void connect() {
        if (!settingsHelper.updateSettings()) {
            // Absolutely no settings defined! Open Settings!
            if (currentActivity != null && !settingsShown) {
                currentActivity.startActivityForResult(new Intent(currentActivity,
                        WifiConnectionSettings.class), SETTINGS);
                settingsShown = true;
            }
        }

        if (currentActivity != null && !settingsHelper.warningShown() && !warningShown) {
            currentActivity.startActivity(new Intent(currentActivity, WarningActivity.class));
            warningShown = true;
        }
        connectMPD();
    }

    public synchronized void connectionFailed(String message) {

        if (ad != null && !(ad instanceof ProgressDialog) && ad.isShowing()) {
            return;
{'fraction_non_alphanumeric': 0.07828282828282829, 'fraction_numerical': 0.004141414141414141, 'mean_word_length': 2.7151969981238273, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1367567', 'n_tokens_mistral': 2509, 'n_tokens_neox': 2410, 'n_words': 666}