diff --git a/app/src/main/java/foundation/e/drive/models/SyncRequest.java b/app/src/main/java/foundation/e/drive/models/SyncRequest.java index e2412cdf2fa5d9fb550cd08364174c85a2ae205f..a07c556ed1919dcd60c77902a28dfc87a48cffa7 100644 --- a/app/src/main/java/foundation/e/drive/models/SyncRequest.java +++ b/app/src/main/java/foundation/e/drive/models/SyncRequest.java @@ -32,7 +32,9 @@ public class SyncRequest { @Override public boolean equals(@Nullable Object obj) { if (obj instanceof SyncRequest) { - return (syncedFileState.getId() == ((SyncRequest) obj).syncedFileState.getId() ); + final SyncedFileState objFileState = ((SyncRequest) obj).syncedFileState; + return (syncedFileState.getLocalPath().equals(objFileState.getLocalPath()) || + syncedFileState.getRemotePath().equals(objFileState.getRemotePath())); } 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 72cee50c44d164f321e499f76523e008e4695769..3133b4eac46a178b76b55b88772977b62eed349a 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -9,9 +9,6 @@ package foundation.e.drive.services; import android.accounts.Account; import android.accounts.AccountManager; -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; @@ -24,7 +21,6 @@ import android.os.Message; import android.util.Log; import androidx.annotation.Nullable; -import androidx.core.app.NotificationCompat; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; @@ -37,7 +33,6 @@ import java.util.Collection; import java.util.Hashtable; import java.util.concurrent.ConcurrentLinkedDeque; -import foundation.e.drive.R; import foundation.e.drive.database.DbHelper; import foundation.e.drive.models.DownloadRequest; import foundation.e.drive.models.SyncRequest; @@ -56,7 +51,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation private final static String TAG = SynchronizationService.class.getSimpleName(); private final SynchronizationBinder binder = new SynchronizationBinder(); - private ConcurrentLinkedDeque syncedRequestQueue; + private ConcurrentLinkedDeque syncRequestQueue; private Hashtable startedOperations; //Operations which are running private Account account; private final int workerAmount = 4; @@ -80,7 +75,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation return START_NOT_STICKY; } - syncedRequestQueue = new ConcurrentLinkedDeque<>(); + syncRequestQueue = new ConcurrentLinkedDeque<>(); startedOperations = new Hashtable<>(); threadPool = new Thread[workerAmount]; threadWorkingState = new boolean[workerAmount]; @@ -101,12 +96,14 @@ public class SynchronizationService extends Service implements OnRemoteOperation Log.w(TAG, "System is low on memory. Service might get killed."); } - public boolean queueOperation(SyncRequest request){ - return syncedRequestQueue.add(request); + public void queueOperation(SyncRequest request){ + syncRequestQueue.remove(request); + syncRequestQueue.add(request); } - public boolean queueOperations(Collection requests){ - return syncedRequestQueue.addAll(requests); + public void queueOperations(Collection requests){ + syncRequestQueue.removeAll(requests); + syncRequestQueue.addAll(requests); } public void startSynchronization(){ @@ -117,23 +114,25 @@ public class SynchronizationService extends Service implements OnRemoteOperation } private void startWorker(int threadIndex){ - if (syncedRequestQueue.isEmpty()) return; final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(account); - if (!threadWorkingState[threadIndex] && CommonUtils.haveNetworkConnection(getApplicationContext(), meteredNetworkAllowed)) { //check if the thread corresponding to threadIndex isn't already working + if (threadWorkingState[threadIndex] + || !CommonUtils.haveNetworkConnection(getApplicationContext(), meteredNetworkAllowed)) { + return; + } - final SyncRequest request = this.syncedRequestQueue.poll(); //return null if deque is empty + final SyncRequest request = this.syncRequestQueue.poll(); //return null if empty + if (request == null) return; - final RemoteOperation operation = this.createRemoteOperation(request); + final RemoteOperation operation = this.createRemoteOperation(request); - if (operation != null) { - Log.v(TAG, " an operation has been poll from queue"); + 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; - } + if (CommonUtils.isThisSyncAllowed(account, request.getSyncedFileState().isMediaType())) { + CommonUtils.createNotificationChannel(this); + startedOperations.put(operation, threadIndex); + threadPool[threadIndex] = operation.execute(client, this, handler); + threadWorkingState[threadIndex] = true; } } }