mirror of https://github.com/iSoron/uhabits.git
commit
5115379fdd
|
After Width: | Height: | Size: 48 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* 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.unit.io;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.test.InstrumentationRegistry;
|
||||||
|
import android.support.test.runner.AndroidJUnit4;
|
||||||
|
import android.test.suitebuilder.annotation.SmallTest;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
|
import org.isoron.uhabits.io.HabitsCSVExporter;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.isoron.uhabits.unit.models.HabitFixtures;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertTrue;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@SmallTest
|
||||||
|
public class HabitsCSVExporterTest
|
||||||
|
{
|
||||||
|
private File baseDir;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup()
|
||||||
|
{
|
||||||
|
HabitFixtures.purgeHabits();
|
||||||
|
HabitFixtures.createNonDailyHabit();
|
||||||
|
HabitFixtures.createEmptyHabit();
|
||||||
|
|
||||||
|
Context targetContext = InstrumentationRegistry.getTargetContext();
|
||||||
|
baseDir = targetContext.getCacheDir();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void unzip(File file) throws IOException
|
||||||
|
{
|
||||||
|
ZipFile zip = new ZipFile(file);
|
||||||
|
Enumeration<? extends ZipEntry> e = zip.entries();
|
||||||
|
|
||||||
|
while(e.hasMoreElements())
|
||||||
|
{
|
||||||
|
ZipEntry entry = e.nextElement();
|
||||||
|
InputStream stream = zip.getInputStream(entry);
|
||||||
|
|
||||||
|
String outputFilename = String.format("%s/%s", baseDir.getAbsolutePath(),
|
||||||
|
entry.getName());
|
||||||
|
File outputFile = new File(outputFilename);
|
||||||
|
|
||||||
|
File parent = outputFile.getParentFile();
|
||||||
|
if(parent != null) parent.mkdirs();
|
||||||
|
|
||||||
|
DatabaseHelper.copy(stream, outputFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
zip.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void exportCSV() throws IOException
|
||||||
|
{
|
||||||
|
List<Habit> habits = Habit.getAll(true);
|
||||||
|
|
||||||
|
HabitsCSVExporter exporter = new HabitsCSVExporter(habits, baseDir);
|
||||||
|
String filename = exporter.writeArchive();
|
||||||
|
assertAbsolutePathExists(filename);
|
||||||
|
|
||||||
|
File archive = new File(filename);
|
||||||
|
unzip(archive);
|
||||||
|
|
||||||
|
assertPathExists("Habits.csv");
|
||||||
|
assertPathExists("001 Wake up early");
|
||||||
|
assertPathExists("001 Wake up early/Checkmarks.csv");
|
||||||
|
assertPathExists("001 Wake up early/Scores.csv");
|
||||||
|
assertPathExists("002 Meditate/Checkmarks.csv");
|
||||||
|
assertPathExists("002 Meditate/Scores.csv");
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,169 @@
|
|||||||
|
/*
|
||||||
|
* 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.unit.io;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.test.InstrumentationRegistry;
|
||||||
|
import android.support.test.runner.AndroidJUnit4;
|
||||||
|
import android.test.suitebuilder.annotation.SmallTest;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.io.GenericImporter;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.isoron.uhabits.unit.models.HabitFixtures;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@SmallTest
|
||||||
|
public class ImportTest
|
||||||
|
{
|
||||||
|
private File baseDir;
|
||||||
|
private Context context;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup()
|
||||||
|
{
|
||||||
|
HabitFixtures.purgeHabits();
|
||||||
|
context = InstrumentationRegistry.getInstrumentation().getContext();
|
||||||
|
baseDir = DatabaseHelper.getFilesDir("Backups");
|
||||||
|
if(baseDir == null) fail("baseDir should not be null");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyAssetToFile(String assetPath, File dst) throws IOException
|
||||||
|
{
|
||||||
|
InputStream in = context.getAssets().open(assetPath);
|
||||||
|
DatabaseHelper.copy(in, dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void importFromFile(String assetFilename) throws IOException
|
||||||
|
{
|
||||||
|
File file = new File(String.format("%s/%s", baseDir.getPath(), assetFilename));
|
||||||
|
copyAssetToFile(assetFilename, file);
|
||||||
|
assertTrue(file.exists());
|
||||||
|
assertTrue(file.canRead());
|
||||||
|
|
||||||
|
GenericImporter importer = new GenericImporter();
|
||||||
|
assertThat(importer.canHandle(file), is(true));
|
||||||
|
|
||||||
|
importer.importHabitsFromFile(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean containsRepetition(Habit h, int year, int month, int day)
|
||||||
|
{
|
||||||
|
GregorianCalendar date = DateHelper.getStartOfTodayCalendar();
|
||||||
|
date.set(year, month - 1, day);
|
||||||
|
return h.repetitions.contains(date.getTimeInMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void tickmateDB() throws IOException
|
||||||
|
{
|
||||||
|
importFromFile("tickmate.db");
|
||||||
|
|
||||||
|
List<Habit> habits = Habit.getAll(true);
|
||||||
|
assertThat(habits.size(), equalTo(3));
|
||||||
|
|
||||||
|
Habit h = habits.get(0);
|
||||||
|
assertThat(h.name, equalTo("Vegan"));
|
||||||
|
assertTrue(containsRepetition(h, 2016, 1, 24));
|
||||||
|
assertTrue(containsRepetition(h, 2016, 2, 5));
|
||||||
|
assertTrue(containsRepetition(h, 2016, 3, 18));
|
||||||
|
assertFalse(containsRepetition(h, 2016, 3, 14));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void rewireDB() throws IOException
|
||||||
|
{
|
||||||
|
importFromFile("rewire.db");
|
||||||
|
|
||||||
|
List<Habit> habits = Habit.getAll(true);
|
||||||
|
assertThat(habits.size(), equalTo(3));
|
||||||
|
|
||||||
|
Habit habit = habits.get(0);
|
||||||
|
assertThat(habit.name, equalTo("Wake up early"));
|
||||||
|
assertThat(habit.freqNum, equalTo(3));
|
||||||
|
assertThat(habit.freqDen, equalTo(7));
|
||||||
|
assertFalse(habit.hasReminder());
|
||||||
|
assertFalse(containsRepetition(habit, 2015, 12, 31));
|
||||||
|
assertTrue(containsRepetition(habit, 2016, 1, 18));
|
||||||
|
assertTrue(containsRepetition(habit, 2016, 1, 28));
|
||||||
|
assertFalse(containsRepetition(habit, 2016, 3, 10));
|
||||||
|
|
||||||
|
habit = habits.get(1);
|
||||||
|
assertThat(habit.name, equalTo("brush teeth"));
|
||||||
|
assertThat(habit.freqNum, equalTo(3));
|
||||||
|
assertThat(habit.freqDen, equalTo(7));
|
||||||
|
assertThat(habit.reminderHour, equalTo(8));
|
||||||
|
assertThat(habit.reminderMin, equalTo(0));
|
||||||
|
boolean[] reminderDays = {false, true, true, true, true, true, false};
|
||||||
|
assertThat(habit.reminderDays, equalTo(DateHelper.packWeekdayList(reminderDays)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void habitbullCSV() throws IOException
|
||||||
|
{
|
||||||
|
importFromFile("habitbull.csv");
|
||||||
|
|
||||||
|
List<Habit> habits = Habit.getAll(true);
|
||||||
|
assertThat(habits.size(), equalTo(4));
|
||||||
|
|
||||||
|
Habit habit = habits.get(0);
|
||||||
|
assertThat(habit.name, equalTo("Breed dragons"));
|
||||||
|
assertThat(habit.description, equalTo("with love and fire"));
|
||||||
|
assertThat(habit.freqNum, equalTo(1));
|
||||||
|
assertThat(habit.freqDen, equalTo(1));
|
||||||
|
assertTrue(containsRepetition(habit, 2016, 3, 18));
|
||||||
|
assertTrue(containsRepetition(habit, 2016, 3, 19));
|
||||||
|
assertFalse(containsRepetition(habit, 2016, 3, 20));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loopDB() throws IOException
|
||||||
|
{
|
||||||
|
importFromFile("loop.db");
|
||||||
|
|
||||||
|
List<Habit> habits = Habit.getAll(true);
|
||||||
|
assertThat(habits.size(), equalTo(9));
|
||||||
|
|
||||||
|
Habit habit = habits.get(0);
|
||||||
|
assertThat(habit.name, equalTo("Wake up early"));
|
||||||
|
assertThat(habit.freqNum, equalTo(3));
|
||||||
|
assertThat(habit.freqDen, equalTo(7));
|
||||||
|
assertTrue(containsRepetition(habit, 2016, 3, 14));
|
||||||
|
assertTrue(containsRepetition(habit, 2016, 3, 16));
|
||||||
|
assertFalse(containsRepetition(habit, 2016, 3, 17));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* 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.unit.tasks;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.test.InstrumentationRegistry;
|
||||||
|
import android.support.test.runner.AndroidJUnit4;
|
||||||
|
import android.test.suitebuilder.annotation.SmallTest;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.isoron.uhabits.tasks.ExportCSVTask;
|
||||||
|
import org.isoron.uhabits.unit.models.HabitFixtures;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertTrue;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
import static org.hamcrest.core.IsNot.not;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@SmallTest
|
||||||
|
public class ExportCSVTaskTest
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
public void exportCSV() throws InterruptedException
|
||||||
|
{
|
||||||
|
Context context = InstrumentationRegistry.getContext();
|
||||||
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
HabitFixtures.createNonDailyHabit();
|
||||||
|
List<Habit> habits = Habit.getAll(true);
|
||||||
|
ProgressBar bar = new ProgressBar(context);
|
||||||
|
|
||||||
|
ExportCSVTask task = new ExportCSVTask(habits, bar);
|
||||||
|
task.setListener(new ExportCSVTask.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onExportCSVFinished(String archiveFilename)
|
||||||
|
{
|
||||||
|
assertThat(archiveFilename, is(not(nullValue())));
|
||||||
|
|
||||||
|
File f = new File(archiveFilename);
|
||||||
|
assertTrue(f.exists());
|
||||||
|
assertTrue(f.canRead());
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
task.execute();
|
||||||
|
latch.await(30, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* 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.unit.tasks;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.test.InstrumentationRegistry;
|
||||||
|
import android.support.test.runner.AndroidJUnit4;
|
||||||
|
import android.test.suitebuilder.annotation.SmallTest;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.tasks.ExportDBTask;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertTrue;
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
import static org.hamcrest.core.IsNot.not;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@SmallTest
|
||||||
|
public class ExportDBTaskTest
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
public void exportCSV() throws InterruptedException
|
||||||
|
{
|
||||||
|
Context context = InstrumentationRegistry.getContext();
|
||||||
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
ProgressBar bar = new ProgressBar(context);
|
||||||
|
ExportDBTask task = new ExportDBTask(bar);
|
||||||
|
task.setListener(new ExportDBTask.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onExportDBFinished(String filename)
|
||||||
|
{
|
||||||
|
assertThat(filename, is(not(nullValue())));
|
||||||
|
|
||||||
|
File f = new File(filename);
|
||||||
|
assertTrue(f.exists());
|
||||||
|
assertTrue(f.canRead());
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
task.execute();
|
||||||
|
latch.await(30, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* 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.unit.tasks;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.test.InstrumentationRegistry;
|
||||||
|
import android.support.test.runner.AndroidJUnit4;
|
||||||
|
import android.test.suitebuilder.annotation.SmallTest;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
|
import org.isoron.uhabits.tasks.ImportDataTask;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
@SmallTest
|
||||||
|
public class ImportDataTaskTest
|
||||||
|
{
|
||||||
|
private Context context;
|
||||||
|
private File baseDir;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup()
|
||||||
|
{
|
||||||
|
context = InstrumentationRegistry.getContext();
|
||||||
|
|
||||||
|
baseDir = DatabaseHelper.getFilesDir("Backups");
|
||||||
|
if(baseDir == null) fail("baseDir should not be null");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyAssetToFile(String assetPath, File dst) throws IOException
|
||||||
|
{
|
||||||
|
InputStream in = context.getAssets().open(assetPath);
|
||||||
|
DatabaseHelper.copy(in, dst);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertTaskResult(final int expectedResult, String assetFilename)
|
||||||
|
throws IOException, InterruptedException
|
||||||
|
{
|
||||||
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
ImportDataTask task = createTask(assetFilename);
|
||||||
|
|
||||||
|
task.setListener(new ImportDataTask.Listener()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onImportFinished(int result)
|
||||||
|
{
|
||||||
|
assertThat(result, equalTo(expectedResult));
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
task.execute();
|
||||||
|
latch.await(30, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private ImportDataTask createTask(String assetFilename) throws IOException
|
||||||
|
{
|
||||||
|
ProgressBar bar = new ProgressBar(context);
|
||||||
|
File file = new File(String.format("%s/%s", baseDir.getPath(), assetFilename));
|
||||||
|
copyAssetToFile(assetFilename, file);
|
||||||
|
|
||||||
|
return new ImportDataTask(file, bar);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void importInvalidData() throws Throwable
|
||||||
|
{
|
||||||
|
assertTaskResult(ImportDataTask.NOT_RECOGNIZED, "icon.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void importValidData() throws Throwable
|
||||||
|
{
|
||||||
|
assertTaskResult(ImportDataTask.SUCCESS, "loop.db");
|
||||||
|
}
|
||||||
|
}
|
@ -1,29 +0,0 @@
|
|||||||
package org.isoron.helpers;
|
|
||||||
|
|
||||||
import com.activeandroid.ActiveAndroid;
|
|
||||||
|
|
||||||
public class ActiveAndroidHelper
|
|
||||||
{
|
|
||||||
public interface Command
|
|
||||||
{
|
|
||||||
void execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void executeAsTransaction(Command command)
|
|
||||||
{
|
|
||||||
ActiveAndroid.beginTransaction();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
command.execute();
|
|
||||||
ActiveAndroid.setTransactionSuccessful();
|
|
||||||
}
|
|
||||||
catch (RuntimeException e)
|
|
||||||
{
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
ActiveAndroid.endTransaction();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,175 @@
|
|||||||
|
/*
|
||||||
|
* 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.dialogs;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.view.WindowManager.LayoutParams;
|
||||||
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.ListView;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class FilePickerDialog implements AdapterView.OnItemClickListener
|
||||||
|
{
|
||||||
|
private static final String PARENT_DIR = "..";
|
||||||
|
|
||||||
|
private final Activity activity;
|
||||||
|
private ListView list;
|
||||||
|
private Dialog dialog;
|
||||||
|
private File currentPath;
|
||||||
|
|
||||||
|
public interface OnFileSelectedListener
|
||||||
|
{
|
||||||
|
void onFileSelected(File file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private OnFileSelectedListener listener;
|
||||||
|
|
||||||
|
public FilePickerDialog(Activity activity, File initialDirectory)
|
||||||
|
{
|
||||||
|
this.activity = activity;
|
||||||
|
|
||||||
|
list = new ListView(activity);
|
||||||
|
list.setOnItemClickListener(this);
|
||||||
|
|
||||||
|
dialog = new Dialog(activity);
|
||||||
|
dialog.setContentView(list);
|
||||||
|
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
|
||||||
|
|
||||||
|
navigateTo(initialDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onItemClick(AdapterView<?> parent, View view, int which, long id)
|
||||||
|
{
|
||||||
|
String filename = (String) list.getItemAtPosition(which);
|
||||||
|
File file;
|
||||||
|
|
||||||
|
if (filename.equals(PARENT_DIR))
|
||||||
|
file = currentPath.getParentFile();
|
||||||
|
else
|
||||||
|
file = new File(currentPath, filename);
|
||||||
|
|
||||||
|
if (file.isDirectory())
|
||||||
|
{
|
||||||
|
navigateTo(file);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (listener != null) listener.onFileSelected(file);
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void show()
|
||||||
|
{
|
||||||
|
dialog.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListener(OnFileSelectedListener listener)
|
||||||
|
{
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void navigateTo(File path)
|
||||||
|
{
|
||||||
|
if (!path.exists()) return;
|
||||||
|
|
||||||
|
File[] dirs = path.listFiles(new ReadableDirFilter());
|
||||||
|
File[] files = path.listFiles(new RegularReadableFileFilter());
|
||||||
|
if(dirs == null || files == null) return;
|
||||||
|
|
||||||
|
this.currentPath = path;
|
||||||
|
dialog.setTitle(currentPath.getPath());
|
||||||
|
list.setAdapter(new FilePickerAdapter(getFileList(path, dirs, files)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private String[] getFileList(File path, File[] dirs, File[] files)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
int length = dirs.length + files.length;
|
||||||
|
String[] fileList;
|
||||||
|
|
||||||
|
if (path.getParentFile() == null || !path.getParentFile().canRead())
|
||||||
|
{
|
||||||
|
fileList = new String[length];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fileList = new String[length + 1];
|
||||||
|
fileList[count++] = PARENT_DIR;
|
||||||
|
}
|
||||||
|
|
||||||
|
Arrays.sort(dirs);
|
||||||
|
Arrays.sort(files);
|
||||||
|
|
||||||
|
for (File dir : dirs)
|
||||||
|
fileList[count++] = dir.getName();
|
||||||
|
|
||||||
|
for (File file : files)
|
||||||
|
fileList[count++] = file.getName();
|
||||||
|
|
||||||
|
return fileList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class FilePickerAdapter extends ArrayAdapter<String>
|
||||||
|
{
|
||||||
|
public FilePickerAdapter(@NonNull String[] fileList)
|
||||||
|
{
|
||||||
|
super(FilePickerDialog.this.activity, android.R.layout.simple_list_item_1, fileList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View getView(int pos, View view, ViewGroup parent)
|
||||||
|
{
|
||||||
|
view = super.getView(pos, view, parent);
|
||||||
|
TextView tv = (TextView) view;
|
||||||
|
tv.setSingleLine(true);
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ReadableDirFilter implements FileFilter
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean accept(File file)
|
||||||
|
{
|
||||||
|
return (file.isDirectory() && file.canRead());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class RegularReadableFileFilter implements FileFilter
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean accept(File file)
|
||||||
|
{
|
||||||
|
return !file.isDirectory() && file.canRead();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,166 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
|
|
||||||
|
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;
|
||||||
|
import org.isoron.uhabits.models.Score;
|
||||||
|
import org.isoron.uhabits.models.Streak;
|
||||||
|
|
||||||
|
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.text.SimpleDateFormat;
|
||||||
|
|
||||||
|
public class DatabaseHelper
|
||||||
|
{
|
||||||
|
public static void copy(File src, File dst) throws IOException
|
||||||
|
{
|
||||||
|
FileInputStream inStream = new FileInputStream(src);
|
||||||
|
FileOutputStream outStream = new FileOutputStream(dst);
|
||||||
|
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
|
||||||
|
{
|
||||||
|
void execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void executeAsTransaction(Command command)
|
||||||
|
{
|
||||||
|
ActiveAndroid.beginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
command.execute();
|
||||||
|
ActiveAndroid.setTransactionSuccessful();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ActiveAndroid.endTransaction();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||||
|
public static String saveDatabaseCopy(File dir) throws IOException
|
||||||
|
{
|
||||||
|
File db = getDatabaseFile();
|
||||||
|
|
||||||
|
SimpleDateFormat dateFormat = DateHelper.getBackupDateFormat();
|
||||||
|
String date = dateFormat.format(DateHelper.getLocalTime());
|
||||||
|
File dbCopy = new File(String.format("%s/Loop Habits Backup %s.db", dir.getAbsolutePath(), date));
|
||||||
|
|
||||||
|
copy(db, dbCopy);
|
||||||
|
|
||||||
|
return dbCopy.getAbsolutePath();
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static File getDatabaseFile()
|
||||||
|
{
|
||||||
|
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 String getDatabaseFilename()
|
||||||
|
{
|
||||||
|
String databaseFilename = BuildConfig.databaseFilename;
|
||||||
|
|
||||||
|
if (HabitsApplication.isTestMode())
|
||||||
|
databaseFilename = "test.db";
|
||||||
|
|
||||||
|
return databaseFilename;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public static File getFilesDir(String prefix)
|
||||||
|
{
|
||||||
|
Context context = HabitsApplication.getContext();
|
||||||
|
if(context == null) return null;
|
||||||
|
|
||||||
|
File chosenDir = null;
|
||||||
|
File externalFilesDirs[] = ContextCompat.getExternalFilesDirs(context, null);
|
||||||
|
if(externalFilesDirs == null) return null;
|
||||||
|
|
||||||
|
for(File dir : externalFilesDirs)
|
||||||
|
{
|
||||||
|
if (dir == null || !dir.canWrite()) continue;
|
||||||
|
chosenDir = dir;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(chosenDir == null) return null;
|
||||||
|
|
||||||
|
File dir = new File(String.format("%s/%s/", chosenDir.getAbsolutePath(), prefix));
|
||||||
|
dir.mkdirs();
|
||||||
|
|
||||||
|
return dir;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static void initializeActiveAndroid()
|
||||||
|
{
|
||||||
|
Context context = HabitsApplication.getContext();
|
||||||
|
if(context == null) throw new RuntimeException("application context should not be null");
|
||||||
|
|
||||||
|
Configuration dbConfig = new Configuration.Builder(context)
|
||||||
|
.setDatabaseName(getDatabaseFilename())
|
||||||
|
.setDatabaseVersion(BuildConfig.databaseVersion)
|
||||||
|
.addModelClasses(Checkmark.class, Habit.class, Repetition.class, Score.class,
|
||||||
|
Streak.class)
|
||||||
|
.create();
|
||||||
|
|
||||||
|
ActiveAndroid.initialize(dbConfig);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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.io;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public abstract class AbstractImporter
|
||||||
|
{
|
||||||
|
public abstract boolean canHandle(@NonNull File file) throws IOException;
|
||||||
|
|
||||||
|
public abstract void importHabitsFromFile(@NonNull File file) throws IOException;
|
||||||
|
|
||||||
|
public static boolean isSQLite3File(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
FileInputStream fis = new FileInputStream(file);
|
||||||
|
|
||||||
|
byte[] sqliteHeader = "SQLite format 3".getBytes();
|
||||||
|
byte[] buffer = new byte[sqliteHeader.length];
|
||||||
|
|
||||||
|
|
||||||
|
int count = fis.read(buffer);
|
||||||
|
if(count < sqliteHeader.length) return false;
|
||||||
|
|
||||||
|
return Arrays.equals(buffer, sqliteHeader);
|
||||||
|
}
|
||||||
|
}
|
@ -1,213 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.io;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.database.Cursor;
|
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import com.activeandroid.Cache;
|
|
||||||
|
|
||||||
import org.isoron.helpers.DateHelper;
|
|
||||||
import org.isoron.uhabits.models.Habit;
|
|
||||||
import org.isoron.uhabits.models.Score;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.TimeZone;
|
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipOutputStream;
|
|
||||||
|
|
||||||
public class CSVExporter
|
|
||||||
{
|
|
||||||
private List<Habit> habits;
|
|
||||||
private Context context;
|
|
||||||
private java.text.DateFormat dateFormat;
|
|
||||||
|
|
||||||
private List<String> generateDirs;
|
|
||||||
private List<String> generateFilenames;
|
|
||||||
|
|
||||||
private String basePath;
|
|
||||||
|
|
||||||
public CSVExporter(Context context, List<Habit> habits)
|
|
||||||
{
|
|
||||||
this.habits = habits;
|
|
||||||
this.context = context;
|
|
||||||
generateDirs = new LinkedList<>();
|
|
||||||
generateFilenames = new LinkedList<>();
|
|
||||||
|
|
||||||
basePath = String.format("%s/export/", context.getFilesDir());
|
|
||||||
|
|
||||||
dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
|
|
||||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
||||||
}
|
|
||||||
|
|
||||||
public String formatDate(long timestamp)
|
|
||||||
{
|
|
||||||
return dateFormat.format(new Date(timestamp));
|
|
||||||
}
|
|
||||||
|
|
||||||
public String formatScore(int score)
|
|
||||||
{
|
|
||||||
return String.format("%.2f", ((float) score) / Score.MAX_VALUE);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeScores(String dirPath, Habit habit) throws IOException
|
|
||||||
{
|
|
||||||
String path = dirPath + "scores.csv";
|
|
||||||
FileWriter out = new FileWriter(basePath + path);
|
|
||||||
generateFilenames.add(path);
|
|
||||||
|
|
||||||
String query = "select timestamp, score from score where habit = ? order by timestamp";
|
|
||||||
String params[] = { habit.getId().toString() };
|
|
||||||
|
|
||||||
SQLiteDatabase db = Cache.openDatabase();
|
|
||||||
Cursor cursor = db.rawQuery(query, params);
|
|
||||||
|
|
||||||
if(!cursor.moveToFirst()) return;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
String timestamp = formatDate(cursor.getLong(0));
|
|
||||||
String score = formatScore(cursor.getInt(1));
|
|
||||||
out.write(String.format("%s,%s\n", timestamp, score));
|
|
||||||
|
|
||||||
} while(cursor.moveToNext());
|
|
||||||
|
|
||||||
out.close();
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeCheckmarks(String dirPath, Habit habit) throws IOException
|
|
||||||
{
|
|
||||||
String path = dirPath + "checkmarks.csv";
|
|
||||||
FileWriter out = new FileWriter(basePath + path);
|
|
||||||
generateFilenames.add(path);
|
|
||||||
|
|
||||||
String query = "select timestamp, value from checkmarks where habit = ? order by timestamp";
|
|
||||||
String params[] = { habit.getId().toString() };
|
|
||||||
|
|
||||||
SQLiteDatabase db = Cache.openDatabase();
|
|
||||||
Cursor cursor = db.rawQuery(query, params);
|
|
||||||
|
|
||||||
if(!cursor.moveToFirst()) return;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
String timestamp = formatDate(cursor.getLong(0));
|
|
||||||
Integer value = cursor.getInt(1);
|
|
||||||
out.write(String.format("%s,%d\n", timestamp, value));
|
|
||||||
|
|
||||||
} while(cursor.moveToNext());
|
|
||||||
|
|
||||||
out.close();
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeFiles(Habit habit) throws IOException
|
|
||||||
{
|
|
||||||
String path = String.format("%s/", habit.name);
|
|
||||||
new File(basePath + path).mkdirs();
|
|
||||||
generateDirs.add(path);
|
|
||||||
|
|
||||||
writeScores(path, habit);
|
|
||||||
writeCheckmarks(path, habit);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void writeZipFile(String zipFilename) throws IOException
|
|
||||||
{
|
|
||||||
FileOutputStream fos = new FileOutputStream(zipFilename);
|
|
||||||
ZipOutputStream zos = new ZipOutputStream(fos);
|
|
||||||
|
|
||||||
for(String filename : generateFilenames)
|
|
||||||
addFileToZip(zos, filename);
|
|
||||||
|
|
||||||
zos.close();
|
|
||||||
fos.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addFileToZip(ZipOutputStream zos, String filename) throws IOException
|
|
||||||
{
|
|
||||||
FileInputStream fis = new FileInputStream(new File(basePath + filename));
|
|
||||||
ZipEntry ze = new ZipEntry(filename);
|
|
||||||
zos.putNextEntry(ze);
|
|
||||||
|
|
||||||
int length;
|
|
||||||
byte bytes[] = new byte[1024];
|
|
||||||
while((length = fis.read(bytes)) >= 0)
|
|
||||||
zos.write(bytes, 0, length);
|
|
||||||
|
|
||||||
zos.closeEntry();
|
|
||||||
fis.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void cleanup()
|
|
||||||
{
|
|
||||||
for(String filename : generateFilenames)
|
|
||||||
new File(basePath + filename).delete();
|
|
||||||
|
|
||||||
for(String filename : generateDirs)
|
|
||||||
new File(basePath + filename).delete();
|
|
||||||
|
|
||||||
new File(basePath).delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String writeArchive()
|
|
||||||
{
|
|
||||||
String date = formatDate(DateHelper.getStartOfToday());
|
|
||||||
|
|
||||||
File dir = context.getExternalCacheDir();
|
|
||||||
|
|
||||||
if(dir == null)
|
|
||||||
{
|
|
||||||
Log.e("CSVExporter", "No suitable directory found.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
String zipFilename = String.format("%s/habits-%s.zip", dir, date);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
for (Habit h : habits)
|
|
||||||
writeFiles(h);
|
|
||||||
|
|
||||||
writeZipFile(zipFilename);
|
|
||||||
cleanup();
|
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return zipFilename;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* 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.io;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class GenericImporter extends AbstractImporter
|
||||||
|
{
|
||||||
|
List<AbstractImporter> importers;
|
||||||
|
|
||||||
|
public GenericImporter()
|
||||||
|
{
|
||||||
|
importers = new LinkedList<>();
|
||||||
|
importers.add(new LoopDBImporter());
|
||||||
|
importers.add(new RewireDBImporter());
|
||||||
|
importers.add(new TickmateDBImporter());
|
||||||
|
importers.add(new HabitBullCSVImporter());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canHandle(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
for(AbstractImporter importer : importers)
|
||||||
|
if(importer.canHandle(file)) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
for(AbstractImporter importer : importers)
|
||||||
|
if(importer.canHandle(file))
|
||||||
|
importer.importHabitsFromFile(file);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* 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.io;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.activeandroid.ActiveAndroid;
|
||||||
|
import com.opencsv.CSVReader;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class HabitBullCSVImporter extends AbstractImporter
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean canHandle(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||||
|
String line = reader.readLine();
|
||||||
|
|
||||||
|
return line.startsWith("HabitName,HabitDescription,HabitCategory");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void importHabitsFromFile(@NonNull final File file) throws IOException
|
||||||
|
{
|
||||||
|
ActiveAndroid.beginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
parseFile(file);
|
||||||
|
ActiveAndroid.setTransactionSuccessful();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ActiveAndroid.endTransaction();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseFile(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
CSVReader reader = new CSVReader(new FileReader(file));
|
||||||
|
HashMap<String, Habit> habits = new HashMap<>();
|
||||||
|
|
||||||
|
for(String line[] : reader)
|
||||||
|
{
|
||||||
|
String name = line[0];
|
||||||
|
if(name.equals("HabitName")) continue;
|
||||||
|
|
||||||
|
String description = line[1];
|
||||||
|
String dateString[] = line[3].split("-");
|
||||||
|
int year = Integer.parseInt(dateString[0]);
|
||||||
|
int month = Integer.parseInt(dateString[1]);
|
||||||
|
int day = Integer.parseInt(dateString[2]);
|
||||||
|
|
||||||
|
Calendar date = DateHelper.getStartOfTodayCalendar();
|
||||||
|
date.set(year, month - 1, day);
|
||||||
|
|
||||||
|
long timestamp = date.getTimeInMillis();
|
||||||
|
|
||||||
|
int value = Integer.parseInt(line[4]);
|
||||||
|
if(value != 1) continue;
|
||||||
|
|
||||||
|
Habit h = habits.get(name);
|
||||||
|
|
||||||
|
if(h == null)
|
||||||
|
{
|
||||||
|
h = new Habit();
|
||||||
|
h.name = name;
|
||||||
|
h.description = description;
|
||||||
|
h.freqNum = h.freqDen = 1;
|
||||||
|
h.save();
|
||||||
|
|
||||||
|
habits.put(name, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!h.repetitions.contains(timestamp))
|
||||||
|
h.repetitions.toggle(timestamp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,148 @@
|
|||||||
|
/*
|
||||||
|
* 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.io;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.models.CheckmarkList;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.isoron.uhabits.models.ScoreList;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
public class HabitsCSVExporter
|
||||||
|
{
|
||||||
|
private List<Habit> habits;
|
||||||
|
|
||||||
|
private List<String> generateDirs;
|
||||||
|
private List<String> generateFilenames;
|
||||||
|
|
||||||
|
private String exportDirName;
|
||||||
|
|
||||||
|
public HabitsCSVExporter(List<Habit> habits, File dir)
|
||||||
|
{
|
||||||
|
this.habits = habits;
|
||||||
|
this.exportDirName = dir.getAbsolutePath() + "/";
|
||||||
|
|
||||||
|
generateDirs = new LinkedList<>();
|
||||||
|
generateFilenames = new LinkedList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeHabits() throws IOException
|
||||||
|
{
|
||||||
|
String filename = "Habits.csv";
|
||||||
|
new File(exportDirName).mkdirs();
|
||||||
|
FileWriter out = new FileWriter(exportDirName + filename);
|
||||||
|
generateFilenames.add(filename);
|
||||||
|
Habit.writeCSV(habits, out);
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
for(Habit h : habits)
|
||||||
|
{
|
||||||
|
String habitDirName = String.format("%03d %s/", h.position + 1, h.name);
|
||||||
|
new File(exportDirName + habitDirName).mkdirs();
|
||||||
|
generateDirs.add(habitDirName);
|
||||||
|
|
||||||
|
writeScores(habitDirName, h.scores);
|
||||||
|
writeCheckmarks(habitDirName, h.checkmarks);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeScores(String habitDirName, ScoreList scores) throws IOException
|
||||||
|
{
|
||||||
|
String path = habitDirName + "Scores.csv";
|
||||||
|
FileWriter out = new FileWriter(exportDirName + path);
|
||||||
|
generateFilenames.add(path);
|
||||||
|
scores.writeCSV(out);
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeCheckmarks(String habitDirName, CheckmarkList checkmarks) throws IOException
|
||||||
|
{
|
||||||
|
String filename = habitDirName + "Checkmarks.csv";
|
||||||
|
FileWriter out = new FileWriter(exportDirName + filename);
|
||||||
|
generateFilenames.add(filename);
|
||||||
|
checkmarks.writeCSV(out);
|
||||||
|
out.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String writeZipFile() throws IOException
|
||||||
|
{
|
||||||
|
SimpleDateFormat dateFormat = DateHelper.getCSVDateFormat();
|
||||||
|
String date = dateFormat.format(DateHelper.getStartOfToday());
|
||||||
|
String zipFilename = String.format("%s/Loop Habits CSV %s.zip", exportDirName, date);
|
||||||
|
|
||||||
|
FileOutputStream fos = new FileOutputStream(zipFilename);
|
||||||
|
ZipOutputStream zos = new ZipOutputStream(fos);
|
||||||
|
|
||||||
|
for(String filename : generateFilenames)
|
||||||
|
addFileToZip(zos, filename);
|
||||||
|
|
||||||
|
zos.close();
|
||||||
|
fos.close();
|
||||||
|
|
||||||
|
return zipFilename;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addFileToZip(ZipOutputStream zos, String filename) throws IOException
|
||||||
|
{
|
||||||
|
FileInputStream fis = new FileInputStream(new File(exportDirName + filename));
|
||||||
|
ZipEntry ze = new ZipEntry(filename);
|
||||||
|
zos.putNextEntry(ze);
|
||||||
|
|
||||||
|
int length;
|
||||||
|
byte bytes[] = new byte[1024];
|
||||||
|
while((length = fis.read(bytes)) >= 0)
|
||||||
|
zos.write(bytes, 0, length);
|
||||||
|
|
||||||
|
zos.closeEntry();
|
||||||
|
fis.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String writeArchive() throws IOException
|
||||||
|
{
|
||||||
|
String zipFilename;
|
||||||
|
|
||||||
|
writeHabits();
|
||||||
|
zipFilename = writeZipFile();
|
||||||
|
cleanup();
|
||||||
|
|
||||||
|
return zipFilename;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void cleanup()
|
||||||
|
{
|
||||||
|
for(String filename : generateFilenames)
|
||||||
|
new File(exportDirName + filename).delete();
|
||||||
|
|
||||||
|
for(String filename : generateDirs)
|
||||||
|
new File(exportDirName + filename).delete();
|
||||||
|
|
||||||
|
new File(exportDirName).delete();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* 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.io;
|
||||||
|
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.activeandroid.ActiveAndroid;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class LoopDBImporter extends AbstractImporter
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean canHandle(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
if(!isSQLite3File(file)) return false;
|
||||||
|
|
||||||
|
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
||||||
|
SQLiteDatabase.OPEN_READONLY);
|
||||||
|
|
||||||
|
Cursor c = db.rawQuery("select count(*) from SQLITE_MASTER where name=? or name=?",
|
||||||
|
new String[]{"Checkmarks", "Repetitions"});
|
||||||
|
|
||||||
|
boolean result = (c.moveToFirst() && c.getInt(0) == 2);
|
||||||
|
|
||||||
|
c.close();
|
||||||
|
db.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
ActiveAndroid.dispose();
|
||||||
|
File originalDB = DatabaseHelper.getDatabaseFile();
|
||||||
|
DatabaseHelper.copy(file, originalDB);
|
||||||
|
DatabaseHelper.initializeActiveAndroid();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,193 @@
|
|||||||
|
/*
|
||||||
|
* 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.io;
|
||||||
|
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
public class RewireDBImporter extends AbstractImporter
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean canHandle(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
if(!isSQLite3File(file)) return false;
|
||||||
|
|
||||||
|
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
||||||
|
SQLiteDatabase.OPEN_READONLY);
|
||||||
|
|
||||||
|
Cursor c = db.rawQuery("select count(*) from SQLITE_MASTER where name=? or name=?",
|
||||||
|
new String[]{"CHECKINS", "UNIT"});
|
||||||
|
|
||||||
|
boolean result = (c.moveToFirst() && c.getInt(0) == 2);
|
||||||
|
|
||||||
|
c.close();
|
||||||
|
db.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
final SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
||||||
|
SQLiteDatabase.OPEN_READONLY);
|
||||||
|
|
||||||
|
DatabaseHelper.executeAsTransaction(new DatabaseHelper.Command()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void execute()
|
||||||
|
{
|
||||||
|
createHabits(db);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
db.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
|
||||||
|
{
|
||||||
|
int id = c.getInt(0);
|
||||||
|
String name = c.getString(1);
|
||||||
|
String description = c.getString(2);
|
||||||
|
int schedule = c.getInt(3);
|
||||||
|
String activeDays = c.getString(4);
|
||||||
|
int repeatingCount = c.getInt(5);
|
||||||
|
int days = c.getInt(6);
|
||||||
|
int periodIndex = c.getInt(7);
|
||||||
|
|
||||||
|
Habit habit = new Habit();
|
||||||
|
habit.name = name;
|
||||||
|
habit.description = description;
|
||||||
|
|
||||||
|
int periods[] = { 7, 31, 365 };
|
||||||
|
|
||||||
|
switch (schedule)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
habit.freqNum = activeDays.split(",").length;
|
||||||
|
habit.freqDen = 7;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
habit.freqNum = days;
|
||||||
|
habit.freqDen = periods[periodIndex];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
habit.freqNum = 1;
|
||||||
|
habit.freqDen = repeatingCount;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
habit.save();
|
||||||
|
|
||||||
|
createReminder(db, habit, id);
|
||||||
|
createCheckmarks(db, habit, id);
|
||||||
|
|
||||||
|
}
|
||||||
|
while (c.moveToNext());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createReminder(SQLiteDatabase db, Habit habit, int rewireHabitId)
|
||||||
|
{
|
||||||
|
String[] params = { Integer.toString(rewireHabitId) };
|
||||||
|
Cursor c = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
c = db.rawQuery("select time, active_days from reminders where habit_id=? limit 1", params);
|
||||||
|
|
||||||
|
if (!c.moveToFirst()) return;
|
||||||
|
int rewireReminder = Integer.parseInt(c.getString(0));
|
||||||
|
if (rewireReminder <= 0 || rewireReminder >= 1440) return;
|
||||||
|
|
||||||
|
boolean reminderDays[] = new boolean[7];
|
||||||
|
|
||||||
|
String activeDays[] = c.getString(1).split(",");
|
||||||
|
for(String d : activeDays)
|
||||||
|
{
|
||||||
|
int idx = (Integer.parseInt(d) + 1) % 7;
|
||||||
|
reminderDays[idx] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
habit.reminderDays = DateHelper.packWeekdayList(reminderDays);
|
||||||
|
habit.reminderHour = rewireReminder / 60;
|
||||||
|
habit.reminderMin = rewireReminder % 60;
|
||||||
|
habit.save();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if(c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createCheckmarks(@NonNull SQLiteDatabase db, @NonNull Habit habit, int rewireHabitId)
|
||||||
|
{
|
||||||
|
Cursor c = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
String[] params = { Integer.toString(rewireHabitId) };
|
||||||
|
c = db.rawQuery("select distinct date from checkins where habit_id=? and type=2", params);
|
||||||
|
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 = DateHelper.getStartOfTodayCalendar();
|
||||||
|
cal.set(year, month - 1, day);
|
||||||
|
|
||||||
|
habit.repetitions.toggle(cal.getTimeInMillis());
|
||||||
|
}
|
||||||
|
while (c.moveToNext());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,133 @@
|
|||||||
|
/*
|
||||||
|
* 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.io;
|
||||||
|
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
|
import org.isoron.uhabits.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
public class TickmateDBImporter extends AbstractImporter
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean canHandle(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
if(!isSQLite3File(file)) return false;
|
||||||
|
|
||||||
|
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
||||||
|
SQLiteDatabase.OPEN_READONLY);
|
||||||
|
|
||||||
|
Cursor c = db.rawQuery("select count(*) from SQLITE_MASTER where name=? or name=?",
|
||||||
|
new String[]{"tracks", "track2groups"});
|
||||||
|
|
||||||
|
boolean result = (c.moveToFirst() && c.getInt(0) == 2);
|
||||||
|
|
||||||
|
c.close();
|
||||||
|
db.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
final SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
||||||
|
SQLiteDatabase.OPEN_READONLY);
|
||||||
|
|
||||||
|
DatabaseHelper.executeAsTransaction(new DatabaseHelper.Command()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void execute()
|
||||||
|
{
|
||||||
|
createHabits(db);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createHabits(SQLiteDatabase db)
|
||||||
|
{
|
||||||
|
Cursor c = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
c = db.rawQuery("select _id, name, description from tracks", new String[0]);
|
||||||
|
if (!c.moveToFirst()) return;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
int id = c.getInt(0);
|
||||||
|
String name = c.getString(1);
|
||||||
|
String description = c.getString(2);
|
||||||
|
|
||||||
|
Habit habit = new Habit();
|
||||||
|
habit.name = name;
|
||||||
|
habit.description = description;
|
||||||
|
habit.freqNum = 1;
|
||||||
|
habit.freqDen = 1;
|
||||||
|
habit.save();
|
||||||
|
|
||||||
|
createCheckmarks(db, habit, id);
|
||||||
|
|
||||||
|
}
|
||||||
|
while (c.moveToNext());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createCheckmarks(@NonNull SQLiteDatabase db, @NonNull Habit habit, int tickmateTrackId)
|
||||||
|
{
|
||||||
|
Cursor c = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
String[] params = { Integer.toString(tickmateTrackId) };
|
||||||
|
c = db.rawQuery("select distinct year, month, day from ticks where _track_id=?", params);
|
||||||
|
if (!c.moveToFirst()) return;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
int year = c.getInt(0);
|
||||||
|
int month = c.getInt(1);
|
||||||
|
int day = c.getInt(2);
|
||||||
|
|
||||||
|
GregorianCalendar cal = DateHelper.getStartOfTodayCalendar();
|
||||||
|
cal.set(year, month, day);
|
||||||
|
|
||||||
|
habit.repetitions.toggle(cal.getTimeInMillis());
|
||||||
|
}
|
||||||
|
while (c.moveToNext());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,96 @@
|
|||||||
|
/*
|
||||||
|
* 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.tasks;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
|
import org.isoron.uhabits.io.HabitsCSVExporter;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ExportCSVTask extends AsyncTask<Void, Void, Void>
|
||||||
|
{
|
||||||
|
public interface Listener
|
||||||
|
{
|
||||||
|
void onExportCSVFinished(@Nullable String archiveFilename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ProgressBar progressBar;
|
||||||
|
private final List<Habit> selectedHabits;
|
||||||
|
private String archiveFilename;
|
||||||
|
private ExportCSVTask.Listener listener;
|
||||||
|
|
||||||
|
public ExportCSVTask(List<Habit> selectedHabits, ProgressBar progressBar)
|
||||||
|
{
|
||||||
|
this.selectedHabits = selectedHabits;
|
||||||
|
this.progressBar = progressBar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListener(Listener listener)
|
||||||
|
{
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPreExecute()
|
||||||
|
{
|
||||||
|
if(progressBar != null)
|
||||||
|
{
|
||||||
|
progressBar.setIndeterminate(true);
|
||||||
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Void aVoid)
|
||||||
|
{
|
||||||
|
if(listener != null)
|
||||||
|
listener.onExportCSVFinished(archiveFilename);
|
||||||
|
|
||||||
|
if(progressBar != null)
|
||||||
|
progressBar.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... params)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File dir = DatabaseHelper.getFilesDir("CSV");
|
||||||
|
if(dir == null) return null;
|
||||||
|
|
||||||
|
HabitsCSVExporter exporter = new HabitsCSVExporter(selectedHabits, dir);
|
||||||
|
archiveFilename = exporter.writeArchive();
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
* 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.tasks;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ExportDBTask extends AsyncTask<Void, Void, Void>
|
||||||
|
{
|
||||||
|
public interface Listener
|
||||||
|
{
|
||||||
|
void onExportDBFinished(@Nullable String filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ProgressBar progressBar;
|
||||||
|
private String filename;
|
||||||
|
private Listener listener;
|
||||||
|
|
||||||
|
public ExportDBTask(ProgressBar progressBar)
|
||||||
|
{
|
||||||
|
this.progressBar = progressBar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListener(Listener listener)
|
||||||
|
{
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPreExecute()
|
||||||
|
{
|
||||||
|
if(progressBar != null)
|
||||||
|
{
|
||||||
|
progressBar.setIndeterminate(true);
|
||||||
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Void aVoid)
|
||||||
|
{
|
||||||
|
if(listener != null)
|
||||||
|
listener.onExportDBFinished(filename);
|
||||||
|
|
||||||
|
if(progressBar != null)
|
||||||
|
progressBar.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... params)
|
||||||
|
{
|
||||||
|
filename = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
File dir = DatabaseHelper.getFilesDir("Backups");
|
||||||
|
if(dir == null) return null;
|
||||||
|
|
||||||
|
filename = DatabaseHelper.saveDatabaseCopy(dir);
|
||||||
|
}
|
||||||
|
catch(IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* 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.tasks;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.io.GenericImporter;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class ImportDataTask extends AsyncTask<Void, Void, Void>
|
||||||
|
{
|
||||||
|
public static final int SUCCESS = 1;
|
||||||
|
public static final int NOT_RECOGNIZED = 2;
|
||||||
|
public static final int FAILED = 3;
|
||||||
|
|
||||||
|
public interface Listener
|
||||||
|
{
|
||||||
|
void onImportFinished(int result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final ProgressBar progressBar;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final File file;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Listener listener;
|
||||||
|
|
||||||
|
int result;
|
||||||
|
|
||||||
|
public ImportDataTask(@NonNull File file, @Nullable ProgressBar progressBar)
|
||||||
|
{
|
||||||
|
this.file = file;
|
||||||
|
this.progressBar = progressBar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListener(@Nullable Listener listener)
|
||||||
|
{
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPreExecute()
|
||||||
|
{
|
||||||
|
if(progressBar != null)
|
||||||
|
{
|
||||||
|
progressBar.setIndeterminate(true);
|
||||||
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Void aVoid)
|
||||||
|
{
|
||||||
|
if(progressBar != null)
|
||||||
|
progressBar.setVisibility(View.GONE);
|
||||||
|
|
||||||
|
if(listener != null) listener.onImportFinished(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... params)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
GenericImporter importer = new GenericImporter();
|
||||||
|
if(importer.canHandle(file))
|
||||||
|
{
|
||||||
|
importer.importHabitsFromFile(file);
|
||||||
|
result = SUCCESS;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = NOT_RECOGNIZED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
result = FAILED;
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue