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

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

Merge branch '7-4-implementWorkRequest' into '7-1-workManager-dependency'

Add workRequest to trigger ObserverService instead of ScheduledJob

See merge request e/apps/eDrive!77
parents e3cb7f01 840bf519
Loading
Loading
Loading
Loading
Loading
+27 −32
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -36,7 +37,6 @@ import foundation.e.drive.receivers.ScreenOffReceiver;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;
import foundation.e.drive.utils.JobUtils;
import foundation.e.drive.utils.ServiceExceptionHandler;

import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR;
import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER;
@@ -44,15 +44,16 @@ import static foundation.e.drive.utils.AppConstants.MEDIA_SYNCABLE_CATEGORIES;
import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES;

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

/**
 * @author Vincent Bourgmayer
 */
public class InitializerService extends Service implements OnRemoteOperationListener {
    final private String TAG = InitializerService.class.getSimpleName();
    //Complex properties
    private int existingRemoteFolderCounter; //@dev-only; Temporarily used to know if all remotePath exist
    private List<SyncedFolder> mSyncedFolders; //syncedFolders
    private final String TAG = InitializerService.class.getSimpleName();

    private int existingRemoteFolderCounter; //Temporarily used to know if all remotePath exist
    private List<SyncedFolder> mSyncedFolders;
    private OwnCloudClient mCloudClient;
    private Handler mHandler;
    private Account mAccount;
@@ -63,7 +64,6 @@ public class InitializerService extends Service implements OnRemoteOperationList
        Log.i(TAG, "onCreate()");
        super.onCreate();
        this.existingRemoteFolderCounter = 0;
        //JobUtils.scheduleInitializerJob(getApplicationContext());
    }

    @Override
@@ -86,7 +86,6 @@ public class InitializerService extends Service implements OnRemoteOperationList
                accountName = intent.getExtras().getString( AccountManager.KEY_ACCOUNT_NAME, "" );
                accountType = intent.getExtras().getString( AccountManager.KEY_ACCOUNT_TYPE, "" );

                //If data come from intent, store them into pref because there aren't stored
                prefs.edit().putString( AccountManager.KEY_ACCOUNT_NAME, accountName )
                        .putString( AccountManager.KEY_ACCOUNT_TYPE, accountType )
                        .apply();
@@ -94,7 +93,6 @@ public class InitializerService extends Service implements OnRemoteOperationList

            if (accountName.isEmpty() ) {
                Log.w(TAG, "Account's name not found. Neither in shared prefs nor in intent's extras");
                //JobUtils.stopScheduledJob(getApplicationContext(), JobUtils.InitializerJobId);
                stopSelf();
            } else {
                this.mAccount = CommonUtils.getAccount( accountName, accountType, AccountManager.get(this) );
@@ -123,7 +121,6 @@ public class InitializerService extends Service implements OnRemoteOperationList
            return;
        }

        //Get categories of element to sync
        List<String> syncCategories = new ArrayList<>();

        if (CommonUtils.isMediaSyncEnabled(mAccount)) {
@@ -134,7 +131,6 @@ public class InitializerService extends Service implements OnRemoteOperationList
            syncCategories.addAll(Arrays.asList(SETTINGS_SYNCABLE_CATEGORIES));
        }

        //Get SyncedFolders
        getInitialSyncedFolders(syncCategories);

        this.existingRemoteFolderCounter = 0;
@@ -213,7 +209,6 @@ public class InitializerService extends Service implements OnRemoteOperationList
        this.restartFolderCreationCounter = 0;

        if (this.mSyncedFolders == null || this.mSyncedFolders.isEmpty() ){
            //JobUtils.stopScheduledJob(getApplicationContext(), JobUtils.InitializerJobId);
            this.stopSelf();
        }

@@ -275,8 +270,7 @@ public class InitializerService extends Service implements OnRemoteOperationList
     **/
    private void doLastStep(){
        Log.i(TAG, "doLastStep()");

        Context appContext = getApplicationContext();
        final Context appContext = getApplicationContext();
        appContext.getSharedPreferences( AppConstants.SHARED_PREFERENCE_NAME,
                Context.MODE_PRIVATE )
                .edit()
@@ -284,9 +278,11 @@ public class InitializerService extends Service implements OnRemoteOperationList
                .putInt( INITIALFOLDERS_NUMBER, mSyncedFolders.size() )
                .apply();

        CommonUtils.registerPeriodicFullScanWorker(WorkManager.getInstance(appContext));

        //all folder have been created
        //JobUtils.stopScheduledJob(appContext, JobUtils.InitializerJobId);
        JobUtils.scheduleScannerJob(appContext);
        //JobUtils.scheduleScannerJob(appContext);

        Log.d(TAG, "RegisterReceiver: screenOffReceiver");
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
@@ -299,7 +295,6 @@ public class InitializerService extends Service implements OnRemoteOperationList


        stopSelf();

    }

    @Override
+4 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER;
import static foundation.e.drive.utils.AppConstants.INITIALIZATION_HAS_BEEN_DONE;

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

/**
 * @author Vincent Bourgmayer
@@ -67,6 +68,9 @@ public class ResetService extends Service {
                    //2. Stop the scheduledJob
                    JobUtils.stopScheduledJob(this, JobUtils.ScannerJobId);
                    //JobUtils.stopScheduledJob(this, JobUtils.InitializerJobId);

                    WorkManager.getInstance(this).cancelAllWorkByTag(AppConstants.WORK_GENERIC_TAG);

                    //3. delete DB
                    result = this.deleteDatabase(DbHelper.DATABASE_NAME);
                    Log.d(TAG, "Remove Database: "+result);
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ public abstract class AppConstants {

    public final static String notificationChannelID ="3310";
    public final static String notificationChannelName="eDrive channel";

    public final static String WORK_GENERIC_TAG="eDrive";

    public final static String USER_AGENT = "eos("+getBuildTime()+")-eDrive("+ BuildConfig.VERSION_NAME +")";

+30 −3
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ import android.content.ContentResolver;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;
@@ -31,13 +30,20 @@ import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.resources.files.FileUtils;

import java.io.File;
import java.util.concurrent.TimeUnit;

import foundation.e.drive.receivers.ScreenOffReceiver;
import foundation.e.drive.work.FullScanWorker;

import static foundation.e.drive.utils.AppConstants.MEDIASYNC_PROVIDER_AUTHORITY;
import static foundation.e.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORITY;

import androidx.annotation.NonNull;
import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;


/**
@@ -45,7 +51,7 @@ import androidx.annotation.NonNull;
 */

public abstract class CommonUtils {
    final private static String TAG = CommonUtils.class.getSimpleName();
    private final static String TAG = CommonUtils.class.getSimpleName();

    /**
     * Set ServiceUncaughtExceptionHandler to be the MainThread Exception Handler
@@ -293,4 +299,25 @@ public abstract class CommonUtils {
                + "\n File can be read?: " + f.canRead()
                + "\n File can be written?: " + f.canWrite();
    }

    /**
     * Enqueue a unique periodic worker to look for file to be synchronized (remote files + local files
     * @param workManager the instance of workManager
     */
    public static void registerPeriodicFullScanWorker(WorkManager workManager){
        final Constraints constraints = new Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .setRequiresBatteryNotLow(true)
                .build();

        final PeriodicWorkRequest periodicFullScanRequest =
                new PeriodicWorkRequest.Builder(FullScanWorker.class,
                        31, TimeUnit.MINUTES,
                        5, TimeUnit.MINUTES)
                        .setConstraints(constraints)
                        .addTag(AppConstants.WORK_GENERIC_TAG)
                        .build();

        workManager.enqueueUniquePeriodicWork(FullScanWorker.UNIQUE_WORK_NAME , ExistingPeriodicWorkPolicy.KEEP, periodicFullScanRequest);
    }
}
 No newline at end of file
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright © Vincent Bourgmayer (/e/ foundation).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

package foundation.e.drive.work;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import foundation.e.drive.services.ObserverService;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;

/**
 * As a first step, this class must replace foundation.e.drive.jobs.ScannerJob
 * in order to allow to use Jetpack Work API
 *
 * In further development it will be a part of Workers that will replace ObserverService
 * I will update this header accordingly
 * 
 * @author Vincent Bourgmayer
 */
public class FullScanWorker extends Worker {
    private final static String TAG = FullScanWorker.class.getSimpleName();
    public final static String UNIQUE_WORK_NAME="FullScan";

    public FullScanWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.d(TAG, "doWork(): going to send intent to ObserverService");
        final SharedPreferences prefs = getApplicationContext().getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME,
                Context.MODE_PRIVATE );
        final String accountName = prefs.getString( AccountManager.KEY_ACCOUNT_NAME, "" );
        final String accountType = prefs.getString( AccountManager.KEY_ACCOUNT_TYPE, "" );

        final Account mAccount = CommonUtils.getAccount( accountName, accountType, AccountManager.get(this.getApplicationContext()) );

        if (mAccount != null && CommonUtils.isSettingsSyncEnabled(mAccount) && CommonUtils.isMediaSyncEnabled(mAccount)) {
            final Intent observerServiceIntent = new Intent(this.getApplicationContext(), ObserverService.class);
            this.getApplicationContext().startService(observerServiceIntent);
        } else {
            Log.w(TAG, "Intent for ObserverService not send : account is null or  \"settings sync\" & \"media sync\" settings are disabled");
        }

        return Result.success();
    }
}
 No newline at end of file