From dabd73b370723b026f19854a79523ee5dea1bc21 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 23 Mar 2022 09:29:02 +0100 Subject: [PATCH 1/5] Rename variables to use a single naming convention --- .../operations/DownloadFileOperation.java | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java index c8d17bb8..68749690 100644 --- a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java @@ -29,11 +29,11 @@ import foundation.e.drive.utils.CommonUtils; public class DownloadFileOperation extends RemoteOperation implements Parcelable { private final static String TAG = DownloadFileOperation.class.getSimpleName(); - private final RemoteFile mRFile; - private Context mContext; - private String mTargetPath; + private final RemoteFile remoteFile; + private Context context; + private String targetPath; private int restartCounter =0; - private SyncedFileState mSyncedState; + private SyncedFileState syncedFileState; private String previousEtag; @@ -42,26 +42,26 @@ public class DownloadFileOperation extends RemoteOperation implements Parcelabl * @param remoteFile remote file to Download * @param syncedFileState SyncedFileState corresponding to remote file */ - public DownloadFileOperation(RemoteFile remoteFile, SyncedFileState syncedFileState){ this.mRFile = remoteFile; - this.mSyncedState = syncedFileState; - this.previousEtag = mSyncedState.getLastETAG(); - this.mTargetPath = this.mSyncedState.getLocalPath(); + public DownloadFileOperation(RemoteFile remoteFile, SyncedFileState syncedFileState){ this.remoteFile = remoteFile; + this.syncedFileState = syncedFileState; + this.previousEtag = this.syncedFileState.getLastETAG(); + this.targetPath = this.syncedFileState.getLocalPath(); } protected DownloadFileOperation(Parcel in) { - mRFile = in.readParcelable(RemoteFile.class.getClassLoader()); - mTargetPath = in.readString(); + remoteFile = in.readParcelable(RemoteFile.class.getClassLoader()); + targetPath = in.readString(); restartCounter = in.readInt(); - mSyncedState = in.readParcelable(SyncedFileState.class.getClassLoader()); + syncedFileState = in.readParcelable(SyncedFileState.class.getClassLoader()); previousEtag = in.readString(); } @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeParcelable(mRFile, flags); - dest.writeString(mTargetPath); + dest.writeParcelable(remoteFile, flags); + dest.writeString(targetPath); dest.writeInt(restartCounter); - dest.writeParcelable(mSyncedState, flags); + dest.writeParcelable(syncedFileState, flags); dest.writeString(previousEtag); } @@ -79,7 +79,7 @@ public class DownloadFileOperation extends RemoteOperation implements Parcelabl }; public void setContext(Context context){ - this.mContext = context; + this.context = context; } @@ -88,41 +88,41 @@ public class DownloadFileOperation extends RemoteOperation implements Parcelabl Log.i(TAG, "run(ownCloudClient)"); //get or build synced file equivalent of this.mFile - if(mSyncedState == null || mTargetPath == null || mTargetPath.isEmpty()) { - Log.e(TAG, "mSyncedState or mTargetPath is empty or null. Can't Download in those conditions"); + if(syncedFileState == null || targetPath == null || targetPath.isEmpty()) { + Log.e(TAG, "syncedFileState or targetPath is empty or null. Can't Download in those conditions"); return new RemoteOperationResult(RemoteOperationResult.ResultCode.FORBIDDEN); - }else if(mSyncedState.getId() == -1){ - this.mSyncedState.setId( DbHelper.manageSyncedFileStateDB(this.mSyncedState, "INSERT", mContext) ); + }else if(syncedFileState.getId() == -1){ + this.syncedFileState.setId( DbHelper.manageSyncedFileStateDB(this.syncedFileState, "INSERT", context) ); } - if(mSyncedState.getLastETAG().equals( mRFile.getEtag() ) && mSyncedState.getLocalLastModified() > 0L){ + if(syncedFileState.getLastETAG().equals( remoteFile.getEtag() ) && syncedFileState.getLocalLastModified() > 0L){ //Same etag and localLastModified not null mean the file is up to date Log.w(TAG, "File already up-to-date"); return new RemoteOperationResult(RemoteOperationResult.ResultCode.ETAG_UNCHANGED ); } - String tmpTargetPath = mContext.getExternalCacheDir()+ FileUtils.PATH_SEPARATOR+mSyncedState.getName(); - DownloadFileRemoteOperation downloadOperation = new DownloadFileRemoteOperation(mRFile.getRemotePath(), + String tmpTargetPath = context.getExternalCacheDir()+ FileUtils.PATH_SEPARATOR+ syncedFileState.getName(); + DownloadFileRemoteOperation downloadOperation = new DownloadFileRemoteOperation(remoteFile.getRemotePath(), tmpTargetPath); RemoteOperationResult downloadResult = downloadOperation.execute( ownCloudClient ); - RemoteOperationResult.ResultCode mResultCode; + RemoteOperationResult.ResultCode resultCode; boolean mustRestart = true; if( downloadResult.isSuccess() ){ File tmpLocalFile = new File(tmpTargetPath); if( !tmpLocalFile.exists() ){ Log.e(TAG, "Downloaded file doesn't exist or is null"); - mResultCode = RemoteOperationResult.ResultCode.FILE_NOT_FOUND; + resultCode = RemoteOperationResult.ResultCode.FILE_NOT_FOUND; - }else if(tmpLocalFile.length() != mRFile.getLength() ){ + }else if(tmpLocalFile.length() != remoteFile.getLength() ){ Log.e(TAG, "Local and remote file doesn't have the same size."); - mResultCode = RemoteOperationResult.ResultCode.INVALID_OVERWRITE; + resultCode = RemoteOperationResult.ResultCode.INVALID_OVERWRITE; tmpLocalFile.delete(); }else{ //file has been correctly download. - File localFile = new File(mTargetPath); + File localFile = new File(targetPath); if( localFile.exists() ){ localFile.delete(); } @@ -139,36 +139,36 @@ public class DownloadFileOperation extends RemoteOperation implements Parcelabl boolean renameResult = tmpLocalFile.renameTo(localFile); if(!renameResult) Log.d(TAG, "File hasn't been successfully moved at its place"); - mSyncedState.setLocalLastModified( localFile.lastModified() ) - .setLastETAG( mRFile.getEtag() ); + syncedFileState.setLocalLastModified( localFile.lastModified() ) + .setLastETAG( remoteFile.getEtag() ); mustRestart = false; - mResultCode = RemoteOperationResult.ResultCode.OK; + resultCode = RemoteOperationResult.ResultCode.OK; //needed to make Gallery show new image - CommonUtils.doActionMediaScannerConnexionScanFile(mContext, mSyncedState.getLocalPath() ); + CommonUtils.doActionMediaScannerConnexionScanFile(context, syncedFileState.getLocalPath() ); } }else{ //If download failed Log.e(TAG, "Download failed: "+downloadResult.getLogMessage() ); - mResultCode = RemoteOperationResult.ResultCode.UNKNOWN_ERROR; + resultCode = RemoteOperationResult.ResultCode.UNKNOWN_ERROR; } if(mustRestart){ Log.w(TAG, restartCounter+" unsuccessfull trial.s of downloading file " - +mRFile.getRemotePath() ); - mSyncedState.setLastETAG(this.previousEtag); + + remoteFile.getRemotePath() ); + syncedFileState.setLastETAG(this.previousEtag); if(this.restartCounter < 3){ this.restartCounter += 1; return this.run(ownCloudClient); }else{ - mResultCode = RemoteOperationResult.ResultCode.INVALID_OVERWRITE; + resultCode = RemoteOperationResult.ResultCode.INVALID_OVERWRITE; } } //So now, we can update instance of SyncedState and save it to DB - if( DbHelper.manageSyncedFileStateDB( mSyncedState, "UPDATE", mContext ) <= 0 ){ + if( DbHelper.manageSyncedFileStateDB(syncedFileState, "UPDATE", context) <= 0 ){ Log.e(TAG, "DB update failed: 0 affected row"); //@TODO : do smtg } - return new RemoteOperationResult( mResultCode ); + return new RemoteOperationResult( resultCode ); } @Override -- GitLab From 672153f690e9bdc6e187f9ac9d52a71df8a5d184 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 23 Mar 2022 09:30:04 +0100 Subject: [PATCH 2/5] remove implementation of Parcelable --- .../operations/DownloadFileOperation.java | 40 +------------------ 1 file changed, 1 insertion(+), 39 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java index 68749690..f64ae9ea 100644 --- a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java @@ -9,8 +9,6 @@ package foundation.e.drive.operations; import android.content.Context; -import android.os.Parcel; -import android.os.Parcelable; import android.util.Log; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.RemoteOperation; @@ -26,7 +24,7 @@ import foundation.e.drive.utils.CommonUtils; * @author Vincent Bourgmayer * Encapsulate a global download process for a file */ -public class DownloadFileOperation extends RemoteOperation implements Parcelable { +public class DownloadFileOperation extends RemoteOperation { private final static String TAG = DownloadFileOperation.class.getSimpleName(); private final RemoteFile remoteFile; @@ -36,7 +34,6 @@ public class DownloadFileOperation extends RemoteOperation implements Parcelabl private SyncedFileState syncedFileState; private String previousEtag; - /** * COnstructor of download operation where syncedFileState is already known * @param remoteFile remote file to Download @@ -48,36 +45,6 @@ public class DownloadFileOperation extends RemoteOperation implements Parcelabl this.targetPath = this.syncedFileState.getLocalPath(); } - protected DownloadFileOperation(Parcel in) { - remoteFile = in.readParcelable(RemoteFile.class.getClassLoader()); - targetPath = in.readString(); - restartCounter = in.readInt(); - syncedFileState = in.readParcelable(SyncedFileState.class.getClassLoader()); - previousEtag = in.readString(); - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeParcelable(remoteFile, flags); - dest.writeString(targetPath); - dest.writeInt(restartCounter); - dest.writeParcelable(syncedFileState, flags); - dest.writeString(previousEtag); - } - - - public static final Creator CREATOR = new Creator() { - @Override - public DownloadFileOperation createFromParcel(Parcel in) { - return new DownloadFileOperation(in); - } - - @Override - public DownloadFileOperation[] newArray(int size) { - return new DownloadFileOperation[size]; - } - }; - public void setContext(Context context){ this.context = context; } @@ -170,9 +137,4 @@ public class DownloadFileOperation extends RemoteOperation implements Parcelabl } return new RemoteOperationResult( resultCode ); } - - @Override - public int describeContents() { - return 0; - } } -- GitLab From f3018b89577c531520a2d60f3758dd92d6308331 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 23 Mar 2022 09:33:19 +0100 Subject: [PATCH 3/5] Fusion DownloadFileOperation's constructor and setContext(Context context) --- .../e/drive/operations/DownloadFileOperation.java | 13 +++++-------- .../e/drive/services/SynchronizationService.java | 2 +- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java index f64ae9ea..2c4cb054 100644 --- a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java @@ -39,17 +39,14 @@ public class DownloadFileOperation extends RemoteOperation { * @param remoteFile remote file to Download * @param syncedFileState SyncedFileState corresponding to remote file */ - public DownloadFileOperation(RemoteFile remoteFile, SyncedFileState syncedFileState){ this.remoteFile = remoteFile; + public DownloadFileOperation(RemoteFile remoteFile, SyncedFileState syncedFileState, Context context){ + this.remoteFile = remoteFile; this.syncedFileState = syncedFileState; - this.previousEtag = this.syncedFileState.getLastETAG(); - this.targetPath = this.syncedFileState.getLocalPath(); + this.previousEtag = syncedFileState.getLastETAG(); + this.targetPath = syncedFileState.getLocalPath(); + this.context = context; } - public void setContext(Context context){ - this.context = context; - } - - @Override protected RemoteOperationResult run(OwnCloudClient ownCloudClient) { Log.i(TAG, "run(ownCloudClient)"); diff --git a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java index cd4de9fe..a392af37 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -226,7 +226,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation break; case DOWNLOAD: final DownloadRequest downloadRequest = (DownloadRequest) request; - operation = new DownloadFileOperation(downloadRequest.getRemoteFile(), downloadRequest.getSyncedFileState()); + operation = new DownloadFileOperation(downloadRequest.getRemoteFile(), downloadRequest.getSyncedFileState(), getApplicationContext()); break; case REMOTE_DELETE: default: -- GitLab From 51b2b79421f71a6c0ca2f1d5c6e69073e85e2b19 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 23 Mar 2022 09:35:51 +0100 Subject: [PATCH 4/5] fix coding style for if/else & brackets --- .../operations/DownloadFileOperation.java | 57 ++++++++++--------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java index 2c4cb054..80cac19b 100644 --- a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java @@ -39,7 +39,7 @@ public class DownloadFileOperation extends RemoteOperation { * @param remoteFile remote file to Download * @param syncedFileState SyncedFileState corresponding to remote file */ - public DownloadFileOperation(RemoteFile remoteFile, SyncedFileState syncedFileState, Context context){ + public DownloadFileOperation(RemoteFile remoteFile, SyncedFileState syncedFileState, Context context) { this.remoteFile = remoteFile; this.syncedFileState = syncedFileState; this.previousEtag = syncedFileState.getLastETAG(); @@ -52,86 +52,87 @@ public class DownloadFileOperation extends RemoteOperation { Log.i(TAG, "run(ownCloudClient)"); //get or build synced file equivalent of this.mFile - if(syncedFileState == null || targetPath == null || targetPath.isEmpty()) { + if (syncedFileState == null || targetPath == null || targetPath.isEmpty()) { Log.e(TAG, "syncedFileState or targetPath is empty or null. Can't Download in those conditions"); return new RemoteOperationResult(RemoteOperationResult.ResultCode.FORBIDDEN); - }else if(syncedFileState.getId() == -1){ - this.syncedFileState.setId( DbHelper.manageSyncedFileStateDB(this.syncedFileState, "INSERT", context) ); + } else if (syncedFileState.getId() == -1) { + this.syncedFileState.setId(DbHelper.manageSyncedFileStateDB(this.syncedFileState, "INSERT", context)); } - if(syncedFileState.getLastETAG().equals( remoteFile.getEtag() ) && syncedFileState.getLocalLastModified() > 0L){ + if (syncedFileState.getLastETAG().equals(remoteFile.getEtag()) && syncedFileState.getLocalLastModified() > 0L) { //Same etag and localLastModified not null mean the file is up to date Log.w(TAG, "File already up-to-date"); - return new RemoteOperationResult(RemoteOperationResult.ResultCode.ETAG_UNCHANGED ); + return new RemoteOperationResult(RemoteOperationResult.ResultCode.ETAG_UNCHANGED); } + String tmpTargetPath = context.getExternalCacheDir()+ FileUtils.PATH_SEPARATOR+ syncedFileState.getName(); DownloadFileRemoteOperation downloadOperation = new DownloadFileRemoteOperation(remoteFile.getRemotePath(), tmpTargetPath); - RemoteOperationResult downloadResult = downloadOperation.execute( ownCloudClient ); + RemoteOperationResult downloadResult = downloadOperation.execute(ownCloudClient); RemoteOperationResult.ResultCode resultCode; boolean mustRestart = true; - if( downloadResult.isSuccess() ){ + if (downloadResult.isSuccess()) { File tmpLocalFile = new File(tmpTargetPath); - if( !tmpLocalFile.exists() ){ + if (!tmpLocalFile.exists()) { Log.e(TAG, "Downloaded file doesn't exist or is null"); resultCode = RemoteOperationResult.ResultCode.FILE_NOT_FOUND; - }else if(tmpLocalFile.length() != remoteFile.getLength() ){ + } else if (tmpLocalFile.length() != remoteFile.getLength()) { Log.e(TAG, "Local and remote file doesn't have the same size."); resultCode = RemoteOperationResult.ResultCode.INVALID_OVERWRITE; tmpLocalFile.delete(); - }else{ + } else { //file has been correctly download. File localFile = new File(targetPath); - if( localFile.exists() ){ + if (localFile.exists()) { localFile.delete(); } //Check parentFolder existence and create if needed String parentFoldersPath = localFile.getParent(); File localParentFile = new File(parentFoldersPath); - if( !localParentFile.exists() ){ - if( localParentFile.mkdirs() ) - Log.d(TAG, "Created folders: "+parentFoldersPath ); + if (!localParentFile.exists()) { + if (localParentFile.mkdirs()) + Log.d(TAG, "Created folders: "+parentFoldersPath); else - Log.d(TAG, "Can't create folders: "+parentFoldersPath ); + Log.d(TAG, "Can't create folders: "+parentFoldersPath); } boolean renameResult = tmpLocalFile.renameTo(localFile); - if(!renameResult) + if (!renameResult) Log.d(TAG, "File hasn't been successfully moved at its place"); - syncedFileState.setLocalLastModified( localFile.lastModified() ) - .setLastETAG( remoteFile.getEtag() ); + syncedFileState.setLocalLastModified(localFile.lastModified()) + .setLastETAG(remoteFile.getEtag()); mustRestart = false; resultCode = RemoteOperationResult.ResultCode.OK; //needed to make Gallery show new image - CommonUtils.doActionMediaScannerConnexionScanFile(context, syncedFileState.getLocalPath() ); + CommonUtils.doActionMediaScannerConnexionScanFile(context, syncedFileState.getLocalPath()); } - }else{ + } else { //If download failed - Log.e(TAG, "Download failed: "+downloadResult.getLogMessage() ); + Log.e(TAG, "Download failed: "+downloadResult.getLogMessage()); resultCode = RemoteOperationResult.ResultCode.UNKNOWN_ERROR; } - if(mustRestart){ + if (mustRestart) { Log.w(TAG, restartCounter+" unsuccessfull trial.s of downloading file " - + remoteFile.getRemotePath() ); + + remoteFile.getRemotePath()); syncedFileState.setLastETAG(this.previousEtag); - if(this.restartCounter < 3){ + if (this.restartCounter < 3) { this.restartCounter += 1; return this.run(ownCloudClient); - }else{ + } else { resultCode = RemoteOperationResult.ResultCode.INVALID_OVERWRITE; } } //So now, we can update instance of SyncedState and save it to DB - if( DbHelper.manageSyncedFileStateDB(syncedFileState, "UPDATE", context) <= 0 ){ + if (DbHelper.manageSyncedFileStateDB(syncedFileState, "UPDATE", context) <= 0) { Log.e(TAG, "DB update failed: 0 affected row"); //@TODO : do smtg } - return new RemoteOperationResult( resultCode ); + return new RemoteOperationResult(resultCode); } } -- GitLab From 54bc9b516a7d8b88f9769c53fd4fdc38948a8b2a Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 23 Mar 2022 09:45:40 +0100 Subject: [PATCH 5/5] set few variables 'final' in run(OwncloudClient client) --- .../e/drive/operations/DownloadFileOperation.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java index 80cac19b..4445239c 100644 --- a/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java +++ b/app/src/main/java/foundation/e/drive/operations/DownloadFileOperation.java @@ -65,16 +65,16 @@ public class DownloadFileOperation extends RemoteOperation { return new RemoteOperationResult(RemoteOperationResult.ResultCode.ETAG_UNCHANGED); } - String tmpTargetPath = context.getExternalCacheDir()+ FileUtils.PATH_SEPARATOR+ syncedFileState.getName(); - DownloadFileRemoteOperation downloadOperation = new DownloadFileRemoteOperation(remoteFile.getRemotePath(), + final String tmpTargetPath = context.getExternalCacheDir()+ FileUtils.PATH_SEPARATOR+ syncedFileState.getName(); + final DownloadFileRemoteOperation downloadOperation = new DownloadFileRemoteOperation(remoteFile.getRemotePath(), tmpTargetPath); - RemoteOperationResult downloadResult = downloadOperation.execute(ownCloudClient); + final RemoteOperationResult downloadResult = downloadOperation.execute(ownCloudClient); RemoteOperationResult.ResultCode resultCode; boolean mustRestart = true; if (downloadResult.isSuccess()) { - File tmpLocalFile = new File(tmpTargetPath); + final File tmpLocalFile = new File(tmpTargetPath); if (!tmpLocalFile.exists()) { Log.e(TAG, "Downloaded file doesn't exist or is null"); resultCode = RemoteOperationResult.ResultCode.FILE_NOT_FOUND; @@ -87,13 +87,13 @@ public class DownloadFileOperation extends RemoteOperation { } else { //file has been correctly download. - File localFile = new File(targetPath); + final File localFile = new File(targetPath); if (localFile.exists()) { localFile.delete(); } //Check parentFolder existence and create if needed - String parentFoldersPath = localFile.getParent(); - File localParentFile = new File(parentFoldersPath); + final String parentFoldersPath = localFile.getParent(); + final File localParentFile = new File(parentFoldersPath); if (!localParentFile.exists()) { if (localParentFile.mkdirs()) Log.d(TAG, "Created folders: "+parentFoldersPath); -- GitLab