Browse Source

<优化广播,防止多次重复下载>

weizhengliang 4 năm trước cách đây
mục cha
commit
28b0b9cb1a

+ 7 - 1
app/src/main/java/com/wdkl/callingbed2/ui/CallingBedActivity.java

@@ -1860,6 +1860,7 @@ public class CallingBedActivity extends BaseActivity implements ISerialPortBedOn
                         break;
                         break;
                     case "broadcast_1":
                     case "broadcast_1":
                         boolean isPlay = false;
                         boolean isPlay = false;
+                        needReDownload = false;
                         handler.removeMessages(RESET_BROADCAST_VIEW);
                         handler.removeMessages(RESET_BROADCAST_VIEW);
                         if (isDownloadMp3) {
                         if (isDownloadMp3) {
                             tvBroadcasting.setVisibility(View.VISIBLE);
                             tvBroadcasting.setVisibility(View.VISIBLE);
@@ -2802,6 +2803,9 @@ public class CallingBedActivity extends BaseActivity implements ISerialPortBedOn
         Constants.INTIME_BROADCAST = "";
         Constants.INTIME_BROADCAST = "";
         handler.removeMessages(RESET_BROADCAST_VIEW);
         handler.removeMessages(RESET_BROADCAST_VIEW);
         handler.sendEmptyMessageDelayed(RESET_BROADCAST_VIEW, 30000);
         handler.sendEmptyMessageDelayed(RESET_BROADCAST_VIEW, 30000);
+
+        needReDownload = true;
+        handler.sendEmptyMessage(UPDATE_BROADCAST);
     }
     }
 
 
     private float mVolume = 1.0f;
     private float mVolume = 1.0f;
@@ -2903,6 +2907,7 @@ public class CallingBedActivity extends BaseActivity implements ISerialPortBedOn
     //广播的实体
     //广播的实体
     private ArrayList<AllBroadcastEntity> allBroadcastList = new ArrayList<>();
     private ArrayList<AllBroadcastEntity> allBroadcastList = new ArrayList<>();
     private boolean isDownloadMp3;
     private boolean isDownloadMp3;
+    private boolean needReDownload = false;
 
 
     //下载广播音频文件
     //下载广播音频文件
     private void getBroadcastAudioFiles() {
     private void getBroadcastAudioFiles() {
@@ -2991,7 +2996,7 @@ public class CallingBedActivity extends BaseActivity implements ISerialPortBedOn
             AllBroadcastEntity entity = allBroadcastList.get(index);
             AllBroadcastEntity entity = allBroadcastList.get(index);
             String filePath = entity.getFileName();
             String filePath = entity.getFileName();
             DownloadUtil2.getInstance().download(Constants.URL + Constants.URL_END + "/" + filePath,
             DownloadUtil2.getInstance().download(Constants.URL + Constants.URL_END + "/" + filePath,
-                    Constants.BROADCAST_AUDIO_PATH, filePath.substring(filePath.lastIndexOf("/") + 1), new DownloadUtil2.OnDownloadListener() {
+                    Constants.BROADCAST_AUDIO_PATH, filePath.substring(filePath.lastIndexOf("/") + 1), needReDownload, new DownloadUtil2.OnDownloadListener() {
                         @Override
                         @Override
                         public void onDownloadSuccess() {
                         public void onDownloadSuccess() {
                             LogUtil.d("broadcast", "download mp3 finish...");
                             LogUtil.d("broadcast", "download mp3 finish...");
@@ -3010,6 +3015,7 @@ public class CallingBedActivity extends BaseActivity implements ISerialPortBedOn
                         }
                         }
                     });
                     });
         } else {
         } else {
+            //下载完成
             isDownloadMp3 = false;
             isDownloadMp3 = false;
         }
         }
     }
     }

+ 27 - 5
app/src/main/java/com/wdkl/callingbed2/util/DownloadUtil2.java

@@ -37,11 +37,25 @@ public class DownloadUtil2 {
      * @param url      下载连接
      * @param url      下载连接
      * @param listener 下载监听
      * @param listener 下载监听
      */
      */
-    public void download(final String url,final String dir, final String name, final OnDownloadListener listener) {
+    public void download(final String url,final String dir, final String name, boolean redownload, final OnDownloadListener listener) {
+        if (!redownload) {
+            File file = new File(dir + "/" + name);
+            if (file.exists()) {
+                LogUtil.d(TAG, "file exist==" + file.getPath());
+                if (listener != null) {
+                    listener.onDownloadSuccess(); // 下载完成
+                }
+                return;
+            }
+        }
+
         final String fileDir = isHaveExistDir(new File(dir), new File(dir + "/" + name));
         final String fileDir = isHaveExistDir(new File(dir), new File(dir + "/" + name));
         LogUtil.d(TAG, "url==" + url + ", fileDir==" + fileDir);
         LogUtil.d(TAG, "url==" + url + ", fileDir==" + fileDir);
         if (fileDir == null) {
         if (fileDir == null) {
             ToastUtil.showToast("下载文件路径创建失败");
             ToastUtil.showToast("下载文件路径创建失败");
+            if (listener != null) {
+                listener.onDownloadFailed(); // 下载失败
+            }
             return;
             return;
         }
         }
         Request request = new Request.Builder().url(url).build();
         Request request = new Request.Builder().url(url).build();
@@ -49,7 +63,9 @@ public class DownloadUtil2 {
             @Override
             @Override
             public void onFailure(Call call, IOException e) {
             public void onFailure(Call call, IOException e) {
                 LogUtil.d(TAG, "onFailure==" + e.toString());
                 LogUtil.d(TAG, "onFailure==" + e.toString());
-                listener.onDownloadFailed(); // 下载失败
+                if (listener != null) {
+                    listener.onDownloadFailed(); // 下载失败
+                }
             }
             }
 
 
             @Override
             @Override
@@ -72,13 +88,19 @@ public class DownloadUtil2 {
                         float sp = (float) sum / (float) total;
                         float sp = (float) sum / (float) total;
                         int progress = (int) (sp * 100);
                         int progress = (int) (sp * 100);
                         //LogUtil.d(TAG, "progress==" + progress);
                         //LogUtil.d(TAG, "progress==" + progress);
-                        listener.onDownloading(progress);// 下载中
+                        if (listener != null) {
+                            listener.onDownloading(progress);// 下载中
+                        }
                     }
                     }
                     fos.flush();
                     fos.flush();
-                    listener.onDownloadSuccess(); // 下载完成
+                    if (listener != null) {
+                        listener.onDownloadSuccess(); // 下载完成
+                    }
                 } catch (Exception e) {
                 } catch (Exception e) {
                     LogUtil.d(TAG, "Exception==");
                     LogUtil.d(TAG, "Exception==");
-                    listener.onDownloadFailed();
+                    if (listener != null) {
+                        listener.onDownloadFailed();
+                    }
                 } finally {
                 } finally {
                     try {
                     try {
                         if (is != null)
                         if (is != null)