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

Commit 5105f600 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

Merge branch '7-1-workManager-dependency' into '7-issue-workmanagerAPI'

Add Jetpack's workManager API dependency

See merge request e/apps/eDrive!74
parents a22ba24b 37750f7d
Loading
Loading
Loading
Loading
Loading
+14 −19
Original line number Diff line number Diff line
@@ -23,7 +23,7 @@ def getTestProp(String propName) {


android {
    compileSdkVersion 28
    compileSdkVersion 31
    defaultConfig {
        applicationId "foundation.e.drive"
        minSdkVersion 26
@@ -53,32 +53,27 @@ android {
            //includeAndroidResources = true
        }
    }

}


dependencies {
    api project(':NextcloudLib')
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'androidx.annotation:annotation:1.3.0'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    api 'androidx.annotation:annotation:1.3.0'
    api project(':NextcloudLib')

    //start to add lib for test - 1/4/21
    //@TODO: add junit runner as lib for testImplementation
    def work_version = "2.7.1"
    // (Java only)
    implementation "androidx.work:work-runtime:$work_version"

    testImplementation 'com.android.support.test:runner:1.0.2'
    testImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation 'androidx.test:rules:1.4.0'
    androidTestImplementation 'androidx.annotation:annotation:1.3.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation  'junit:junit:4.12'

    testImplementation 'androidx.test:runner:1.4.0'
    testImplementation 'androidx.test:rules:1.4.0'
    testImplementation 'junit:junit:4.12'
    //testImplementation 'org.robolectric:robolectric:4.4' //need AndroidX
    testImplementation "org.robolectric:robolectric:3.8"
    testImplementation 'org.robolectric:robolectric:4.4'
    testImplementation('org.mockito:mockito-inline:3.4.0')

    //testImplementation Libs.AndroidX.Test.archCoreTesting //TODO: replace by not android X version
    //implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
    androidTestImplementation  'junit:junit:4.12'
}
+29 −31
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;
@@ -35,7 +36,7 @@ import foundation.e.drive.operations.CreateInitialFolderRemoteOperation;
import foundation.e.drive.receivers.ScreenOffReceiver;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;
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;
@@ -43,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;
@@ -62,7 +64,6 @@ public class InitializerService extends Service implements OnRemoteOperationList
        Log.i(TAG, "onCreate()");
        super.onCreate();
        this.existingRemoteFolderCounter = 0;
        //JobUtils.scheduleInitializerJob(getApplicationContext());
    }

    @Override
@@ -73,7 +74,9 @@ public class InitializerService extends Service implements OnRemoteOperationList
        //Get account
        SharedPreferences prefs = this.getSharedPreferences( AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE );


        if (prefs.getBoolean( AppConstants.INITIALIZATION_HAS_BEEN_DONE, false ) ) {

            Log.w(TAG, "Initializer has already been run");
        } else {
            String accountName = prefs.getString( AccountManager.KEY_ACCOUNT_NAME, "" );
@@ -84,7 +87,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();
@@ -92,7 +94,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) );
@@ -121,7 +122,6 @@ public class InitializerService extends Service implements OnRemoteOperationList
            return;
        }

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

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

        //Get SyncedFolders
        getInitialSyncedFolders(syncCategories);

        this.existingRemoteFolderCounter = 0;
@@ -211,7 +210,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();
        }

@@ -273,8 +271,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()
@@ -282,6 +279,8 @@ public class InitializerService extends Service implements OnRemoteOperationList
                .putInt( INITIALFOLDERS_NUMBER, mSyncedFolders.size() )
                .apply();

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

        //all folder have been created


@@ -296,7 +295,6 @@ public class InitializerService extends Service implements OnRemoteOperationList


        stopSelf();

    }

    @Override
+2 −0
Original line number Diff line number Diff line
@@ -26,6 +26,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
@@ -63,6 +64,7 @@ public class ResetService extends Service {
                    result = getApplicationContext().stopService( stopperIntent );
                    Log.d(TAG, "stop OperationManagerService: "+result);

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

                    //3. delete DB
                    result = this.deleteDatabase(DbHelper.DATABASE_NAME);
+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
Loading