Forráskód Böngészése

1.修改Android Apex Drivers上传网络连接恢复时重传。
2.修改Android Apex Drivers Upload Manager为单例,使之与Configure保持同步修改。

Pen Li 7 éve
szülő
commit
cc4c4b35cf

+ 27 - 39
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/service/RAService.java

@@ -58,6 +58,7 @@ public abstract class RAService extends Service {
     public static final int FLAG_SERVICE_LOCATION = 1 << 1;
     public static final int FLAG_SERVICE_NOTIFICATION = 1 << 2;
     public static final int FLAG_SERVICE_UPLOAD = 1 << 3;
+    public static final int FLAG_SERVICE_REACHABILITY = 1 << 4;
 //    public static final int FLAG_LOCATION_SERVICE = 1<<1;
 
     private static final boolean System_Location_Flag = true;
@@ -68,6 +69,20 @@ public abstract class RAService extends Service {
 
     protected abstract void Setup();
 
+    /** Reachability */
+
+    private ServiceNetworkReachabilityCallback reachabilityCallback;
+
+    public interface ServiceNetworkReachabilityCallback {
+        void networkStateChanged(NetworkInfo networkInfo);
+    }
+
+    public void initServiceNetworkReachability(ServiceNetworkReachabilityCallback callback) {
+        service_flag = service_flag | FLAG_SERVICE_REACHABILITY;
+        reachabilityCallback = callback;
+    }
+
+
     // sub function upload
     public interface ServiceUpload {
 
@@ -482,7 +497,7 @@ public abstract class RAService extends Service {
 
 
         if ((service_flag & FLAG_SERVICE_UPLOAD) == FLAG_SERVICE_UPLOAD) {
-            uploadManager = new RAUploadManager(getApplicationContext());
+            uploadManager = RAUploadManager.sharedManager(getApplicationContext());
 //            msgFilter.addAction("REDANT.POP.RESET_LOCATION");
 //            msgFilter.addAction("REDANT.POP.QUERY_UPLOAD_STATE");
             msgFilter.addAction("REDANT.BROADCAST.ACTION_UPLOAD_ADD_TASK");
@@ -579,6 +594,9 @@ public abstract class RAService extends Service {
             msgFilter.addAction(RABroadcast.ACTION_PUSHNOTIFICATION_CHECK);
             msgFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
         }
+        if ((service_flag & FLAG_SERVICE_REACHABILITY) == FLAG_SERVICE_REACHABILITY) {
+            msgFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
+        }
 
         registerReceiver(uploadReceiver, msgFilter);
 
@@ -676,53 +694,23 @@ public abstract class RAService extends Service {
 //            else if ("REDANT.RAImage.SAVE_TASK".equals(action)) {
 //                uploadManager.stopAllTasks();
 //            }
-            else if (intent
-                    .getAction()
-                    .equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
+            else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
 
 
-                ConnectivityManager connManager = (ConnectivityManager) context
-                        .getSystemService(Context.CONNECTIVITY_SERVICE);
+                ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 
                 NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
                 if (networkInfo == null) {
-                    dbgUtil.Logd(
-                            "Current Network info",
-                            "can not get Active NetworkInfo!");
-                    return;
+                    dbgUtil.Logd("Current Network info","can not get Active NetworkInfo!");
                 }
-                NetworkInfo.State netState = networkInfo
-                        .getState();
-                if (netState != NetworkInfo.State.CONNECTED) {
-                    dbgUtil.Logd(
-                            "Current Network info",
-                            "not Connected!State="
-                                    + netState);
-                    return;
-                } else {
-
-                    int iconntype = -1;
-                    iconntype = networkInfo
-                            .getType();
-
-
-                    SharedPreferences pref = RAUtil.sharedPreferences(getApplicationContext(),"UploadManager");
 
-
-
-                    boolean wifi_only = pref.getBoolean("wifi_only", false);
-
-                    if (wifi_only == true && iconntype != ConnectivityManager.TYPE_WIFI && iconntype != 9/* earthnet */) {
-                        uploadManager.stopAllTasks();
-//															 dbgUtil.Log(Log.DEBUG,
-//															 "Current Network info",
-//															 "not allowed!Connection type="
-//															 +
-//															 networkInfo.getTypeName());
-                        return;
-                    }
+                if (uploadManager != null) {
+                    uploadManager.networkStateChanged(networkInfo);
                 }
 
+                if (reachabilityCallback != null) {
+                    reachabilityCallback.networkStateChanged(networkInfo);
+                }
 
             } else if (RABroadcast.ACTION_LOCATION_ENABLE_TRACING.equals(action)) {
                 enable_locationTracing();

+ 177 - 12
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/upload/RAUploadManager.java

@@ -26,6 +26,8 @@ import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.List;
 
+import static android.net.NetworkInfo.State.UNKNOWN;
+
 /**
  * Created by ray on 13/06/2017.
  */
@@ -38,10 +40,11 @@ public class RAUploadManager {
 
     public int maxThread = 3;
     private int maxRetry = 5;
-    private Boolean wifiOnly = false;
-    private Boolean autoStart=true;
-    private Boolean removeFinish=true;
-    private Boolean removeError=false;
+    private boolean wifiOnly = false;
+    private boolean autoStart=false;
+    private boolean removeFinish=false;
+    private boolean removeError=false;
+    private boolean monitorNetwork = false;///<网络断开,停止所有任务。网络恢复重启任务,除了Error状态下的任务
     public TaskStatus newtaskStatus = TaskStatus.TaskStatusWait;
 
 
@@ -81,16 +84,18 @@ public class RAUploadManager {
 
     public static class RAUploadManagerConfigure {
 
-        public Boolean autoStart=false;
-        public Boolean removeFinish=false;
-        public Boolean removeError=false;
-        public Boolean wifiOnly = false;
+        public boolean autoStart=false;
+        public boolean removeFinish=false;
+        public boolean removeError=false;
+        public boolean wifiOnly = false;
+        public boolean monitorNetwork = false;///<网络断开,停止所有任务。网络恢复重启任务,除了Error状态下的任务
 
         public int maxRetry = 5;
         public int maxThread = 3;
 
         public TaskStatus newtaskStatus = TaskStatus.TaskStatusWait;
 
+
         private Context appContext;
 
         RAUploadManagerConfigure(Context applicationContext) {
@@ -102,15 +107,18 @@ public class RAUploadManager {
             SharedPreferences UMSetting = RAUtil.sharedPreferences(applicationContext,"UploadManager");
 
             if (UMSetting != null) {
+
                 autoStart = UMSetting.getBoolean("auto_upload",false);
                 removeFinish = UMSetting.getBoolean("auto_rm_finish",false);
                 removeError = UMSetting.getBoolean("auto_rm_error",false);
                 wifiOnly = UMSetting.getBoolean("wifi_only", false);
+                monitorNetwork = UMSetting.getBoolean("monitor_network", false);
 
                 maxRetry = UMSetting.getInt("retry_count", 5);
                 maxThread = UMSetting.getInt("max_thread", 3);
 
                 newtaskStatus = TaskStatus.values()[UMSetting.getInt("newtask_status", TaskStatus.TaskStatusWait.ordinal())];
+
             }
         }
 
@@ -126,6 +134,7 @@ public class RAUploadManager {
                     editor.putBoolean("auto_rm_finish",removeFinish);
                     editor.putBoolean("auto_rm_error",removeError);
                     editor.putBoolean("wifi_only",wifiOnly);
+                    editor.putBoolean("monitor_network",monitorNetwork);
                     editor.putInt("retry_count",maxRetry);
                     editor.putInt("max_thread",maxThread);
                     editor.putInt("newtask_status",newtaskStatus.ordinal());
@@ -134,6 +143,18 @@ public class RAUploadManager {
                 }
 
             }
+
+            if (RAUploadManager.instance != null) {
+
+                RAUploadManager.instance.autoStart = autoStart;
+                RAUploadManager.instance.removeFinish = removeFinish;
+                RAUploadManager.instance.removeError = removeError;
+                RAUploadManager.instance.wifiOnly = wifiOnly;
+                RAUploadManager.instance.monitorNetwork = monitorNetwork;
+                RAUploadManager.instance.maxRetry = maxRetry;
+                RAUploadManager.instance.maxThread = maxThread;
+                RAUploadManager.instance.newtaskStatus = newtaskStatus;
+            }
         }
 
     }
@@ -165,8 +186,110 @@ public class RAUploadManager {
         void updateList(ArrayList<Bundle> newlist);
     }
 
+    private boolean isConnected = true;
+    private boolean isNotReachable = false;
+    public void networkStateChanged(NetworkInfo networkInfo) {
+
+        boolean connected = networkInfo != null;
+        NetworkInfo.State netState = UNKNOWN;
+        if (connected) {
+            netState = networkInfo.getState();
+            connected = connected && (netState == NetworkInfo.State.CONNECTED);
+        }
+
+        isConnected = connected;
+
+        if (monitorNetwork) {
+
+            if (!connected) {
+
+                isNotReachable = true;
+
+                stopAllTasks();
+
+            } else {
+
+                int networkType = -1;
+                networkType = networkInfo.getType();
+                boolean wifi_only = wifiOnly;
+
+                if (wifi_only == true && networkType != ConnectivityManager.TYPE_WIFI && networkType != 9) {
+
+                    stopAllTasks();
+
+                } else {
+
+                    if (isNotReachable) {
+                        restartAllTasks();
+                    }
+
+                }
+
+                isNotReachable = false;
+            }
+
+        } else {
+
+            if (!connected) {
+                dbgUtil.Logd("Current Network info", "not Connected!State=" + netState);
+
+
+
+            } else {
+
+                int networkType = -1;
+
+                networkType = networkInfo.getType();
+                boolean wifi_only = wifiOnly;
+
+                if (wifi_only == true && networkType != ConnectivityManager.TYPE_WIFI && networkType != 9) {
+
+                    stopAllTasks();
+                }
+            } // else
+
+        } // else
+    }
+
+    private NetworkInfo getNetworkInfo() {
 
-    public RAUploadManager(Context appCtx) {
+        ConnectivityManager connManager = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
+        NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
+
+        return networkInfo;
+    }
+
+    private void restartAllTasks() {
+
+        if (arr_queue == null)
+            return;
+        for (Bundle task : arr_queue) {
+
+            int status = task.getInt("status");
+
+            if (status == TaskStatus.TaskStatusWait.ordinal() || status == TaskStatus.TaskStatusStop.ordinal()) {
+                startTask(task);
+            }
+        }
+
+        if (uiUpdateListener != null)
+            uiUpdateListener.updateList(arr_queue);
+    }
+
+    private static volatile RAUploadManager instance;
+
+    public static RAUploadManager sharedManager(Context context) {
+        if (instance == null) {
+            synchronized (RAUploadManager.class) {
+                if (instance == null && context != null) {
+                    instance = new RAUploadManager(context);
+                }
+            }
+        }
+        return instance;
+    }
+
+    private RAUploadManager(Context appCtx) {
 
         if(appCtx==null)
             throw new IllegalArgumentException("applicationContext can't be null");
@@ -182,6 +305,7 @@ public class RAUploadManager {
             removeFinish = UMSetting.getBoolean("auto_rm_finish",false);
             removeError = UMSetting.getBoolean("auto_rm_error",false);
             wifiOnly = UMSetting.getBoolean("wifi_only", false);
+            monitorNetwork = UMSetting.getBoolean("monitor_network", false);
 
             maxRetry = UMSetting.getInt("retry_count", 5);
             maxThread = UMSetting.getInt("max_thread", 3);
@@ -194,6 +318,19 @@ public class RAUploadManager {
             @Override
             public Boolean operate(final Bundle taskinfo) {
 
+                if (!isConnected) {
+                    taskinfo.putInt("status", TaskStatus.TaskStatusStop.ordinal());
+                    taskinfo.putDouble("progress", 0.0);
+                    taskinfo.putString("msg", "");
+
+                    if (uiUpdateListener != null) {
+                        int index = arr_queue.indexOf(taskinfo);
+
+                        uiUpdateListener.updateCell(index, taskinfo);
+                    }
+
+                    return false;
+                }
 
                 boolean ret = false;
                 String path = taskinfo.getString("path");
@@ -217,6 +354,10 @@ public class RAUploadManager {
                     e.printStackTrace();
                 }
 
+                taskinfo.putBoolean("iscancel", false);
+                taskinfo.putInt("status", TaskStatus.TaskStatusStart.ordinal());
+                taskinfo.remove("msg");
+
                 JSONObject result = Network.uploadFileJSON(path, url, taskinfo.getBundle("params"), new Network.FileUploadListener() {
                     @Override
                     public double percent_step() {
@@ -275,11 +416,33 @@ public class RAUploadManager {
                             String rmsg = result.getString("msg");
                             taskinfo.putString("msg", rmsg);
                         } else {
+
                             taskinfo.putInt("retry", taskinfo.getInt("retry", 0) + 1);
-                            taskinfo.putInt("status", TaskStatus.TaskStatusWait.ordinal());
                             taskinfo.putDouble("progress", 0.0);
-                            taskinfo.putString("msg", "connection lost, retry...");
-                            ret = true;
+
+                            if (taskinfo.getBoolean("iscancel",false)) {
+
+                                taskinfo.putInt("status", TaskStatus.TaskStatusStop.ordinal());
+                                taskinfo.putString("msg", "");
+
+                            } else {
+
+                                // 主动调用一次,网络中断情况下,返回结果会比网络状态监听更早
+                                networkStateChanged(getNetworkInfo());
+
+                                if (isConnected) {
+
+                                    taskinfo.putInt("status", TaskStatus.TaskStatusWait.ordinal());
+                                    taskinfo.putString("msg", "connection lost, retry...");
+                                    ret = true;
+
+                                } else {
+                                    // 完成100%等待返回到时候,网络中断
+
+                                    taskinfo.putInt("status", TaskStatus.TaskStatusStop.ordinal());
+                                    taskinfo.putString("msg", "");
+                                }
+                            }
 
                         }
                     }
@@ -616,6 +779,8 @@ public class RAUploadManager {
 
         task.putBoolean("iscancel", true);
 
+        if (uiUpdateListener != null)
+            uiUpdateListener.updateList(arr_queue);
     }
 
     public void saveTasks() {

+ 1 - 1
ApexDrivers/RAUtilsLibrary/src/main/java/com/usai/redant/rautils/upload/UploadService.java

@@ -28,7 +28,7 @@ public class UploadService extends Service
 //	LocationManager			locationManager;
 //	LocationListener		locationListener;
 
-    public RAUploadManager uploadManager = new RAUploadManager(getApplicationContext());
+    public RAUploadManager uploadManager = RAUploadManager.sharedManager(getApplicationContext());
 
     // private boolean location_changed = false;
 

+ 7 - 0
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/ApexDriversBackgroundService.java

@@ -6,6 +6,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.location.Location;
+import android.net.NetworkInfo;
 import android.util.Log;
 
 import com.usai.redant.rautils.receiver.RABroadcast;
@@ -73,6 +74,12 @@ public class ApexDriversBackgroundService extends RAService implements RAService
         });
 
         initServiceUpload(this);
+        initServiceNetworkReachability(new ServiceNetworkReachabilityCallback() {
+            @Override
+            public void networkStateChanged(NetworkInfo networkInfo) {
+
+            }
+        });
 
         dbgUtil.fileLog(this,"ApexDriversBackgroundService Setup()");
 

+ 41 - 2
ApexDrivers/apexdriverslib/src/main/java/com/usai/redant/apexdrivers/upload/UploadListActivity.java

@@ -1,9 +1,11 @@
 package com.usai.redant.apexdrivers.upload;
 
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.ServiceConnection;
 import android.os.Bundle;
 import android.os.IBinder;
@@ -32,6 +34,8 @@ import com.usai.redant.rautils.upload.RAUploadManager;
 
 import java.util.ArrayList;
 
+import static com.usai.redant.apexdrivers.ApexDriversBackgroundService.APEX_DRIVER_UPLOAD_COUNT_CHANGE_NOTIFICATION;
+
 
 public class UploadListActivity extends BasicActivity {
 
@@ -53,8 +57,8 @@ public class UploadListActivity extends BasicActivity {
         @Override
         public void updateCell(long index, final Bundle taskinfo)
         {
-            if(true)
-                return;
+//            if(true)
+//                return;
 
             if(true) {
 
@@ -156,6 +160,38 @@ public class UploadListActivity extends BasicActivity {
         }
     };
 
+    private BroadcastReceiver uploadReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+
+            String action = intent.getAction();
+            if (action.equals(APEX_DRIVER_UPLOAD_COUNT_CHANGE_NOTIFICATION)) {
+
+                runOnUiThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        notifyDataChanged();
+                    }
+                });
+            }
+        }
+    };
+
+    private void registerBroadcastReceiver() {
+
+        IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(APEX_DRIVER_UPLOAD_COUNT_CHANGE_NOTIFICATION);
+
+        registerReceiver(uploadReceiver,intentFilter);
+    }
+
+    public void notifyDataChanged() {
+
+        displaylist = (ArrayList<Bundle>) uploadManager.get_arr_queue().clone(); //uploadManager.arr_queue;
+
+        adapter.notifyDataSetChanged();
+    }
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -200,6 +236,7 @@ public class UploadListActivity extends BasicActivity {
 
         bindService();
 
+        registerBroadcastReceiver();
     }
 
     @Override
@@ -207,6 +244,8 @@ public class UploadListActivity extends BasicActivity {
 
         unbindService(serviceConnection);
 
+        unregisterReceiver(uploadReceiver);
+
         super.onDestroy();
     }