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

Commit d578d162 authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

add IntentService to replace CreateInitialFolderOperation

parent 2216c64d
Loading
Loading
Loading
Loading
Loading
+77 −0
Original line number Diff line number Diff line
package foundation.e.drive.backgroundServices;

import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation;

import java.io.File;

import foundation.e.drive.database.DbHelper;
import foundation.e.drive.models.SyncedFolder;

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) {
        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);

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


    private void performTask(SyncedFolder mSyncedFolder, boolean mCreateFullPath){

        Intent resultIntent = new Intent(resultIntentActionKey);

        File folder = new File(mSyncedFolder.getLocalFolder() );
        if( !folder.exists() ){
            Log.e(TAG, "Local folder doesn't exist, so create it");
            folder.mkdirs();
        }
        CreateFolderRemoteOperation createFolderOperation = new CreateFolderRemoteOperation(mSyncedFolder.getRemoteFolder(), mCreateFullPath);

        RemoteOperationResult createOperationResult;
        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);
            }else {
                Log.d(TAG, "insertion of folder in DB failed");
                resultIntent.putExtra(resultCodeKey, RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS);
            }
        }else{
            resultIntent.putExtra(resultCodeKey, createOperationResult.getCode());
        }

        Log.d(TAG, createOperationResult.getLogMessage()+" \n"+createOperationResult.getCode()+
                " \n"+createOperationResult.getHttpCode()+" \n"+createOperationResult.getAuthenticateHeaders().toString());
        LocalBroadcastManager.getInstance(this).sendBroadcast(resultIntent);
    }
}