mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-08 01:58:52 -06:00
Refactor DatabaseHelper; write tests for data import
This commit is contained in:
@@ -21,20 +21,25 @@ package org.isoron.uhabits;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.Context;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.activeandroid.ActiveAndroid;
|
||||
|
||||
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class HabitsApplication extends Application
|
||||
{
|
||||
@Nullable
|
||||
private static Context context;
|
||||
|
||||
private boolean isTestMode()
|
||||
public static boolean isTestMode()
|
||||
{
|
||||
try
|
||||
{
|
||||
getClassLoader().loadClass("org.isoron.uhabits.unit.models.HabitTest");
|
||||
if(context != null)
|
||||
context.getClassLoader().loadClass("org.isoron.uhabits.unit.models.HabitTest");
|
||||
return true;
|
||||
}
|
||||
catch (final Exception e)
|
||||
@@ -43,6 +48,7 @@ public class HabitsApplication extends Application
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Context getContext()
|
||||
{
|
||||
return context;
|
||||
@@ -54,15 +60,13 @@ public class HabitsApplication extends Application
|
||||
super.onCreate();
|
||||
HabitsApplication.context = this;
|
||||
|
||||
String databaseFilename = BuildConfig.databaseFilename;
|
||||
|
||||
if (isTestMode())
|
||||
{
|
||||
databaseFilename = "test.db";
|
||||
DatabaseHelper.deleteDatabase(this, databaseFilename);
|
||||
File db = DatabaseHelper.getDatabaseFile();
|
||||
if(db.exists()) db.delete();
|
||||
}
|
||||
|
||||
DatabaseHelper.initializeActiveAndroid(this, databaseFilename);
|
||||
DatabaseHelper.initializeActiveAndroid(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,3 +1,22 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||
*
|
||||
* 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.helpers;
|
||||
|
||||
import android.content.Context;
|
||||
@@ -8,6 +27,7 @@ import com.activeandroid.ActiveAndroid;
|
||||
import com.activeandroid.Configuration;
|
||||
|
||||
import org.isoron.uhabits.BuildConfig;
|
||||
import org.isoron.uhabits.HabitsApplication;
|
||||
import org.isoron.uhabits.models.Checkmark;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.models.Repetition;
|
||||
@@ -18,7 +38,8 @@ import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class DatabaseHelper
|
||||
@@ -27,11 +48,22 @@ public class DatabaseHelper
|
||||
{
|
||||
FileInputStream inStream = new FileInputStream(src);
|
||||
FileOutputStream outStream = new FileOutputStream(dst);
|
||||
FileChannel inChannel = inStream.getChannel();
|
||||
FileChannel outChannel = outStream.getChannel();
|
||||
inChannel.transferTo(0, inChannel.size(), outChannel);
|
||||
inStream.close();
|
||||
outStream.close();
|
||||
copy(inStream, outStream);
|
||||
}
|
||||
|
||||
public static void copy(InputStream inStream, File dst) throws IOException
|
||||
{
|
||||
FileOutputStream outStream = new FileOutputStream(dst);
|
||||
copy(inStream, outStream);
|
||||
}
|
||||
|
||||
public static void copy(InputStream in, OutputStream out) throws IOException
|
||||
{
|
||||
int numBytes;
|
||||
byte[] buffer = new byte[1024];
|
||||
|
||||
while ((numBytes = in.read(buffer)) != -1)
|
||||
out.write(buffer, 0, numBytes);
|
||||
}
|
||||
|
||||
public interface Command
|
||||
@@ -54,9 +86,9 @@ public class DatabaseHelper
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public static String saveDatabaseCopy(Context context, File dir) throws IOException
|
||||
public static String saveDatabaseCopy(File dir) throws IOException
|
||||
{
|
||||
File db = getDatabaseFile(context, BuildConfig.databaseFilename);
|
||||
File db = getDatabaseFile();
|
||||
|
||||
SimpleDateFormat dateFormat = DateHelper.getBackupDateFormat();
|
||||
String date = dateFormat.format(DateHelper.getLocalTime());
|
||||
@@ -67,17 +99,27 @@ public class DatabaseHelper
|
||||
return dbCopy.getAbsolutePath();
|
||||
}
|
||||
|
||||
public static void deleteDatabase(Context context, String databaseFilename)
|
||||
@NonNull
|
||||
public static File getDatabaseFile()
|
||||
{
|
||||
File db = getDatabaseFile(context, databaseFilename);
|
||||
if(db.exists()) db.delete();
|
||||
Context context = HabitsApplication.getContext();
|
||||
if(context == null) throw new RuntimeException("No application context found");
|
||||
|
||||
String databaseFilename = getDatabaseFilename();
|
||||
|
||||
return new File(String.format("%s/../databases/%s",
|
||||
context.getApplicationContext().getFilesDir().getPath(), databaseFilename));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static File getDatabaseFile(Context context, String databaseFilename)
|
||||
public static String getDatabaseFilename()
|
||||
{
|
||||
return new File(String.format("%s/../databases/%s",
|
||||
context.getApplicationContext().getFilesDir().getPath(), databaseFilename));
|
||||
String databaseFilename = BuildConfig.databaseFilename;
|
||||
|
||||
if (HabitsApplication.isTestMode())
|
||||
databaseFilename = "test.db";
|
||||
|
||||
return databaseFilename;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -93,10 +135,10 @@ public class DatabaseHelper
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void initializeActiveAndroid(Context context, String databaseFilename)
|
||||
public static void initializeActiveAndroid(Context context)
|
||||
{
|
||||
Configuration dbConfig = new Configuration.Builder(context)
|
||||
.setDatabaseName(databaseFilename)
|
||||
.setDatabaseName(getDatabaseFilename())
|
||||
.setDatabaseVersion(BuildConfig.databaseVersion)
|
||||
.addModelClasses(Checkmark.class, Habit.class, Repetition.class, Score.class,
|
||||
Streak.class)
|
||||
|
||||
@@ -57,12 +57,11 @@ public class LoopDBImporter extends AbstractImporter
|
||||
{
|
||||
ActiveAndroid.dispose();
|
||||
Context context = HabitsApplication.getContext();
|
||||
|
||||
File originalDB = DatabaseHelper.getDatabaseFile(context, BuildConfig.databaseFilename);
|
||||
File originalDB = DatabaseHelper.getDatabaseFile();
|
||||
File backupDir = DatabaseHelper.getFilesDir(context, "Backups");
|
||||
|
||||
DatabaseHelper.saveDatabaseCopy(context, backupDir);
|
||||
DatabaseHelper.saveDatabaseCopy(backupDir);
|
||||
DatabaseHelper.copy(file, originalDB);
|
||||
DatabaseHelper.initializeActiveAndroid(context, BuildConfig.databaseFilename);
|
||||
DatabaseHelper.initializeActiveAndroid(context);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public class ExportDBTask extends AsyncTask<Void, Void, Void>
|
||||
File dir = DatabaseHelper.getFilesDir(activity, "Backups");
|
||||
if(dir == null) return null;
|
||||
|
||||
filename = DatabaseHelper.saveDatabaseCopy(activity, dir);
|
||||
filename = DatabaseHelper.saveDatabaseCopy(dir);
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user