Bladeren bron

First commit

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 jaren geleden
bovenliggende
commit
9c6dab3238

+ 8 - 6
src/main/java/com/owncloud/android/MainApp.java

@@ -40,8 +40,8 @@ import com.owncloud.android.datamodel.SyncedFolder;
 import com.owncloud.android.datamodel.SyncedFolderProvider;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.db.PreferenceManager;
+import com.owncloud.android.jobs.FilesSyncJob;
 import com.owncloud.android.jobs.NCJobCreator;
-import com.owncloud.android.jobs.NewAutoUploadJob;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
 import com.owncloud.android.lib.common.OwnCloudClientManagerFactory.Policy;
 import com.owncloud.android.lib.common.utils.Log_OC;
@@ -127,11 +127,13 @@ public class MainApp extends MultiDexApplication {
             PreferenceManager.setAutoUploadSplitEntries(this, true);
         }
 
-        new JobRequest.Builder(NewAutoUploadJob.TAG)
-                .setExecutionWindow(3000L, 4000L)
-                .setUpdateCurrent(true)
-                .build()
-                .schedule();
+        if (!JobManager.instance().getAllJobRequestsForTag(FilesSyncJob.TAG).isEmpty()) {
+            new JobRequest.Builder(FilesSyncJob.TAG)
+                    .setPeriodic(900000L, 300000L)
+                    .setUpdateCurrent(true)
+                    .build()
+                    .schedule();
+        }
 
         // register global protection with pass code
         registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

+ 12 - 0
src/main/java/com/owncloud/android/datamodel/ArbitraryDataProvider.java

@@ -58,6 +58,18 @@ public class ArbitraryDataProvider {
         return result;
     }
 
+    public int deleteForKeyWhereAccountNotIn(ArrayList<String> accounts, String key) {
+
+        int result = contentResolver.delete(
+                ProviderMeta.ProviderTableMeta.CONTENT_URI_ARBITRARY_DATA,
+                ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_CLOUD_ID + " NOT IN (?) AND " +
+                        ProviderMeta.ProviderTableMeta.ARBITRARY_DATA_KEY + "= ?",
+                new String[]{String.valueOf(accounts), key}
+        );
+
+        return result;
+    }
+
 
     public void storeOrUpdateKeyValue(String accountName, String key, String newValue) {
         ArbitraryDataSet data = getArbitraryDataSet(accountName, key);

+ 7 - 7
src/main/java/com/owncloud/android/datamodel/FileSystemDataSet.java

@@ -29,20 +29,20 @@ public class FileSystemDataSet {
     private boolean isFolder;
     private boolean isSentForUpload;
     private long foundAt;
-    private boolean isFileModified;
+    private String account;
 
     public FileSystemDataSet() {
     }
 
     public FileSystemDataSet(int id, String localPath, long modifiedAt, boolean isFolder,
-                             boolean isSentForUpload, long foundAt, boolean isFileModified) {
+                             boolean isSentForUpload, long foundAt, String account) {
         this.id = id;
         this.localPath = localPath;
         this.modifiedAt = modifiedAt;
         this.isFolder = isFolder;
         this.isSentForUpload = isSentForUpload;
         this.foundAt = foundAt;
-        this.isFileModified = isFileModified;
+        this.account = account;
     }
 
     public int getId() {
@@ -93,11 +93,11 @@ public class FileSystemDataSet {
         isSentForUpload = sentForUpload;
     }
 
-    public boolean isFileModified() {
-        return isFileModified;
+    public String getAccount() {
+        return account;
     }
 
-    public void setFileModified(boolean fileModified) {
-        isFileModified = fileModified;
+    public void setAccount(String account) {
+        this.account = account;
     }
 }

+ 54 - 38
src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java

@@ -30,6 +30,8 @@ import com.owncloud.android.lib.common.utils.Log_OC;
 import java.util.HashSet;
 import java.util.Set;
 
+import javax.annotation.Nullable;
+
 public class FilesystemDataProvider {
 
     static private final String TAG = FilesystemDataProvider.class.getSimpleName();
@@ -43,42 +45,49 @@ public class FilesystemDataProvider {
         this.contentResolver = contentResolver;
     }
 
-    public int updateFilesInList(Object[] paths) {
+    public void updateFilesInList(Object[] paths, String account) {
         ContentValues cv = new ContentValues();
         cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD, 1);
 
         String[] stringPaths = new String[paths.length];
-        for(int i = 0; i < paths.length; i++) {
+        for (int i = 0; i < paths.length; i++) {
             stringPaths[i] = (String) paths[i];
         }
 
-        int result = contentResolver.update(
+        contentResolver.update(
                 ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
                 cv,
-                ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " IN (?)",
+                ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " IN (?) and " +
+                        ProviderMeta.ProviderTableMeta.FILESYSTEM_ACCOUNT + " = ?",
                 stringPaths
         );
 
-        return result;
-
     }
 
-    public Object[] getFilesToUploadForPath(String localPath) {
+    public Set<String> getFilesForUpload(String localPath, String account, @Nullable String filetype) {
         Set<String> localPathsToUpload = new HashSet<>();
 
         String likeParam = localPath + "%";
+        String likeFiletypeParam = "";
+        if (filetype != null) {
+            likeFiletypeParam = filetype + "%";
+        } else {
+            likeFiletypeParam = "%";
+        }
+
 
         Cursor cursor = contentResolver.query(
                 ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
                 null,
                 ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " LIKE ? and " +
-                        ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_BEING_MODIFIED + " = ? and " +
+                        ProviderMeta.ProviderTableMeta.FILESYSTEM_ACCOUNT + " = ? and " +
+                        ProviderMeta.ProviderTableMeta.FILESYSTEM_MIMETYPE + " LIKE ? and " +
                         ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD + " = ? and " +
                         ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_IS_FOLDER + " = ?",
-                new String[]{likeParam, "0", "0", "0"},
+                new String[]{likeParam, account, likeFiletypeParam, "0", "0"},
                 null);
 
-        if (cursor.moveToFirst()) {
+        if (cursor != null && cursor.moveToFirst()) {
             do {
                 String value = cursor.getString(cursor.getColumnIndex(
                         ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH));
@@ -88,11 +97,13 @@ public class FilesystemDataProvider {
                     localPathsToUpload.add(value);
                 }
             } while (cursor.moveToNext());
+
+            cursor.close();
+
         }
 
 
-        cursor.close();
-        return localPathsToUpload.toArray();
+        return localPathsToUpload;
 
     }
 
@@ -106,40 +117,45 @@ public class FilesystemDataProvider {
                 new String[]{likeParam},
                 null);
 
-        if (cursor.getCount() == 0) {
-            cursor.close();
-            return 0;
+        if (cursor != null) {
+            if (cursor.getCount() == 0) {
+                cursor.close();
+                return 0;
+            } else {
+                cursor.moveToFirst();
+                int result = cursor.getInt(0);
+                cursor.close();
+                return result;
+            }
         } else {
-            cursor.moveToFirst();
-            int result = cursor.getInt(0);
-            cursor.close();
-            return result;
+            return 0;
         }
     }
 
-    public void storeOrUpdateFileValue(String localPath, long modifiedAt, boolean isFolder, boolean fileBeingModified) {
-        FileSystemDataSet data = getFilesystemDataSet(localPath);
+    public void storeOrUpdateFileValue(String localPath, long modifiedAt, boolean isFolder, String account,
+                                       boolean dryRun, @Nullable String mimetype) {
+        FileSystemDataSet data = getFilesystemDataSet(localPath, account);
 
         int isFolderValue = 0;
         if (isFolder) {
             isFolderValue = 1;
         }
 
-        int isBeingModified = 0;
-        if (fileBeingModified) {
-            isBeingModified = 1;
-        }
 
         ContentValues cv = new ContentValues();
         cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_FOUND_RECENTLY, System.currentTimeMillis());
-        cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_BEING_MODIFIED, isBeingModified);
 
         if (data == null) {
 
             cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH, localPath);
             cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_MODIFIED, modifiedAt);
             cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_IS_FOLDER, isFolderValue);
-            cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD, 0);
+            cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD, dryRun);
+            cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_ACCOUNT, account);
+
+            if (mimetype != null) {
+                cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_MIMETYPE, mimetype);
+            }
 
 
             Uri result = contentResolver.insert(ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM, cv);
@@ -153,6 +169,11 @@ public class FilesystemDataProvider {
                 cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD, 0);
             }
 
+            if (mimetype != null) {
+                cv.put(ProviderMeta.ProviderTableMeta.FILESYSTEM_MIMETYPE, mimetype);
+            }
+
+
             int result = contentResolver.update(
                     ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
                     cv,
@@ -166,12 +187,14 @@ public class FilesystemDataProvider {
         }
     }
 
-    private FileSystemDataSet getFilesystemDataSet(String localPathParam) {
+    private FileSystemDataSet getFilesystemDataSet(String localPathParam, String account) {
+
         Cursor cursor = contentResolver.query(
                 ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
                 null,
-                ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " = ?",
-                new String[]{localPathParam},
+                ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " = ? and " +
+                        ProviderMeta.ProviderTableMeta.FILESYSTEM_ACCOUNT + " = ?",
+                new String[]{localPathParam, account},
                 null
         );
 
@@ -197,18 +220,11 @@ public class FilesystemDataProvider {
                     isSentForUpload = true;
                 }
 
-                boolean isBeingModified = false;
-                if (cursor.getInt(cursor.getColumnIndex(
-                        ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_BEING_MODIFIED)) != 0) {
-                    isBeingModified = true;
-                }
-
-
                 if (id == -1) {
                     Log_OC.e(TAG, "Arbitrary value could not be created from cursor");
                 } else {
                     dataSet = new FileSystemDataSet(id, localPath, modifiedAt, isFolder, isSentForUpload, foundAt,
-                            isBeingModified);
+                            account);
                 }
             }
             cursor.close();

+ 2 - 1
src/main/java/com/owncloud/android/db/ProviderMeta.java

@@ -208,6 +208,7 @@ public class ProviderMeta {
         public static final String FILESYSTEM_FILE_IS_FOLDER = "is_folder";
         public static final String FILESYSTEM_FILE_FOUND_RECENTLY = "found_at";
         public static final String FILESYSTEM_FILE_SENT_FOR_UPLOAD = "upload_triggered";
-        public static final String FILESYSTEM_FILE_BEING_MODIFIED = "file_being_modified";
+        public static final String FILESYSTEM_ACCOUNT = "account";
+        public static final String FILESYSTEM_MIMETYPE = "mimetype";
     }
 }

+ 196 - 0
src/main/java/com/owncloud/android/jobs/FilesSyncJob.java

@@ -0,0 +1,196 @@
+/**
+ * Nextcloud Android client application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017 Mario Danic
+ * Copyright (C) 2017 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/>.
+ */
+
+package com.owncloud.android.jobs;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.media.ExifInterface;
+import android.os.PowerManager;
+import android.support.annotation.NonNull;
+import android.text.TextUtils;
+import android.util.Log;
+
+import com.evernote.android.job.Job;
+import com.evernote.android.job.JobManager;
+import com.evernote.android.job.JobRequest;
+import com.evernote.android.job.util.Device;
+import com.evernote.android.job.util.support.PersistableBundleCompat;
+import com.owncloud.android.MainApp;
+import com.owncloud.android.datamodel.FilesystemDataProvider;
+import com.owncloud.android.datamodel.MediaFolder;
+import com.owncloud.android.datamodel.SyncedFolder;
+import com.owncloud.android.datamodel.SyncedFolderProvider;
+import com.owncloud.android.lib.common.utils.Log_OC;
+import com.owncloud.android.utils.FileStorageUtils;
+import com.owncloud.android.utils.FilesSyncHelper;
+
+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.List;
+import java.util.Locale;
+import java.util.TimeZone;
+
+public class FilesSyncJob extends Job {
+    public static final String TAG = "FilesSyncJob";
+
+    private static final String LAST_AUTOUPLOAD_JOB_RUN = "last_autoupload_job_run";
+
+    // TODO: check for wifi status & charging status, stop and restart jobs as required
+
+    @NonNull
+    @Override
+    protected Result onRunJob(Params params) {
+        final Context context = MainApp.getAppContext();
+        final ContentResolver contentResolver = context.getContentResolver();
+
+        PowerManager powerManager = (PowerManager) context.getSystemService(context.POWER_SERVICE);
+        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
+                TAG);
+        wakeLock.acquire();
+
+        restartJobsIfNeeded();
+
+        FilesSyncHelper.prepareSyncStatusForAccounts();
+
+        // Create all the providers we'll need
+        final FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(contentResolver);
+        SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver);
+
+        for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) {
+            if (syncedFolder.isEnabled()) {
+                String syncedFolderType;
+                if (MediaFolder.IMAGE == syncedFolder.getType()) {
+                    syncedFolderType = "image/";
+                    Log.d("IN A JOB", "SYNCED FOLDER VIDEO");
+                } else if (MediaFolder.VIDEO == syncedFolder.getType()) {
+                    syncedFolderType = "video/";
+                    Log.d("IN A JOB", "SYNCED FOLDER IMAGE");
+                } else {
+                    syncedFolderType = null;
+                    Log.d("IN A JOB", "SYNCED FOLDER ENABLED");
+                }
+
+                Log.d("IN A JOB", "SYNCED FOLDER ENABLED");
+
+                // ignore custom folders for now
+                if (syncedFolderType != null) {
+                    for (String path : filesystemDataProvider.getFilesForUpload(syncedFolder.getLocalPath(),
+                            syncedFolder.getAccount(), syncedFolderType)) {
+                        File file = new File(path);
+
+                        Log.d("IN A JOB", "PERO");
+                        Long lastModificationTime = file.lastModified();
+                        final Locale currentLocale = context.getResources().getConfiguration().locale;
+
+                        if (syncedFolder.equals("image/")) {
+                            String mimetypeString = FileStorageUtils.getMimeTypeFromName(file.getAbsolutePath());
+                            if ("image/jpeg".equalsIgnoreCase(mimetypeString) || "image/tiff".
+                                    equalsIgnoreCase(mimetypeString)) {
+                                try {
+                                    ExifInterface exifInterface = new ExifInterface(file.getAbsolutePath());
+                                    String exifDate = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
+                                    if (!TextUtils.isEmpty(exifDate)) {
+                                        ParsePosition pos = new ParsePosition(0);
+                                        SimpleDateFormat sFormatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss",
+                                                currentLocale);
+                                        sFormatter.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
+                                        Date dateTime = sFormatter.parse(exifDate, pos);
+                                        lastModificationTime = dateTime.getTime();
+                                    }
+
+                                } catch (IOException e) {
+                                    Log_OC.d(TAG, "Failed to get the proper time " + e.getLocalizedMessage());
+                                }
+                            }
+                        }
+
+                        PersistableBundleCompat bundle = new PersistableBundleCompat();
+                        bundle.putString(AutoUploadJob.LOCAL_PATH, file.getAbsolutePath());
+                        bundle.putString(AutoUploadJob.REMOTE_PATH, FileStorageUtils.getInstantUploadFilePath(
+                                currentLocale,
+                                syncedFolder.getRemotePath(), file.getName(),
+                                lastModificationTime,
+                                syncedFolder.getSubfolderByDate()));
+                        bundle.putString(AutoUploadJob.ACCOUNT, syncedFolder.getAccount());
+                        bundle.putInt(AutoUploadJob.UPLOAD_BEHAVIOUR, syncedFolder.getUploadAction());
+
+                        new JobRequest.Builder(AutoUploadJob.TAG)
+                                .setExecutionWindow(30_000L, 80_000L)
+                                .setRequiresCharging(syncedFolder.getChargingOnly())
+                                .setRequiredNetworkType(syncedFolder.getWifiOnly() ? JobRequest.NetworkType.UNMETERED :
+                                        JobRequest.NetworkType.CONNECTED)
+                                .setExtras(bundle)
+                                .setRequirementsEnforced(true)
+                                .setUpdateCurrent(false)
+                                .build()
+                                .schedule();
+
+                        filesystemDataProvider.updateFilesInList(new Object[]{path}, syncedFolder.getAccount());
+                    }
+                }
+            }
+        }
+
+        wakeLock.release();
+        return Result.SUCCESS;
+    }
+
+
+    private void restartJobsIfNeeded() {
+        final Context context = MainApp.getAppContext();
+        List<Integer> restartedJobIds = new ArrayList<Integer>();
+        int jobId;
+        boolean restartedInCurrentIteration = false;
+
+        for (JobRequest jobRequest : JobManager.instance().getAllJobRequestsForTag(AutoUploadJob.TAG)) {
+            restartedInCurrentIteration = false;
+            // Handle case of charging
+            if (jobRequest.requiresCharging() && Device.isCharging(context)) {
+                if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.CONNECTED) &&
+                        !Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
+                    jobId = jobRequest.cancelAndEdit().build().schedule();
+                    restartedInCurrentIteration = true;
+                } else if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.UNMETERED) &&
+                        Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
+                    jobId = jobRequest.cancelAndEdit().build().schedule();
+                    restartedInCurrentIteration = true;
+                }
+            }
+
+            // Handle case of wifi
+
+            if (!restartedInCurrentIteration) {
+                if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.CONNECTED) &&
+                        !Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
+                    jobRequest.cancelAndEdit().build().schedule();
+                } else if (jobRequest.requiredNetworkType().equals(JobRequest.NetworkType.UNMETERED) &&
+                        Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED)) {
+                    jobRequest.cancelAndEdit().build().schedule();
+                }
+            }
+        }
+    }
+}

+ 2 - 0
src/main/java/com/owncloud/android/jobs/NCJobCreator.java

@@ -41,6 +41,8 @@ public class NCJobCreator implements JobCreator {
                 return new AccountRemovalJob();
             case NewAutoUploadJob.TAG:
                 return new NewAutoUploadJob();
+            case FilesSyncJob.TAG:
+                return new FilesSyncJob();
             default:
                 return null;
         }

+ 7 - 32
src/main/java/com/owncloud/android/jobs/NewAutoUploadJob.java

@@ -23,45 +23,20 @@ package com.owncloud.android.jobs;
 
 import android.content.ContentResolver;
 import android.content.Context;
-import android.media.ExifInterface;
 import android.os.PowerManager;
 import android.support.annotation.NonNull;
-import android.text.TextUtils;
-import android.util.Log;
 
 import com.evernote.android.job.Job;
-import com.evernote.android.job.JobRequest;
-import com.evernote.android.job.util.support.PersistableBundleCompat;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.datamodel.ArbitraryDataProvider;
 import com.owncloud.android.datamodel.FilesystemDataProvider;
 import com.owncloud.android.datamodel.SyncedFolder;
 import com.owncloud.android.datamodel.SyncedFolderProvider;
-import com.owncloud.android.lib.common.utils.Log_OC;
-import com.owncloud.android.utils.FileStorageUtils;
-
-import org.lukhnos.nnio.file.FileVisitResult;
-import org.lukhnos.nnio.file.Files;
-import org.lukhnos.nnio.file.Path;
-import org.lukhnos.nnio.file.Paths;
-import org.lukhnos.nnio.file.SimpleFileVisitor;
-import org.lukhnos.nnio.file.attribute.BasicFileAttributes;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-import java.nio.channels.FileChannel;
-import java.nio.channels.FileLock;
-import java.nio.channels.OverlappingFileLockException;
-import java.text.ParsePosition;
-import java.text.SimpleDateFormat;
+
 import java.util.ArrayList;
-import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Locale;
 import java.util.Set;
-import java.util.TimeZone;
 
 /*
     This job is meant to run periodically every half an hour, and has the following burden on it's shoulders:
@@ -119,7 +94,7 @@ public class NewAutoUploadJob extends Job {
         syncedFolders.removeAll(syncedFoldersToDelete);
 
         // store all files from the filesystem
-        for (int i = 0; i < syncedFolders.size(); i++) {
+        /*for (int i = 0; i < syncedFolders.size(); i++) {
             Path path = Paths.get(syncedFolders.get(i).getLocalPath());
 
             try {
@@ -153,13 +128,13 @@ public class NewAutoUploadJob extends Job {
             } catch (IOException e) {
                 Log.d(TAG, "Something went wrong while indexing files for auto upload");
             }
-        }
+        }*/
 
         Set<String> pathsToSet = new HashSet<>();
 
         // get all files that we want to upload
-        for (SyncedFolder syncedFolder : syncedFoldersOriginalList) {
-            Object[] pathsToUpload = filesystemDataProvider.getFilesToUploadForPath(syncedFolder.getLocalPath());
+        /*for (SyncedFolder syncedFolder : syncedFoldersOriginalList) {
+            Object[] pathsToUpload = filesystemDataProvider.getFilesForUpload(syncedFolder.getLocalPath());
 
             for (Object pathToUpload : pathsToUpload) {
                 File file = new File((String) pathToUpload);
@@ -213,9 +188,9 @@ public class NewAutoUploadJob extends Job {
         }
 
         // set them as sent for upload
-        filesystemDataProvider.updateFilesInList(pathsToSet.toArray());
+        //filesystemDataProvider.updateFilesInList(pathsToSet.toArray());
 
-        wakeLock.release();
+        wakeLock.release();*/
         return Result.SUCCESS;
     }
 }

+ 4 - 1
src/main/java/com/owncloud/android/providers/FileContentProvider.java

@@ -625,6 +625,8 @@ public class FileContentProvider extends ContentProvider {
                 return db.update(ProviderTableMeta.SYNCED_FOLDERS_TABLE_NAME, values, selection, selectionArgs);
             case ARBITRARY_DATA:
                 return db.update(ProviderTableMeta.ARBITRARY_DATA_TABLE_NAME, values, selection, selectionArgs);
+            case FILESYSTEM:
+                return db.update(ProviderTableMeta.FILESYSTEM_TABLE_NAME, values, selection, selectionArgs);
             default:
                 return db.update(ProviderTableMeta.FILE_TABLE_NAME, values, selection, selectionArgs);
         }
@@ -1256,7 +1258,8 @@ public class FileContentProvider extends ContentProvider {
                 + ProviderTableMeta.FILESYSTEM_FILE_IS_FOLDER + " INTEGER, "
                 + ProviderTableMeta.FILESYSTEM_FILE_FOUND_RECENTLY + " LONG, "
                 + ProviderTableMeta.FILESYSTEM_FILE_SENT_FOR_UPLOAD + " INTEGER, "
-                + ProviderTableMeta.FILESYSTEM_FILE_BEING_MODIFIED + " INTEGER, "
+                + ProviderTableMeta.FILESYSTEM_ACCOUNT + " STRING, "
+                + ProviderTableMeta.FILESYSTEM_MIMETYPE + " STRING, "
                 + ProviderTableMeta.FILESYSTEM_FILE_MODIFIED + " LONG );"
         );
     }

+ 7 - 0
src/main/java/com/owncloud/android/ui/activity/FolderSyncActivity.java

@@ -59,6 +59,7 @@ import com.owncloud.android.ui.events.CustomFolderEvent;
 import com.owncloud.android.utils.AnalyticsUtils;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.PermissionUtil;
+import com.owncloud.android.utils.FilesSyncHelper;
 import com.owncloud.android.utils.ThemeUtils;
 
 import org.greenrobot.eventbus.Subscribe;
@@ -479,6 +480,9 @@ public class FolderSyncActivity extends FileActivity implements FolderSyncAdapte
                 syncedFolderDisplayItem.setId(storedId);
             }
         }
+
+        FilesSyncHelper.prepareSyncStatusForAccounts();
+
     }
 
     @Override
@@ -521,6 +525,7 @@ public class FolderSyncActivity extends FileActivity implements FolderSyncAdapte
             long storedId = mSyncedFolderProvider.storeFolderSync(newCustomFolder);
             if (storedId != -1) {
                 newCustomFolder.setId(storedId);
+                FilesSyncHelper.prepareSyncStatusForAccounts();
             }
             mAdapter.addSyncFolderItem(newCustomFolder);
         } else {
@@ -535,10 +540,12 @@ public class FolderSyncActivity extends FileActivity implements FolderSyncAdapte
                 long storedId = mSyncedFolderProvider.storeFolderSync(item);
                 if (storedId != -1) {
                     item.setId(storedId);
+                    FilesSyncHelper.prepareSyncStatusForAccounts();
                 }
             } else {
                 // existing synced folder setup to be updated
                 mSyncedFolderProvider.updateSyncFolder(item);
+                FilesSyncHelper.prepareSyncStatusForAccounts();
             }
 
             if(dirty) {

+ 126 - 0
src/main/java/com/owncloud/android/utils/FilesSyncHelper.java

@@ -0,0 +1,126 @@
+/**
+ * Nextcloud Android client application
+ *
+ * @author Mario Danic
+ * Copyright (C) 2017 Mario Danic
+ * Copyright (C) 2017 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/>.
+ */
+package com.owncloud.android.utils;
+
+import android.accounts.Account;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.database.Cursor;
+import android.net.Uri;
+import android.provider.MediaStore;
+import android.text.TextUtils;
+
+import com.owncloud.android.MainApp;
+import com.owncloud.android.authentication.AccountUtils;
+import com.owncloud.android.datamodel.ArbitraryDataProvider;
+import com.owncloud.android.datamodel.FilesystemDataProvider;
+import com.owncloud.android.datamodel.SyncedFolder;
+import com.owncloud.android.datamodel.SyncedFolderProvider;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Set;
+
+public class FilesSyncHelper {
+
+    private static final String LAST_AUTOUPLOAD_JOB_RUN = "last_autoupload_job_run";
+
+
+    private static void insertAllDBEntries() {
+        boolean dryRun = false;
+
+        final Context context = MainApp.getAppContext();
+        final ContentResolver contentResolver = context.getContentResolver();
+        ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(contentResolver);
+
+        for (Account account : AccountUtils.getAccounts(context)) {
+            if (TextUtils.isEmpty(arbitraryDataProvider.getValue(account.name, LAST_AUTOUPLOAD_JOB_RUN))) {
+                dryRun = true;
+            } else {
+                dryRun = false;
+            }
+
+            FilesSyncHelper.insertContentIntoDB(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, dryRun,
+                    account.name);
+            FilesSyncHelper.insertContentIntoDB(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, dryRun, account.name);
+            FilesSyncHelper.insertContentIntoDB(android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI, dryRun,
+                    account.name);
+            FilesSyncHelper.insertContentIntoDB(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, dryRun, account.name);
+        }
+    }
+
+    public static void prepareSyncStatusForAccounts() {
+        final Context context = MainApp.getAppContext();
+        final ContentResolver contentResolver = context.getContentResolver();
+        SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver);
+        ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(contentResolver);
+
+        Set<String> enabledAccounts = new HashSet<>();
+        for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) {
+            enabledAccounts.add(syncedFolder.getAccount());
+        }
+
+        for (String enabledAccount : enabledAccounts) {
+            arbitraryDataProvider.storeOrUpdateKeyValue(enabledAccount, LAST_AUTOUPLOAD_JOB_RUN,
+                    Long.toString(System.currentTimeMillis()));
+        }
+
+        ArrayList<String> accountsArrayList = new ArrayList<>();
+        accountsArrayList.addAll(enabledAccounts);
+        arbitraryDataProvider.deleteForKeyWhereAccountNotIn(accountsArrayList, LAST_AUTOUPLOAD_JOB_RUN);
+
+        insertAllDBEntries();
+
+    }
+
+    public static void insertContentIntoDB(Uri uri, boolean dryRun, String account) {
+        final Context context = MainApp.getAppContext();
+        final ContentResolver contentResolver = context.getContentResolver();
+
+        Cursor cursor;
+        int column_index_data, column_index_date_modified, column_index_mimetype;
+
+        final FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(contentResolver);
+
+        String contentPath;
+        boolean isFolder;
+
+        String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DATE_MODIFIED,
+                MediaStore.MediaColumns.MIME_TYPE};
+
+        cursor = context.getContentResolver().query(uri, projection, null, null, null);
+        if (cursor != null) {
+            column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
+            column_index_date_modified = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED);
+            column_index_mimetype = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.MIME_TYPE);
+            while (cursor.moveToNext()) {
+                contentPath = cursor.getString(column_index_data);
+                isFolder = new File(contentPath).isDirectory();
+                filesystemDataProvider.storeOrUpdateFileValue(cursor.getString(column_index_data),
+                        cursor.getLong(column_index_date_modified), isFolder, account, dryRun,
+                        cursor.getString(column_index_mimetype));
+            }
+            cursor.close();
+        }
+    }
+
+}