diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/tasks/AndroidTaskRunner.java b/uhabits-android/src/main/java/org/isoron/uhabits/tasks/AndroidTaskRunner.java index 654e9bdef..f1348cec5 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/tasks/AndroidTaskRunner.java +++ b/uhabits-android/src/main/java/org/isoron/uhabits/tasks/AndroidTaskRunner.java @@ -36,7 +36,7 @@ public class AndroidTaskRunner implements TaskRunner private final HashMap taskToAsyncTask; - private LinkedList listeners; + private final LinkedList listeners; public AndroidTaskRunner() { diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ExportDBTask.java b/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ExportDBTask.java deleted file mode 100644 index f2082d4c9..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ExportDBTask.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (C) 2016-2021 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.tasks; - -import android.content.*; - -import androidx.annotation.*; - -import org.isoron.uhabits.*; -import org.isoron.uhabits.core.tasks.*; -import org.isoron.uhabits.inject.*; -import org.isoron.uhabits.utils.*; - -import java.io.*; - -public class ExportDBTask implements Task -{ - private String filename; - - @NonNull - private Context context; - - private AndroidDirFinder system; - - @NonNull - private final Listener listener; - - public ExportDBTask(@AppContext @NonNull Context context, - @NonNull AndroidDirFinder system, - @NonNull Listener listener) - { - this.system = system; - this.listener = listener; - this.context = context; - } - - @Override - public void doInBackground() - { - filename = null; - - try - { - File dir = system.getFilesDir("Backups"); - if (dir == null) return; - - filename = DatabaseUtils.saveDatabaseCopy(context, dir); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } - - @Override - public void onPostExecute() - { - listener.onExportDBFinished(filename); - } - - public interface Listener - { - void onExportDBFinished(@Nullable String filename); - } -} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ExportDBTask.kt b/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ExportDBTask.kt new file mode 100644 index 000000000..45db42a76 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ExportDBTask.kt @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package org.isoron.uhabits.tasks + +import android.content.Context +import org.isoron.uhabits.AndroidDirFinder +import org.isoron.uhabits.core.tasks.Task +import org.isoron.uhabits.inject.AppContext +import org.isoron.uhabits.utils.DatabaseUtils.saveDatabaseCopy +import java.io.IOException + +class ExportDBTask( + @param:AppContext private val context: Context, + private val system: AndroidDirFinder, + private val listener: Listener +) : Task { + private var filename: String? = null + override fun doInBackground() { + filename = null + filename = try { + val dir = system.getFilesDir("Backups") ?: return + saveDatabaseCopy(context, dir) + } catch (e: IOException) { + throw RuntimeException(e) + } + } + + override fun onPostExecute() { + listener.onExportDBFinished(filename) + } + + fun interface Listener { + fun onExportDBFinished(filename: String?) + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ImportDataTask.java b/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ImportDataTask.java deleted file mode 100644 index 98ee48eff..000000000 --- a/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ImportDataTask.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2016-2021 Álinson Santos Xavier - * - * This file is part of Loop Habit Tracker. - * - * Loop Habit Tracker is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * Loop Habit Tracker is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -package org.isoron.uhabits.tasks; - -import android.util.*; - -import androidx.annotation.NonNull; - -import org.isoron.uhabits.core.io.*; -import org.isoron.uhabits.core.models.ModelFactory; -import org.isoron.uhabits.core.models.sqlite.SQLModelFactory; -import org.isoron.uhabits.core.tasks.*; - -import java.io.*; - -public class ImportDataTask implements Task -{ - public static final int FAILED = 3; - - public static final int NOT_RECOGNIZED = 2; - - public static final int SUCCESS = 1; - - private int result; - - @NonNull - private final File file; - - private GenericImporter importer; - - private SQLModelFactory modelFactory; - - @NonNull - private final Listener listener; - - public ImportDataTask(@NonNull GenericImporter importer, - @NonNull ModelFactory modelFactory, - @NonNull File file, - @NonNull Listener listener) - { - this.importer = importer; - this.modelFactory = (SQLModelFactory) modelFactory; - this.listener = listener; - this.file = file; - } - - @Override - public void doInBackground() - { - modelFactory.getDatabase().beginTransaction(); - - try - { - if (importer.canHandle(file)) - { - importer.importHabitsFromFile(file); - result = SUCCESS; - modelFactory.getDatabase().setTransactionSuccessful(); - } - else - { - result = NOT_RECOGNIZED; - } - } - catch (Exception e) - { - result = FAILED; - Log.e("ImportDataTask", "Import failed", e); - } - - modelFactory.getDatabase().endTransaction(); - } - - @Override - public void onPostExecute() - { - listener.onImportDataFinished(result); - } - - public interface Listener - { - void onImportDataFinished(int result); - } -} \ No newline at end of file diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ImportDataTask.kt b/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ImportDataTask.kt new file mode 100644 index 000000000..6f804233a --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/tasks/ImportDataTask.kt @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +package org.isoron.uhabits.tasks + +import android.util.Log +import org.isoron.uhabits.core.io.GenericImporter +import org.isoron.uhabits.core.models.ModelFactory +import org.isoron.uhabits.core.models.sqlite.SQLModelFactory +import org.isoron.uhabits.core.tasks.Task +import java.io.File + +class ImportDataTask( + private val importer: GenericImporter, + modelFactory: ModelFactory, + private val file: File, + private val listener: Listener +) : Task { + private var result = 0 + private val modelFactory: SQLModelFactory = modelFactory as SQLModelFactory + override fun doInBackground() { + modelFactory.database.beginTransaction() + try { + if (importer.canHandle(file)) { + importer.importHabitsFromFile(file) + result = SUCCESS + modelFactory.database.setTransactionSuccessful() + } else { + result = NOT_RECOGNIZED + } + } catch (e: Exception) { + result = FAILED + Log.e("ImportDataTask", "Import failed", e) + } + modelFactory.database.endTransaction() + } + + override fun onPostExecute() { + listener.onImportDataFinished(result) + } + + fun interface Listener { + fun onImportDataFinished(result: Int) + } + + companion object { + const val FAILED = 3 + const val NOT_RECOGNIZED = 2 + const val SUCCESS = 1 + } +}