Browse Source

Work a bit more on stuff

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 8 years ago
parent
commit
8ec926697d

+ 1 - 1
src/main/AndroidManifest.xml

@@ -160,7 +160,7 @@
             android:label="@string/search_users_and_groups_hint" />
 
         <provider
-            android:name="org.nextcloud.providers.DocumentsStorageProvider"
+            android:name=".providers.DocumentsStorageProvider"
             android:authorities="@string/document_provider_authority"
             android:exported="true"
             android:grantUriPermissions="true"

+ 16 - 1
src/main/java/com/owncloud/android/MainApp.java

@@ -19,6 +19,7 @@
  */
 package com.owncloud.android;
 
+import android.Manifest;
 import android.app.Activity;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -31,6 +32,7 @@ import android.support.multidex.MultiDexApplication;
 import android.support.v4.util.Pair;
 
 import com.evernote.android.job.JobManager;
+import com.evernote.android.job.JobRequest;
 import com.owncloud.android.authentication.PassCodeManager;
 import com.owncloud.android.datamodel.MediaFolder;
 import com.owncloud.android.datamodel.MediaProvider;
@@ -39,12 +41,14 @@ import com.owncloud.android.datamodel.SyncedFolderProvider;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
 import com.owncloud.android.db.PreferenceManager;
 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;
 import com.owncloud.android.ui.activity.Preferences;
 import com.owncloud.android.ui.activity.WhatsNewActivity;
 import com.owncloud.android.utils.AnalyticsUtils;
+import com.owncloud.android.utils.PermissionUtil;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -116,7 +120,18 @@ public class MainApp extends MultiDexApplication {
 
         cleanOldEntries();
         updateAutoUploadEntries();
-        splitOutAutoUploadEntries();
+
+        if (PermissionUtil.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
+            splitOutAutoUploadEntries();
+        } else {
+            PreferenceManager.setAutoUploadSplitEntries(this, true);
+        }
+
+        new JobRequest.Builder(NewAutoUploadJob.TAG)
+                .setExecutionWindow(3000L, 4000L)
+                .setUpdateCurrent(true)
+                .build()
+                .schedule();
 
         // register global protection with pass code
         registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

+ 57 - 1
src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java

@@ -27,6 +27,9 @@ import android.net.Uri;
 import com.owncloud.android.db.ProviderMeta;
 import com.owncloud.android.lib.common.utils.Log_OC;
 
+import java.util.HashSet;
+import java.util.Set;
+
 public class FilesystemDataProvider {
 
     static private final String TAG = FilesystemDataProvider.class.getSimpleName();
@@ -40,12 +43,65 @@ public class FilesystemDataProvider {
         this.contentResolver = contentResolver;
     }
 
+    public int updateFilesInList(Object[] paths) {
+        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++) {
+            stringPaths[i] = (String) paths[i];
+        }
+
+        int result = contentResolver.update(
+                ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
+                cv,
+                ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + "IN (?)",
+                stringPaths
+        );
+
+        return result;
+
+    }
+
+    public Object[] getFilesToUploadForPath(String localPath) {
+        Set<String> localPathsToUpload = new HashSet<>();
+
+        String likeParam = localPath + "%";
+
+        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_FILE_SENT_FOR_UPLOAD + " = ? and " +
+                        ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_IS_FOLDER + " = ?",
+                new String[]{likeParam, "0", "0", "0"},
+                null);
+
+        if (cursor.moveToFirst()) {
+            do {
+                String value = cursor.getString(cursor.getColumnIndex(
+                        ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH));
+                if (value == null) {
+                    Log_OC.e(TAG, "Cannot get local path");
+                } else {
+                    localPathsToUpload.add(value);
+                }
+            } while (cursor.moveToNext());
+        }
+
+
+        cursor.close();
+        return localPathsToUpload.toArray();
+
+    }
+
     public long countFilesThatNeedUploadInFolder(String localPath) {
         String likeParam = localPath + "%";
 
         Cursor cursor = contentResolver.query(
                 ProviderMeta.ProviderTableMeta.CONTENT_URI_FILESYSTEM,
-                new String[] {"count(*)"},
+                new String[]{"count(*)"},
                 ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH + " LIKE ?",
                 new String[]{likeParam},
                 null);

+ 74 - 0
src/main/java/com/owncloud/android/jobs/NewAutoUploadJob.java

@@ -23,16 +23,22 @@ 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;
@@ -47,8 +53,15 @@ 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:
@@ -88,6 +101,7 @@ public class NewAutoUploadJob extends Job {
                 Long.toString(System.currentTimeMillis()));
 
         List<SyncedFolder> syncedFolders = syncedFolderProvider.getSyncedFolders();
+        List<SyncedFolder> syncedFoldersOriginalList = syncedFolderProvider.getSyncedFolders();
         List<SyncedFolder> syncedFoldersToDelete = new ArrayList<>();
 
         // be smart, and only traverse folders once instead of multiple times
@@ -141,6 +155,66 @@ public class NewAutoUploadJob extends Job {
             }
         }
 
+        Set<String> pathsToSet = new HashSet<>();
+
+        // get all files that we want to upload
+        for (SyncedFolder syncedFolder : syncedFoldersOriginalList) {
+            Object[] pathsToUpload = filesystemDataProvider.getFilesToUploadForPath(syncedFolder.getLocalPath());
+
+            for (Object pathToUpload : pathsToUpload) {
+                File file = new File((String) pathToUpload);
+
+                String mimetypeString = FileStorageUtils.getMimeTypeFromName(file.getAbsolutePath());
+                Long lastModificationTime = file.lastModified();
+                final Locale currentLocale = context.getResources().getConfiguration().locale;
+
+                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());
+
+                pathsToSet.add((String) pathToUpload);
+
+                new JobRequest.Builder(AutoUploadJob.TAG)
+                        .setExecutionWindow(30_000L, 80_000L)
+                        .setRequiresCharging(syncedFolder.getChargingOnly())
+                        .setRequiredNetworkType(syncedFolder.getWifiOnly() ? JobRequest.NetworkType.UNMETERED :
+                                JobRequest.NetworkType.ANY)
+                        .setExtras(bundle)
+                        .setPersisted(false)
+                        .setRequirementsEnforced(true)
+                        .setUpdateCurrent(false)
+                        .build()
+                        .schedule();
+            }
+        }
+
+        // set them as sent for upload
+        filesystemDataProvider.updateFilesInList(pathsToSet.toArray());
+
         wakeLock.release();
         return Result.SUCCESS;
     }

+ 1 - 1
src/main/java/org/nextcloud/providers/DocumentsStorageProvider.java → src/main/java/com/owncloud/android/providers/DocumentsStorageProvider.java

@@ -18,7 +18,7 @@
  *
  */
 
-package org.nextcloud.providers;
+package com.owncloud.android.providers;
 
 import android.accounts.Account;
 import android.annotation.TargetApi;

+ 27 - 0
src/main/java/com/owncloud/android/providers/FileContentProvider.java

@@ -74,6 +74,7 @@ public class FileContentProvider extends ContentProvider {
     private static final int EXTERNAL_LINKS = 8;
     private static final int ARBITRARY_DATA = 9;
     private static final int VIRTUAL = 10;
+    private static final int FILESYSTEM = 11;
 
     private static final String TAG = FileContentProvider.class.getSimpleName();
 
@@ -209,6 +210,9 @@ public class FileContentProvider extends ContentProvider {
             case VIRTUAL:
                 count = db.delete(ProviderTableMeta.VIRTUAL_TABLE_NAME, where, whereArgs);
                 break;
+            case FILESYSTEM:
+                count = db.delete(ProviderTableMeta.FILESYSTEM_TABLE_NAME, where, whereArgs);
+                break;
             default:
                 //Log_OC.e(TAG, "Unknown uri " + uri);
                 throw new IllegalArgumentException("Unknown uri: " + uri.toString());
@@ -365,6 +369,18 @@ public class FileContentProvider extends ContentProvider {
                 }
 
                 return insertedVirtualUri;
+            case FILESYSTEM:
+                Uri insertedFilesystemUri = null;
+                long filesystedId = db.insert(ProviderTableMeta.FILESYSTEM_TABLE_NAME, null, values);
+                if (filesystedId > 0) {
+                    insertedFilesystemUri =
+                            ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILESYSTEM, filesystedId);
+                } else {
+                    throw new SQLException("ERROR " + uri);
+
+                }
+                return insertedFilesystemUri;
+
             default:
                 throw new IllegalArgumentException("Unknown uri id: " + uri);
         }
@@ -417,6 +433,7 @@ public class FileContentProvider extends ContentProvider {
         mUriMatcher.addURI(authority, "external_links", EXTERNAL_LINKS);
         mUriMatcher.addURI(authority, "arbitrary_data", ARBITRARY_DATA);
         mUriMatcher.addURI(authority, "virtual", VIRTUAL);
+        mUriMatcher.addURI(authority, "filesystem", FILESYSTEM);
 
         return true;
     }
@@ -518,6 +535,13 @@ public class FileContentProvider extends ContentProvider {
                     sqlQuery.appendWhere(ProviderTableMeta._ID + "=" + uri.getPathSegments().get(1));
                 }
                 break;
+            case FILESYSTEM:
+                sqlQuery.setTables(ProviderTableMeta.FILESYSTEM_TABLE_NAME);
+                if (uri.getPathSegments().size() > 1) {
+                    sqlQuery.appendWhere(ProviderTableMeta._ID + "="
+                            + uri.getPathSegments().get(1));
+                }
+                break;
             default:
                 throw new IllegalArgumentException("Unknown uri id: " + uri);
         }
@@ -549,6 +573,9 @@ public class FileContentProvider extends ContentProvider {
                 default: // Files
                     order = ProviderTableMeta.FILE_DEFAULT_SORT_ORDER;
                     break;
+                case FILESYSTEM:
+                    order = ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH;
+                    break;
             }
         } else {
             order = sortOrder;