|
@@ -26,6 +26,8 @@ import java.io.ObjectOutputStream;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
+import static android.net.NetworkInfo.State.UNKNOWN;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Created by ray on 13/06/2017.
|
|
* Created by ray on 13/06/2017.
|
|
|
*/
|
|
*/
|
|
@@ -38,10 +40,11 @@ public class RAUploadManager {
|
|
|
|
|
|
|
|
public int maxThread = 3;
|
|
public int maxThread = 3;
|
|
|
private int maxRetry = 5;
|
|
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;
|
|
public TaskStatus newtaskStatus = TaskStatus.TaskStatusWait;
|
|
|
|
|
|
|
|
|
|
|
|
@@ -81,16 +84,18 @@ public class RAUploadManager {
|
|
|
|
|
|
|
|
public static class RAUploadManagerConfigure {
|
|
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 maxRetry = 5;
|
|
|
public int maxThread = 3;
|
|
public int maxThread = 3;
|
|
|
|
|
|
|
|
public TaskStatus newtaskStatus = TaskStatus.TaskStatusWait;
|
|
public TaskStatus newtaskStatus = TaskStatus.TaskStatusWait;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
private Context appContext;
|
|
private Context appContext;
|
|
|
|
|
|
|
|
RAUploadManagerConfigure(Context applicationContext) {
|
|
RAUploadManagerConfigure(Context applicationContext) {
|
|
@@ -102,15 +107,18 @@ public class RAUploadManager {
|
|
|
SharedPreferences UMSetting = RAUtil.sharedPreferences(applicationContext,"UploadManager");
|
|
SharedPreferences UMSetting = RAUtil.sharedPreferences(applicationContext,"UploadManager");
|
|
|
|
|
|
|
|
if (UMSetting != null) {
|
|
if (UMSetting != null) {
|
|
|
|
|
+
|
|
|
autoStart = UMSetting.getBoolean("auto_upload",false);
|
|
autoStart = UMSetting.getBoolean("auto_upload",false);
|
|
|
removeFinish = UMSetting.getBoolean("auto_rm_finish",false);
|
|
removeFinish = UMSetting.getBoolean("auto_rm_finish",false);
|
|
|
removeError = UMSetting.getBoolean("auto_rm_error",false);
|
|
removeError = UMSetting.getBoolean("auto_rm_error",false);
|
|
|
wifiOnly = UMSetting.getBoolean("wifi_only", false);
|
|
wifiOnly = UMSetting.getBoolean("wifi_only", false);
|
|
|
|
|
+ monitorNetwork = UMSetting.getBoolean("monitor_network", false);
|
|
|
|
|
|
|
|
maxRetry = UMSetting.getInt("retry_count", 5);
|
|
maxRetry = UMSetting.getInt("retry_count", 5);
|
|
|
maxThread = UMSetting.getInt("max_thread", 3);
|
|
maxThread = UMSetting.getInt("max_thread", 3);
|
|
|
|
|
|
|
|
newtaskStatus = TaskStatus.values()[UMSetting.getInt("newtask_status", TaskStatus.TaskStatusWait.ordinal())];
|
|
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_finish",removeFinish);
|
|
|
editor.putBoolean("auto_rm_error",removeError);
|
|
editor.putBoolean("auto_rm_error",removeError);
|
|
|
editor.putBoolean("wifi_only",wifiOnly);
|
|
editor.putBoolean("wifi_only",wifiOnly);
|
|
|
|
|
+ editor.putBoolean("monitor_network",monitorNetwork);
|
|
|
editor.putInt("retry_count",maxRetry);
|
|
editor.putInt("retry_count",maxRetry);
|
|
|
editor.putInt("max_thread",maxThread);
|
|
editor.putInt("max_thread",maxThread);
|
|
|
editor.putInt("newtask_status",newtaskStatus.ordinal());
|
|
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);
|
|
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)
|
|
if(appCtx==null)
|
|
|
throw new IllegalArgumentException("applicationContext can't be null");
|
|
throw new IllegalArgumentException("applicationContext can't be null");
|
|
@@ -182,6 +305,7 @@ public class RAUploadManager {
|
|
|
removeFinish = UMSetting.getBoolean("auto_rm_finish",false);
|
|
removeFinish = UMSetting.getBoolean("auto_rm_finish",false);
|
|
|
removeError = UMSetting.getBoolean("auto_rm_error",false);
|
|
removeError = UMSetting.getBoolean("auto_rm_error",false);
|
|
|
wifiOnly = UMSetting.getBoolean("wifi_only", false);
|
|
wifiOnly = UMSetting.getBoolean("wifi_only", false);
|
|
|
|
|
+ monitorNetwork = UMSetting.getBoolean("monitor_network", false);
|
|
|
|
|
|
|
|
maxRetry = UMSetting.getInt("retry_count", 5);
|
|
maxRetry = UMSetting.getInt("retry_count", 5);
|
|
|
maxThread = UMSetting.getInt("max_thread", 3);
|
|
maxThread = UMSetting.getInt("max_thread", 3);
|
|
@@ -194,6 +318,19 @@ public class RAUploadManager {
|
|
|
@Override
|
|
@Override
|
|
|
public Boolean operate(final Bundle taskinfo) {
|
|
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;
|
|
boolean ret = false;
|
|
|
String path = taskinfo.getString("path");
|
|
String path = taskinfo.getString("path");
|
|
@@ -217,6 +354,10 @@ public class RAUploadManager {
|
|
|
e.printStackTrace();
|
|
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() {
|
|
JSONObject result = Network.uploadFileJSON(path, url, taskinfo.getBundle("params"), new Network.FileUploadListener() {
|
|
|
@Override
|
|
@Override
|
|
|
public double percent_step() {
|
|
public double percent_step() {
|
|
@@ -275,11 +416,33 @@ public class RAUploadManager {
|
|
|
String rmsg = result.getString("msg");
|
|
String rmsg = result.getString("msg");
|
|
|
taskinfo.putString("msg", rmsg);
|
|
taskinfo.putString("msg", rmsg);
|
|
|
} else {
|
|
} else {
|
|
|
|
|
+
|
|
|
taskinfo.putInt("retry", taskinfo.getInt("retry", 0) + 1);
|
|
taskinfo.putInt("retry", taskinfo.getInt("retry", 0) + 1);
|
|
|
- taskinfo.putInt("status", TaskStatus.TaskStatusWait.ordinal());
|
|
|
|
|
taskinfo.putDouble("progress", 0.0);
|
|
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);
|
|
task.putBoolean("iscancel", true);
|
|
|
|
|
|
|
|
|
|
+ if (uiUpdateListener != null)
|
|
|
|
|
+ uiUpdateListener.updateList(arr_queue);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public void saveTasks() {
|
|
public void saveTasks() {
|