浏览代码

上传调试

guilin 1 月之前
父节点
当前提交
d41a659aa5

+ 17 - 1
app/src/main/java/com/fxy/common/AsyncFileUpload.java

@@ -18,6 +18,7 @@ import com.fxy.constant.EventCode;
 import com.fxy.constant.SPCache;
 import com.fxy.net.MyDialogCallback;
 import com.fxy.realm.AsyncUploadRealm;
+import com.fxy.utils.UploadUtils;
 import com.luck.picture.lib.entity.LocalMedia;
 import com.lzy.okgo.OkGo;
 import com.lzy.okgo.model.Response;
@@ -250,6 +251,16 @@ public class AsyncFileUpload {
         realmResults.load();
         return  realmResults.size();
     }
+    public int getAllAwaitNum(Integer[] pushStatus){
+
+        RealmResults<AsyncUploadRealm> realmResults  = mRealm.where(AsyncUploadRealm.class)
+                .equalTo("isDelete",0)
+                .in("pushStatus",pushStatus)
+                .sort("pushStatus").findAll();
+        realmResults.load();
+        return  realmResults.size();
+    }
+
 
     //存储数据问题
     private void asyncUpload(){
@@ -280,7 +291,12 @@ public class AsyncFileUpload {
 
     public void setStartUpload(){
         allowUpload = true;
-        asyncUpload();
+        if (UploadUtils.getUploadExists(mContext)){
+            asyncUpload();
+        }else{
+            XLog.d("AsyncFileUpload当前有任务正在上传");
+        }
+
     }
 
     public void setStopUpload(){

+ 7 - 6
app/src/main/java/com/fxy/common/AsyncPictureUpload.java

@@ -106,7 +106,7 @@ public class AsyncPictureUpload {
     private boolean allowUpload = true;
 
     //
-    private boolean isAllRe =false;
+    private boolean isAllRePush =false;
 
     private boolean debug = false;
     public void setDebug(boolean debug) {
@@ -186,7 +186,7 @@ public class AsyncPictureUpload {
      */
     public int getAllAwaitNum(){
 
-        if (isAllRe){
+        if (isAllRePush){
             Integer[] pushStatus = {0,1};
             RealmResults<AsyncUploadRealm> realmResults  = mRealm.where(AsyncUploadRealm.class)
                     .equalTo("isDelete",0)
@@ -390,6 +390,7 @@ public class AsyncPictureUpload {
     //存储数据问题
     private void asyncUpload(){
 
+
         if (!getIsNetwork()){
             //无网络时在请求检查是否有网络
             return;
@@ -462,8 +463,8 @@ public class AsyncPictureUpload {
         allowUpload = true;
         asyncUpload();
     }
-    public void setIsAllRe(){
-        isAllRe = true;
+    public void setIsAllRePush(){
+        isAllRePush = true;
     }
 
     public void setStopUpload(){
@@ -508,13 +509,13 @@ public class AsyncPictureUpload {
         RealmQuery<AsyncUploadRealm> queryRealm =  mRealm.where(AsyncUploadRealm.class)
                 .in("isFinish",isFinish)
                 .equalTo("isDelete",0);
-        if (isAllRe){
+        if (isAllRePush){
             Integer[] pushStatus = {0,1};
             queryRealm.in("pushStatus",pushStatus)
 //                    .equalTo("notifyKey",fileKey)
 //                    .equalTo("uploadType",uploadType)
                     ;
-            isAllRe = false;
+            isAllRePush = false;
         }else{
             queryRealm.equalTo("pushStatus",0)
 //                    .equalTo("notifyKey",fileKey)

+ 4 - 0
app/src/main/java/com/fxy/common/OssService.java

@@ -26,6 +26,7 @@ import com.fxy.BuildConfig;
 import com.fxy.constant.SPCache;
 import com.fxy.net.Urls;
 import com.fxy.putIn.PutinPhotosMainActivity;
+import com.fxy.utils.UploadUtils;
 
 import org.json.JSONObject;
 
@@ -151,9 +152,11 @@ public class OssService {
             public void uncaughtException(Thread t, Throwable e) {
                 // 在这里处理线程抛出的异常,并使用传递的参数进行操作
                 XLog.e("oss上传出现异常",e.getMessage());
+                UploadUtils.delUploadTime(mContext);
             }
         });
         thread.start();
+        UploadUtils.setUploadTime(mContext);
 
 
 
@@ -203,6 +206,7 @@ public class OssService {
 
             XLog.e("开始执行上传"+getFilePath()+objectKey+"上传的目录是:"+urlPath);
 
+
             // 设置进度回调
             put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
                 @Override

+ 1 - 1
app/src/main/java/com/fxy/common/UploadDialog.java

@@ -36,7 +36,7 @@ public class UploadDialog {
 
     private void initData(){
         asyncPictureUpload = new AsyncPictureUpload(Objects.requireNonNull(mActivity));
-        asyncPictureUpload.setIsAllRe();
+        asyncPictureUpload.setIsAllRePush();
         progressDialog = new ProgressDialog(mActivity);
         progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
             @Override

+ 13 - 3
app/src/main/java/com/fxy/service/UploadJobService.java

@@ -6,6 +6,7 @@ import android.content.Context;
 
 import com.elvishew.xlog.XLog;
 import com.fxy.base.UploadContentProvider;
+import com.fxy.baselibrary.language.other.CommSharedUtil;
 import com.fxy.common.AsyncFileUpload;
 
 public class UploadJobService  extends JobService {
@@ -25,13 +26,20 @@ public class UploadJobService  extends JobService {
         XLog.d("------------执行异步上传图片--------------------"+jobParameters.getJobId());
         asyncFileUpload = new AsyncFileUpload(context);
 
-        asyncFileUpload.setStartUpload();
-        int awaitNum = asyncFileUpload.getAllAwaitNum();
+
+        Integer[] pushStatus = {0,1};
+
+        int awaitNum = asyncFileUpload.getAllAwaitNum(pushStatus);
         if (awaitNum == 0){
             // 标记任务为已完成(如果你不需要重试)
             jobFinished(jobParameters, false);
+            return false;
+        }else{
+            asyncFileUpload.setStartUpload();
+            jobFinished(jobParameters, true);
+            return true;
         }
-        return false;
+
     }
 
     @Override
@@ -43,4 +51,6 @@ public class UploadJobService  extends JobService {
     }
 
 
+
+
 }

+ 4 - 4
app/src/main/java/com/fxy/utils/JobUtils.java

@@ -47,8 +47,8 @@ public class JobUtils {
 
         // 其他设置,如网络条件、延迟等
         builder.setMinimumLatency(1000 * 60 * leisure); // 延迟至少60秒
-//        builder.setMinimumLatency(1000); // 延迟至少60秒
-        builder.setOverrideDeadline(2*60 * 1000 * 60); // 最多延迟2小时
+//        builder.setMinimumLatency(10); // 延迟至少60秒
+        builder.setOverrideDeadline(4*60 * 1000 * 60); // 最多延迟2小时
 
         JobInfo jobInfo = builder.build();
 
@@ -87,8 +87,8 @@ public class JobUtils {
 
 
         // 其他设置,如网络条件、延迟等
-        builder.setMinimumLatency(1000 * 30); // 延迟至少60秒
-//        builder.setMinimumLatency(1000); // 延迟至少60秒
+        builder.setMinimumLatency(1000 * 90); // 延迟至少60秒
+        //builder.setMinimumLatency(1000); // 延迟至少60秒
         builder.setOverrideDeadline(2*60 * 1000 * 60); // 最多延迟2分钟
 
         JobInfo jobInfo = builder.build();

+ 55 - 1
app/src/main/java/com/fxy/utils/UploadUtils.java

@@ -1,12 +1,18 @@
 package com.fxy.utils;
 
+import android.content.Context;
+
+import com.fxy.baselibrary.language.other.CommSharedUtil;
 import com.fxy.realm.AsyncUploadRealm;
 
+import java.util.Calendar;
+
 import io.realm.Realm;
 import io.realm.RealmResults;
 
 public class UploadUtils {
-    public static int getAwaitNum(Realm mRealm,String uploadType){
+    public static String currentUploadTimeTag="oss_current_upload_time";
+     public static int getAwaitNum(Realm mRealm,String uploadType){
         RealmResults<AsyncUploadRealm> realmResults  = mRealm.where(AsyncUploadRealm.class)
                 .equalTo("isDelete",0)
                 .equalTo("pushStatus",0)
@@ -24,4 +30,52 @@ public class UploadUtils {
         realmResults.load();
         return  realmResults.size();
     }
+    public static long getCurrentTime(){
+        Calendar calendar = Calendar.getInstance();
+        return calendar.getTimeInMillis();
+    }
+
+    /**
+     * 计算两个时间戳之间的差异,并返回以秒为单位的结果。
+     *
+     * @param timestamp1 第一个时间戳(毫秒)
+     * @param timestamp2 第二个时间戳(毫秒)
+     * @return 差异的秒数
+     */
+    public static long calculateDifferenceInSeconds(long timestamp1, long timestamp2) {
+        // 计算两个时间戳之间的差异(毫秒)
+        long differenceInMillis = Math.abs(timestamp1 - timestamp2);
+
+        // 将差异转换为秒
+        return differenceInMillis / 1000;
+    }
+
+    public static boolean getUploadExists(Context context){
+       String  currentUploadTimeStr = CommSharedUtil.getInstance(context).getString(currentUploadTimeTag,"");
+
+       if (currentUploadTimeStr.isEmpty()){
+           return true;
+       }
+        long diff = calculateDifferenceInSeconds( getCurrentTime() , Long.getLong(currentUploadTimeStr));
+        return diff >= 60 * 5;
+    }
+
+    /**
+     * 设置上传时间
+     * @param context Context
+     */
+    public static void setUploadTime(Context context){
+        CommSharedUtil.getInstance(context).putString(currentUploadTimeTag,String.valueOf(getCurrentTime()));
+    }
+
+    /**
+     * 删除上传时间
+     * @param context Context
+     * @return boolean
+     */
+    public static boolean delUploadTime(Context context){
+        CommSharedUtil.getInstance(context).remove(currentUploadTimeTag);
+        return true;
+    }
+
 }