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

Commit b5b010a6 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

Implement database dump

parent a3ca2918
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -104,11 +104,12 @@
            </intent-filter>
        </receiver>
        <receiver
            android:name=".receivers.ForceSyncReceiver"
            android:name=".receivers.DebugCmdReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="foundation.e.drive.action.FORCE_SYNC" />
                <action android:name="foundation.e.drive.action.DUMP_DATABASE"/>
            </intent-filter>
        </receiver>
    </application>
+37 −0
Original line number Diff line number Diff line
@@ -13,8 +13,17 @@ import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
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.BuildConfig;
import foundation.e.drive.models.SyncedFolder;
import foundation.e.drive.models.SyncedFileState;

@@ -287,4 +296,32 @@ 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 static void dumpDatabase(Context context) {
        final File database = context.getDatabasePath(DbHelper.DATABASE_NAME);
        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);
                }
                Log.i(TAG,  "Database has been dump at "+ dbDump.getAbsolutePath());
                return;
            } catch (IOException e) {
                Log.e(TAG, "IOException", e);
            }
        }
        Log.e(TAG,"Failed to dump Database");
    }
}
+43 −0
Original line number Diff line number Diff line
@@ -12,21 +12,32 @@ import android.content.Context;
import android.content.Intent;
import android.util.Log;

import foundation.e.drive.database.DbHelper;
import foundation.e.drive.services.ObserverService;

/**
 * @author Jonathan Klee
 * @author Vincent Bourgmayer
 */
public class ForceSyncReceiver extends BroadcastReceiver {
public class DebugCmdReceiver extends BroadcastReceiver {

    public static final String ACTION_FORCE_SYNC = "foundation.e.drive.action.FORCE_SYNC";
    public static final String TAG = "ForceSyncReceiver";
    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) {
        if (ACTION_FORCE_SYNC.equals(intent.getAction())) {
            Log.i(TAG, "Start ObserverService");
        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:
                DbHelper.dumpDatabase(context);
                break;
            default:
                break;
        }
    }
}
 No newline at end of file
+2 −2
Original line number Diff line number Diff line
@@ -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