From 7fd4cdea1afbfa55c7b73f289d4977c0893a0bfa Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 26 Aug 2022 09:46:52 +0200 Subject: [PATCH 1/8] Implement database dump --- app/src/main/AndroidManifest.xml | 3 +- .../foundation/e/drive/database/DbHelper.java | 41 +++++++++++- .../e/drive/receivers/DebugCmdReceiver.java | 64 +++++++++++++++++++ .../e/drive/receivers/ForceSyncReceiver.java | 32 ---------- .../e/drive/services/ObserverService.java | 4 +- 5 files changed, 108 insertions(+), 36 deletions(-) create mode 100644 app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java delete mode 100644 app/src/main/java/foundation/e/drive/receivers/ForceSyncReceiver.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4b674380..9bbbf1bb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -104,11 +104,12 @@ + 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 0c84a757..5cc66a30 100644 --- a/app/src/main/java/foundation/e/drive/database/DbHelper.java +++ b/app/src/main/java/foundation/e/drive/database/DbHelper.java @@ -12,7 +12,16 @@ package foundation.e.drive.database; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.os.Build; +import android.os.FileUtils; import android.util.Log; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import foundation.e.drive.models.SyncedFolder; @@ -27,12 +36,13 @@ 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"; - + private Context context; /** * Constructor of the helper */ public DbHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); + this.context = context; } /** @@ -287,4 +297,33 @@ public final class DbHelper extends SQLiteOpenHelper { dao.close(); return result; } + + + /** + * Copy database file into user accessible directory for debuging purpose + * @return path to the dump or null if failure + */ + public String dumpDatabase() { + final File database = context.getDatabasePath(DbHelper.DATABASE_NAME); + if (database.exists()) { + try { + final InputStream src = new FileInputStream(database); + final File dstDir = context.getExternalFilesDir("DataBaseDump"); + if (!dstDir.exists()) dstDir.mkdir(); + final File dbDump = new File(dstDir, "dump-"+ System.currentTimeMillis()+".db"); + final OutputStream dst = new FileOutputStream(dbDump); + + byte[] buffer = new byte[1024]; + int read; + while ((read = src.read(buffer)) != -1) { + dst.write(buffer, 0, read); + } + return dbDump.getAbsolutePath(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } + } diff --git a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java new file mode 100644 index 00000000..3aade318 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java @@ -0,0 +1,64 @@ +/* + * Copyright © ECORP SAS 2021-2022. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + */ +package foundation.e.drive.receivers; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Build; +import android.os.FileUtils; +import android.os.IBinder; +import android.util.Log; + +import androidx.work.impl.utils.ForceStopRunnable; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import foundation.e.drive.database.DbHelper; +import foundation.e.drive.services.ObserverService; +import foundation.e.drive.services.SynchronizationService; +import foundation.e.drive.utils.AppConstants; +import foundation.e.drive.utils.CommonUtils; +import foundation.e.drive.utils.SynchronizationServiceConnection; + +/** + * @author Jonathan Klee + * @author Vincent Bourgmayer + */ +public class DebugCmdReceiver extends BroadcastReceiver { + + public static final String ACTION_FORCE_SYNC = "foundation.e.drive.action.FORCE_SYNC"; + public static final String ACTION_DUMP_DATABASE = "foundation.e.drive.action.DUMP_DATABASE"; + public static final String TAG = "DebugCmdReceiver"; + + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "onReceive"); + switch (intent.getAction()) { + case ACTION_FORCE_SYNC: + Log.i(TAG, "Force Sync intent received"); + context.startService(new Intent(ACTION_FORCE_SYNC, null, context, ObserverService.class)); + break; + case ACTION_DUMP_DATABASE: + final String dumpPath = new DbHelper(context).dumpDatabase(); + final String logMsg; + if (dumpPath != null) logMsg = "Database has been dump at "+dumpPath; + else logMsg = "Failed to dump Database"; + Log.i(TAG, logMsg); + break; + default: + break; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/foundation/e/drive/receivers/ForceSyncReceiver.java b/app/src/main/java/foundation/e/drive/receivers/ForceSyncReceiver.java deleted file mode 100644 index a9656fec..00000000 --- a/app/src/main/java/foundation/e/drive/receivers/ForceSyncReceiver.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright © ECORP SAS 2021-2022. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the GNU Public License v3.0 - * which accompanies this distribution, and is available at - * http://www.gnu.org/licenses/gpl.html - */ -package foundation.e.drive.receivers; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -import foundation.e.drive.services.ObserverService; - -/** - * @author Jonathan Klee - */ -public class ForceSyncReceiver extends BroadcastReceiver { - - public static final String ACTION_FORCE_SYNC = "foundation.e.drive.action.FORCE_SYNC"; - public static final String TAG = "ForceSyncReceiver"; - - @Override - public void onReceive(Context context, Intent intent) { - if (ACTION_FORCE_SYNC.equals(intent.getAction())) { - Log.i(TAG, "Start ObserverService"); - context.startService(new Intent(ACTION_FORCE_SYNC, null, context, ObserverService.class)); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/foundation/e/drive/services/ObserverService.java b/app/src/main/java/foundation/e/drive/services/ObserverService.java index 133abbab..e57f135e 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -43,7 +43,7 @@ import foundation.e.drive.models.SyncRequest; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.models.SyncedFileState; import foundation.e.drive.operations.ListFileRemoteOperation; -import foundation.e.drive.receivers.ForceSyncReceiver; +import foundation.e.drive.receivers.DebugCmdReceiver; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.DavClientProvider; @@ -128,7 +128,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene long currentTime = System.currentTimeMillis(); boolean forceSync = false; if (intent != null) { - forceSync = ForceSyncReceiver.ACTION_FORCE_SYNC.equals(intent.getAction()); + forceSync = DebugCmdReceiver.ACTION_FORCE_SYNC.equals(intent.getAction()); } //if time diff between current sync and last sync is higher or equal to delay minimum between two sync -- GitLab From 24c3c80cd8fc361ca5865b6e320ad64f0769c8a4 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 26 Aug 2022 10:43:45 +0200 Subject: [PATCH 2/8] update dump name to include version number and improve logging --- app/src/main/java/foundation/e/drive/database/DbHelper.java | 5 +++-- .../java/foundation/e/drive/receivers/DebugCmdReceiver.java | 6 ++---- 2 files changed, 5 insertions(+), 6 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 5cc66a30..dec79190 100644 --- a/app/src/main/java/foundation/e/drive/database/DbHelper.java +++ b/app/src/main/java/foundation/e/drive/database/DbHelper.java @@ -24,6 +24,8 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; + +import foundation.e.drive.BuildConfig; import foundation.e.drive.models.SyncedFolder; import foundation.e.drive.models.SyncedFileState; @@ -310,7 +312,7 @@ public final class DbHelper extends SQLiteOpenHelper { final InputStream src = new FileInputStream(database); final File dstDir = context.getExternalFilesDir("DataBaseDump"); if (!dstDir.exists()) dstDir.mkdir(); - final File dbDump = new File(dstDir, "dump-"+ System.currentTimeMillis()+".db"); + final File dbDump = new File(dstDir, "dump-" + BuildConfig.VERSION_NAME + "-" + System.currentTimeMillis() + ".db"); final OutputStream dst = new FileOutputStream(dbDump); byte[] buffer = new byte[1024]; @@ -325,5 +327,4 @@ public final class DbHelper extends SQLiteOpenHelper { } return null; } - } diff --git a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java index 3aade318..5a1c4be7 100644 --- a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java @@ -52,10 +52,8 @@ public class DebugCmdReceiver extends BroadcastReceiver { break; case ACTION_DUMP_DATABASE: final String dumpPath = new DbHelper(context).dumpDatabase(); - final String logMsg; - if (dumpPath != null) logMsg = "Database has been dump at "+dumpPath; - else logMsg = "Failed to dump Database"; - Log.i(TAG, logMsg); + if (dumpPath != null) Log.i(TAG, "Database has been dump at "+dumpPath); + else Log.e(TAG,"Failed to dump Database"); break; default: break; -- GitLab From db9e900000b3ed16e029aa5ef5ec00bae17350b7 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 26 Aug 2022 14:02:35 +0200 Subject: [PATCH 3/8] close in/out putStream when dumping DB --- app/src/main/java/foundation/e/drive/database/DbHelper.java | 2 ++ 1 file changed, 2 insertions(+) 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 dec79190..a1d3aa53 100644 --- a/app/src/main/java/foundation/e/drive/database/DbHelper.java +++ b/app/src/main/java/foundation/e/drive/database/DbHelper.java @@ -320,6 +320,8 @@ public final class DbHelper extends SQLiteOpenHelper { while ((read = src.read(buffer)) != -1) { dst.write(buffer, 0, read); } + src.close(); + dst.close(); return dbDump.getAbsolutePath(); } catch (IOException e) { e.printStackTrace(); -- GitLab From ea634a6013d586d0fe9e2ab44f059b8f83f50254 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Fri, 26 Aug 2022 14:14:11 +0200 Subject: [PATCH 4/8] Improve dump of database: use Try with to autoclose stream --- .../foundation/e/drive/database/DbHelper.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 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 a1d3aa53..337dc165 100644 --- a/app/src/main/java/foundation/e/drive/database/DbHelper.java +++ b/app/src/main/java/foundation/e/drive/database/DbHelper.java @@ -18,6 +18,7 @@ import android.util.Log; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -307,21 +308,18 @@ public final class DbHelper extends SQLiteOpenHelper { */ public String dumpDatabase() { final File database = context.getDatabasePath(DbHelper.DATABASE_NAME); - if (database.exists()) { - try { - final InputStream src = new FileInputStream(database); - final File dstDir = context.getExternalFilesDir("DataBaseDump"); - if (!dstDir.exists()) dstDir.mkdir(); - final File dbDump = new File(dstDir, "dump-" + BuildConfig.VERSION_NAME + "-" + System.currentTimeMillis() + ".db"); - final OutputStream dst = new FileOutputStream(dbDump); + final File dstDir = context.getExternalFilesDir("DataBaseDump"); + if (!dstDir.exists()) dstDir.mkdir(); + final File dbDump = new File(dstDir, "dump-" + BuildConfig.VERSION_NAME + "-" + System.currentTimeMillis() + ".db"); + if (database.exists()) { + try ( final InputStream src = new FileInputStream(database); + final OutputStream dst = new FileOutputStream(dbDump)) { byte[] buffer = new byte[1024]; int read; while ((read = src.read(buffer)) != -1) { dst.write(buffer, 0, read); } - src.close(); - dst.close(); return dbDump.getAbsolutePath(); } catch (IOException e) { e.printStackTrace(); -- GitLab From 0b4fa929ce866f592050b898f0ff3ae184132fa1 Mon Sep 17 00:00:00 2001 From: Abhishek Aggarwal Date: Fri, 26 Aug 2022 13:53:57 +0000 Subject: [PATCH 5/8] Apply 1 suggestion(s) to 1 file(s) --- .../java/foundation/e/drive/receivers/DebugCmdReceiver.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java index 5a1c4be7..c19c2055 100644 --- a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java @@ -52,7 +52,7 @@ public class DebugCmdReceiver extends BroadcastReceiver { break; case ACTION_DUMP_DATABASE: final String dumpPath = new DbHelper(context).dumpDatabase(); - if (dumpPath != null) Log.i(TAG, "Database has been dump at "+dumpPath); + if (dumpPath != null) Log.i(TAG, "Database has been dump at "+ dumpPath); else Log.e(TAG,"Failed to dump Database"); break; default: -- GitLab From fd32b095cda47091dce6b2e42a4767c260dfb4f3 Mon Sep 17 00:00:00 2001 From: Fahim Salam Chowdhury Date: Mon, 29 Aug 2022 06:59:01 +0000 Subject: [PATCH 6/8] Apply 1 suggestion(s) to 1 file(s) --- app/src/main/java/foundation/e/drive/database/DbHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 337dc165..15ca2bb8 100644 --- a/app/src/main/java/foundation/e/drive/database/DbHelper.java +++ b/app/src/main/java/foundation/e/drive/database/DbHelper.java @@ -39,7 +39,7 @@ 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"; - private Context context; + private final Context context; /** * Constructor of the helper */ -- GitLab From 61f1ced0cb623d687884533b88f077803c4342c4 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Mon, 29 Aug 2022 10:02:46 +0200 Subject: [PATCH 7/8] Refactor DbHelper.dumpDatabase() - Set Method static - Add Context as parameter - Remove Context class field from DBHelper - Replace exception.printStackTrace by Log.e method - Move Logging from Receiver to dumpDatabase() method - remove Returning String from the method --- .../foundation/e/drive/database/DbHelper.java | 15 ++++++------- .../e/drive/receivers/DebugCmdReceiver.java | 21 +------------------ 2 files changed, 7 insertions(+), 29 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 15ca2bb8..58d74783 100644 --- a/app/src/main/java/foundation/e/drive/database/DbHelper.java +++ b/app/src/main/java/foundation/e/drive/database/DbHelper.java @@ -12,13 +12,10 @@ package foundation.e.drive.database; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; -import android.os.Build; -import android.os.FileUtils; import android.util.Log; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -39,13 +36,12 @@ 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"; - private final Context context; + /** * Constructor of the helper */ public DbHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); - this.context = context; } /** @@ -306,7 +302,7 @@ public final class DbHelper extends SQLiteOpenHelper { * Copy database file into user accessible directory for debuging purpose * @return path to the dump or null if failure */ - public String dumpDatabase() { + public static void dumpDatabase(Context context) { final File database = context.getDatabasePath(DbHelper.DATABASE_NAME); final File dstDir = context.getExternalFilesDir("DataBaseDump"); if (!dstDir.exists()) dstDir.mkdir(); @@ -320,11 +316,12 @@ public final class DbHelper extends SQLiteOpenHelper { while ((read = src.read(buffer)) != -1) { dst.write(buffer, 0, read); } - return dbDump.getAbsolutePath(); + Log.i(TAG, "Database has been dump at "+ dbDump.getAbsolutePath()); + return; } catch (IOException e) { - e.printStackTrace(); + Log.e(TAG, "IOException", e); } } - return null; + Log.e(TAG,"Failed to dump Database"); } } diff --git a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java index c19c2055..79809dd2 100644 --- a/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/DebugCmdReceiver.java @@ -10,27 +10,10 @@ package foundation.e.drive.receivers; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.os.Build; -import android.os.FileUtils; -import android.os.IBinder; import android.util.Log; -import androidx.work.impl.utils.ForceStopRunnable; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - import foundation.e.drive.database.DbHelper; import foundation.e.drive.services.ObserverService; -import foundation.e.drive.services.SynchronizationService; -import foundation.e.drive.utils.AppConstants; -import foundation.e.drive.utils.CommonUtils; -import foundation.e.drive.utils.SynchronizationServiceConnection; /** * @author Jonathan Klee @@ -51,9 +34,7 @@ public class DebugCmdReceiver extends BroadcastReceiver { context.startService(new Intent(ACTION_FORCE_SYNC, null, context, ObserverService.class)); break; case ACTION_DUMP_DATABASE: - final String dumpPath = new DbHelper(context).dumpDatabase(); - if (dumpPath != null) Log.i(TAG, "Database has been dump at "+ dumpPath); - else Log.e(TAG,"Failed to dump Database"); + DbHelper.dumpDatabase(context); break; default: break; -- GitLab From c9caf2ab5eddee4627bc31b181d15643f297c4dd Mon Sep 17 00:00:00 2001 From: Abhishek Aggarwal Date: Mon, 29 Aug 2022 12:02:11 +0000 Subject: [PATCH 8/8] Apply 1 suggestion(s) to 1 file(s) --- app/src/main/java/foundation/e/drive/database/DbHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 58d74783..ca30b84f 100644 --- a/app/src/main/java/foundation/e/drive/database/DbHelper.java +++ b/app/src/main/java/foundation/e/drive/database/DbHelper.java @@ -309,8 +309,8 @@ public final class DbHelper extends SQLiteOpenHelper { final File dbDump = new File(dstDir, "dump-" + BuildConfig.VERSION_NAME + "-" + System.currentTimeMillis() + ".db"); if (database.exists()) { - try ( final InputStream src = new FileInputStream(database); - final OutputStream dst = new FileOutputStream(dbDump)) { + try (final InputStream src = new FileInputStream(database); + final OutputStream dst = new FileOutputStream(dbDump)) { byte[] buffer = new byte[1024]; int read; while ((read = src.read(buffer)) != -1) { -- GitLab