From 4a08266816f4242626ed7dfe559708f0cb504f17 Mon Sep 17 00:00:00 2001 From: yutou Date: Thu, 3 Dec 2020 18:30:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E6=94=B6=E8=97=8F?= =?UTF-8?q?=E5=A4=B9=E5=8A=9F=E8=83=BD=20=E6=8E=A5=E5=8F=A3=E6=94=B9?= =?UTF-8?q?=E6=88=90=E4=BA=86POST=E8=AF=B7=E6=B1=82=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E4=B8=8D=E5=86=8D=E5=81=9Abase64=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 4 +- .../Adapters/AlbumsRecyclerAdapter.java | 27 ++- .../Adapters/MusicLibsAdapter.java | 151 +++++++++---- .../nas_music_player/Datas/MusicData.java | 6 +- .../containers/MusicContainer.java | 199 ++++++++++++++---- .../nas_music_player/tools/AppTools.java | 8 +- .../nas_music_player/tools/NetworkTool.java | 62 ++++-- .../views/AlbumsActivity.java | 64 +++++- .../views/Fragments/MusicLibsFragment.java | 114 +++++----- .../views/PlayLibsActivity.java | 136 ++++++------ .../main/res/layout/activity_play_list.xml | 64 +++--- 11 files changed, 576 insertions(+), 259 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 1b81f4d..b0b0799 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -67,10 +67,8 @@ dependencies { implementation 'androidx.palette:palette:1.0.0' implementation 'com.github.zrunker:ZTextView:v1.0.2' - debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.5' //内存泄漏检测工具 + //debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.5' //内存泄漏检测工具 implementation 'com.kaopiz:kprogresshud:1.2.0' - implementation 'com.ogaclejapan.smarttablayout:library:2.0.0@aar' - implementation 'com.ogaclejapan.smarttablayout:utils-v4:2.0.0@aar' implementation 'com.github.promeg:tinypinyin:2.0.3' implementation 'com.github.myinnos:AlphabetIndex-Fast-Scroll-RecyclerView:1.0.95' implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' diff --git a/app/src/main/java/com/yutou/nas_music_player/Adapters/AlbumsRecyclerAdapter.java b/app/src/main/java/com/yutou/nas_music_player/Adapters/AlbumsRecyclerAdapter.java index 122a6c8..ae44bea 100644 --- a/app/src/main/java/com/yutou/nas_music_player/Adapters/AlbumsRecyclerAdapter.java +++ b/app/src/main/java/com/yutou/nas_music_player/Adapters/AlbumsRecyclerAdapter.java @@ -3,6 +3,7 @@ package com.yutou.nas_music_player.Adapters; import android.annotation.SuppressLint; import android.content.Context; import android.content.DialogInterface; +import android.renderscript.AllocationAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -35,10 +36,16 @@ public class AlbumsRecyclerAdapter extends RecyclerView.Adapter list; Context context; MediaBrowserHelper browserHelper; + int model; + int fid; - public AlbumsRecyclerAdapter(Context context, List list) { + public AlbumsRecyclerAdapter(Context context, List list,int model) { this.list = list; this.context = context; + this.model=model; + } + public void setFid(int fid){ + this.fid=fid; } @NonNull @@ -105,12 +112,20 @@ public class AlbumsRecyclerAdapter extends RecyclerView.Adapter{ + MusicContainer.getLibs().removeCollection(fid, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + AlbumsRecyclerAdapter.this.notifyDataSetChanged(); + } + + @Override + public void httpError(Exception e) { + + } + }, data); }); + holder.layout.setOnClickListener(view -> browserHelper.play(data)); if(MusicContainer.getInstance().getNowPlayData().getMd5().equals(data.getMd5())){ holder.play_mask.setVisibility(View.VISIBLE); }else{ 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 68f2989..033aedf 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 @@ -14,6 +14,7 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; @@ -49,28 +50,28 @@ public class MusicLibsAdapter extends ArrayAdapter { 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; - private MusicLibsAdapter adapter; - + KProgressHUD hud; List list; - Map> listMap; Map icons; + Context context; public MusicLibsAdapter(Context context, int model) { super(context, R.layout.layout_music_libs_item); list = new ArrayList<>(); icons = new HashMap<>(); - listMap = new HashMap<>(); - adapter = this; - initData(model); + this.context = context; + initData(model, true); } private int model = -1; private String musicPath = "root"; - public void initData(int model) { + public void initData(int model, boolean init) { this.model = model; list.clear(); + if (!init) + hud = AppTools.showLoading(context); System.out.println("填充序列 设置model " + model + " " + this.model + " &"); Map> map; switch (model) { @@ -78,7 +79,12 @@ public class MusicLibsAdapter extends ArrayAdapter { new Thread(new Runnable() { @Override public void run() { - String album=null; + 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) { @@ -86,11 +92,14 @@ public class MusicLibsAdapter extends ArrayAdapter { 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(); } }); } @@ -101,6 +110,9 @@ public class MusicLibsAdapter extends ArrayAdapter { 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()); @@ -109,8 +121,12 @@ public class MusicLibsAdapter extends ArrayAdapter { new Thread(new Runnable() { @Override public void run() { - String collection=null; - MusicContainer.getLibs().onCollectionList(collection,new NetworkInterface(){ + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + MusicContainer.getLibs().onCollectionList(null, new NetworkInterface() { @Override public void httpGetData(Object data, int state) { @@ -118,11 +134,14 @@ public class MusicLibsAdapter extends ArrayAdapter { 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(); } }); } @@ -134,6 +153,11 @@ public class MusicLibsAdapter extends ArrayAdapter { 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) { @@ -143,22 +167,27 @@ public class MusicLibsAdapter extends ArrayAdapter { AppData.handler.post(new Runnable() { @Override public void run() { - notifyDataSetChanged(); - + notifyDataSetChanged(); + if (hud != null) + hud.dismiss(); } }); } @Override public void httpError(Exception e) { - + if (hud != null) + hud.dismiss(); } }); } }).start(); } - listMap.put(model, list); + } + + public void initData(int model) { + initData(model, false); } @Override @@ -198,9 +227,18 @@ public class MusicLibsAdapter extends ArrayAdapter { 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) { - holder.message.setText(data.getBitRate() + "kbps | " + data.getSampleRate() + "hz | " + data.getEncodingType()); + 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 {//为专辑或艺术家 @@ -233,11 +271,50 @@ public class MusicLibsAdapter extends ArrayAdapter { holder.play_mask.setVisibility(View.GONE); holder.icon.setImageResource(R.drawable.ic_dir); } - if(MusicContainer.getInstance().getNowPlayData().getMd5().equals(data.getMd5())){ + if (MusicContainer.getInstance().getNowPlayData().getMd5().equals(data.getMd5())) { holder.play_mask.setVisibility(View.VISIBLE); - }else{ + } 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.getFileBase64()); + 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) { @@ -257,12 +334,22 @@ public class MusicLibsAdapter extends ArrayAdapter { private void addAlbumList(String album) { System.out.println("添加专辑:" + album); - List list = MusicContainer.getInstance().getAllAlbumMapList().get(album); - for (MusicData musicData : list) { - System.out.println(musicData.getTitle() + "| 专辑名 =" + musicData.getAlbum()); - MusicContainer.getInstance().addTmpList(musicData); - } - Toast.makeText(getContext(), "已添加到队列中", Toast.LENGTH_LONG).show(); + 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) { @@ -275,15 +362,15 @@ public class MusicLibsAdapter extends ArrayAdapter { @Override public void onClick(DialogInterface dialog, int which) { hud = AppTools.showLoading(getContext()); - JSONObject json=new JSONObject(); + JSONObject json = new JSONObject(); json.put("path", data.getFileBase64()); - json.put("type",true); + 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) { - System.out.println("加入到临时列表:"+musicData.getTitle()); + System.out.println("加入到临时列表:" + musicData.getTitle()); MusicContainer.getInstance().addTmpList(musicData); hud.dismiss(); } @@ -331,13 +418,6 @@ public class MusicLibsAdapter extends ArrayAdapter { for (String integer : icons.keySet()) { Objects.requireNonNull(icons.get(integer)).recycle(); } - for (Integer integer : listMap.keySet()) { - for (MusicData data : Objects.requireNonNull(listMap.get(integer))) { - data.recycleImg(); - } - Objects.requireNonNull(listMap.get(integer)).clear(); - } - listMap.clear(); icons.clear(); } @@ -354,7 +434,6 @@ public class MusicLibsAdapter extends ArrayAdapter { } - public static class ViewHolder extends RecyclerView.ViewHolder { MarqueeTextView title, artist, message; LinearLayout buttons; 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 fb010ce..fab5bc2 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 @@ -195,7 +195,7 @@ public class MusicData { public String getFileBase64() { if (StringUtil.isEmpty(file)) return null; - return new String(Base64.encode(file.getBytes(), Base64.DEFAULT)).replace("\n", ""); + return file; } public String getPlayUrl() { @@ -388,7 +388,8 @@ public class MusicData { @Override public String toString() { return "MusicData{" + - "artist='" + artist + '\'' + + "id=" + id + + ", artist='" + artist + '\'' + ", album='" + album + '\'' + ", title='" + title + '\'' + ", comment='" + comment + '\'' + @@ -410,7 +411,6 @@ public class MusicData { ", lossless=" + lossless + ", variableBitRate=" + variableBitRate + ", md5='" + md5 + '\'' + - ", isDownloadImg=" + isDownloadImg + '}'; } } 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 a073fdc..ee21923 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 @@ -1,18 +1,25 @@ package com.yutou.nas_music_player.containers; +import android.content.Context; +import android.content.DialogInterface; import android.media.MediaPlayer; import android.os.Build; import android.support.v4.media.MediaMetadataCompat; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.ListView; import android.widget.Toast; +import androidx.appcompat.app.AlertDialog; + import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Datas.PreviousPlayerList; -import com.yutou.nas_music_player.Interfaces.DownloadInterface; import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.MyApplication; -import com.yutou.nas_music_player.tools.AppData; import com.yutou.nas_music_player.tools.CollectionTools; import com.yutou.nas_music_player.tools.ConfigTools; import com.yutou.nas_music_player.tools.NetworkTool; @@ -21,9 +28,7 @@ import com.yutou.nas_music_player.tools.StringUtil; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; -import java.net.URLEncoder; import java.util.ArrayList; -import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -449,7 +454,7 @@ public class MusicContainer { return collectionMapList; } - public class MusicLibs { + public class MusicLibs { private List mainData; private MusicLibs() { @@ -457,12 +462,158 @@ public class MusicContainer { initData(); } - public void onCollectionList(String collection, NetworkInterface networkInterface) { - JSONObject json=new JSONObject(); - NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_List, json, new NetworkInterface() { + public void onAddCollection(String md5, int fid) { + + } + + public void onAddCollectionDir(Context context, MusicData... collectionData) { + onCollectionList(null, new NetworkInterface() { @Override public void httpGetData(Object data, int state) { + List list = (List) data; + List titles = new ArrayList<>(); + titles.add("添加新收藏夹"); + for (MusicData musicData : list) { + titles.add(musicData.getTitle()); + } + AlertDialog dialog = new AlertDialog.Builder(context) + .setTitle("添加到收藏夹") + .setItems((CharSequence[]) titles.toArray(new CharSequence[list.size()]), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (which == 0) {//添加新收藏夹 + EditText editText = new EditText(context); + new AlertDialog.Builder(context) + .setTitle("收藏夹名字") + .setView(editText) + .setPositiveButton("提交", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (StringUtil.isEmpty(editText.getText().toString())) { + Toast.makeText(context, "名字不能为空", Toast.LENGTH_LONG).show(); + return; + } + JSONObject json = new JSONObject(); + json.put("favorite", editText.getText().toString()); + NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_Dir_Add, json, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + JSONObject json = JSONObject.parseObject((String) data); + Toast.makeText(context, json.getString("msg"), Toast.LENGTH_LONG).show(); + dialog.dismiss(); + } + + @Override + public void httpError(Exception e) { + dialog.dismiss(); + } + }); + } + }) + .setNegativeButton("取消", (dialog1, which1) -> dialog1.dismiss()).create().show(); + } else {//选择现有收藏夹 + MusicData musicData = list.get(which - 1); + JSONArray array = new JSONArray(); + for (MusicData data : collectionData) { + if (!StringUtil.isEmpty(data.getMd5())) + array.add(data.getMd5()); + } + JSONObject json = new JSONObject(); + json.put("fid", musicData.getId()); + json.put("md5s", array); + NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_Add, json, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + JSONObject json = JSONObject.parseObject((String) data); + Toast.makeText(context, json.getString("msg"), Toast.LENGTH_LONG).show(); + dialog.dismiss(); + } + + @Override + public void httpError(Exception e) { + dialog.dismiss(); + } + }); + + } + } + }) + .setNegativeButton("取消", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }).create(); + dialog.show(); + + } + + @Override + public void httpError(Exception e) { + + } + }); + } + + public void onCollectionList(String collection, NetworkInterface networkInterface) { + JSONObject json = new JSONObject(); + String url = NetworkTool.NetworkAPI.MUSIC_Collection_Dir_List; + if (collection != null) { + url = NetworkTool.NetworkAPI.MUSIC_Collection_Get; + json.put("fid",Integer.parseInt(collection)); + } + NetworkTool.init().httpGet(url, json, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + JSONObject json = JSONObject.parseObject((String) data); + if (json.getInteger("code") == 0) { + if(json.getJSONArray("data").toJSONString().contains("md5")){ + networkInterface.httpGetData(JSONArray.parseArray(json.getJSONArray("data").toJSONString(), MusicData.class), 0); + }else{ + List list = new ArrayList<>(); + for (Object album : json.getJSONArray("data")) { + JSONObject js = (JSONObject) album; + MusicData musicData = new MusicData(); + musicData.setId(js.getInteger("id")); + musicData.setComment(js.getLong("count") + ""); + musicData.setTitle(js.getString("title")); + musicData.setAlbum(js.getString("title")); + musicData.setDir(false); + list.add(musicData); + } + networkInterface.httpGetData(list, 0); + } + } else { + networkInterface.httpGetData(new ArrayList<>(), 0); + } + } + + @Override + public void httpError(Exception e) { + + } + }); + } + public void removeCollection(int fid,NetworkInterface networkInterface,MusicData... musicData){ + JSONObject json=new JSONObject(); + JSONArray array=new JSONArray(); + for (MusicData data : musicData) { + JSONObject js=new JSONObject(); + js.put("md5",data.getMd5()); + js.put("fid",fid); + array.add(js); + } + json.put("data",array); + + NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_Collection_Remove, json, new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + JSONObject json=JSONObject.parseObject((String) data); + if(networkInterface!=null){ + networkInterface.httpGetData(data,state); + } + Toast.makeText(MyApplication.application,json.getString("msg"),Toast.LENGTH_LONG).show(); } @Override @@ -480,32 +631,6 @@ public class MusicContainer { if (initInterface != null) initInterface.init(); initInterface = null; - /*NetworkTool.init().httpGet(NetworkTool.NetworkAPI.MUSIC_ALL, new JSONObject(), new NetworkInterface() { - @Override - public void httpGetData(Object data, int state) { - JSONObject json = JSONObject.parseObject(data.toString()); - if (json.getInteger("code") == 0) { - mainData = JSONArray.parseArray(json.getJSONArray("data").toJSONString(), MusicData.class); - System.out.println(mainData.size()); - if (mainData.size() > 0) { - nowPlayData = mainData.get(0); - } - System.out.println("获取列表完成"); - for (MusicData mainDatum : mainData) { - if (StringUtil.isEmpty(mainDatum.getMd5())) { - System.out.println(mainDatum.getFile()); - } - } - updateLibsMap(); - - } - } - - @Override - public void httpError(Exception e) { - - } - });*/ } public void onAlbumMusicData(String album, NetworkInterface networkInterface) { @@ -523,14 +648,14 @@ public class MusicContainer { try { networkInterface.httpGetData(JSONArray.parseArray(json.getJSONArray("data").toJSONString(), MusicData.class), 0); } catch (Exception e) { - List list=new ArrayList<>(); + List list = new ArrayList<>(); for (Object album : json.getJSONArray("data")) { - MusicData musicData=new MusicData(); + MusicData musicData = new MusicData(); musicData.setTitle((String) album); musicData.setAlbum((String) album); list.add(musicData); } - networkInterface.httpGetData(list,0); + networkInterface.httpGetData(list, 0); } } else { networkInterface.httpGetData(new ArrayList<>(), 0); 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 f9050bd..8f095b1 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 @@ -18,15 +18,19 @@ import java.security.MessageDigest; public class AppTools { - public static KProgressHUD showLoading(Context context){ + public static KProgressHUD showLoading(Context context,String title){ return KProgressHUD.create(context) .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE) - .setDetailsLabel("loading...") + .setDetailsLabel(title) .setCancellable(true) .setAnimationSpeed(2) .setDimAmount(0.5f) .show(); } + + public static KProgressHUD showLoading(Context context){ + return showLoading(context,"loading..."); + } public static void saveBitmap(final Bitmap bitmap, final String saveKey){ new Thread(new Runnable() { @Override diff --git a/app/src/main/java/com/yutou/nas_music_player/tools/NetworkTool.java b/app/src/main/java/com/yutou/nas_music_player/tools/NetworkTool.java index 4670539..3891a4a 100644 --- a/app/src/main/java/com/yutou/nas_music_player/tools/NetworkTool.java +++ b/app/src/main/java/com/yutou/nas_music_player/tools/NetworkTool.java @@ -37,7 +37,12 @@ public class NetworkTool { public static String HOME = ""; public static String MUSIC_LIST = HOME + "/music/list.do"; public static String MUSIC_Album = HOME + "/music/getAlbum.do"; - public static String MUSIC_Collection_List = HOME + "/music/favorite/dir/list.do"; + public static String MUSIC_Collection_Dir_List = HOME + "/music/favorite/dir/list.do"; + public static String MUSIC_Collection_Dir_Add = HOME + "/music/favorite/dir/add.do"; + public static String MUSIC_Collection_Dir_ReName = HOME + "/music/favorite/dir/rename.do"; + public static String MUSIC_Collection_Get = HOME + "/music/favorite/get.do"; + public static String MUSIC_Collection_Add = HOME + "/music/favorite/add.do"; + public static String MUSIC_Collection_Remove = HOME + "/music/favorite/remove.do"; public static String MUSIC_ALL = HOME + "/music/all.do"; public static String MUSIC_METADATA = HOME + "/music/find/file.do"; public static String MUSIC_IMAGE = HOME + "/music/image.do"; @@ -53,7 +58,8 @@ public class NetworkTool { public static String donwloadPath = AppData.dir + AppData.download_dir + File.separator; private NetworkTool() { - HOME = "http://192.168.31.92:8000/nas"; + //HOME = "http://192.168.31.92:8000/nas"; + HOME = "http://192.168.137.1:8000/nas"; if (StringUtil.isEmpty(HOME)) { httpGet("http://tools.yutou233.cn/nas/music/getlocalhost.do", new JSONObject(), new NetworkInterface() { @Override @@ -83,7 +89,11 @@ public class NetworkTool { return network; } - public void httpGet(final String url, final JSONObject body, final NetworkInterface networkInterface) { + public void httpGet(String url, JSONObject body, NetworkInterface networkInterface) { + httpPost(url, body, networkInterface); + } + @Deprecated + public void httpGet_old(final String url, final JSONObject body, final NetworkInterface networkInterface) { if (!url.startsWith("http:") && !StringUtil.isEmpty(HOME)) { httpGet(HOME + url, body, networkInterface); return; @@ -148,6 +158,19 @@ public class NetworkTool { private Map map = Collections.synchronizedMap(new HashMap()); public void httpPost(final String url, final JSONObject body, final NetworkInterface networkInterface) { + if (!url.startsWith("http:") && !StringUtil.isEmpty(HOME)) { + httpPost(HOME + url, body, networkInterface); + return; + } + if (StringUtil.isEmpty(HOME) && !url.contains("getlocalhost.do")) { + AppData.handler.postDelayed(new Runnable() { + @Override + public void run() { + NetworkTool.init().httpPost(url, body, networkInterface); + } + }, 300); + return; + } new Thread(new Runnable() { @Override @@ -161,14 +184,16 @@ public class NetworkTool { connection.setDoInput(true); connection.setConnectTimeout(5 * 1000); connection.setReadTimeout(10 * 1000); - connection.addRequestProperty("Connection", "keep-alive"); - connection.addRequestProperty("User-Agent", getExtUa()); + //connection.addRequestProperty("Connection", "keep-alive"); + //connection.addRequestProperty("User-Agent", getExtUa()); + connection.addRequestProperty("content-type","application/json"); + connection.addRequestProperty("charset","UTF-8"); OutputStream outputStream = connection.getOutputStream(); - String bytes = body.toString(); - outputStream.write(bytes.getBytes()); + outputStream.write(body.toJSONString().getBytes()); outputStream.flush(); outputStream.close(); + System.out.println(connection.getResponseCode()); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); while ((tmp = reader.readLine()) != null) { str.append(tmp); @@ -180,12 +205,22 @@ public class NetworkTool { if (map.containsKey(body)) { map.remove(body); } - if (networkInterface != null) - networkInterface.httpGetData(str.toString(), connection.getResponseCode()); - connection.disconnect(); - } catch (SocketTimeoutException e) { - Log.e(TAG, url + "\n传参:" + body.toString() + "\n密文:" + str + "\n" + e); + AppData.handler.post(new Runnable() { + @Override + public void run() { + if (networkInterface != null) { + try { + networkInterface.httpGetData(str.toString(), connection.getResponseCode()); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + }); + connection.disconnect(); + reader.close(); + } catch (SocketTimeoutException e) { synchronized (NetworkTool.this) { if (!map.containsKey(body)) { @@ -203,11 +238,14 @@ public class NetworkTool { } catch (ConnectException e) { e.printStackTrace(); } catch (IOException e) { + e.printStackTrace(); if (networkInterface != null) networkInterface.httpError(e); } catch (Exception e) { e.printStackTrace(); networkInterface.httpError(e); + }finally { + Log.e(TAG, url + "\n传参:" + body.toString() + "\n接收:" + str ); } } }).start(); 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 0af2936..d3edc45 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 @@ -23,6 +23,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.StringUtil; import java.util.ArrayList; import java.util.List; @@ -30,31 +31,76 @@ import java.util.List; import jp.wasabeef.glide.transformations.BlurTransformation; public class AlbumsActivity extends AppCompatActivity { - private ImageView album_image,image_background; + private ImageView album_image, image_background; private RecyclerView listView; private MediaBrowserHelper browserHelper; private AlbumsRecyclerAdapter adapter; + private int model;//0 专辑, 1 歌手, 2 收藏 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_album); - String album=getIntent().getStringExtra("album"); + model = getIntent().getIntExtra("model", -1); initView(); - initData(album); - browserHelper=new MediaBrowserHelper(this); + switch (model) { + case 2: + initDataOfCollection(getIntent().getIntExtra("fid", -1)); + break; + case 0: + initData(getIntent().getStringExtra("album")); + break; + default: + finish(); + } + browserHelper = new MediaBrowserHelper(this); browserHelper.regPlayListener(new PlayListener()); adapter.setPlayListener(browserHelper); } + private static class PlayListener extends MediaControllerCompat.Callback { } + private void initDataOfCollection(int fid) { + adapter.setFid(fid); + MusicContainer.getLibs().onCollectionList(fid + "", new NetworkInterface() { + @Override + public void httpGetData(Object data, int state) { + List list = (List) data; + adapter.setData(list); + adapter.notifyDataSetChanged(); + if(list.isEmpty()) + return; + 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 + public void httpError(Exception e) { + + } + }); + } + private void initData(String album) { MusicContainer.getLibs().onAlbumMusicData(album, new NetworkInterface() { @Override public void httpGetData(Object data, int state) { - List list= (List) data; + List list = (List) data; adapter.setData(list); adapter.notifyDataSetChanged(); list.get(0).getImg(-1, -1, new NetworkInterface() { @@ -84,11 +130,11 @@ public class AlbumsActivity extends AppCompatActivity { } private void initView() { - album_image=findViewById(R.id.album_image); - image_background=findViewById(R.id.image_background); - listView=findViewById(R.id.album_list); + album_image = findViewById(R.id.album_image); + image_background = findViewById(R.id.image_background); + listView = findViewById(R.id.album_list); listView.setLayoutManager(new LinearLayoutManager(this)); - adapter=new AlbumsRecyclerAdapter(AlbumsActivity.this,new ArrayList<>()); + adapter = new AlbumsRecyclerAdapter(AlbumsActivity.this, new ArrayList<>(),model); listView.setAdapter(adapter); } 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 5a399a0..0ec2526 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 @@ -3,42 +3,35 @@ package com.yutou.nas_music_player.views.Fragments; import android.content.Context; import android.content.Intent; import android.os.Bundle; -import android.util.ArrayMap; 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 com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItem; -import com.yutou.nas_music_player.Adapters.MusicLibsAdapter; -import com.yutou.nas_music_player.Datas.MusicData; -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.views.AlbumsActivity; -import com.yutou.nas_music_player.views.MainActivity; +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 java.util.HashMap; +import com.yutou.nas_music_player.Adapters.MusicLibsAdapter; +import com.yutou.nas_music_player.Datas.MusicData; +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 java.util.List; -import java.util.Map; - -import in.myinnos.alphabetsindexfastscrollrecycler.IndexFastScrollRecyclerView; public class MusicLibsFragment extends Fragment { - public static Map map = new HashMap<>(); private Context context; private View view; - private ListView recyclerView; + private ListView listView; private int model = 0; private MediaBrowserHelper browserHelper; @@ -72,16 +65,10 @@ public class MusicLibsFragment extends Fragment { @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - if(view!=null) + if (view != null) return view; view = inflater.inflate(R.layout.fragment_music_libs, null); - int id = FragmentPagerItem.getPosition(getArguments()); - if (map == null) - map = new HashMap<>(); - if (!map.containsKey(id)) { - map.put(id, this); - } - if (adapter == null || recyclerView == null) + if (adapter == null || listView == null) initViews(); return view; } @@ -89,11 +76,11 @@ public class MusicLibsFragment extends Fragment { private MusicLibsAdapter adapter; public void initViews() { - recyclerView = view.findViewById(R.id.listView); + listView = view.findViewById(R.id.listView); // recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); adapter = new MusicLibsAdapter(getContext(), model); - recyclerView.setAdapter(adapter); - recyclerView.setOnScrollListener(new AbsListView.OnScrollListener() { + listView.setAdapter(adapter); + listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { @@ -109,31 +96,54 @@ public class MusicLibsFragment extends Fragment { } }); - recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - switch (adapter.getModel()){ + switch (adapter.getModel()) { case MusicLibsAdapter.LIBS_MODEL_ALL: clickAllList(position); break; case MusicLibsAdapter.LIBS_MODEL_Album: - clickAlbum(position,view); + clickAlbum(position, view); + break; + case MusicLibsAdapter.LIBS_MODEL_PLAY_Collection: + clickCollection(position, view); break; } } - 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) + 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; - Intent intent=new Intent(getContext(), AlbumsActivity.class); - intent.putExtra("album",data.getAlbum()); - startActivity(intent,bundle); + 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 clickAllList(int position){ + + 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()) { @@ -157,7 +167,7 @@ public class MusicLibsFragment extends Fragment { break; } if (pos != -1) - recyclerView.setSelection(pos); + listView.setSelection(pos); } public MusicLibsAdapter getAdapter() { @@ -181,9 +191,19 @@ public class MusicLibsFragment extends Fragment { }); } - @Override - public void onDestroyView() { - super.onDestroyView(); - map.clear(); + public void flash() { + if (adapter == null) + return; + if (model == MusicLibsAdapter.LIBS_MODEL_Tmp) { + adapter.initData(model); + } + adapter.notifyDataSetChanged(); + } + + public void update() { + if (adapter == null) + return; + adapter.initData(model); + adapter.notifyDataSetChanged(); } } 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 d29b96e..cbf499c 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 @@ -8,34 +8,33 @@ import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.session.MediaControllerCompat; import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.PlaybackStateCompat; -import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.ProgressBar; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; +import androidx.viewpager2.adapter.FragmentStateAdapter; +import androidx.viewpager2.widget.ViewPager2; + import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; -import com.ogaclejapan.smarttablayout.SmartTabLayout; -import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter; -import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems; +import com.google.android.material.tabs.TabLayout; +import com.google.android.material.tabs.TabLayoutMediator; import com.yutou.nas_music_player.Adapters.MusicLibsAdapter; import com.yutou.nas_music_player.Datas.MusicData; 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; import java.util.List; -import java.util.Timer; -import java.util.TimerTask; - -import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatActivity; -import androidx.viewpager.widget.ViewPager; import cc.ibooker.ztextviewlib.MarqueeTextView; import jp.wasabeef.glide.transformations.RoundedCornersTransformation; @@ -49,8 +48,8 @@ public class PlayLibsActivity extends AppCompatActivity { private Handler handler; private int bar_pos = 0; - private SmartTabLayout tabLayout; - private ViewPager viewPager; + private TabLayout tabLayout; + private ViewPager2 viewPager; private int index = 0; @Override @@ -108,6 +107,7 @@ public class PlayLibsActivity extends AppCompatActivity { }); } + List fragments; private void initView() { play = findViewById(R.id.collection); next = findViewById(R.id.top); @@ -116,61 +116,63 @@ public class PlayLibsActivity extends AppCompatActivity { title = findViewById(R.id.title); artist = findViewById(R.id.artist); progressBar = findViewById(R.id.progressBar); - tabLayout = findViewById(R.id.viewpagertab); - viewPager = findViewById(R.id.viewpager); - FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter( - getSupportFragmentManager(), FragmentPagerItems.with(this) - .add("全部", MusicLibsFragment.class) - .add("专辑", MusicLibsFragment.class) - .add("收藏夹", MusicLibsFragment.class) - .add("队列", MusicLibsFragment.class) - .create()); - viewPager.setAdapter(adapter); - tabLayout.setViewPager(viewPager); - tabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { - boolean init = false; - + tabLayout = findViewById(R.id.tabLayout); + viewPager = findViewById(R.id.viewPager); + fragments=new ArrayList<>(); + fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_ALL)); + fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_Album)); + fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_PLAY_Collection)); + fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_Tmp)); + viewPager.setAdapter(new FragmentStateAdapter(this) { + @NonNull @Override - public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { - if (!init) { - MusicLibsFragment fragment = MusicLibsFragment.map.get(position); - if (fragment != null) { - fragment.setPlayContainer(browserHelper); - init = true; - } - } + public Fragment createFragment(int position) { + return fragments.get(position); } @Override - public void onPageSelected(int position) { - System.out.println("debug 切换页面:" + position); - index = position; - MusicLibsFragment fragment = MusicLibsFragment.map.get(position); - if (fragment != null) { - fragment.setPlayContainer(browserHelper); - switch (position) { - case 0: - fragment.setModel(MusicLibsAdapter.LIBS_MODEL_ALL); - break; - case 1: - fragment.setModel(MusicLibsAdapter.LIBS_MODEL_Album); - break; - case 2: - fragment.setModel(MusicLibsAdapter.LIBS_MODEL_PLAY_Collection); - break; - case 3: - fragment.setModel(MusicLibsAdapter.LIBS_MODEL_Tmp); - break; - } - } else { - System.out.println("debug 切换页面 没得页面"); - } - } - - @Override - public void onPageScrollStateChanged(int state) { + public int getItemCount() { + return fragments.size(); } }); + tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { + @Override + public void onTabSelected(TabLayout.Tab tab) { + fragments.get(tab.getPosition()).flash(); + } + + @Override + public void onTabUnselected(TabLayout.Tab tab) { + + } + + @Override + public void onTabReselected(TabLayout.Tab tab) { + fragments.get(tab.getPosition()).update(); + } + }); + TabLayoutMediator mediator=new TabLayoutMediator(tabLayout, viewPager, true, new TabLayoutMediator.TabConfigurationStrategy() { + @Override + public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) { + switch (position){ + case 0: + tab.setText("全部"); + break; + case 1: + tab.setText("专辑"); + break; + case 2: + tab.setText("收藏"); + break; + case 3: + tab.setText("队列"); + break; + } + } + }); + mediator.attach(); + + } private void setImage(Bitmap data) { @@ -198,21 +200,13 @@ public class PlayLibsActivity extends AppCompatActivity { } }); - if (MusicLibsFragment.map != null && MusicLibsFragment.map.containsKey(index)) - MusicLibsFragment.map.get(index).getAdapter().notifyDataSetChanged(); - - } + } @Override protected void onDestroy() { super.onDestroy(); browserHelper.onStop(); browserHelper = null; - for (Integer integer : MusicLibsFragment.map.keySet()) { - MusicLibsFragment.map.get(integer).onDestroy(); - } - MusicLibsFragment.map.clear(); - MusicLibsFragment.map = null; finish(); } diff --git a/app/src/main/res/layout/activity_play_list.xml b/app/src/main/res/layout/activity_play_list.xml index 44a18aa..17b19fa 100644 --- a/app/src/main/res/layout/activity_play_list.xml +++ b/app/src/main/res/layout/activity_play_list.xml @@ -14,46 +14,44 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> - + android:background="#F3F5F5" + app:layout_constraintTop_toBottomOf="@+id/include"> - + + + + + + + + + + app:layout_constraintTop_toBottomOf="@+id/tabLayout" /> + \ No newline at end of file