Remove dependency: ActiveAndroid

pull/312/head
Alinson S. Xavier 8 years ago
parent 6d06e06840
commit 0a5d565030

@ -0,0 +1,79 @@
/*
* 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.androidbase.storage;
import android.content.*;
import android.database.sqlite.*;
import org.isoron.androidbase.*;
import java.io.*;
import java.util.*;
public class BaseSQLiteOpenHelper extends SQLiteOpenHelper
{
private final Context context;
private final int version;
public BaseSQLiteOpenHelper(@AppContext Context context,
String databaseFilename,
int version)
{
super(context, databaseFilename, null, version);
this.context = context;
this.version = version;
}
@Override
public void onCreate(SQLiteDatabase db)
{
executeMigrations(db, -1, version);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
executeMigrations(db, oldVersion, newVersion);
}
private void executeMigrations(SQLiteDatabase db,
int oldVersion,
int newVersion)
{
try
{
for (int v = oldVersion + 1; v <= newVersion; v++)
{
String fname = String.format(Locale.US, "migrations/%d.sql", v);
InputStream stream = context.getAssets().open(fname);
for (String command : SQLParser.parse(stream))
db.execSQL(command);
}
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}

@ -0,0 +1,163 @@
/*
* Copyright (C) 2014 Markus Pfeiffer
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.isoron.androidbase.storage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
class Tokenizer {
private final InputStream mStream;
private boolean mIsNext;
private int mCurrent;
public Tokenizer(final InputStream in) {
this.mStream = in;
}
public boolean hasNext() throws IOException {
if (!this.mIsNext) {
this.mIsNext = true;
this.mCurrent = this.mStream.read();
}
return this.mCurrent != -1;
}
public int next() throws IOException {
if (!this.mIsNext) {
this.mCurrent = this.mStream.read();
}
this.mIsNext = false;
return this.mCurrent;
}
public boolean skip(final String s) throws IOException {
if (s == null || s.length() == 0) {
return false;
}
if (s.charAt(0) != this.mCurrent) {
return false;
}
final int len = s.length();
this.mStream.mark(len - 1);
for (int n = 1; n < len; n++) {
final int value = this.mStream.read();
if (value != s.charAt(n)) {
this.mStream.reset();
return false;
}
}
return true;
}
}
public class SQLParser {
public final static int STATE_NONE = 0;
public final static int STATE_STRING = 1;
public final static int STATE_COMMENT = 2;
public final static int STATE_COMMENT_BLOCK = 3;
public static List<String> parse(final InputStream stream) throws IOException {
final BufferedInputStream buffer = new BufferedInputStream(stream);
final List<String> commands = new ArrayList<String>();
final StringBuffer sb = new StringBuffer();
try {
final Tokenizer tokenizer = new Tokenizer(buffer);
int state = STATE_NONE;
while (tokenizer.hasNext()) {
final char c = (char) tokenizer.next();
if (state == STATE_COMMENT_BLOCK) {
if (tokenizer.skip("*/")) {
state = STATE_NONE;
}
continue;
} else if (state == STATE_COMMENT) {
if (isNewLine(c)) {
state = STATE_NONE;
}
continue;
} else if (state == STATE_NONE && tokenizer.skip("/*")) {
state = STATE_COMMENT_BLOCK;
continue;
} else if (state == STATE_NONE && tokenizer.skip("--")) {
state = STATE_COMMENT;
continue;
} else if (state == STATE_NONE && c == ';') {
final String command = sb.toString().trim();
commands.add(command);
sb.setLength(0);
continue;
} else if (state == STATE_NONE && c == '\'') {
state = STATE_STRING;
} else if (state == STATE_STRING && c == '\'') {
state = STATE_NONE;
}
if (state == STATE_NONE || state == STATE_STRING) {
if (state == STATE_NONE && isWhitespace(c)) {
if (sb.length() > 0 && sb.charAt(sb.length() - 1) != ' ') {
sb.append(' ');
}
} else {
sb.append(c);
}
}
}
} finally {
buffer.close();
}
if (sb.length() > 0) {
commands.add(sb.toString().trim());
}
return commands;
}
private static boolean isNewLine(final char c) {
return c == '\r' || c == '\n';
}
private static boolean isWhitespace(final char c) {
return c == '\r' || c == '\n' || c == '\t' || c == ' ';
}
}

@ -64,7 +64,6 @@ dependencies {
implementation 'com.github.paolorotolo:appintro:3.4.0'
implementation 'com.google.dagger:dagger:2.9'
implementation 'com.jakewharton:butterknife:8.6.1-SNAPSHOT'
implementation 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
implementation 'org.apmem.tools:layouts:1.10'
implementation 'org.jetbrains:annotations-java5:15.0'
implementation 'com.google.code.gson:gson:2.7'

@ -22,12 +22,11 @@ package org.isoron.uhabits.models.sqlite;
import android.support.test.runner.*;
import android.test.suitebuilder.annotation.*;
import com.activeandroid.*;
import org.isoron.androidbase.storage.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.models.sqlite.records.*;
import org.isoron.uhabits.utils.*;
import org.junit.*;
import org.junit.runner.*;
@ -41,7 +40,7 @@ public class HabitRecordTest extends BaseAndroidTest
private Habit habit;
private SQLiteRepository<HabitRecord> sqlite =
new SQLiteRepository<>(HabitRecord.class, Cache.openDatabase());
new SQLiteRepository<>(HabitRecord.class, DatabaseUtils.openDatabase());
@Before
@Override

@ -22,13 +22,13 @@ package org.isoron.uhabits.models.sqlite;
import android.support.test.runner.*;
import android.test.suitebuilder.annotation.*;
import com.activeandroid.*;
import com.google.common.collect.*;
import org.isoron.androidbase.storage.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.models.sqlite.records.*;
import org.isoron.uhabits.utils.*;
import org.junit.*;
import org.junit.rules.*;
import org.junit.runner.*;
@ -61,7 +61,8 @@ public class SQLiteHabitListTest extends BaseAndroidTest
modelFactory = component.getModelFactory();
repository =
new SQLiteRepository<>(HabitRecord.class, Cache.openDatabase());
new SQLiteRepository<>(HabitRecord.class,
DatabaseUtils.openDatabase());
for (int i = 0; i < 10; i++)
{

@ -23,13 +23,12 @@ import android.support.annotation.*;
import android.support.test.runner.*;
import android.test.suitebuilder.annotation.*;
import com.activeandroid.*;
import org.isoron.androidbase.storage.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.models.sqlite.records.*;
import org.isoron.uhabits.utils.*;
import org.junit.*;
import org.junit.runner.*;
@ -63,7 +62,7 @@ public class SQLiteRepetitionListTest extends BaseAndroidTest
today = DateUtils.getStartOfToday();
day = DateUtils.millisecondsInOneDay;
sqlite = new SQLiteRepository<>(RepetitionRecord.class,
Cache.openDatabase());
DatabaseUtils.openDatabase());
}
@Test

@ -23,11 +23,10 @@ import android.database.sqlite.*;
import android.support.test.runner.*;
import android.test.suitebuilder.annotation.*;
import com.activeandroid.*;
import org.apache.commons.lang3.builder.*;
import org.isoron.androidbase.storage.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.utils.*;
import org.junit.*;
import org.junit.runner.*;
@ -47,10 +46,9 @@ public class SQLiteRepositoryTest extends BaseAndroidTest
public void setUp()
{
super.setUp();
repository =
new SQLiteRepository<>(ThingRecord.class, Cache.openDatabase());
this.db = DatabaseUtils.openDatabase();
repository = new SQLiteRepository<>(ThingRecord.class, db);
db = Cache.openDatabase();
db.execSQL("drop table if exists tests");
db.execSQL("create table tests(" +
"id integer not null primary key autoincrement, " +

@ -22,8 +22,6 @@ package org.isoron.uhabits;
import android.app.*;
import android.content.*;
import com.activeandroid.*;
import org.isoron.androidbase.*;
import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
@ -92,13 +90,13 @@ public class HabitsApplication extends Application
try
{
DatabaseUtils.initializeActiveAndroid(context);
DatabaseUtils.initializeDatabase(context);
}
catch (InvalidDatabaseVersionException e)
{
File db = DatabaseUtils.getDatabaseFile(context);
db.renameTo(new File(db.getAbsolutePath() + ".invalid"));
DatabaseUtils.initializeActiveAndroid(context);
DatabaseUtils.initializeDatabase(context);
}
widgetUpdater = component.getWidgetUpdater();
@ -124,8 +122,6 @@ public class HabitsApplication extends Application
public void onTerminate()
{
context = null;
ActiveAndroid.dispose();
reminderScheduler.stopListening();
widgetUpdater.stopListening();
notificationTray.stopListening();

@ -21,7 +21,6 @@ package org.isoron.uhabits.io;
import android.support.annotation.*;
import com.activeandroid.*;
import com.opencsv.*;
import org.isoron.uhabits.core.models.*;
@ -32,6 +31,8 @@ import java.util.*;
import javax.inject.*;
import static org.isoron.uhabits.utils.DatabaseUtils.executeAsTransaction;
/**
* Class that imports data from HabitBull CSV files.
*/
@ -59,16 +60,7 @@ public class HabitBullCSVImporter extends AbstractImporter
@Override
public void importHabitsFromFile(@NonNull final File file) throws IOException
{
ActiveAndroid.beginTransaction();
try
{
parseFile(file);
ActiveAndroid.setTransactionSuccessful();
}
finally
{
ActiveAndroid.endTransaction();
}
executeAsTransaction(() -> parseFile(file));
}
private void parseFile(@NonNull File file) throws IOException

@ -25,8 +25,6 @@ import android.database.sqlite.*;
import android.support.annotation.*;
import android.util.*;
import com.activeandroid.*;
import org.isoron.androidbase.*;
import org.isoron.androidbase.utils.*;
import org.isoron.uhabits.BuildConfig;
@ -89,9 +87,9 @@ public class LoopDBImporter extends AbstractImporter
@Override
public void importHabitsFromFile(@NonNull File file) throws IOException
{
ActiveAndroid.dispose();
DatabaseUtils.dispose();
File originalDB = DatabaseUtils.getDatabaseFile(context);
FileUtils.copy(file, originalDB);
DatabaseUtils.initializeActiveAndroid(context);
DatabaseUtils.initializeDatabase(context);
}
}

@ -22,12 +22,11 @@ package org.isoron.uhabits.models.sqlite;
import android.database.sqlite.*;
import android.support.annotation.*;
import com.activeandroid.*;
import org.isoron.androidbase.storage.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.memory.*;
import org.isoron.uhabits.models.sqlite.records.*;
import org.isoron.uhabits.utils.*;
import java.util.*;
@ -58,7 +57,7 @@ public class SQLiteHabitList extends HabitList
this.list = new MemoryHabitList();
repository =
new SQLiteRepository<>(HabitRecord.class, Cache.openDatabase());
new SQLiteRepository<>(HabitRecord.class, DatabaseUtils.openDatabase());
}
private void loadRecords()
@ -185,7 +184,7 @@ public class SQLiteHabitList extends HabitList
{
loadRecords();
list.removeAll();
SQLiteDatabase db = Cache.openDatabase();
SQLiteDatabase db = DatabaseUtils.openDatabase();
db.execSQL("delete from habits");
db.execSQL("delete from repetitions");
}
@ -206,7 +205,7 @@ public class SQLiteHabitList extends HabitList
Integer fromPos = fromRecord.position;
Integer toPos = toRecord.position;
SQLiteDatabase db = Cache.openDatabase();
SQLiteDatabase db = DatabaseUtils.openDatabase();
if (toPos < fromPos)
{
db.execSQL("update habits set position = position + 1 " +

@ -22,12 +22,11 @@ package org.isoron.uhabits.models.sqlite;
import android.support.annotation.*;
import android.support.annotation.Nullable;
import com.activeandroid.*;
import org.isoron.androidbase.storage.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.memory.*;
import org.isoron.uhabits.models.sqlite.records.*;
import org.isoron.uhabits.utils.*;
import org.jetbrains.annotations.*;
import java.util.*;
@ -47,7 +46,7 @@ public class SQLiteRepetitionList extends RepetitionList
{
super(habit);
repository = new SQLiteRepository<>(RepetitionRecord.class,
Cache.openDatabase());
DatabaseUtils.openDatabase());
list = new MemoryRepetitionList(habit);
}

@ -19,8 +19,6 @@
package org.isoron.uhabits.models.sqlite.records;
import com.activeandroid.*;
import org.apache.commons.lang3.builder.*;
import org.isoron.androidbase.storage.*;
import org.isoron.uhabits.core.models.*;
@ -29,67 +27,51 @@ import org.isoron.uhabits.core.models.*;
* The SQLite database record corresponding to a {@link Habit}.
*/
@Table(name = "habits")
@com.activeandroid.annotation.Table(name = "Habits")
public class HabitRecord extends Model
public class HabitRecord
{
@Column
@com.activeandroid.annotation.Column
public String description;
@Column
@com.activeandroid.annotation.Column
public String name;
@Column(name = "freq_num")
@com.activeandroid.annotation.Column(name = "freq_num")
public Integer freqNum;
@Column(name = "freq_den")
@com.activeandroid.annotation.Column(name = "freq_den")
public Integer freqDen;
@Column
@com.activeandroid.annotation.Column
public Integer color;
@Column
@com.activeandroid.annotation.Column
public Integer position;
@Column(name = "reminder_hour")
@com.activeandroid.annotation.Column(name = "reminder_hour")
public Integer reminderHour;
@Column(name = "reminder_min")
@com.activeandroid.annotation.Column(name = "reminder_min")
public Integer reminderMin;
@Column(name = "reminder_days")
@com.activeandroid.annotation.Column(name = "reminder_days")
public Integer reminderDays;
@Column
@com.activeandroid.annotation.Column
public Integer highlight;
@Column
@com.activeandroid.annotation.Column
public Integer archived;
@Column
@com.activeandroid.annotation.Column
public Integer type;
@Column(name = "target_value")
@com.activeandroid.annotation.Column(name = "target_value")
public Double targetValue;
@Column(name = "target_type")
@com.activeandroid.annotation.Column(name = "target_type")
public Integer targetType;
@Column
@com.activeandroid.annotation.Column
public String unit;
@Column

@ -19,8 +19,6 @@
package org.isoron.uhabits.models.sqlite.records;
import com.activeandroid.*;
import org.isoron.androidbase.storage.*;
import org.isoron.uhabits.core.models.*;
@ -28,21 +26,17 @@ import org.isoron.uhabits.core.models.*;
* The SQLite database record corresponding to a {@link Repetition}.
*/
@Table(name = "Repetitions")
@com.activeandroid.annotation.Table(name = "Repetitions")
public class RepetitionRecord extends Model
public class RepetitionRecord
{
@com.activeandroid.annotation.Column(name = "habit")
public HabitRecord habit;
@Column(name = "habit")
public Long habit_id;
@Column
@com.activeandroid.annotation.Column(name = "timestamp")
public Long timestamp;
@Column
@com.activeandroid.annotation.Column(name = "value")
public Integer value;
@Column

@ -19,18 +19,17 @@
package org.isoron.uhabits.sync;
import android.support.annotation.NonNull;
import android.support.annotation.*;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Select;
import java.util.List;
import org.isoron.androidbase.storage.*;
@Table(name = "Events")
public class Event extends Model
public class Event
{
@Nullable
@Column
public Long id;
@NonNull
@Column(name = "timestamp")
public Long timestamp;
@ -56,10 +55,4 @@ public class Event extends Model
this.timestamp = timestamp;
this.message = message;
}
@NonNull
public static List<Event> getAll()
{
return new Select().from(Event.class).orderBy("timestamp").execute();
}
}

@ -23,10 +23,12 @@ import android.support.annotation.*;
import android.util.*;
import org.isoron.androidbase.*;
import org.isoron.androidbase.storage.*;
import org.isoron.uhabits.BuildConfig;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.utils.*;
import org.json.*;
import java.net.*;
@ -95,6 +97,8 @@ public class SyncManager implements CommandRunner.Listener
private SSLContextProvider sslProvider;
private final SQLiteRepository<Event> repository;
@Inject
public SyncManager(@NonNull SSLContextProvider sslProvider,
@NonNull Preferences prefs,
@ -109,8 +113,10 @@ public class SyncManager implements CommandRunner.Listener
this.commandParser = commandParser;
this.isListening = false;
repository =
new SQLiteRepository<>(Event.class, DatabaseUtils.openDatabase());
pendingConfirmation = new LinkedList<>();
pendingEmit = new LinkedList<>(Event.getAll());
pendingEmit = new LinkedList<>(repository.findAll("order by timestamp"));
groupKey = prefs.getSyncKey();
clientId = prefs.getSyncClientId();
@ -129,7 +135,7 @@ public class SyncManager implements CommandRunner.Listener
JSONObject msg = toJSONObject(command.toJson());
Long now = new Date().getTime();
Event e = new Event(command.getId(), now, msg.toString());
e.save();
repository.save(e);
Log.i("SyncManager", "Adding to outbox: " + msg.toString());
@ -337,7 +343,7 @@ public class SyncManager implements CommandRunner.Listener
{
Log.i("SyncManager", "Pending command confirmed");
pendingConfirmation.remove(e);
e.delete();
repository.remove(e);
return;
}
}

@ -20,33 +20,41 @@
package org.isoron.uhabits.utils;
import android.content.*;
import android.database.sqlite.*;
import android.support.annotation.*;
import com.activeandroid.*;
import org.isoron.androidbase.storage.*;
import org.isoron.androidbase.utils.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.models.sqlite.*;
import org.isoron.uhabits.models.sqlite.records.*;
import org.isoron.uhabits.sync.*;
import java.io.*;
import java.text.*;
public abstract class DatabaseUtils
{
@Nullable
private static BaseSQLiteOpenHelper helper = null;
public static void executeAsTransaction(Callback callback)
{
ActiveAndroid.beginTransaction();
try
try (SQLiteDatabase db = openDatabase())
{
callback.execute();
ActiveAndroid.setTransactionSuccessful();
}
finally
{
ActiveAndroid.endTransaction();
db.beginTransaction();
try
{
callback.execute();
db.setTransactionSuccessful();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
finally
{
db.endTransaction();
}
}
}
@ -71,28 +79,24 @@ public abstract class DatabaseUtils
}
@SuppressWarnings("unchecked")
public static void initializeActiveAndroid(Context context)
public static void initializeDatabase(Context context)
{
Configuration dbConfig = new Configuration.Builder(context)
.setDatabaseName(getDatabaseFilename())
.setDatabaseVersion(BuildConfig.databaseVersion)
.addModelClasses(HabitRecord.class, RepetitionRecord.class,
Event.class).create();
try
{
ActiveAndroid.initialize(dbConfig);
helper = new BaseSQLiteOpenHelper(context, getDatabaseFilename(),
BuildConfig.databaseVersion);
}
catch (RuntimeException e)
{
if(e.getMessage().contains("downgrade"))
if (e.getMessage().contains("downgrade"))
throw new InvalidDatabaseVersionException();
else throw e;
}
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static String saveDatabaseCopy(Context context, File dir) throws IOException
public static String saveDatabaseCopy(Context context, File dir)
throws IOException
{
SimpleDateFormat dateFormat = DateFormats.getBackupDateFormat();
String date = dateFormat.format(DateUtils.getLocalTime());
@ -106,8 +110,20 @@ public abstract class DatabaseUtils
return dbCopy.getAbsolutePath();
}
@NonNull
public static SQLiteDatabase openDatabase()
{
if(helper == null) throw new IllegalStateException();
return helper.getWritableDatabase();
}
public static void dispose()
{
helper = null;
}
public interface Callback
{
void execute();
void execute() throws Exception;
}
}

@ -25,14 +25,15 @@ import android.os.*;
import android.support.annotation.*;
import android.widget.*;
import com.activeandroid.util.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.preferences.*;
import static android.appwidget.AppWidgetManager.*;
import static org.isoron.androidbase.utils.InterfaceUtils.*;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH;
import static org.isoron.androidbase.utils.InterfaceUtils.dpToPixels;
public abstract class BaseWidgetProvider extends AppWidgetProvider
{
@ -109,7 +110,7 @@ public abstract class BaseWidgetProvider extends AppWidgetProvider
}
catch (HabitNotFoundException e)
{
Log.e("BaseWidgetProvider", e);
e.printStackTrace();
}
}
}

Loading…
Cancel
Save