public class LineItemDecorator extends RecyclerView.ItemDecoration {

    private static LineItemDecorator lineItemDecorator;
    private int dimension;
    private boolean isDrawHorizontal=false;
    private boolean isDrawVertical=true;

    public LineItemDecorator() {
        super();
    }

    public void recycle() {
        lineItemDecorator = null;
    }

    public void setDrawHorizontal(boolean isDraw) {
        isDrawHorizontal = isDraw;
    }

    public void setDrawVertical(boolean isDraw) {
        isDrawVertical = isDraw;
    }

    public static LineItemDecorator getInstance() {
        if (null == lineItemDecorator) {
            synchronized (LineItemDecorator.class) {
                lineItemDecorator = new LineItemDecorator();
            }
        }
        return lineItemDecorator;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (isDrawVertical) {
            outRect.top = 1;
//        outRect.left = dimension;
//        outRect.right = dimension;
            outRect.bottom = 1;
        }
        if (isDrawHorizontal){
            outRect.left = 1;
//        outRect.left = dimension;
//        outRect.right = dimension;
            outRect.right = 1;
        }
    }


    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (isDrawHorizontal) {
            drawHrozontal(c, parent);
        }
        if (isDrawVertical){
            drawVertical(c, parent);
        }
    }



    private void drawVertical(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        if (childCount<1)return;
//        int left = parent.getPaddingLeft()+leftView.getLeft()+leftView.getPaddingLeft();
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.LTGRAY);
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            int top = child.getBottom()+params.bottomMargin;
            int bottom = top + 1;
            c.drawRect(left, top, right, bottom, paint);
        }
    }

    private void drawHrozontal(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        if (childCount<1)return;
//        int left = parent.getPaddingLeft()+leftView.getLeft()+leftView.getPaddingLeft();
        int top = parent.getPaddingTop();
        int bottom = parent.getHeight() - parent.getPaddingBottom();

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.LTGRAY);
        for (int i = 0; i < childCount-1; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            int left = child.getRight()+params.rightMargin;
            int right = left + 1;
            c.drawRect(left, top, right, bottom, paint);

        }
    }

} public class MusicPlayerService extends IntentService {
    // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
    private static final String ACTION_PLAY = "com.zsorg.neteasecloudmusic.action.PLAY";
    private static final String ACTION_PAUSE = "com.zsorg.neteasecloudmusic.action.PAUSE";
    private static final String ACTION_STOP ="com.zsorg.neteasecloudmusic.action.STOP";
    private static final String ACTION_SET = "com.zsorg.neteasecloudmusic.action.SET";

    private static final String EXTRA_PLAYLIST = "com.zsorg.neteasecloudmusic.extra.PLAYLIST_ID";
    private static final String EXTRA_IS_RESTART = "com.zsorg.neteasecloudmusic.extra.IS_RESTART";
    private static final String EXTRA_PLAYLIST_POSITION = "com.zsorg.neteasecloudmusic.extra.PLAYLIST_POSITION";


    private final MusicPlayerBinder mBinder;

    private List<MusicBean> mCurrentPlaylist;

    public MusicPlayerService() {
        super("MyIntentService");
        mBinder = new MusicPlayerBinder();
    }

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

    /**
     * Starts this service to perform action Foo with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see IntentService
     */
    public static void startService(Context context, ServiceConnection connection) {
        Intent intent = new Intent(context, MusicPlayerService.class);
        context.bindService(intent,connection,Context.BIND_AUTO_CREATE);
    }

    /**
     * Starts this service to perform action Foo with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see IntentService
     */
    public static void startActionPlay(Context context,boolean isRestart) {
        Intent intent = new Intent(context, MusicPlayerService.class);
        intent.putExtra(EXTRA_IS_RESTART, isRestart);
        intent.setAction(ACTION_PLAY);
        context.startService(intent);
    }

    public static void startActionSet(Context context, ArrayList<MusicBean> playlist, int position) {
        Intent intent = new Intent(context, MusicPlayerService.class);
        intent.setAction(ACTION_SET);
        intent.putParcelableArrayListExtra(EXTRA_PLAYLIST, playlist);
        intent.putExtra(EXTRA_PLAYLIST_POSITION, position);
        context.startService(intent);
    }

    /**
     * Starts this service to perform action Baz with the given parameters. If
     * the service is already performing a task this action will be queued.
     *
     * @see IntentService
     */
    public static void startActionPause(Context context) {
        Intent intent = new Intent(context, MusicPlayerService.class);
        intent.setAction(ACTION_PAUSE);
        context.startService(intent);
    }

    public static void startActionStop(Context context) {
        Intent intent = new Intent(context, MusicPlayerService.class);
        intent.setAction(ACTION_STOP);
        context.startService(intent);
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_PLAY.equals(action)) {
                boolean isRestart = intent.getBooleanExtra(EXTRA_IS_RESTART, true);
                handleActionPlay(isRestart);
            } else if (ACTION_PAUSE.equals(action)) {
                handleActionPause();
            }else if (ACTION_SET.equals(action)) {
                ArrayList<MusicBean> list = intent.getParcelableArrayListExtra(EXTRA_PLAYLIST);
                if (null != list) {
                    mCurrentPlaylist = list;
                }
                final int position = intent.getIntExtra(EXTRA_PLAYLIST_POSITION,CONST.DEFAULT_PLAYLIST_POSITION);
                handleActionSetPlaylist(position);
            }
        }
    }

    private void handleActionPause() {
        PlayerManager playerManager = PlayerManager.getInstance(this);
        playerManager.setPauseMusic(this,true);
    }

    /**
     * Handle action Play in the provided background thread with the provided
     * parameters.
     * @param isRestart
     */
    private void handleActionPlay(boolean isRestart) {
        PlayerManager playerManager = PlayerManager.getInstance(this);
        if (isRestart) {
            playerManager.playMusic(this);
        } else {
            playerManager.setPauseMusic(this,false);
        }
    }

    private void handleActionSetPlaylist(int position) {
        PlayerManager playerManager = PlayerManager.getInstance(this);
        if (mCurrentPlaylist != null) {
            playerManager.setCurrentPlaylist(mCurrentPlaylist);
        }
        playerManager.setCurrentPosition(position);
    }

//    private void playMusic(final int position) {
//        if (position < mCurrentPlaylist.size()) {
//            PlayerManager.newInstance(this).playMusic(this, mCurrentPlaylist.get(position).getPath()).setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//                @Override
//                public void onCompletion(MediaPlayer mediaPlayer) {
//                    playMusic(position + 1);
//                }
//            });
//        }
//    }

    /**
     * Handle action Stop in the provided background thread with the provided
     * parameters.
     */
    private void handleActionStop() {
        PlayerManager playerManager = PlayerManager.getInstance(this);
//        playerManager.stopMusic()
    }

    private class MusicPlayerBinder extends Binder {

    }
} public interface OnItemCLickListener {
    void onItemClick(View view, int position);
} public class MusicListActivity extends AppCompatActivity implements IPlayerView,OnItemCLickListener, View.OnClickListener {

    private static final String EXTRA_MUSIC_LIST = "com.zsorg.neteasecloudmusic.extra.MUSIC_LIST";
    private static final String EXTRA_TITLE = "com.zsorg.neteasecloudmusic.extra.TITLE";

    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.tv_song_name)
    TextView tvSongName;
    @BindView(R.id.tv_singer)
    TextView tvSinger;
    @BindView(R.id.pb_progress)
    ProgressBar progressBar;
    @BindView(R.id.iv_album)
    ImageView ivAlbum;
    @BindView(R.id.iv_play)
    ImageView ivPlay;
    @BindView(R.id.bottomLayout)
    View mBottomLayout;
    private MusicSingleAdapter mAdapter;
    private PlayerPresenter mPresenter;
    private PlaylistDialog mDialog;

    public static void startMusicList(Context context,String title, ArrayList<MusicBean> list) {

        Intent intent = new Intent(context, MusicListActivity.class);
        intent.putParcelableArrayListExtra(EXTRA_MUSIC_LIST, list);
        intent.putExtra(EXTRA_TITLE, title);
        context.startActivity(intent);
    }

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

        ButterKnife.bind(this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(this);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.addItemDecoration(LineItemDecorator.getInstance());
        mAdapter = new MusicSingleAdapter(getLayoutInflater());
        mAdapter.setOnItemClickListener(this);
        rvList.setAdapter(mAdapter);

        Intent intent = getIntent();

        String singer = intent.getStringExtra(EXTRA_TITLE);
        singer = singer == null ? getString(R.string.unknown) : singer;
        setTitle(singer);
        toolbar.setTitle(singer);

        if (null != intent) {
            ArrayList<MusicBean> list = intent.getParcelableArrayListExtra(EXTRA_MUSIC_LIST);
            mAdapter.setDatas(list);
        }

        mPresenter = new PlayerPresenter(this);
    }

    @Override
    public void onItemClick(View view, int position) {
        if (position>=0) {
            MusicPlayerService.startActionSet(this,mAdapter.getDataList(),position>0?position-1:0);
            MusicPlayerService.startActionPlay(this,true);
        }
    }

    @OnClick(R.id.bottomLayout)
    public void onBottomLayoutClick(){
        startActivity(new Intent(this,PlayerActivity.class));
    }


    @OnClick(R.id.iv_list)
    public void onPlaylistClick(){
        mDialog = new PlaylistDialog(this);
        mDialog.setPlaylist(PlayerManager.getInstance(this).getPlaylist());
        mDialog.setCurrentPlayPosition(PlayerManager.getInstance(this).getCurrentPosition());
        mDialog.show();
    }

    @OnClick(R.id.iv_play)
    public void onPlayClick(){
        if (PlayerManager.getInstance(this).isPause()) {
            mPresenter.startPlay(true);
        } else {
            mPresenter.Pause();
        }
    }

    @OnClick(R.id.iv_next)
    public void onNextClick(){
        mPresenter.nextMusic();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (null != mPresenter) {
            mPresenter.requestMusicInfo();
            mPresenter.syncPlayerInfo();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        overridePendingTransition(0,0);
    }

    @Override
    protected void onPause() {
        super.onPause();
        overridePendingTransition(0,0);
    }

    @Override
    public void onClick(View view) {
        finish();
    }

    @Override
    public void displayMusicInfo(MusicBean bean) {
        if (null!=mDialog && mDialog.isShowing()) {
            mDialog.setCurrentPlayPosition(PlayerManager.getInstance(this).getCurrentPosition());
        }
        if (null != bean) {
            ImageCacheManager2.getInstance(this).displayImage(ivAlbum,bean.getPath());
            mBottomLayout.setVisibility(View.VISIBLE);
            tvSongName.setText(bean.getName());
            String singer = bean.getSinger();
            singer = null == singer ? getString(R.string.unknown) : singer;
            tvSinger.setText(singer);
            progressBar.setMax((int) bean.getDuration());
        } else {
            mBottomLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void updateTrackInfo(int position) {
        progressBar.setProgress(position);
    }

    @Override
    public void updatePlayButton(boolean isPlay) {
        int resID = isPlay? R.drawable.ic_pause_primary : R.drawable.ic_play_primary;
        ivPlay.setImageDrawable(VectorDrawableCompat.create(getResources(), resID, null));
    }
} public abstract class BaseAdapter<T extends BaseHolder> extends RecyclerView.Adapter<BaseHolder> {

    public static final int TYPE_HEADER = Integer.MAX_VALUE;

    public static final int TYPE_FOOTER = Integer.MIN_VALUE;

    public final LayoutInflater mInflater;

    private OnItemCLickListener mListener;

    private View mHeaderView;

    private View mFooterView;


    public BaseAdapter(@NonNull LayoutInflater inflater) {
        mInflater = inflater;
    }

    public abstract BaseHolder onCreateHolder(ViewGroup parent, int viewType);

    public abstract void onBindHolder(T holder, int position);

    public abstract int getDataCount();

    public void setDatas(List<MusicBean> list){}

    public int getItemType(int position){
        return super.getItemViewType(position);
    }

    public MusicBean getDataAtPosition(int position) {
        return null;
    }

    public ArrayList<MusicBean> getDataList() {
        return null;
    }

    public void setOnItemClickListener(OnItemCLickListener listener) {
        mListener = listener;
    }

    public void addHeaderView(View view) {
        mHeaderView = view;
    }

    public void addFooterView(View view) {
        mFooterView = view;
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView != null && position == 0) {
            //headerView
            return TYPE_HEADER;
        } else if (mFooterView != null && position == getItemCount() - 1) {
            //footerView
            return TYPE_FOOTER;
        } else {
            //normalView
            if (mFooterView != null) {
                position--;
            }
            if (mHeaderView != null) {
                position--;
            }
            return getItemType(position);
        }
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEADER:
                return new BaseHolder(mHeaderView);
            case TYPE_FOOTER:
                return new BaseHolder(mFooterView);
            default:
                return onCreateHolder(parent, viewType);
        }
    }

    @Override
    public void onBindViewHolder(BaseHolder holder, int position) {
        if (null != mListener) {
            holder.setOnItemClickListener(mListener);
        }

        if (mHeaderView != null && position == 0) {
            //bind headerView
        } else if (mFooterView != null && position == getItemCount() - 1) {
            //bind footerView
        } else {
            //bind normalView
            if (mFooterView != null) {
                position--;
            }
            if (mHeaderView != null) {
                position--;
            }
            onBindHolder((T) holder, position);
        }
    }

    @Override
    public int getItemCount() {
        return getDataCount() + (null == mHeaderView ? 0 : 1) + (null == mFooterView ? 0 : 1);
    }
} public class MusicBean implements Parcelable{
    private final String mSinger;
    private final String mAlbum;
    private String mPath;
    private String mName;
    private long mDuration;

    protected MusicBean(Parcel in) {
        mSinger = in.readString();
        mAlbum = in.readString();
        mPath = in.readString();
        mName = in.readString();
        mDuration = in.readLong();
    }

    public static final Creator<MusicBean> CREATOR = new Creator<MusicBean>() {
        @Override
        public MusicBean createFromParcel(Parcel in) {
            return new MusicBean(in);
        }

        @Override
        public MusicBean[] newArray(int size) {
            return new MusicBean[size];
        }
    };

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

    @Override
    public void writeToParcel(Parcel parcel, int i) {

        parcel.writeString(mSinger);
        parcel.writeString(mAlbum);
        parcel.writeString(mPath);
        parcel.writeString(mName);
        parcel.writeLong(mDuration);
    }

    @Override
    public int hashCode() {
        return mPath==null?0:mPath.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof MusicBean && (((MusicBean) obj).hashCode()==hashCode());
    }

    public MusicBean(String name, String singer, String album, long duration, String path) {
        mPath = path;
        mName = name;
        mSinger = singer;
        mAlbum = album;
        mDuration = duration;
    }

    public String getSinger() {
        return mSinger;
    }

    public String getAlbum() {
        return mAlbum;
    }

    public String getPath() {
        return mPath;
    }

    public void setPath(String mPath) {
        this.mPath = mPath;
    }

    public String getName() {
        return mName;
    }

    public void setName(String mName) {
        this.mName = mName;
    }

    public long getDuration() {
        return mDuration;
    }

    public void setDuration(long mDuration) {
        this.mDuration = mDuration;
    }
} public class MusicAlbumAdapter extends BaseAdapter<AlbumHolder> {

    private List<MusicBean> mList;

    public MusicAlbumAdapter(LayoutInflater layoutInflater) {
        super(layoutInflater);
    }

    @Override
    public void onBindHolder(AlbumHolder holder, final int position) {

        final MusicBean bean = mList.get(position);

        final Context context = holder.tvSinger.getContext();

        String unknown = "未知";
        String subTitle = "首 ";

        if (null != context) {
            ImageCacheManager2.getInstance(context).displayImage(holder.ivAlbum, bean.getPath());
            unknown = context.getString(R.string.unknown);
            subTitle = context.getString(R.string.songs_count, String.valueOf(bean.getDuration())) + (bean.getSinger() == null ? unknown : bean.getSinger());

            holder.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override
                public void onMenuItemClick(int menuPosition) {
                    final DiskMusicDao diskMusicDao = new DiskMusicDao(context);
                    final ArrayList<MusicBean> list = (ArrayList<MusicBean>) diskMusicDao.queryAlbumMusicBeanList(bean.getAlbum());
                    if (menuPosition == 0) {
                        MusicPlayerService.startActionSet(context, list, 0);
                        MusicPlayerService.startActionPlay(context, true);
                    } else {
                        AlertUtil.showDeleteDialog(context, new OnDeleteListener() {
                            @Override
                            public void onDelete(boolean isDeleteOnDisk) {
                                diskMusicDao.deleteAlbumMusicList(bean.getAlbum());
                                if (isDeleteOnDisk) {
                                    FileUtil.deleteFileOnDisk(list);
                                }
                                mList.remove(position);
                                notifyItemRemoved(position);
                            }
                        });
                    }
                }
            });
        } else {
            subTitle = bean.getDuration() + subTitle + bean.getSinger();
        }


        String title = bean.getAlbum() == null ? unknown : bean.getAlbum();


        holder.setTitle(title);
        holder.tvSinger.setText(title);
        holder.tvCount.setText(subTitle);
    }

    @Override
    public void setDatas(List<MusicBean> list) {
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public MusicBean getDataAtPosition(int position) {
        return mList.get(position);
    }

    @Override
    public AlbumHolder onCreateHolder(ViewGroup parent, int viewType) {
        AlbumHolder holder = new AlbumHolder(mInflater.inflate(R.layout.album_list_item, parent, false));
        holder.setMenuList(GroupSongMenuModel.getInstance(parent.getContext()).getMenuList());
        return holder;
    }

    @Override
    public int getDataCount() {
        return mList != null ? mList.size() : 0;
    }
} public class MusicFolderAdapter extends BaseAdapter<FolderHolder> {

    private List<MusicBean> mList;

    public MusicFolderAdapter(LayoutInflater layoutInflater) {
        super(layoutInflater);
    }

    @Override
    public void onBindHolder(FolderHolder holder, final int position) {

        final MusicBean bean = mList.get(position);

        final Context context = holder.tvFolderName.getContext();

        String unknown = "未知";
        String count = "首 ";
        if (null != context) {
            unknown = context.getString(R.string.unknown);
            count = context.getString(R.string.songs_count, String.valueOf(bean.getDuration()));

            holder.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override
                public void onMenuItemClick(int menuPosition) {
                    final DiskMusicDao diskMusicDao = new DiskMusicDao(context);
                    final ArrayList<MusicBean> list = (ArrayList<MusicBean>) diskMusicDao.queryFolderMusicBeanList(bean.getSinger());
                    if (menuPosition == 0) {
                        MusicPlayerService.startActionSet(context, list, 0);
                        MusicPlayerService.startActionPlay(context, true);
                    } else {
                        AlertUtil.showDeleteDialog(context, new OnDeleteListener() {
                            @Override
                            public void onDelete(boolean isDeleteOnDisk) {
                                diskMusicDao.deleteFolderMusicList(bean.getSinger());
                                if (isDeleteOnDisk) {
                                    FileUtil.deleteFileOnDisk(list);
                                }
                                mList.remove(position);
                                notifyItemRemoved(position);
                            }
                        });
                    }
                }
            });
        } else {
            count = bean.getDuration() + count+bean.getSinger();
        }

        String path = bean.getAlbum() == null ? unknown : bean.getAlbum();
        String title = bean.getName() == null ? unknown : bean.getName();

        holder.setTitle(title);
        holder.tvFolderName.setText(title);
        holder.tvCount.setText(count);
        holder.tvFolderPath.setText(path);
    }


    @Override
    public void setDatas(List list) {
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public MusicBean getDataAtPosition(int position) {
        return mList.get(position);
    }

    @Override
    public FolderHolder onCreateHolder(ViewGroup parent, int viewType) {
        FolderHolder holder = new FolderHolder(mInflater.inflate(R.layout.folder_list_item, parent, false));
        holder.setMenuList(GroupSongMenuModel.getInstance(parent.getContext()).getMenuList());
        return holder;
    }

    @Override
    public int getDataCount() {
        return mList!=null?mList.size():0;
    }
} public class MusicSingerAdapter extends BaseAdapter<SingerHolder> {

    private List<MusicBean> mList;

    public MusicSingerAdapter(LayoutInflater layoutInflater) {
        super(layoutInflater);
    }

    @Override
    public void onBindHolder(SingerHolder holder, final int position) {
        final MusicBean bean = mList.get(position);
        final Context context = holder.tvTitle.getContext();
        ImageCacheManager2.getInstance(context).displayImage(holder.iv,bean.getPath());
        String singer = bean.getSinger() == null ? context.getString(R.string.unknown) : bean.getSinger();
        holder.setTitle(singer);
        holder.tvTitle.setText(singer);
        holder.tvContent.setText(context.getString(R.string.songs_count, String.valueOf(bean.getDuration())));

        if (null != context) {
            holder.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override
                public void onMenuItemClick(int menuPosition) {
                    final DiskMusicDao diskMusicDao = new DiskMusicDao(context);
                    final ArrayList<MusicBean> list = (ArrayList<MusicBean>) diskMusicDao.querySingerMusicBeanList(bean.getSinger());
                    if (menuPosition == 0) {
                        MusicPlayerService.startActionSet(context, list, 0);
                        MusicPlayerService.startActionPlay(context, true);
                    } else {
                        AlertUtil.showDeleteDialog(context, new OnDeleteListener() {
                            @Override
                            public void onDelete(boolean isDeleteOnDisk) {
                                diskMusicDao.deleteSingerMusicList(bean.getSinger());
                                if (isDeleteOnDisk) {
                                    FileUtil.deleteFileOnDisk(list);
                                }
                                mList.remove(position);
                                notifyItemRemoved(position);
                            }
                        });
                    }
                }
            });
        }

    }

    @Override
    public SingerHolder onCreateHolder(ViewGroup parent, int viewType) {
        SingerHolder holder = new SingerHolder(mInflater.inflate(R.layout.singer_list_item, parent, false));
        holder.setMenuList(GroupSongMenuModel.getInstance(parent.getContext()).getMenuList());
        return holder;
    }


    @Override
    public void setDatas(List list) {
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public MusicBean getDataAtPosition(int position) {
        return mList.get(position);
    }

    @Override
    public int getDataCount() {
        return mList!=null?mList.size():0;
    }
} public class MusicSingleAdapter extends BaseAdapter {
    private final int TYPE_HEAD = 1;
    private final int TYPE_CONTENT = 0;
    private final PlayerManager mPlayer;
    private ArrayList<MusicBean> mList;
    private String title;

    public MusicSingleAdapter(@NonNull LayoutInflater inflater) {
        super(inflater);
        mPlayer = PlayerManager.getInstance(inflater.getContext());
    }

    @Override
    public int getItemType(int position) {
        return position==0?TYPE_HEAD:TYPE_CONTENT;
    }

    @Override
    public BaseHolder onCreateHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_CONTENT) {
            SongListItemHolder holder = new SongListItemHolder(mInflater.inflate(R.layout.song_list_item, parent, false));
            holder.setMenuList(SingleSongMenuModel.getInstance(parent.getContext()).getMenuList());
            return holder;
        } else {
            return new PlayAllHolder(mInflater.inflate(R.layout.play_all_layout, parent, false));
        }
    }

    @Override
    public void onBindHolder(final BaseHolder holder, final int position) {

        if (position != 0) {
            final MusicBean bean = mList.get(position - 1);

            final SongListItemHolder itemHolder = (SongListItemHolder) holder;
            final Context context = itemHolder.tvTitle.getContext();
            String unknown = context.getString(R.string.unknown);
            title = bean.getName() == null ? unknown : bean.getName();
            itemHolder.setTitle(title);
            itemHolder.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override
                public void onMenuItemClick(int menuPosition) {
                    switch (menuPosition) {
                        case 0:
                            List<MusicBean> playlist = mPlayer.getPlaylist();
                            if (playlist != null && playlist.size()>0) {
                                int index = playlist.indexOf(bean);
                                if (index >= 0) {
                                    playlist.remove(index);
                                }
                                int currentPosition = mPlayer.getCurrentPosition();
                                currentPosition = (currentPosition >= playlist.size() - 1 ? playlist.size() : currentPosition+1);
                                playlist.add(currentPosition, bean);
                            } else {
                                ArrayList<MusicBean> beanList = new ArrayList<>();
                                beanList.add(bean);
                                MusicPlayerService.startActionSet(context, beanList, 0);
//                                mPlayer.setCurrentPlaylist(beanList);
//                                mPlayer.setCurrentPosition(0);
//                                mPlayer.playMusic(itemHolder.tvTitle.getContext());
                                MusicPlayerService.startActionPlay(context,true);
                            }
                            break;
                        case 1:
                            //收藏

                            AlertUtil.showCollectionDialog(context,bean);
                            break;
                        case 2:
                            //分享
                            Intent intent = new Intent(Intent.ACTION_SEND);
                            intent.setType("audio/x-mpeg");
                            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(bean.getPath())));
                            context.startActivity(Intent.createChooser(intent,context.getString(R.string.share_to)));
                            break;
                        case 3:
//                            删除
                            AlertUtil.showDeleteDialog(context, new OnDeleteListener() {
                                @Override
                                public void onDelete(boolean isDeleteOnDisk) {
                                    new DiskMusicDao(context).deleteSingleSong(bean.getPath());
                                    if (isDeleteOnDisk) {
                                        new File(bean.getPath()).delete();
                                    }
                                    mList.remove(position);
                                    notifyItemRemoved(position);
                                }
                            });

                            break;
                    }
                }
            });
            itemHolder.tvTitle.setText(title);
            String singer = bean.getSinger() == null ? unknown : bean.getSinger();
            String album = bean.getAlbum() == null ? unknown : bean.getAlbum();
            itemHolder.tvContent.setText(singer + " - " + album);


        } else {
            PlayAllHolder itemHolder = (PlayAllHolder) holder;
            Context context = itemHolder.tvCount.getContext();
            itemHolder.tvCount.setText(context.getString(R.string.songs_in_total, String.valueOf(mList.size())));
        }
    }

    @Override
    public void setDatas(List list) {
        this.mList = (ArrayList<MusicBean>) list;
        notifyDataSetChanged();
    }

    public MusicBean getDataAtPosition(int position) {
        return mList.get(position);
    }

    @Override
    public ArrayList<MusicBean> getDataList() {
        return mList;
    }

    @Override
    public int getDataCount() {
        return mList!=null && mList.size()>0?mList.size()+1:0;
    }

} public class SubMusicPresenter {

    private final ISubMusicView mIView;
    private final DiskMusicDao musicDao;
    private int mMusicType;

    public SubMusicPresenter(ISubMusicView view) {
        mIView = view;
        musicDao = new DiskMusicDao(view.getContext());
    }

    public void requestList(int mMusicType) {
        this.mMusicType = mMusicType;
        switch (mMusicType) {
            case CONST.TYPE_SINGLE:
                mIView.showItems(musicDao.queryAll());
                break;
            case CONST.TYPE_SINGER:
                mIView.showItems(musicDao.querySingerList());
                break;
            case CONST.TYPE_ALBUM:
                mIView.showItems(musicDao.queryAlbumList());
                break;
            case CONST.TYPE_FOLDER:
                mIView.showItems(musicDao.queryPathList());
                break;
        }

    }


    public List<MusicBean> getMusicBeanList(String key) {
        switch (mMusicType) {
            case CONST.TYPE_SINGER:
                return musicDao.querySingerMusicBeanList(key);
            case CONST.TYPE_ALBUM:
                return musicDao.queryAlbumMusicBeanList(key);
            case CONST.TYPE_FOLDER:
                return musicDao.queryFolderMusicBeanList(key);
        }
        return null;
    }

    public int findPosition(MusicBean bean) {
        return new PlaylistModel(mIView.getContext()).findPositionInPlaylist(CONST.TEMP_PLAYLIST_ID, bean.getPath());
    }

} public interface ISubMusicView{
    void showItems(List<MusicBean> list);

    Context getContext();
} public static final String MUSIC_TYPE = "MUSIC_TYPE"; public static final int TYPE_ALBUM = 2; public static final int TYPE_FOLDER = 3; public static final int TYPE_SINGER = 1; public static final int TYPE_SINGLE = 0;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.zsorg.neteasecloudmusic.LineItemDecorator;
import com.zsorg.neteasecloudmusic.MusicPlayerService;
import com.zsorg.neteasecloudmusic.callbacks.OnItemCLickListener;
import com.zsorg.neteasecloudmusic.R;
import com.zsorg.neteasecloudmusic.activities.MusicListActivity;
import com.zsorg.neteasecloudmusic.adapters.BaseAdapter;
import com.zsorg.neteasecloudmusic.models.beans.MusicBean;
import com.zsorg.neteasecloudmusic.adapters.MusicAlbumAdapter;
import com.zsorg.neteasecloudmusic.adapters.MusicFolderAdapter;
import com.zsorg.neteasecloudmusic.adapters.MusicSingerAdapter;
import com.zsorg.neteasecloudmusic.adapters.MusicSingleAdapter;
import com.zsorg.neteasecloudmusic.presenters.SubMusicPresenter;
import com.zsorg.neteasecloudmusic.views.ISubMusicView;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import static com.zsorg.neteasecloudmusic.CONST.MUSIC_TYPE;
import static com.zsorg.neteasecloudmusic.CONST.TYPE_ALBUM;
import static com.zsorg.neteasecloudmusic.CONST.TYPE_FOLDER;
import static com.zsorg.neteasecloudmusic.CONST.TYPE_SINGER;
import static com.zsorg.neteasecloudmusic.CONST.TYPE_SINGLE;
package com.zsorg.neteasecloudmusic.fragments;







/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link SubMusicFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link SubMusicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SubMusicFragment extends Fragment implements ISubMusicView, OnItemCLickListener {

    private int mMusicType;


    private BaseAdapter mAdapter;

    @BindView(R.id.rv_sub_music)
    RecyclerView mRecyclerView;
    private SubMusicPresenter mPresenter;

    public SubMusicFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param musicType 加载单曲，歌手，专辑，或文件夹.
     * @return A new instance of fragment SubMusicFragment.
     */
    public static SubMusicFragment newInstance(int musicType) {
        SubMusicFragment fragment = new SubMusicFragment();
        Bundle args = new Bundle();
        args.putInt(MUSIC_TYPE, musicType);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mMusicType = getArguments().getInt(MUSIC_TYPE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (null!=mPresenter) {
            mPresenter.requestList(mMusicType);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_sub_music, container, false);
        ButterKnife.bind(this, view);

        mRecyclerView.addItemDecoration(LineItemDecorator.getInstance());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        mPresenter = new SubMusicPresenter(this);

        switch (mMusicType) {
            case TYPE_SINGLE:
                mAdapter = new MusicSingleAdapter(getActivity().getLayoutInflater());