|
@@ -8,6 +8,7 @@ import android.content.Intent;
|
|
|
import android.content.SharedPreferences;
|
|
import android.content.SharedPreferences;
|
|
|
import android.net.ConnectivityManager;
|
|
import android.net.ConnectivityManager;
|
|
|
import android.net.NetworkCapabilities;
|
|
import android.net.NetworkCapabilities;
|
|
|
|
|
+import android.net.NetworkRequest;
|
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
|
import android.util.Base64;
|
|
import android.util.Base64;
|
|
|
import android.util.Log;
|
|
import android.util.Log;
|
|
@@ -35,6 +36,52 @@ import java.util.List;
|
|
|
|
|
|
|
|
public class RAUploadManager {
|
|
public class RAUploadManager {
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private ConnectionStateMonitor monitor = new ConnectionStateMonitor();
|
|
|
|
|
+ public class ConnectionStateMonitor extends ConnectivityManager.NetworkCallback
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ final NetworkRequest networkRequest;
|
|
|
|
|
+
|
|
|
|
|
+ public ConnectionStateMonitor() {
|
|
|
|
|
+ networkRequest = new NetworkRequest.Builder()
|
|
|
|
|
+ .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
|
|
|
|
|
+ .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void enable(Context context) {
|
|
|
|
|
+ ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
+ connectivityManager.registerNetworkCallback(networkRequest, this);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Likewise, you can have a disable method that simply calls ConnectivityManager.unregisterNetworkCallback(NetworkCallback) too.
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onAvailable( android.net.Network network) {
|
|
|
|
|
+ // Do what you need to do here
|
|
|
|
|
+
|
|
|
|
|
+ Log.e(TAG,"ConnectionStateMonitor onAvailable()");
|
|
|
|
|
+
|
|
|
|
|
+ RAUploadManager.this.networkStateConnect();
|
|
|
|
|
+ }
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onLost( android.net.Network network)
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ Log.e(TAG,"ConnectionStateMonitor onLost()");
|
|
|
|
|
+ RAUploadManager.this.networkStateDisconnect();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void onUnavailable() {
|
|
|
|
|
+//// RAUploadManager.this.networkStateDisconnect();
|
|
|
|
|
+// }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
String TAG="RAUploadManager";
|
|
String TAG="RAUploadManager";
|
|
|
|
|
|
|
|
private int retryWaiting = 300;
|
|
private int retryWaiting = 300;
|
|
@@ -43,10 +90,10 @@ 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 wifiOnly = false;
|
|
|
- private boolean autoStart=false;
|
|
|
|
|
|
|
+// private boolean autoStart=false;
|
|
|
private boolean removeFinish=false;
|
|
private boolean removeFinish=false;
|
|
|
private boolean removeError=false;
|
|
private boolean removeError=false;
|
|
|
- private boolean monitorNetwork = false;///<网络断开,停止所有任务。网络恢复重启任务,除了Error状态下的任务
|
|
|
|
|
|
|
+ private boolean monitorNetwork = true;///<网络断开,停止所有任务。网络恢复重启任务,除了Error状态下的任务
|
|
|
public TaskStatus newtaskStatus = TaskStatus.TaskStatusWait;
|
|
public TaskStatus newtaskStatus = TaskStatus.TaskStatusWait;
|
|
|
|
|
|
|
|
|
|
|
|
@@ -171,11 +218,11 @@ public class RAUploadManager {
|
|
|
|
|
|
|
|
public static class RAUploadManagerConfigure {
|
|
public static class RAUploadManagerConfigure {
|
|
|
|
|
|
|
|
- public boolean autoStart=false;
|
|
|
|
|
|
|
+// public boolean autoStart=false;
|
|
|
public boolean removeFinish=false;
|
|
public boolean removeFinish=false;
|
|
|
public boolean removeError=false;
|
|
public boolean removeError=false;
|
|
|
public boolean wifiOnly = false;
|
|
public boolean wifiOnly = false;
|
|
|
- public boolean monitorNetwork = false;///<网络断开,停止所有任务。网络恢复重启任务,除了Error状态下的任务
|
|
|
|
|
|
|
+// public boolean monitorNetwork = false;///<网络断开,停止所有任务。网络恢复重启任务,除了Error状态下的任务
|
|
|
public int maxRetry = 5;
|
|
public int maxRetry = 5;
|
|
|
public int maxThread = 3;
|
|
public int maxThread = 3;
|
|
|
public int retryWaiting = 300;
|
|
public int retryWaiting = 300;
|
|
@@ -194,11 +241,11 @@ public class RAUploadManager {
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
+// 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);
|
|
@@ -217,11 +264,11 @@ public class RAUploadManager {
|
|
|
if (UMSetting != null) {
|
|
if (UMSetting != null) {
|
|
|
|
|
|
|
|
SharedPreferences.Editor editor = UMSetting.edit();
|
|
SharedPreferences.Editor editor = UMSetting.edit();
|
|
|
- editor.putBoolean("auto_upload",autoStart);
|
|
|
|
|
|
|
+// editor.putBoolean("auto_upload",autoStart);
|
|
|
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.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("retry_waiting",retryWaiting);
|
|
editor.putInt("retry_waiting",retryWaiting);
|
|
@@ -234,11 +281,11 @@ public class RAUploadManager {
|
|
|
|
|
|
|
|
if (RAUploadManager.instance != null) {
|
|
if (RAUploadManager.instance != null) {
|
|
|
|
|
|
|
|
- RAUploadManager.instance.autoStart = autoStart;
|
|
|
|
|
|
|
+// RAUploadManager.instance.autoStart = autoStart;
|
|
|
RAUploadManager.instance.removeFinish = removeFinish;
|
|
RAUploadManager.instance.removeFinish = removeFinish;
|
|
|
RAUploadManager.instance.removeError = removeError;
|
|
RAUploadManager.instance.removeError = removeError;
|
|
|
RAUploadManager.instance.wifiOnly = wifiOnly;
|
|
RAUploadManager.instance.wifiOnly = wifiOnly;
|
|
|
- RAUploadManager.instance.monitorNetwork = monitorNetwork;
|
|
|
|
|
|
|
+// RAUploadManager.instance.monitorNetwork = monitorNetwork;
|
|
|
RAUploadManager.instance.maxRetry = maxRetry;
|
|
RAUploadManager.instance.maxRetry = maxRetry;
|
|
|
RAUploadManager.instance.maxThread = maxThread;
|
|
RAUploadManager.instance.maxThread = maxThread;
|
|
|
RAUploadManager.instance.retryWaiting = retryWaiting;
|
|
RAUploadManager.instance.retryWaiting = retryWaiting;
|
|
@@ -263,6 +310,8 @@ public class RAUploadManager {
|
|
|
configure.save();
|
|
configure.save();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static RAUploadManagerConfigure configure(Context applicationContext) {
|
|
public static RAUploadManagerConfigure configure(Context applicationContext) {
|
|
@@ -356,14 +405,20 @@ public class RAUploadManager {
|
|
|
// }
|
|
// }
|
|
|
|
|
|
|
|
private void restartAllTasks() {
|
|
private void restartAllTasks() {
|
|
|
|
|
+ Log.e(TAG, "restartAllTasks: restartAllTasks()" );
|
|
|
|
|
|
|
|
if (arr_queue == null)
|
|
if (arr_queue == null)
|
|
|
|
|
+ {
|
|
|
|
|
+ Log.e(TAG, "restartAllTasks: restartAllTasks() 1" );
|
|
|
return;
|
|
return;
|
|
|
|
|
+ }
|
|
|
for (Bundle task : arr_queue) {
|
|
for (Bundle task : arr_queue) {
|
|
|
|
|
|
|
|
int status = task.getInt("status");
|
|
int status = task.getInt("status");
|
|
|
|
|
|
|
|
if (status == TaskStatus.TaskStatusWait.ordinal() || status == TaskStatus.TaskStatusStop.ordinal()) {
|
|
if (status == TaskStatus.TaskStatusWait.ordinal() || status == TaskStatus.TaskStatusStop.ordinal()) {
|
|
|
|
|
+
|
|
|
|
|
+ Log.e(TAG, "restartAllTasks: restartAllTasks() startTask " );
|
|
|
startTask(task);
|
|
startTask(task);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -379,6 +434,8 @@ public class RAUploadManager {
|
|
|
synchronized (RAUploadManager.class) {
|
|
synchronized (RAUploadManager.class) {
|
|
|
if (instance == null && context != null) {
|
|
if (instance == null && context != null) {
|
|
|
instance = new RAUploadManager(context);
|
|
instance = new RAUploadManager(context);
|
|
|
|
|
+
|
|
|
|
|
+ instance.monitor.enable(context);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -397,11 +454,11 @@ public class RAUploadManager {
|
|
|
SharedPreferences UMSetting = RAUtil.unProtectedPreferences(applicationContext,"UploadManager");
|
|
SharedPreferences UMSetting = RAUtil.unProtectedPreferences(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);
|
|
|
|
|
|
|
+// 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);
|
|
@@ -445,7 +502,17 @@ public class RAUploadManager {
|
|
|
|
|
|
|
|
// try{
|
|
// try{
|
|
|
try {
|
|
try {
|
|
|
- Thread.currentThread().sleep(waiting * 1000);
|
|
|
|
|
|
|
+
|
|
|
|
|
+// if(RAUploadManager.this.arr_queue.stream().count()<maxThread)
|
|
|
|
|
+ Thread.currentThread().sleep(waiting * 1000);
|
|
|
|
|
+// else
|
|
|
|
|
+// {
|
|
|
|
|
+// //如果当前任务还在等待,那跳过当前,开始其他任务。
|
|
|
|
|
+// taskinfo.putInt("status", TaskStatus.TaskStatusWait.ordinal());
|
|
|
|
|
+// taskinfo.remove("msg");
|
|
|
|
|
+// taskinfo.putBoolean("iscancel", false);
|
|
|
|
|
+// return true;
|
|
|
|
|
+// }
|
|
|
} catch (InterruptedException e) {
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
}
|
|
}
|
|
@@ -900,6 +967,9 @@ public class RAUploadManager {
|
|
|
;
|
|
;
|
|
|
|
|
|
|
|
public void stopTask(Bundle task) {
|
|
public void stopTask(Bundle task) {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
if (task.getInt("status") != TaskStatus.TaskStatusStart.ordinal() && task.getInt("status") != TaskStatus.TaskStatusWait.ordinal())
|
|
if (task.getInt("status") != TaskStatus.TaskStatusStart.ordinal() && task.getInt("status") != TaskStatus.TaskStatusWait.ordinal())
|
|
|
return;
|
|
return;
|
|
|
|
|
|