version:1.4

update:2021-10-21 10:14:40
fix:
add:增加浏览器桌面检测升级,修复重复请求
This commit is contained in:
2021-12-03 14:33:03 +08:00
parent 768519e14e
commit 34fa9fcdb7
70 changed files with 3189 additions and 1503 deletions

View File

@@ -7,8 +7,11 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import com.aoleyun.sn.utils.Logutils;
import com.aoleyun.sn.bean.BaseResponse;
import com.aoleyun.sn.bean.SnRunLog;
import com.aoleyun.sn.network.NetInterfaceManager;
import com.aoleyun.sn.utils.Utils;
import java.io.BufferedReader;
@@ -19,6 +22,16 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
public class LogcatService extends Service {
public final static String LOGCAT_START_ACTION = "START";
@@ -58,11 +71,33 @@ public class LogcatService extends Service {
//// exec.destroy();
// } catch (IOException e) {
//// e.printStackTrace();
// Logutils.e("第一个是Logcat", e.getMessage());
// Log.e("第一个是Logcat", e.getMessage());
// }
registerLogcatReceiver();
}
private static long start_Time;
private static long condition_duration;
private static long condition_file_max_size;
private static boolean clear_file;
public static void setStart_Time(long start_Time) {
LogcatService.start_Time = start_Time;
}
public static void setDuration(long duration) {
condition_duration = duration;
}
public static void setFileMaxSize(long fileMaxSize) {
condition_file_max_size = fileMaxSize * 1024 * 1024;
}
public static void setClearFile(int clearfile) {
clear_file = clearfile == 1;
}
LogcatReceiver receiver;
private void registerLogcatReceiver() {
@@ -103,6 +138,7 @@ public class LogcatService extends Service {
e.printStackTrace();
}
}
uploadFile();
}
@@ -118,6 +154,7 @@ public class LogcatService extends Service {
String logFilePath;
//= getExternalCacheDir() + File.separator + "LOG" + File.separator;
String logFileName;
String tempFilePath;
class LogThread extends Thread {
InputStream is;
@@ -133,7 +170,7 @@ public class LogcatService extends Service {
try {
//新建一个路径信息
File file = new File(logFilePath + logFileName);
Logutils.e(TAG, "run: " + logFilePath);
Log.e(TAG, "run: " + logFilePath);
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
@@ -146,7 +183,7 @@ public class LogcatService extends Service {
os.flush();
}
} catch (Exception e) {
Logutils.e("writelog", "read logcat process failed. message: "
Log.e("writelog", "read logcat process failed. message: "
+ e.getMessage());
} finally {
if (null != os) {
@@ -161,12 +198,7 @@ public class LogcatService extends Service {
}
}
private Runnable LogcatRunnale = new Runnable() {
@Override
public void run() {
getLog();
}
};
private Runnable LogcatRunnale = this::getLog;
public static boolean shouldGetLog = true;
@@ -181,27 +213,41 @@ public class LogcatService extends Service {
BufferedReader bufferedReader;
FileOutputStream fileOutputStream;
File file = new File(logFilePath);
Logutils.e(TAG, "getLog: " + logFilePath);
Log.e(TAG, "getLog: " + logFilePath);
if (!file.exists()) {
file.mkdirs();
}
logFileName = Utils.getSerial() + "-" + getTime() + ".log";
if (clear_file) {
logFileName = Utils.getSerial() + ".log";
} else {
logFileName = Utils.getSerial() + "-" + getTime() + ".log";
}
try {
inputStreamReader = new InputStreamReader(process.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
fileOutputStream = new FileOutputStream(logFilePath + logFileName);
Logutils.e(TAG, "getLog: " + logFilePath + logFileName);
tempFilePath = logFilePath + logFileName;
fileOutputStream = new FileOutputStream(tempFilePath);
Log.e(TAG, "getLog: " + tempFilePath);
long fileSize = file.length();
String logEntry;
while (shouldGetLog) {
logEntry = bufferedReader.readLine() + "\n";
fileOutputStream.write(logEntry.getBytes());
fileOutputStream.flush();
fileSize += logEntry.length();
// Log.e(TAG, "getLog: " + fileSize);
if (fileSize >= condition_file_max_size ||
(start_Time + condition_duration) * 1000 < System.currentTimeMillis()) {
shouldGetLog = false;
stopLogThread();
}
}
inputStreamReader.close();
bufferedReader.close();
fileOutputStream.close();
Logutils.e(TAG, "getLog: " + "closed");
Log.e(TAG, "getLog: " + "closed");
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
@@ -209,20 +255,66 @@ public class LogcatService extends Service {
}
}
private void uploadFile() {
File file = new File(tempFilePath);
if (!file.exists() || file.isDirectory()) {
Log.e("uploadFile", "File does not exists");
return;
}
Map<String, String> params = new HashMap<>();
params.put("sn", Utils.getSerial());
MediaType mediaType = MediaType.Companion.parse("text/plain");
RequestBody fileBody = RequestBody.Companion.create(file, mediaType);
//设置一个file文件
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), fileBody);
NetInterfaceManager.getInstance().getUploadLogApi()
.GetSnRunLog(params, body)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<BaseResponse<SnRunLog>>() {
@Override
public void onSubscribe(Disposable d) {
Log.e("uploadFile", "onSubscribe: ");
}
@Override
public void onNext(BaseResponse<SnRunLog> snRunLogBaseResponse) {
Log.e("uploadFile", "onNext: " + snRunLogBaseResponse);
}
@Override
public void onError(Throwable e) {
Log.e("uploadFile", "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.e("uploadFile", "onComplete: ");
if (clear_file) {
file.delete();
}
}
});
}
class LogcatReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Logutils.e(TAG, "onReceive: " + action);
Log.e(TAG, "onReceive: " + action);
if (TextUtils.isEmpty(action)) {
return;
}
switch (action) {
case LOGCAT_START_ACTION:
Log.e(TAG, "onReceive: "+"开始录制log");
startLogThread();
break;
case LOGCAT_STOP_ACTION:
Log.e(TAG, "onReceive: "+"停止录制log");
stopLogThread();
break;
default: