From 483d25c65f3cdc53d41322cddd604691ac4677fa Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 12 May 2022 14:37:22 +0200 Subject: [PATCH 1/7] add SyncThreadHolder class --- .../e/drive/models/SyncThreadHolder.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java diff --git a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java b/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java new file mode 100644 index 00000000..8a355b25 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java @@ -0,0 +1,81 @@ +/* + * Copyright © Vincent Bourgmayer (/e/ foundation). + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ +package foundation.e.drive.models; + +import android.content.Context; + +import com.owncloud.android.lib.common.operations.RemoteOperation; + +import foundation.e.drive.operations.DownloadFileOperation; +import foundation.e.drive.operations.UploadFileOperation; + +/** + * This class encapsulates data, for SynchronizationSerivce, about a thread which run a RemoteOperation + * + * @author Vincent Bourgmayer + */ +public class SyncThreadHolder { + private final SyncRequest request; + private final RemoteOperation remoteOperation; + private final int threadIndex; + private boolean isRunning; + + /** + * Build an instance of SyncThreadHolder for a file transfer + * @param threadIndex identifier of the thread for SynchronizationService + * @param request SyncRequest at origin of the file transfer + * @param context Application context, used to create RemoteOperation to run + */ + public SyncThreadHolder(int threadIndex, SyncRequest request, Context context) { + this.request = request; + this.threadIndex = threadIndex; + remoteOperation = createRemoteOperation(context); + isRunning = false; + } + + public SyncRequest getRequest() { + return request; + } + + public RemoteOperation getRemoteOperation() { + return remoteOperation; + } + + public int getThreadIndex() { + return threadIndex; + } + + public boolean isRunning() { + return isRunning; + } + + public void setRunning(boolean running) { + isRunning = running; + } + + private RemoteOperation createRemoteOperation(Context context) { + final RemoteOperation operation; + switch (request.getOperationType()){ + case UPLOAD: + final SyncedFileState sfs = request.getSyncedFileState(); + operation = new UploadFileOperation(sfs, context); + break; + case DOWNLOAD: + final DownloadRequest downloadRequest = (DownloadRequest) request; + operation = new DownloadFileOperation(downloadRequest.getRemoteFile(), + downloadRequest.getSyncedFileState(), + context); + break; + case REMOTE_DELETE: + default: + operation = null; + break; + } + return operation; + } +} -- GitLab From 43fe92dc3e62727ff95b92e6ab258a596a2e0caf Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 12 May 2022 17:13:52 +0200 Subject: [PATCH 2/7] skip SyncRequest that are already being performed --- .../e/drive/models/SyncThreadHolder.java | 6 +- .../services/SynchronizationService.java | 94 ++++++++++--------- 2 files changed, 57 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java b/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java index 8a355b25..27663e53 100644 --- a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java +++ b/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java @@ -54,7 +54,7 @@ public class SyncThreadHolder { return isRunning; } - public void setRunning(boolean running) { + public synchronized void setRunning(boolean running) { isRunning = running; } @@ -78,4 +78,8 @@ public class SyncThreadHolder { } return operation; } + + public boolean alsoRunFor(SyncRequest request){ + return (request.equals(this.request) && request.getOperationType() == this.request.getOperationType()); + } } 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 3133b4ea..de37b430 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -30,13 +30,13 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collection; -import java.util.Hashtable; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedDeque; import foundation.e.drive.database.DbHelper; -import foundation.e.drive.models.DownloadRequest; import foundation.e.drive.models.SyncRequest; -import foundation.e.drive.models.SyncedFileState; +import foundation.e.drive.models.SyncThreadHolder; import foundation.e.drive.operations.DownloadFileOperation; import foundation.e.drive.operations.RemoveFileOperation; import foundation.e.drive.operations.UploadFileOperation; @@ -52,10 +52,10 @@ public class SynchronizationService extends Service implements OnRemoteOperation private final SynchronizationBinder binder = new SynchronizationBinder(); private ConcurrentLinkedDeque syncRequestQueue; - private Hashtable startedOperations; //Operations which are running + private ConcurrentHashMap startedSync; //Integer is thread index (1 to workerAmount) + private Account account; private final int workerAmount = 4; - private boolean[] threadWorkingState; //State of the threads; true mean the thread is working private Thread[] threadPool; private OwnCloudClient client; private OperationHandler handler; @@ -76,9 +76,8 @@ public class SynchronizationService extends Service implements OnRemoteOperation } syncRequestQueue = new ConcurrentLinkedDeque<>(); - startedOperations = new Hashtable<>(); + startedSync = new ConcurrentHashMap<>(); threadPool = new Thread[workerAmount]; - threadWorkingState = new boolean[workerAmount]; client = DavClientProvider.getInstance().getClientInstance(account, getApplicationContext()); handler = new OperationHandler(this); @@ -97,15 +96,39 @@ public class SynchronizationService extends Service implements OnRemoteOperation } public void queueOperation(SyncRequest request){ + if (!isAlreadyHandled(request)) { syncRequestQueue.remove(request); syncRequestQueue.add(request); + } } public void queueOperations(Collection requests){ - syncRequestQueue.removeAll(requests); - syncRequestQueue.addAll(requests); + //@TODO find a way to filter running request before to add them + syncRequestQueue.removeAll(requests); + syncRequestQueue.addAll(requests); } + //TMP code + + private boolean isAlreadyHandled(SyncRequest request) { + for (SyncThreadHolder threadHolder : startedSync.values()) { + if (threadHolder.alsoRunFor(request)) { + return true; + } + } + return false; + } + + private ArrayList getRunningSyncRequest(){ + ArrayList result = new ArrayList<>(); + for (SyncThreadHolder threadHolder : startedSync.values()) { + result.add(threadHolder.getRequest()); + } + return result; + } + + // End of TMP code + public void startSynchronization(){ Log.d(TAG, "startAllThreads"); for(int i =-1; ++i < workerAmount;){ @@ -113,9 +136,11 @@ public class SynchronizationService extends Service implements OnRemoteOperation } } - private void startWorker(int threadIndex){ + + private void startWorker(Integer threadIndex){ final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); - if (threadWorkingState[threadIndex] + + if (startedSync.get(threadIndex) == null || startedSync.get(threadIndex).isRunning() || !CommonUtils.haveNetworkConnection(getApplicationContext(), meteredNetworkAllowed)) { return; } @@ -123,16 +148,18 @@ public class SynchronizationService extends Service implements OnRemoteOperation final SyncRequest request = this.syncRequestQueue.poll(); //return null if empty if (request == null) return; - final RemoteOperation operation = this.createRemoteOperation(request); + final SyncThreadHolder threadHolder = new SyncThreadHolder(threadIndex, request, getApplicationContext()); + final RemoteOperation operation = threadHolder.getRemoteOperation(); if (operation != null) { Log.v(TAG, " an operation has been poll from queue"); if (CommonUtils.isThisSyncAllowed(account, request.getSyncedFileState().isMediaType())) { CommonUtils.createNotificationChannel(this); - startedOperations.put(operation, threadIndex); + threadPool[threadIndex] = operation.execute(client, this, handler); - threadWorkingState[threadIndex] = true; + startedSync.put(threadIndex, threadHolder); + threadHolder.setRunning(true); } } } @@ -140,10 +167,11 @@ public class SynchronizationService extends Service implements OnRemoteOperation @Override public void onRemoteOperationFinish(RemoteOperation callerOperation, RemoteOperationResult result) { Log.d(TAG, "onRemoteOperationFinish()"); - Integer threadIndex = this.startedOperations.remove(callerOperation); - if (threadIndex != null) { - this.threadWorkingState[threadIndex] = false; - this.startWorker(threadIndex); + for (Map.Entry keyValue : startedSync.entrySet()) { + if (keyValue.getValue().getRemoteOperation().equals(callerOperation)) { + keyValue.getValue().setRunning(false); + startWorker(keyValue.getKey()); + } } if (callerOperation instanceof RemoveFileOperation){ @@ -209,26 +237,6 @@ public class SynchronizationService extends Service implements OnRemoteOperation } } - private RemoteOperation createRemoteOperation(SyncRequest request){ - RemoteOperation operation; - switch (request.getOperationType()){ - case UPLOAD: - final SyncedFileState sfs = request.getSyncedFileState(); - operation = new UploadFileOperation(sfs, getApplicationContext()); - break; - case DOWNLOAD: - final DownloadRequest downloadRequest = (DownloadRequest) request; - operation = new DownloadFileOperation(downloadRequest.getRemoteFile(), downloadRequest.getSyncedFileState(), getApplicationContext()); - break; - case REMOTE_DELETE: - default: - operation = null; - break; - } - return operation; - } - - /** * Handler for the class */ @@ -244,10 +252,12 @@ public class SynchronizationService extends Service implements OnRemoteOperation @Override public void handleMessage(Message msg) { Log.i(TAG, "handler.handleMessage()"); - Bundle data = msg.getData(); - - serviceWeakRef.get() - .threadWorkingState[data.getInt("thread index")] = data.getBoolean("mThreadWorkingState"); + final Bundle data = msg.getData(); + if (data == null || !data.containsKey("thread index") || !data.containsKey("mThreadWorkingState")) { + return ; + } + final SyncThreadHolder threadHolder = serviceWeakRef.get().startedSync.get(data.getInt("thread index")); + if (threadHolder != null) threadHolder.setRunning(data.getBoolean("mThreadWorkingState")); } } -- GitLab From e6067dff61453e5dbdd272669f6bd13f047cb313 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 12 May 2022 17:53:13 +0200 Subject: [PATCH 3/7] Rewrite Synchronization code to check if SyncRequest is already running before to add it --- .../e/drive/models/SyncThreadHolder.java | 4 +- .../services/SynchronizationService.java | 64 +++++++++---------- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java b/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java index 27663e53..efc5ec4f 100644 --- a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java +++ b/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java @@ -35,7 +35,7 @@ public class SyncThreadHolder { this.request = request; this.threadIndex = threadIndex; remoteOperation = createRemoteOperation(context); - isRunning = false; + isRunning = true; } public SyncRequest getRequest() { @@ -79,7 +79,7 @@ public class SyncThreadHolder { return operation; } - public boolean alsoRunFor(SyncRequest request){ + public boolean alsoWorksFor(SyncRequest request){ return (request.equals(this.request) && request.getOperationType() == this.request.getOperationType()); } } 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 de37b430..a7debe37 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -96,38 +96,23 @@ public class SynchronizationService extends Service implements OnRemoteOperation } public void queueOperation(SyncRequest request){ - if (!isAlreadyHandled(request)) { - syncRequestQueue.remove(request); - syncRequestQueue.add(request); - } - } - - public void queueOperations(Collection requests){ - //@TODO find a way to filter running request before to add them - syncRequestQueue.removeAll(requests); - syncRequestQueue.addAll(requests); - } - - //TMP code - - private boolean isAlreadyHandled(SyncRequest request) { for (SyncThreadHolder threadHolder : startedSync.values()) { - if (threadHolder.alsoRunFor(request)) { - return true; + if (threadHolder.alsoWorksFor(request)) { + return; } } - return false; + syncRequestQueue.remove(request); + syncRequestQueue.add(request); } - private ArrayList getRunningSyncRequest(){ - ArrayList result = new ArrayList<>(); - for (SyncThreadHolder threadHolder : startedSync.values()) { - result.add(threadHolder.getRequest()); + public void queueOperations(Collection requests){ + for (SyncThreadHolder holder : startedSync.values()) { + requests.removeIf(syncRequest -> holder.alsoWorksFor(syncRequest)); } - return result; - } - // End of TMP code + syncRequestQueue.removeAll(requests); + syncRequestQueue.addAll(requests); + } public void startSynchronization(){ Log.d(TAG, "startAllThreads"); @@ -138,12 +123,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation private void startWorker(Integer threadIndex){ - final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); - - if (startedSync.get(threadIndex) == null || startedSync.get(threadIndex).isRunning() - || !CommonUtils.haveNetworkConnection(getApplicationContext(), meteredNetworkAllowed)) { - return; - } + if (!canStart(threadIndex)) return; final SyncRequest request = this.syncRequestQueue.poll(); //return null if empty if (request == null) return; @@ -152,18 +132,36 @@ public class SynchronizationService extends Service implements OnRemoteOperation final RemoteOperation operation = threadHolder.getRemoteOperation(); if (operation != null) { - Log.v(TAG, " an operation has been poll from queue"); + if (CommonUtils.isThisSyncAllowed(account, request.getSyncedFileState().isMediaType())) { CommonUtils.createNotificationChannel(this); + Log.v(TAG, " starts " + request.getSyncedFileState().getName() + + " " + request.getOperationType().name() + " on thread " + threadIndex); threadPool[threadIndex] = operation.execute(client, this, handler); startedSync.put(threadIndex, threadHolder); - threadHolder.setRunning(true); } } } + /** + * Check if conditions are met to run a new file transfer + * @param threadIndex index of thread on which we want to perform the transfer + * @return false if nogo + */ + private boolean canStart(int threadIndex) { + final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); + + final SyncThreadHolder runningHolder = startedSync.get(threadIndex); + + if ( (runningHolder != null && runningHolder.isRunning()) + || !CommonUtils.haveNetworkConnection(getApplicationContext(), meteredNetworkAllowed)) { + return false; + } + return true; + } + @Override public void onRemoteOperationFinish(RemoteOperation callerOperation, RemoteOperationResult result) { Log.d(TAG, "onRemoteOperationFinish()"); -- GitLab From 9487690219de2350481ca07e18a5753069516d71 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 12 May 2022 18:18:53 +0200 Subject: [PATCH 4/7] fix coding style & remove dead code --- .../e/drive/models/SyncThreadHolder.java | 17 +++-------------- .../drive/services/SynchronizationService.java | 2 +- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java b/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java index efc5ec4f..5c0cd2d7 100644 --- a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java +++ b/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java @@ -22,34 +22,23 @@ import foundation.e.drive.operations.UploadFileOperation; public class SyncThreadHolder { private final SyncRequest request; private final RemoteOperation remoteOperation; - private final int threadIndex; private boolean isRunning; /** * Build an instance of SyncThreadHolder for a file transfer - * @param threadIndex identifier of the thread for SynchronizationService * @param request SyncRequest at origin of the file transfer * @param context Application context, used to create RemoteOperation to run */ - public SyncThreadHolder(int threadIndex, SyncRequest request, Context context) { + public SyncThreadHolder(SyncRequest request, Context context) { this.request = request; - this.threadIndex = threadIndex; remoteOperation = createRemoteOperation(context); isRunning = true; } - public SyncRequest getRequest() { - return request; - } - public RemoteOperation getRemoteOperation() { return remoteOperation; } - public int getThreadIndex() { - return threadIndex; - } - public boolean isRunning() { return isRunning; } @@ -60,7 +49,7 @@ public class SyncThreadHolder { private RemoteOperation createRemoteOperation(Context context) { final RemoteOperation operation; - switch (request.getOperationType()){ + switch (request.getOperationType()) { case UPLOAD: final SyncedFileState sfs = request.getSyncedFileState(); operation = new UploadFileOperation(sfs, context); @@ -79,7 +68,7 @@ public class SyncThreadHolder { return operation; } - public boolean alsoWorksFor(SyncRequest request){ + public boolean alsoWorksFor(SyncRequest request) { return (request.equals(this.request) && request.getOperationType() == this.request.getOperationType()); } } 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 a7debe37..da3cb5b3 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -128,7 +128,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation final SyncRequest request = this.syncRequestQueue.poll(); //return null if empty if (request == null) return; - final SyncThreadHolder threadHolder = new SyncThreadHolder(threadIndex, request, getApplicationContext()); + final SyncThreadHolder threadHolder = new SyncThreadHolder(request, getApplicationContext()); final RemoteOperation operation = threadHolder.getRemoteOperation(); if (operation != null) { -- GitLab From 4926b254cfc9bbe3bb0d36340e618d2038102bc5 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 13 May 2022 10:05:11 +0200 Subject: [PATCH 5/7] Make SyncThreadHolder.createRemoteOperation(...) a static method and add comment for this method and for 'alsoRunFor(SyncRequest request)' --- .../e/drive/models/SyncThreadHolder.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java b/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java index 5c0cd2d7..bfad0046 100644 --- a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java +++ b/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java @@ -31,7 +31,7 @@ public class SyncThreadHolder { */ public SyncThreadHolder(SyncRequest request, Context context) { this.request = request; - remoteOperation = createRemoteOperation(context); + remoteOperation = createRemoteOperation(request, context); isRunning = true; } @@ -47,7 +47,13 @@ public class SyncThreadHolder { isRunning = running; } - private RemoteOperation createRemoteOperation(Context context) { + /** + * Create the RemoteOperation (to perform file transfer) based on SyncRequest + * @param request syncRequest for the file + * @param context App context to be passed to RemoteOperation's contructor + * @return RemoteOperation for Upload/Download request or null + */ + private static RemoteOperation createRemoteOperation(SyncRequest request, Context context) { final RemoteOperation operation; switch (request.getOperationType()) { case UPLOAD: @@ -68,6 +74,14 @@ public class SyncThreadHolder { return operation; } + /** + * This method indicate that the given SyncRequest is the same as the one that has created + * Running RemoteOperation of this instance. + * + * Note: SyncRequest.equals() only compare file path (local or remote), not operation type. + * @param request SyncRequest to compare + * @return true is the given syncRequest is for the same file and of the same type as the one of this instance + */ public boolean alsoWorksFor(SyncRequest request) { return (request.equals(this.request) && request.getOperationType() == this.request.getOperationType()); } -- GitLab From dea192f268058afb24fa9b56baceb12aa640d57e Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 13 May 2022 13:39:49 +0200 Subject: [PATCH 6/7] rename SyncThreadHolder in SyncThreadWrapper also replace "alsoWorksFor()" by equals override in SyncThreadWrapper --- ...readHolder.java => SyncThreadWrapper.java} | 21 ++++++-------- .../services/SynchronizationService.java | 28 +++++++++---------- 2 files changed, 23 insertions(+), 26 deletions(-) rename app/src/main/java/foundation/e/drive/models/{SyncThreadHolder.java => SyncThreadWrapper.java} (78%) diff --git a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java b/app/src/main/java/foundation/e/drive/models/SyncThreadWrapper.java similarity index 78% rename from app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java rename to app/src/main/java/foundation/e/drive/models/SyncThreadWrapper.java index bfad0046..1ddd4519 100644 --- a/app/src/main/java/foundation/e/drive/models/SyncThreadHolder.java +++ b/app/src/main/java/foundation/e/drive/models/SyncThreadWrapper.java @@ -19,7 +19,7 @@ import foundation.e.drive.operations.UploadFileOperation; * * @author Vincent Bourgmayer */ -public class SyncThreadHolder { +public class SyncThreadWrapper { private final SyncRequest request; private final RemoteOperation remoteOperation; private boolean isRunning; @@ -29,7 +29,7 @@ public class SyncThreadHolder { * @param request SyncRequest at origin of the file transfer * @param context Application context, used to create RemoteOperation to run */ - public SyncThreadHolder(SyncRequest request, Context context) { + public SyncThreadWrapper(SyncRequest request, Context context) { this.request = request; remoteOperation = createRemoteOperation(request, context); isRunning = true; @@ -74,15 +74,12 @@ public class SyncThreadHolder { return operation; } - /** - * This method indicate that the given SyncRequest is the same as the one that has created - * Running RemoteOperation of this instance. - * - * Note: SyncRequest.equals() only compare file path (local or remote), not operation type. - * @param request SyncRequest to compare - * @return true is the given syncRequest is for the same file and of the same type as the one of this instance - */ - public boolean alsoWorksFor(SyncRequest request) { - return (request.equals(this.request) && request.getOperationType() == this.request.getOperationType()); + @Override + public boolean equals(Object obj) { + if (obj instanceof SyncRequest) { + final SyncRequest objRequest = (SyncRequest) obj; + return (objRequest.equals(this.request) && objRequest.getOperationType() == this.request.getOperationType()); + } + return super.equals(obj); } } 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 da3cb5b3..25a553c3 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -36,7 +36,7 @@ import java.util.concurrent.ConcurrentLinkedDeque; import foundation.e.drive.database.DbHelper; import foundation.e.drive.models.SyncRequest; -import foundation.e.drive.models.SyncThreadHolder; +import foundation.e.drive.models.SyncThreadWrapper; import foundation.e.drive.operations.DownloadFileOperation; import foundation.e.drive.operations.RemoveFileOperation; import foundation.e.drive.operations.UploadFileOperation; @@ -52,7 +52,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation private final SynchronizationBinder binder = new SynchronizationBinder(); private ConcurrentLinkedDeque syncRequestQueue; - private ConcurrentHashMap startedSync; //Integer is thread index (1 to workerAmount) + private ConcurrentHashMap startedSync; //Integer is thread index (1 to workerAmount) private Account account; private final int workerAmount = 4; @@ -96,8 +96,8 @@ public class SynchronizationService extends Service implements OnRemoteOperation } public void queueOperation(SyncRequest request){ - for (SyncThreadHolder threadHolder : startedSync.values()) { - if (threadHolder.alsoWorksFor(request)) { + for (SyncThreadWrapper threadWrapper : startedSync.values()) { + if (threadWrapper.equals(request)) { return; } } @@ -106,8 +106,8 @@ public class SynchronizationService extends Service implements OnRemoteOperation } public void queueOperations(Collection requests){ - for (SyncThreadHolder holder : startedSync.values()) { - requests.removeIf(syncRequest -> holder.alsoWorksFor(syncRequest)); + for (SyncThreadWrapper threadWrapper : startedSync.values()) { + requests.removeIf(syncRequest -> threadWrapper.equals(syncRequest)); } syncRequestQueue.removeAll(requests); @@ -128,8 +128,8 @@ public class SynchronizationService extends Service implements OnRemoteOperation final SyncRequest request = this.syncRequestQueue.poll(); //return null if empty if (request == null) return; - final SyncThreadHolder threadHolder = new SyncThreadHolder(request, getApplicationContext()); - final RemoteOperation operation = threadHolder.getRemoteOperation(); + final SyncThreadWrapper threadWrapper = new SyncThreadWrapper(request, getApplicationContext()); + final RemoteOperation operation = threadWrapper.getRemoteOperation(); if (operation != null) { @@ -140,7 +140,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation + " " + request.getOperationType().name() + " on thread " + threadIndex); threadPool[threadIndex] = operation.execute(client, this, handler); - startedSync.put(threadIndex, threadHolder); + startedSync.put(threadIndex, threadWrapper); } } } @@ -153,9 +153,9 @@ public class SynchronizationService extends Service implements OnRemoteOperation private boolean canStart(int threadIndex) { final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); - final SyncThreadHolder runningHolder = startedSync.get(threadIndex); + final SyncThreadWrapper threadWrapper = startedSync.get(threadIndex); - if ( (runningHolder != null && runningHolder.isRunning()) + if ( (threadWrapper != null && threadWrapper.isRunning()) || !CommonUtils.haveNetworkConnection(getApplicationContext(), meteredNetworkAllowed)) { return false; } @@ -165,7 +165,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation @Override public void onRemoteOperationFinish(RemoteOperation callerOperation, RemoteOperationResult result) { Log.d(TAG, "onRemoteOperationFinish()"); - for (Map.Entry keyValue : startedSync.entrySet()) { + for (Map.Entry keyValue : startedSync.entrySet()) { if (keyValue.getValue().getRemoteOperation().equals(callerOperation)) { keyValue.getValue().setRunning(false); startWorker(keyValue.getKey()); @@ -254,8 +254,8 @@ public class SynchronizationService extends Service implements OnRemoteOperation if (data == null || !data.containsKey("thread index") || !data.containsKey("mThreadWorkingState")) { return ; } - final SyncThreadHolder threadHolder = serviceWeakRef.get().startedSync.get(data.getInt("thread index")); - if (threadHolder != null) threadHolder.setRunning(data.getBoolean("mThreadWorkingState")); + final SyncThreadWrapper threadWrapper = serviceWeakRef.get().startedSync.get(data.getInt("thread index")); + if (threadWrapper != null) threadWrapper.setRunning(data.getBoolean("mThreadWorkingState")); } } -- GitLab From 7fd217cbdbdbabc0ff4fba362720bfedccd8f2ce Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 13 May 2022 14:22:18 +0200 Subject: [PATCH 7/7] rename SyncThreadWrapper into SyncWrapper --- ...yncThreadWrapper.java => SyncWrapper.java} | 4 +-- .../services/SynchronizationService.java | 28 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) rename app/src/main/java/foundation/e/drive/models/{SyncThreadWrapper.java => SyncWrapper.java} (96%) diff --git a/app/src/main/java/foundation/e/drive/models/SyncThreadWrapper.java b/app/src/main/java/foundation/e/drive/models/SyncWrapper.java similarity index 96% rename from app/src/main/java/foundation/e/drive/models/SyncThreadWrapper.java rename to app/src/main/java/foundation/e/drive/models/SyncWrapper.java index 1ddd4519..fdf119d9 100644 --- a/app/src/main/java/foundation/e/drive/models/SyncThreadWrapper.java +++ b/app/src/main/java/foundation/e/drive/models/SyncWrapper.java @@ -19,7 +19,7 @@ import foundation.e.drive.operations.UploadFileOperation; * * @author Vincent Bourgmayer */ -public class SyncThreadWrapper { +public class SyncWrapper { private final SyncRequest request; private final RemoteOperation remoteOperation; private boolean isRunning; @@ -29,7 +29,7 @@ public class SyncThreadWrapper { * @param request SyncRequest at origin of the file transfer * @param context Application context, used to create RemoteOperation to run */ - public SyncThreadWrapper(SyncRequest request, Context context) { + public SyncWrapper(SyncRequest request, Context context) { this.request = request; remoteOperation = createRemoteOperation(request, context); isRunning = true; 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 25a553c3..061672de 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -36,7 +36,7 @@ import java.util.concurrent.ConcurrentLinkedDeque; import foundation.e.drive.database.DbHelper; import foundation.e.drive.models.SyncRequest; -import foundation.e.drive.models.SyncThreadWrapper; +import foundation.e.drive.models.SyncWrapper; import foundation.e.drive.operations.DownloadFileOperation; import foundation.e.drive.operations.RemoveFileOperation; import foundation.e.drive.operations.UploadFileOperation; @@ -52,7 +52,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation private final SynchronizationBinder binder = new SynchronizationBinder(); private ConcurrentLinkedDeque syncRequestQueue; - private ConcurrentHashMap startedSync; //Integer is thread index (1 to workerAmount) + private ConcurrentHashMap startedSync; //Integer is thread index (1 to workerAmount) private Account account; private final int workerAmount = 4; @@ -96,8 +96,8 @@ public class SynchronizationService extends Service implements OnRemoteOperation } public void queueOperation(SyncRequest request){ - for (SyncThreadWrapper threadWrapper : startedSync.values()) { - if (threadWrapper.equals(request)) { + for (SyncWrapper syncWrapper : startedSync.values()) { + if (syncWrapper.equals(request)) { return; } } @@ -106,8 +106,8 @@ public class SynchronizationService extends Service implements OnRemoteOperation } public void queueOperations(Collection requests){ - for (SyncThreadWrapper threadWrapper : startedSync.values()) { - requests.removeIf(syncRequest -> threadWrapper.equals(syncRequest)); + for (SyncWrapper syncWrapper : startedSync.values()) { + requests.removeIf(syncRequest -> syncWrapper.equals(syncRequest)); } syncRequestQueue.removeAll(requests); @@ -128,8 +128,8 @@ public class SynchronizationService extends Service implements OnRemoteOperation final SyncRequest request = this.syncRequestQueue.poll(); //return null if empty if (request == null) return; - final SyncThreadWrapper threadWrapper = new SyncThreadWrapper(request, getApplicationContext()); - final RemoteOperation operation = threadWrapper.getRemoteOperation(); + final SyncWrapper syncWrapper = new SyncWrapper(request, getApplicationContext()); + final RemoteOperation operation = syncWrapper.getRemoteOperation(); if (operation != null) { @@ -140,7 +140,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation + " " + request.getOperationType().name() + " on thread " + threadIndex); threadPool[threadIndex] = operation.execute(client, this, handler); - startedSync.put(threadIndex, threadWrapper); + startedSync.put(threadIndex, syncWrapper); } } } @@ -153,9 +153,9 @@ public class SynchronizationService extends Service implements OnRemoteOperation private boolean canStart(int threadIndex) { final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); - final SyncThreadWrapper threadWrapper = startedSync.get(threadIndex); + final SyncWrapper syncWrapper = startedSync.get(threadIndex); - if ( (threadWrapper != null && threadWrapper.isRunning()) + if ( (syncWrapper != null && syncWrapper.isRunning()) || !CommonUtils.haveNetworkConnection(getApplicationContext(), meteredNetworkAllowed)) { return false; } @@ -165,7 +165,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation @Override public void onRemoteOperationFinish(RemoteOperation callerOperation, RemoteOperationResult result) { Log.d(TAG, "onRemoteOperationFinish()"); - for (Map.Entry keyValue : startedSync.entrySet()) { + for (Map.Entry keyValue : startedSync.entrySet()) { if (keyValue.getValue().getRemoteOperation().equals(callerOperation)) { keyValue.getValue().setRunning(false); startWorker(keyValue.getKey()); @@ -254,8 +254,8 @@ public class SynchronizationService extends Service implements OnRemoteOperation if (data == null || !data.containsKey("thread index") || !data.containsKey("mThreadWorkingState")) { return ; } - final SyncThreadWrapper threadWrapper = serviceWeakRef.get().startedSync.get(data.getInt("thread index")); - if (threadWrapper != null) threadWrapper.setRunning(data.getBoolean("mThreadWorkingState")); + final SyncWrapper syncWrapper = serviceWeakRef.get().startedSync.get(data.getInt("thread index")); + if (syncWrapper != null) syncWrapper.setRunning(data.getBoolean("mThreadWorkingState")); } } -- GitLab