From 9b50b0be8927bea155112ca6eaae3e1b653dca3b Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Fri, 30 Jun 2017 15:42:50 +0200 Subject: [PATCH] Updates Signed-off-by: Mario Danic --- .../owncloud/android/jobs/FilesSyncJob.java | 115 +++++++++--------- .../ui/activity/FolderSyncActivity.java | 10 ++ .../ui/events/InitiateSyncedFolder.java | 35 ++++++ .../android/utils/FilesSyncHelper.java | 65 +++++----- 4 files changed, 140 insertions(+), 85 deletions(-) create mode 100644 src/main/java/com/owncloud/android/ui/events/InitiateSyncedFolder.java diff --git a/src/main/java/com/owncloud/android/jobs/FilesSyncJob.java b/src/main/java/com/owncloud/android/jobs/FilesSyncJob.java index 2b2d37a76a..d0cdd63920 100644 --- a/src/main/java/com/owncloud/android/jobs/FilesSyncJob.java +++ b/src/main/java/com/owncloud/android/jobs/FilesSyncJob.java @@ -63,70 +63,73 @@ public class FilesSyncJob extends Job { TAG); wakeLock.acquire(); - FilesSyncHelper.restartJobsIfNeeded(); - FilesSyncHelper.insertAllDBEntries(); + if (JobManager.instance().getAllJobsForTag(FilesSyncJob.TAG).size() == 1) { - // Create all the providers we'll need - final FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(contentResolver); - SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver); + FilesSyncHelper.restartJobsIfNeeded(); + FilesSyncHelper.insertAllDBEntries(); - for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) { - if (syncedFolder.isEnabled()) { - // ignore custom folders for now - if (MediaFolder.CUSTOM != syncedFolder.getType()) { - for (String path : filesystemDataProvider.getFilesForUpload(syncedFolder.getLocalPath(), - Long.toString(syncedFolder.getId()))) { - if (JobManager.instance().getAllJobRequests().size() < 80) { - File file = new File(path); + // Create all the providers we'll need + final FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(contentResolver); + SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver); - Long lastModificationTime = file.lastModified(); - final Locale currentLocale = context.getResources().getConfiguration().locale; + for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) { + if (syncedFolder.isEnabled()) { + // ignore custom folders for now + if (MediaFolder.CUSTOM != syncedFolder.getType()) { + for (String path : filesystemDataProvider.getFilesForUpload(syncedFolder.getLocalPath(), + Long.toString(syncedFolder.getId()))) { + if (JobManager.instance().getAllJobRequests().size() < 80) { + File file = new File(path); - if (MediaFolder.IMAGE == syncedFolder.getType()) { - 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(); + Long lastModificationTime = file.lastModified(); + final Locale currentLocale = context.getResources().getConfiguration().locale; + + if (MediaFolder.IMAGE == syncedFolder.getType()) { + 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()); } - - } 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.updateFilesystemFileAsSentForUpload(path, + Long.toString(syncedFolder.getId())); } - - 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.updateFilesystemFileAsSentForUpload(path, - Long.toString(syncedFolder.getId())); } } } diff --git a/src/main/java/com/owncloud/android/ui/activity/FolderSyncActivity.java b/src/main/java/com/owncloud/android/ui/activity/FolderSyncActivity.java index a79c40a745..48bbbb33cd 100644 --- a/src/main/java/com/owncloud/android/ui/activity/FolderSyncActivity.java +++ b/src/main/java/com/owncloud/android/ui/activity/FolderSyncActivity.java @@ -56,12 +56,14 @@ import com.owncloud.android.ui.decoration.MediaGridItemDecoration; import com.owncloud.android.ui.dialog.SyncedFolderPreferencesDialogFragment; import com.owncloud.android.ui.dialog.parcel.SyncedFolderParcelable; import com.owncloud.android.ui.events.CustomFolderEvent; +import com.owncloud.android.ui.events.InitiateSyncedFolder; 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.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; @@ -478,6 +480,7 @@ public class FolderSyncActivity extends FileActivity implements FolderSyncAdapte long storedId = mSyncedFolderProvider.storeFolderSync(syncedFolderDisplayItem); if (storedId != -1) { syncedFolderDisplayItem.setId(storedId); + EventBus.getDefault().post(new InitiateSyncedFolder(syncedFolderDisplayItem)); } } } @@ -536,6 +539,7 @@ public class FolderSyncActivity extends FileActivity implements FolderSyncAdapte long storedId = mSyncedFolderProvider.storeFolderSync(item); if (storedId != -1) { item.setId(storedId); + EventBus.getDefault().post(new InitiateSyncedFolder(item)); } } else { // existing synced folder setup to be updated @@ -626,4 +630,10 @@ public class FolderSyncActivity extends FileActivity implements FolderSyncAdapte onSyncFolderSettingsClick(0, emptyCustomFolder); }; + @Subscribe(threadMode = ThreadMode.BACKGROUND) + public void onMessageEvent(InitiateSyncedFolder event) { + FilesSyncHelper.insertAllDBEntriesForSyncedFolder(event.getSyncedFolder()); + }; + + } diff --git a/src/main/java/com/owncloud/android/ui/events/InitiateSyncedFolder.java b/src/main/java/com/owncloud/android/ui/events/InitiateSyncedFolder.java new file mode 100644 index 0000000000..0526061435 --- /dev/null +++ b/src/main/java/com/owncloud/android/ui/events/InitiateSyncedFolder.java @@ -0,0 +1,35 @@ +/** + * Nextcloud Android client application + * + * @author Mario Danic + * Copyright (C) 2017 Mario Danic + * + * 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 . + */ +package com.owncloud.android.ui.events; + +import com.owncloud.android.datamodel.SyncedFolder; + +public class InitiateSyncedFolder { + private final SyncedFolder syncedFolder; + + + public InitiateSyncedFolder(SyncedFolder syncedFolder) { + this.syncedFolder = syncedFolder; + } + + public SyncedFolder getSyncedFolder() { + return syncedFolder; + } +} diff --git a/src/main/java/com/owncloud/android/utils/FilesSyncHelper.java b/src/main/java/com/owncloud/android/utils/FilesSyncHelper.java index daa04fc369..6494c6ee0c 100644 --- a/src/main/java/com/owncloud/android/utils/FilesSyncHelper.java +++ b/src/main/java/com/owncloud/android/utils/FilesSyncHelper.java @@ -42,44 +42,51 @@ import java.io.File; public class FilesSyncHelper { + public static void insertAllDBEntriesForSyncedFolder(SyncedFolder syncedFolder) { + final Context context = MainApp.getAppContext(); + final ContentResolver contentResolver = context.getContentResolver(); + ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(contentResolver); + + String syncedFolderInitiatedKey = "syncedFolderIntitiated_" + syncedFolder.getId(); + boolean dryRun = TextUtils.isEmpty(arbitraryDataProvider.getValue + ("global", syncedFolderInitiatedKey)); + + if (MediaFolder.IMAGE == syncedFolder.getType()) { + FilesSyncHelper.insertContentIntoDB(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI + , dryRun, syncedFolder); + FilesSyncHelper.insertContentIntoDB(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, dryRun, + syncedFolder); + + if (dryRun) { + arbitraryDataProvider.storeOrUpdateKeyValue("global", syncedFolderInitiatedKey, + "1"); + } + } else if (MediaFolder.VIDEO == syncedFolder.getType()) { + FilesSyncHelper.insertContentIntoDB(android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI, + dryRun, syncedFolder); + FilesSyncHelper.insertContentIntoDB(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, dryRun, + syncedFolder); + + if (dryRun) { + arbitraryDataProvider.storeOrUpdateKeyValue("global", syncedFolderInitiatedKey, + "1"); + } + + } else { + // custom folder, do nothing + } + } + public static void insertAllDBEntries() { boolean dryRun; final Context context = MainApp.getAppContext(); final ContentResolver contentResolver = context.getContentResolver(); SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver); - ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(contentResolver); for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) { if (syncedFolder.isEnabled()) { - String syncedFolderInitiatedKey = "syncedFolderIntitiated_" + syncedFolder.getId(); - dryRun = TextUtils.isEmpty(arbitraryDataProvider.getValue - ("global", syncedFolderInitiatedKey)); - - if (MediaFolder.IMAGE == syncedFolder.getType()) { - FilesSyncHelper.insertContentIntoDB(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI - , dryRun, syncedFolder); - FilesSyncHelper.insertContentIntoDB(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, dryRun, - syncedFolder); - - if (dryRun) { - arbitraryDataProvider.storeOrUpdateKeyValue("global", syncedFolderInitiatedKey, - "1"); - } - } else if (MediaFolder.VIDEO == syncedFolder.getType()) { - FilesSyncHelper.insertContentIntoDB(android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI, - dryRun, syncedFolder); - FilesSyncHelper.insertContentIntoDB(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, dryRun, - syncedFolder); - - if (dryRun) { - arbitraryDataProvider.storeOrUpdateKeyValue("global", syncedFolderInitiatedKey, - "1"); - } - - } else { - // custom folder, do nothing - } + insertAllDBEntriesForSyncedFolder(syncedFolder); } } }