Browse Source

Attempt to fix HDR issues

Mario Danic 8 năm trước cách đây
mục cha
commit
3fdd90c545

+ 51 - 32
src/com/owncloud/android/services/FileAlterationMagicListener.java

@@ -3,17 +3,17 @@
  *
  * @author Mario Danic
  * Copyright (C) 2017 Mario Danic
- * <p>
+ *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * at your option) any later version.
- * <p>
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Affero General Public License for more details.
- * <p>
+ *
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
@@ -23,6 +23,7 @@ import android.app.job.JobInfo;
 import android.app.job.JobScheduler;
 import android.content.ComponentName;
 import android.content.Context;
+import android.os.Handler;
 import android.os.PersistableBundle;
 
 import com.owncloud.android.MainApp;
@@ -35,6 +36,8 @@ import org.apache.commons.io.monitor.FileAlterationObserver;
 
 import java.io.File;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * Magical file alteration listener
@@ -47,6 +50,9 @@ public class FileAlterationMagicListener implements FileAlterationListener {
     private Context context;
 
     private SyncedFolder syncedFolder;
+    private Handler handler = new Handler();
+
+    private Map<String, Runnable> fileRunnable = new HashMap<>();
 
     public FileAlterationMagicListener(SyncedFolder syncedFolder) {
         super();
@@ -76,40 +82,53 @@ public class FileAlterationMagicListener implements FileAlterationListener {
     }
 
     @Override
-    public void onFileCreate(File file) {
-        PersistableBundle bundle = new PersistableBundle();
-        // TODO extract
-        bundle.putString(SyncedFolderJobService.LOCAL_PATH, file.getAbsolutePath());
-        bundle.putString(SyncedFolderJobService.REMOTE_PATH, FileStorageUtils.getInstantUploadFilePath(
-                syncedFolder.getRemotePath(), file.getName(),
-                new Date().getTime(),
-                syncedFolder.getSubfolderByDate()));
-        bundle.putString(SyncedFolderJobService.ACCOUNT, syncedFolder.getAccount());
-        bundle.putInt(SyncedFolderJobService.UPLOAD_BEHAVIOUR, syncedFolder.getUploadAction());
-
-        JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
-
-        Long date = new Date().getTime();
-        JobInfo job = new JobInfo.Builder(
-                date.intValue(),
-                new ComponentName(context, SyncedFolderJobService.class))
-                .setRequiresCharging(syncedFolder.getChargingOnly())
-                .setMinimumLatency(10000)
-                .setRequiredNetworkType(syncedFolder.getWifiOnly() ? JobInfo.NETWORK_TYPE_UNMETERED : JobInfo.NETWORK_TYPE_ANY)
-                .setExtras(bundle)
-                .setPersisted(true)
-                .build();
-
-        Integer result = js.schedule(job);
-        if (result <= 0) {
-            Log_OC.d(TAG, "Job failed to start: " + result);
-        }
+    public void onFileCreate(final File file) {
+        final Runnable runnable = new Runnable() {
+            @Override
+            public void run() {
+                PersistableBundle bundle = new PersistableBundle();
+                // TODO extract
+                bundle.putString(SyncedFolderJobService.LOCAL_PATH, file.getAbsolutePath());
+                bundle.putString(SyncedFolderJobService.REMOTE_PATH, FileStorageUtils.getInstantUploadFilePath(
+                        syncedFolder.getRemotePath(), file.getName(),
+                        new Date().getTime(),
+                        syncedFolder.getSubfolderByDate()));
+                bundle.putString(SyncedFolderJobService.ACCOUNT, syncedFolder.getAccount());
+                bundle.putInt(SyncedFolderJobService.UPLOAD_BEHAVIOUR, syncedFolder.getUploadAction());
+
+                JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
+
+                Long date = new Date().getTime();
+                JobInfo job = new JobInfo.Builder(
+                        date.intValue(),
+                        new ComponentName(context, SyncedFolderJobService.class))
+                        .setRequiresCharging(syncedFolder.getChargingOnly())
+                        .setMinimumLatency(10000)
+                        .setRequiredNetworkType(syncedFolder.getWifiOnly() ? JobInfo.NETWORK_TYPE_UNMETERED : JobInfo.NETWORK_TYPE_ANY)
+                        .setExtras(bundle)
+                        .setPersisted(true)
+                        .build();
+
+                Integer result = js.schedule(job);
+                if (result <= 0) {
+                    Log_OC.d(TAG, "Job failed to start: " + result);
+                }
+
+                fileRunnable.remove(file.getAbsolutePath());
+            }
+        };
+
+        fileRunnable.put(file.getAbsolutePath(), runnable);
+        handler.postDelayed(runnable, 500);
 
     }
 
     @Override
     public void onFileChange(File file) {
-        // This method is intentionally empty
+        if (fileRunnable.containsKey(file.getAbsolutePath())) {
+            handler.removeCallbacks(fileRunnable.get(file.getAbsolutePath()));
+            handler.postDelayed(fileRunnable.get(file.getAbsolutePath()), 500);
+        }
     }
 
     @Override