Browse Source

Wait on shutdown

Mario Danic 8 years ago
parent
commit
4ef5bc8c80

+ 7 - 0
AndroidManifest.xml

@@ -244,6 +244,13 @@
                 <action android:name="android.intent.action.BOOT_COMPLETED" />
             </intent-filter>
         </receiver>
+        <receiver android:name=".services.ShutdownReceiver">
+            <intent-filter>
+                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
+                <action android:name="android.intent.action.QUICKBOOT_POWEROFF" />
+            </intent-filter>
+        </receiver>
+
 
         <service android:name=".services.observer.FileObserverService" />
 

+ 18 - 8
src/com/owncloud/android/services/FileAlterationMagicListener.java

@@ -40,10 +40,10 @@ import java.io.File;
 import java.io.IOException;
 import java.text.ParsePosition;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
-import java.util.HashMap;
+import java.util.List;
 import java.util.Locale;
-import java.util.Map;
 import java.util.TimeZone;
 
 /**
@@ -59,7 +59,9 @@ public class FileAlterationMagicListener implements FileAlterationListener {
     private SyncedFolder syncedFolder;
     private Handler handler = new Handler();
 
-    private Map<String, Runnable> fileRunnable = new HashMap<>();
+    //private Map<String, Runnable> fileRunnable = new HashMap<>();
+
+    private List<String> filesList = new ArrayList<>();
 
     public FileAlterationMagicListener(SyncedFolder syncedFolder) {
         super();
@@ -91,6 +93,7 @@ public class FileAlterationMagicListener implements FileAlterationListener {
     @Override
     public void onFileCreate(final File file) {
         if (file != null) {
+            filesList.add(file.getAbsolutePath());
 
             String mimetypeString = FileStorageUtils.getMimeTypeFromName(file.getAbsolutePath());
             Long lastModificationTime = file.lastModified();
@@ -148,34 +151,41 @@ public class FileAlterationMagicListener implements FileAlterationListener {
                         Log_OC.d(TAG, "Job failed to start: " + result);
                     }
 
-                    fileRunnable.remove(file.getAbsolutePath());
+                    //fileRunnable.remove(file.getAbsolutePath());
+                    filesList.remove(file.getAbsolutePath());
                 }
             };
 
-            fileRunnable.put(file.getAbsolutePath(), runnable);
-            handler.postDelayed(runnable, 1500);
+            //fileRunnable.put(file.getAbsolutePath(), runnable);
+            handler.post(runnable);
         }
 
     }
 
     @Override
     public void onFileChange(File file) {
+        /* Left here for later
         if (fileRunnable.containsKey(file.getAbsolutePath())) {
             handler.removeCallbacks(fileRunnable.get(file.getAbsolutePath()));
             handler.postDelayed(fileRunnable.get(file.getAbsolutePath()), 1500);
-        }
+        }*/
     }
 
     @Override
     public void onFileDelete(File file) {
+        /* Left here for later
         if (fileRunnable.containsKey(file.getAbsolutePath())) {
             handler.removeCallbacks(fileRunnable.get(file.getAbsolutePath()));
             fileRunnable.remove(file.getAbsolutePath());
-        }
+        }*/
     }
 
     @Override
     public void onStop(FileAlterationObserver observer) {
         // This method is intentionally empty
     }
+
+    public int getActiveTasksCount() {
+        return filesList.size();
+    }
 }

+ 42 - 0
src/com/owncloud/android/services/ShutdownReceiver.java

@@ -0,0 +1,42 @@
+/**
+ * Nextcloud Android client application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017 Mario Danic
+ * Copyright (C) 2017 Nextcloud GmbH
+ *
+ * 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.
+ *
+ * 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.
+ *
+ * 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/>.
+ */
+package com.owncloud.android.services;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+
+import com.owncloud.android.MainApp;
+
+/**
+ * Handles shutdown procedure - basically just waits a little bit for all jobs to finish
+ */
+
+public class ShutdownReceiver extends BroadcastReceiver {
+    private static final String TAG = "ShutdownReceiver";
+
+    @Override
+    public void onReceive(final Context context, final Intent intent) {
+        if (MainApp.getSyncedFolderObserverService() != null) {
+            MainApp.getSyncedFolderObserverService().onDestroy();
+        }
+    }
+}

+ 42 - 34
src/com/owncloud/android/services/SyncedFolderJobService.java

@@ -1,22 +1,22 @@
 /**
- *   Nextcloud Android client application
+ * Nextcloud Android client application
  *
- *   @author Tobias Kaminsky
- *   Copyright (C) 2016 Tobias Kaminsky
- *   Copyright (C) 2016 Nextcloud
- *
- *   This program is free software; you can redistribute it and/or
- *   modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- *   License as published by the Free Software Foundation; either
- *   version 3 of the License, or any later version.
- *
- *   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.
- *
- *   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/>.
+ * @author Tobias Kaminsky
+ * Copyright (C) 2016 Tobias Kaminsky
+ * Copyright (C) 2016 Nextcloud
+ * <p>
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or 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/>.
  */
 
 package com.owncloud.android.services;
@@ -28,6 +28,7 @@ import android.app.job.JobService;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Build;
+import android.os.Handler;
 import android.os.PersistableBundle;
 
 import com.owncloud.android.MainApp;
@@ -55,33 +56,40 @@ public class SyncedFolderJobService extends JobService {
 
     @Override
     public boolean onStartJob(JobParameters params) {
-        Context context = MainApp.getAppContext();
+        final Context context = MainApp.getAppContext();
         PersistableBundle bundle = params.getExtras();
-        String filePath = bundle.getString(LOCAL_PATH);
-        String remotePath = bundle.getString(REMOTE_PATH);
-        Account account = AccountUtils.getOwnCloudAccountByName(context, bundle.getString(ACCOUNT));
-        Integer uploadBehaviour = bundle.getInt(UPLOAD_BEHAVIOUR);
+        final String filePath = bundle.getString(LOCAL_PATH);
+        final String remotePath = bundle.getString(REMOTE_PATH);
+        final Account account = AccountUtils.getOwnCloudAccountByName(context, bundle.getString(ACCOUNT));
+        final Integer uploadBehaviour = bundle.getInt(UPLOAD_BEHAVIOUR);
 
         Log_OC.d(TAG, "startJob: " + params.getJobId() + ", filePath: " + filePath);
 
         File file = new File(filePath);
 
+        Handler handler = new Handler();
+
         // File can be deleted between job generation and job execution. If file does not exist, just ignore it
         if (file.exists()) {
-            String mimeType = MimeTypeUtil.getBestMimeTypeByFilename(file.getAbsolutePath());
+            final String mimeType = MimeTypeUtil.getBestMimeTypeByFilename(file.getAbsolutePath());
 
-            FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
+            final FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
 
-            requester.uploadNewFile(
-                    context,
-                    account,
-                    filePath,
-                    remotePath,
-                    uploadBehaviour,
-                    mimeType,
-                    true,           // create parent folder if not existent
-                    UploadFileOperation.CREATED_AS_INSTANT_PICTURE
-            );
+            handler.postDelayed(new Runnable() {
+                @Override
+                public void run() {
+                    requester.uploadNewFile(
+                            context,
+                            account,
+                            filePath,
+                            remotePath,
+                            uploadBehaviour,
+                            mimeType,
+                            true,           // create parent folder if not existent
+                            UploadFileOperation.CREATED_AS_INSTANT_PICTURE
+                    );
+                }
+            }, 5000);
         }
         return false;
     }

+ 10 - 1
src/com/owncloud/android/services/observer/FileAlterationMagicObserver.java

@@ -38,7 +38,10 @@
  */
 package com.owncloud.android.services.observer;
 
+import android.os.SystemClock;
+
 import com.owncloud.android.datamodel.SyncedFolder;
+import com.owncloud.android.services.FileAlterationMagicListener;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.comparator.NameFileComparator;
@@ -170,7 +173,13 @@ public class FileAlterationMagicObserver extends FileAlterationObserver implemen
      * @throws Exception if an error occurs
      */
     public void destroy() throws Exception {
-        // does nothing
+        while (getListeners().iterator().hasNext()) {
+            FileAlterationMagicListener fileAlterationListener = (FileAlterationMagicListener)
+                    getListeners().iterator().next();
+            while (fileAlterationListener.getActiveTasksCount() > 0) {
+                SystemClock.sleep(250);
+            }
+        }
     }
 
     /**

+ 16 - 1
src/com/owncloud/android/services/observer/SyncedFolderObserverService.java

@@ -26,6 +26,7 @@ import android.app.Service;
 import android.content.Intent;
 import android.os.Binder;
 import android.os.IBinder;
+import android.util.Log;
 
 import com.owncloud.android.MainApp;
 import com.owncloud.android.datamodel.SyncedFolder;
@@ -84,6 +85,21 @@ public class SyncedFolderObserverService extends Service {
 
     }
 
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        Log.d("MARIO", "DESTROY");
+        for(FileAlterationObserver fileAlterationObserver : monitor.getObservers()) {
+            FileAlterationMagicObserver fileAlterationMagicObserver = (FileAlterationMagicObserver)
+                    fileAlterationObserver;
+            try {
+                fileAlterationMagicObserver.destroy();
+            } catch (Exception e) {
+                Log_OC.d(TAG, "Something went very wrong on trying to destroy observers");
+            }
+        }
+    }
+
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
         return Service.START_NOT_STICKY;
@@ -121,7 +137,6 @@ public class SyncedFolderObserverService extends Service {
                 fileAlterationMagicObserver = new FileAlterationMagicObserver(syncedFolder, fileFilter);
                 fileAlterationMagicObserver.init();
                 fileAlterationMagicObserver.addListener(new FileAlterationMagicListener(syncedFolder));
-                monitor.addObserver(fileAlterationMagicObserver);
             } catch (Exception e) {
                 Log_OC.d(TAG, "Failed getting an observer to intialize");
             }