2 Commits b4bd7b26f1 ... 27a3a6f7be

Autor SHA1 Mensagem Data
  guilin 27a3a6f7be Merge branch 'master' into master_925 1 mês atrás
  guilin 2d5af115b6 异步上传图片优化 1 mês atrás

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

@@ -46,7 +46,8 @@ public class AsyncFileUpload {
     protected Realm mRealm;
     private List<AsyncUploadRealm> awaitUploadList=new ArrayList<>();
 
-
+    //全部重推
+    private boolean isAllRePush =false;
 
 
     private OssService ossService;
@@ -72,7 +73,9 @@ public class AsyncFileUpload {
         this.fileKey = fileKey;
     }
 
-
+    public void setIsAllRePush(){
+        isAllRePush = true;
+    }
 
     @SuppressLint("HandlerLeak")
     private Handler uploadHandler = new Handler() {
@@ -292,6 +295,7 @@ public class AsyncFileUpload {
     public void setStartUpload(){
         allowUpload = true;
         if (UploadUtils.getUploadExists(mContext)){
+            UploadUtils.delInterfaceRequestTime(mContext);
             asyncUpload();
         }else{
             XLog.d("AsyncFileUpload当前有任务正在上传");
@@ -382,10 +386,13 @@ public class AsyncFileUpload {
 
         }
 
+        allowUpload = UploadUtils.getUploadIsContinue(mContext);
         //可以允许上传
         if (allowUpload){
             //上传完,继续执行
             asyncUpload();
+        }else{
+            awaitUploadList.clear();
         }
 
     }
@@ -400,6 +407,14 @@ public class AsyncFileUpload {
         mRealm.commitTransaction();
     }
 
+    /**
+     * 获取待上传数量
+     * @return
+     */
+    public int getAwaitUploadListNum(){
+        return  awaitUploadList.size();
+    }
+
     //销毁
     public void removeHandler(){
         if (uploadHandler!=null){

+ 3 - 0
app/src/main/java/com/fxy/common/AsyncPictureUpload.java

@@ -32,6 +32,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.fxy.view.FullyGridLayoutManager;
 import com.google.gson.Gson;
 import com.luck.picture.lib.PictureSelector;
@@ -464,6 +465,7 @@ public class AsyncPictureUpload {
             asyncUpload();
         }
         allowUpload = true;
+        UploadUtils.delInterfaceRequestTime(mContext);
     }
     public void setIsAllRePush(){
         isAllRePush = true;
@@ -584,6 +586,7 @@ public class AsyncPictureUpload {
 
         }
 
+        allowUpload = UploadUtils.getUploadIsContinue(mContext);
         //可以允许上传
         if (allowUpload){
             //上传完,继续执行

+ 0 - 1
app/src/main/java/com/fxy/detection/BoxPutInDetailsMainActivity.java

@@ -38,7 +38,6 @@ import com.fxy.baselibrary.views.MarqueeTextView;
 import com.fxy.bean.ActionBean;
 import com.fxy.bean.ScanBean;
 import com.fxy.common.AsyncPictureSelector;
-import com.fxy.common.AsyncPictureUpload;
 import com.fxy.common.UploadProgressEvent;
 import com.fxy.constant.BaseConfig;
 import com.fxy.constant.EventCode;

+ 0 - 3
app/src/main/java/com/fxy/detection/BoxPutInMainActivity.java

@@ -35,7 +35,6 @@ import com.fxy.baselibrary.views.MarqueeTextView;
 import com.fxy.bean.ActionBean;
 import com.fxy.bean.ScanBean;
 import com.fxy.common.AsyncPictureSelector;
-import com.fxy.common.AsyncPictureUpload;
 import com.fxy.common.CommonDialog;
 import com.fxy.constant.BaseConfig;
 import com.fxy.constant.EventCode;
@@ -118,8 +117,6 @@ public class BoxPutInMainActivity extends  BaseActivity implements StoJNI.ScanCa
 
     private HashMap<String, AsyncPictureSelector> mapPictureSelector = new HashMap<>();
 
-    private List<TakePictureBean> takePictureBeanList = new ArrayList<>();
-    private AsyncPictureUpload asyncPictureUpload = new AsyncPictureUpload(this);
 
 
     private String currentPictureType= "label_img";

+ 4 - 59
app/src/main/java/com/fxy/detection/TrayMakeFourMainActivity.java

@@ -36,7 +36,6 @@ import com.fxy.baselibrary.views.MarqueeTextView;
 import com.fxy.bean.ActionBean;
 import com.fxy.bean.ScanBean;
 import com.fxy.common.AsyncPictureSelector;
-import com.fxy.common.AsyncPictureUpload;
 import com.fxy.constant.BaseConfig;
 import com.fxy.constant.EventCode;
 import com.fxy.constant.SPCache;
@@ -46,6 +45,7 @@ import com.fxy.detection.bean.TrackingBoxBean;
 import com.fxy.detection.bean.TrayMakeBean;
 import com.fxy.net.MyDialogCallback;
 import com.fxy.net.Urls;
+import com.fxy.utils.UploadUtils;
 import com.google.gson.Gson;
 import com.google.zxing.Result;
 import com.luck.picture.lib.config.PictureConfig;
@@ -122,7 +122,6 @@ public class TrayMakeFourMainActivity extends  BaseActivity implements StoJNI.Sc
     private HashMap<String, Object> postData  = new HashMap<>();
 
 
-     private AsyncPictureUpload asyncPictureUpload = new AsyncPictureUpload(this);
 
 
     private String currentPictureType= "";
@@ -141,11 +140,8 @@ public class TrayMakeFourMainActivity extends  BaseActivity implements StoJNI.Sc
 
     private JSONObject postParam = new JSONObject();
 
-    private int leisure = 10;
 
 
-    private Handler workHandler;
-    private Runnable idleRunnable;
 
 
     private View notDataView;   //没有数据显示页
@@ -233,37 +229,10 @@ public class TrayMakeFourMainActivity extends  BaseActivity implements StoJNI.Sc
         totalAwaitNum();
 
 
-        workHandler = new Handler(Looper.getMainLooper());
-        // 定义空闲任务
-        idleRunnable = new Runnable() {
-            @Override
-            public void run() {
-                // 执行空闲后的操作
-                operateStartUpload();
-            }
-        };
-
-        SPCache spCache = SPCache.getInstance(this);
-        leisure = spCache.getLeisure();
 
-        // 初始化时设置计时器
-        workHandler.postDelayed(idleRunnable, leisure * 60 * 1000);
     }
 
 
-    private void setNewTiming(){
-        try {
-            // 取消之前的计时器
-            workHandler.removeCallbacks(idleRunnable);
-            // 重新计时
-            workHandler.postDelayed(idleRunnable, leisure * 60 * 1000); // 2 分钟
-        } catch (Exception e) {
-            e.printStackTrace();
-
-        }
-
-    }
-
 
 
     /**
@@ -448,17 +417,12 @@ public class TrayMakeFourMainActivity extends  BaseActivity implements StoJNI.Sc
 
     //getAwaitNum
     private void totalAwaitNum(){
-        int awaitNum = asyncPictureUpload.getAwaitNum();
-        tvAwaitNum.setText(String.valueOf(awaitNum));
-        if (awaitNum == 0){
-            operateStopUpload();
-        }
+        tvAwaitNum.setText(String.valueOf(UploadUtils.getAwaitNum(mRealm,getClass().getSimpleName())));
     }
 
     //统一按钮回调
     @SuppressLint("NonConstantResourceId")
     public void callbackEditor(View v){
-        setNewTiming();
         switch (v.getId()) {
 
             case R.id.et_unit_code:
@@ -581,13 +545,7 @@ public class TrayMakeFourMainActivity extends  BaseActivity implements StoJNI.Sc
     }
 
 
-    //检查是否存在上传
-    private void checkIsUpload(){
-        int awaitNum = asyncPictureUpload.getAwaitNum();
-        if (currentUploadStatus  && awaitNum>0){
-            operateStartUpload();
-        }
-    }
+
 
 
 
@@ -614,7 +572,6 @@ public class TrayMakeFourMainActivity extends  BaseActivity implements StoJNI.Sc
 
 
     private void checkUnitCode(){
-        checkIsUpload();
         if (etUnitCode.getText().toString().isEmpty()){
             showErrorToast(etUnitCode.getHint().toString());
             return;
@@ -755,25 +712,13 @@ public class TrayMakeFourMainActivity extends  BaseActivity implements StoJNI.Sc
         }
     }
 
-    private void operateStopUpload(){
-        if (currentUploadStatus){
-            asyncPictureUpload.setStopUpload();
-        }
-    }
-    private void operateStartUpload(){
-        if (!currentUploadStatus){
-            asyncPictureUpload.setStartUpload();
-        }
-    }
+
 
 
     @Override
     protected void onDestroy() {
         unbinder.unbind();
-        asyncPictureUpload.removeHandler();
 
-        // 确保清除所有的回调以防止内存泄漏
-        workHandler.removeCallbacks(idleRunnable);
         super.onDestroy();
     }
     @Override

+ 3 - 52
app/src/main/java/com/fxy/detection/TrayMakeSecondMainActivity.java

@@ -32,7 +32,6 @@ import com.fxy.baselibrary.views.MarqueeTextView;
 import com.fxy.bean.ActionBean;
 import com.fxy.bean.ScanBean;
 import com.fxy.common.AsyncPictureSelector;
-import com.fxy.common.AsyncPictureUpload;
 import com.fxy.constant.BaseConfig;
 import com.fxy.constant.EventCode;
 import com.fxy.constant.SPCache;
@@ -41,6 +40,7 @@ import com.fxy.detection.bean.TrackingBoxBean;
 import com.fxy.detection.bean.TrayMakeBean;
 import com.fxy.net.MyDialogCallback;
 import com.fxy.net.Urls;
+import com.fxy.utils.UploadUtils;
 import com.google.gson.Gson;
 import com.google.zxing.Result;
 import com.luck.picture.lib.config.PictureConfig;
@@ -95,7 +95,6 @@ public class TrayMakeSecondMainActivity extends  BaseActivity implements StoJNI.
     private HashMap<String, AsyncPictureSelector> mapPictureSelector = new HashMap<>();
 
     private List<TakePictureBean> takePictureBeanList = new ArrayList<>();
-    private AsyncPictureUpload asyncPictureUpload = new AsyncPictureUpload(this);
 
     private TrayMakeBean trayMakeBean;
 
@@ -120,12 +119,8 @@ public class TrayMakeSecondMainActivity extends  BaseActivity implements StoJNI.
 
     private JSONObject postParam = new JSONObject();
 
-    private int leisure = 10;
 
 
-    private Handler workHandler;
-    private Runnable idleRunnable;
-
     @Override
     public int getContentViewResId() {
         return R.layout.activity_fxy_detection_tray_make_second;
@@ -166,7 +161,6 @@ public class TrayMakeSecondMainActivity extends  BaseActivity implements StoJNI.
                     mapPictureSelector.get(currentPictureType).getSelectImg(data);
                 }
                 totalAwaitNum();
-                setNewTiming();
                 for (int i=0;i<takePictureBeanList.size();i++){
                     int num = takePictureBeanList.get(i).getNumber();
                     String field = takePictureBeanList.get(i).getField();
@@ -248,36 +242,9 @@ public class TrayMakeSecondMainActivity extends  BaseActivity implements StoJNI.
 
         totalAwaitNum();
 
-        workHandler = new Handler(Looper.getMainLooper());
-        // 定义空闲任务
-        idleRunnable = new Runnable() {
-            @Override
-            public void run() {
-                // 执行空闲后的操作
-                operateStartUpload();
-            }
-        };
-
-        SPCache spCache = SPCache.getInstance(this);
-        leisure = spCache.getLeisure();
-
-        // 初始化时设置计时器
-        workHandler.postDelayed(idleRunnable, leisure * 60 * 1000);
     }
 
 
-    private void setNewTiming(){
-        try {
-            // 取消之前的计时器
-            workHandler.removeCallbacks(idleRunnable);
-            // 重新计时
-            workHandler.postDelayed(idleRunnable, leisure * 60 * 1000); // 2 分钟
-        } catch (Exception e) {
-            e.printStackTrace();
-
-        }
-
-    }
 
 
     //设置返回
@@ -459,17 +426,14 @@ public class TrayMakeSecondMainActivity extends  BaseActivity implements StoJNI.
 
     //getAwaitNum
     private void totalAwaitNum(){
-        int awaitNum = asyncPictureUpload.getAwaitNum();
+        int awaitNum = UploadUtils.getAwaitNum(mRealm,getClass().getSimpleName());
         tvAwaitNum.setText(String.valueOf(awaitNum));
-        if (awaitNum == 0){
-            operateStopUpload();
-        }
+
     }
 
     //统一按钮回调
     @SuppressLint("NonConstantResourceId")
     public void callbackEditor(View v){
-        setNewTiming();
         switch (v.getId()) {
 
             default:
@@ -698,16 +662,6 @@ public class TrayMakeSecondMainActivity extends  BaseActivity implements StoJNI.
         }
     }
 
-    private void operateStopUpload(){
-        if (currentUploadStatus){
-            asyncPictureUpload.setStopUpload();
-        }
-    }
-    private void operateStartUpload(){
-        if (!currentUploadStatus){
-            asyncPictureUpload.setStartUpload();
-        }
-    }
 
 
 
@@ -716,9 +670,6 @@ public class TrayMakeSecondMainActivity extends  BaseActivity implements StoJNI.
     protected void onDestroy() {
         unbinder.unbind();
 
-        asyncPictureUpload.removeHandler();
-        // 确保清除所有的回调以防止内存泄漏
-        workHandler.removeCallbacks(idleRunnable);
         super.onDestroy();
     }
     @Override

+ 3 - 96
app/src/main/java/com/fxy/detection/TrayPutInMainActivity.java

@@ -31,7 +31,6 @@ import com.fxy.baselibrary.util.JsonUtil;
 import com.fxy.bean.ActionBean;
 import com.fxy.bean.ScanBean;
 import com.fxy.common.AsyncPictureSelector;
-import com.fxy.common.AsyncPictureUpload;
 import com.fxy.constant.BaseConfig;
 import com.fxy.constant.EventCode;
 import com.fxy.constant.SPCache;
@@ -39,6 +38,7 @@ import com.fxy.detection.bean.TakePictureBean;
 import com.fxy.detection.bean.TrackingBoxBean;
 import com.fxy.net.MyDialogCallback;
 import com.fxy.net.Urls;
+import com.fxy.utils.UploadUtils;
 import com.google.gson.Gson;
 import com.google.zxing.Result;
 import com.luck.picture.lib.config.PictureConfig;
@@ -86,7 +86,6 @@ public class TrayPutInMainActivity extends  BaseActivity implements StoJNI.ScanC
 
     private HashMap<String, AsyncPictureSelector> mapPictureSelector = new HashMap<>();
 
-    private AsyncPictureUpload asyncPictureUpload = new AsyncPictureUpload(this);
 
     private TrackingBoxBean trackingBoxBean;
 
@@ -114,9 +113,6 @@ public class TrayPutInMainActivity extends  BaseActivity implements StoJNI.ScanC
     private int leisure = 10;
 
 
-    private Handler workHandler;
-    private Runnable idleRunnable;
-
     @Override
     public int getContentViewResId() {
         return R.layout.activity_fxy_detection_tray_putin;
@@ -156,9 +152,6 @@ public class TrayPutInMainActivity extends  BaseActivity implements StoJNI.ScanC
                     mapPictureSelector.get(currentPictureType).getSelectImg(data);
                 }
                 totalAwaitNum();
-                setNewTiming();
-
-
 
             }
         }
@@ -217,36 +210,10 @@ public class TrayPutInMainActivity extends  BaseActivity implements StoJNI.ScanC
 
         totalAwaitNum();
 
-        workHandler = new Handler(Looper.getMainLooper());
-        // 定义空闲任务
-        idleRunnable = new Runnable() {
-            @Override
-            public void run() {
-                // 执行空闲后的操作
-                operateStartUpload();
-            }
-        };
-
-        SPCache spCache = SPCache.getInstance(this);
-        leisure = spCache.getLeisure();
 
-        // 初始化时设置计时器
-        workHandler.postDelayed(idleRunnable, leisure * 60 * 1000);
     }
 
 
-    private void setNewTiming(){
-        try {
-            // 取消之前的计时器
-            workHandler.removeCallbacks(idleRunnable);
-            // 重新计时
-            workHandler.postDelayed(idleRunnable, leisure * 60 * 1000); // 2 分钟
-        } catch (Exception e) {
-            e.printStackTrace();
-
-        }
-
-    }
 
 
 
@@ -404,17 +371,14 @@ public class TrayPutInMainActivity extends  BaseActivity implements StoJNI.ScanC
 
     //getAwaitNum
     private void totalAwaitNum(){
-        int awaitNum = asyncPictureUpload.getAwaitNum();
+        int awaitNum = UploadUtils.getAwaitNum(mRealm,getClass().getSimpleName());
         tvAwaitNum.setText(String.valueOf(awaitNum));
-        if (awaitNum == 0){
-            operateStopUpload();
-        }
+
     }
 
     //统一按钮回调
     @SuppressLint("NonConstantResourceId")
     public void callbackEditor(View v){
-        setNewTiming();
         switch (v.getId()) {
             case R.id.et_tracking_number:
                 getTrackingNumber();
@@ -692,69 +656,12 @@ public class TrayPutInMainActivity extends  BaseActivity implements StoJNI.ScanC
         }
     }
 
-    private void operateStopUpload(){
-        if (currentUploadStatus){
-            asyncPictureUpload.setStopUpload();
-        }
-    }
-    private void operateStartUpload(){
-        if (!currentUploadStatus){
-            asyncPictureUpload.setStartUpload();
-        }
-    }
-
-    protected void showBack() {
-        super.showBack();
-        tb_iv_left.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-
-                int num = asyncPictureUpload.getAwaitNum();
-
-                if (num>0){
-                    new AlertDialog.Builder(mContext).setTitle(R.string.dl_hint)
-                            .setMessage("返回会取消上传还有"+num+"条数据未上传,确定要返回吗")
-                            .setNegativeButton(R.string.str_cancel, new DialogInterface.OnClickListener() {
-                                @Override
-                                public void onClick(DialogInterface dialogInterface, int i) {
-                                    if (!currentUploadStatus){
-                                        operateStartUpload();
-                                    }
-                                    dialogInterface.dismiss();
-                                }
-                            }).setPositiveButton(R.string.str_submit, new DialogInterface.OnClickListener() {
-
-                        @Override
-                        public void onClick(DialogInterface dialogInterface, int i) {
-                                if (isShowBackDialog) {
-                                    onBackPressedSupport();
-                                } else {
-                                    finish();
-                                }
-                        }
-                    }).show();
-
-                }else{
-                    if (isShowBackDialog) {
-                        onBackPressedSupport();
-                    } else {
-                        finish();
-                    }
-                }
-
-            }
-        });
-    }
-
 
 
     @Override
     protected void onDestroy() {
         unbinder.unbind();
 
-        asyncPictureUpload.removeHandler();
-        // 确保清除所有的回调以防止内存泄漏
-        workHandler.removeCallbacks(idleRunnable);
         super.onDestroy();
     }
     @Override

+ 0 - 1
app/src/main/java/com/fxy/main/fragment/MineFragment.java

@@ -21,7 +21,6 @@ import android.widget.TextView;
 
 import com.elvishew.xlog.XLog;
 import com.fxy.baselibrary.bean.BaseEventBusBean;
-import com.fxy.common.AsyncPictureUpload;
 import com.fxy.common.UploadDialog;
 import com.fxy.constant.EventCode;
 import com.fxy.utils.CompressUtil;

+ 3 - 0
app/src/main/java/com/fxy/net/MyDialogCallback.java

@@ -9,6 +9,7 @@ import android.widget.LinearLayout;
 
 import com.fxy.BuildConfig;
 import com.elvishew.xlog.XLog;
+import com.fxy.utils.UploadUtils;
 import com.lzy.okgo.callback.StringCallback;
 import com.lzy.okgo.model.Response;
 import com.lzy.okgo.request.base.Request;
@@ -99,6 +100,8 @@ public abstract class MyDialogCallback extends StringCallback {
         // 请求是否携带token
         if (needToken) {
             request.getHeaders().put("token",SPCache.getInstance(context).getToken());
+            //记录本次请求时间
+            UploadUtils.setInterfaceRequestTime(context);
         }
         XLog.d(request.getUrl() + request.getParams());
         //网络请求前显示对话框

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

@@ -12,6 +12,7 @@ import io.realm.RealmResults;
 
 public class UploadUtils {
     public static String currentUploadTimeTag="oss_current_upload_time";
+    public static String interfaceRequestTimeTag="interface_request_time";
      public static int getAwaitNum(Realm mRealm,String uploadType){
         RealmResults<AsyncUploadRealm> realmResults  = mRealm.where(AsyncUploadRealm.class)
                 .equalTo("isDelete",0)
@@ -50,6 +51,11 @@ public class UploadUtils {
         return differenceInMillis / 1000;
     }
 
+    /**
+     * 获取是否允许上传
+     * @param context Context
+     * @return boolean
+     */
     public static boolean getUploadExists(Context context){
        String  currentUploadTimeStr = CommSharedUtil.getInstance(context).getString(currentUploadTimeTag);
 
@@ -57,7 +63,7 @@ public class UploadUtils {
            return true;
        }
         long diff = calculateDifferenceInSeconds( getCurrentTime() , Long.parseLong(currentUploadTimeStr));
-        return diff <= 60 * 2;
+        return diff > 60 * 3;
     }
 
     /**
@@ -76,4 +82,35 @@ public class UploadUtils {
         CommSharedUtil.getInstance(context).remove(currentUploadTimeTag);
     }
 
+    /**
+     * 获取是否继续
+     * @param context Context
+     * @return boolean
+     */
+    public static boolean getUploadIsContinue(Context context){
+        String  currentUploadTimeStr = CommSharedUtil.getInstance(context).getString(interfaceRequestTimeTag);
+
+        if (currentUploadTimeStr==null ||currentUploadTimeStr.isEmpty()){
+            return true;
+        }
+        long diff = calculateDifferenceInSeconds( getCurrentTime() , Long.parseLong(currentUploadTimeStr));
+        //
+        return diff > 60 * 5;
+    }
+
+    /**
+     * 设置上传时间
+     * @param context Context
+     */
+    public static void setInterfaceRequestTime(Context context){
+        CommSharedUtil.getInstance(context).putString(interfaceRequestTimeTag,String.valueOf(getCurrentTime()));
+
+    }
+    /**
+     * 删除上传时间
+     * @param context Context
+     */
+    public static void delInterfaceRequestTime(Context context){
+        CommSharedUtil.getInstance(context).remove(interfaceRequestTimeTag);
+    }
 }