<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.fragments;

import com.namelessdev.mpdroid.MPDApplication;
import com.namelessdev.mpdroid.R;
import com.namelessdev.mpdroid.StreamingService;
import com.namelessdev.mpdroid.helpers.AlbumCoverDownloadListener;
import com.namelessdev.mpdroid.helpers.CoverAsyncHelper;
import com.namelessdev.mpdroid.helpers.CoverManager;
import com.namelessdev.mpdroid.helpers.MPDConnectionHandler;
import com.namelessdev.mpdroid.library.SimpleLibraryActivity;

import org.a0z.mpd.AlbumInfo;
import org.a0z.mpd.MPD;
import org.a0z.mpd.MPDStatus;
import org.a0z.mpd.Music;
import org.a0z.mpd.event.StatusChangeListener;
import org.a0z.mpd.event.TrackPositionListener;
import org.a0z.mpd.exception.MPDServerException;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.PopupMenuCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import static android.text.TextUtils.isEmpty;
import static com.namelessdev.mpdroid.tools.StringUtils.getExtension;

public class NowPlayingFragment extends Fragment implements StatusChangeListener,
        TrackPositionListener,
        OnSharedPreferenceChangeListener, OnMenuItemClickListener {

    private class ButtonEventHandler implements Button.OnClickListener, Button.OnLongClickListener {

        public void onClick(View v) {
            final MPD mpd = app.oMPDAsyncHelper.oMPD;
            Intent i;

            switch (v.getId()) {
                case R.id.stop:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mpd.stop();
                            } catch (MPDServerException e) {
                                Log.w(MPDApplication.TAG, e.getMessage());
                            }
                        }
                    }).start();
                    if (((MPDApplication) activity.getApplication()).getApplicationState().streamingMode) {
                        i = new Intent(app, StreamingService.class);
                        i.setAction(StreamingService.ACTION_DIE);
                        activity.startService(i);
                        ((MPDApplication) activity.getApplication()).getApplicationState().streamingMode = false;
                    }
                    break;
                case R.id.next:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mpd.next();
                            } catch (MPDServerException e) {
                                Log.w(MPDApplication.TAG, e.getMessage());
                            }
                        }
                    }).start();
                    if (((MPDApplication) activity.getApplication()).getApplicationState().streamingMode) {
                        i = new Intent(app, StreamingService.class);
                        i.setAction(StreamingService.ACTION_RESET);
                        activity.startService(i);
                    }
                    break;
                case R.id.prev:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mpd.previous();
                            } catch (MPDServerException e) {
                                Log.w(MPDApplication.TAG, e.getMessage());
                            }
                        }
                    }).start();

                    if (((MPDApplication) activity.getApplication()).getApplicationState().streamingMode) {
                        i = new Intent(app, StreamingService.class);
                        i.setAction(StreamingService.ACTION_RESET);
                        activity.startService(i);
                    }
                    break;
                case R.id.playpause:
                    /**
                     * If playing or paused, just toggle state, otherwise start
                     * playing.
                     * 
                     * @author slubman
                     */
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String state;
                            try {
                                state = mpd.getStatus().getState();
                                if (state.equals(MPDStatus.MPD_STATE_PLAYING)
                                        || state.equals(MPDStatus.MPD_STATE_PAUSED)) {
                                    mpd.pause();
                                } else {
                                    mpd.play();
                                }
                            } catch (MPDServerException e) {
                                Log.w(MPDApplication.TAG, e.getMessage());
                            }
                        }
                    }).start();
                    break;
                case R.id.shuffle:
                    try {
                        mpd.setRandom(!mpd.getStatus().isRandom());
                    } catch (MPDServerException e) {
                    }
                    break;
                case R.id.repeat:
                    try {
                        mpd.setRepeat(!mpd.getStatus().isRepeat());
                    } catch (MPDServerException e) {
                    }
                    break;

            }
        }

        public boolean onLongClick(View v) {
            MPD mpd = app.oMPDAsyncHelper.oMPD;
            try {
                switch (v.getId()) {
                    case R.id.playpause:
                        // Implements the ability to stop playing (may be useful
                        // for streams)
                        mpd.stop();
                        Intent i;
                        if (((MPDApplication) activity.getApplication()).getApplicationState().streamingMode) {
                            i = new Intent(app, StreamingService.class);
                            i.setAction(StreamingService.ACTION_STOP);
                            activity.startService(i);
                        }
                        break;
                    default:
                        return false;
                }
                return true;
            } catch (MPDServerException e) {

            }
            return true;
        }<commit_msg>NowPlayingFragment: Don't control StreamingService by Intent.<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.fragments;

import com.namelessdev.mpdroid.MPDApplication;
import com.namelessdev.mpdroid.R;
import com.namelessdev.mpdroid.helpers.AlbumCoverDownloadListener;
import com.namelessdev.mpdroid.helpers.CoverAsyncHelper;
import com.namelessdev.mpdroid.helpers.CoverManager;
import com.namelessdev.mpdroid.helpers.MPDConnectionHandler;
import com.namelessdev.mpdroid.library.SimpleLibraryActivity;

import org.a0z.mpd.AlbumInfo;
import org.a0z.mpd.MPD;
import org.a0z.mpd.MPDStatus;
import org.a0z.mpd.Music;
import org.a0z.mpd.event.StatusChangeListener;
import org.a0z.mpd.event.TrackPositionListener;
import org.a0z.mpd.exception.MPDServerException;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.PopupMenuCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import static android.text.TextUtils.isEmpty;
import static com.namelessdev.mpdroid.tools.StringUtils.getExtension;

public class NowPlayingFragment extends Fragment implements StatusChangeListener,
        TrackPositionListener,
        OnSharedPreferenceChangeListener, OnMenuItemClickListener {

    private class ButtonEventHandler implements Button.OnClickListener, Button.OnLongClickListener {

        public void onClick(View v) {
            final MPD mpd = app.oMPDAsyncHelper.oMPD;
            Intent i;

            switch (v.getId()) {
                case R.id.stop:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mpd.stop();
                            } catch (MPDServerException e) {
                                Log.w(MPDApplication.TAG, e.getMessage());
                            }
                        }
                    }).start();
                    break;
                case R.id.next:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mpd.next();
                            } catch (MPDServerException e) {
                                Log.w(MPDApplication.TAG, e.getMessage());
                            }
                        }
                    }).start();
                    break;
                case R.id.prev:
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                mpd.previous();
                            } catch (MPDServerException e) {
                                Log.w(MPDApplication.TAG, e.getMessage());
                            }
                        }
                    }).start();
                    break;
                case R.id.playpause:
                    /**
                     * If playing or paused, just toggle state, otherwise start
                     * playing.
                     * 
                     * @author slubman
                     */
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String state;
                            try {
                                state = mpd.getStatus().getState();
                                if (state.equals(MPDStatus.MPD_STATE_PLAYING)
                                        || state.equals(MPDStatus.MPD_STATE_PAUSED)) {
                                    mpd.pause();
                                } else {
                                    mpd.play();
                                }
                            } catch (MPDServerException e) {
                                Log.w(MPDApplication.TAG, e.getMessage());
                            }
                        }
                    }).start();
                    break;
                case R.id.shuffle:
                    try {
                        mpd.setRandom(!mpd.getStatus().isRandom());
                    } catch (MPDServerException e) {
                    }
                    break;
                case R.id.repeat:
                    try {
                        mpd.setRepeat(!mpd.getStatus().isRepeat());
                    } catch (MPDServerException e) {
                    }
                    break;

            }
        }

        public boolean onLongClick(View v) {
            MPD mpd = app.oMPDAsyncHelper.oMPD;
            try {
                switch (v.getId()) {
                    case R.id.playpause:
                        // Implements the ability to stop playing (may be useful
                        // for streams)
                        mpd.stop();
                        break;
                    default:
                        return false;
                }
                return true;
            } catch (MPDServerException e) {

            }
            return true;
        }
{'fraction_non_alphanumeric': 0.08120987490899464, 'fraction_numerical': 0.002912171553378781, 'mean_word_length': 1.393094710167881, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5937125', 'n_tokens_mistral': 3695, 'n_tokens_neox': 3501, 'n_words': 774}