mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 17:18:52 -06:00
Move importers to uhabits-core
This commit is contained in:
@@ -10,11 +10,6 @@ android {
|
|||||||
applicationId "org.isoron.uhabits"
|
applicationId "org.isoron.uhabits"
|
||||||
minSdkVersion 19
|
minSdkVersion 19
|
||||||
targetSdkVersion 25
|
targetSdkVersion 25
|
||||||
|
|
||||||
buildConfigField "Integer", "databaseVersion", "21"
|
|
||||||
buildConfigField "String", "databaseFilename", "\"uhabits.db\""
|
|
||||||
buildConfigField "int", "roboSdk", (System.getenv("ROBO_SDK") ?: "25")
|
|
||||||
|
|
||||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 48 KiB |
@@ -24,6 +24,7 @@ import android.content.*;
|
|||||||
import org.isoron.androidbase.*;
|
import org.isoron.androidbase.*;
|
||||||
import org.isoron.uhabits.core.*;
|
import org.isoron.uhabits.core.*;
|
||||||
import org.isoron.uhabits.core.commands.*;
|
import org.isoron.uhabits.core.commands.*;
|
||||||
|
import org.isoron.uhabits.core.io.*;
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.isoron.uhabits.core.preferences.*;
|
import org.isoron.uhabits.core.preferences.*;
|
||||||
import org.isoron.uhabits.core.reminders.*;
|
import org.isoron.uhabits.core.reminders.*;
|
||||||
@@ -32,7 +33,6 @@ import org.isoron.uhabits.core.ui.*;
|
|||||||
import org.isoron.uhabits.core.ui.screens.habits.list.*;
|
import org.isoron.uhabits.core.ui.screens.habits.list.*;
|
||||||
import org.isoron.uhabits.core.utils.*;
|
import org.isoron.uhabits.core.utils.*;
|
||||||
import org.isoron.uhabits.intents.*;
|
import org.isoron.uhabits.intents.*;
|
||||||
import org.isoron.uhabits.io.*;
|
|
||||||
import org.isoron.uhabits.sync.*;
|
import org.isoron.uhabits.sync.*;
|
||||||
import org.isoron.uhabits.tasks.*;
|
import org.isoron.uhabits.tasks.*;
|
||||||
import org.isoron.uhabits.widgets.*;
|
import org.isoron.uhabits.widgets.*;
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ package org.isoron.uhabits;
|
|||||||
|
|
||||||
import org.isoron.uhabits.core.*;
|
import org.isoron.uhabits.core.*;
|
||||||
import org.isoron.uhabits.core.commands.*;
|
import org.isoron.uhabits.core.commands.*;
|
||||||
|
import org.isoron.uhabits.core.database.*;
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.isoron.uhabits.core.models.sqlite.*;
|
import org.isoron.uhabits.core.models.sqlite.*;
|
||||||
import org.isoron.uhabits.core.preferences.*;
|
import org.isoron.uhabits.core.preferences.*;
|
||||||
@@ -86,5 +87,12 @@ public class HabitsModule
|
|||||||
{
|
{
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@AppScope
|
||||||
|
public DatabaseOpener getDatabaseOpener(AndroidDatabaseOpener opener)
|
||||||
|
{
|
||||||
|
return opener;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,6 +67,18 @@ public class AndroidDatabase implements Database
|
|||||||
db.endTransaction();
|
db.endTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close()
|
||||||
|
{
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getVersion()
|
||||||
|
{
|
||||||
|
return db.getVersion();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int update(String tableName,
|
public int update(String tableName,
|
||||||
Map<String, Object> map,
|
Map<String, Object> map,
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Á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.database;
|
||||||
|
|
||||||
|
import android.content.*;
|
||||||
|
import android.database.sqlite.*;
|
||||||
|
import android.support.annotation.*;
|
||||||
|
|
||||||
|
import org.isoron.androidbase.*;
|
||||||
|
import org.isoron.uhabits.core.database.*;
|
||||||
|
import org.isoron.uhabits.utils.*;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
import javax.inject.*;
|
||||||
|
|
||||||
|
public class AndroidDatabaseOpener implements DatabaseOpener
|
||||||
|
{
|
||||||
|
private Context context;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public AndroidDatabaseOpener(@NonNull @AppContext Context context)
|
||||||
|
{
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Database open(@NonNull File file)
|
||||||
|
{
|
||||||
|
return new AndroidDatabase(
|
||||||
|
SQLiteDatabase.openDatabase(file.getAbsolutePath(), null,
|
||||||
|
SQLiteDatabase.OPEN_READWRITE));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getProductionDatabaseFile()
|
||||||
|
{
|
||||||
|
return DatabaseUtils.getDatabaseFile(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,8 +23,8 @@ import android.support.annotation.*;
|
|||||||
|
|
||||||
import com.google.auto.factory.*;
|
import com.google.auto.factory.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.core.io.*;
|
||||||
import org.isoron.uhabits.core.tasks.*;
|
import org.isoron.uhabits.core.tasks.*;
|
||||||
import org.isoron.uhabits.io.*;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
|
|||||||
@@ -25,11 +25,14 @@ import android.support.annotation.*;
|
|||||||
|
|
||||||
import org.isoron.androidbase.utils.*;
|
import org.isoron.androidbase.utils.*;
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.core.*;
|
||||||
import org.isoron.uhabits.core.utils.*;
|
import org.isoron.uhabits.core.utils.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.text.*;
|
import java.text.*;
|
||||||
|
|
||||||
|
import static org.isoron.uhabits.core.Config.DATABASE_VERSION;
|
||||||
|
|
||||||
public abstract class DatabaseUtils
|
public abstract class DatabaseUtils
|
||||||
{
|
{
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -72,7 +75,7 @@ public abstract class DatabaseUtils
|
|||||||
@NonNull
|
@NonNull
|
||||||
public static String getDatabaseFilename()
|
public static String getDatabaseFilename()
|
||||||
{
|
{
|
||||||
String databaseFilename = BuildConfig.databaseFilename;
|
String databaseFilename = Config.DATABASE_FILENAME;
|
||||||
if (HabitsApplication.isTestMode()) databaseFilename = "test.db";
|
if (HabitsApplication.isTestMode()) databaseFilename = "test.db";
|
||||||
return databaseFilename;
|
return databaseFilename;
|
||||||
}
|
}
|
||||||
@@ -81,7 +84,7 @@ public abstract class DatabaseUtils
|
|||||||
public static void initializeDatabase(Context context)
|
public static void initializeDatabase(Context context)
|
||||||
{
|
{
|
||||||
opener = new HabitsDatabaseOpener(context, getDatabaseFilename(),
|
opener = new HabitsDatabaseOpener(context, getDatabaseFilename(),
|
||||||
BuildConfig.databaseVersion);
|
DATABASE_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ dependencies {
|
|||||||
implementation 'com.android.support:support-annotations:25.3.1'
|
implementation 'com.android.support:support-annotations:25.3.1'
|
||||||
implementation 'com.google.code.findbugs:jsr305:3.0.2'
|
implementation 'com.google.code.findbugs:jsr305:3.0.2'
|
||||||
implementation 'org.apache.commons:commons-lang3:3.5'
|
implementation 'org.apache.commons:commons-lang3:3.5'
|
||||||
|
implementation 'org.apache.commons:commons-io:1.3.2'
|
||||||
implementation 'com.google.code.gson:gson:2.7'
|
implementation 'com.google.code.gson:gson:2.7'
|
||||||
|
|
||||||
testImplementation 'junit:junit:4+'
|
testImplementation 'junit:junit:4+'
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Á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.core;
|
||||||
|
|
||||||
|
public class Config
|
||||||
|
{
|
||||||
|
public static final String DATABASE_FILENAME = "uhabits.db";
|
||||||
|
public static int DATABASE_VERSION = 21;
|
||||||
|
}
|
||||||
@@ -41,4 +41,8 @@ public interface Database
|
|||||||
void setTransactionSuccessful();
|
void setTransactionSuccessful();
|
||||||
|
|
||||||
void endTransaction();
|
void endTransaction();
|
||||||
|
|
||||||
|
void close();
|
||||||
|
|
||||||
|
int getVersion();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 Á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.core.database;
|
||||||
|
|
||||||
|
import android.support.annotation.*;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public interface DatabaseOpener
|
||||||
|
{
|
||||||
|
Database open(@NonNull File file);
|
||||||
|
|
||||||
|
File getProductionDatabaseFile();
|
||||||
|
}
|
||||||
@@ -180,4 +180,27 @@ public class JdbcDatabase implements Database
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
catch (SQLException e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getVersion()
|
||||||
|
{
|
||||||
|
try (Cursor c = select("PRAGMA user_version"))
|
||||||
|
{
|
||||||
|
c.moveToNext();
|
||||||
|
return c.getInt(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
*
|
*
|
||||||
* This file is part of Loop Habit Tracker.
|
* This file is part of Loop Habit Tracker.
|
||||||
*
|
*
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.isoron.uhabits.io;
|
package org.isoron.uhabits.core.io;
|
||||||
|
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
|
|
||||||
@@ -50,7 +50,6 @@ public abstract class AbstractImporter
|
|||||||
byte[] sqliteHeader = "SQLite format 3".getBytes();
|
byte[] sqliteHeader = "SQLite format 3".getBytes();
|
||||||
byte[] buffer = new byte[sqliteHeader.length];
|
byte[] buffer = new byte[sqliteHeader.length];
|
||||||
|
|
||||||
|
|
||||||
int count = fis.read(buffer);
|
int count = fis.read(buffer);
|
||||||
if(count < sqliteHeader.length) return false;
|
if(count < sqliteHeader.length) return false;
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
*
|
*
|
||||||
* This file is part of Loop Habit Tracker.
|
* This file is part of Loop Habit Tracker.
|
||||||
*
|
*
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.isoron.uhabits.io;
|
package org.isoron.uhabits.core.io;
|
||||||
|
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
*
|
*
|
||||||
* This file is part of Loop Habit Tracker.
|
* This file is part of Loop Habit Tracker.
|
||||||
*
|
*
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.isoron.uhabits.io;
|
package org.isoron.uhabits.core.io;
|
||||||
|
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
|
|
||||||
@@ -31,7 +31,6 @@ import java.util.*;
|
|||||||
|
|
||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
|
|
||||||
import static org.isoron.uhabits.utils.DatabaseUtils.executeAsTransaction;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that imports data from HabitBull CSV files.
|
* Class that imports data from HabitBull CSV files.
|
||||||
@@ -53,25 +52,20 @@ public class HabitBullCSVImporter extends AbstractImporter
|
|||||||
{
|
{
|
||||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||||
String line = reader.readLine();
|
String line = reader.readLine();
|
||||||
|
|
||||||
return line.startsWith("HabitName,HabitDescription,HabitCategory");
|
return line.startsWith("HabitName,HabitDescription,HabitCategory");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void importHabitsFromFile(@NonNull final File file) throws IOException
|
public void importHabitsFromFile(@NonNull final File file)
|
||||||
{
|
throws IOException
|
||||||
executeAsTransaction(() -> parseFile(file));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void parseFile(@NonNull File file) throws IOException
|
|
||||||
{
|
{
|
||||||
CSVReader reader = new CSVReader(new FileReader(file));
|
CSVReader reader = new CSVReader(new FileReader(file));
|
||||||
HashMap<String, Habit> map = new HashMap<>();
|
HashMap<String, Habit> map = new HashMap<>();
|
||||||
|
|
||||||
for(String line[] : reader)
|
for (String line[] : reader)
|
||||||
{
|
{
|
||||||
String name = line[0];
|
String name = line[0];
|
||||||
if(name.equals("HabitName")) continue;
|
if (name.equals("HabitName")) continue;
|
||||||
|
|
||||||
String description = line[1];
|
String description = line[1];
|
||||||
String dateString[] = line[3].split("-");
|
String dateString[] = line[3].split("-");
|
||||||
@@ -85,11 +79,11 @@ public class HabitBullCSVImporter extends AbstractImporter
|
|||||||
long timestamp = date.getTimeInMillis();
|
long timestamp = date.getTimeInMillis();
|
||||||
|
|
||||||
int value = Integer.parseInt(line[4]);
|
int value = Integer.parseInt(line[4]);
|
||||||
if(value != 1) continue;
|
if (value != 1) continue;
|
||||||
|
|
||||||
Habit h = map.get(name);
|
Habit h = map.get(name);
|
||||||
|
|
||||||
if(h == null)
|
if (h == null)
|
||||||
{
|
{
|
||||||
h = modelFactory.buildHabit();
|
h = modelFactory.buildHabit();
|
||||||
h.setName(name);
|
h.setName(name);
|
||||||
@@ -99,7 +93,7 @@ public class HabitBullCSVImporter extends AbstractImporter
|
|||||||
map.put(name, h);
|
map.put(name, h);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!h.getRepetitions().containsTimestamp(timestamp))
|
if (!h.getRepetitions().containsTimestamp(timestamp))
|
||||||
h.getRepetitions().toggle(timestamp);
|
h.getRepetitions().toggle(timestamp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
*
|
*
|
||||||
* This file is part of Loop Habit Tracker.
|
* This file is part of Loop Habit Tracker.
|
||||||
*
|
*
|
||||||
@@ -17,38 +17,34 @@
|
|||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.isoron.uhabits.io;
|
package org.isoron.uhabits.core.io;
|
||||||
|
|
||||||
import android.content.*;
|
|
||||||
import android.database.*;
|
|
||||||
import android.database.sqlite.*;
|
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
import android.util.*;
|
|
||||||
|
|
||||||
import org.isoron.androidbase.*;
|
import org.apache.commons.io.*;
|
||||||
import org.isoron.androidbase.utils.*;
|
import org.isoron.uhabits.core.database.*;
|
||||||
import org.isoron.uhabits.BuildConfig;
|
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.isoron.uhabits.utils.DatabaseUtils;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
|
|
||||||
|
import static org.isoron.uhabits.core.Config.DATABASE_VERSION;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that imports data from database files exported by Loop Habit Tracker.
|
* Class that imports data from database files exported by Loop Habit Tracker.
|
||||||
*/
|
*/
|
||||||
public class LoopDBImporter extends AbstractImporter
|
public class LoopDBImporter extends AbstractImporter
|
||||||
{
|
{
|
||||||
@NonNull
|
@NonNull
|
||||||
private Context context;
|
private final DatabaseOpener opener;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public LoopDBImporter(@NonNull @AppContext Context context,
|
public LoopDBImporter(@NonNull HabitList habits,
|
||||||
@NonNull HabitList habits)
|
@NonNull DatabaseOpener opener)
|
||||||
{
|
{
|
||||||
super(habits);
|
super(habits);
|
||||||
this.context = context;
|
this.opener = opener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -56,26 +52,23 @@ public class LoopDBImporter extends AbstractImporter
|
|||||||
{
|
{
|
||||||
if (!isSQLite3File(file)) return false;
|
if (!isSQLite3File(file)) return false;
|
||||||
|
|
||||||
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
Database db = opener.open(file);
|
||||||
SQLiteDatabase.OPEN_READONLY);
|
|
||||||
|
|
||||||
boolean canHandle = true;
|
boolean canHandle = true;
|
||||||
|
|
||||||
Cursor c = db.rawQuery(
|
Cursor c = db.select("select count(*) from SQLITE_MASTER " +
|
||||||
"select count(*) from SQLITE_MASTER where name=? or name=?",
|
"where name='Checkmarks' or name='Repetitions'");
|
||||||
new String[]{ "Checkmarks", "Repetitions" });
|
|
||||||
|
|
||||||
if (!c.moveToFirst() || c.getInt(0) != 2)
|
if (!c.moveToNext() || c.getInt(0) != 2)
|
||||||
{
|
{
|
||||||
Log.w("LoopDBImporter", "Cannot handle file: tables not found");
|
// Log.w("LoopDBImporter", "Cannot handle file: tables not found");
|
||||||
canHandle = false;
|
canHandle = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (db.getVersion() > BuildConfig.databaseVersion)
|
if (db.getVersion() > DATABASE_VERSION)
|
||||||
{
|
{
|
||||||
Log.w("LoopDBImporter", String.format(
|
// Log.w("LoopDBImporter", String.format(
|
||||||
"Cannot handle file: incompatible version: %d > %d",
|
// "Cannot handle file: incompatible version: %d > %d",
|
||||||
db.getVersion(), BuildConfig.databaseVersion));
|
// db.getVersion(), DATABASE_VERSION));
|
||||||
canHandle = false;
|
canHandle = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,9 +80,9 @@ public class LoopDBImporter extends AbstractImporter
|
|||||||
@Override
|
@Override
|
||||||
public void importHabitsFromFile(@NonNull File file) throws IOException
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
{
|
{
|
||||||
DatabaseUtils.dispose();
|
// DatabaseUtils.dispose();
|
||||||
File originalDB = DatabaseUtils.getDatabaseFile(context);
|
File originalDB = opener.getProductionDatabaseFile();
|
||||||
FileUtils.copy(file, originalDB);
|
FileUtils.copyFile(file, originalDB);
|
||||||
DatabaseUtils.initializeDatabase(context);
|
// DatabaseUtils.initializeDatabase(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
*
|
*
|
||||||
* This file is part of Loop Habit Tracker.
|
* This file is part of Loop Habit Tracker.
|
||||||
*
|
*
|
||||||
@@ -17,14 +17,12 @@
|
|||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.isoron.uhabits.io;
|
package org.isoron.uhabits.core.io;
|
||||||
|
|
||||||
import android.database.*;
|
|
||||||
import android.database.sqlite.*;
|
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.core.database.*;
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.isoron.uhabits.utils.DatabaseUtils;
|
|
||||||
import org.isoron.uhabits.core.utils.*;
|
import org.isoron.uhabits.core.utils.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -32,8 +30,6 @@ import java.util.*;
|
|||||||
|
|
||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
|
|
||||||
import static android.database.sqlite.SQLiteDatabase.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class that imports database files exported by Rewire.
|
* Class that imports database files exported by Rewire.
|
||||||
*/
|
*/
|
||||||
@@ -41,12 +37,17 @@ public class RewireDBImporter extends AbstractImporter
|
|||||||
{
|
{
|
||||||
private ModelFactory modelFactory;
|
private ModelFactory modelFactory;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final DatabaseOpener opener;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public RewireDBImporter(@NonNull HabitList habits,
|
public RewireDBImporter(@NonNull HabitList habits,
|
||||||
@NonNull ModelFactory modelFactory)
|
@NonNull ModelFactory modelFactory,
|
||||||
|
@NonNull DatabaseOpener opener)
|
||||||
{
|
{
|
||||||
super(habits);
|
super(habits);
|
||||||
this.modelFactory = modelFactory;
|
this.modelFactory = modelFactory;
|
||||||
|
this.opener = opener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -54,13 +55,11 @@ public class RewireDBImporter extends AbstractImporter
|
|||||||
{
|
{
|
||||||
if (!isSQLite3File(file)) return false;
|
if (!isSQLite3File(file)) return false;
|
||||||
|
|
||||||
SQLiteDatabase db = openDatabase(file.getPath(), null, OPEN_READONLY);
|
Database db = opener.open(file);
|
||||||
|
Cursor c = db.select("select count(*) from SQLITE_MASTER " +
|
||||||
|
"where name='CHECKINS' or name='UNIT'");
|
||||||
|
|
||||||
Cursor c = db.rawQuery(
|
boolean result = (c.moveToNext() && c.getInt(0) == 2);
|
||||||
"select count(*) from SQLITE_MASTER where name=? or name=?",
|
|
||||||
new String[]{ "CHECKINS", "UNIT" });
|
|
||||||
|
|
||||||
boolean result = (c.moveToFirst() && c.getInt(0) == 2);
|
|
||||||
|
|
||||||
c.close();
|
c.close();
|
||||||
db.close();
|
db.close();
|
||||||
@@ -70,56 +69,24 @@ public class RewireDBImporter extends AbstractImporter
|
|||||||
@Override
|
@Override
|
||||||
public void importHabitsFromFile(@NonNull File file) throws IOException
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
{
|
{
|
||||||
String path = file.getPath();
|
Database db = opener.open(file);
|
||||||
final SQLiteDatabase db = openDatabase(path, null, OPEN_READONLY);
|
db.beginTransaction();
|
||||||
|
createHabits(db);
|
||||||
DatabaseUtils.executeAsTransaction(() -> createHabits(db));
|
db.setTransactionSuccessful();
|
||||||
|
db.endTransaction();
|
||||||
db.close();
|
db.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createCheckmarks(@NonNull SQLiteDatabase db,
|
private void createHabits(Database db)
|
||||||
@NonNull Habit habit,
|
|
||||||
int rewireHabitId)
|
|
||||||
{
|
{
|
||||||
Cursor c = null;
|
Cursor c = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
String[] params = { Integer.toString(rewireHabitId) };
|
c = db.select("select _id, name, description, schedule, " +
|
||||||
c = db.rawQuery(
|
"active_days, repeating_count, days, period " +
|
||||||
"select distinct date from checkins where habit_id=? and type=2",
|
"from habits");
|
||||||
params);
|
if (!c.moveToNext()) return;
|
||||||
if (!c.moveToFirst()) return;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
String date = c.getString(0);
|
|
||||||
int year = Integer.parseInt(date.substring(0, 4));
|
|
||||||
int month = Integer.parseInt(date.substring(4, 6));
|
|
||||||
int day = Integer.parseInt(date.substring(6, 8));
|
|
||||||
|
|
||||||
GregorianCalendar cal = DateUtils.getStartOfTodayCalendar();
|
|
||||||
cal.set(year, month - 1, day);
|
|
||||||
|
|
||||||
habit.getRepetitions().toggle(cal.getTimeInMillis());
|
|
||||||
} while (c.moveToNext());
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
if (c != null) c.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void createHabits(SQLiteDatabase db)
|
|
||||||
{
|
|
||||||
Cursor c = null;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
c = db.rawQuery(
|
|
||||||
"select _id, name, description, schedule, active_days, " +
|
|
||||||
"repeating_count, days, period from habits", new String[0]);
|
|
||||||
if (!c.moveToFirst()) return;
|
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@@ -174,20 +141,51 @@ public class RewireDBImporter extends AbstractImporter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createReminder(SQLiteDatabase db,
|
private void createCheckmarks(@NonNull Database db,
|
||||||
Habit habit,
|
@NonNull Habit habit,
|
||||||
int rewireHabitId)
|
int rewireHabitId)
|
||||||
|
{
|
||||||
|
Cursor c = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
String[] params = { Integer.toString(rewireHabitId) };
|
||||||
|
c = db.select(
|
||||||
|
"select distinct date from checkins where habit_id=? and type=2",
|
||||||
|
params);
|
||||||
|
if (!c.moveToNext()) return;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
String date = c.getString(0);
|
||||||
|
int year = Integer.parseInt(date.substring(0, 4));
|
||||||
|
int month = Integer.parseInt(date.substring(4, 6));
|
||||||
|
int day = Integer.parseInt(date.substring(6, 8));
|
||||||
|
|
||||||
|
GregorianCalendar cal = DateUtils.getStartOfTodayCalendar();
|
||||||
|
cal.set(year, month - 1, day);
|
||||||
|
|
||||||
|
habit.getRepetitions().toggle(cal.getTimeInMillis());
|
||||||
|
} while (c.moveToNext());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createReminder(Database db, Habit habit, int rewireHabitId)
|
||||||
{
|
{
|
||||||
String[] params = { Integer.toString(rewireHabitId) };
|
String[] params = { Integer.toString(rewireHabitId) };
|
||||||
Cursor c = null;
|
Cursor c = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
c = db.rawQuery(
|
c = db.select(
|
||||||
"select time, active_days from reminders where habit_id=? limit 1",
|
"select time, active_days from reminders where habit_id=? limit 1",
|
||||||
params);
|
params);
|
||||||
|
|
||||||
if (!c.moveToFirst()) return;
|
if (!c.moveToNext()) return;
|
||||||
int rewireReminder = Integer.parseInt(c.getString(0));
|
int rewireReminder = Integer.parseInt(c.getString(0));
|
||||||
if (rewireReminder <= 0 || rewireReminder >= 1440) return;
|
if (rewireReminder <= 0 || rewireReminder >= 1440) return;
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
*
|
*
|
||||||
* This file is part of Loop Habit Tracker.
|
* This file is part of Loop Habit Tracker.
|
||||||
*
|
*
|
||||||
@@ -17,14 +17,12 @@
|
|||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.isoron.uhabits.io;
|
package org.isoron.uhabits.core.io;
|
||||||
|
|
||||||
import android.database.*;
|
|
||||||
import android.database.sqlite.*;
|
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.core.database.*;
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.isoron.uhabits.utils.DatabaseUtils;
|
|
||||||
import org.isoron.uhabits.core.utils.*;
|
import org.isoron.uhabits.core.utils.*;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
@@ -39,12 +37,17 @@ public class TickmateDBImporter extends AbstractImporter
|
|||||||
{
|
{
|
||||||
private ModelFactory modelFactory;
|
private ModelFactory modelFactory;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final DatabaseOpener opener;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public TickmateDBImporter(@NonNull HabitList habits,
|
public TickmateDBImporter(@NonNull HabitList habits,
|
||||||
@NonNull ModelFactory modelFactory)
|
@NonNull ModelFactory modelFactory,
|
||||||
|
@NonNull DatabaseOpener opener)
|
||||||
{
|
{
|
||||||
super(habits);
|
super(habits);
|
||||||
this.modelFactory = modelFactory;
|
this.modelFactory = modelFactory;
|
||||||
|
this.opener = opener;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -52,14 +55,11 @@ public class TickmateDBImporter extends AbstractImporter
|
|||||||
{
|
{
|
||||||
if (!isSQLite3File(file)) return false;
|
if (!isSQLite3File(file)) return false;
|
||||||
|
|
||||||
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
Database db = opener.open(file);
|
||||||
SQLiteDatabase.OPEN_READONLY);
|
Cursor c = db.select("select count(*) from SQLITE_MASTER " +
|
||||||
|
"where name='tracks' or name='track2groups'");
|
||||||
|
|
||||||
Cursor c = db.rawQuery(
|
boolean result = (c.moveToNext() && c.getInt(0) == 2);
|
||||||
"select count(*) from SQLITE_MASTER where name=? or name=?",
|
|
||||||
new String[]{"tracks", "track2groups"});
|
|
||||||
|
|
||||||
boolean result = (c.moveToFirst() && c.getInt(0) == 2);
|
|
||||||
|
|
||||||
c.close();
|
c.close();
|
||||||
db.close();
|
db.close();
|
||||||
@@ -69,15 +69,15 @@ public class TickmateDBImporter extends AbstractImporter
|
|||||||
@Override
|
@Override
|
||||||
public void importHabitsFromFile(@NonNull File file) throws IOException
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
{
|
{
|
||||||
final SQLiteDatabase db =
|
final Database db = opener.open(file);
|
||||||
SQLiteDatabase.openDatabase(file.getPath(), null,
|
db.beginTransaction();
|
||||||
SQLiteDatabase.OPEN_READONLY);
|
createHabits(db);
|
||||||
|
db.setTransactionSuccessful();
|
||||||
DatabaseUtils.executeAsTransaction(() -> createHabits(db));
|
db.endTransaction();
|
||||||
db.close();
|
db.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createCheckmarks(@NonNull SQLiteDatabase db,
|
private void createCheckmarks(@NonNull Database db,
|
||||||
@NonNull Habit habit,
|
@NonNull Habit habit,
|
||||||
int tickmateTrackId)
|
int tickmateTrackId)
|
||||||
{
|
{
|
||||||
@@ -86,10 +86,10 @@ public class TickmateDBImporter extends AbstractImporter
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
String[] params = {Integer.toString(tickmateTrackId)};
|
String[] params = {Integer.toString(tickmateTrackId)};
|
||||||
c = db.rawQuery(
|
c = db.select(
|
||||||
"select distinct year, month, day from ticks where _track_id=?",
|
"select distinct year, month, day from ticks where _track_id=?",
|
||||||
params);
|
params);
|
||||||
if (!c.moveToFirst()) return;
|
if (!c.moveToNext()) return;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@@ -109,15 +109,15 @@ public class TickmateDBImporter extends AbstractImporter
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createHabits(SQLiteDatabase db)
|
private void createHabits(Database db)
|
||||||
{
|
{
|
||||||
Cursor c = null;
|
Cursor c = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
c = db.rawQuery("select _id, name, description from tracks",
|
c = db.select("select _id, name, description from tracks",
|
||||||
new String[0]);
|
new String[0]);
|
||||||
if (!c.moveToFirst()) return;
|
if (!c.moveToNext()) return;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
*
|
*
|
||||||
* This file is part of Loop Habit Tracker.
|
* This file is part of Loop Habit Tracker.
|
||||||
*
|
*
|
||||||
@@ -20,4 +20,4 @@
|
|||||||
/**
|
/**
|
||||||
* Provides classes that deal with importing from and exporting to files.
|
* Provides classes that deal with importing from and exporting to files.
|
||||||
*/
|
*/
|
||||||
package org.isoron.uhabits.io;
|
package org.isoron.uhabits.core.io;
|
||||||
@@ -66,7 +66,7 @@ public class BaseUnitTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown()
|
public void tearDown() throws Exception
|
||||||
{
|
{
|
||||||
validateMockitoUsage();
|
validateMockitoUsage();
|
||||||
DateUtils.setFixedLocalTime(null);
|
DateUtils.setFixedLocalTime(null);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
*
|
*
|
||||||
* This file is part of Loop Habit Tracker.
|
* This file is part of Loop Habit Tracker.
|
||||||
*
|
*
|
||||||
@@ -17,41 +17,40 @@
|
|||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.isoron.uhabits.io;
|
package org.isoron.uhabits.core.io;
|
||||||
|
|
||||||
import android.content.*;
|
import org.apache.commons.io.*;
|
||||||
import android.support.test.*;
|
|
||||||
import android.support.test.runner.*;
|
|
||||||
import android.test.suitebuilder.annotation.*;
|
|
||||||
|
|
||||||
import org.isoron.androidbase.utils.*;
|
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
import org.isoron.uhabits.core.io.*;
|
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
import org.junit.runner.*;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.file.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.zip.*;
|
import java.util.zip.*;
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
import static org.junit.Assert.assertNotNull;
|
||||||
@MediumTest
|
import static org.junit.Assert.assertTrue;
|
||||||
public class HabitsCSVExporterTest extends BaseAndroidTest
|
|
||||||
|
public class HabitsCSVExporterTest extends BaseUnitTest
|
||||||
{
|
{
|
||||||
private File baseDir;
|
private File baseDir;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp()
|
public void setUp() throws Exception
|
||||||
{
|
{
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
habitList.add(fixtures.createShortHabit());
|
||||||
|
habitList.add(fixtures.createEmptyHabit());
|
||||||
|
baseDir = Files.createTempDirectory("csv").toFile();
|
||||||
|
assertNotNull(baseDir);
|
||||||
|
}
|
||||||
|
|
||||||
fixtures.purgeHabits(habitList);
|
@Override
|
||||||
fixtures.createShortHabit();
|
public void tearDown() throws Exception
|
||||||
fixtures.createEmptyHabit();
|
{
|
||||||
|
FileUtils.deleteDirectory(baseDir);
|
||||||
Context targetContext = InstrumentationRegistry.getTargetContext();
|
super.tearDown();
|
||||||
baseDir = targetContext.getCacheDir();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -78,20 +77,6 @@ public class HabitsCSVExporterTest extends BaseAndroidTest
|
|||||||
assertPathExists("Scores.csv");
|
assertPathExists("Scores.csv");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertAbsolutePathExists(String s)
|
|
||||||
{
|
|
||||||
File file = new File(s);
|
|
||||||
assertTrue(
|
|
||||||
String.format("File %s should exist", file.getAbsolutePath()),
|
|
||||||
file.exists());
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assertPathExists(String s)
|
|
||||||
{
|
|
||||||
assertAbsolutePathExists(
|
|
||||||
String.format("%s/%s", baseDir.getAbsolutePath(), s));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void unzip(File file) throws IOException
|
private void unzip(File file) throws IOException
|
||||||
{
|
{
|
||||||
ZipFile zip = new ZipFile(file);
|
ZipFile zip = new ZipFile(file);
|
||||||
@@ -105,14 +90,27 @@ public class HabitsCSVExporterTest extends BaseAndroidTest
|
|||||||
String outputFilename =
|
String outputFilename =
|
||||||
String.format("%s/%s", baseDir.getAbsolutePath(),
|
String.format("%s/%s", baseDir.getAbsolutePath(),
|
||||||
entry.getName());
|
entry.getName());
|
||||||
File outputFile = new File(outputFilename);
|
File out = new File(outputFilename);
|
||||||
|
File parent = out.getParentFile();
|
||||||
File parent = outputFile.getParentFile();
|
|
||||||
if (parent != null) parent.mkdirs();
|
if (parent != null) parent.mkdirs();
|
||||||
|
|
||||||
FileUtils.copy(stream, outputFile);
|
IOUtils.copy(stream, new FileOutputStream(out));
|
||||||
}
|
}
|
||||||
|
|
||||||
zip.close();
|
zip.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void assertPathExists(String s)
|
||||||
|
{
|
||||||
|
assertAbsolutePathExists(
|
||||||
|
String.format("%s/%s", baseDir.getAbsolutePath(), s));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertAbsolutePathExists(String s)
|
||||||
|
{
|
||||||
|
File file = new File(s);
|
||||||
|
assertTrue(
|
||||||
|
String.format("File %s should exist", file.getAbsolutePath()),
|
||||||
|
file.exists());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
*
|
*
|
||||||
* This file is part of Loop Habit Tracker.
|
* This file is part of Loop Habit Tracker.
|
||||||
*
|
*
|
||||||
@@ -17,40 +17,37 @@
|
|||||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.isoron.uhabits.io;
|
package org.isoron.uhabits.core.io;
|
||||||
|
|
||||||
import android.content.*;
|
import android.support.annotation.*;
|
||||||
import android.support.test.*;
|
|
||||||
import android.support.test.runner.*;
|
|
||||||
import android.test.suitebuilder.annotation.*;
|
|
||||||
|
|
||||||
import org.isoron.androidbase.utils.*;
|
import org.apache.commons.io.*;
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.core.database.*;
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.isoron.uhabits.core.utils.*;
|
import org.isoron.uhabits.core.utils.*;
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
import org.junit.runner.*;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.*;
|
import sun.reflect.generics.reflectiveObjects.*;
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
@RunWith(AndroidJUnit4.class)
|
import static junit.framework.TestCase.assertFalse;
|
||||||
@MediumTest
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
public class ImportTest extends BaseAndroidTest
|
import static org.hamcrest.core.IsEqual.*;
|
||||||
|
import static org.isoron.uhabits.core.models.Frequency.THREE_TIMES_PER_WEEK;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
public class ImportTest extends BaseUnitTest
|
||||||
{
|
{
|
||||||
private Context context;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Before
|
@Before
|
||||||
public void setUp()
|
public void setUp() throws Exception
|
||||||
{
|
{
|
||||||
super.setUp();
|
super.setUp();
|
||||||
DateUtils.setFixedLocalTime(null);
|
DateUtils.setFixedLocalTime(null);
|
||||||
fixtures.purgeHabits(habitList);
|
|
||||||
context = InstrumentationRegistry.getInstrumentation().getContext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -78,8 +75,7 @@ public class ImportTest extends BaseAndroidTest
|
|||||||
|
|
||||||
Habit habit = habitList.getByPosition(0);
|
Habit habit = habitList.getByPosition(0);
|
||||||
assertThat(habit.getName(), equalTo("Wake up early"));
|
assertThat(habit.getName(), equalTo("Wake up early"));
|
||||||
assertThat(habit.getFrequency(),
|
assertThat(habit.getFrequency(), equalTo(THREE_TIMES_PER_WEEK));
|
||||||
equalTo(Frequency.THREE_TIMES_PER_WEEK));
|
|
||||||
assertTrue(containsRepetition(habit, 2016, 3, 14));
|
assertTrue(containsRepetition(habit, 2016, 3, 14));
|
||||||
assertTrue(containsRepetition(habit, 2016, 3, 16));
|
assertTrue(containsRepetition(habit, 2016, 3, 16));
|
||||||
assertFalse(containsRepetition(habit, 2016, 3, 17));
|
assertFalse(containsRepetition(habit, 2016, 3, 17));
|
||||||
@@ -94,8 +90,7 @@ public class ImportTest extends BaseAndroidTest
|
|||||||
|
|
||||||
Habit habit = habitList.getByPosition(0);
|
Habit habit = habitList.getByPosition(0);
|
||||||
assertThat(habit.getName(), equalTo("Wake up early"));
|
assertThat(habit.getName(), equalTo("Wake up early"));
|
||||||
assertThat(habit.getFrequency(),
|
assertThat(habit.getFrequency(), equalTo(THREE_TIMES_PER_WEEK));
|
||||||
equalTo(Frequency.THREE_TIMES_PER_WEEK));
|
|
||||||
assertFalse(habit.hasReminder());
|
assertFalse(habit.hasReminder());
|
||||||
assertFalse(containsRepetition(habit, 2015, 12, 31));
|
assertFalse(containsRepetition(habit, 2015, 12, 31));
|
||||||
assertTrue(containsRepetition(habit, 2016, 1, 18));
|
assertTrue(containsRepetition(habit, 2016, 1, 18));
|
||||||
@@ -104,8 +99,7 @@ public class ImportTest extends BaseAndroidTest
|
|||||||
|
|
||||||
habit = habitList.getByPosition(1);
|
habit = habitList.getByPosition(1);
|
||||||
assertThat(habit.getName(), equalTo("brush teeth"));
|
assertThat(habit.getName(), equalTo("brush teeth"));
|
||||||
assertThat(habit.getFrequency(),
|
assertThat(habit.getFrequency(), equalTo(THREE_TIMES_PER_WEEK));
|
||||||
equalTo(Frequency.THREE_TIMES_PER_WEEK));
|
|
||||||
assertThat(habit.hasReminder(), equalTo(true));
|
assertThat(habit.hasReminder(), equalTo(true));
|
||||||
|
|
||||||
Reminder reminder = habit.getReminder();
|
Reminder reminder = habit.getReminder();
|
||||||
@@ -139,8 +133,13 @@ public class ImportTest extends BaseAndroidTest
|
|||||||
|
|
||||||
private void copyAssetToFile(String assetPath, File dst) throws IOException
|
private void copyAssetToFile(String assetPath, File dst) throws IOException
|
||||||
{
|
{
|
||||||
InputStream in = context.getAssets().open(assetPath);
|
InputStream in = getClass().getResourceAsStream(assetPath);
|
||||||
FileUtils.copy(in, dst);
|
if(in == null) {
|
||||||
|
File file = new File("uhabits-core/src/test/resources/" + assetPath);
|
||||||
|
if(file.exists()) in = new FileInputStream(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
IOUtils.copy(in, new FileOutputStream(dst));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void importFromFile(String assetFilename) throws IOException
|
private void importFromFile(String assetFilename) throws IOException
|
||||||
@@ -150,9 +149,34 @@ public class ImportTest extends BaseAndroidTest
|
|||||||
assertTrue(file.exists());
|
assertTrue(file.exists());
|
||||||
assertTrue(file.canRead());
|
assertTrue(file.canRead());
|
||||||
|
|
||||||
GenericImporter importer = component.getGenericImporter();
|
DatabaseOpener opener = new DatabaseOpener() {
|
||||||
assertThat(importer.canHandle(file), is(true));
|
@Override
|
||||||
|
public Database open(@NonNull File file)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new JdbcDatabase(DriverManager.getConnection(
|
||||||
|
String.format("jdbc:sqlite:%s", file.getAbsolutePath())));
|
||||||
|
}
|
||||||
|
catch (SQLException e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public File getProductionDatabaseFile()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
GenericImporter importer = new GenericImporter(habitList,
|
||||||
|
new LoopDBImporter(habitList, opener),
|
||||||
|
new RewireDBImporter(habitList, modelFactory, opener),
|
||||||
|
new TickmateDBImporter(habitList, modelFactory, opener),
|
||||||
|
new HabitBullCSVImporter(habitList, modelFactory));
|
||||||
|
|
||||||
|
assertTrue(importer.canHandle(file));
|
||||||
importer.importHabitsFromFile(file);
|
importer.importHabitsFromFile(file);
|
||||||
|
|
||||||
file.delete();
|
file.delete();
|
||||||
@@ -74,7 +74,7 @@ public class RepetitionListTest extends BaseUnitTest
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@After
|
@After
|
||||||
public void tearDown()
|
public void tearDown() throws Exception
|
||||||
{
|
{
|
||||||
super.tearDown();
|
super.tearDown();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ public class SQLiteHabitListTest extends BaseUnitTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tearDown()
|
public void tearDown() throws Exception
|
||||||
{
|
{
|
||||||
habitList.getObservable().removeListener(listener);
|
habitList.getObservable().removeListener(listener);
|
||||||
super.tearDown();
|
super.tearDown();
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ package org.isoron.uhabits.core.models.sqlite.records;
|
|||||||
|
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
import org.isoron.uhabits.core.models.*;
|
import org.isoron.uhabits.core.models.*;
|
||||||
import org.isoron.uhabits.core.models.sqlite.records.*;
|
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
|||||||
Reference in New Issue
Block a user