From 84dcfa93759af82e06a127eabfedb234cd30934e Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 28 Sep 2022 15:27:42 +0200 Subject: [PATCH 1/4] ContentScanner package: replace android logging by Timber logging --- .../contentScanner/LocalContentScanner.java | 15 +++++++-------- .../contentScanner/RemoteContentScanner.java | 19 +++++++++---------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/contentScanner/LocalContentScanner.java b/app/src/main/java/foundation/e/drive/contentScanner/LocalContentScanner.java index 5864c980..23ac48db 100644 --- a/app/src/main/java/foundation/e/drive/contentScanner/LocalContentScanner.java +++ b/app/src/main/java/foundation/e/drive/contentScanner/LocalContentScanner.java @@ -9,7 +9,6 @@ package foundation.e.drive.contentScanner; import android.accounts.Account; import android.content.Context; -import android.util.Log; import java.io.File; import java.util.List; @@ -20,6 +19,7 @@ import foundation.e.drive.models.SyncedFileState; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.FileDiffUtils; +import timber.log.Timber; /** * Class to encapsulate function about scanning local file and @@ -27,10 +27,9 @@ import foundation.e.drive.utils.FileDiffUtils; */ public class LocalContentScanner extends AbstractContentScanner{ - private static final String TAG = LocalContentScanner.class.getSimpleName(); - public LocalContentScanner(Context context, Account account, List syncedFolders) { super(context, account, syncedFolders); + Timber.tag(LocalContentScanner.class.getSimpleName()); } @Override @@ -42,11 +41,11 @@ public class LocalContentScanner extends AbstractContentScanner{ final File file = new File(fileState.getLocalPath()); if (file.exists()) { - Log.w(TAG, "Expected " + file.getAbsolutePath() + "to be missing. but it still exists"); + Timber.d("Expected %s to be missing. but it still exists", file.getAbsolutePath()); return; } - Log.i(TAG, "Add remove SyncRequest for file " + file.getAbsolutePath()); + Timber.d("Add remove SyncRequest for file %s", file.getAbsolutePath()); syncRequests.put(fileState.getId(), new SyncRequest(fileState, SyncRequest.Type.REMOTE_DELETE)); } @@ -69,17 +68,17 @@ public class LocalContentScanner extends AbstractContentScanner{ int storedId = DbHelper.manageSyncedFileStateDB(newSyncedFileState, "INSERT", context); if (storedId > 0){ newSyncedFileState.setId( storedId ); - Log.i(TAG, "Add upload SyncRequest for new file " + filePath); + Timber.d("Add upload SyncRequest for new file %s", filePath); syncRequests.put(storedId, new SyncRequest(newSyncedFileState, SyncRequest.Type.UPLOAD)); } else { - Log.w(TAG, "Failed to insert (in DB) new SyncedFileState for " + filePath); + Timber.d("Failed to insert (in DB) new SyncedFileState for %s", filePath); } } @Override protected void onKnownFileFound(File file, SyncedFileState fileState) { if (FileDiffUtils.getActionForFileDiff(file, fileState) == FileDiffUtils.Action.Upload) { - Log.d(TAG, "Add upload SyncRequest for " + file.getAbsolutePath()); + Timber.d("Add upload SyncRequest for %s", file.getAbsolutePath()); syncRequests.put(fileState.getId(), new SyncRequest(fileState, SyncRequest.Type.UPLOAD)); } } diff --git a/app/src/main/java/foundation/e/drive/contentScanner/RemoteContentScanner.java b/app/src/main/java/foundation/e/drive/contentScanner/RemoteContentScanner.java index 4f424bb2..047bd8ff 100644 --- a/app/src/main/java/foundation/e/drive/contentScanner/RemoteContentScanner.java +++ b/app/src/main/java/foundation/e/drive/contentScanner/RemoteContentScanner.java @@ -12,7 +12,6 @@ import static foundation.e.drive.utils.FileDiffUtils.getActionForFileDiff; import android.accounts.Account; import android.content.Context; import android.provider.MediaStore; -import android.util.Log; import com.owncloud.android.lib.resources.files.model.RemoteFile; @@ -25,13 +24,13 @@ import foundation.e.drive.models.SyncedFileState; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.FileDiffUtils; +import timber.log.Timber; /** * Implementation of AbstractContentScanner for RemoteFile * @author vincent Bourgmayer */ public class RemoteContentScanner extends AbstractContentScanner { - private static final String TAG = RemoteContentScanner.class.getSimpleName(); /** * @param context Context used to access Database, etc. @@ -39,6 +38,7 @@ public class RemoteContentScanner extends AbstractContentScanner { */ public RemoteContentScanner(Context context, Account account, List syncedFolders) { super(context, account, syncedFolders); + Timber.tag(RemoteContentScanner.class.getSimpleName()); } @Override @@ -46,14 +46,14 @@ public class RemoteContentScanner extends AbstractContentScanner { final FileDiffUtils.Action action = getActionForFileDiff(file, fileState); if (action == FileDiffUtils.Action.Download) { - Log.d(TAG, "Add download SyncRequest for " + file.getRemotePath()); + Timber.d("Add download SyncRequest for %s", file.getRemotePath()); syncRequests.put(fileState.getId(), new DownloadRequest(file, fileState)); } else if (action == FileDiffUtils.Action.updateDB) { fileState.setLastETAG(file.getEtag()); final int affectedRows = DbHelper.manageSyncedFileStateDB(fileState, "UPDATE", context); - if (affectedRows == 0) Log.e(TAG, "Error while updating eTag in DB for: " + file.getRemotePath()); + if (affectedRows == 0) Timber.d("Error while updating eTag in DB for: %s", file.getRemotePath()); } } @@ -78,21 +78,20 @@ public class RemoteContentScanner extends AbstractContentScanner { final int storedId = DbHelper.manageSyncedFileStateDB(newFileState, "INSERT", context); if (storedId > 0) { newFileState.setId(storedId); - Log.d(TAG, "Add downloadSyncRequest for new remote file: " + remoteFilePath); + Timber.d("Add downloadSyncRequest for new remote file: %s", remoteFilePath); this.syncRequests.put(storedId, new DownloadRequest(file, newFileState)); } else { - Log.w(TAG, "Failed to insert (in DB) new SyncedFileState for remote file " + remoteFilePath); + Timber.d("Failed to insert (in DB) new SyncedFileState for remote file %s", remoteFilePath); } } @Override protected void onMissingRemoteFile(SyncedFileState fileState) { if (!CommonUtils.isThisSyncAllowed(account, fileState.isMediaType())) { - Log.d(TAG, "Sync of current file: " + fileState.getName() + " isn't allowed"); + Timber.d("Sync of current file: %s isn't allowed", fileState.getName()); return; } - //Check that file has already been synced fully if (!fileState.hasBeenSynchronizedOnce()) { return; } @@ -107,12 +106,12 @@ public class RemoteContentScanner extends AbstractContentScanner { new String[]{CommonUtils.getLocalPath(file)}); if (!file.delete()) { //May throw SecurityException or IOException - Log.w(TAG, "local file (" + file.getName() + ") removal failed."); + Timber.d("local file ( %s ) removal failed",file.getName()); return; } if (DbHelper.manageSyncedFileStateDB(fileState, "DELETE", context) <= 0) { - Log.e(TAG, "Failed to remove " + file.getName() + " from DB"); + Timber.e("Failed to remove %s from DB", file.getName()); } } -- GitLab From 2e6bf07ac669d70da3927e86aafda18bbed95dbd Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 28 Sep 2022 16:34:20 +0200 Subject: [PATCH 2/4] database package: replace Android logging by Timber logging and - Clean coding (space around brackets, etc.) - Add final keyword where possible - Remove one unused method in SyncedFileStateDAO (removeByFolder) - rewrite few part of the code --- .../foundation/e/drive/database/DbHelper.java | 128 +++++++++--------- .../e/drive/database/SyncedFileStateDAO.java | 38 ++---- .../e/drive/database/SyncedFolderDAO.java | 60 ++++---- 3 files changed, 94 insertions(+), 132 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/database/DbHelper.java b/app/src/main/java/foundation/e/drive/database/DbHelper.java index 54708046..ee94b9f9 100644 --- a/app/src/main/java/foundation/e/drive/database/DbHelper.java +++ b/app/src/main/java/foundation/e/drive/database/DbHelper.java @@ -13,7 +13,6 @@ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; -import android.util.Log; import java.io.File; import java.io.FileInputStream; @@ -27,6 +26,7 @@ import java.util.List; import foundation.e.drive.BuildConfig; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.models.SyncedFileState; +import timber.log.Timber; /** * @author Vincent Bourgmayer @@ -34,7 +34,7 @@ import foundation.e.drive.models.SyncedFileState; * https://vogella.developpez.com/tutoriels/android/utilisation-base-donnees-sqlite/ */ public final class DbHelper extends SQLiteOpenHelper { - final private static String TAG = DbHelper.class.getSimpleName(); //Tag for log + private static final int DATABASE_VERSION = 20; //16/03/2022 public static final String DATABASE_NAME = "eelo_drive.db"; @@ -43,6 +43,7 @@ public final class DbHelper extends SQLiteOpenHelper { */ public DbHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); + Timber.tag(DbHelper.class.getSimpleName()); } /** @@ -63,7 +64,7 @@ public final class DbHelper extends SQLiteOpenHelper { */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - Log.i(TAG, "onUpgrade(db, "+oldVersion+", "+newVersion+")"); + Timber.i("onUpgrade(db, %s, %s)",oldVersion, newVersion); try { if (oldVersion < 19) { db.execSQL(SyncedFolderContract.UPDATE_TABLE_TO_VERSION_19); @@ -78,29 +79,29 @@ public final class DbHelper extends SQLiteOpenHelper { db.execSQL(SyncedFileStateContract.UPDATE_MEDIA_DATA_TO_VERSION_20); db.execSQL(SyncedFileStateContract.UPDATE_SETTINGS_DATA_TO_VERSION_20); } - } catch(Exception e) { - Log.e(TAG, e.getMessage()); + } catch(Exception exception) { + Timber.e(exception); } } private static SyncedFileStateDAO openSyncedFileStateDAO(Context context, boolean writeMod){ - SyncedFileStateDAO dao = new SyncedFileStateDAO(context); - try{ + final SyncedFileStateDAO dao = new SyncedFileStateDAO(context); + try { dao.open(writeMod); - }catch (Exception e){ - Log.e(TAG, e.toString()); + } catch (Exception exception) { //todo catch SQLiteException instead or throw it and handle it in higher stack + Timber.e(exception); return null; } return dao; } private static SyncedFolderDAO openSyncedFolderDAO(Context context, boolean writeMod){ - SyncedFolderDAO dao = new SyncedFolderDAO(context); - try{ + final SyncedFolderDAO dao = new SyncedFolderDAO(context); + try { dao.open(writeMod); - }catch(Exception e){ - Log.e(TAG, e.toString() ); + } catch(Exception exception) { //todo catch SQLiteException instead or throw it and handle it in higher stack + Timber.e(exception); return null; } return dao; @@ -114,20 +115,17 @@ public final class DbHelper extends SQLiteOpenHelper { * but if it is an insert then it return the id of the inserted data. */ public static int manageSyncedFileStateDB(SyncedFileState syncedFileState, String action, Context context){ - Log.i(TAG, "manageSyncedFileStateDB()"); + Timber.d("manageSyncedFileStateDB( %s, %s )", syncedFileState.getName(), action); int result = -1; - SyncedFileStateDAO dao = openSyncedFileStateDAO(context, true); - if(dao != null){ - if(action.equals( "UPDATE") ) { - Log.d(TAG, "DB Update for "+syncedFileState.getName() ); + final SyncedFileStateDAO dao = openSyncedFileStateDAO(context, true); + if (dao != null) { + if (action.equals( "UPDATE")) { result = dao.update(syncedFileState); - }else if(action.equals("INSERT") ){ - Log.d(TAG, "DB Insert for "+syncedFileState.getName() ); + } else if (action.equals("INSERT")) { result = (int) dao.insert( syncedFileState ); } - else if(action.equals("DELETE") ) { - Log.d(TAG, "DB Delete for "+syncedFileState.getName() ); - result = dao.delete( syncedFileState.getId() ); + else if (action.equals("DELETE")) { + result = dao.delete(syncedFileState.getId()); } dao.close(); } @@ -142,9 +140,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 = openSyncedFileStateDAO(context, false); + final SyncedFileStateDAO dao = openSyncedFileStateDAO(context, false); - if(dao == null){ + if (dao == null) { return null; } SyncedFileState syncedFileState = dao.getByPath(Path, useLocalPath); @@ -160,11 +158,11 @@ public final class DbHelper extends SQLiteOpenHelper { * @return empty list if DB opening failed, either return the list of SyncedFileState */ public static List getSyncedFileStatesByFolders(Context context, List ids) {//Connect to DB - SyncedFileStateDAO dao = openSyncedFileStateDAO(context, false); - if(dao == null){ + final SyncedFileStateDAO dao = openSyncedFileStateDAO(context, false); + if (dao == null) { return new ArrayList<>(); } - List result = dao.getBySyncedFolderID(ids); + final List result = dao.getBySyncedFolderID(ids); dao.close(); return result; } @@ -178,15 +176,15 @@ public final class DbHelper extends SQLiteOpenHelper { public static int updateSyncedFolders(List syncedFolders, Context context){ int result = -1; - //Connect to DB - SyncedFolderDAO dao = openSyncedFolderDAO(context, true); - if(dao == null){ + + final SyncedFolderDAO dao = openSyncedFolderDAO(context, true); + if (dao == null) { return result; } - //Execute queries - int SyncedFoldersSize = syncedFolders.size(); + + final int SyncedFoldersSize = syncedFolders.size(); for(int i = 0; i < SyncedFoldersSize; ++i ){ - result += dao.update( syncedFolders.get(i) ); + result += dao.update(syncedFolders.get(i)); } dao.close(); return result; @@ -195,12 +193,12 @@ public final class DbHelper extends SQLiteOpenHelper { public static int updateSyncedFolder(SyncedFolder syncedFolder, Context context) { int result = -1; - //Connect to DB - SyncedFolderDAO dao = openSyncedFolderDAO(context, true); - if (dao == null){ + + final SyncedFolderDAO dao = openSyncedFolderDAO(context, true); + if (dao == null) { return result; } - result = dao.update( syncedFolder ); + result = dao.update(syncedFolder); dao.close(); return result; } @@ -211,12 +209,11 @@ public final class DbHelper extends SQLiteOpenHelper { * @return List a list of SyncedFolder from DB */ public static List getAllSyncedFolders(Context context) { - SyncedFolderDAO dao = openSyncedFolderDAO(context, false); + final SyncedFolderDAO dao = openSyncedFolderDAO(context, false); List mSyncedFolder = new ArrayList(); - if(dao == null){ + if (dao == null) { return mSyncedFolder; - }else { - + } else { mSyncedFolder = dao.getSyncedFolderList(SyncedFolderContract.ENABLED+" = 1 ", null ); dao.close(); return mSyncedFolder; @@ -224,12 +221,11 @@ public final class DbHelper extends SQLiteOpenHelper { } public static List getSyncedFolderList(Context context, boolean isMediaType) { - SyncedFolderDAO dao = openSyncedFolderDAO(context, false); + final SyncedFolderDAO dao = openSyncedFolderDAO(context, false); List mSyncedFolder = new ArrayList<>(); - if(dao == null){ + if (dao == null) { return mSyncedFolder; - } - else { + } else { mSyncedFolder = dao.getSyncedFolderList(SyncedFolderContract.ENABLED+" = 1 AND " +SyncedFolderContract.IS_MEDIA_TYPE+" = ?", new String[] {(isMediaType)? "1":"0"} ); dao.close(); @@ -244,12 +240,11 @@ public final class DbHelper extends SQLiteOpenHelper { * @return the id of the Synced folder or -1 if it hasn't been inserted */ public static long insertSyncedFolder(SyncedFolder mSyncedFolder, Context context){ - SyncedFolderDAO dao = openSyncedFolderDAO(context, true); - if(dao == null){ - Log.d(TAG+"_insertSyncedFolder", "dao is null"); + final SyncedFolderDAO dao = openSyncedFolderDAO(context, true); + if (dao == null) { return -2; } - long id = dao.insert(mSyncedFolder); + final long id = dao.insert(mSyncedFolder); dao.close(); return id; } @@ -261,21 +256,21 @@ public final class DbHelper extends SQLiteOpenHelper { * @return int number of row affected */ public static int deleteSyncedFolder(long id, Context context){ - SyncedFolderDAO dao = openSyncedFolderDAO(context, true); - if(dao == null){ + final SyncedFolderDAO dao = openSyncedFolderDAO(context, true); + if (dao == null) { return -1; } - int result = dao.delete(id); + final int result = dao.delete(id); dao.close(); return result; } public static SyncedFolder getSyncedFolderByLocalPath(String localPath, Context context){ - SyncedFolderDAO dao = openSyncedFolderDAO(context, true); + final SyncedFolderDAO dao = openSyncedFolderDAO(context, true); if (dao == null) { return null; } - SyncedFolder syncedFolder = dao.getSyncedFolderByLocalPath(localPath); + final SyncedFolder syncedFolder = dao.getSyncedFolderByLocalPath(localPath); dao.close(); return syncedFolder; } @@ -288,12 +283,12 @@ public final class DbHelper extends SQLiteOpenHelper { * @return number of row affected */ public static int forceFoldertoBeRescan(int syncedFolderId, Context context){ - SyncedFolderDAO dao = openSyncedFolderDAO(context, true); - if(dao == null){ + final SyncedFolderDAO dao = openSyncedFolderDAO(context, true); + if (dao == null) { return -1; } - int result = dao.reduceLastModifiedValue(syncedFolderId); + final int result = dao.reduceLastModifiedValue(syncedFolderId); dao.close(); return result; } @@ -310,8 +305,8 @@ public final class DbHelper extends SQLiteOpenHelper { final SyncedFileStateDAO dao = openSyncedFileStateDAO(context, false); result = dao.countFileWaitingForUploadForSyncedFolder(syncedFolderID) > 0; dao.close(); - } catch (SQLiteException e) { - Log.e(TAG, "SQlite error", e); + } catch (SQLiteException exception) { + Timber.e(exception); } return result; } @@ -328,8 +323,8 @@ public final class DbHelper extends SQLiteOpenHelper { final SyncedFileStateDAO dao = openSyncedFileStateDAO(context, false); result = dao.countFileWaitingForDownloadForSyncedFolder(syncedFolderId) > 0; dao.close(); - } catch (SQLiteException e) { - Log.e(TAG, "SQLite error", e); + } catch (SQLiteException exception) { + Timber.e(exception); } return result; } @@ -352,13 +347,12 @@ public final class DbHelper extends SQLiteOpenHelper { while ((read = src.read(buffer)) != -1) { dst.write(buffer, 0, read); } - Log.i(TAG, "Database has been dump at "+ dbDump.getAbsolutePath()); + Timber.d("Database has been dump at %s", dbDump.getAbsolutePath()); return; - } catch (IOException e) { - Log.e(TAG, "IOException", e); + } catch (IOException exception) { + Timber.d(exception); } } - Log.e(TAG,"Failed to dump Database"); } /** @@ -367,7 +361,7 @@ public final class DbHelper extends SQLiteOpenHelper { * @throws SQLiteException if database can't be open */ public static void cleanSyncedFileStateTableAfterUpdate(Context context) throws SQLiteException { - SyncedFileStateDAO dao = openSyncedFileStateDAO(context, true); + final SyncedFileStateDAO dao = openSyncedFileStateDAO(context, true); dao.deleteHiddenFileStates(); dao.deleteUnscannableMediaFiles(); dao.close(); diff --git a/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java b/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java index c7522735..8093770f 100644 --- a/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java +++ b/app/src/main/java/foundation/e/drive/database/SyncedFileStateDAO.java @@ -17,12 +17,12 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDoneException; import android.database.sqlite.SQLiteStatement; -import android.util.Log; import java.util.ArrayList; import java.util.List; import foundation.e.drive.models.SyncedFileState; +import timber.log.Timber; import static foundation.e.drive.database.SyncedFileStateContract.SCANNABLE; import static foundation.e.drive.database.SyncedFileStateContract.TABLE_NAME; @@ -34,19 +34,17 @@ import static foundation.e.drive.database.SyncedFileStateContract.LOCAL_PATH; import static foundation.e.drive.database.SyncedFileStateContract.REMOTE_PATH; import static foundation.e.drive.database.SyncedFileStateContract.SYNCEDFOLDER_ID; - /** * @author Vincent Bourgmayer * Offers Query to CRUD operation for SyncedFIleState Object */ class SyncedFileStateDAO { - final private static String TAG = SyncedFileStateDAO.class.getSimpleName(); //Tag for log - private SQLiteDatabase mDB; private final DbHelper mHelper; SyncedFileStateDAO(Context context){ this.mHelper = new DbHelper(context); + Timber.tag(SyncedFileStateDAO.class.getSimpleName()); } /** @@ -71,7 +69,7 @@ class SyncedFileStateDAO { * @return a ContentValues object */ private ContentValues toContentValues(SyncedFileState syncedFileState) { - ContentValues values = new ContentValues(); + final ContentValues values = new ContentValues(); values.put( FILE_NAME, syncedFileState.getName() ); values.put( LOCAL_PATH, syncedFileState.getLocalPath() ); values.put( REMOTE_PATH, syncedFileState.getRemotePath() ); @@ -104,7 +102,6 @@ class SyncedFileStateDAO { + " = " + id, null); } - /** * Remove SyncedFileState for hidden files (starting with '.') * @return number of deleted input @@ -122,33 +119,16 @@ class SyncedFileStateDAO { return mDB.delete(TABLE_NAME, IS_MEDIA_TYPE + " =1 AND "+SCANNABLE+" =0", 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(TABLE_NAME, SYNCEDFOLDER_ID - + " = " + folderId, null); - } - /** * Update a specific syncedFile * @param syncedFileState SyncedFileState to update * @return number of row affected */ int update(SyncedFileState syncedFileState){ - int result = 0; - try{ - result = mDB.update(TABLE_NAME, + return mDB.update(TABLE_NAME, toContentValues(syncedFileState), SyncedFileStateContract._ID+" = "+syncedFileState.getId(), null); - }catch(Exception e){ - Log.e(TAG, e.toString()); - } - - return result; } @@ -217,10 +197,10 @@ class SyncedFileStateDAO { } } } - Log.d(TAG+"_getBySyncedFolderID(...)", query); - Cursor cursor = mDB.rawQuery(query, null); + Timber.v("getBySyncedFolderID's query: %s", query); + final Cursor cursor = mDB.rawQuery(query, null); cursor.moveToFirst(); - List result = new ArrayList<>(); + final List result = new ArrayList<>(); while(!cursor.isAfterLast() ) { result.add( cursorToSyncedFileState(cursor) ); cursor.moveToNext(); @@ -254,10 +234,10 @@ class SyncedFileStateDAO { query +=" like \""+path+"\""; - Cursor cursor = mDB.rawQuery(query, null); + final Cursor cursor = mDB.rawQuery(query, null); cursor.moveToFirst(); SyncedFileState syncedFileState = null; - if ( !cursor.isAfterLast()) { + if (!cursor.isAfterLast()) { syncedFileState = cursorToSyncedFileState(cursor); } cursor.close(); diff --git a/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java b/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java index 4e63e002..cbf1dc13 100644 --- a/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java +++ b/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java @@ -14,11 +14,12 @@ import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; -import android.util.Log; import java.util.ArrayList; import java.util.List; import foundation.e.drive.models.SyncedFolder; +import timber.log.Timber; + import static foundation.e.drive.database.SyncedFolderContract.CATEGORIE_LABEL; import static foundation.e.drive.database.SyncedFolderContract.ENABLED; import static foundation.e.drive.database.SyncedFolderContract.LAST_ETAG; @@ -34,10 +35,10 @@ import com.owncloud.android.lib.resources.files.FileUtils; /** * @author Vincent Bourgmayer + * //todo put consistency in method's visibility * Source: https://vogella.developpez.com/tutoriels/android/utilisation-base-donnees-sqlite/ */ class SyncedFolderDAO { - final private static String TAG = SyncedFolderDAO.class.getSimpleName(); //Tag for log private SQLiteDatabase mDB; private final DbHelper mHelper; private final String[] allColumns = { SyncedFolderContract._ID, @@ -53,6 +54,7 @@ class SyncedFolderDAO { }; SyncedFolderDAO(Context context){ + Timber.tag(SyncedFolderDAO.class.getSimpleName()); this.mHelper = new DbHelper(context); } @@ -78,7 +80,7 @@ class SyncedFolderDAO { * @return ContentValues object */ private ContentValues toContentValues(SyncedFolder syncedFolder){ - ContentValues values = new ContentValues(); + final ContentValues values = new ContentValues(); values.put( CATEGORIE_LABEL, syncedFolder.getLibelle() ); values.put( LOCAL_PATH, syncedFolder.getLocalFolder() ); values.put( REMOTE_PATH, syncedFolder.getRemoteFolder() ); @@ -117,71 +119,58 @@ class SyncedFolderDAO { * @return Number of row affected */ public int update(SyncedFolder syncedFolder){ - int result = 0; - try{ - result = mDB.update(TABLE_NAME, - toContentValues(syncedFolder), - SyncedFolderContract._ID+" = "+ syncedFolder.getId(), - null); - }catch(Exception e){ - Log.e(TAG, e.toString()); - } - - return result; + return mDB.update(TABLE_NAME, + toContentValues(syncedFolder), + SyncedFolderContract._ID+" = "+ syncedFolder.getId(), + null); } /** * Get list of syncedFolder * @param selection if true, return all enabled syncedFolder, if false return all disabled syncedFolder + * todo rewrite this method and at least its description because it's not clear... * @return List of all syncedFolder */ List getSyncedFolderList(String selection, String[] args) { - Log.i(TAG, "getSyncedFolderList("+selection+", "+args+")"); - List syncedFolders = new ArrayList(); - - Cursor cursor = mDB.query(TABLE_NAME, allColumns, selection,args, null, null, + final List syncedFolders = new ArrayList(); + final Cursor cursor = mDB.query(TABLE_NAME, allColumns, selection,args, null, null, null); cursor.moveToFirst(); - while ( !cursor.isAfterLast() ) { - SyncedFolder syncedFolder = cursorToSyncedFolder(cursor); - if(syncedFolder == null ){ - Log.e(TAG, "getAllSyncedFolders : current Folder is null"); - }else { + while (!cursor.isAfterLast()) { + final SyncedFolder syncedFolder = cursorToSyncedFolder(cursor); + if (syncedFolder == null ) { + Timber.v("getSyncedFolderList : current Folder ( %s ) is null", syncedFolder.getLocalFolder()); + } else { syncedFolders.add(syncedFolder); } cursor.moveToNext(); } - // assurez-vous de la fermeture du curseur + cursor.close(); return syncedFolders; } /** * reduce to 1 the value stored in lastModified of this syncFolder . The goal is to allow app to resync this folder next time - * @param syncFolderID if true, return all enabled syncedFolder, if false return all disabled syncedFolder + * @param syncFolderID id of the synced folder to update * @return number of row affected */ int reduceLastModifiedValue(int syncFolderID) { - ContentValues values = new ContentValues(); + final ContentValues values = new ContentValues(); values.put( LOCAL_LAST_MODIFIED, 1 ); - int result = 0; - try{ - result = mDB.update(TABLE_NAME, values, + + return mDB.update(TABLE_NAME, values, SyncedFolderContract._ID+" = "+ syncFolderID, null); - }catch(Exception e){ - Log.e(TAG, e.toString()); - } - return result; } SyncedFolder getSyncedFolderByLocalPath(String localPath){ - Cursor cursor = mDB.query(TABLE_NAME, allColumns, LOCAL_PATH+" like \""+localPath+ FileUtils.PATH_SEPARATOR+"\"", new String[0], null, null, null); + final Cursor cursor = mDB.query(TABLE_NAME, allColumns, LOCAL_PATH+" like \""+localPath+ FileUtils.PATH_SEPARATOR+"\"", new String[0], null, null, null); cursor.moveToFirst(); SyncedFolder result = null; - if ( !cursor.isAfterLast() ) { + if (!cursor.isAfterLast()) { result = cursorToSyncedFolder(cursor); } cursor.close(); @@ -205,6 +194,5 @@ class SyncedFolderDAO { .setId(cursor.getInt(0) ) .setLastEtag(cursor.getString( 4 ) ) .setLastModified(cursor.getLong( 5 ) ); - } } -- GitLab From a15d14f7fbb0f2a8304cf4885e3a69bc04fc9b9f Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Wed, 28 Sep 2022 16:53:50 +0200 Subject: [PATCH 3/4] FileObservers package: Replace Android logging by Timber logging --- .../FileObservers/FileEventListener.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/FileObservers/FileEventListener.java b/app/src/main/java/foundation/e/drive/FileObservers/FileEventListener.java index 43266bee..b047c955 100644 --- a/app/src/main/java/foundation/e/drive/FileObservers/FileEventListener.java +++ b/app/src/main/java/foundation/e/drive/FileObservers/FileEventListener.java @@ -14,7 +14,6 @@ import static foundation.e.drive.models.SyncRequest.Type.UPLOAD; import android.content.Context; import android.content.Intent; import android.os.FileObserver; -import android.util.Log; import com.owncloud.android.lib.resources.files.FileUtils; @@ -27,18 +26,19 @@ import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.services.SynchronizationService; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.SynchronizationServiceConnection; +import timber.log.Timber; /** * @author Narinder Rana * @author vincent Bourgmayer */ public class FileEventListener { - private static final String TAG = FileEventListener.class.getSimpleName(); private final Context appContext; private final SynchronizationServiceConnection serviceConnection = new SynchronizationServiceConnection(); public FileEventListener(Context applicationContext) { + Timber.tag(FileEventListener.class.getSimpleName()); this.appContext = applicationContext; } @@ -66,7 +66,7 @@ public class FileEventListener { handleFileDelete(file); break; case FileObserver.MOVE_SELF: //todo to be able to catch that, we probably need a buffer to catch a succession (MOVE_FROM, MOVE_TO, then MOVE_SELF). - Log.d(TAG, file.getAbsolutePath() + " has been moved. Not handled yet"); + Timber.d("%s has been moved. Not handled yet", file.getAbsolutePath()); break; default: break; @@ -90,7 +90,7 @@ public class FileEventListener { handleDirectoryDelete(dir); break; case FileObserver.MOVE_SELF: - Log.d(TAG, dir.getAbsolutePath() + " has been moved. Not handled yet"); + Timber.d("%s has been moved. Not handled yet", dir.getAbsolutePath()); break; default: break; @@ -102,12 +102,12 @@ public class FileEventListener { * @param request */ private void sendSyncRequestToSynchronizationService(SyncRequest request) { - Log.d(TAG, "Sending a SyncRequest for " + request.getSyncedFileState().getName()); + Timber.d("Sending a SyncRequest for %s", request.getSyncedFileState().getName()); if (serviceConnection.isBoundToSynchronizationService()) { serviceConnection.getSynchronizationService().queueSyncRequest(request); serviceConnection.getSynchronizationService().startSynchronization(); }else{ - Log.w(TAG, "Impossible to send SyncRequest. FileEventListener is not bound to SynchronizationService"); + Timber.d("Impossible to send SyncRequest. FileEventListener is not bound to SynchronizationService"); } } @@ -117,7 +117,7 @@ public class FileEventListener { * @param directory */ private void handleDirectoryCreate(File directory) { - Log.d(TAG, "handleDirectoryCreate(" + directory.getAbsolutePath() + ")"); + Timber.d("handleDirectoryCreate( %s )",directory.getAbsolutePath()); final String parentPath = CommonUtils.getLocalPath(directory.getParentFile()); final SyncedFolder parentFolder = DbHelper.getSyncedFolderByLocalPath(parentPath, appContext); if (parentFolder != null) { @@ -133,7 +133,7 @@ public class FileEventListener { */ private void handleDirectoryCloseWrite(File directory) { final String fileLocalPath = CommonUtils.getLocalPath(directory); - Log.d(TAG, "handleDirectoryCloseWrite(" + fileLocalPath + ")"); + Timber.d("handleDirectoryCloseWrite( %s )",fileLocalPath ); final SyncedFolder folder = DbHelper.getSyncedFolderByLocalPath(fileLocalPath, appContext); if (folder == null) { handleDirectoryCreate(directory); //todo check if really relevant @@ -149,7 +149,7 @@ public class FileEventListener { */ private void handleDirectoryDelete(File directory) { final String fileLocalPath = CommonUtils.getLocalPath(directory); - Log.d(TAG, "handleDirectoryDelete("+fileLocalPath+")"); + Timber.d("handleDirectoryDelete( %s )", fileLocalPath); SyncedFolder folder = DbHelper.getSyncedFolderByLocalPath(fileLocalPath, appContext); if (folder == null) { //look for parent @@ -173,7 +173,7 @@ public class FileEventListener { */ private void handleFileCloseWrite(File file) { final String fileLocalPath = CommonUtils.getLocalPath(file); - Log.d(TAG, "handleFileCloseWrite("+fileLocalPath+")"); + Timber.d("handleFileCloseWrite( %s )", fileLocalPath); SyncRequest request = null; SyncedFileState fileState = DbHelper.loadSyncedFile( appContext, fileLocalPath, true); @@ -181,7 +181,7 @@ public class FileEventListener { final String parentPath = CommonUtils.getLocalPath(file.getParentFile()); SyncedFolder parentFolder = DbHelper.getSyncedFolderByLocalPath(parentPath, appContext); if (parentFolder == null || !parentFolder.isEnabled()) { - Log.w(TAG, "Won't send sync request: no parent are known for new file: "+file.getName()); + Timber.w("Won't send sync request: no parent are known for new file: %s", file.getName()); return; } int scannableValue = 0; @@ -197,7 +197,7 @@ public class FileEventListener { fileState.setId(storedId); request = new SyncRequest(fileState, UPLOAD); } else { - Log.w(TAG, "New File " + file.getName() + " observed but impossible to insert it in DB"); + Timber.d("New File %s observed but impossible to insert it in DB", file.getName()); } } else { //File update if (fileState.getScannable() > 1) { @@ -215,7 +215,7 @@ public class FileEventListener { */ private void handleFileDelete(File file) { final String fileLocalPath = CommonUtils.getLocalPath(file); - Log.d(TAG, "handleFileDelete("+fileLocalPath+")"); + Timber.d("handleFileDelete( %s )",fileLocalPath); final SyncedFileState fileState = DbHelper.loadSyncedFile( appContext, fileLocalPath, true); if (fileState == null) { return; //Todo #1: should we call handleDirectoryDelete before to return ? @@ -232,7 +232,7 @@ public class FileEventListener { if (serviceConnection.isBoundToSynchronizationService()) appContext.unbindService(serviceConnection); else - Log.w(TAG, "Not bound to SynchronizationService: can't unbind."); + Timber.w("Not bound to SynchronizationService: can't unbind."); } public void bindToSynchronizationService(){ -- GitLab From 7b32d894af905fb47d6a84e97b29e51e2188fa2e Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Thu, 29 Sep 2022 07:42:13 +0000 Subject: [PATCH 4/4] Apply 1 suggestion(s) to 1 file(s) --- .../main/java/foundation/e/drive/database/SyncedFolderDAO.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java b/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java index cbf1dc13..ec5c818d 100644 --- a/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java +++ b/app/src/main/java/foundation/e/drive/database/SyncedFolderDAO.java @@ -167,7 +167,7 @@ class SyncedFolderDAO { } SyncedFolder getSyncedFolderByLocalPath(String localPath){ - final Cursor cursor = mDB.query(TABLE_NAME, allColumns, LOCAL_PATH+" like \""+localPath+ FileUtils.PATH_SEPARATOR+"\"", new String[0], null, null, null); + final Cursor cursor = mDB.query(TABLE_NAME, allColumns, LOCAL_PATH + " like \"" + localPath + FileUtils.PATH_SEPARATOR + "\"", new String[0], null, null, null); cursor.moveToFirst(); SyncedFolder result = null; if (!cursor.isAfterLast()) { -- GitLab