diff --git a/app/build.gradle b/app/build.gradle index b4529d5..848b63f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -63,7 +63,7 @@ dependencies { //noinspection GradleCompatible implementation 'com.android.support:support-media-compat:28.0.0' - implementation 'androidx.palette:palette:1.0.0' + implementation 'androidx.palette:palette-ktx:1.0.0' implementation 'com.github.zrunker:ZTextView:v1.0.2' //debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.5' //内存泄漏检测工具 diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 66f3177..e6d9804 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,15 +26,17 @@ list,int model) { + public AlbumsRecyclerAdapter(Context context, List list, int model) { this.list = list; this.context = context; - this.model=model; + this.model = model; } - public void setFid(int fid){ - this.fid=fid; + + public void setFid(int fid) { + this.fid = fid; } @NonNull @@ -53,7 +50,8 @@ public class AlbumsRecyclerAdapter extends RecyclerView.Adapter{ + holder.collection.setOnClickListener(view -> { MusicContainer.getLibs().removeCollection(fid, new NetworkInterface() { @Override public void httpGetData(Object data, int state) { @@ -122,10 +124,18 @@ public class AlbumsRecyclerAdapter extends RecyclerView.Adapter browserHelper.play(data)); - if(MusicContainer.getInstance().getNowPlayData().getMd5().equals(data.getMd5())){ + holder.layout.setOnClickListener(view -> { + browserHelper.play(data); + if (MusicContainer.getInstance().getNowPlayData().getMd5().equals(data.getMd5())) { + holder.play_mask.setVisibility(View.VISIBLE); + } else { + holder.play_mask.setVisibility(View.GONE); + } + notifyDataSetChanged(); + }); + if (MusicContainer.getInstance().getNowPlayData().getMd5().equals(data.getMd5())) { holder.play_mask.setVisibility(View.VISIBLE); - }else{ + } else { holder.play_mask.setVisibility(View.GONE); } } @@ -143,6 +153,10 @@ public class AlbumsRecyclerAdapter extends RecyclerView.Adapter getMusicDatas() { + return list; + } + public static class AlbumHolder extends RecyclerView.ViewHolder { MarqueeTextView title, artist, message; LinearLayout buttons, layout; diff --git a/app/src/main/java/com/yutou/nas_music_player/Adapters/MusicLibsAdapter.java b/app/src/main/java/com/yutou/nas_music_player/Adapters/MusicLibsAdapter.java index 6898adb..5983894 100644 --- a/app/src/main/java/com/yutou/nas_music_player/Adapters/MusicLibsAdapter.java +++ b/app/src/main/java/com/yutou/nas_music_player/Adapters/MusicLibsAdapter.java @@ -1,64 +1,66 @@ package com.yutou.nas_music_player.Adapters; -import android.annotation.SuppressLint; import android.content.Context; import android.content.DialogInterface; +import android.content.Intent; import android.graphics.Bitmap; -import android.media.ThumbnailUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; -import com.alibaba.fastjson.JSONArray; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AlertDialog; +import androidx.recyclerview.widget.RecyclerView; + import com.alibaba.fastjson.JSONObject; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.kaopiz.kprogresshud.KProgressHUD; import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Interfaces.NetworkInterface; +import com.yutou.nas_music_player.MyApplication; import com.yutou.nas_music_player.R; +import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MusicContainer; import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.tools.AppTools; -import com.yutou.nas_music_player.tools.NetworkTool; import com.yutou.nas_music_player.tools.StringUtil; +import com.yutou.nas_music_player.views.AlbumsActivity; import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.Objects; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; -import androidx.recyclerview.widget.RecyclerView; import cc.ibooker.ztextviewlib.MarqueeTextView; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; -public class MusicLibsAdapter extends ArrayAdapter { +public class MusicLibsAdapter extends RecyclerView.Adapter { public static final int LIBS_MODEL_ALL = 0; public static final int LIBS_MODEL_Album = 1; public static final int LIBS_MODEL_Artist = 2; public static final int LIBS_MODEL_Tmp = 3; public static final int LIBS_MODEL_PLAY_LIST = 4; public static final int LIBS_MODEL_PLAY_Collection = 5; - KProgressHUD hud; - List list; - Context context; - public MusicLibsAdapter(Context context, int model) { - super(context, R.layout.layout_music_libs_item); - list = new ArrayList<>(); + public interface OnClickAction{ + void onClick(Intent intent,View view); + } + protected OnClickAction clickAction; + public void setOnClickAction(OnClickAction onClickAction) { + this.clickAction=onClickAction; + } + + + + List list = new ArrayList<>(); + Context context; + MediaBrowserHelper browserHelper; + + public MusicLibsAdapter(Context context) { this.context = context; - initData(model, true); } private int model = -1; @@ -67,140 +69,21 @@ public class MusicLibsAdapter extends ArrayAdapter { public void initData(int model, boolean init) { this.model = model; - list.clear(); - if (!init) - hud = AppTools.showLoading(context); + //list.clear(); System.out.println("填充序列 设置model " + model + " " + this.model + " &"); - Map> map; - switch (model) { - case LIBS_MODEL_Album: - new Thread(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } - String album = null; - MusicContainer.getLibs().onAlbumMusicData(album, new NetworkInterface() { - @Override - public void httpGetData(Object data, int state) { - list.clear(); - if (data != null && state == 0) - list.addAll((List) data); - notifyDataSetChanged(); - if (hud != null) - hud.dismiss(); - } - @Override - public void httpError(Exception e) { - if (hud != null) - hud.dismiss(); - } - }); - } - }).start(); - break; - case LIBS_MODEL_Artist: - - break; - case LIBS_MODEL_Tmp: - list.addAll(MusicContainer.getInstance().getTmpPlayList()); - System.out.println("临时播放列表:" + MusicContainer.getInstance().getTmpPlayList().size()); - if (hud != null) - hud.dismiss(); - break; - case LIBS_MODEL_PLAY_LIST: - list.addAll(MusicContainer.getInstance().getPlayList()); - break; - case LIBS_MODEL_PLAY_Collection: - new Thread(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } - MusicContainer.getLibs().onCollectionList(null, new NetworkInterface() { - - @Override - public void httpGetData(Object data, int state) { - list.clear(); - if (data != null && state == 0) - list.addAll((List) data); - notifyDataSetChanged(); - if (hud != null) - hud.dismiss(); - } - - @Override - public void httpError(Exception e) { - if (hud != null) - hud.dismiss(); - } - }); - } - }).start(); - list = new ArrayList<>(); - break; - case LIBS_MODEL_ALL: - default: - new Thread(new Runnable() { - @Override - public void run() { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } - MusicContainer.getLibs().onDirMusicData(musicPath, new NetworkInterface() { - @Override - public void httpGetData(Object data, int state) { - list.clear(); - if (data != null && state == 0) - list.addAll((List) data); - AppData.handler.post(new Runnable() { - @Override - public void run() { - notifyDataSetChanged(); - if (hud != null) - hud.dismiss(); - } - }); - } - - @Override - public void httpError(Exception e) { - if (hud != null) - hud.dismiss(); - } - }); - } - }).start(); - - } } + @Override + public int getItemViewType(int position) { + return model; + } + + public void initData(int model) { initData(model, false); } - @Override - public int getCount() { - super.getCount(); - int size = list.size(); - System.out.println("列表数量:" + size); - return list.size(); - } - - @Nullable - @Override - public MusicData getItem(int position) { - return list.get(position); - } private int scrollStatus = 0; @@ -209,199 +92,6 @@ public class MusicLibsAdapter extends ArrayAdapter { } - @SuppressLint("SetTextI18n") - @NonNull - @Override - public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { - ViewHolder holder; - if (convertView == null) { - convertView = LayoutInflater.from(getContext()).inflate(R.layout.layout_music_libs_item, null); - holder = new ViewHolder(convertView); - convertView.setTag(holder); - } else { - holder = (ViewHolder) convertView.getTag(); - } - MusicData data = list.get(position); - holder.title.setText(data.getTitle()); - holder.artist.setText(data.getArtist()); - holder.icon.setImageResource(R.drawable.ic_dir); - if (model == LIBS_MODEL_PLAY_Collection) { - holder.collection.setVisibility(View.GONE); - } else { - holder.collection.setVisibility(View.VISIBLE); - } - - if (!data.isDir()) { - if (model != LIBS_MODEL_Album && model != LIBS_MODEL_Artist) { - if (model == LIBS_MODEL_PLAY_Collection) { - holder.message.setText("共 " + data.getComment() + " 首歌"); - } else { - holder.message.setText(data.getBitRate() + "kbps | " + data.getSampleRate() + "hz | " + data.getEncodingType()); - } - holder.message.setVisibility(View.VISIBLE); - holder.artist.setVisibility(View.VISIBLE); - } else {//为专辑或艺术家 - holder.message.setVisibility(View.GONE); - holder.artist.setVisibility(View.GONE); - } - // holder.icon.setImageBitmap(ThumbnailUtils.extractThumbnail(data.getImg(), 64,64)); - showImage(data, holder); - holder.icon.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (data.getImg(-1, -1) != null) { - Glide.with(getContext()).load(data.getImg(-1, -1)) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3))) - .into(holder.icon); - } - } - }); - } else { - holder.message.setText(""); - holder.play_mask.setVisibility(View.GONE); - holder.icon.setImageResource(R.drawable.ic_dir); - } - if (MusicContainer.getInstance().getNowPlayData().getMd5().equals(data.getMd5())) { - holder.play_mask.setVisibility(View.VISIBLE); - } else { - holder.play_mask.setVisibility(View.GONE); - } - holder.collection.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (data.isDir()) { - if (model == LIBS_MODEL_Album) {//添加整个专辑 - MusicContainer.getLibs().onAlbumMusicData(data.getAlbum(), new NetworkInterface() { - @Override - public void httpGetData(Object md, int state) { - List list = (List) md; - MusicContainer.getLibs().onAddCollectionDir(context, list.toArray(new MusicData[0])); - } - - @Override - public void httpError(Exception e) { - - } - }); - } else {//添加整个文件夹 - JSONObject json = new JSONObject(); - json.put("path", data.getFile()); - json.put("type", true); - MusicContainer.getLibs().onDirMusicData(json, new NetworkInterface() { - @Override - public void httpGetData(Object data, int state) { - List list = (List) data; - MusicContainer.getLibs().onAddCollectionDir(context, list.toArray(new MusicData[0])); - } - - @Override - public void httpError(Exception e) { - - } - }); - } - } else {//单曲 - MusicContainer.getLibs().onAddCollectionDir(context, data); - } - } - }); - holder.top.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - System.out.println("model = " + model); - if (model == LIBS_MODEL_Album) { - addAlbumList(data.getTitle()); - } else if (data.isDir()) { - showAddTmpListDialog(data); - } else { - MusicContainer.getInstance().addTmpList(data); - Toast.makeText(getContext(), "已添加到队列中", Toast.LENGTH_LONG).show(); - for (MusicData musicData : MusicContainer.getInstance().getTmpPlayList()) { - System.out.println("临时队列:" + musicData.getTitle()); - } - } - } - - private void addAlbumList(String album) { - System.out.println("添加专辑:" + album); - MusicContainer.getLibs().onAlbumMusicData(album, new NetworkInterface() { - @Override - public void httpGetData(Object data, int state) { - List list = (List) data; - for (MusicData musicData : list) { - System.out.println(musicData.getTitle() + "| 专辑名 =" + musicData.getAlbum()); - MusicContainer.getInstance().addTmpList(musicData); - } - Toast.makeText(getContext(), "已添加到队列中", Toast.LENGTH_LONG).show(); - } - - @Override - public void httpError(Exception e) { - - } - }); - } - - private void showAddTmpListDialog(MusicData data) { - AlertDialog dialog = new AlertDialog.Builder(getContext()) - .setTitle("添加文件夹到队列") - .setMessage("将文件夹中所有歌曲添加到队列?") - .setPositiveButton("添加", new DialogInterface.OnClickListener() { - KProgressHUD hud; - - @Override - public void onClick(DialogInterface dialog, int which) { - hud = AppTools.showLoading(getContext()); - JSONObject json = new JSONObject(); - json.put("path", data.getFile()); - json.put("type", true); - MusicContainer.getLibs().onDirMusicData(json, new NetworkInterface() { - @Override - public void httpGetData(Object data, int state) { - List list = (List) data; - for (MusicData musicData : list) { - MusicContainer.getInstance().addTmpList(musicData); - hud.dismiss(); - } - - } - - @Override - public void httpError(Exception e) { - - } - }); - } - }) - .setNegativeButton("取消", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - } - }).create(); - dialog.show(); - } - }); - return convertView; - } - - private void showImage(MusicData musicData, ViewHolder holder) { - musicData.getImg(-1, -1, new NetworkInterface() { - @Override - public void httpGetData(Object data, int state) { - Glide.with(getContext()).load((Bitmap) data) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3))) - .into(holder.icon); - System.out.println("展示图片:" + state); - } - - @Override - public void httpError(Exception e) { - System.out.println("图片下载失败"); - } - }); - } - public void onDestroy() { } @@ -417,8 +107,97 @@ public class MusicLibsAdapter extends ArrayAdapter { return model; } + @NonNull + @Override + public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(context).inflate(R.layout.layout_music_libs_item, parent, false); + switch (viewType) { + case LIBS_MODEL_Album: + return new ViewHolderAlbum(view); + case LIBS_MODEL_PLAY_Collection: + return new ViewHolderCollection(view); + default: + return new ViewHolderAll(view); + } + } - public static class ViewHolder extends RecyclerView.ViewHolder { + @Override + public void onBindViewHolder(@NonNull ViewHolder holder, int position) { + holder.bind(list.get(position)); + System.out.println("list.size() = " + list.size()); + } + + @Override + public int getItemCount() { + return list.size(); + } + + public MusicData getItem(int position) { + return list.get(position); + } + + public void setBrowserHelper(MediaBrowserHelper browserHelper) { + this.browserHelper = browserHelper; + } + + public void setModel(int model) { + this.model=model; + } + + public class ViewHolderCollection extends ViewHolder { + + ViewHolderCollection(View itemView) { + super(itemView); + } + + @Override + public void bind(MusicData data) { + super.bind(data); + collection.setVisibility(View.GONE); + message.setText("共 " + data.getComment() + " 首歌"); + message.setVisibility(View.VISIBLE); + artist.setVisibility(View.VISIBLE); + collectionDir(data); + topDir(data); + itemView.setOnClickListener(v -> clickCollection(data)); + icon.setImageResource(R.drawable.ic_dir); + } + } + + public class ViewHolderAll extends ViewHolder { + + ViewHolderAll(View itemView) { + super(itemView); + } + + @Override + public void bind(MusicData data) { + super.bind(data); + collectionDir(data); + topDir(data); + itemView.setOnClickListener(v -> clickAllList(data)); + } + } + + public class ViewHolderAlbum extends ViewHolder { + + ViewHolderAlbum(View itemView) { + super(itemView); + } + + @Override + public void bind(MusicData data) { + super.bind(data); + message.setVisibility(View.GONE); + collectionAlbum(data); + topAlbum(data); + itemView.setOnClickListener(v -> clickAlbum(data)); + } + + + } + + public class ViewHolder extends RecyclerView.ViewHolder { MarqueeTextView title, artist, message; LinearLayout buttons; ImageButton collection, top, dislike; @@ -436,5 +215,252 @@ public class MusicLibsAdapter extends ArrayAdapter { icon = itemView.findViewById(R.id.album_image); play_mask = itemView.findViewById(R.id.play_mask); } + + protected void collectionDir(MusicData data) { + collection.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (data.isDir()) { + JSONObject json = new JSONObject(); + json.put("path", data.getFile()); + json.put("type", true); + MusicContainer.getLibs().onDirMusicData(json, new NetworkInterface() { + @Override + public void httpGetData(Object data1, int state) { + List list = (List) data1; + MusicContainer.getLibs().onAddCollectionDir(itemView.getContext(), list.toArray(new MusicData[0])); + } + + @Override + public void httpError(Exception e) { + + } + }); + } else { + MusicContainer.getLibs().onAddCollectionDir(itemView.getContext(), data); + } + + } + }); + } + + protected void collectionAlbum(MusicData data) { + collection.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (data.isDir()) { + MusicContainer.getLibs().onAlbumMusicData(data.getAlbum(), new NetworkInterface() { + @Override + public void httpGetData(Object md, int state) { + List list = (List) md; + MusicContainer.getLibs().onAddCollectionDir(itemView.getContext(), list.toArray(new MusicData[0])); + } + + @Override + public void httpError(Exception e) { + + } + }); + } else { + MusicContainer.getLibs().onAddCollectionDir(itemView.getContext(), data); + } + + } + }); + + } + + protected void topAlbum(MusicData data) { + top.setOnClickListener(v -> addAlbumList(data.getTitle())); + } + + public void topDir(MusicData data) { + top.setOnClickListener(v -> { + if (data.isDir()) { + showAddTmpListDialog(data); + } else { + MusicContainer.getInstance().addTmpList(data); + Toast.makeText(v.getContext(), "已添加到队列中", Toast.LENGTH_LONG).show(); + for (MusicData musicData : MusicContainer.getInstance().getTmpPlayList()) { + System.out.println("临时队列:" + musicData.getTitle()); + } + } + }); + } + + protected void clickCollection(MusicData data) { + if (data == null) + return; + if (StringUtil.isEmpty(data.getComment())) { + Toast.makeText(itemView.getContext(), "未收藏任何歌曲", Toast.LENGTH_LONG).show(); + return; + } + Intent intent = new Intent(itemView.getContext(), AlbumsActivity.class); + intent.putExtra("model", 2); + intent.putExtra("fid", data.getId()); + intent.putExtra("image",data.getImg(AppData.imageWidth, AppData.imageHigh)); + if(clickAction!=null){ + clickAction.onClick(intent,icon); + } + } + + protected void clickAlbum(MusicData data) { + if (data == null) + return; + Intent intent = new Intent(itemView.getContext(), AlbumsActivity.class); + intent.putExtra("model", 0); + intent.putExtra("album", data.getAlbum()); + intent.putExtra("image",data.getImg(AppData.imageWidth, AppData.imageHigh)); + intent.putExtra("color",AppTools.getImageColor(data.getImg(AppData.minImageWidth,AppData.minImageHigh))); + if(clickAction!=null){ + clickAction.onClick(intent,icon); + } + } + + + protected void clickAllList(MusicData data) { + if (data.isDir()) { + changeList(data.getFile()); + } else { + if (browserHelper != null) { + browserHelper.play(data); + notifyDataSetChanged(); + } else { + System.out.println("browserHelper为空"); + } + } + } + + protected void changeList(String path) { + AppTools.showHud(context); + MusicContainer.getLibs().onDirMusicData(path, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + List list = (List) data; + musicPath = path; + AppTools.hideHud(); + setData(list); + notifyDataSetChanged(); + } + + @Override + public void httpError(Exception e) { + AppTools.hideHud(); + } + }); + } + + + public void bind(MusicData data) { + title.setText(data.getTitle()); + artist.setText(data.getArtist()); + icon.setImageResource(R.drawable.ic_dir); + collection.setVisibility(View.VISIBLE); + if (!data.isDir()) { + message.setText(data.getBitRate() + "kbps | " + data.getSampleRate() + "hz | " + data.getEncodingType()); + /* message.setVisibility(View.GONE); + artist.setVisibility(View.GONE);*/ + // icon.setImageBitmap(ThumbnailUtils.extractThumbnail(data.getImg(), 64,64)); + showImage(data, this); + + icon.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (data.getImg(AppData.minImageWidth,AppData.minImageHigh) != null) { + Glide.with(MyApplication.application).load(data.getImg(AppData.minImageWidth,AppData.minImageHigh)) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3))) + .into(icon); + icon.setTag("img"); + } + } + }); + } else { + message.setText(""); + play_mask.setVisibility(View.GONE); + icon.setImageResource(R.drawable.ic_dir); + } + if (MusicContainer.getInstance().getNowPlayData().getMd5().equals(data.getMd5())) { + play_mask.setVisibility(View.VISIBLE); + } else { + play_mask.setVisibility(View.GONE); + } + } + + protected void addAlbumList(String album) { + System.out.println("添加专辑:" + album); + MusicContainer.getLibs().onAlbumMusicData(album, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + List list = (List) data; + for (MusicData musicData : list) { + System.out.println(musicData.getTitle() + "| 专辑名 =" + musicData.getAlbum()); + MusicContainer.getInstance().addTmpList(musicData); + } + Toast.makeText(itemView.getContext(), "已添加到队列中", Toast.LENGTH_LONG).show(); + } + + @Override + public void httpError(Exception e) { + + } + }); + } + + protected void showAddTmpListDialog(MusicData data) { + AlertDialog dialog = new AlertDialog.Builder(itemView.getContext()) + .setTitle("添加文件夹到队列") + .setMessage("将文件夹中所有歌曲添加到队列?") + .setPositiveButton("添加", new DialogInterface.OnClickListener() { + KProgressHUD hud; + + @Override + public void onClick(DialogInterface dialog, int which) { + hud = AppTools.showLoading(itemView.getContext()); + JSONObject json = new JSONObject(); + json.put("path", data.getFile()); + json.put("type", true); + MusicContainer.getLibs().onDirMusicData(json, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + List list = (List) data; + for (MusicData musicData : list) { + MusicContainer.getInstance().addTmpList(musicData); + hud.dismiss(); + } + + } + + @Override + public void httpError(Exception e) { + + } + }); + } + }) + .setNegativeButton("取消", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }).create(); + dialog.show(); + } + + + protected void showImage(MusicData musicData, ViewHolder holder) { + musicData.getImg(AppData.minImageWidth,AppData.minImageHigh, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + Glide.with(MyApplication.application).load((Bitmap) data) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3))) + .into(holder.icon); + } + + @Override + public void httpError(Exception e) { + System.out.println("图片下载失败"); + } + }); + } } } diff --git a/app/src/main/java/com/yutou/nas_music_player/Datas/MusicData.java b/app/src/main/java/com/yutou/nas_music_player/Datas/MusicData.java index e93203e..121f2a0 100644 --- a/app/src/main/java/com/yutou/nas_music_player/Datas/MusicData.java +++ b/app/src/main/java/com/yutou/nas_music_player/Datas/MusicData.java @@ -3,11 +3,10 @@ package com.yutou.nas_music_player.Datas; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.Color; import android.media.ThumbnailUtils; -import android.util.Base64; import com.alibaba.fastjson.JSONObject; +import com.yutou.nas_music_player.Interfaces.DefaultNetworkInterface; import com.yutou.nas_music_player.Interfaces.DownloadInterface; import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.MyApplication; @@ -20,7 +19,6 @@ import com.yutou.nas_music_player.tools.StringUtil; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.palette.graphics.Palette; import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.Ignore; @@ -56,8 +54,7 @@ public class MusicData { private String file; @ColumnInfo(name = "isDir") private boolean isDir = false; - @Ignore - private Bitmap img; + @Ignore private boolean isDownloadImg = false; @@ -83,9 +80,6 @@ public class MusicData { public MusicData() { } - public void setImg(Bitmap img) { - this.img = img; - } public void setDownloadImg(boolean downloadImg) { isDownloadImg = downloadImg; @@ -108,6 +102,9 @@ public class MusicData { } public String getTitle() { + if (StringUtil.isEmpty(title)) { + title = album; + } return title; } @@ -223,9 +220,9 @@ public class MusicData { public String getPlayUrl() { - String url= HttpManager.NetworkAPI.HOME + HttpManager.NetworkAPI.MUSIC_PLAY + String url = HttpManager.NetworkAPI.HOME + HttpManager.NetworkAPI.MUSIC_PLAY + "?random=false&token=" + HttpManager.NetworkAPI.HTTP_KEY + "&filePath=" + getMd5(); - System.out.println("播放地址:" +url); + System.out.println("播放地址:" + url); return url; } @@ -234,66 +231,55 @@ public class MusicData { return HttpManager.NetworkAPI.MUSIC_IMAGE + "?fileName=" + getFileBase64(); } - public Bitmap getImg() { - return img; - } public Bitmap getImg(int width, int height) { - return getImg(width, height, null); + return getImg(width, height, new DefaultNetworkInterface()); } - public Bitmap getImg(int width, int height, NetworkInterface networkInterface) { - if (img != null) { - if (width == -1 && height == -1) { - if (networkInterface != null) - networkInterface.httpGetData(img, 0); - return img; - } - if (networkInterface != null) - networkInterface.httpGetData(ThumbnailUtils.extractThumbnail(img, width, height), width + height); - return ThumbnailUtils.extractThumbnail(img, width, height); - } - if (isDownloadImg) { - return null; - } + public Bitmap getImg(int width, int height,@NonNull NetworkInterface networkInterface) { + Bitmap img; if (StringUtil.isEmpty(file) && StringUtil.isEmpty(title) && StringUtil.isEmpty(album)) { img = BitmapFactory.decodeResource(MyApplication.application.getResources(), R.mipmap.ic_launcher); + networkInterface.httpGetData(img, 0); return img; } String imageSaveName = (StringUtil.isEmpty(album) ? title : album) + ".png"; img = AppTools.getSaveBitmap(imageSaveName); if (img != null) { if (width == -1 && height == -1) { - if (networkInterface != null) - networkInterface.httpGetData(img, 0); + networkInterface.httpGetData(img, 0); return img; } - if (networkInterface != null) - networkInterface.httpGetData(ThumbnailUtils.extractThumbnail(img, width, height), width + height); + networkInterface.httpGetData(ThumbnailUtils.extractThumbnail(img, width, height), width + height); return ThumbnailUtils.extractThumbnail(img, width, height); } - img = null; + img = BitmapFactory.decodeResource(MyApplication.application.getResources(), R.mipmap.ic_launcher); + if (isDownloadImg) { + networkInterface.httpGetData(img, 0); + return img; + } isDownloadImg = true; JSONObject json = new JSONObject(); - if (StringUtil.isEmpty(getAlbum())) { + if (!StringUtil.isEmpty(getAlbum())) { + json.put("type", "album"); + } + if (!StringUtil.isEmpty(getMd5())) { json.put("fileName", getMd5()); } else { - json.put("fileName", getMd5()); - json.put("type", "album"); + json.put("fileName", getTitle()); } System.out.println("下载图片:" + HttpManager.NetworkAPI.MUSIC_IMAGE + "?" + NetworkTool.toGetSplice(json)); HttpManager.getInstance().downloadImage(HttpManager.NetworkAPI.MUSIC_IMAGE, json, imageSaveName, "post", new DownloadInterface() { @Override public void onDownloadOver(File oldJar) { + isDownloadImg = false; AppTools.saveBitmap(oldJar, imageSaveName); - img = BitmapFactory.decodeFile(oldJar.getAbsolutePath()); + Bitmap img = BitmapFactory.decodeFile(oldJar.getAbsolutePath()); AppData.handler.post(() -> { if (width == -1 && height == -1) { - if (networkInterface != null) - networkInterface.httpGetData(img, 0); + networkInterface.httpGetData(img, 0); } else { - if (networkInterface != null) - networkInterface.httpGetData(ThumbnailUtils.extractThumbnail(img, width, height), width + height); + networkInterface.httpGetData(ThumbnailUtils.extractThumbnail(img, width, height), width + height); } }); @@ -302,60 +288,17 @@ public class MusicData { @Override public void onError(Exception e) { super.onError(e); - img = BitmapFactory.decodeResource(MyApplication.application.getResources(), R.mipmap.ic_launcher); + isDownloadImg = false; + Bitmap img = BitmapFactory.decodeResource(MyApplication.application.getResources(), R.mipmap.ic_launcher); + AppData.handler.post(() -> networkInterface.httpGetData(img, 0)); } }); - /* HttpManager.getInstance().httpGet(HttpManager.NetworkAPI.MUSIC_IMAGE, json, new NetworkInterface() { - @Override - public void httpGetData(Object data, int state) { - JSONObject json = JSONObject.parseObject(data.toString()); - try { - if (json.getInteger("code") == 0) { - byte[] bytes = json.getBytes("data"); - img = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); - AppTools.saveBitmap(img, getFileBase64()); - if (networkInterface != null) { - AppData.handler.post(new Runnable() { - @Override - public void run() { - if (width == -1 && height == -1) { - networkInterface.httpGetData(img, 0); - } else - networkInterface.httpGetData(ThumbnailUtils.extractThumbnail(img, width, height), width + height); - } - }); - } - } - } catch (Exception e) { - e.printStackTrace(); - img = BitmapFactory.decodeResource(MyApplication.application.getResources(), R.mipmap.ic_launcher); - } - } - - @Override - public void httpError(Exception e) { - isDownloadImg = false; - } - });*/ if (width == -1 && height == -1) { return img; } return ThumbnailUtils.extractThumbnail(img, width, height); } - public void recycleImg() { - if (img != null) { - img.recycle(); - img = null; - } - } - - public int getImageColor() { - if (img == null) { - return -1; - } - return Palette.from(img).generate().getLightVibrantColor(Color.parseColor("#6DE1A1")); - } public int getBitRate() { return bitRate; @@ -457,7 +400,6 @@ public class MusicData { ", lastDir='" + lastDir + '\'' + ", file='" + file + '\'' + ", isDir=" + isDir + - ", img=" + img + ", bitRate=" + bitRate + ", sampleRate=" + sampleRate + ", noOfSamples=" + noOfSamples + @@ -473,7 +415,7 @@ public class MusicData { @NonNull @Override public MusicData clone() { - MusicData data=new MusicData(); + MusicData data = new MusicData(); data.setId(id); data.setArtist(artist); data.setAlbum(album); @@ -487,7 +429,6 @@ public class MusicData { data.setLastDir(lastDir); data.setFile(file); data.setDir(isDir); - data.setImg(img); data.setBitRate(bitRate); data.setSampleRate(sampleRate); data.setNoOfSamples(noOfSamples); diff --git a/app/src/main/java/com/yutou/nas_music_player/Interfaces/DefaultNetworkInterface.java b/app/src/main/java/com/yutou/nas_music_player/Interfaces/DefaultNetworkInterface.java new file mode 100644 index 0000000..324a976 --- /dev/null +++ b/app/src/main/java/com/yutou/nas_music_player/Interfaces/DefaultNetworkInterface.java @@ -0,0 +1,13 @@ +package com.yutou.nas_music_player.Interfaces; + +public class DefaultNetworkInterface implements NetworkInterface { + @Override + public void httpGetData(Object data, int state) { + + } + + @Override + public void httpError(Exception e) { + + } +} diff --git a/app/src/main/java/com/yutou/nas_music_player/MyApplication.java b/app/src/main/java/com/yutou/nas_music_player/MyApplication.java index 5dfc187..1923e72 100644 --- a/app/src/main/java/com/yutou/nas_music_player/MyApplication.java +++ b/app/src/main/java/com/yutou/nas_music_player/MyApplication.java @@ -1,14 +1,61 @@ package com.yutou.nas_music_player; +import android.app.Activity; import android.app.Application; -import android.content.Context; +import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.lang.ref.WeakReference; public class MyApplication extends Application { public static Application application; + private static WeakReference activityWeakReference=null; + + public static Activity getActivity() { + return activityWeakReference.get(); + } @Override public void onCreate() { super.onCreate(); MyApplication.application = this; + registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { + @Override + public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) { + activityWeakReference=new WeakReference<>(activity); + } + + @Override + public void onActivityStarted(@NonNull Activity activity) { + + } + + @Override + public void onActivityResumed(@NonNull Activity activity) { + + } + + @Override + public void onActivityPaused(@NonNull Activity activity) { + + } + + @Override + public void onActivityStopped(@NonNull Activity activity) { + + } + + @Override + public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) { + + } + + @Override + public void onActivityDestroyed(@NonNull Activity activity) { + + } + }); } } diff --git a/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java b/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java index dac76ac..57eac36 100644 --- a/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java +++ b/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java @@ -21,10 +21,9 @@ import com.yutou.nas_music_player.Datas.PreviousPlayerList; import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.MyApplication; import com.yutou.nas_music_player.network.HttpManager; +import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.tools.AppTools; -import com.yutou.nas_music_player.tools.CollectionTools; import com.yutou.nas_music_player.tools.ConfigTools; -import com.yutou.nas_music_player.tools.NetworkTool; import com.yutou.nas_music_player.tools.StringUtil; import java.io.IOException; @@ -34,6 +33,7 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.Timer; import java.util.TimerTask; @@ -42,11 +42,11 @@ public class MusicContainer { public static final int PLAY_MODEL_RANDOM = 0;//随机 public static final int PLAY_MODEL_ORDER = 1;//顺序 public static final int PLAY_MODEL_ONE_LOOP = 2;//单曲循环 - public static final int PLAY_MODEL_LOOP = 3;//列表循环 + public static final int PLAY_MODEL_LIST_RANDOM = 3;//列表随机 private static MusicContainer container; private MusicLibs libs; private MediaPlayer mediaPlayer; - private int playIndex = 0; + private int playIndex = -1; private MusicData nowPlayData = null; private MusicLibsInitInterface initInterface; private List completionListener; @@ -68,7 +68,7 @@ public class MusicContainer { collectionMapList = new LinkedHashMap<>(); previousPlayerList = new PreviousPlayerList(); browserHelpers = new ArrayList<>(); - playIndex = ConfigTools.getPreferences().getInt("playIndex", 0); + playIndex = ConfigTools.getPreferences().getInt("playIndex", -1); initMediaPlayer(); libs = new MusicLibs(); } @@ -220,6 +220,7 @@ public class MusicContainer { * 默认直接播放下一首 */ public void playOfAllRandom() { + playList.clear(); playOfAllRandom(true, null); } @@ -256,7 +257,6 @@ public class MusicContainer { }, 0, 1000); } MusicData tmp = data.clone(); - tmp.setImg(null); ConfigTools.getPreferences().edit().putString(ConfigTools.previous_music, JSONObject.toJSONString(tmp)).apply(); MediaNotificationManager manager = MediaNotificationManager.getInstance(); if (manager != null) { @@ -304,13 +304,13 @@ public class MusicContainer { } private MediaMetadataCompat metadataCompat; - private String metaDataFile=""; + private String metaDataFile = ""; public MediaMetadataCompat getMetadataCompat(MusicData data) { if (data.getMd5().equals(metaDataFile)) { return metadataCompat; } - metaDataFile=data.getMd5(); + metaDataFile = data.getMd5(); MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder(); builder.putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, data.getTitle()); builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, data.getArtist()); @@ -321,8 +321,8 @@ public class MusicContainer { builder.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, Long.parseLong(data.getTrack())); builder.putLong(MediaMetadataCompat.METADATA_KEY_DISC_NUMBER, Long.parseLong(data.getDisc_no())); builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, ((int) (data.getDurationAsDouble() * 1000))); - if (data.getImg(-1, -1) != null) { - builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, data.getImg(1024, 1024)); + if (data.getImg(AppData.minImageWidth, AppData.minImageHigh) != null) { + builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, data.getImg(AppData.minImageWidth, AppData.minImageHigh)); } builder.putString("md5", data.getMd5()); metadataCompat = builder.build(); @@ -352,7 +352,12 @@ public class MusicContainer { * 播放下一曲 */ public void playNext() { - System.out.println("播放模式:" + playModel); + System.out.println("播放模式:" + playModel + " index = " + playIndex + " play size = " + playList.size()); + if (tmpList.size() != 0) {//有插队列表,优先播放 + play(tmpList.get(0));//先插入的先播 + tmpList.remove(0); + return; + } switch (playModel) { case PLAY_MODEL_ORDER: playOrder(false); @@ -360,16 +365,12 @@ public class MusicContainer { case PLAY_MODEL_ONE_LOOP: playOneLoop(); break; - case PLAY_MODEL_LOOP: + case PLAY_MODEL_LIST_RANDOM: playOrder(true); break; case PLAY_MODEL_RANDOM: default: - if (tmpList.size() != 0) {//有插队列表,优先播放 - play(tmpList.get(0));//先插入的先播 - tmpList.remove(0); - } else - playOfAllRandom(); + playOfAllRandom(); } } @@ -389,35 +390,25 @@ public class MusicContainer { /** * 顺序播放 * - * @param isLoop 循环播放列表 + * @param isRandom 随机播放 */ - private void playOrder(boolean isLoop) { - if (tmpList.size() != 0) {//有插队列表,优先播放 - play(tmpList.get(0));//先插入的先播 - tmpList.remove(0); - } else if (playList.size() == 0) {//播放列表为空,以所有歌列表顺序播放 - if (libs.mainData.size() > (playIndex + 1)) { - playIndex++; - } else { - playIndex = 0; - } - if (libs.mainData.isEmpty()) { - playOfAllRandom(); - AppTools.toast("当前播放列表为空,随机播放"); - } else { - play(libs.mainData.get(playIndex)); - } + private void playOrder(boolean isRandom) { + if (playList.isEmpty()) {//播放列表为空,以所有歌列表顺序播放 + playOfAllRandom(); + AppTools.toast("当前播放列表为空,随机播放"); } else {//播放列表中的歌单 - if (playList.size() > (playIndex + 1)) { - playIndex++; - } - if (isLoop) { - playIndex = 0; - play(playList.get(playIndex)); + ConfigTools.getPreferences().edit().putInt("playModel", MusicContainer.PLAY_MODEL_ORDER).apply(); + if (isRandom) { + playIndex = new Random().nextInt(playList.size()); } else { - playList.clear();//列表播放完了,清空 - playOfAllRandom();//暂时先设计成后续随机播放,毕竟列表已经空了 + if (playList.size() > (playIndex + 1)) { + playIndex++; + } else { + playIndex = 0; + } } + play(playList.get(playIndex)); + } } @@ -438,31 +429,13 @@ public class MusicContainer { } public List getPlayList() { + System.out.println("playList.size() = " + playList.size() + " tmpList.size() = " + tmpList.size()); if (!tmpList.isEmpty()) { return tmpList; } - if (!playList.isEmpty()) { - return playList; - } - return libs.mainData; + return playList; } - private void flashCollection() { - collectionMapList.clear(); - JSONArray array = CollectionTools.readConfig(); - for (Object o : array) { - JSONObject json = (JSONObject) o; - JSONArray collections = json.getJSONArray("collections"); - List tmp = new ArrayList<>(); - for (Object collection : collections) { - MusicData data = libs.findMusic((String) collection, libs.mainData); - if (data != null) { - tmp.add(data); - } - } - collectionMapList.put(json.getString("title"), tmp); - } - } public void addTmpList(MusicData data) { if (data.getTitle().equals("返回") && data.getTrack().equals("-1") || data.isDir()) { @@ -491,11 +464,27 @@ public class MusicContainer { return collectionMapList; } + public void setPlayList(List dataList) { + this.playList = dataList; + this.playIndex = -1; + this.playModel = PLAY_MODEL_ORDER; + } + + public void addPlayList(List dataList) { + this.playList.addAll(dataList); + } + + public int getPlayModel() { + return playModel; + } + + public int getPlayIndex() { + return playIndex; + } + public class MusicLibs { - private List mainData; private MusicLibs() { - mainData = new ArrayList<>(); initData(); } @@ -672,9 +661,6 @@ public class MusicContainer { }); } - public List getMainData() { - return mainData; - } private void initData() { if (nowPlayData == null) { @@ -716,9 +702,11 @@ public class MusicContainer { } catch (Exception e) { List list = new ArrayList<>(); for (Object album : json.getJSONArray("data")) { + JSONObject _album = (JSONObject) album; MusicData musicData = new MusicData(); - musicData.setTitle((String) album); - musicData.setAlbum((String) album); + musicData.setTitle(_album.getString("title")); + musicData.setAlbum(_album.getString("album")); + musicData.setMd5(_album.getString("md5")); list.add(musicData); } new Handler(Looper.getMainLooper()) @@ -789,62 +777,6 @@ public class MusicContainer { }); } - public void updateLibsMap() { - - for (MusicData musicData : mainData) { - List artist = new ArrayList<>(); - List album = new ArrayList<>(); - String albumName = StringUtil.isEmpty(musicData.getAlbum()) ? - "未知专辑" - : musicData.getAlbum(); - String artistName = StringUtil.isEmpty(musicData.getArtist()) ? - "未知作曲家" - : musicData.getArtist(); - if (albumMapList.containsKey(albumName)) { - album.addAll(albumMapList.get(albumName)); - } - album.add(musicData); - if (artistMapList.containsKey(artistName)) { - artist.addAll(artistMapList.get(artistName)); - } - artist.add(musicData); - artistMapList.put(artistName, artist); - albumMapList.put(albumName, album); - - } - //flashCollection(); - if (initInterface != null) - initInterface.init(); - initInterface = null; - } - - public MusicData findMusic(String md5, List list) { - for (MusicData data : list) { - if (data.getMd5() == null) { - continue; - } - if (md5.contains(data.getMd5())) { - return data; - } - } - return null; - } - - public int findMusicIndex(String md5, List list) { - if (StringUtil.isEmpty(md5)) { - return -1; - } - for (int i = 0; i < list.size(); i++) { - if (list.get(i).getMd5() == null) { - continue; - } - if (md5.contains((list.get(i).getMd5()))) { - return i; - } - } - - return 0; - } } public interface MusicLibsInitInterface { diff --git a/app/src/main/java/com/yutou/nas_music_player/network/HttpManager.java b/app/src/main/java/com/yutou/nas_music_player/network/HttpManager.java index c8edae9..bd2a859 100644 --- a/app/src/main/java/com/yutou/nas_music_player/network/HttpManager.java +++ b/app/src/main/java/com/yutou/nas_music_player/network/HttpManager.java @@ -8,6 +8,7 @@ import androidx.annotation.NonNull; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; +import com.bumptech.glide.load.HttpException; import com.yutou.nas_music_player.Interfaces.DownloadInterface; import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.MyApplication; @@ -18,6 +19,7 @@ import com.yutou.nas_music_player.tools.NetworkTool; import com.yutou.nas_music_player.tools.StringUtil; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -251,6 +253,10 @@ public class HttpManager { private List downloadImageList = new ArrayList<>(); public synchronized void downloadImage(String url, JSONObject body, String saveName, String method, DownloadInterface downloadInterface) { + if(saveName.contains("/")){ + downloadImage(url, body, saveName.replace("/","_"), method, downloadInterface); + return; + } if (!StringUtil.isEmpty(NetworkAPI.HOME) && !url.startsWith("http")) { downloadImage(NetworkAPI.HOME + url, body, saveName, method, downloadInterface); return; @@ -289,8 +295,13 @@ public class HttpManager { } Log.i("donwloadImage", "下载文件:" + url + "?" + body + " 保存文件:" + saveName); Log.i("downloadImageCode", response.code() + ""); + if (response.code() != 200) { + downloadInterface.onError(new HttpException("http error = " + response.code())); + return; + } InputStream inputStream = response.body().byteStream(); + jar = new File(downloadPath + File.separator + "image_cache" + File.separator + saveName + "_tmp.tmp"); if (!new File(downloadPath + File.separator + "image_cache").exists()) { new File(downloadPath + File.separator + "image_cache").mkdirs(); @@ -307,6 +318,13 @@ public class HttpManager { } outputStream.close(); inputStream.close(); + if(jar.length()==0){ + System.out.println("jar.getAbsolutePath() = " + jar.getAbsolutePath()); + System.out.println("jar.length() = " + jar.length()); + downloadInterface.onError(new FileNotFoundException("文件大小为空")); + jar.delete(); + return; + } File oldJar = new File(downloadPath + saveName); /*if (oldJar.exists()) { oldJar.delete(); diff --git a/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java b/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java index e7cbfb0..c6382e8 100644 --- a/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java +++ b/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java @@ -18,10 +18,10 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.media.MediaBrowserServiceCompat; +import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MediaNotificationManager; import com.yutou.nas_music_player.containers.MusicContainer; -import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.views.MainActivity; import java.util.ArrayList; @@ -71,6 +71,20 @@ public class MusicService extends MediaBrowserServiceCompat { notificationManager.getManager() .notify(MediaNotificationManager.NOTIFICATION_ID, notification); } + + @Override + public void onPlaybackStateChanged(PlaybackStateCompat state) { + super.onPlaybackStateChanged(state); + if(mState==state.getState()){ + return; + } + mState=state.getState(); + MusicData data = MusicContainer.getInstance().getNowPlayData(); + MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getMetadataCompat(data); + Notification notification = notificationManager.getNotification(metadataCompat, builderState(state.getPosition()).build(), getSessionToken()); + notificationManager.getManager() + .notify(MediaNotificationManager.NOTIFICATION_ID, notification); + } }); } diff --git a/app/src/main/java/com/yutou/nas_music_player/tools/AppData.java b/app/src/main/java/com/yutou/nas_music_player/tools/AppData.java index 8fe0b43..83fece4 100644 --- a/app/src/main/java/com/yutou/nas_music_player/tools/AppData.java +++ b/app/src/main/java/com/yutou/nas_music_player/tools/AppData.java @@ -13,4 +13,8 @@ public class AppData { public static String dir= MyApplication.application.getFilesDir() + File.separator; public static boolean isDebug=true; public static Handler handler=new Handler(Looper.getMainLooper()); + public static int imageWidth=400; + public static int imageHigh=400; + public static int minImageWidth=200; + public static int minImageHigh=200; } diff --git a/app/src/main/java/com/yutou/nas_music_player/tools/AppTools.java b/app/src/main/java/com/yutou/nas_music_player/tools/AppTools.java index 68b14c4..fec4707 100644 --- a/app/src/main/java/com/yutou/nas_music_player/tools/AppTools.java +++ b/app/src/main/java/com/yutou/nas_music_player/tools/AppTools.java @@ -3,10 +3,13 @@ package com.yutou.nas_music_player.tools; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.graphics.Color; import android.graphics.Matrix; import android.util.Base64; import android.widget.Toast; +import androidx.palette.graphics.Palette; + import com.kaopiz.kprogresshud.KProgressHUD; import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache; import com.nostra13.universalimageloader.core.DisplayImageOptions; @@ -20,8 +23,22 @@ import java.io.File; import java.security.MessageDigest; public class AppTools { + private static KProgressHUD hud; - public static KProgressHUD showLoading(Context context,String title){ + public static synchronized void showHud(Context context) { + hideHud(); + hud = AppTools.showLoading(context); + hud.show(); + } + + public static void hideHud() { + if (hud != null && hud.isShowing()) { + hud.dismiss(); + hud = null; + } + } + + public static KProgressHUD showLoading(Context context, String title) { return KProgressHUD.create(context) .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE) .setDetailsLabel(title) @@ -31,30 +48,34 @@ public class AppTools { .show(); } - public static KProgressHUD showLoading(Context context){ - return showLoading(context,"loading..."); + public static KProgressHUD showLoading(Context context) { + return showLoading(context, "loading..."); } - public static void saveBitmap(final File img, final String saveKey){ - ConfigTools.getPreferences().edit().putString("img_"+saveKey,img.getAbsolutePath()).apply(); + + public static void saveBitmap(final File img, final String saveKey) { + ConfigTools.getPreferences().edit().putString("img_" + saveKey, img.getAbsolutePath()).apply(); } - public static Bitmap getSaveBitmap(String saveKey){ - String path=ConfigTools.getPreferences().getString("img_"+saveKey,null); - if(path==null){ + + public static Bitmap getSaveBitmap(String saveKey) { + String path = ConfigTools.getPreferences().getString("img_" + saveKey, null); + if (path == null) { return null; } - File img=new File(path); - if(img.length()>3145728){ - Bitmap bitmap=BitmapFactory.decodeFile(path); + File img = new File(path); + if (img.length() > 3145728) { + Bitmap bitmap = BitmapFactory.decodeFile(path); return compressQuality(bitmap); } return BitmapFactory.decodeFile(path); } + private static Bitmap compressQuality(Bitmap bm) { Matrix matrix = new Matrix(); matrix.setScale(0.5f, 0.5f); - return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); + return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); } + public static String getMD5(String data) { try { MessageDigest digest = MessageDigest.getInstance("MD5"); @@ -65,6 +86,7 @@ public class AppTools { } return null; } + private static String bytesToHexString(byte[] src) { StringBuilder stringBuilder = new StringBuilder(""); if (src == null || src.length <= 0) { @@ -80,6 +102,7 @@ public class AppTools { } return stringBuilder.toString(); } + private static DisplayImageOptions initDisplayOptions() { DisplayImageOptions.Builder displayImageOptionsBuilder = new DisplayImageOptions.Builder(); displayImageOptionsBuilder.cacheInMemory(false); @@ -103,15 +126,25 @@ public class AppTools { imageLoader.init(builder.build()); return imageLoader; } - public static String getUrlBase64(String text){ - return new String(Base64.encode(text.getBytes(), Base64.DEFAULT)).replace("\n", ""); + + public static String getUrlBase64(String text) { + return new String(Base64.encode(text.getBytes(), Base64.DEFAULT)).replace("\n", ""); } + public static void toast(String s) { AppData.handler.post(new Runnable() { @Override public void run() { - Toast.makeText(MyApplication.application,s,Toast.LENGTH_LONG).show(); + Toast.makeText(MyApplication.application, s, Toast.LENGTH_LONG).show(); } }); } + + public static int getImageColor(Bitmap img) { + try { + return Palette.from(img).generate().getLightVibrantColor(Color.parseColor("#bbbbbb")); + } catch (Exception e) { + return Color.parseColor("#bbbbbb"); + } + } } diff --git a/app/src/main/java/com/yutou/nas_music_player/tools/CollectionTools.java b/app/src/main/java/com/yutou/nas_music_player/tools/CollectionTools.java index dc14136..ab62f3d 100644 --- a/app/src/main/java/com/yutou/nas_music_player/tools/CollectionTools.java +++ b/app/src/main/java/com/yutou/nas_music_player/tools/CollectionTools.java @@ -62,10 +62,6 @@ public class CollectionTools { JSONObject json= (JSONObject) o; if(json.getString("title").equals(title)) { JSONArray co=json.getJSONArray("collections"); - for (Object md5 : co) { - MusicData data=MusicContainer.getLibs().findMusic((String)md5,MusicContainer.getLibs().getMainData()); - list.add(data); - } break; } } diff --git a/app/src/main/java/com/yutou/nas_music_player/views/AlbumsActivity.java b/app/src/main/java/com/yutou/nas_music_player/views/AlbumsActivity.java index a78bf64..747d6cd 100644 --- a/app/src/main/java/com/yutou/nas_music_player/views/AlbumsActivity.java +++ b/app/src/main/java/com/yutou/nas_music_player/views/AlbumsActivity.java @@ -1,8 +1,12 @@ package com.yutou.nas_music_player.views; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Color; import android.os.Bundle; import android.support.v4.media.session.MediaControllerCompat; +import android.view.Window; +import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; @@ -21,6 +25,7 @@ import com.yutou.nas_music_player.R; import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MusicContainer; import com.yutou.nas_music_player.tools.AppData; +import com.yutou.nas_music_player.tools.AppTools; import java.util.ArrayList; import java.util.List; @@ -34,10 +39,12 @@ public class AlbumsActivity extends AppCompatActivity { private AlbumsRecyclerAdapter adapter; private AppBarLayout appBarLayout; private TextView breakApp; + private Button playAll; private int model;//0 专辑, 1 歌手, 2 收藏 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { + getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); super.onCreate(savedInstanceState); setContentView(R.layout.activity_album); model = getIntent().getIntExtra("model", -1); @@ -52,9 +59,23 @@ public class AlbumsActivity extends AppCompatActivity { default: finish(); } + Bitmap image=getIntent().getParcelableExtra("image"); + if(image!=null){ + album_image.setImageBitmap(image); + Glide.with(AlbumsActivity.this).load(image) + .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) + .into(image_background); + }else{ + album_image.setImageResource(R.mipmap.ic_launcher); + Glide.with(AlbumsActivity.this).load(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) + .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) + .into(image_background); + } browserHelper = new MediaBrowserHelper(this); browserHelper.regPlayListener(new PlayListener()); adapter.setPlayListener(browserHelper); + listView.setBackgroundColor(getIntent().getIntExtra("color", Color.parseColor("#bbbbbb"))); + playAll.setBackgroundColor(getIntent().getIntExtra("color", Color.parseColor("#bbbbbb"))); } @@ -72,14 +93,13 @@ public class AlbumsActivity extends AppCompatActivity { adapter.notifyDataSetChanged(); if (list.isEmpty()) return; - list.get(0).getImg(-1, -1, new NetworkInterface() { + list.get(0).getImg(AppData.imageWidth,AppData.imageHigh, new NetworkInterface() { @Override public void httpGetData(Object data, int state) { album_image.setImageBitmap((Bitmap) data); Glide.with(AlbumsActivity.this).load(data) .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) .into(image_background); - listView.setBackgroundColor(list.get(0).getImageColor()); } @Override @@ -106,22 +126,6 @@ public class AlbumsActivity extends AppCompatActivity { adapter.notifyDataSetChanged(); }); - list.get(0).getImg(-1, -1, new NetworkInterface() { - @Override - public void httpGetData(Object data, int state) { - album_image.setImageBitmap((Bitmap) data); - Glide.with(AlbumsActivity.this).load(data) - .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) - .into(image_background); - listView.setBackgroundColor(list.get(0).getImageColor()); - } - - @Override - public void httpError(Exception e) { - - } - }); - } @Override @@ -138,6 +142,7 @@ public class AlbumsActivity extends AppCompatActivity { listView = findViewById(R.id.album_list); breakApp = findViewById(R.id.breakApp); appBarLayout = findViewById(R.id.appbar); + playAll=findViewById(R.id.playAll); listView.setLayoutManager(new LinearLayoutManager(this)); adapter = new AlbumsRecyclerAdapter(AlbumsActivity.this, new ArrayList<>(), model); listView.setAdapter(adapter); @@ -148,8 +153,11 @@ public class AlbumsActivity extends AppCompatActivity { breakApp.setTextColor(getResources().getColor(R.color.musicTitle, null)); } }); - appBarLayout.setOnClickListener(v -> finish()); - + breakApp.setOnClickListener(v -> finish()); + playAll.setOnClickListener(v -> { + MusicContainer.getInstance().setPlayList(new ArrayList<>(adapter.getMusicDatas())); + MusicContainer.getInstance().playNext(); + }); } @Override diff --git a/app/src/main/java/com/yutou/nas_music_player/views/Fragments/MusicLibsFragment.java b/app/src/main/java/com/yutou/nas_music_player/views/Fragments/MusicLibsFragment.java index 5bb4866..7e9b20c 100644 --- a/app/src/main/java/com/yutou/nas_music_player/views/Fragments/MusicLibsFragment.java +++ b/app/src/main/java/com/yutou/nas_music_player/views/Fragments/MusicLibsFragment.java @@ -1,21 +1,25 @@ package com.yutou.nas_music_player.views.Fragments; +import static com.yutou.nas_music_player.Adapters.MusicLibsAdapter.LIBS_MODEL_ALL; +import static com.yutou.nas_music_player.Adapters.MusicLibsAdapter.LIBS_MODEL_Album; +import static com.yutou.nas_music_player.Adapters.MusicLibsAdapter.LIBS_MODEL_Artist; +import static com.yutou.nas_music_player.Adapters.MusicLibsAdapter.LIBS_MODEL_PLAY_Collection; +import static com.yutou.nas_music_player.Adapters.MusicLibsAdapter.LIBS_MODEL_PLAY_LIST; +import static com.yutou.nas_music_player.Adapters.MusicLibsAdapter.LIBS_MODEL_Tmp; + import android.content.Context; -import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.AbsListView; -import android.widget.AdapterView; -import android.widget.ListView; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.ActivityOptionsCompat; import androidx.core.util.Pair; import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import com.yutou.nas_music_player.Adapters.MusicLibsAdapter; import com.yutou.nas_music_player.Datas.MusicData; @@ -23,15 +27,16 @@ import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.R; import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MusicContainer; -import com.yutou.nas_music_player.tools.StringUtil; -import com.yutou.nas_music_player.views.AlbumsActivity; +import com.yutou.nas_music_player.tools.AppData; +import com.yutou.nas_music_player.tools.AppTools; +import java.util.ArrayList; import java.util.List; public class MusicLibsFragment extends Fragment { private Context context; private View view; - private ListView listView; + private RecyclerView listView; private int model = 0; private MediaBrowserHelper browserHelper; @@ -67,9 +72,10 @@ public class MusicLibsFragment extends Fragment { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (view != null) return view; - view = inflater.inflate(R.layout.fragment_music_libs, null); + view = inflater.inflate(R.layout.fragment_music_libs, container, false); if (adapter == null || listView == null) initViews(); + initData(); return view; } @@ -77,128 +83,33 @@ public class MusicLibsFragment extends Fragment { public void initViews() { listView = view.findViewById(R.id.listView); - // recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); - adapter = new MusicLibsAdapter(getContext(), model); + listView.setLayoutManager(new LinearLayoutManager(getContext())); + adapter = new MusicLibsAdapter(getContext()); listView.setAdapter(adapter); - listView.setOnScrollListener(new AbsListView.OnScrollListener() { - @Override - public void onScrollStateChanged(AbsListView view, int scrollState) { - if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { - adapter.setScrollStatus(1); - adapter.notifyDataSetChanged(); - } else { - adapter.setScrollStatus(0); - } - } - - @Override - public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { - - } - }); - listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - System.out.println("点击列表:"+adapter.getModel()); - switch (adapter.getModel()) { - case MusicLibsAdapter.LIBS_MODEL_ALL: - clickAllList(position); - break; - case MusicLibsAdapter.LIBS_MODEL_Album: - clickAlbum(position, view); - break; - case MusicLibsAdapter.LIBS_MODEL_PLAY_Collection: - clickCollection(position, view); - break; - } - } - - private void clickCollection(int position, View view) { - MusicLibsAdapter.ViewHolder holder = (MusicLibsAdapter.ViewHolder) view.getTag(); - Pair pImage = Pair.create(holder.itemView, "album_image"); - Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), pImage).toBundle(); - MusicData data = adapter.getItem(position); - if (data == null) - return; - if(StringUtil.isEmpty(data.getComment())){ - Toast.makeText(getContext(),"未收藏任何歌曲",Toast.LENGTH_LONG).show(); - return; - } - Intent intent = new Intent(getContext(), AlbumsActivity.class); - intent.putExtra("model",2); - intent.putExtra("fid", data.getId()); - startActivity(intent, bundle); - } - - private void clickAlbum(int position, View view) { - MusicLibsAdapter.ViewHolder holder = (MusicLibsAdapter.ViewHolder) view.getTag(); - Pair pImage = Pair.create(holder.itemView, "album_image"); - Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), pImage).toBundle(); - - MusicData data = adapter.getItem(position); - if (data == null) - return; - Intent intent = new Intent(getContext(), AlbumsActivity.class); - intent.putExtra("model",0); - intent.putExtra("album", data.getAlbum()); - startActivity(intent, bundle); - } - - private void clickAllList(int position) { - MusicData musicData = adapter.getItem(position); - if (musicData != null) { - if (musicData.isDir()) { - changeList(musicData.getFile()); - } else { - if (browserHelper != null) { - browserHelper.play(adapter.getItem(position)); - adapter.notifyDataSetChanged(); - }else{ - System.out.println("browserHelper为空"); - } - } - } - } - }); + adapter.setBrowserHelper(browserHelper); adapter.setScrollStatus(1); + adapter.setOnClickAction((intent, view) -> { + Pair pImage = Pair.create(view, "main_album_image"); + Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), pImage).toBundle(); + getContext().startActivity(intent, bundle); + }); adapter.notifyDataSetChanged(); - int pos = 0; - switch (model) { - case MusicLibsAdapter.LIBS_MODEL_ALL: - pos = MusicContainer.getLibs().findMusicIndex(MusicContainer.getInstance().getNowPlayData().getMd5() - , MusicContainer.getLibs().getMainData()); - break; + if(MusicContainer.getInstance().getNowPlayData()==null){ + return; } - if (pos != -1) - listView.setSelection(pos); } public MusicLibsAdapter getAdapter() { return adapter; } - private void changeList(String path) { - MusicContainer.getLibs().onDirMusicData(path, new NetworkInterface() { - @Override - public void httpGetData(Object data, int state) { - List list = (List) data; - adapter.setPath(path); - adapter.setData(list); - adapter.notifyDataSetChanged(); - } - - @Override - public void httpError(Exception e) { - - } - }); - } public void flash() { if (adapter == null) return; if (model == MusicLibsAdapter.LIBS_MODEL_Tmp) { - adapter.initData(model); + // adapter.initData(model); + initData(); } adapter.notifyDataSetChanged(); } @@ -209,4 +120,92 @@ public class MusicLibsFragment extends Fragment { adapter.initData(model); adapter.notifyDataSetChanged(); } + + List list = new ArrayList<>(); + private String musicPath = "root"; + + public void initData() { + AppTools.showHud(context); + list.clear(); + adapter.setModel(model); + switch (model) { + case LIBS_MODEL_Album: + String album = null; + MusicContainer.getLibs().onAlbumMusicData(album, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + list.clear(); + if (data != null && state == 0) + list.addAll((List) data); + adapter.setData(list); + adapter.notifyDataSetChanged(); + AppTools.hideHud(); + } + + @Override + public void httpError(Exception e) { + AppTools.hideHud(); + } + }); + break; + case LIBS_MODEL_Artist: + AppTools.hideHud(); + break; + case LIBS_MODEL_Tmp: + list.addAll(MusicContainer.getInstance().getTmpPlayList()); + System.out.println("临时播放列表:" + MusicContainer.getInstance().getTmpPlayList().size()); + AppTools.hideHud(); + break; + case LIBS_MODEL_PLAY_LIST: + list.addAll(MusicContainer.getInstance().getPlayList()); + AppTools.hideHud(); + break; + case LIBS_MODEL_PLAY_Collection: + MusicContainer.getLibs().onCollectionList(null, new NetworkInterface() { + + @Override + public void httpGetData(Object data, int state) { + list.clear(); + if (data != null && state == 0) + list.addAll((List) data); + adapter.setData(list); + adapter.notifyDataSetChanged(); + AppTools.hideHud(); + } + + @Override + public void httpError(Exception e) { + AppTools.hideHud(); + } + }); + break; + case LIBS_MODEL_ALL: + default: + MusicContainer.getLibs().onDirMusicData(musicPath, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + list.clear(); + if (data != null && state == 0) + list.addAll((List) data); + AppData.handler.post(new Runnable() { + @Override + public void run() { + adapter.setData(list); + AppTools.hideHud(); + adapter.notifyDataSetChanged(); + } + }); + } + + @Override + public void httpError(Exception e) { + AppData.handler.post(AppTools::hideHud); + } + }); + + } + System.out.println("list.size() = " + list.size()); + adapter.setData(list); + adapter.notifyDataSetChanged(); + } } diff --git a/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java b/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java index e7f868b..0458d92 100644 --- a/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java +++ b/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java @@ -12,7 +12,6 @@ import android.support.v4.media.session.PlaybackStateCompat; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; -import android.view.Window; import android.view.WindowManager; import android.widget.ImageButton; import android.widget.ImageView; @@ -32,6 +31,7 @@ import com.yutou.nas_music_player.R; import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MusicContainer; import com.yutou.nas_music_player.tools.AppData; +import com.yutou.nas_music_player.tools.AppTools; import com.yutou.nas_music_player.tools.ConfigTools; import java.util.List; @@ -48,12 +48,11 @@ public class MainActivity extends AppCompatActivity { private MediaBrowserHelper browserHelper; private ImageView album_image, background_image; private MarqueeTextView title, album, artist; - private TextView positionTime, durationTime, bitRate; + private TextView positionTime, durationTime, bitRate, playIndex; private SeekBar seekBar; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { - getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); super.onCreate(savedInstanceState); setBarColor(android.R.color.transparent); @@ -86,7 +85,6 @@ public class MainActivity extends AppCompatActivity { @Override public void run() { browserHelper.getTransportControls().skipToNext(); - } }); @@ -131,11 +129,11 @@ public class MainActivity extends AppCompatActivity { @Override public void onClick(View v) { int model = (int) playModel.getTag(); - if (model == MusicContainer.PLAY_MODEL_LOOP) { - setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM); + if (model == MusicContainer.PLAY_MODEL_LIST_RANDOM) { + setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM,true); return; } - setPlayModelButton(model + 1); + setPlayModelButton(model + 1,true); } }); getWindow().getDecorView().setOnTouchListener(new View.OnTouchListener() { @@ -148,14 +146,13 @@ public class MainActivity extends AppCompatActivity { downPosition_y = event.getRawY(); } else if (event.getAction() == MotionEvent.ACTION_UP) { if (Math.abs(downPosition_x - event.getRawX()) < 500 && downPosition_y - event.getRawY() > 500) { - Pair pImage = Pair.create((View) album_image, "album_image"); + Pair pImage = Pair.create((View) album_image, "main_album_image"); Pair pPrevious = Pair.create((View) previous, "previous"); Pair pPlay = Pair.create((View) play, "play"); Pair pNext = Pair.create((View) next, "next"); Pair pTitle = Pair.create((View) title, "title"); Pair pArtist = Pair.create((View) artist, "artist"); Pair pBar = Pair.create((View) seekBar, "bar"); - //ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this,list); Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, pImage, pPrevious, @@ -211,6 +208,11 @@ public class MainActivity extends AppCompatActivity { artist.setText(data.getArtist()); album.setText(data.getAlbum()); bitRate.setText(data.getBitRate() + "kbps / " + data.getSampleRate() + "hz"); + if (MusicContainer.getInstance().getPlayList().isEmpty()) { + playIndex.setText("(∞)"); + } else { + playIndex.setText("(" + (MusicContainer.getInstance().getPlayIndex() + 1) + "/" + MusicContainer.getInstance().getPlayList().size() + ")"); + } seekBar.setMax((int) (data.getDurationAsDouble() * 1000)); int minutes = (int) (data.getDurationAsDouble() / 60); int seconds = (int) (data.getDurationAsDouble() % 60); @@ -218,13 +220,13 @@ public class MainActivity extends AppCompatActivity { + String.format(Locale.CHINA, "%02d", seconds)); int model = ConfigTools.getPreferences().getInt("playModel", MusicContainer.PLAY_MODEL_RANDOM); setPlayModelButton(model); - if (data.getImg(-1, -1) != null) { + if (data.getImg(AppData.imageWidth, AppData.imageHigh) != null) { setImage(data); } else { new Timer().schedule(new TimerTask() { @Override public void run() { - if (data.getImg(-1, -1) != null) { + if (data.getImg(AppData.imageWidth, AppData.imageHigh) != null) { AppData.handler.post(new Runnable() { @Override public void run() { @@ -240,31 +242,46 @@ public class MainActivity extends AppCompatActivity { } private void setPlayModelButton(int model) { + setPlayModelButton(model, false); + } + + private void setPlayModelButton(int model, boolean isButton) { playModel.setTag(model); MusicContainer.getInstance().setPlayModel(model); switch (model) { case MusicContainer.PLAY_MODEL_RANDOM: playModel.setImageResource(R.drawable.ic_play_random); + if (isButton) + AppTools.toast("全列表随机模式"); break; case MusicContainer.PLAY_MODEL_ORDER: playModel.setImageResource(R.drawable.ic_play_list); + if (isButton) + AppTools.toast("列表顺序模式"); break; case MusicContainer.PLAY_MODEL_ONE_LOOP: playModel.setImageResource(R.drawable.ic_play_one_loop); + if (isButton) + AppTools.toast("单曲循环"); break; - case MusicContainer.PLAY_MODEL_LOOP: + case MusicContainer.PLAY_MODEL_LIST_RANDOM: playModel.setImageResource(R.drawable.ic_play_loop); + if (isButton) + AppTools.toast("列表随机"); break; + default: + setPlayModelButton(MusicContainer.PLAY_MODEL_RANDOM,isButton); + return; } ConfigTools.getPreferences().edit().putInt("playModel", model).apply(); } private void setImage(MusicData data) { - setBarColor(data.getImageColor()); - Glide.with(this).load(data.getImg(-1, -1)) + // setBarColor(data.getImageColor()); + Glide.with(this).load(data.getImg(AppData.imageWidth, AppData.imageHigh)) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3))) .into(album_image); - Glide.with(this).load(data.getImg(200, 200)) + Glide.with(this).load(data.getImg(AppData.minImageWidth, AppData.minImageHigh)) .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) .into(background_image); } @@ -283,11 +300,14 @@ public class MainActivity extends AppCompatActivity { durationTime = findViewById(R.id.durationTime); seekBar = findViewById(R.id.seekBar); playModel = findViewById(R.id.playModel); + playIndex = findViewById(R.id.playIndex); + int model = ConfigTools.getPreferences().getInt("playModel", MusicContainer.PLAY_MODEL_RANDOM); + setPlayModelButton(model); } public void setBarColor(int color) { - getWindow().setStatusBarColor(color); - getWindow().setNavigationBarColor(color); + //getWindow().setStatusBarColor(color); + // getWindow().setNavigationBarColor(Color.parseColor("#00FFFFFF")); } @Override @@ -300,6 +320,12 @@ public class MainActivity extends AppCompatActivity { finish(); } + @Override + protected void onResume() { + super.onResume(); + setPlayData(MusicContainer.getInstance().getNowPlayData()); + } + @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); @@ -308,29 +334,32 @@ public class MainActivity extends AppCompatActivity { @Override public boolean onKeyUp(int keyCode, KeyEvent event) { - if(keyCode==KeyEvent.KEYCODE_BACK){ + if (keyCode == KeyEvent.KEYCODE_BACK) { moveTaskToBack(true); return false; } return super.onKeyUp(keyCode, event); } - float downX=0; - float downY=0; + + float downX = 0; + float downY = 0; + @Override public boolean onTouchEvent(MotionEvent event) { - if(event.getAction()==MotionEvent.ACTION_DOWN){ - downX=event.getRawX(); - downY=event.getRawY(); - }else if (event.getAction()==MotionEvent.ACTION_UP){ - pass(event.getRawX(),event.getRawY()); + if (event.getAction() == MotionEvent.ACTION_DOWN) { + downX = event.getRawX(); + downY = event.getRawY(); + } else if (event.getAction() == MotionEvent.ACTION_UP) { + pass(event.getRawX(), event.getRawY()); } return super.onTouchEvent(event); } - private void pass(float upX,float upY){ - boolean isOkY=Math.abs(upY-downY)<=200; - if(upX-downX>200&&isOkY){ + + private void pass(float upX, float upY) { + boolean isOkY = Math.abs(upY - downY) <= 200; + if (upX - downX > 200 && isOkY) { previous.callOnClick(); - }else if (upX-downX<-200&&isOkY){ + } else if (upX - downX < -200 && isOkY) { next.callOnClick(); } } @@ -340,7 +369,7 @@ public class MainActivity extends AppCompatActivity { @Override public void onPlaybackStateChanged(PlaybackStateCompat state) { super.onPlaybackStateChanged(state); - System.out.println("播放状态变化:" + state.getPosition() + " " + seekBar.getMax()); + //System.out.println("播放状态变化:" + state.getPosition() + " " + seekBar.getMax()); if (state.getState() == PlaybackStateCompat.STATE_PLAYING) { int duration = (int) (state.getPosition() / 1000); int minutes = duration / 60; @@ -348,6 +377,7 @@ public class MainActivity extends AppCompatActivity { positionTime.setText(String.format(Locale.CHINA, "%02d", minutes) + ":" + String.format(Locale.CHINA, "%02d", seconds)); seekBar.setProgress((int) (state.getPosition())); + play.setImageResource(android.R.drawable.ic_media_pause); } else { play.setImageResource(android.R.drawable.ic_media_play); @@ -381,9 +411,5 @@ public class MainActivity extends AppCompatActivity { super.onQueueChanged(queue); System.out.println("不知道是啥变换了"); } - - public void setImage() { - - } } } \ No newline at end of file diff --git a/app/src/main/java/com/yutou/nas_music_player/views/PlayLibsActivity.java b/app/src/main/java/com/yutou/nas_music_player/views/PlayLibsActivity.java index fd0eeca..08007b3 100644 --- a/app/src/main/java/com/yutou/nas_music_player/views/PlayLibsActivity.java +++ b/app/src/main/java/com/yutou/nas_music_player/views/PlayLibsActivity.java @@ -9,7 +9,6 @@ import android.support.v4.media.session.MediaControllerCompat; import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.PlaybackStateCompat; import android.view.View; -import android.view.Window; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ProgressBar; @@ -31,6 +30,7 @@ import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.R; import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MusicContainer; +import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.views.Fragments.MusicLibsFragment; import java.util.ArrayList; @@ -54,7 +54,6 @@ public class PlayLibsActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { - getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); super.onCreate(savedInstanceState); setContentView(R.layout.activity_play_list); PlayListener playListener = new PlayListener(); @@ -191,7 +190,7 @@ public class PlayLibsActivity extends AppCompatActivity { artist.setText(data.getArtist()); progressBar.setMax((int) (data.getDurationAsDouble() * 1000)); progressBar.setProgress(bar_pos); - data.getImg(-1, -1, new NetworkInterface() { + data.getImg(AppData.minImageWidth,AppData.minImageHigh, new NetworkInterface() { @Override public void httpGetData(Object data, int state) { setImage((Bitmap) data); diff --git a/app/src/main/res/layout/activity_album.xml b/app/src/main/res/layout/activity_album.xml index d036a85..75fd720 100644 --- a/app/src/main/res/layout/activity_album.xml +++ b/app/src/main/res/layout/activity_album.xml @@ -29,7 +29,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitCenter" - android:transitionName="album_image" + android:transitionName="main_album_image" app:srcCompat="@drawable/ic_launcher_background" /> + + + + + +