浏览代码

Fix #2094

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 年之前
父节点
当前提交
e27b2f7689

+ 6 - 4
src/main/java/com/owncloud/android/files/BootupBroadcastReceiver.java

@@ -52,10 +52,12 @@ public class BootupBroadcastReceiver extends BroadcastReceiver {
             Log_OC.d(TAG, "Starting file observer service...");
             Intent initObservers = FileObserverService.makeInitIntent(context);
 
-            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
-                context.startForegroundService(initObservers);
-            } else {
-                context.startService(initObservers);
+            if (FileObserverService.shouldStart()) {
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+                    context.startForegroundService(initObservers);
+                } else {
+                    context.startService(initObservers);
+                }
             }
 
             MainApp.initAutoUpload();

+ 23 - 0
src/main/java/com/owncloud/android/services/observer/FileObserverService.java

@@ -94,6 +94,29 @@ public class FileObserverService extends Service {
         return i;
     }
 
+    public static boolean shouldStart() {
+
+        // query for any favorite file in any OC account
+        Cursor cursorOnKeptInSync = MainApp.getAppContext().getContentResolver().query(
+                ProviderTableMeta.CONTENT_URI,
+                null,
+                ProviderTableMeta.FILE_KEEP_IN_SYNC + " = ?",
+                new String[]{String.valueOf(1)},
+                null
+        );
+
+        boolean returnValue = false;
+        if (cursorOnKeptInSync != null && cursorOnKeptInSync.moveToFirst()) {
+            returnValue = true;
+        }
+
+        if (cursorOnKeptInSync != null) {
+            cursorOnKeptInSync.close();
+        }
+
+        return returnValue;
+    }
+
     /**
      * Factory method to create intents that allow to start or stop the
      * observance of a file.

+ 8 - 1
src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -190,7 +190,14 @@ public class FileDisplayActivity extends HookActivity
         /// grant that FileObserverService is watching favorite files
         if (savedInstanceState == null) {
             Intent initObserversIntent = FileObserverService.makeInitIntent(this);
-            startService(initObserversIntent);
+
+            if (FileObserverService.shouldStart()) {
+                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+                    this.startForegroundService(initObserversIntent);
+                } else {
+                    this.startService(initObserversIntent);
+                }
+            }
         }
 
         /// Load of saved instance state

+ 6 - 1
src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java

@@ -729,7 +729,12 @@ public class FileOperationsHelper {
                     file,
                     mFileActivity.getAccount(),
                     isAvailableOffline);
-            mFileActivity.startService(observedFileIntent);
+
+            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
+                mFileActivity.startForegroundService(observedFileIntent);
+            } else {
+                mFileActivity.startService(observedFileIntent);
+            }
 
             /// immediate content synchronization
             if (file.isAvailableOffline()) {