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

Commit 683ea5bd authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

add IntentService to create initialFolder on cloud

parent d578d162
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -58,7 +58,8 @@ http://www.gnu.org/licenses/gpl.html
        <service android:name=".services.ObserverService"
            android:enabled="true"/>
        <service android:name=".services.OperationManagerService"/>

        <service android:name=".backgroundServices.CreateInitialRemoteFoldersIntentService"
            android:enabled="true"/>
        <!-- Receivers -->
        <receiver android:name=".receivers.BootCompleteReceiver"
            android:enabled="true">
+20 −8
Original line number Diff line number Diff line
package foundation.e.drive.backgroundServices;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
@@ -14,38 +16,46 @@ import java.io.File;

import foundation.e.drive.database.DbHelper;
import foundation.e.drive.models.SyncedFolder;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;

import static foundation.e.drive.utils.AppConstants.ACCOUNT_KEY_CODE;
import static foundation.e.drive.utils.AppConstants.resultCodeKey;
import static foundation.e.drive.utils.AppConstants.resultIntentActionKey;


//https://developer.android.com/training/run-background-service/report-status.html
//https://developer.android.com/guide/components/services.html#ExtendingIntentService
public class CreateInitialRemoteFoldersIntentService extends IntentService {
    private static final String TAG = CreateInitialRemoteFoldersIntentService.class.getSimpleName();
    public static final String createFullPathKey = "create_full_path";
    public static final String syncedFolderKey = "synced_folder";
    public static final String resultCodeKey = "result_code";
    public static final String resultIntentActionKey = "result";

    private OwnCloudClient client;

    /**
     * Creates an IntentService.
     *
     */
    public CreateInitialRemoteFoldersIntentService(OwnCloudClient client) {
    public CreateInitialRemoteFoldersIntentService() {
        super(TAG);
        client = client;
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {
        if(workIntent == null || workIntent.getExtras() == null) return;

        Bundle extras = workIntent.getExtras();
        boolean mCreateFullPath = extras.getBoolean(createFullPathKey);
        SyncedFolder mSyncedFolder = (SyncedFolder) extras.getParcelable(syncedFolderKey);
        Account mAccount = extras.getParcelable(ACCOUNT_KEY_CODE);
        OwnCloudClient client = CommonUtils.getOwnCloudClient(mAccount, this);

        if(mSyncedFolder == null) return; //@TODO: Move this check in performTask
        performTask(mSyncedFolder, mCreateFullPath);
        performTask(mSyncedFolder, mCreateFullPath, client);
    }


    private void performTask(SyncedFolder mSyncedFolder, boolean mCreateFullPath){
    private void performTask(SyncedFolder mSyncedFolder, boolean mCreateFullPath, OwnCloudClient client){

        Intent resultIntent = new Intent(resultIntentActionKey);

@@ -60,14 +70,16 @@ public class CreateInitialRemoteFoldersIntentService extends IntentService {
        createOperationResult = createFolderOperation.execute(client, true);
        if(createOperationResult.isSuccess() || createOperationResult.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS ){
            if(DbHelper.insertSyncedFolder(mSyncedFolder, this) >= 0 ) {

                resultIntent.putExtra(resultCodeKey, RemoteOperationResult.ResultCode.OK);
                resultIntent.putExtra(AppConstants.resultIntentHttpCodeKey, createOperationResult.getHttpCode());
            }else {
                Log.d(TAG, "insertion of folder in DB failed");
                resultIntent.putExtra(resultCodeKey, RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS);
                resultIntent.putExtra(AppConstants.resultIntentHttpCodeKey, createOperationResult.getHttpCode());
            }
        }else{
            resultIntent.putExtra(resultCodeKey, createOperationResult.getCode());
            resultIntent.putExtra(AppConstants.resultIntentHttpCodeKey, createOperationResult.getHttpCode());
        }

        Log.d(TAG, createOperationResult.getLogMessage()+" \n"+createOperationResult.getCode()+