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

Commit 942996f1 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer Committed by Aayush Gupta
Browse files

E1 clean initializer service

parent 1b09c5c5
Loading
Loading
Loading
Loading
+66 −83
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ 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 +35,6 @@ import foundation.e.drive.operations.CreateInitialFolderRemoteOperation;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;


import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR;
import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER;
import static foundation.e.drive.utils.AppConstants.MEDIA_SYNCABLE_CATEGORIES;
@@ -52,10 +50,10 @@ public class InitializerService extends Service implements OnRemoteOperationList
    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;
    private List<SyncedFolder> syncedFolders;
    private OwnCloudClient cloudClient;
    private Handler handler;
    private Account account;
    private int restartFolderCreationCounter =0;

    @Override
@@ -73,9 +71,7 @@ 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, "");
@@ -95,10 +91,10 @@ public class InitializerService extends Service implements OnRemoteOperationList
                Log.w(TAG, "Account's name not found. Neither in shared prefs nor in intent's extras");
                stopSelf();
            } else {
                this.mAccount = CommonUtils.getAccount( accountName, accountType, AccountManager.get(this) );
                this.account = CommonUtils.getAccount(accountName, accountType, AccountManager.get(this));
                //Get OwnCloudlient
                if (this.mAccount != null) {
                    this.mCloudClient = CommonUtils.getOwnCloudClient( this.mAccount, getApplicationContext());
                if (this.account != null) {
                    this.cloudClient = CommonUtils.getOwnCloudClient(this.account, getApplicationContext());
                    start();
                } else {
                    Log.w(TAG, "Got account is invalid.");
@@ -109,25 +105,20 @@ public class InitializerService extends Service implements OnRemoteOperationList
        return super.onStartCommand(intent, flags, startId);
    }


    /**
     * start to do its job
     */

    public void start() {
        Log.i(TAG, "start()");
        if (mCloudClient == null){
        if (cloudClient == null) {
            stopSelf();
            return;
        }

        List<String> syncCategories = new ArrayList<>();

        if (CommonUtils.isMediaSyncEnabled(mAccount)) {
        if (CommonUtils.isMediaSyncEnabled(account)) {
            syncCategories.addAll(Arrays.asList(MEDIA_SYNCABLE_CATEGORIES));
        }

        if (CommonUtils.isSettingsSyncEnabled(mAccount)) {
        if (CommonUtils.isSettingsSyncEnabled(account)) {
            syncCategories.addAll(Arrays.asList(SETTINGS_SYNCABLE_CATEGORIES));
        }

@@ -145,7 +136,7 @@ public class InitializerService extends Service implements OnRemoteOperationList
    private void getInitialSyncedFolders(List<String> categories) {
        Log.i(TAG, "getInitialSyncedFolders");

        this.mSyncedFolders = new ArrayList<>();
        this.syncedFolders = new ArrayList<>();

        for(int i=-1, size = categories.size(); ++i < size;) {
            final String DEVICE_SPECIFIC_PATH = PATH_SEPARATOR+"Devices"+PATH_SEPARATOR+ Build.BRAND+"_"+ Build.MODEL+"_"
@@ -154,36 +145,36 @@ public class InitializerService extends Service implements OnRemoteOperationList
                case "Medias":
                    break;
                case "Images":
                    mSyncedFolders.add( new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_DCIM),
                    syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_DCIM),
                            "/Photos/", true));
                    mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_PICTURES),
                    syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_PICTURES),
                            "/Pictures/", true));
                    break;
                case "Movies":
                    mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_MOVIES),
                    syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_MOVIES),
                            "/Movies/", true));
                    break;
                case "Music":
                    mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_MUSIC),
                    syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_MUSIC),
                            "/Music/", true));
                    break;
                case "Ringtones":
                    mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_RINGTONES),
                    syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_RINGTONES),
                            "/Ringtones/", true));
                    break;
                case "Documents":
                    mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_DOCUMENTS),
                    syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_DOCUMENTS),
                            "/Documents/", true));
                    break;
                case "Podcasts":
                    mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_PODCASTS),
                    syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_PODCASTS),
                            "/Podcasts/", true));
                    break;
                case "Rom settings":
                    String remoteFolderPath = DEVICE_SPECIFIC_PATH+"/rom_settings/";
                    mSyncedFolders.add( new SyncedFolder(categories.get(i), "/data/system/users/0/", remoteFolderPath, true, false, true, false) );
                    syncedFolders.add(new SyncedFolder(categories.get(i), "/data/system/users/0/", remoteFolderPath, true, false, true, false));
                    try{
                        mSyncedFolders.add( new SyncedFolder(
                        syncedFolders.add(new SyncedFolder(
                                categories.get(i),
                        getFilesDir().getCanonicalPath()+PATH_SEPARATOR,
                        remoteFolderPath+"app_list/",
@@ -201,36 +192,30 @@ public class InitializerService extends Service implements OnRemoteOperationList
        return CommonUtils.getLocalPath(Environment.getExternalStoragePublicDirectory(directory))+ PATH_SEPARATOR;
    }

    /**
     * Start to createSyncedFolder in the cloud
     */
    private void CreateNextRemoteFolder() {
        Log.i(TAG, "createNextRemoteFolder()");
        this.restartFolderCreationCounter = 0;

        if (this.mSyncedFolders == null || this.mSyncedFolders.isEmpty() ){
        if (this.syncedFolders == null || this.syncedFolders.isEmpty()) {
            this.stopSelf();
        }

        //It means that there are still folders to create
        if (this.existingRemoteFolderCounter < this.mSyncedFolders.size() ){

            if (this.mHandler == null ) this.mHandler = new Handler();
        if (this.existingRemoteFolderCounter < this.syncedFolders.size()) {
            if (this.handler == null) this.handler = new Handler();

            CreateInitialFolderRemoteOperation createFolderOperation =
                    new CreateInitialFolderRemoteOperation(
                        this.mSyncedFolders.get( this.existingRemoteFolderCounter ),
                        this.syncedFolders.get(this.existingRemoteFolderCounter),
                            true,
                            this);

            createFolderOperation.execute(this.mCloudClient, this, this.mHandler);

        } else if (this.existingRemoteFolderCounter ==  this.mSyncedFolders.size() ){
            createFolderOperation.execute(this.cloudClient, this, this.handler);

        } else if (this.existingRemoteFolderCounter ==  this.syncedFolders.size()) {
            doLastStep();

        } else {
            Log.e(TAG, "this.existingRemoteFolderCounter : "+this.existingRemoteFolderCounter+" > this.mSyncedFolders.size() : "+this.mSyncedFolders.size() );
            Log.e(TAG, "this.existingRemoteFolderCounter : "+this.existingRemoteFolderCounter+" > this.mSyncedFolders.size() : "+this.syncedFolders.size());
            this.stopSelf();
        }
    }
@@ -249,14 +234,12 @@ public class InitializerService extends Service implements OnRemoteOperationList

                if (this.restartFolderCreationCounter < 3) {
                    Log.w(TAG, " restart operation");
                    operation.execute( this.mCloudClient, this, this.mHandler );
                    operation.execute(this.cloudClient, this, this.handler);
                    this.restartFolderCreationCounter+=1;

                } else {
                    Log.e(TAG, "Remote folder's creation failed due to conflict with server");
                    stopSelf();
                }

            } else {
                Log.e(TAG, result.getLogMessage()+" "+result.getHttpCode());
                stopSelf();
@@ -275,7 +258,7 @@ public class InitializerService extends Service implements OnRemoteOperationList
                Context.MODE_PRIVATE)
                .edit()
                .putBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, true)
                .putInt( INITIALFOLDERS_NUMBER, mSyncedFolders.size() )
                .putInt(INITIALFOLDERS_NUMBER, syncedFolders.size())
                .apply();

        CommonUtils.registerPeriodicFullScanWorker(WorkManager.getInstance(appContext));
@@ -296,11 +279,11 @@ public class InitializerService extends Service implements OnRemoteOperationList
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mHandler = null;
        this.mAccount = null;
        this.mCloudClient = null;
        if(this.mSyncedFolders != null) this.mSyncedFolders.clear();
        this.mSyncedFolders = null;
        this.handler = null;
        this.account = null;
        this.cloudClient = null;
        if (this.syncedFolders != null) this.syncedFolders.clear();
        this.syncedFolders = null;
    }

    @Nullable