From e2019ded66e9a49279e7b051b50076debc4cd398 Mon Sep 17 00:00:00 2001 From: yutou Date: Wed, 30 Dec 2020 18:12:18 +0800 Subject: [PATCH] fix bugs add new bug --- .../Adapters/AlbumsRecyclerAdapter.java | 3 - .../Adapters/MusicLibsAdapter.java | 4 +- .../nas_music_player/Datas/MusicData.java | 14 ++- .../containers/MusicContainer.java | 12 +- .../nas_music_player/tools/AppTools.java | 5 +- .../nas_music_player/tools/NetworkTool.java | 116 +++++++++++++++--- .../views/Fragments/MusicLibsFragment.java | 3 + .../views/PlayLibsActivity.java | 10 +- 8 files changed, 136 insertions(+), 31 deletions(-) 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 ae44bea..e197888 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 @@ -86,9 +86,6 @@ public class AlbumsRecyclerAdapter extends RecyclerView.Adapter { } else { holder.collection.setVisibility(View.VISIBLE); } + if (!data.isDir()) { if (model != LIBS_MODEL_Album && model != LIBS_MODEL_Artist) { if (model == LIBS_MODEL_PLAY_Collection) { @@ -256,9 +257,6 @@ public class MusicLibsAdapter extends ArrayAdapter { holder.icon.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - System.out.println(data.getFile()); - System.out.println(data.getImageUrl()); - System.out.println(data.getImg(-1, -1)); if (data.getImg(-1, -1) != null) { Glide.with(getContext()).load(data.getImg(-1, -1)) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3))) 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 da14952..9738802 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 @@ -198,7 +198,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 AppTools.getUrlBase64(file); } @@ -236,7 +236,7 @@ public class MusicData { if (isDownloadImg) { return null; } - if (StringUtil.isEmpty(file)) { + if (StringUtil.isEmpty(file)&&StringUtil.isEmpty(title)&&StringUtil.isEmpty(album)) { img = BitmapFactory.decodeResource(MyApplication.application.getResources(), R.mipmap.ic_launcher); return img; } @@ -255,8 +255,14 @@ public class MusicData { img = null; isDownloadImg = true; JSONObject json = new JSONObject(); - json.put("fileName", getFile()); - NetworkTool.init().download(NetworkTool.NetworkAPI.MUSIC_IMAGE,json, "image_cache" + File.separator + imageSaveName,"post", new DownloadInterface() { + if(StringUtil.isEmpty(getAlbum())) { + json.put("fileName", getFile()); + }else{ + json.put("fileName",AppTools.getUrlBase64(getAlbum())); + json.put("type","album"); + } + System.out.println("下载图片:"+ NetworkTool.NetworkAPI.MUSIC_IMAGE+"?"+NetworkTool.toGetSplice(json)); + NetworkTool.init().downloadImage(NetworkTool.NetworkAPI.MUSIC_IMAGE,json, imageSaveName,"post", new DownloadInterface() { @Override public void onDownloadOver(File oldJar) { AppTools.saveBitmap(oldJar,imageSaveName); 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 6552f6c..b81c6e0 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 @@ -20,6 +20,7 @@ import com.yutou.nas_music_player.Datas.MusicData; 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.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; @@ -389,7 +390,12 @@ public class MusicContainer { } else { playIndex = 0; } - play(libs.mainData.get(playIndex)); + if(libs.mainData.isEmpty()){ + playOfAllRandom(); + AppTools.toast("当前播放列表为空,随机播放"); + }else { + play(libs.mainData.get(playIndex)); + } } else {//播放列表中的歌单 if (playList.size() > (playIndex + 1)) { playIndex++; @@ -583,6 +589,10 @@ public class MusicContainer { NetworkTool.init().httpGet(url, json, new NetworkInterface() { @Override public void httpGetData(Object data, int state) { + if(StringUtil.isEmpty((String)data)){ + networkInterface.httpGetData(new ArrayList<>(), 0); + return; + } JSONObject json = JSONObject.parseObject((String) data); if (json.getInteger("code") == 0) { if (json.getJSONArray("data").toJSONString().contains("md5")) { 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 f4ff58f..aca4c0b 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,6 +3,7 @@ package com.yutou.nas_music_player.tools; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.util.Base64; import android.widget.Toast; import com.kaopiz.kprogresshud.KProgressHUD; @@ -92,7 +93,9 @@ 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 void toast(String s) { AppData.handler.post(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 317f539..c26c1f6 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 @@ -20,8 +20,10 @@ import java.net.HttpURLConnection; import java.net.SocketTimeoutException; import java.net.URL; import java.net.URLEncoder; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Set; @@ -44,7 +46,7 @@ public class NetworkTool { 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"; + public static String MUSIC_IMAGE = HOME + "/music/web/image.do"; public static String MUSIC_PLAY = HOME + "/music/play.do"; public static String MUSIC_RANDOM = HOME + "/music/random.do"; @@ -61,14 +63,14 @@ public class NetworkTool { ping("http://192.168.31.88:8000/public/version.do", new NetworkInterface() { @Override public void httpGetData(Object data, int state) { - HOME="http://192.168.31.88:8000/nas"; + HOME = "http://192.168.31.88:8000/nas"; AppTools.toast("检测到局域网在线,使用局域网模式"); } @Override public void httpError(Exception e) { System.out.println("局域网不在线,走互联网"); - httpGet("http://tools.yutou233.cn/nas/music/getlocalhost.do?token="+HTTP_KEY, new JSONObject(), new NetworkInterface() { + httpGet("http://tools.yutou233.cn/nas/music/getlocalhost.do?token=" + HTTP_KEY, new JSONObject(), new NetworkInterface() { @Override public void httpGetData(Object data, int state) { try { @@ -102,23 +104,29 @@ public class NetworkTool { public void httpGet(String url, JSONObject body, NetworkInterface networkInterface) { httpPost(url, body, networkInterface); } - private void ping(String url,NetworkInterface networkInterface){ + + private void ping(String url, NetworkInterface networkInterface) { new Thread(new Runnable() { @Override public void run() { try { - HttpURLConnection connection = (HttpURLConnection) new URL(url ).openConnection(); + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + connection.setConnectTimeout(50); + connection.setReadTimeout(50); connection.connect(); - connection.setConnectTimeout(100); - connection.setReadTimeout(100); + InputStream inputStream = connection.getErrorStream(); + System.out.println(inputStream.read()); + inputStream.close(); System.out.println(connection.getResponseCode()); - networkInterface.httpGetData(null,0); + networkInterface.httpGetData(null, 0); + connection.disconnect(); } catch (Exception e) { networkInterface.httpError(e); } } }).start(); } + @Deprecated public void httpGet_old(final String url, final JSONObject body, final NetworkInterface networkInterface) { if (!url.startsWith("http:") && !StringUtil.isEmpty(HOME)) { @@ -281,7 +289,7 @@ public class NetworkTool { return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"; } - public String toGetSplice(JSONObject json) { + public static String toGetSplice(JSONObject json) { try { json.put("token", NetworkAPI.HTTP_KEY); } catch (JSONException e) { @@ -307,15 +315,93 @@ public class NetworkTool { return string.toString(); } - public void download(final String url, final String saveName) { - download(url,new JSONObject(), saveName,"GET", null); + public void download(final String url, final String saveName) { + download(url, new JSONObject(), saveName, "GET", null); } + private List downloadImageList = new ArrayList<>(); - public void download(final String url,final JSONObject body, final String saveName,String method, final DownloadInterface downloadInterface) { - System.out.println(">>>>>>>>>>"+url); + public synchronized void downloadImage(final String url, final JSONObject body, final String saveName, String method, final DownloadInterface downloadInterface) { + System.out.println(">>>>>>>>>>" + url); if (!url.startsWith("http:") && !StringUtil.isEmpty(HOME)) { - download(HOME+url,body,saveName,method,downloadInterface); + downloadImage(HOME + url, body, saveName, method, downloadInterface); + return; + } + if (downloadImageList.contains(url + "?" + toGetSplice(body))) { + return; + } else { + downloadImageList.add(url + "?" + toGetSplice(body)); + } + new Thread(new Runnable() { + @Override + public void run() { + File jar = null; + try { + File savePath = new File(downloadPath + saveName + "_tmp.tmp"); + if (!savePath.exists()) { + savePath.getParentFile().mkdirs(); + } + Log.i(TAG, "下载文件:" + url + "?" + toGetSplice(body) + " 保存文件:" + saveName); + HttpURLConnection connection = (HttpURLConnection) new URL(url + "?" + toGetSplice(body)).openConnection(); + + // Log.i(TAG,"获取到网络请求:"+connection.getResponseCode()); + if (method.toLowerCase().equals("post")) { + connection.setRequestMethod("POST"); + connection.setDoOutput(true); + connection.setDoInput(true); + connection.setConnectTimeout(5 * 1000); + connection.setReadTimeout(10 * 1000); + // connection.addRequestProperty("content-type", "application/json"); + connection.addRequestProperty("charset", "UTF-8"); + /* OutputStream outputStream = connection.getOutputStream(); + outputStream.write(body.toJSONString().getBytes()); + outputStream.flush(); + outputStream.close();*/ + } + + InputStream inputStream = connection.getInputStream(); + 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(); + } + if(!jar.exists()){ + jar.createNewFile(); + } + Log.i(NetworkTool.class, "临时保存文件:" + jar.getAbsolutePath()); + OutputStream outputStream = new FileOutputStream(jar); + byte[] bytes = new byte[1024]; + int len; + while ((len = inputStream.read(bytes)) > 0) { + outputStream.write(bytes, 0, len); + } + outputStream.close(); + inputStream.close(); + File oldJar = new File(downloadPath + saveName); + /*if (oldJar.exists()) { + oldJar.delete(); + }*/ + jar.renameTo(oldJar); + Log.i(NetworkTool.class, "实际保存:" + oldJar.getAbsolutePath() + " " + oldJar.getName()); + Log.toast(MyApplication.application, "下载完成,重启生效"); + + if (downloadInterface != null) { + downloadInterface.onDownloadOver(oldJar); + } + + } catch (Exception e) { + e.printStackTrace(); + if (downloadInterface != null) + downloadInterface.onError(e); + } + downloadImageList.remove(url + "?" + toGetSplice(body)); + } + }).start(); + } + + public void download(final String url, final JSONObject body, final String saveName, String method, final DownloadInterface downloadInterface) { + System.out.println(">>>>>>>>>>" + url); + if (!url.startsWith("http:") && !StringUtil.isEmpty(HOME)) { + download(HOME + url, body, saveName, method, downloadInterface); return; } new Thread(new Runnable() { @@ -331,7 +417,7 @@ public class NetworkTool { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); // Log.i(TAG,"获取到网络请求:"+connection.getResponseCode()); - if(method.toLowerCase().equals("post")){ + if (method.toLowerCase().equals("post")) { connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); 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 3deff4b..5bb4866 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 @@ -99,6 +99,7 @@ public class MusicLibsFragment extends Fragment { 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); @@ -152,6 +153,8 @@ public class MusicLibsFragment extends Fragment { if (browserHelper != null) { browserHelper.play(adapter.getItem(position)); adapter.notifyDataSetChanged(); + }else{ + System.out.println("browserHelper为空"); } } } 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 cbf499c..fd0eeca 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 @@ -57,12 +57,12 @@ public class PlayLibsActivity extends AppCompatActivity { getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS); super.onCreate(savedInstanceState); setContentView(R.layout.activity_play_list); + PlayListener playListener = new PlayListener(); + browserHelper = new MediaBrowserHelper(this); + browserHelper.regPlayListener(playListener); initView(); bar_pos = getIntent().getIntExtra("bar_pos", 0); setPlayData(MusicContainer.getInstance().getNowPlayData()); - browserHelper = new MediaBrowserHelper(this); - PlayListener playListener = new PlayListener(); - browserHelper.regPlayListener(playListener); handler = new Handler(); play.setOnClickListener(new View.OnClickListener() { @Override @@ -123,6 +123,9 @@ public class PlayLibsActivity extends AppCompatActivity { 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)); + for (MusicLibsFragment fragment : fragments) { + fragment.setPlayContainer(browserHelper); + } viewPager.setAdapter(new FragmentStateAdapter(this) { @NonNull @Override @@ -143,7 +146,6 @@ public class PlayLibsActivity extends AppCompatActivity { @Override public void onTabUnselected(TabLayout.Tab tab) { - } @Override