Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f46be3ca authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

Implement new class SyncRequest

This class and subclasses will be passed from ObserverService and FileObserver to SynchronizationService
instead of RemoteOperation (UploadFileOperation, RemoveFileOperation, DownloadFileOperation)
The naming is more clear, and ObserverService doesn't have to create the RemoteOperation wich is out of its scope.

The design isn't yet full complete so this Commit might be updated
As it is now, the implementation isn't complete so the build won't succeed.

Changes:
- Add SyncRequest class into models package
- Add UploadRequest as subclass of SyncRequest
- Add DownloadRequest as Subclass of Syncrequest
- Replace ComparableOperation in ObserverService and in SynchronizationService by SyncRequest
- Add a method "CreateRemoteOperation(SyncRequest request" in SynchronizationService
parent 67c7a85b
Loading
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
package foundation.e.drive.models;

import com.owncloud.android.lib.resources.files.model.RemoteFile;

public class DownloadRequest extends SyncRequest{
    private RemoteFile remoteFile;

    public DownloadRequest (RemoteFile remoteFile, int syncedFileStateId){
        super(syncedFileStateId, Type.DOWNLOAD);
        this.remoteFile = remoteFile;
    }

    public RemoteFile getRemoteFile() {
        return remoteFile;
    }
}
+31 −0
Original line number Diff line number Diff line
package foundation.e.drive.models;

import androidx.annotation.Nullable;

public class SyncRequest {
    public enum Type { UPLOAD, DOWNLOAD, REMOTE_DELETE};

    final private int syncedFileStateId;
    final private Type operationType;

    public SyncRequest(int syncedFileStateId, Type operationType){
        this.syncedFileStateId = syncedFileStateId;
        this.operationType = operationType;
    }

    public Type getOperationType(){
        return operationType;
    }

    public int getSyncedFileStateId(){
        return syncedFileStateId;
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if(obj instanceof SyncRequest){
            return (syncedFileStateId == ((SyncRequest) obj).syncedFileStateId );
        }
        return super.equals(obj);
    }
}
+14 −0
Original line number Diff line number Diff line
package foundation.e.drive.models;

public class UploadRequest extends SyncRequest{
    private boolean checkEtag;

    public UploadRequest(boolean checkEtag, int syncedFileStateId){
        super(syncedFileStateId, Type.UPLOAD);
        this.checkEtag = checkEtag;
    }

    public boolean mustCheckEtag() {
        return checkEtag;
    }
}
+13 −13
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.Handler;
import android.os.IBinder;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudClient;
@@ -35,15 +34,14 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import foundation.e.drive.database.DbHelper;
import foundation.e.drive.fileFilters.CrashlogsFileFilter;
import foundation.e.drive.fileFilters.FileFilterFactory;
import foundation.e.drive.fileFilters.OnlyFileFilter;
import foundation.e.drive.models.SyncRequest;
import foundation.e.drive.models.SyncedFolder;
import foundation.e.drive.models.SyncedFileState;
import foundation.e.drive.operations.ComparableOperation;
import foundation.e.drive.operations.DownloadFileOperation;
import foundation.e.drive.operations.ListFileRemoteOperation;
import foundation.e.drive.operations.RemoveFileOperation;
@@ -72,7 +70,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
    private boolean isWorking = false;
    private int initialFolderCounter;
    private Account mAccount;
    private HashMap<Integer, ComparableOperation> operationsForIntent; //integer is SyncedFileState id; Parcelable is the operation
    private HashMap<Integer, SyncRequest> syncRequests; //integer is SyncedFileState id; Parcelable is the operation

    private SynchronizationService synchronizationService;
    private boolean boundToSynchronizationService = false;
@@ -163,7 +161,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
            Log.w(TAG, "There is no Internet connexion.");
            return super.onStartCommand( intent, flags, startId );
        }
        this.operationsForIntent = new HashMap<>();
        this.syncRequests = new HashMap<>();

        Intent SynchronizationServiceIntent = new Intent(this.getApplicationContext(), SynchronizationService.class);
        bindService(SynchronizationServiceIntent, SynchronizationServiceConnection, Context.BIND_AUTO_CREATE);
@@ -325,11 +323,11 @@ public class ObserverService extends Service implements OnRemoteOperationListene
            }
            this.startScan(false);

            Log.v(TAG, "operationsForIntent contains "+ operationsForIntent.size()  );
            Log.v(TAG, "operationsForIntent contains "+ syncRequests.size()  );

            //After everything has been scanned. Send Intent to OperationmanagerService with data in bundle
            if (operationsForIntent != null && !operationsForIntent.isEmpty()) {
                this.synchronizationService.queueOperations(operationsForIntent.values());
            if (syncRequests != null && !syncRequests.isEmpty()) {
                this.synchronizationService.queueOperations(syncRequests.values());
            } else {
                Log.w(TAG, "There is no file to sync.");
                getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE)
@@ -414,8 +412,10 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                            Log.v(TAG, affectedRows + " syncedFileState.s row in DB has been updated.");
                        } else {
                            Log.i(TAG, "Add download operation for file "+syncedFileState.getId());


                            DownloadFileOperation downloadFileOperation = new DownloadFileOperation(remoteFile, syncedFileState);
                            this.operationsForIntent.put(syncedFileState.getId(), downloadFileOperation);
                            this.syncRequests.put(syncedFileState.getId(), downloadFileOperation);
                        }
                    }
                    syncedFileListIterator.remove(); //we can delete syncedFile from list because its correspondant has already been found and handled
@@ -448,7 +448,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                            newRemoteFile.setId(storedId);
                            Log.i(TAG, "Add download operation for new file "+storedId);
                            //Create Download operation and add it into Bundle
                            this.operationsForIntent.put(storedId, new DownloadFileOperation(remoteFile, newRemoteFile));
                            this.syncRequests.put(storedId, new DownloadFileOperation(remoteFile, newRemoteFile));

                        } else {
                            Log.w(TAG, "Can't save new remote File in DB. Ignore file.");
@@ -710,7 +710,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                        }
                        Log.i(TAG, "Add upload operation for file "+syncedFileState.getId());
                        UploadFileOperation uploadFileOperation = new UploadFileOperation(syncedFileState, checkEtag );
                        this.operationsForIntent.put(syncedFileState.getId(), uploadFileOperation);
                        this.syncRequests.put(syncedFileState.getId(), uploadFileOperation);
                    }
                    // No need to reloop on it.
                    syncedFileListIterator.remove();
@@ -738,7 +738,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                        Log.i(TAG, "Add upload operation for new file "+storedId);
                        //create UploadOperation and add it into bundle
                        UploadFileOperation uploadOperation = new UploadFileOperation(newSyncedFileState, syncedFolder.isScanRemote());
                        this.operationsForIntent.put(storedId, uploadOperation);
                        this.syncRequests.put(storedId, uploadOperation);
                    } else {
                        Log.w(TAG, "The new file to synced cannot be store in DB. Ignore it");
                    }
@@ -767,7 +767,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                } else {
                    Log.i(TAG, "Add remove operation for file "+fileState.getId());
                    RemoveFileOperation removeOperation = new RemoveFileOperation(fileState);
                    this.operationsForIntent.put(fileState.getId(), removeOperation);
                    this.syncRequests.put(fileState.getId(), removeOperation);
                }
            }
        }
+29 −10
Original line number Diff line number Diff line
@@ -26,11 +26,10 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;

import foundation.e.drive.database.DbHelper;
import foundation.e.drive.operations.ComparableOperation;
import foundation.e.drive.models.SyncRequest;
import foundation.e.drive.operations.DownloadFileOperation;
import foundation.e.drive.operations.RemoveFileOperation;
import foundation.e.drive.operations.UploadFileOperation;
@@ -45,7 +44,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation
    private final static String TAG = SynchronizationService.class.getSimpleName();
    private final SynchronizationBinder binder = new SynchronizationBinder();

    private ConcurrentLinkedDeque<ComparableOperation> operationsQueue;
    private ConcurrentLinkedDeque<SyncRequest> syncedRequestQueue;
    private Hashtable<RemoteOperation, Integer> startedOperations; //Operations which are running
    private Account account;
    private final int workerAmount = 4;
@@ -68,7 +67,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation


        account = (Account) intent.getParcelableExtra("account");
        operationsQueue = new ConcurrentLinkedDeque<>();
        syncedRequestQueue = new ConcurrentLinkedDeque<>();
        threadPool = new Thread[workerAmount];
        threadWorkingState = new boolean[workerAmount];
        client = DavClientProvider.getInstance().getClientInstance(account, getApplicationContext());
@@ -91,12 +90,12 @@ public class SynchronizationService extends Service implements OnRemoteOperation
                .apply();
    }

    public boolean queueOperation(ComparableOperation operation){
        return operationsQueue.add(operation);
    public boolean queueOperation(SyncRequest request){
        return syncedRequestQueue.add(request);
    }

    public boolean queueOperations(Collection<ComparableOperation> operations){
        return operationsQueue.addAll(operations);
    public boolean queueOperations(Collection<SyncRequest> requests){
        return syncedRequestQueue.addAll(requests);
    }

    public void startSynchronization(){
@@ -107,10 +106,14 @@ public class SynchronizationService extends Service implements OnRemoteOperation
    }

    private void startWorker(int threadIndex){
        if (operationsQueue.isEmpty()) return;
        if (syncedRequestQueue.isEmpty()) return;
        if (!threadWorkingState[threadIndex] && CommonUtils.haveNetworkConnexion(getApplicationContext())) { //check if the thread corresponding to threadIndex isn't already working

            ComparableOperation operation = this.operationsQueue.poll(); //return null if deque is empty
            SyncRequest request = this.syncedRequestQueue.poll(); //return null if deque is empty

            RemoteOperation operation = this.createRemoteOperation(request);


            if (operation != null) {
                Log.v(TAG, " an operation has been poll from queue");

@@ -204,6 +207,22 @@ public class SynchronizationService extends Service implements OnRemoteOperation
        } //Close else
    }

    private RemoteOperation createRemoteOperation(SyncRequest request){
        RemoteOperation operation;
        switch (request.getOperationType()){
            case UPLOAD:
                operation = new UploadFileOperation();
                break;
            case DOWNLOAD:
                operation = new DownloadFileOperation();
                break;
            case REMOTE_DELETE:
                operation = new RemoveFileOperation();
                break;
        }
    }


    /**
     * Handler for the class
     */