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

Commit f1c78b3b authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

fix: stateMachine is not updating after account logout

After user logged out from the account, stateMachine doesn't move to
idle state, but the jobs are canceled. Which create some issue until
user reboot the device.

example, if the state IS `PERIODIC_SCAN`, & account logged-out,
FullScanWorker will be stopped, but state won't update. So if user
re-login, the FullScannWorker won't run again, until user reboot device.
parent 61ac15dd
Loading
Loading
Loading
Loading
Loading
+3 −8
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.Application;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -25,10 +26,10 @@ 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.synchronization.SyncProxy;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.DavClientProvider;
import foundation.e.drive.utils.ViewUtils;
@@ -50,7 +51,7 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver {
        }

        cancelWorkers(applicationContext);
        stopRecursiveFileObserver(applicationContext);
        SyncProxy.INSTANCE.moveToIdle((Application) applicationContext);
        deleteDatabase(applicationContext);
        cleanSharedPreferences(applicationContext, preferences);
        removeCachedFiles(applicationContext);
@@ -72,12 +73,6 @@ public class AccountRemoveCallbackReceiver extends BroadcastReceiver {
        Timber.d("Remove Database: %s", result);
    }

    private void stopRecursiveFileObserver(@NonNull Context applicationContext) {
        if (applicationContext instanceof EdriveApplication) {
            ((EdriveApplication) applicationContext).stopRecursiveFileObserver();
        }
    }

    private boolean shouldProceedWithRemoval(@NonNull Intent intent, @NonNull SharedPreferences preferences, @NonNull Context context) {
        if (isInvalidAction(intent) || intent.getExtras() == null) {
            Timber.w("Invalid account removal request");
+7 −2
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ enum class SyncState {
 * It can be synchronizing (transfering file)
 * It can be listening for file event
 * It can be Scanning cloud & device for missed files
 * It can be Idle (i.e: after boot, before restart)
 * It can be Idle (i.e: after boot, before restart, after logout)
 * @author Vincent Bourgmayer
 */
object StateMachine {
@@ -41,7 +41,7 @@ object StateMachine {
            SyncState.PERIODIC_SCAN -> setPeriodicScanState()
            SyncState.SYNCHRONIZING -> setSynchronizing()
            SyncState.LISTENING_FILES -> setListeningFilesState()
            SyncState.IDLE -> false
            SyncState.IDLE -> setIdleFilesState()
        }

        if (!isStateChanged) {
@@ -59,6 +59,11 @@ object StateMachine {
        return true
    }

    private fun setIdleFilesState(): Boolean {
        currentState = SyncState.IDLE
        return true
    }

    private fun setPeriodicScanState(): Boolean {
        if (currentState == SyncState.SYNCHRONIZING) {
            Timber.d("Cannot change state: files sync is running")
+20 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ interface SyncRequestCollector {
    fun startSynchronization(context: Context)
    fun onPeriodicScanStart(application: Application): Boolean
    fun startListeningFiles(application: Application)
    fun moveToIdle(application: Application)
}

/**
@@ -180,6 +181,25 @@ object SyncProxy: SyncRequestCollector, SyncManager {
        }
    }

    /**
     * called after account logged out
     * update the stateMachine's state & stop recursive fileObserver
     */
    override fun moveToIdle(application: Application) {
        if (application !is EdriveApplication) {
            Timber.d("Invalid parameter: : moveToIdle(application)")
            return
        }

        val isStateChanged = StateMachine.changeState(SyncState.IDLE)
        if (!isStateChanged) {
            Timber.d("failed to change state. moveToIdle")
            return
        }

        application.stopRecursiveFileObserver()
    }


    /**
     * Progressively delay synchronization of a file in case of failure