|
@@ -10,6 +10,8 @@ import android.os.Handler;
|
|
import android.util.Base64;
|
|
import android.util.Base64;
|
|
import android.util.Log;
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
+import androidx.annotation.NonNull;
|
|
|
|
+
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
@@ -53,34 +55,25 @@ import okhttp3.WebSocketListener;
|
|
* description:
|
|
* description:
|
|
*/
|
|
*/
|
|
public final class WsManager {
|
|
public final class WsManager {
|
|
|
|
+ private static final int NORMAL_CLOSURE_STATUS = 1000;
|
|
|
|
+ private static final long RECONNECT_DELAY_MS = 1000; // 重连延迟时间
|
|
private static WebSocketListener webSocketListener;
|
|
private static WebSocketListener webSocketListener;
|
|
private static USSDApi ussdApi = USSDController.getInstance(Utils.getApp());
|
|
private static USSDApi ussdApi = USSDController.getInstance(Utils.getApp());
|
|
private static WebSocket webSocket;
|
|
private static WebSocket webSocket;
|
|
private static ScheduledFuture<?> reconnectTask;
|
|
private static ScheduledFuture<?> reconnectTask;
|
|
private static ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
|
private static ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
|
|
private static ScheduledExecutorService executorServiceUi = Executors.newSingleThreadScheduledExecutor();
|
|
private static ScheduledExecutorService executorServiceUi = Executors.newSingleThreadScheduledExecutor();
|
|
-
|
|
|
|
private static Intent svc = null;
|
|
private static Intent svc = null;
|
|
- private static final int NORMAL_CLOSURE_STATUS = 1000;
|
|
|
|
- private static final long RECONNECT_DELAY_MS = 1000; // 重连延迟时间
|
|
|
|
-
|
|
|
|
|
|
|
|
public static WebSocket connectWebSocket() {
|
|
public static WebSocket connectWebSocket() {
|
|
if (StringUtils.isTrimEmpty(BuildConfig.WEB_URL) || StringUtils.equals("", BuildConfig.WEB_URL)) {
|
|
if (StringUtils.isTrimEmpty(BuildConfig.WEB_URL) || StringUtils.equals("", BuildConfig.WEB_URL)) {
|
|
- ThreadUtils.runOnUiThreadDelayed(new Runnable() {
|
|
|
|
- @Override
|
|
|
|
- public void run() {
|
|
|
|
- LiveEventBus.get(WEBSOCKET_STATUS_TAG).post(StringUtils.getString(R.string.log_tip_13));
|
|
|
|
- }
|
|
|
|
- }, 10000);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+ ThreadUtils.runOnUiThreadDelayed(() -> LiveEventBus.get(WEBSOCKET_STATUS_TAG).post(StringUtils.getString(R.string.log_tip_13)), 10000);
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
webSocketListener = new WebSocketListener() {
|
|
webSocketListener = new WebSocketListener() {
|
|
@Override
|
|
@Override
|
|
- public void onOpen(WebSocket webSocket, Response response) {
|
|
|
|
|
|
+ public void onOpen(@NonNull WebSocket webSocket, @NonNull Response response) {
|
|
super.onOpen(webSocket, response);
|
|
super.onOpen(webSocket, response);
|
|
Log.i(Config.LOG_TAG, StringUtils.getString(R.string.log_tip_14));
|
|
Log.i(Config.LOG_TAG, StringUtils.getString(R.string.log_tip_14));
|
|
XLog.i(StringUtils.getString(R.string.log_tip_14));
|
|
XLog.i(StringUtils.getString(R.string.log_tip_14));
|
|
@@ -88,7 +81,7 @@ public final class WsManager {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void onMessage(WebSocket webSocket, String text) {
|
|
|
|
|
|
+ public void onMessage(@NonNull WebSocket webSocket, @NonNull String text) {
|
|
super.onMessage(webSocket, text);
|
|
super.onMessage(webSocket, text);
|
|
JSONObject json = JSON.parseObject(text);
|
|
JSONObject json = JSON.parseObject(text);
|
|
String command = json.getString("command");
|
|
String command = json.getString("command");
|
|
@@ -194,7 +187,7 @@ public final class WsManager {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void onClosing(WebSocket webSocket, int code, String reason) {
|
|
|
|
|
|
+ public void onClosing(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
|
|
super.onClosing(webSocket, code, reason);
|
|
super.onClosing(webSocket, code, reason);
|
|
webSocket.close(code, reason);
|
|
webSocket.close(code, reason);
|
|
Log.i(Config.LOG_TAG, StringUtils.getString(R.string.log_tip_15_s, " "));
|
|
Log.i(Config.LOG_TAG, StringUtils.getString(R.string.log_tip_15_s, " "));
|
|
@@ -203,7 +196,7 @@ public final class WsManager {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void onClosed(WebSocket webSocket, int code, String reason) {
|
|
|
|
|
|
+ public void onClosed(@NonNull WebSocket webSocket, int code, @NonNull String reason) {
|
|
super.onClosed(webSocket, code, reason);
|
|
super.onClosed(webSocket, code, reason);
|
|
reconnectWebSocket();
|
|
reconnectWebSocket();
|
|
Log.i(Config.LOG_TAG, StringUtils.getString(R.string.log_tip_16, reason));
|
|
Log.i(Config.LOG_TAG, StringUtils.getString(R.string.log_tip_16, reason));
|
|
@@ -213,7 +206,7 @@ public final class WsManager {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
|
|
|
|
|
+ public void onFailure(@NonNull WebSocket webSocket, @NonNull Throwable t, Response response) {
|
|
super.onFailure(webSocket, t, response);
|
|
super.onFailure(webSocket, t, response);
|
|
reconnectWebSocket();
|
|
reconnectWebSocket();
|
|
Log.e(Config.LOG_TAG, StringUtils.getString(R.string.log_tip_18_s, t.getMessage()), t);
|
|
Log.e(Config.LOG_TAG, StringUtils.getString(R.string.log_tip_18_s, t.getMessage()), t);
|
|
@@ -323,7 +316,7 @@ public final class WsManager {
|
|
}
|
|
}
|
|
|
|
|
|
private static void sendWebSocketMsg(String request_id, String return_type, String message) {
|
|
private static void sendWebSocketMsg(String request_id, String return_type, String message) {
|
|
- HashMap map = new HashMap();
|
|
|
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
map.put("request_id", request_id);
|
|
map.put("request_id", request_id);
|
|
map.put("return_type", return_type);
|
|
map.put("return_type", return_type);
|
|
map.put("message", message);
|
|
map.put("message", message);
|
|
@@ -392,11 +385,7 @@ public final class WsManager {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取根文件列表
|
|
|
|
- *
|
|
|
|
- * @param request_id
|
|
|
|
- */
|
|
|
|
|
|
+
|
|
private static void getFileList(String request_id) {
|
|
private static void getFileList(String request_id) {
|
|
try {
|
|
try {
|
|
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
|
|
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
|
|
@@ -419,12 +408,7 @@ public final class WsManager {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 根据路径获取文件列表
|
|
|
|
- *
|
|
|
|
- * @param request_id
|
|
|
|
- * @param path
|
|
|
|
- */
|
|
|
|
|
|
+
|
|
private static void getPath(String request_id, String path) {
|
|
private static void getPath(String request_id, String path) {
|
|
try {
|
|
try {
|
|
path = Environment.getExternalStorageDirectory().getAbsolutePath() + path;
|
|
path = Environment.getExternalStorageDirectory().getAbsolutePath() + path;
|
|
@@ -446,12 +430,7 @@ public final class WsManager {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 根据路径获取文件
|
|
|
|
- *
|
|
|
|
- * @param request_id
|
|
|
|
- * @param path
|
|
|
|
- */
|
|
|
|
|
|
+
|
|
private static void getFileByPath(String request_id, String path) {
|
|
private static void getFileByPath(String request_id, String path) {
|
|
try {
|
|
try {
|
|
path = Environment.getExternalStorageDirectory().getAbsolutePath() + path;
|
|
path = Environment.getExternalStorageDirectory().getAbsolutePath() + path;
|
|
@@ -463,12 +442,6 @@ public final class WsManager {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- /**
|
|
|
|
- * 删除文件或目录
|
|
|
|
- *
|
|
|
|
- * @param request_id
|
|
|
|
- * @param path 删除目录的路径
|
|
|
|
- */
|
|
|
|
private static void deleteFile(String request_id, String path) {
|
|
private static void deleteFile(String request_id, String path) {
|
|
try {
|
|
try {
|
|
path = Environment.getExternalStorageDirectory().getAbsolutePath() + path;
|
|
path = Environment.getExternalStorageDirectory().getAbsolutePath() + path;
|