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

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

Merge branch '299-step-5-SyncService-as-worker' into 'main'

Start to implement a worker to replace SyncrhonizationService

See merge request !259
parents 6871e555 dbfe0887
Loading
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -83,7 +83,6 @@
            android:exported="true"
            android:label="@string/account_setting_metered_network"
            tools:ignore="ExportedContentProvider" />
        <service android:name=".services.SynchronizationService" />
        <receiver
            android:name=".receivers.BootCompletedReceiver"
            android:enabled="true"
@@ -120,5 +119,9 @@
                <action android:name="foundation.e.drive.action.ADD_ACCOUNT"/>
            </intent-filter>
        </receiver>

        <service android:name="androidx.work.impl.foreground.SystemForegroundService"
            android:foregroundServiceType="dataSync"
            tools:node="merge" />
    </application>
</manifest>
 No newline at end of file
+5 −5
Original line number Diff line number Diff line
@@ -105,12 +105,12 @@ public class FileEventListener {
     * @param request SyncRequest that should be executed asap
     */
    private void sendSyncRequestToSynchronizationService(@NonNull SyncRequest request) {
        Timber.d("Sending a SyncRequest for %s", request.getSyncedFileState().getName());

        final SyncRequestCollector syncManager = SyncProxy.INSTANCE;
        syncManager.queueSyncRequest(request, appContext.getApplicationContext());
        final boolean requestAdded = syncManager.queueSyncRequest(request, appContext.getApplicationContext());
        if (requestAdded) {
            Timber.d("Sending a SyncRequest for %s", request.getSyncedFileState().getName());
            syncManager.startSynchronization(appContext);

        }
    }

    /**
+28 −14
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import static foundation.e.drive.utils.AppConstants.SETUP_COMPLETED;

import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -20,13 +21,15 @@ import android.content.SharedPreferences;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.work.WorkManager;

import java.io.File;

import foundation.e.drive.EdriveApplication;
import foundation.e.drive.R;
import foundation.e.drive.database.DbHelper;
import foundation.e.drive.database.FailedSyncPrefsManager;
import foundation.e.drive.services.SynchronizationService;
import foundation.e.drive.synchronization.SyncWorker;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.DavClientProvider;
import foundation.e.drive.utils.ViewUtils;
@@ -43,21 +46,27 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver {

        final SharedPreferences preferences = applicationContext.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);

        if (!shouldProceedWithRemoval(intent, preferences)) {
        if (!shouldProceedWithRemoval(intent, preferences, applicationContext)) {
            return;
        }

        cancelWorkers(applicationContext);
        stopRecursiveFileObserver(applicationContext);
        stopAllServices(applicationContext);
        deleteDatabase(applicationContext);
        cleanSharedPreferences(applicationContext, preferences);
        removeCachedFiles(applicationContext);

        deleteNotificationChannels(applicationContext);
        DavClientProvider.getInstance().cleanUp();

        ViewUtils.updateWidgetView(applicationContext);
    }


    private void cancelWorkers(@NonNull Context context) {
        final WorkManager workManager = WorkManager.getInstance(context);
        workManager.cancelAllWorkByTag(AppConstants.WORK_GENERIC_TAG);
    }

    private void deleteDatabase(@NonNull Context applicationContext) {
        final boolean result = applicationContext.deleteDatabase(DbHelper.DATABASE_NAME);
        Timber.d("Remove Database: %s", result);
@@ -69,16 +78,16 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver {
        }
    }

    private boolean shouldProceedWithRemoval(@NonNull Intent intent, @NonNull SharedPreferences preferences) {
    private boolean shouldProceedWithRemoval(@NonNull Intent intent, @NonNull SharedPreferences preferences, @NonNull Context context) {
        if (isInvalidAction(intent) || intent.getExtras() == null) {
            Timber.w("Invalid account removal request");
            return false;
        }

        String currentAccount = preferences.getString(AccountManager.KEY_ACCOUNT_NAME, "");
        String currentAccountType = preferences.getString(AccountManager.KEY_ACCOUNT_TYPE, "");
        String currentAccountType = context.getString(R.string.eelo_account_type);

        if (currentAccount.isEmpty() || currentAccountType.isEmpty()) {
        if (currentAccount.isEmpty()) {
            Timber.d("No account set up, ignoring account removal");
            return false;
        }
@@ -93,13 +102,6 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver {
        return !"android.accounts.action.ACCOUNT_REMOVED".equals(intent.getAction());
    }

    private void stopAllServices(@NonNull Context applicationContext) {

        Intent synchronizationServiceIntent = new Intent(applicationContext, SynchronizationService.class);
        boolean syncServiceStopResult = applicationContext.stopService(synchronizationServiceIntent);
        Timber.d("stop SynchronizationService: %s", syncServiceStopResult);
    }

    private void cleanSharedPreferences(@NonNull Context applicationContext, @NonNull SharedPreferences prefs) {
        if (!applicationContext.deleteSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME)) {
            //If removal failed, clear all data inside
@@ -144,4 +146,16 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver {

        return dir.delete();
    }

    private void deleteNotificationChannels(Context context) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.cancelAll();
        try {
            notificationManager.deleteNotificationChannel(AppConstants.notificationChannelID);
            notificationManager.deleteNotificationChannel(SyncWorker.NOTIF_CHANNEL_ID);
        } catch (Exception exception) {
            Timber.e(exception, "Cannot delete notification Channel");
        }
    }
}
+2 −12
Original line number Diff line number Diff line
@@ -15,8 +15,8 @@ import androidx.annotation.Nullable;

import com.owncloud.android.lib.common.operations.RemoteOperation;

import foundation.e.drive.operations.DownloadFileOperation;
import foundation.e.drive.operations.UploadFileOperation;
import foundation.e.drive.synchronization.tasks.DownloadFileOperation;
import foundation.e.drive.synchronization.tasks.UploadFileOperation;

/**
 * This class encapsulates data, for SynchronizationService, about a thread which run a RemoteOperation
@@ -26,7 +26,6 @@ import foundation.e.drive.operations.UploadFileOperation;
public class SyncWrapper {
    private final SyncRequest request;
    private final RemoteOperation remoteOperation;
    private boolean isRunning;

    /**
     * Build an instance of SyncThreadHolder for a file transfer
@@ -36,7 +35,6 @@ public class SyncWrapper {
    public SyncWrapper(@NonNull final SyncRequest request, @Nullable final Account account, @NonNull final Context context) {
        this.request = request;
        remoteOperation = createRemoteOperation(request, account, context);
        isRunning = true;
    }

    @Nullable
@@ -44,14 +42,6 @@ public class SyncWrapper {
        return remoteOperation;
    }

    public boolean isRunning() {
        return isRunning;
    }

    public synchronized void setRunning(boolean running) {
        isRunning = running;
    }

    /**
     * Create the RemoteOperation (to perform file transfer) based on SyncRequest
     * @param request syncRequest for the file
+2 −3
Original line number Diff line number Diff line
@@ -56,7 +56,6 @@ class FullScanWorker(private val context: Context, private val workerParams: Wor
            val startAllowed = checkStartConditions(account, prefs, requestCollector)
            if (!startAllowed) {
                Timber.d("Start Periodic Scan is not allowed")
                requestCollector.startListeningFiles(applicationContext as Application)
                return Result.failure()
            }

@@ -66,12 +65,12 @@ class FullScanWorker(private val context: Context, private val workerParams: Wor
                return Result.success()
            }

            val remoteSyncRequests = scanRemoteFiles(account, syncFolders.toMutableList())
            val remoteSyncRequests = scanRemoteFiles(account, syncFolders)
            syncRequests.putAll(remoteSyncRequests)

            Timber.d("${remoteSyncRequests.size} request collected from cloud")

            val localSyncRequests = scanLocalFiles(syncFolders.toMutableList())
            val localSyncRequests = scanLocalFiles(syncFolders)
            syncRequests.putAll(localSyncRequests)
            Timber.d("${localSyncRequests.size} request collected from device")

Loading