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

Commit 7d596aeb authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

Implement ObserverService binding to SynchronizationService

- Update SynchronizationService.enqueueOperations() parameters: List<ComparableOperations> is now Collections<ComparableOperation>
- Implement Binding component into ObserverService
- Replace intent for OperationManagerService by binding usage to pass list of operations to perform into SynchronizationService
- Fixed coding style: add spaces around if/else statement in InitializerService
parent a49b3ffc
Loading
Loading
Loading
Loading
+72 −52
Original line number Diff line number Diff line
@@ -11,8 +11,10 @@ package foundation.e.drive.services;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.Handler;
@@ -41,6 +43,7 @@ import foundation.e.drive.fileFilters.FileFilterFactory;
import foundation.e.drive.fileFilters.OnlyFileFilter;
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;
@@ -69,9 +72,27 @@ public class ObserverService extends Service implements OnRemoteOperationListene
    private boolean isWorking = false;
    private int initialFolderCounter;
    private Account mAccount;
    private HashMap<Integer, Parcelable> operationsForIntent;
    /* Lifecycle Methods */
    private HashMap<Integer, ComparableOperation> operationsForIntent; //integer is SyncedFileState id; Parcelable is the operation

    private SynchronizationService synchronizationService;
    private boolean boundToSynchronizationService = false;
    private ServiceConnection SynchronizationServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            SynchronizationService.SynchronizationBinder binder = (SynchronizationService.SynchronizationBinder) iBinder;
            synchronizationService = binder.getService();
            boundToSynchronizationService = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.e(TAG, "onServiceDisconnected");
            boundToSynchronizationService = false;
        }
    };


    /* Lifecycle Methods */
    @Override
    public void onDestroy(){
        Log.i(TAG, "onDestroy()");
@@ -143,6 +164,10 @@ public class ObserverService extends Service implements OnRemoteOperationListene
            return super.onStartCommand( intent, flags, startId );
        }
        this.operationsForIntent = new HashMap<>();

        Intent SynchronizationServiceIntent = new Intent(this.getApplicationContext(), SynchronizationService.class);
        bindService(SynchronizationServiceIntent, SynchronizationServiceConnection, Context.BIND_AUTO_CREATE);

        begin();
        return super.onStartCommand( intent, flags, startId );
    }
@@ -304,13 +329,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene

            //After everything has been scanned. Send Intent to OperationmanagerService with data in bundle
            if (operationsForIntent != null && !operationsForIntent.isEmpty()) {
                Intent OMSIntent = new Intent(this, OperationManagerService.class);
                for(Map.Entry<Integer, Parcelable> entry: operationsForIntent.entrySet()){
                    OMSIntent.putExtra(entry.getKey()+"", entry.getValue());
                }

                OMSIntent.putExtra("account", mAccount);
                startService(OMSIntent);
                this.synchronizationService.queueOperations(operationsForIntent.values());
            } else {
                Log.w(TAG, "There is no file to sync.");
                getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE)
@@ -321,6 +340,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
            }

            this.isWorking = false;
            unbindService(SynchronizationServiceConnection);
            this.stopSelf();
        }
    }
+9 −6
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import com.owncloud.android.lib.common.operations.RemoteOperation;
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;
@@ -42,11 +43,7 @@ import foundation.e.drive.utils.DavClientProvider;
 */
public class SynchronizationService extends Service implements OnRemoteOperationListener {
    private final static String TAG = SynchronizationService.class.getSimpleName();
    private final Binder binder = new Binder(){
        SynchronizationService getService(){
            return SynchronizationService.this;
        }
    };
    private final SynchronizationBinder binder = new SynchronizationBinder();

    private ConcurrentLinkedDeque<ComparableOperation> operationsQueue;
    private Hashtable<RemoteOperation, Integer> startedOperations; //Operations which are running
@@ -98,7 +95,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation
        return operationsQueue.add(operation);
    }

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

@@ -228,4 +225,10 @@ public class SynchronizationService extends Service implements OnRemoteOperation
                    .threadWorkingState[data.getInt("thread index")] = data.getBoolean("mThreadWorkingState");
        }
    }

    public class SynchronizationBinder extends Binder{
        SynchronizationService getService(){
            return SynchronizationService.this;
        }
    }
}