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

Commit 7a48475a authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

replace CommonUtils.buildPath by a method in ObserverService to get data from...

replace CommonUtils.buildPath by a method in ObserverService to get data from RootFolder. Remote subfolder synchronisation is done
parent 4147628a
Loading
Loading
Loading
Loading
+43 −33
Original line number Diff line number Diff line
@@ -57,6 +57,29 @@ public final class DbHelper extends SQLiteOpenHelper {
        this.onCreate(db);
    }

    private static SyncedFileStateDAO openSyncedFileStateDAO(Context context, boolean writeMod){
        SyncedFileStateDAO dao = new SyncedFileStateDAO(context);
        try{
            dao.open(writeMod);
        }catch (Exception e){
            Log.e(TAG, e.toString());
            return null;
        }
        return dao;
    }

    private static RootFolderDAO openRootFolderDAO(Context context, boolean writeMod){
        RootFolderDAO dao = new RootFolderDAO(context);
        try{
            dao.open(writeMod);
        }catch(Exception e){
            Log.e(TAG, e.toString() );
            return null;
        }
        return dao;
    }



    /**
     * Insert, update or delete a new SyncedFileState in DB.
@@ -69,11 +92,9 @@ public final class DbHelper extends SQLiteOpenHelper {
        Log.i(TAG, "manageSyncedFileStateDB()");

        int result = -1;
        SyncedFileStateDAO dao = new SyncedFileStateDAO(context);
        try{
            dao.open(true);
        }catch (Exception e){
            Log.e(TAG, e.toString());
        SyncedFileStateDAO dao = openSyncedFileStateDAO(context, true);

        if(dao == null){
            return result;
        }
        if(action == "UPDATE") {
@@ -102,11 +123,9 @@ public final class DbHelper extends SQLiteOpenHelper {
     * @return instance of SyncedFileState or null if it can't open DB
     */
    public static SyncedFileState loadSyncedFile(Context context, String Path, boolean useLocalPath) {
        SyncedFileStateDAO dao = new SyncedFileStateDAO( context );
        try{
            dao.open( false );
        }catch ( Exception e ){
            Log.e( TAG, e.toString() );
        SyncedFileStateDAO dao = openSyncedFileStateDAO(context, false);

        if(dao == null){
            return null;
        }
        SyncedFileState syncedFileState = dao.getByPath(Path, useLocalPath);
@@ -122,14 +141,11 @@ public final class DbHelper extends SQLiteOpenHelper {
     * @return null if DB opening failed, either return thelist of SyncedFileState
     */
    public static List<SyncedFileState> getSyncedFileStatesByFolders(Context context, List<Long> ids) {//Connect to DB
        SyncedFileStateDAO dao = new SyncedFileStateDAO( context );
        try{
            dao.open( false );
        }catch ( Exception e ){
            Log.e( TAG, e.toString() );
        SyncedFileStateDAO dao = openSyncedFileStateDAO(context, false);

        if(dao == null){
            return null;
        }

        List<SyncedFileState> result = dao.getFilteredByRootFolderID(ids);

        dao.close();
@@ -142,13 +158,12 @@ public final class DbHelper extends SQLiteOpenHelper {
     * @param context
     * @return number of affected row. Return -1 if failed
     */
    public static int updateRootFolders(List<RootFolder> rootFolders, Context context){ int result = -1;
    public static int updateRootFolders(List<RootFolder> rootFolders, Context context){

        int result = -1;
        //Connect to DB
        RootFolderDAO dao = new RootFolderDAO(context);
        try{
            dao.open(true);
        }catch(Exception e){
            Log.e(TAG, e.toString() );
        RootFolderDAO dao = openRootFolderDAO(context, true);
        if(dao == null){
            return result;
        }

@@ -171,12 +186,9 @@ public final class DbHelper extends SQLiteOpenHelper {
     * @return List<RootFolder> a list of RootFolder from DB
     */
    public static List<RootFolder> getRootFolders(Context context, String filter, boolean filter_val) {
        RootFolderDAO dao = new RootFolderDAO(context );
        RootFolderDAO dao = openRootFolderDAO(context, false);
        List<RootFolder> mRootFolder = new ArrayList<RootFolder>();
        try{
            dao.open( false );
        }catch ( Exception e ){
            Log.e( TAG, e.toString() );
        if(dao == null){
            return mRootFolder;
        }

@@ -199,14 +211,12 @@ public final class DbHelper extends SQLiteOpenHelper {
     * @return the id of the root folder or -1 if it hasn't been inserted
     */
    public static long insertRootFolder(RootFolder mRootFolder, Context context){
        RootFolderDAO rootFolderDAO = new RootFolderDAO(context);
        try {
            rootFolderDAO.open(true);
        }catch(SQLException e){
        RootFolderDAO dao = openRootFolderDAO(context, true);
        if(dao == null){
            return -1;
        }
        long id = rootFolderDAO.insert(mRootFolder);
        rootFolderDAO.close();
        long id = dao.insert(mRootFolder);
        dao.close();
        return id;
    }
}
+3 −2
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ public class DownloadFileOperation extends RemoteOperation {
     * @param targetPath Must not be null or empty
     * @param context
     */
    public DownloadFileOperation(RemoteFile remoteFile, String targetPath, Context context){
    public DownloadFileOperation(RemoteFile remoteFile, String targetPath, Long rootFolderId, Context context){
        this.mRFile = remoteFile;
        this.mTargetPath = targetPath;
        this.mContext = context;
@@ -53,7 +53,8 @@ public class DownloadFileOperation extends RemoteOperation {
                    .setLocalPath( this.mTargetPath)
                    .setLastETAG( this.mRFile.getEtag() )
                    .setName( CommonUtils.getFileNameFromPath( remotePath ) )
                    .setLocalLastModified( 0L );
                    .setLocalLastModified( 0L )
                    .setRootFolderId( rootFolderId );
        }
        this.previousEtag = mSyncedState.getLastETAG();
    }
+1 −2
Original line number Diff line number Diff line
@@ -88,7 +88,6 @@ public class ListRemoteFileOperation extends RemoteOperation {
                                mRemoteFiles.add(remoteFile);
                            }
                        }
                        //mRemoteFiles.addAll( result.getData().subList( 1, dataSize ) );
                        rootFolder.setLastEtag(directory.getEtag() );
                        rootFolder.setToSync(true);
                        atLeastOneDirAsChanged = true;
@@ -99,7 +98,7 @@ public class ListRemoteFileOperation extends RemoteOperation {
                    }

                } else {
                    Log.e(TAG, "http " + result.getHttpCode() + " : " + result.getLogMessage());
                    Log.e(TAG, "http " + result.getHttpCode() + " : " + result.getLogMessage()+" => Ignored");
                    //return result; //@dev-only: Should I replace it by a new instance of RemoteOperationResult?
                }
            }
+11 −11
Original line number Diff line number Diff line
@@ -37,12 +37,12 @@ public class UploadFileOperation extends RemoteOperation {
    private long previousLastModified = 0L;

    /**
     * Constructor of uploadFileOperation when no there is no syncedFileState provide
     * Constructor of uploadFileOperation when there is no syncedFileState provide
     * @param file The local file to synchronize
     * @param targetPath target path to upload filte. must not be null.
     * @param context executing context
     */
    public UploadFileOperation(File file, String targetPath, Context context){
    public UploadFileOperation(File file, String targetPath, Long rootFolderId, Context context){
        this.mFile = file;
        this.mContext = context;
        this.mTargetPath = targetPath;
@@ -56,12 +56,13 @@ public class UploadFileOperation extends RemoteOperation {
            Log.d( TAG, "There is no stored SyncedFileState for: "+mFile.getName()
                    +"\nso it's first synchronisation of this file." );
            this.mSyncedState = new SyncedFileState();
            this.mSyncedState.setId( -1 ); //allow to know that this file isn't in DB.
            this.mSyncedState.setRemotePath(mTargetPath);
            this.mSyncedState.setLocalPath( CommonUtils.getLocalPath(mFile) );
            this.mSyncedState.setLastETAG("");
            this.mSyncedState.setName( mFile.getName() );
            this.mSyncedState.setLocalLastModified( mFile.lastModified() );
            this.mSyncedState.setId( -1 )
                    .setRemotePath(mTargetPath)
                    .setLocalPath( CommonUtils.getLocalPath(mFile) )
                    .setLastETAG("")
                    .setName( mFile.getName() )
                    .setLocalLastModified( mFile.lastModified() )
                    .setRootFolderId( rootFolderId );
        }
        this.previousLastModified = mSyncedState.getLocalLastModified();
    }
@@ -69,15 +70,14 @@ public class UploadFileOperation extends RemoteOperation {
    /**
     * Construct an upload operation with an already known syncedFileState
     * @param file File to upload
     * @param targetPath target path of upload. Can be null if already stored in syncedFileState.
     * @param context context in which to upload
     * @param syncedFileState syncedFileState corresponding to file.
     */
    public UploadFileOperation (File file, String targetPath, Context context, SyncedFileState syncedFileState){
    public UploadFileOperation (File file, Context context, SyncedFileState syncedFileState){
        this.mFile = file;
        this.mContext = context;
        this.mSyncedState = syncedFileState;
        this.mTargetPath = ( targetPath == null ) ? syncedFileState.getRemotePath() : targetPath;
        this.mTargetPath = syncedFileState.getRemotePath();
        this.previousLastModified = mSyncedState.getLocalLastModified();

    }
+60 −27
Original line number Diff line number Diff line
@@ -6,12 +6,11 @@ import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.media.MediaScannerConnection;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.ArraySet;
import android.util.Log;
import android.util.Pair;

import com.crashlytics.android.Crashlytics;
import com.owncloud.android.lib.common.OwnCloudClient;
@@ -25,10 +24,8 @@ import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;

import io.eelo.drive.database.DbHelper;
import io.eelo.drive.fileFilters.FileFilterFactory;
@@ -57,7 +54,6 @@ public class ObserverService extends Service implements OnRemoteOperationListene
    private boolean isWorking = false;
    private OperationManagerService mOperationManagerService;


    /**
     * ServiceConnection for binder to OperationManagerService
     */
@@ -180,7 +176,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
        for(int i = 0; i < datasSize; ++i){
            RootFolder rootFolder = (RootFolder) datas.get(i);
            if(rootFolder.isToSync() ){
                result.add( new Long( rootFolder.getId() ) );
                result.add( Long.valueOf( rootFolder.getId() ) );
            }
        }
        return result;
@@ -254,7 +250,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
            RemoteFile remoteFile = remoteFiles.get(i);
            Log.d( CURRENTTAG, "remoteFile.getRemotePath() : "+remoteFile.getRemotePath() );
            Log.v( CURRENTTAG, "start to loop through syncedFileListIterator");
            while( syncedFileListIterator.hasNext() && correspondant_found == false ){
            while( syncedFileListIterator.hasNext() && !correspondant_found  ){
                SyncedFileState syncedFileState = syncedFileListIterator.next();

                Log.d(CURRENTTAG, "syncedFileState.getRemotePath() :"+syncedFileState.getRemotePath() );
@@ -295,17 +291,20 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                }
            }
            //If we get here, it is that no syncedFile exist for this remote file
            if( correspondant_found == false ){
            if( !correspondant_found ){
                Log.v(CURRENTTAG, "SyncedFileState corresponding to remoteFile not found.");

                if(mBoundToOperationManager) {

                    String localPath = CommonUtils.buildPath( remoteFile.getRemotePath(), false, mRootFolders);
                    localPath += CommonUtils.getFileNameFromPath(remoteFile.getRemotePath());
                    Pair<String, Long> rootFolderData = getDataFromRootFolder( remoteFile.getRemotePath(), false);
                    if(rootFolderData == null){
                        Log.e(CURRENTTAG, "Can't get rootFolderDAta. Ignore this file "); //@dev-only: until better solution has been found
                    }else {
                        String targetPath = rootFolderData.first + CommonUtils.getFileNameFromPath(remoteFile.getRemotePath());

                    DownloadFileOperation downloadOperation = new DownloadFileOperation(remoteFile, localPath, this);
                        DownloadFileOperation downloadOperation = new DownloadFileOperation(remoteFile, targetPath, rootFolderData.second, this);
                        this.mOperationManagerService.addOperation(downloadOperation);

                    }
                }else{
                    Log.e(CURRENTTAG, "Connexion to OperationManagerService has been lost");
                }
@@ -315,7 +314,6 @@ public class ObserverService extends Service implements OnRemoteOperationListene
        // In most cases, we consider those files as remotly removed files. So we start to delete those local file.
        Log.v( TAG, "Start to handle remotly missing file" );
        handleRemoteRemainingSyncedFileState( syncedFileStates );

        startScan(false);
    }

@@ -324,14 +322,13 @@ public class ObserverService extends Service implements OnRemoteOperationListene
     * @param syncedFileStates
     * @return number of file which has been deleted
     */
    private int handleRemoteRemainingSyncedFileState(List<SyncedFileState> syncedFileStates){
    private void handleRemoteRemainingSyncedFileState(List<SyncedFileState> syncedFileStates){
        final String CURRENTTAG = TAG+"_handleRemoteRemainingSyncedFileState(...)";

        Log.i( TAG, "handleRemoteRemainingSyncedFileState()" );
        int remainingSyncedFileStateSize = syncedFileStates.size();
        Log.d( CURRENTTAG, "remaining syncedFIleStates size: "+remainingSyncedFileStateSize );

        int deletedCounter = 0;
        Log.v(CURRENTTAG, "start to loop over syncedFileStates");
        for(int i = 0; i < remainingSyncedFileStateSize; ++i ){
            SyncedFileState syncedFileState = syncedFileStates.get(i);
@@ -355,7 +352,6 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                    }
                }
                if( fileDeleted ) {
                    deletedCounter += 1;
                    //It means that file has been correctly deleted from device. So update DB.
                    int result = DbHelper.manageSyncedFileStateDB(syncedFileState, "DELETE", this);
                    if (result > 0)
@@ -366,12 +362,11 @@ public class ObserverService extends Service implements OnRemoteOperationListene
            }
            //File is currently being transfered. Do not remove it.
        }
        return deletedCounter;
    }

    /** end of methods related to server scanning */
    /* end of methods related to server scanning */

    /** Methods related to device Scanning **/
    /* Methods related to device Scanning */
    /**
     * manage rest of the list of syncedFilesState to look for remote file to delete
     * @param syncedFileStates
@@ -450,7 +445,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                //update roots instance
                rootFolder.setToSync( true );
                rootFolder.setLastModified( f.lastModified() );
                folderIds.add( new Long( rootFolder.getId() ) );
                folderIds.add( Long.valueOf( rootFolder.getId() ) );
                //Get list of subFiles
                if( f.exists() ) {
                    FileFilterFactory FileFilterFactoryInstance = FileFilterFactory.getInstance();
@@ -537,7 +532,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                        //if File has been modified since last time and or if no lastEtag is stored meaning first sync has failed
                        if( sf.getLocalLastModified() < f.lastModified() || !sf.isLastEtagStored()){
                            //add Upload operation to queue.
                            UploadFileOperation uploadFileOperation = new UploadFileOperation( f, null, this, sf );
                            UploadFileOperation uploadFileOperation = new UploadFileOperation( f, this, sf );
                            this.mOperationManagerService.addOperation( uploadFileOperation );
                        }

@@ -552,17 +547,55 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                }
            }
            if( ! correspondant_found ){
                String remotePath = CommonUtils.buildPath( filePath,true, mRootFolders );

                UploadFileOperation uploadOperation = new UploadFileOperation( f, remotePath+f.getName(), this );
                Pair<String, Long> rootFolderData = getDataFromRootFolder( filePath,true);
                if(rootFolderData == null){
                    Log.e(TAG, "Can't get root Folder, so ignre this file. "); //@dev-only: temporary solution
                }else {
                    UploadFileOperation uploadOperation = new UploadFileOperation(f, rootFolderData.first + f.getName(), rootFolderData.second, this);
                    this.mOperationManagerService.addOperation(uploadOperation);
                }
            }
        }
        //Handle remaining file
        handleLocalRemainingSyncedFileState( syncedFileStates );
        this.isWorking = false;
    }

    /** end of methods related to device Scanning **/
    /* end of methods related to device Scanning */


    /**
     * Use localPath to get Remote equivalent,
     * or use remotePath to get LocalPath equivalent
     * Be carefull that this methods doesn't give file name at the end of returned path
     *
     * @param knownPath     already known path
     * @param isRemoteAsked If you want to get remote path: true; else use false
     * @return Pair<String, Long> Missing path (if you provide RemotePath you'll get LocalPath and vice versa) and RootFolder ID.
     * Return null if no RootFolder has been found.
     */
    private Pair<String, Long> getDataFromRootFolder(String knownPath, boolean isRemoteAsked) {
        final String CURRENTTAG = TAG+"_getDataFromRootFolder(...)";
        Log.i(CURRENTTAG, "input: - knownpath " + knownPath + ",\n isRemoteAsked : " + isRemoteAsked );
        //Extract parent path from knownPath
        String knownFolderPath = knownPath.substring(0, knownPath.lastIndexOf(FileUtils.PATH_SEPARATOR) + 1);
        int rootSize = this.mRootFolders.size();
        Log.d(CURRENTTAG, "RootFolders.Size(): "+rootSize );
        Pair<String, Long> result;
        //look into rootsFolder is folder path exist
        for (int i = 0; i < rootSize; ++i) {
            RootFolder rootFolder = mRootFolders.get(i);
            if (( (isRemoteAsked) ? rootFolder.getLocalFolder() : rootFolder.getRemoteFolder() ).equals( knownFolderPath )) {

                String otherPath = (isRemoteAsked) ? rootFolder.getRemoteFolder() : rootFolder.getLocalFolder();
                Long rootId = Long.valueOf( rootFolder.getId() );

                result = new Pair<String, Long>(otherPath, rootId);
                return result;
            }
        }
        Log.e(TAG, "rootFolder of this filde hasn't been found. ");
        return null;
    }

}
Loading