<|start_filename|>app/src/main/java/com/lijunyan/blackmusic/view/PlayingPopWindow.java<|end_filename|>
package com.lijunyan.blackmusic.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lijunyan.blackmusic.R;
import com.lijunyan.blackmusic.database.DBManager;
import com.lijunyan.blackmusic.entity.MusicInfo;
import com.lijunyan.blackmusic.service.MusicPlayerService;
import com.lijunyan.blackmusic.util.Constant;
import com.lijunyan.blackmusic.util.MyMusicUtil;

import java.util.List;

/**
 * Created by lijunyan on 2017/4/5.
 */

public class PlayingPopWindow extends PopupWindow{
    
    private static final String TAG = PlayingPopWindow.class.getName();
    private View view;
    private Activity activity;
//    private ImageView playModeIv;
    private TextView countTv;
//    private TextView playModeTv;
    private RelativeLayout closeRv;
    private RecyclerView recyclerView;
    private Adapter adapter;
    private List<MusicInfo> musicInfoList;
    private DBManager dbManager;

    public PlayingPopWindow(Activity activity) {
        super(activity);
        this.activity = activity;
        dbManager = DBManager.getInstance(activity);
        musicInfoList = MyMusicUtil.getCurPlayList(activity);
        initView();
    }

    private void initView(){
        this.view = LayoutInflater.from(activity).inflate(R.layout.playbar_menu_window, null);
        this.setContentView(this.view);
        Point size = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(size);
        int height = (int)(size.y * 0.5);
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        this.setHeight(height);

        this.setFocusable(true);
        this.setOutsideTouchable(true);

        // 设置弹出窗体的背景
        this.setBackgroundDrawable(activity.getResources().getDrawable(R.color.colorWhite));
        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.style.pop_window_animation);

        // 添加OnTouchListener监听判断获取触屏位置，如果在选择框外面则销毁弹出框
        this.view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                int height = view.getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        recyclerView = (RecyclerView) view.findViewById(R.id.playing_list_rv);
        adapter = new Adapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);

//        playModeIv = (ImageView)view.findViewById(R.id.playing_list_playmode_iv);
//        playModeTv = (TextView) view.findViewById(R.id.playing_list_playmode_Tv);
        closeRv = (RelativeLayout) view.findViewById(R.id.playing_list_close_rv);
        countTv = (TextView)view.findViewById(R.id.playing_list_count_tv);
        countTv.setText("("+musicInfoList.size()+")");

//        initPlayMode();

//        playModeIv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                switchPlayMode();
//            }
//        });

        closeRv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }


//    private void initPlayMode() {
//        int playMode = MyMusicUtil.getIntShared(Constant.KEY_MODE);
//        if (playMode == -1) {
//            playMode = 0;
//        }
//        playModeIv.setImageLevel(playMode);
//        switch (playMode){
//            case Constant.PLAYMODE_SEQUENCE:
//                playModeTv.setText(Constant.PLAYMODE_SEQUENCE_TEXT);
//                break;
//            case Constant.PLAYMODE_RANDOM:
//                playModeTv.setText(Constant.PLAYMODE_RANDOM_TEXT);
//                break;
//            case Constant.PLAYMODE_SINGLE_REPEAT:
//                playModeTv.setText(Constant.PLAYMODE_SINGLE_REPEAT_TEXT);
//                break;
//        }
//    }

//    private void switchPlayMode() {
//        int playMode = MyMusicUtil.getIntShared(Constant.KEY_MODE);
//        switch (playMode){
//            case Constant.PLAYMODE_SEQUENCE:
//                playModeTv.setText(Constant.PLAYMODE_RANDOM_TEXT);
//                MyMusicUtil.setShared(Constant.KEY_MODE,Constant.PLAYMODE_RANDOM);
//                break;
//            case Constant.PLAYMODE_RANDOM:
//                playModeTv.setText(Constant.PLAYMODE_SINGLE_REPEAT_TEXT);
//                MyMusicUtil.setShared(Constant.KEY_MODE,Constant.PLAYMODE_SINGLE_REPEAT);
//                break;
//            case Constant.PLAYMODE_SINGLE_REPEAT:
//                playModeTv.setText(Constant.PLAYMODE_SEQUENCE_TEXT);
//                MyMusicUtil.setShared(Constant.KEY_MODE,Constant.PLAYMODE_SEQUENCE);
//                break;
//        }
//        initPlayMode();
//    }


    private class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

        class ViewHolder extends RecyclerView.ViewHolder{
            LinearLayout contentLl;
            TextView nameTv;
            TextView singerTv;

            public ViewHolder(View itemView) {
                super(itemView);
                this.contentLl = (LinearLayout) itemView.findViewById(R.id.palybar_list_item_ll);
                this.nameTv = (TextView) itemView.findViewById(R.id.palybar_list_item_name_tv);
                this.singerTv = (TextView) itemView.findViewById(R.id.palybar_list_item_singer_tv);
            }
        }

        @Override
        public int getItemCount() {
            return musicInfoList.size();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(activity).inflate(R.layout.item_playbar_rv_list,parent,false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder,final int position) {
            Log.d(TAG, "onBindViewHolder: position = "+position);
            final MusicInfo musicInfo = musicInfoList.get(position);
            holder.nameTv.setText(musicInfo.getName());
            holder.singerTv.setText("-"+musicInfo.getSinger());

            if (musicInfo.getId() == MyMusicUtil.getIntShared(Constant.KEY_ID)){
                holder.nameTv.setTextColor(activity.getResources().getColor(R.color.colorAccent));
                holder.singerTv.setTextColor(activity.getResources().getColor(R.color.colorAccent));
            }else {
                holder.nameTv.setTextColor(activity.getResources().getColor(R.color.grey700));
                holder.singerTv.setTextColor(activity.getResources().getColor(R.color.grey500));
            }

            holder.contentLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i(TAG, "onClick: 播放 "+musicInfo.getName());
                    String path = dbManager.getMusicPath(musicInfo.getId());
                    Intent intent = new Intent(MusicPlayerService.PLAYER_MANAGER_ACTION);
                    intent.putExtra(Constant.COMMAND, Constant.COMMAND_PLAY);
                    intent.putExtra(Constant.KEY_PATH, path);
                    activity.sendBroadcast(intent);
                    MyMusicUtil.setShared(Constant.KEY_ID,musicInfo.getId());
                    notifyDataSetChanged();
                }
            });

        }

    }



}


<|start_filename|>app/src/main/java/com/lijunyan/blackmusic/database/DatabaseHelper.java<|end_filename|>
package com.lijunyan.blackmusic.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

	private static final String TAG = DatabaseHelper.class.getName();
	//数据库名
	private static final String DATABASE_NAME = "MusicDatabase.db";

	//音乐表
	public static final String MUSIC_TABLE = "music_table";

	public static final String ID_COLUMN = "id";
	public static final String MUSIC_ID_COLUMN = "music_id";
	public static final String NAME_COLUMN = "name";
	public static final String SINGER_COLUMN = "singer";
	public static final String DURATION_COLUMN = "duration";
	public static final String ALBUM_COLUMN = "album";
	public static final String PATH_COLUMN = "path";
	public static final String PARENT_PATH_COLUMN = "parent_path";
	public static final String FIRST_LETTER_COLUMN = "first_letter";
	public static final String LOVE_COLUMN = "love";


	//最近播放表
	public static final String LAST_PLAY_TABLE = "last_play_table";

	//歌单表
	public static final String PLAY_LIST_TABLE = "play_list_table";
	//歌单歌曲表
	public static final String PLAY_LISY_MUSIC_TABLE = "play_list_music_table";


	//数据库版本号
	private static final int VERSION = 2;

	//音乐表建表语句
	private String createMusicTable = "create table if not exists " + MUSIC_TABLE+ "("
			+ ID_COLUMN +" integer PRIMARY KEY ,"
			+ NAME_COLUMN +" text,"
			+ SINGER_COLUMN +" text,"
			+ ALBUM_COLUMN + " text,"
			+ DURATION_COLUMN + " long,"
			+ PATH_COLUMN + " text,"
			+ PARENT_PATH_COLUMN + " text,"
			+ LOVE_COLUMN + " integer,"
			+ FIRST_LETTER_COLUMN + " text );";

	//创建播放历史表
	private String createLastPlayTable = "create table if not exists " + LAST_PLAY_TABLE +" ("
			+ ID_COLUMN +" integer,"
			+ "FOREIGN KEY(id) REFERENCES "+ MUSIC_TABLE + " (id) ON DELETE CASCADE);";


	//创建歌单表
	private String createPlaylistTable = "create table if not exists " + PLAY_LIST_TABLE + " ("
			+ ID_COLUMN +" integer PRIMARY KEY autoincrement,"
			+ NAME_COLUMN + " text);";

	//创建歌单歌曲表
	private String createListinfoTable = "create table if not exists " + PLAY_LISY_MUSIC_TABLE +" ("
			+ ID_COLUMN + " integer,"
			+ MUSIC_ID_COLUMN + " integer,"
			+ "FOREIGN KEY(id) REFERENCES " + PLAY_LIST_TABLE + "(id) ON DELETE CASCADE,"
			+ "FOREIGN KEY(music_id) REFERENCES "+ MUSIC_TABLE + " (id) ON DELETE CASCADE) ;";


	public DatabaseHelper(Context context) {
		// 数据库实际被创建是在getWritableDatabase()或getReadableDatabase()方法调用时
		super(context, DATABASE_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.d(TAG, "onCreate");
		db.execSQL(createMusicTable);			//创建音乐表
		db.execSQL(createLastPlayTable);		//创建播放历史表
		db.execSQL(createPlaylistTable);		//创建歌单表
		db.execSQL(createListinfoTable);		//创建歌单歌曲表

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.e(TAG, "onUpgrade: oldVersion ="+oldVersion );
		Log.e(TAG, "onUpgrade: newVersion ="+newVersion );
		if (oldVersion < VERSION){
			db.execSQL("drop table if exists "+MUSIC_TABLE);
			db.execSQL("drop table if exists "+LAST_PLAY_TABLE);
			db.execSQL("drop table if exists "+ PLAY_LIST_TABLE);
			db.execSQL("drop table if exists "+ PLAY_LISY_MUSIC_TABLE);
			onCreate(db);
		}
	}


}


<|start_filename|>app/src/main/java/com/lijunyan/blackmusic/util/Constant.java<|end_filename|>
package com.lijunyan.blackmusic.util;



public class Constant {

	//service名
	public static final String SERVICE_NAME = "com.example.vinyl.service.MediaPlayerService";//服务的名称为包名+类名
	//播放状态
	public static final String STATUS = "status";

	public static final int STATUS_STOP = 0; //停止状态
	public static final int STATUS_PLAY = 1; //播放状态
	public static final int STATUS_PAUSE = 2; //暂停状态
	public static final int STATUS_RUN = 3;  //   状态

	public static final String COMMAND = "cmd";

	public static final int COMMAND_INIT = 1; //初始化命令
	public static final int COMMAND_PLAY = 2; //播放命令
	public static final int COMMAND_PAUSE = 3; //暂停命令
	public static final int COMMAND_STOP = 4; //停止命令
	public static final int COMMAND_PROGRESS = 5; //改变进度命令
	public static final int COMMAND_RELEASE = 6; //退出程序时释放

	//播放模式
	public static final int PLAYMODE_SEQUENCE = -1;
	public static final int PLAYMODE_SINGLE_REPEAT = 1;
	public static final int PLAYMODE_RANDOM = 2;

	public static final String PLAYMODE_SEQUENCE_TEXT = "顺序播放";
	public static final String PLAYMODE_RANDOM_TEXT = "随机播放";
	public static final String PLAYMODE_SINGLE_REPEAT_TEXT = "单曲循环";


	//Activity label

	public static final String LABEL = "label";
	public static final String LABEL_MYLOVE = "我喜爱";
	public static final String LABEL_LAST = "最近播放";
	public static final String LABEL_LOCAL = "本地音乐";

	public static final int ACTIVITY_LOCAL = 20; //我喜爱
	public static final int ACTIVITY_RECENTPLAY = 21;//最近播放
	public static final int ACTIVITY_MYLOVE = 22; //我喜爱
	public static final int ACTIVITY_MYLIST = 24;//我的歌单

	//fragment_title
	public static final String FRAGMENT_MYLOVE = "我喜爱";
	public static final String FRAGMENT_DOWNLOAD = "下载管理";
	public static final String FRAGMENT_MYLIST = "我的歌单";
	public static final String FRAGMENT_RECENTPLAY = "最近播放";

	//AlertDialog
	public static final String DIALOG_TITLE = "创建歌单";
	public static final String DIALOG_OK = "确定";
	public static final String DIALOG_CANCEL = "取消";

	//handle常量
	public static final int SCAN_ERROR = 0;
	public static final int SCAN_COMPLETE = 1;
	public static final int SCAN_UPDATE = 2;
	public static final int SCAN_NO_MUSIC = 3;
//	public static final int LOAD_COMPLETE = 4;
//	public static final int LOAD_PREPARE = 5;
//	public static final int LOAD_ERROR = 6;
//	public static final int DOWNLOAD_UPDATE = 14;


	//SharedPreferences key 常量
	public static final String KEY_ID = "id";
	public static final String KEY_PATH = "path";
	public static final String KEY_MODE = "mode";
	public static final String KEY_LIST = "list";
	public static final String KEY_LIST_ID = "list_id";
	public static final String KEY_CURRENT = "current";
	public static final String KEY_DURATION = "duration";

	//SharedPreferences value 常量 匹配 KEY_LIST
	public static final int LIST_SINGLE = 101;	//单曲列表
//	public static final int LIST_SINGLE = 101;	//歌手列表
//	public static final int LIST_SINGLE = 101;	//专辑列表
//	public static final int LIST_SINGLE = 101;	//最近播放列表
//	public static final int LIST_SINGLE = 101;	//我喜爱列表


	//歌曲列表常量
	public static final int LIST_ALLMUSIC = -1;
	public static final int LIST_MYLOVE = 10000;
	public static final int LIST_LASTPLAY = 10001;
	public static final int LIST_DOWNLOAD = 10002;
	public static final int LIST_MYPLAY = 10003; //我的歌单列表
	public static final int LIST_PLAYLIST = 10004;	//歌单音乐列表

	public static final int LIST_SINGER = 10005;	//歌手
	public static final int LIST_ALBUM = 10006;	    //专辑
	public static final int LIST_FOLDER = 10007;	//文件夹


	//ReceiverForMain.action
	public static final String UPDATE_MAIN_ACTIVITY ="MainActivityToReceiver.action";
	//MediaPlayerManager.action
	public static final String MP_FILTER = "com.example.vinyl.start_mediaplayer";
	//WidgetUtil.action
	public static final String UPDATE_WIDGET = "android.intent.ACTION_WIDGET";
	//UpdateWidget.action
	public static final String WIDGET_STATUS = "android.appwidget.action.WIDGET_STATUS";
	public static final String WIDGET_SEEK = "android.appwidget.action.WIDGET_SEEK";
	//
	public static final String MUSIC_CONTROL = "kugoumusic.ACTION_CONTROL";
	public static final String UPDATE_STATUS = "kugoumusic.ACTION_STATUS";

	//widget播放控制
	public static final String WIDGET_PLAY="android.appwidget.WIDGET_PLAY";
	public static final String WIDGET_NEXT="android.appwidget.WIDGET_NEXT";
	public static final String WIDGET_PREVIOUS="android.appwidget.WIDGET_PREVIOUS";


	//主题
	public static final String THEME="theme";
}


<|start_filename|>app/src/main/java/com/lijunyan/blackmusic/activity/WelcomeActivity.java<|end_filename|>
package com.lijunyan.blackmusic.activity;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lijunyan.blackmusic.R;
import com.lijunyan.blackmusic.database.DBManager;
import com.lijunyan.blackmusic.util.HttpUtil;
import com.lijunyan.blackmusic.util.MyApplication;
import com.lijunyan.blackmusic.util.MyMusicUtil;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class WelcomeActivity extends BaseActivity {

    private static final String TAG = "WelcomeActivity";
    private DBManager dbManager;
    private SharedPreferences sharepreferences;
    private SharedPreferences.Editor editor;
    private static final int PERMISSON_REQUESTCODE = 1;
    private ImageView bingIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        bingIv = (ImageView)findViewById(R.id.welcome_bing_iv);
        loadBingPic();
        dbManager = DBManager.getInstance(getApplicationContext());
        sharepreferences=this.getSharedPreferences("check", MODE_PRIVATE);
        editor=sharepreferences.edit();
        initPermission();

    }


    private void loadBingPic(){
        HttpUtil.sendOkHttpRequest(HttpUtil.requestBingPic, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    final String bingPic = response.body().string();
                    MyMusicUtil.setBingShared(bingPic);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(MyApplication.getContext()).load(bingPic).into(bingIv);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }
        });
    }

    private void checkSkip() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                startMusicActivity();
            }
        };
        timer.schedule(task, 1000);
    }

    private void startMusicActivity() {
        Intent intent = new Intent();
        intent.setClass(this, HomeActivity.class);
        startActivity(intent);
        finish();
    }



    private void initPermission() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            checkSkip();
            return;
        }
        if (ContextCompat.checkSelfPermission(WelcomeActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(WelcomeActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    PERMISSON_REQUESTCODE);
        }else {
            checkSkip();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case PERMISSON_REQUESTCODE:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    checkSkip();
                } else {
                    Toast.makeText(this, "必须同意所有权限才能使用本程序", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
        }
    }

}
