mirror of https://github.com/iSoron/uhabits.git
parent
9a72141567
commit
2bfbff9b14
@ -1,82 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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?)
|
||||
}
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Reference in new issue