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

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

fix local folder deletion

parent e4f55a47
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -206,6 +206,12 @@ public final class DbHelper extends SQLiteOpenHelper {
        return id;
    }

    /**
     * Delete a syncedFolder in DB
     * @param id Id of the syncedFOlder to persist
     * @param context Context of the app
     * @return int number of row affected
     */
    public static int deleteSyncedFolder(long id, Context context){
        SyncedFolderDAO dao = openSyncedFolderDAO(context, true);
        if(dao == null){
@@ -215,4 +221,20 @@ public final class DbHelper extends SQLiteOpenHelper {
        dao.close();
        return result;
    }

    /**
     * Delete a syncedFolder and its relative SyncedFileState
     * @param id Id of the syncedFolder to remove
     * @param context context to use
     * @return
     */
    public static int deleteSyncedFolderAndSyncedFileStates(long id, Context context){
        SyncedFileStateDAO dao = openSyncedFileStateDAO(context, true);
        if(dao == null){
            return -1;
        }
        int result = dao.deleteByFolder(id);
        dao.close();
        return result+deleteSyncedFolder(id, context);
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -86,6 +86,15 @@ class SyncedFileStateDAO {
                + " = " + id, null);
    }

    /**
     * Delete each syncedFileState which is bound to syncedFOlder with specified ID
     * @param folderId syncedFolder's id used as foreign key
     * @return number of row affected
     */
    public int deleteByFolder(long folderId) {
        return mDB.delete(SyncedFileStateContract.TABLE_NAME, SyncedFileStateContract.SYNCEDFOLDER_ID
                + " = " + folderId, null);
    }
    /**
     * Not used actually
     * Delete all rows
+32 −10
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.lib.resources.files.ReadRemoteFolderOperation;
import com.owncloud.android.lib.resources.files.RemoteFile;

@@ -109,23 +110,22 @@ public class ListRemoteFileOperation extends RemoteOperation {
                            syncedFolder.setLastEtag( ( (RemoteFile) result.getData().get(0) ).getEtag() ).setToSync(true);
                        }//Last else correspond to error 404 at readRemoteFolderOperation (see below)
                    }else{ //Result isn't a success
                        if(result.getHttpCode() == 404){
                            //If not, just go to next
                            syncedFolder.setToSync(false);
                        if(result.getHttpCode() == 404){ //File not found
                            atLeastOneDirAsChanged = true;
                            syncedFolder.setToSync(true); //@TODO: tester de mettre à true et de commenter le code ci dessous, juste pour voir
                            //If there is no remote file, then try to delete local one if empty. Finally remove Synced Folder from DB.
                            File localFolder = new File(syncedFolder.getLocalFolder());
                            if( localFolder.exists() && localFolder.listFiles().length == 0)
                            {
                                boolean deletion_result = localFolder.delete();
                                Log.d(TAG, "Removed local folder: "+deletion_result);
                            if(localFolder.exists() && localFolder.listFiles().length == 0){
                                localFolder.delete();
                            }
                            /*if( deleteDirectory(localFolder)) {
                                Log.d(TAG, "Removed local folder and all subelement" );
                            }*/
                            if( !localFolder.exists() ) {
                                if (syncedFolder.getId() > -1) { //does the synced folder has been persisted?
                                    //remove it from DB

                                    //@TODO remove syncedFileState bound to syncedFolderToDelete

                                    int deleteResult = DbHelper.deleteSyncedFolder(syncedFolder.getId(), mContext);
                                    //int deleteResult = DbHelper.deleteSyncedFolderAndSyncedFileStates(syncedFolder.getId(), mContext);
                                    Log.d(TAG, "syncedFolder Id: "+syncedFolder.getId() + " deletion from db return " + deleteResult + " row affected");
                                }
                                mSyncedFolderIterator.remove();
@@ -149,4 +149,26 @@ public class ListRemoteFileOperation extends RemoteOperation {
        Log.v(TAG, "end of run()");
        return finalResult;
    }


    /**
     * Delete a folder and all subelements of this folder
     * @param path folder to delete
     * @return true if folder is deleted
     */
    private boolean deleteDirectory(File path){
        if( path.exists() ) {
            File[] files = path.listFiles();
            for(int i=-1, size = files.length; ++i<size;) {
                if(files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                }
                else {
                    files[i].delete();
                }
            }
            return( path.delete() );
        }
        return true;
    }
}
+13 −15
Original line number Diff line number Diff line
@@ -47,7 +47,6 @@ import io.eelo.drive.operations.DownloadFileOperation;
import io.eelo.drive.operations.ListRemoteFileOperation;
import io.eelo.drive.operations.RemoveFileOperation;
import io.eelo.drive.operations.UploadFileOperation;
import io.eelo.drive.receivers.ScreenOffReceiver;
import io.eelo.drive.utils.AppConstants;
import io.eelo.drive.utils.CommonUtils;
import io.eelo.drive.utils.IGetOCClient;
@@ -59,6 +58,7 @@ import static io.eelo.drive.utils.AppConstants.INITIALIZERSERVICE_HAS_RUN;

/**
 * @author Vincent Bourgmayer
 * This service look for remote or looale file to synchronize
 */
public class ObserverService extends Service implements OnRemoteOperationListener, IGetOCClient {
    private final static String TAG = ObserverService.class.getSimpleName();
@@ -131,10 +131,9 @@ public class ObserverService extends Service implements OnRemoteOperationListene
            String accountType = prefs.getString(AccountManager.KEY_ACCOUNT_TYPE, "");
            Account mAccount = CommonUtils.getAccount(accountName, accountType, AccountManager.get(this));


            //There is no account set
            if(mAccount == null){
                final String CURRENTTAG = TAG+"_onStartCOmmand/ no Account";
                final String CURRENTTAG = TAG+"_onStartCommand / no Account";
                Log.d(CURRENTTAG, "Account is null, so start to clear prefs.");
                //clear prefs
                prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, "")
@@ -148,8 +147,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                //Disable scheduledJob
                JobUtils.stopScheduledJob(this);
                //Stop operationManagerService
                if(mBoundToOperationManager)
                    mOperationManagerService.stopSelf();
                if(mBoundToOperationManager) mOperationManagerService.stopSelf();
                boolean unregisteredReceiver = CommonUtils.unregisterScreenOff(getApplicationContext());
                Log.d(CURRENTTAG, "ScreenOffReceiver unregistered : "+unregisteredReceiver);

@@ -162,7 +160,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                Log.w(TAG, "ContentResolver.getSyncAutomatically(account, '**_PROVIDER_AUTHORITY') return false");
            }
        }else{
            Log.w(TAG, "Service must initialize before to start");
            Log.w(TAG, "Server must be initialized before to start");
        }
        return super.onStartCommand( intent, flags, startId );
    }
@@ -171,7 +169,6 @@ public class ObserverService extends Service implements OnRemoteOperationListene
    /**
     * Start to bind this service to OperationManagerService or start scan if binding is already set.
     * Method to factorise code that is called from different place
     *
     */
    private void begin(){
        Log.i(TAG, "begin()");
@@ -179,7 +176,6 @@ public class ObserverService extends Service implements OnRemoteOperationListene
            this.isWorking = true;
            //If there is a internet connexion
            if (CommonUtils.haveNetworkConnexion(this)) {

                if (!mBoundToOperationManager) {
                    Intent opMgrSrvIntent = new Intent(this, OperationManagerService.class);
                    bindService(opMgrSrvIntent, this.mOperationManagerServiceConnection, Context.BIND_AUTO_CREATE);
@@ -214,7 +210,6 @@ public class ObserverService extends Service implements OnRemoteOperationListene
        }
    }


    /**
     * Clear cached file unused:
     * remove each cached file which isn't in OperationManagerService.lockedSyncedFileState();
@@ -420,18 +415,21 @@ public class ObserverService extends Service implements OnRemoteOperationListene
                File file = new File( syncedFileStates.get(i).getLocalPath() );
                boolean fileExists = file.exists();
                if( fileExists) {
                    Log.d(CURRENTTAG, file.getName()+" exists *1");
                    //delete file
                    int rowAffected = getContentResolver().delete(MediaStore.Files.getContentUri("external"),
                            MediaStore.Files.FileColumns.DATA + "=?",
                            new String[]{file.getAbsolutePath()});
                            new String[]{CommonUtils.getLocalPath(file)});
                    Log.d(CURRENTTAG, "deleted rows by mediastore : "+rowAffected);
                    //if delete failed, try another methods
                    if (rowAffected < 1) {
                    //if (rowAffected < 1) {
                        //Log.d(CURRENTTAG, "first deletion trials failed");
                    fileExists = !file.delete();

                    }else
                        fileExists = false;
                    /*}else
                        fileExists = false;*/
                }
                if(! fileExists ) {
                    Log.d(CURRENTTAG, file.getName()+" doesn't exists *2");
                    //It means that file has been correctly deleted from device. So update DB.
                    if (DbHelper.manageSyncedFileStateDB(syncedFileState, "DELETE", this) > 0)
                        CommonUtils.sendNotification(this,3310, syncedFileState.getName() + " has been removed", android.R.drawable.ic_delete);
+1 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import static io.eelo.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORITY;
public  abstract class CommonUtils {
    final private static String TAG = CommonUtils.class.getSimpleName();


    public static boolean unregisterScreenOff(Context context){
        try {
            Log.d("TAG", "unregisterReceiver(screenOffReceiver)");