Browse Source

[Modify]调整相册备份/恢复执行顺序

zhipeng 4 months ago
parent
commit
b25426e8f5

+ 1 - 1
app/src/main/java/com/datarecovery/master/module/imgrecover/ImageItemAdapter.java

@@ -67,7 +67,7 @@ public class ImageItemAdapter extends RecyclerView.Adapter<ImageItemAdapter.View
             if (o1 == null || o2 == null) {
                 return 0;
             }
-            return Long.compare(o1.getLastModified(), o2.getLastModified());
+            return Long.compare(o2.getLastModified(), o1.getLastModified());
         });
         int itemCount;
         if (list != null) {

+ 24 - 1
app/src/main/java/com/datarecovery/master/utils/GalleryBackupManager.java

@@ -113,7 +113,18 @@ public class GalleryBackupManager implements ActivityManager.ProcessLifecycleLis
                 AtmobLog.d(TAG, "Starting gallery backup...");
                 long startTime = System.currentTimeMillis();
                 try {
+                    doRecovery(); // attempt recovery first
+                } catch (Exception e) {
+                    AtmobLog.e(TAG, "Error during gallery recovery: %s", e.getMessage(), e);
+                } finally {
+                    long duration = System.currentTimeMillis() - startTime;
+                    AtmobLog.d(TAG, "Gallery recovery completed in %d ms", duration);
+                }
+                startTime = System.currentTimeMillis(); // reset start time for backup
+                try {
                     doBackup();
+                } catch (Exception e) {
+                    AtmobLog.e(TAG, "Error during gallery backup: %s", e.getMessage(), e);
                 } finally {
                     isBackupInProgress.set(false); // reset backup flag
                     long duration = System.currentTimeMillis() - startTime;
@@ -163,10 +174,23 @@ public class GalleryBackupManager implements ActivityManager.ProcessLifecycleLis
                 count++;
             }
         }
+        cleanupOldBackups();
+    }
 
+    private void doRecovery() {
+        Uri collection;
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+            collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
+        } else {
+            collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
+        }
+        String[] projection = new String[]{
+                MediaStore.Images.Media._ID
+        };
         long oldestDate = KVUtils.getDefault().getLong(KEY_OLDEST_IMAGE_ADDED_DATE, Long.MAX_VALUE);
         String selection = MediaStore.Images.Media.DATE_ADDED + " >= ?";
         String[] selectionArgs = new String[]{String.valueOf(oldestDate)};
+        String sortOrder = MediaStore.Images.Media.DATE_ADDED + " DESC";
         try (Cursor cursor = contentResolver.query(
                 collection,
                 projection,
@@ -189,7 +213,6 @@ public class GalleryBackupManager implements ActivityManager.ProcessLifecycleLis
             }
             checkRecoveredImages(imagePaths);
         }
-        cleanupOldBackups();
     }
 
     private long getBitmapSizePerPixel(String displayName, String mimeType) {