mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
@@ -20,14 +20,16 @@
|
|||||||
package org.isoron.uhabits;
|
package org.isoron.uhabits;
|
||||||
|
|
||||||
import android.app.Application;
|
import android.app.Application;
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
import com.activeandroid.ActiveAndroid;
|
import com.activeandroid.ActiveAndroid;
|
||||||
import com.activeandroid.Configuration;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.helpers.DatabaseHelper;
|
import org.isoron.uhabits.helpers.DatabaseHelper;
|
||||||
|
|
||||||
public class HabitsApplication extends Application
|
public class HabitsApplication extends Application
|
||||||
{
|
{
|
||||||
|
private static Context context;
|
||||||
|
|
||||||
private boolean isTestMode()
|
private boolean isTestMode()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -41,10 +43,17 @@ public class HabitsApplication extends Application
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Context getContext()
|
||||||
|
{
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate()
|
public void onCreate()
|
||||||
{
|
{
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
HabitsApplication.context = this;
|
||||||
|
|
||||||
String databaseFilename = BuildConfig.databaseFilename;
|
String databaseFilename = BuildConfig.databaseFilename;
|
||||||
|
|
||||||
if (isTestMode())
|
if (isTestMode())
|
||||||
@@ -53,17 +62,13 @@ public class HabitsApplication extends Application
|
|||||||
DatabaseHelper.deleteDatabase(this, databaseFilename);
|
DatabaseHelper.deleteDatabase(this, databaseFilename);
|
||||||
}
|
}
|
||||||
|
|
||||||
Configuration dbConfig = new Configuration.Builder(this)
|
DatabaseHelper.initializeActiveAndroid(this, databaseFilename);
|
||||||
.setDatabaseName(databaseFilename)
|
|
||||||
.setDatabaseVersion(BuildConfig.databaseVersion)
|
|
||||||
.create();
|
|
||||||
|
|
||||||
ActiveAndroid.initialize(dbConfig);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onTerminate()
|
public void onTerminate()
|
||||||
{
|
{
|
||||||
|
HabitsApplication.context = null;
|
||||||
ActiveAndroid.dispose();
|
ActiveAndroid.dispose();
|
||||||
super.onTerminate();
|
super.onTerminate();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,14 @@ import android.support.annotation.NonNull;
|
|||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
|
|
||||||
import com.activeandroid.ActiveAndroid;
|
import com.activeandroid.ActiveAndroid;
|
||||||
|
import com.activeandroid.Configuration;
|
||||||
|
|
||||||
import org.isoron.uhabits.BuildConfig;
|
import org.isoron.uhabits.BuildConfig;
|
||||||
|
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.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
@@ -68,7 +74,7 @@ public class DatabaseHelper
|
|||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private static File getDatabaseFile(Context context, String databaseFilename)
|
public static File getDatabaseFile(Context context, String databaseFilename)
|
||||||
{
|
{
|
||||||
return new File(String.format("%s/../databases/%s",
|
return new File(String.format("%s/../databases/%s",
|
||||||
context.getApplicationContext().getFilesDir().getPath(), databaseFilename));
|
context.getApplicationContext().getFilesDir().getPath(), databaseFilename));
|
||||||
@@ -85,4 +91,17 @@ public class DatabaseHelper
|
|||||||
dir.mkdirs();
|
dir.mkdirs();
|
||||||
return dir;
|
return dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static void initializeActiveAndroid(Context context, String databaseFilename)
|
||||||
|
{
|
||||||
|
Configuration dbConfig = new Configuration.Builder(context)
|
||||||
|
.setDatabaseName(databaseFilename)
|
||||||
|
.setDatabaseVersion(BuildConfig.databaseVersion)
|
||||||
|
.addModelClasses(Checkmark.class, Habit.class, Repetition.class, Score.class,
|
||||||
|
Streak.class)
|
||||||
|
.create();
|
||||||
|
|
||||||
|
ActiveAndroid.initialize(dbConfig);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ public class GenericImporter extends AbstractImporter
|
|||||||
public GenericImporter()
|
public GenericImporter()
|
||||||
{
|
{
|
||||||
importers = new LinkedList<>();
|
importers = new LinkedList<>();
|
||||||
|
importers.add(new LoopDBImporter());
|
||||||
importers.add(new RewireDBImporter());
|
importers.add(new RewireDBImporter());
|
||||||
importers.add(new TickmateDBImporter());
|
importers.add(new TickmateDBImporter());
|
||||||
importers.add(new HabitBullCSVImporter());
|
importers.add(new HabitBullCSVImporter());
|
||||||
|
|||||||
68
app/src/main/java/org/isoron/uhabits/io/LoopDBImporter.java
Normal file
68
app/src/main/java/org/isoron/uhabits/io/LoopDBImporter.java
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* 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.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.activeandroid.ActiveAndroid;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.BuildConfig;
|
||||||
|
import org.isoron.uhabits.HabitsApplication;
|
||||||
|
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();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
ActiveAndroid.dispose();
|
||||||
|
Context context = HabitsApplication.getContext();
|
||||||
|
|
||||||
|
File originalDB = DatabaseHelper.getDatabaseFile(context, BuildConfig.databaseFilename);
|
||||||
|
File backupDir = DatabaseHelper.getFilesDir(context, "Backups");
|
||||||
|
|
||||||
|
DatabaseHelper.saveDatabaseCopy(context, backupDir);
|
||||||
|
DatabaseHelper.copy(file, originalDB);
|
||||||
|
DatabaseHelper.initializeActiveAndroid(context, BuildConfig.databaseFilename);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user