<commit_before>/*
 * Copyright 2016, The Android Open Source 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 edu.Groove9.TunesMaster.songplayer;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.CountDownTimer;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import edu.Groove9.TunesMaster.Injection;
import edu.Groove9.TunesMaster.R;
import edu.Groove9.TunesMaster.addedittask.AddEditTaskActivity;
import edu.Groove9.TunesMaster.addedittask.AddEditTaskFragment;
import edu.Groove9.TunesMaster.logging.Logger;
import edu.Groove9.TunesMaster.logging.UserEvent;
import edu.Groove9.TunesMaster.songplayer.player.AudioPlayerContract;
import edu.Groove9.TunesMaster.songplayer.player.SongStatus;
import edu.Groove9.TunesMaster.voice.CommandParseException;
import edu.Groove9.TunesMaster.voice.IVoiceListener;
import edu.Groove9.TunesMaster.voice.IVoiceRecognizer;
import edu.Groove9.TunesMaster.voice.VoiceListener;
import edu.Groove9.TunesMaster.voice.VoiceRecognizer;
import edu.Groove9.TunesMaster.voice.VoiceResult;

import static android.app.Activity.RESULT_OK;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Main UI for the task detail screen.
 */
public class SongPlayerFragment extends Fragment implements SongPlayerContract.View {

    @NonNull
    private static final String ARGUMENT_TASK_ID = "TASK_ID";

    @NonNull
    private static final int REQUEST_EDIT_TASK = 1;

    private SongPlayerContract.Presenter mPresenter;

    private TextView mDetailTitle;

   // private TextView mDetailDescription;

    private TextView mDetailDescription;
    private Button playpause ;
    private Button last ;
    private Button next;
    private Button shuffle;
    private SeekBar mSongProgress;

    // The following are used for the shake detection
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;

    // Use for Voice
    IVoiceListener voiceListener;
    IVoiceRecognizer voiceRecognizer;

    private AudioPlayerContract audioPlayer;
    private Handler songProgressUpdateHandler;
    private Timer songProgressUpdateTimer;

    public static SongPlayerFragment newInstance(@Nullable String taskId) {
        Bundle arguments = new Bundle();
        arguments.putString(ARGUMENT_TASK_ID, taskId);
        SongPlayerFragment fragment = new SongPlayerFragment();
        fragment.setArguments(arguments);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        // Add the following line to register the Session Manager Listener onResume
        mSensorManager.registerListener(mShakeDetector, mAccelerometer,	SensorManager.SENSOR_DELAY_UI);

        mPresenter.start();
        mPresenter.updatePlayPauseIcon(audioPlayer);
    }

    @Override
    public void onPause() {
        // Add the following line to unregister the Sensor Manager onPause
        mSensorManager.unregisterListener(mShakeDetector);
        super.onPause();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.songplayer_frag, container, false);
        setHasOptionsMenu(true);
        mDetailTitle = (TextView) root.findViewById(R.id.task_detail_title);
       // mDetailDescription = (TextView) root.findViewById(R.id.task_detail_description);

        playpause = (Button) root.findViewById(R.id.song_player_playpause);
        last = (Button) root.findViewById(R.id.song_player_last);
        next = (Button) root.findViewById(R.id.song_player_next);
        shuffle = (Button) root.findViewById(R.id.song_player_shuffle);

        audioPlayer = Injection.provideAudioPlayer(getActivity().getApplicationContext());

        //Set up floating action button
//        FloatingActionButton fab =
//                (FloatingActionButton) getActivity().findViewById(R.id.fab_edit_task);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mPresenter.editSong();
//            }
//        });

        setupSongPanel(root);
        setupShakeDetector();
        setupVoice();
        setupControlPanel(root);
        setupMacroPanel(root);

        setupSongProgressSeekbar(root);

        return root;
    }

    private void setupSongPanel(View root) {
        //Set up song panel (gestures)
        LinearLayout songPanel = (LinearLayout) root.findViewById(R.id.song_player_song_panel);
        songPanel.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
            public void onSwipeTop() {
                Toast.makeText(getActivity(),"Increase Volume",Toast.LENGTH_SHORT).show();
                mPresenter.volumeUp();
            }
            public void onSwipeRight() {
                mPresenter.nextSong();
                Logger.get().log(new UserEvent(UserEvent.Source.Gesture, UserEvent.Action.Next));
            }
            public void onSwipeLeft() {
                mPresenter.lastSong();
                Logger.get().log(new UserEvent(UserEvent.Source.Gesture, UserEvent.Action.Last));
            }
            public void onSwipeBottom() {
                Toast.makeText(getActivity(), "Decrease Volume", Toast.LENGTH_SHORT).show();
                mPresenter.volumeDown();<commit_msg>swapped next/last gestures<commit_after>/*
 * Copyright 2016, The Android Open Source 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 edu.Groove9.TunesMaster.songplayer;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.os.CountDownTimer;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import edu.Groove9.TunesMaster.Injection;
import edu.Groove9.TunesMaster.R;
import edu.Groove9.TunesMaster.addedittask.AddEditTaskActivity;
import edu.Groove9.TunesMaster.addedittask.AddEditTaskFragment;
import edu.Groove9.TunesMaster.logging.Logger;
import edu.Groove9.TunesMaster.logging.UserEvent;
import edu.Groove9.TunesMaster.songplayer.player.AudioPlayerContract;
import edu.Groove9.TunesMaster.songplayer.player.SongStatus;
import edu.Groove9.TunesMaster.voice.CommandParseException;
import edu.Groove9.TunesMaster.voice.IVoiceListener;
import edu.Groove9.TunesMaster.voice.IVoiceRecognizer;
import edu.Groove9.TunesMaster.voice.VoiceListener;
import edu.Groove9.TunesMaster.voice.VoiceRecognizer;
import edu.Groove9.TunesMaster.voice.VoiceResult;

import static android.app.Activity.RESULT_OK;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Main UI for the task detail screen.
 */
public class SongPlayerFragment extends Fragment implements SongPlayerContract.View {

    @NonNull
    private static final String ARGUMENT_TASK_ID = "TASK_ID";

    @NonNull
    private static final int REQUEST_EDIT_TASK = 1;

    private SongPlayerContract.Presenter mPresenter;

    private TextView mDetailTitle;

   // private TextView mDetailDescription;

    private TextView mDetailDescription;
    private Button playpause ;
    private Button last ;
    private Button next;
    private Button shuffle;
    private SeekBar mSongProgress;

    // The following are used for the shake detection
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private ShakeDetector mShakeDetector;

    // Use for Voice
    IVoiceListener voiceListener;
    IVoiceRecognizer voiceRecognizer;

    private AudioPlayerContract audioPlayer;
    private Handler songProgressUpdateHandler;
    private Timer songProgressUpdateTimer;

    public static SongPlayerFragment newInstance(@Nullable String taskId) {
        Bundle arguments = new Bundle();
        arguments.putString(ARGUMENT_TASK_ID, taskId);
        SongPlayerFragment fragment = new SongPlayerFragment();
        fragment.setArguments(arguments);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        // Add the following line to register the Session Manager Listener onResume
        mSensorManager.registerListener(mShakeDetector, mAccelerometer,	SensorManager.SENSOR_DELAY_UI);

        mPresenter.start();
        mPresenter.updatePlayPauseIcon(audioPlayer);
    }

    @Override
    public void onPause() {
        // Add the following line to unregister the Sensor Manager onPause
        mSensorManager.unregisterListener(mShakeDetector);
        super.onPause();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.songplayer_frag, container, false);
        setHasOptionsMenu(true);
        mDetailTitle = (TextView) root.findViewById(R.id.task_detail_title);
       // mDetailDescription = (TextView) root.findViewById(R.id.task_detail_description);

        playpause = (Button) root.findViewById(R.id.song_player_playpause);
        last = (Button) root.findViewById(R.id.song_player_last);
        next = (Button) root.findViewById(R.id.song_player_next);
        shuffle = (Button) root.findViewById(R.id.song_player_shuffle);

        audioPlayer = Injection.provideAudioPlayer(getActivity().getApplicationContext());

        //Set up floating action button
//        FloatingActionButton fab =
//                (FloatingActionButton) getActivity().findViewById(R.id.fab_edit_task);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mPresenter.editSong();
//            }
//        });

        setupSongPanel(root);
        setupShakeDetector();
        setupVoice();
        setupControlPanel(root);
        setupMacroPanel(root);

        setupSongProgressSeekbar(root);

        return root;
    }

    private void setupSongPanel(View root) {
        //Set up song panel (gestures)
        LinearLayout songPanel = (LinearLayout) root.findViewById(R.id.song_player_song_panel);
        songPanel.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
            public void onSwipeTop() {
                Toast.makeText(getActivity(),"Increase Volume",Toast.LENGTH_SHORT).show();
                mPresenter.volumeUp();
            }
            public void onSwipeRight() {
                mPresenter.lastSong();
                Logger.get().log(new UserEvent(UserEvent.Source.Gesture, UserEvent.Action.Next));
            }
            public void onSwipeLeft() {
                mPresenter.nextSong();
                Logger.get().log(new UserEvent(UserEvent.Source.Gesture, UserEvent.Action.Last));
            }
            public void onSwipeBottom() {
                Toast.makeText(getActivity(), "Decrease Volume", Toast.LENGTH_SHORT).show();
                mPresenter.volumeDown();
{'fraction_non_alphanumeric': 0.08701172604376727, 'fraction_numerical': 0.0037344088430801402, 'mean_word_length': 3.4220607661822986, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4744696', 'n_tokens_mistral': 3843, 'n_tokens_neox': 3643, 'n_words': 849}