|
@@ -47,6 +47,7 @@ import com.owncloud.android.operations.UploadFileOperation;
|
|
|
import com.owncloud.android.ui.activity.Preferences;
|
|
|
import com.owncloud.android.utils.FileStorageUtils;
|
|
|
import com.owncloud.android.utils.FilesSyncHelper;
|
|
|
+import com.owncloud.android.utils.MimeType;
|
|
|
import com.owncloud.android.utils.MimeTypeUtil;
|
|
|
import com.owncloud.android.utils.PowerUtils;
|
|
|
|
|
@@ -65,7 +66,6 @@ import java.util.TimeZone;
|
|
|
*/
|
|
|
public class FilesSyncJob extends Job {
|
|
|
public static final String TAG = "FilesSyncJob";
|
|
|
-
|
|
|
public static final String SKIP_CUSTOM = "skipCustom";
|
|
|
public static final String OVERRIDE_POWER_SAVING = "overridePowerSaving";
|
|
|
|
|
@@ -73,8 +73,6 @@ public class FilesSyncJob extends Job {
|
|
|
@Override
|
|
|
protected Result onRunJob(@NonNull Params params) {
|
|
|
final Context context = MainApp.getAppContext();
|
|
|
- final ContentResolver contentResolver = context.getContentResolver();
|
|
|
- FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
|
|
|
PowerManager.WakeLock wakeLock = null;
|
|
|
|
|
|
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
|
@@ -84,7 +82,6 @@ public class FilesSyncJob extends Job {
|
|
|
}
|
|
|
|
|
|
PersistableBundleCompat bundle = params.getExtras();
|
|
|
- final boolean skipCustom = bundle.getBoolean(SKIP_CUSTOM, false);
|
|
|
final boolean overridePowerSaving = bundle.getBoolean(OVERRIDE_POWER_SAVING, false);
|
|
|
|
|
|
// If we are in power save mode, better to postpone upload
|
|
@@ -96,110 +93,123 @@ public class FilesSyncJob extends Job {
|
|
|
Resources resources = MainApp.getAppContext().getResources();
|
|
|
boolean lightVersion = resources.getBoolean(R.bool.syncedFolder_light);
|
|
|
|
|
|
+ final boolean skipCustom = bundle.getBoolean(SKIP_CUSTOM, false);
|
|
|
FilesSyncHelper.restartJobsIfNeeded();
|
|
|
FilesSyncHelper.insertAllDBEntries(skipCustom);
|
|
|
|
|
|
// Create all the providers we'll need
|
|
|
+ final ContentResolver contentResolver = context.getContentResolver();
|
|
|
final FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(contentResolver);
|
|
|
SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver);
|
|
|
|
|
|
+ Locale currentLocale = context.getResources().getConfiguration().locale;
|
|
|
+ SimpleDateFormat sFormatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", currentLocale);
|
|
|
+ sFormatter.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
|
|
|
+ FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
|
|
|
+
|
|
|
for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) {
|
|
|
if ((syncedFolder.isEnabled()) && (!skipCustom || MediaFolderType.CUSTOM != syncedFolder.getType())) {
|
|
|
- for (String path : filesystemDataProvider.getFilesForUpload(syncedFolder.getLocalPath(),
|
|
|
- Long.toString(syncedFolder.getId()))) {
|
|
|
- File file = new File(path);
|
|
|
-
|
|
|
- Long lastModificationTime = file.lastModified();
|
|
|
- final Locale currentLocale = context.getResources().getConfiguration().locale;
|
|
|
-
|
|
|
- if (MediaFolderType.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 (Exception e) {
|
|
|
- Log_OC.d(TAG, "Failed to get the proper time " + e.getLocalizedMessage());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- String mimeType = MimeTypeUtil.getBestMimeTypeByFilename(file.getAbsolutePath());
|
|
|
-
|
|
|
- Account account = AccountUtils.getOwnCloudAccountByName(context, syncedFolder.getAccount());
|
|
|
+ syncFolder(context, resources, lightVersion, filesystemDataProvider, currentLocale, sFormatter,
|
|
|
+ requester, syncedFolder);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- String remotePath;
|
|
|
- boolean subfolderByDate;
|
|
|
- Integer uploadAction;
|
|
|
- boolean needsCharging;
|
|
|
- boolean needsWifi;
|
|
|
+ if (wakeLock != null) {
|
|
|
+ wakeLock.release();
|
|
|
+ }
|
|
|
|
|
|
- if (lightVersion) {
|
|
|
- ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(
|
|
|
- context.getContentResolver());
|
|
|
+ return Result.SUCCESS;
|
|
|
+ }
|
|
|
|
|
|
- needsCharging = resources.getBoolean(R.bool.syncedFolder_light_on_charging);
|
|
|
- needsWifi = account == null || arbitraryDataProvider.getBooleanValue(account.name,
|
|
|
- Preferences.SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI);
|
|
|
- String uploadActionString = resources.getString(R.string.syncedFolder_light_upload_behaviour);
|
|
|
- uploadAction = getUploadAction(uploadActionString);
|
|
|
+ private void syncFolder(Context context, Resources resources, boolean lightVersion,
|
|
|
+ FilesystemDataProvider filesystemDataProvider, Locale currentLocale,
|
|
|
+ SimpleDateFormat sFormatter, FileUploader.UploadRequester requester,
|
|
|
+ SyncedFolder syncedFolder) {
|
|
|
+ String remotePath;
|
|
|
+ boolean subfolderByDate;
|
|
|
+ Integer uploadAction;
|
|
|
+ boolean needsCharging;
|
|
|
+ boolean needsWifi;
|
|
|
+ Account account = AccountUtils.getOwnCloudAccountByName(context, syncedFolder.getAccount());
|
|
|
+
|
|
|
+ for (String path : filesystemDataProvider.getFilesForUpload(syncedFolder.getLocalPath(),
|
|
|
+ Long.toString(syncedFolder.getId()))) {
|
|
|
+ File file = new File(path);
|
|
|
+ Long lastModificationTime = calculateLastModificationTime(file, syncedFolder, sFormatter);
|
|
|
+ String mimeType = MimeTypeUtil.getBestMimeTypeByFilename(file.getAbsolutePath());
|
|
|
+
|
|
|
+ if (lightVersion) {
|
|
|
+ ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(
|
|
|
+ context.getContentResolver());
|
|
|
+
|
|
|
+ needsCharging = resources.getBoolean(R.bool.syncedFolder_light_on_charging);
|
|
|
+ needsWifi = account == null || arbitraryDataProvider.getBooleanValue(account.name,
|
|
|
+ Preferences.SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI);
|
|
|
+ String uploadActionString = resources.getString(R.string.syncedFolder_light_upload_behaviour);
|
|
|
+ uploadAction = getUploadAction(uploadActionString);
|
|
|
+
|
|
|
+ subfolderByDate = resources.getBoolean(R.bool.syncedFolder_light_use_subfolders);
|
|
|
+
|
|
|
+ remotePath = resources.getString(R.string.syncedFolder_remote_folder);
|
|
|
+ } else {
|
|
|
+ needsCharging = syncedFolder.getChargingOnly();
|
|
|
+ needsWifi = syncedFolder.getWifiOnly();
|
|
|
+ uploadAction = syncedFolder.getUploadAction();
|
|
|
+ subfolderByDate = syncedFolder.getSubfolderByDate();
|
|
|
+ remotePath = syncedFolder.getRemotePath();
|
|
|
+ }
|
|
|
|
|
|
- subfolderByDate = resources.getBoolean(R.bool.syncedFolder_light_use_subfolders);
|
|
|
+ if (!subfolderByDate) {
|
|
|
+ String adaptedPath = file.getAbsolutePath()
|
|
|
+ .replace(syncedFolder.getLocalPath(), "")
|
|
|
+ .replace("/" + file.getName(), "");
|
|
|
+ remotePath += adaptedPath;
|
|
|
+ }
|
|
|
|
|
|
- remotePath = resources.getString(R.string.syncedFolder_remote_folder);
|
|
|
- } else {
|
|
|
- needsCharging = syncedFolder.getChargingOnly();
|
|
|
- needsWifi = syncedFolder.getWifiOnly();
|
|
|
- uploadAction = syncedFolder.getUploadAction();
|
|
|
- subfolderByDate = syncedFolder.getSubfolderByDate();
|
|
|
- remotePath = syncedFolder.getRemotePath();
|
|
|
- }
|
|
|
+ requester.uploadFileWithOverwrite(
|
|
|
+ context,
|
|
|
+ account,
|
|
|
+ file.getAbsolutePath(),
|
|
|
+ FileStorageUtils.getInstantUploadFilePath(
|
|
|
+ currentLocale,
|
|
|
+ remotePath, file.getName(),
|
|
|
+ lastModificationTime, subfolderByDate),
|
|
|
+ uploadAction,
|
|
|
+ mimeType,
|
|
|
+ true, // create parent folder if not existent
|
|
|
+ UploadFileOperation.CREATED_AS_INSTANT_PICTURE,
|
|
|
+ needsWifi,
|
|
|
+ needsCharging,
|
|
|
+ true
|
|
|
+ );
|
|
|
+
|
|
|
+ filesystemDataProvider.updateFilesystemFileAsSentForUpload(path,
|
|
|
+ Long.toString(syncedFolder.getId()));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if (!subfolderByDate) {
|
|
|
- String adaptedPath = file.getAbsolutePath()
|
|
|
- .replace(syncedFolder.getLocalPath(), "")
|
|
|
- .replace("/" + file.getName(), "");
|
|
|
- remotePath += adaptedPath;
|
|
|
+ private Long calculateLastModificationTime(File file, SyncedFolder syncedFolder, SimpleDateFormat formatter) {
|
|
|
+ Long lastModificationTime = file.lastModified();
|
|
|
+
|
|
|
+ if (MediaFolderType.IMAGE == syncedFolder.getType()) {
|
|
|
+ String mimeTypeString = FileStorageUtils.getMimeTypeFromName(file.getAbsolutePath());
|
|
|
+ if (MimeType.JPEG.equalsIgnoreCase(mimeTypeString)
|
|
|
+ || MimeType.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);
|
|
|
+ Date dateTime = formatter.parse(exifDate, pos);
|
|
|
+ lastModificationTime = dateTime.getTime();
|
|
|
}
|
|
|
-
|
|
|
- requester.uploadFileWithOverwrite(
|
|
|
- context,
|
|
|
- account,
|
|
|
- file.getAbsolutePath(),
|
|
|
- FileStorageUtils.getInstantUploadFilePath(
|
|
|
- currentLocale,
|
|
|
- remotePath, file.getName(),
|
|
|
- lastModificationTime, subfolderByDate),
|
|
|
- uploadAction,
|
|
|
- mimeType,
|
|
|
- true, // create parent folder if not existent
|
|
|
- UploadFileOperation.CREATED_AS_INSTANT_PICTURE,
|
|
|
- needsWifi,
|
|
|
- needsCharging,
|
|
|
- true
|
|
|
- );
|
|
|
-
|
|
|
- filesystemDataProvider.updateFilesystemFileAsSentForUpload(path,
|
|
|
- Long.toString(syncedFolder.getId()));
|
|
|
+ } catch (Exception e) {
|
|
|
+ Log_OC.d(TAG, "Failed to get the proper time " + e.getLocalizedMessage());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (wakeLock != null) {
|
|
|
- wakeLock.release();
|
|
|
- }
|
|
|
-
|
|
|
- return Result.SUCCESS;
|
|
|
+ return lastModificationTime;
|
|
|
}
|
|
|
|
|
|
private Integer getUploadAction(String action) {
|