package com.neeplayer;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;


public class NowPlayingActivity extends Activity implements MediaController.MediaPlayerControl {
    ArrayList<Album> albumList;
    String artistName;

    int albumPosition;
    int songPosition;

    private MusicService musicService;
    private Intent playIntent;

    private MusicController controller;

    private Boolean musicBound = false;

    private ImageLoader imageLoader;

    private ServiceConnection musicConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MusicService.MusicBinder binder = (MusicService.MusicBinder) service;
            musicService = binder.getService();

            musicService.setList(albumList);
            musicService.setPosition(albumPosition, songPosition);
            musicService.playSong();

            musicBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicBound = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_now_playing);

        imageLoader = ImageLoader.getInstance();

        onNewIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        albumPosition = intent.getIntExtra("ALBUM_POSITION", 0);
        songPosition = intent.getIntExtra("SONG_POSITION", 0);
        albumList = (ArrayList<Album>) intent.getSerializableExtra("ALBUM_LIST");
        artistName = intent.getStringExtra("ARTIST_NAME");

        if (playIntent != null) {
            unbindService(musicConnection);
            stopService(playIntent);
        }

        playIntent = new Intent(this, MusicService.class);
        bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);
        startService(playIntent);

        Album album = albumList.get(albumPosition);
        Song song = album.getSongs().get(songPosition);

        TextView songTitleView = (TextView)findViewById(R.id.np_song_title);
        songTitleView.setText(song.getTitle());

        TextView albumArtistView = (TextView)findViewById(R.id.np_artist_and_album);
        albumArtistView.setText(String.format("%s — %s", artistName, album.getTitle()));

        ImageView artView = (ImageView)findViewById(R.id.np_album_art);
        imageLoader.displayImage("file://" + album.getArt(), artView);

        setController();
    }

    @Override
    protected void onDestroy() {
        unbindService(musicConnection);
        stopService(playIntent);
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_now_playing, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void setController() {
        controller = new MusicController(this);
        controller.setMediaPlayer(this);
        controller.setAnchorView(findViewById(R.id.np_layout));
        controller.setEnabled(true);
    }

    private void playNext() {
        musicService.playNext();
        controller.show(0);
    }

    private void playPrevious() {
        musicService.playPrevious();
        controller.show(0);
    }

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }

    @Override
    public void start() {
        musicService.start();
    }

    @Override
    public void pause() {
        musicService.pausePlayer();
    }

    @Override
    public int getDuration() {
        if (musicService != null && musicBound && musicService.isPlaying()) {
            return musicService.getDuration();
        } else {
            return 0;
        }
    }

    @Override
    public int getCurrentPosition() {
        if (musicService != null && musicBound && musicService.isPlaying()) {
            return musicService.getSongPosition();
        } else {
            return 0;
        }
    }

    @Override
    public void seekTo(int pos) {
        musicService.seekTo(pos);
    }

    @Override
    public boolean isPlaying() {
        if (musicService != null && musicBound) {
            return musicService.isPlaying();
        } else {
            return false;
        }
    }

    @Override
    public int getBufferPercentage() {
        return 0;
    }

    @Override
    public boolean canPause() {
        return true;
    }

    @Override
    public boolean canSeekBackward() {
        return true;
    }

    @Override
    public boolean canSeekForward() {
        return true;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }
}

@@ -685,54 +685,8 @@
 ity 
-implements MediaController.MediaPlayerControl 
 %7B%0A  
@@ -860,49 +860,8 @@
 t;%0A%0A
-    private MusicController controller;%0A%0A
     
@@ -3044,33 +3044,8 @@
 );%0A%0A
-        setController();%0A
     
@@ -3945,1825 +3945,87 @@
     
-private void setController() %7B%0A        controller = new MusicController(this);%0A        controller.setMediaPlayer(this);%0A        controller.setAnchorView(findViewById(R.id.np_layout));%0A        controller.setEnabled(true);%0A    %7D%0A%0A    private void playNext() %7B%0A        musicService.playNext();%0A        controller.show(0);%0A    %7D%0A%0A    private void playPrevious() %7B%0A        musicService.playPrevious();%0A        controller.show(0);%0A    %7D%0A%0A    @Override%0A    public void onBackPressed() %7B%0A        moveTaskToBack(true);%0A    %7D%0A%0A    @Override%0A    public void start() %7B%0A        musicService.start();%0A    %7D%0A%0A    @Override%0A    public void pause() %7B%0A        musicService.pausePlayer();%0A    %7D%0A%0A    @Override%0A    public int getDuration() %7B%0A        if (musicService != null && musicBound && musicService.isPlaying()) %7B%0A            return musicService.getDuration();%0A        %7D else %7B%0A            return 0;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public int getCurrentPosition() %7B%0A        if (musicService != null && musicBound && musicService.isPlaying()) %7B%0A            return musicService.getSongPosition();%0A        %7D else %7B%0A            return 0;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void seekTo(int pos) %7B%0A        musicService.seekTo(pos);%0A    %7D%0A%0A    @Override%0A    public boolean isPlaying() %7B%0A        if (musicService != null && musicBound) %7B%0A            return musicService.isPlaying();%0A        %7D else %7B%0A            return false;%0A        %7D%0A    %7D%0A%0A    @Override%0A    public int getBufferPercentage() %7B%0A        return 0;%0A    %7D%0A%0A    @Override%0A    public boolean canPause() %7B%0A        return true;%0A    %7D%0A%0A    @Override%0A    public boolean canSeekBackward() %7B%0A        return true;%0A    %7D%0A%0A    @Override%0A    public boolean canSeekForward() %7B%0A        return true;%0A    %7D%0A%0A    @Override%0A    public int getAudioSessionId() %7B%0A        return 0;%0A    %7D
+@Override%0A    public void onBackPressed() %7B%0A        moveTaskToBack(true);%0A    %7D%0A
 %0A%7D%0A

Remove MediaController implementation