refactor(diagnostics): 将截图逻辑从 PushManager 迁移至 RemoteDiagnostics
- 移除 PushManager 中 doscreenshot、getScreenshot、uploadScreenshot 方法 - 清理 PushManager 中不再需要的 import 依赖 - 在 RemoteDiagnostics 中新增 pushScreenshot 方法承载截图上传逻辑 - 推送处理入口改为调用 RemoteDiagnostics.getInstance().pushScreenshot()
This commit is contained in:
1 parent
cac9c50948
commit
d643f7980e
2 files changed
+53
-79
No files matched your search
@@ -16,7 +16,6 @@ import android.view.Gravity;
|
|||||||
import android.view.WindowManager;
|
import android.view.WindowManager;
|
||||||
|
|
||||||
import com.android.internal.view.RotationPolicy;
|
import com.android.internal.view.RotationPolicy;
|
||||||
import com.fuying.sn.core.common.device.DeviceIdUtils;
|
|
||||||
import com.fuying.sn.core.common.gson.GsonUtils;
|
import com.fuying.sn.core.common.gson.GsonUtils;
|
||||||
import com.fuying.sn.core.common.ui.ToastUtil;
|
import com.fuying.sn.core.common.ui.ToastUtil;
|
||||||
import com.fuying.sn.core.data.dto.common.BaseResponse;
|
import com.fuying.sn.core.data.dto.common.BaseResponse;
|
||||||
@@ -29,7 +28,6 @@ import com.fuying.sn.platform.app.ApkUtils;
|
|||||||
import com.fuying.sn.platform.app.AppProcessUtils;
|
import com.fuying.sn.platform.app.AppProcessUtils;
|
||||||
import com.fuying.sn.platform.control.AppWhitelistUtils;
|
import com.fuying.sn.platform.control.AppWhitelistUtils;
|
||||||
import com.fuying.sn.platform.control.ControlManager;
|
import com.fuying.sn.platform.control.ControlManager;
|
||||||
import com.fuying.sn.platform.system.CmdUtil;
|
|
||||||
import com.fuying.sn.platform.system.DeviceManager;
|
import com.fuying.sn.platform.system.DeviceManager;
|
||||||
import com.fuying.sn.platform.system.JgyUtils;
|
import com.fuying.sn.platform.system.JgyUtils;
|
||||||
import com.fuying.sn.platform.system.SystemUtils;
|
import com.fuying.sn.platform.system.SystemUtils;
|
||||||
@@ -50,8 +48,6 @@ import com.fuying.sn.runtime.sync.RemoteDiagnostics;
|
|||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
@@ -65,9 +61,6 @@ import io.reactivex.rxjava3.functions.Consumer;
|
|||||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||||
import io.reactivex.rxjava3.subjects.PublishSubject;
|
import io.reactivex.rxjava3.subjects.PublishSubject;
|
||||||
import io.reactivex.rxjava3.subjects.Subject;
|
import io.reactivex.rxjava3.subjects.Subject;
|
||||||
import okhttp3.MediaType;
|
|
||||||
import okhttp3.MultipartBody;
|
|
||||||
import okhttp3.RequestBody;
|
|
||||||
|
|
||||||
public class PushManager {
|
public class PushManager {
|
||||||
private static final String TAG = "PushManager";
|
private static final String TAG = "PushManager";
|
||||||
@@ -834,7 +827,7 @@ public class PushManager {
|
|||||||
|
|
||||||
private void handleSnapshotPush(String extras) {
|
private void handleSnapshotPush(String extras) {
|
||||||
ToastUtil.debugShow("收到推送消息: 截图");
|
ToastUtil.debugShow("收到推送消息: 截图");
|
||||||
doscreenshot();
|
RemoteDiagnostics.getInstance().pushScreenshot();
|
||||||
Log.e(TAG, extras);
|
Log.e(TAG, extras);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1406,73 +1399,6 @@ public class PushManager {
|
|||||||
DeviceSettingsSync.getInstance().getLockScreenPwd();
|
DeviceSettingsSync.getInstance().getLockScreenPwd();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行截图命令,返回命令退出码(0 表示成功)。
|
|
||||||
* 线程切换交给 {@link DeviceRepository#schedule},避免每处调用各写一遍。
|
|
||||||
*/
|
|
||||||
private Observable<Integer> getScreenshot(String filePath) {
|
|
||||||
return DeviceRepository.getInstance().schedule(Observable.<Integer>create(e -> {
|
|
||||||
int code = CmdUtil.execute("screencap -p " + filePath).code;
|
|
||||||
e.onNext(code);
|
|
||||||
e.onComplete();
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 上传截图文件。
|
|
||||||
*/
|
|
||||||
private Observable<BaseResponse> uploadScreenshot(File file) {
|
|
||||||
MediaType mediaType = MediaType.Companion.parse("image/png");
|
|
||||||
RequestBody requestBody = RequestBody.Companion.create(file, mediaType);
|
|
||||||
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
|
|
||||||
return DeviceRepository.getInstance().uploadScreenshot(body);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 截图并上传:截图失败或文件未落盘时直接在链上抛异常,由下游 onError 统一记录,
|
|
||||||
* 避免在链式调用里再嵌套一层 if 判断。
|
|
||||||
*/
|
|
||||||
public void doscreenshot() {
|
|
||||||
File dbDir = mContext.getExternalFilesDir("db");
|
|
||||||
if (dbDir == null) {
|
|
||||||
Log.e(TAG, "doscreenshot: 外部存储不可用,放弃本次截图");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String filePath = dbDir.getAbsolutePath()
|
|
||||||
+ File.separator + DeviceIdUtils.getSerial() + ".png";
|
|
||||||
getScreenshot(filePath)
|
|
||||||
.map(code -> {
|
|
||||||
File file = new File(filePath);
|
|
||||||
if (code != 0 || !file.exists()) {
|
|
||||||
throw new FileNotFoundException(filePath + ", code=" + code);
|
|
||||||
}
|
|
||||||
return file;
|
|
||||||
})
|
|
||||||
.flatMap(this::uploadScreenshot)
|
|
||||||
.to(BackgroundRequests.submit(new Observer<BaseResponse>() {
|
|
||||||
@Override
|
|
||||||
public void onSubscribe(@NonNull Disposable d) {
|
|
||||||
Log.e(TAG, "doscreenshot onSubscribe");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onNext(@NonNull BaseResponse baseResponse) {
|
|
||||||
Log.e(TAG, "doscreenshot onNext: " + baseResponse);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onError(@NonNull Throwable e) {
|
|
||||||
Log.e(TAG, "doscreenshot onError: " + e.getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onComplete() {
|
|
||||||
Log.e(TAG, "doscreenshot onComplete");
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void hideDesktopIcon() {
|
private void hideDesktopIcon() {
|
||||||
DeviceRepository.removeCache(UrlAddress.GET_SETTINGS);
|
DeviceRepository.removeCache(UrlAddress.GET_SETTINGS);
|
||||||
DeviceSettingsSync.getInstance().getSystemSettings();
|
DeviceSettingsSync.getInstance().getSystemSettings();
|
||||||
|
|||||||
@@ -9,13 +9,13 @@ import android.util.Log;
|
|||||||
|
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
|
import com.fuying.sn.core.common.device.DeviceIdUtils;
|
||||||
import com.fuying.sn.core.data.dto.common.BaseResponse;
|
import com.fuying.sn.core.data.dto.common.BaseResponse;
|
||||||
import com.fuying.sn.core.data.dto.device.SnRunLog;
|
import com.fuying.sn.core.data.dto.device.SnRunLog;
|
||||||
import com.fuying.sn.core.data.repository.DeviceRepository;
|
|
||||||
import com.fuying.sn.core.data.remote.BackgroundRequests;
|
import com.fuying.sn.core.data.remote.BackgroundRequests;
|
||||||
import com.fuying.sn.runtime.service.LogcatService;
|
import com.fuying.sn.core.data.repository.DeviceRepository;
|
||||||
import com.fuying.sn.platform.system.CmdUtil;
|
import com.fuying.sn.platform.system.CmdUtil;
|
||||||
import com.fuying.sn.core.common.device.DeviceIdUtils;
|
import com.fuying.sn.runtime.service.LogcatService;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@@ -29,7 +29,6 @@ import io.reactivex.rxjava3.core.Observer;
|
|||||||
import io.reactivex.rxjava3.disposables.Disposable;
|
import io.reactivex.rxjava3.disposables.Disposable;
|
||||||
import io.reactivex.rxjava3.functions.Function;
|
import io.reactivex.rxjava3.functions.Function;
|
||||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||||
|
|
||||||
import okhttp3.MediaType;
|
import okhttp3.MediaType;
|
||||||
import okhttp3.MultipartBody;
|
import okhttp3.MultipartBody;
|
||||||
import okhttp3.RequestBody;
|
import okhttp3.RequestBody;
|
||||||
@@ -38,6 +37,7 @@ import okhttp3.RequestBody;
|
|||||||
* 处理远程日志采集配置和设备截图上传。
|
* 处理远程日志采集配置和设备截图上传。
|
||||||
*/
|
*/
|
||||||
public final class RemoteDiagnostics {
|
public final class RemoteDiagnostics {
|
||||||
|
private static final String TAG = "RemoteDiagnostics";
|
||||||
private static volatile RemoteDiagnostics instance;
|
private static volatile RemoteDiagnostics instance;
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final DeviceRepository repository;
|
private final DeviceRepository repository;
|
||||||
@@ -135,6 +135,54 @@ public final class RemoteDiagnostics {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 推送"截图"指令触发的截图上传。
|
||||||
|
* 与 {@link #screenshot()} 走不同的上传接口,因此单独保留一条链路,不要合并。
|
||||||
|
*/
|
||||||
|
public void pushScreenshot() {
|
||||||
|
File dbDir = mContext.getExternalFilesDir("db");
|
||||||
|
if (dbDir == null) {
|
||||||
|
Log.e(TAG, "pushScreenshot: 外部存储不可用,放弃本次截图");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String filePath = dbDir.getAbsolutePath() + File.separator + DeviceIdUtils.getSerial() + ".png";
|
||||||
|
getScreenshot(filePath)
|
||||||
|
.map(code -> {
|
||||||
|
File file = new File(filePath);
|
||||||
|
if (code != 0 || !file.exists()) {
|
||||||
|
throw new FileNotFoundException(filePath + ", code=" + code);
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
})
|
||||||
|
.flatMap(file -> {
|
||||||
|
MediaType mediaType = MediaType.Companion.parse("image/png");
|
||||||
|
RequestBody requestBody = RequestBody.Companion.create(file, mediaType);
|
||||||
|
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
|
||||||
|
return repository.uploadScreenshot(body);
|
||||||
|
})
|
||||||
|
.to(BackgroundRequests.submit(new Observer<BaseResponse>() {
|
||||||
|
@Override
|
||||||
|
public void onSubscribe(Disposable d) {
|
||||||
|
Log.e(TAG, "pushScreenshot onSubscribe");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNext(BaseResponse baseResponse) {
|
||||||
|
Log.e(TAG, "pushScreenshot onNext: " + baseResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(Throwable e) {
|
||||||
|
Log.e(TAG, "pushScreenshot onError: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onComplete() {
|
||||||
|
Log.e(TAG, "pushScreenshot onComplete");
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
private static final int REQUEST_CODE = 12345;
|
private static final int REQUEST_CODE = 12345;
|
||||||
|
|
||||||
public void getRunLog() {
|
public void getRunLog() {
|
||||||
|
|||||||
Reference in new issue
Block a user