Convert more classes to Kotlin

pull/316/head^2
Alinson S. Xavier 8 years ago
parent 3783fd8506
commit efcb5710c0

@ -97,7 +97,7 @@ public class BaseAndroidTest extends TestCase
.appContextModule(new AppContextModule(targetContext.getApplicationContext()))
.build();
HabitsApplication.setComponent(appComponent);
HabitsApplication.Companion.setComponent(appComponent);
prefs = appComponent.getPreferences();
habitList = appComponent.getHabitList();
taskRunner = appComponent.getTaskRunner();

@ -17,39 +17,25 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits;
package org.isoron.uhabits
import android.support.annotation.*;
import android.util.*;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.utils.*;
import java.text.*;
import java.util.*;
import javax.inject.*;
import android.util.*
import org.isoron.uhabits.core.*
import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.core.utils.*
import java.util.*
import javax.inject.*
@AppScope
public class HabitLogger
{
@Inject
public HabitLogger()
{
}
public void logReminderScheduled(@NonNull Habit habit,
@NonNull Long reminderTime)
{
int min = Math.min(3, habit.getName().length());
String name = habit.getName().substring(0, min);
DateFormat df = DateFormats.getBackupDateFormat();
String time = df.format(new Date(reminderTime));
class HabitLogger
@Inject constructor() {
fun logReminderScheduled(habit: Habit, reminderTime: Long) {
val min = Math.min(3, habit.name.length)
val name = habit.name.substring(0, min)
val df = DateFormats.getBackupDateFormat()
val time = df.format(Date(reminderTime))
Log.i("ReminderHelper",
String.format("Setting alarm (%s): %s", time, name));
String.format("Setting alarm (%s): %s", time, name))
}
}

@ -1,130 +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;
import android.app.*;
import android.content.*;
import org.isoron.androidbase.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.widgets.*;
import java.io.*;
/**
* The Android application for Loop Habit Tracker.
*/
public class HabitsApplication extends Application
{
private Context context;
private static HabitsApplicationComponent component;
private WidgetUpdater widgetUpdater;
private ReminderScheduler reminderScheduler;
private NotificationTray notificationTray;
public HabitsApplicationComponent getComponent()
{
return component;
}
public static void setComponent(HabitsApplicationComponent component)
{
HabitsApplication.component = component;
}
public static boolean isTestMode()
{
try
{
Class.forName("org.isoron.uhabits.BaseAndroidTest");
return true;
}
catch (final ClassNotFoundException e)
{
return false;
}
}
@Override
public void onCreate()
{
super.onCreate();
context = this;
component = DaggerHabitsApplicationComponent
.builder()
.appContextModule(new AppContextModule(context))
.build();
if (isTestMode())
{
File db = DatabaseUtils.getDatabaseFile(context);
if (db.exists()) db.delete();
}
try
{
DatabaseUtils.initializeDatabase(context);
}
catch (UnsupportedDatabaseVersionException e)
{
File db = DatabaseUtils.getDatabaseFile(context);
db.renameTo(new File(db.getAbsolutePath() + ".invalid"));
DatabaseUtils.initializeDatabase(context);
}
widgetUpdater = component.getWidgetUpdater();
widgetUpdater.startListening();
reminderScheduler = component.getReminderScheduler();
reminderScheduler.startListening();
notificationTray = component.getNotificationTray();
notificationTray.startListening();
Preferences prefs = component.getPreferences();
prefs.setLastAppVersion(BuildConfig.VERSION_CODE);
TaskRunner taskRunner = component.getTaskRunner();
taskRunner.execute(() -> {
reminderScheduler.scheduleAll();
widgetUpdater.updateWidgets();
});
}
@Override
public void onTerminate()
{
context = null;
reminderScheduler.stopListening();
widgetUpdater.stopListening();
notificationTray.stopListening();
super.onTerminate();
}
}

@ -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
import android.app.*
import android.content.*
import org.isoron.androidbase.*
import org.isoron.uhabits.core.database.*
import org.isoron.uhabits.core.reminders.*
import org.isoron.uhabits.core.ui.*
import org.isoron.uhabits.utils.*
import org.isoron.uhabits.widgets.*
import java.io.*
/**
* The Android application for Loop Habit Tracker.
*/
class HabitsApplication : Application() {
private lateinit var context: Context
private lateinit var widgetUpdater: WidgetUpdater
private lateinit var reminderScheduler: ReminderScheduler
private lateinit var notificationTray: NotificationTray
override fun onCreate() {
super.onCreate()
context = this
HabitsApplication.component = DaggerHabitsApplicationComponent
.builder()
.appContextModule(AppContextModule(context))
.build()
if (isTestMode()) {
val db = DatabaseUtils.getDatabaseFile(context)
if (db.exists()) db.delete()
}
try {
DatabaseUtils.initializeDatabase(context)
} catch (e: UnsupportedDatabaseVersionException) {
val db = DatabaseUtils.getDatabaseFile(context)
db.renameTo(File(db.absolutePath + ".invalid"))
DatabaseUtils.initializeDatabase(context)
}
widgetUpdater = component.widgetUpdater
widgetUpdater.startListening()
reminderScheduler = component.reminderScheduler
reminderScheduler.startListening()
notificationTray = component.notificationTray
notificationTray.startListening()
val prefs = component.preferences
prefs.setLastAppVersion(BuildConfig.VERSION_CODE)
val taskRunner = component.taskRunner
taskRunner.execute {
reminderScheduler.scheduleAll()
widgetUpdater.updateWidgets()
}
}
override fun onTerminate() {
reminderScheduler.stopListening()
widgetUpdater.stopListening()
notificationTray.stopListening()
super.onTerminate()
}
val component: HabitsApplicationComponent
get() = HabitsApplication.component
companion object {
lateinit var component: HabitsApplicationComponent
fun isTestMode(): Boolean {
try {
Class.forName("org.isoron.uhabits.BaseAndroidTest")
return true
} catch (e: ClassNotFoundException) {
return false
}
}
}
}

@ -17,23 +17,16 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits;
package org.isoron.uhabits
import android.app.backup.BackupAgentHelper;
import android.app.backup.FileBackupHelper;
import android.app.backup.SharedPreferencesBackupHelper;
import android.app.backup.*
/**
* An Android BackupAgentHelper customized for this application.
*/
public class HabitsBackupAgent extends BackupAgentHelper
{
@Override
public void onCreate()
{
addHelper("preferences",
new SharedPreferencesBackupHelper(this, "preferences"));
addHelper("database",
new FileBackupHelper(this, "../databases/uhabits.db"));
class HabitsBackupAgent : BackupAgentHelper() {
override fun onCreate() {
addHelper("preferences", SharedPreferencesBackupHelper(this, "preferences"))
addHelper("database", FileBackupHelper(this, "../databases/uhabits.db"))
}
}

@ -1,65 +0,0 @@
/*
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
package org.isoron.uhabits;
import android.content.*;
import android.database.sqlite.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.database.*;
public class HabitsDatabaseOpener extends SQLiteOpenHelper
{
private final int version;
private MigrationHelper helper;
public HabitsDatabaseOpener(Context context,
String databaseFilename,
int version)
{
super(context, databaseFilename, null, version);
this.version = version;
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.setVersion(8);
onUpgrade(db, -1, version);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if(db.getVersion() < 8) throw new UnsupportedDatabaseVersionException();
helper = new MigrationHelper(new AndroidDatabase(db));
helper.migrateTo(newVersion);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
throw new UnsupportedDatabaseVersionException();
}
}

@ -0,0 +1,54 @@
/*
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
package org.isoron.uhabits
import android.content.*
import android.database.sqlite.*
import org.isoron.uhabits.core.database.*
import org.isoron.uhabits.database.*
class HabitsDatabaseOpener(
context: Context,
databaseFilename: String,
private val version: Int
) : SQLiteOpenHelper(context, databaseFilename, null, version) {
override fun onCreate(db: SQLiteDatabase) {
db.version = 8
onUpgrade(db, -1, version)
}
override fun onUpgrade(db: SQLiteDatabase,
oldVersion: Int,
newVersion: Int) {
if (db.version < 8) throw UnsupportedDatabaseVersionException()
val helper = MigrationHelper(AndroidDatabase(db))
helper.migrateTo(newVersion)
}
override fun onDowngrade(db: SQLiteDatabase,
oldVersion: Int,
newVersion: Int) {
throw UnsupportedDatabaseVersionException()
}
}

@ -1,98 +0,0 @@
/*
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.sqlite.*;
import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.database.*;
import org.isoron.uhabits.intents.*;
import org.isoron.uhabits.notifications.*;
import org.isoron.uhabits.preferences.*;
import org.isoron.uhabits.utils.*;
import dagger.*;
@Module
public class HabitsModule
{
@Provides
@AppScope
public static Preferences getPreferences(SharedPreferencesStorage storage)
{
return new Preferences(storage);
}
@Provides
@AppScope
public static ReminderScheduler getReminderScheduler(IntentScheduler sys,
CommandRunner commandRunner,
HabitList habitList)
{
return new ReminderScheduler(commandRunner, habitList, sys);
}
@Provides
@AppScope
public static NotificationTray getTray(TaskRunner taskRunner,
CommandRunner commandRunner,
Preferences preferences,
AndroidNotificationTray screen)
{
return new NotificationTray(taskRunner, commandRunner, preferences,
screen);
}
@Provides
@AppScope
public static WidgetPreferences getWidgetPreferences(
SharedPreferencesStorage storage)
{
return new WidgetPreferences(storage);
}
@Provides
public ModelFactory getModelFactory()
{
return new SQLModelFactory(
new AndroidDatabase(DatabaseUtils.openDatabase()));
}
@Provides
@AppScope
public HabitList getHabitList(SQLiteHabitList list)
{
return list;
}
@Provides
@AppScope
public DatabaseOpener getDatabaseOpener(AndroidDatabaseOpener opener)
{
return opener;
}
}

@ -0,0 +1,93 @@
/*
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits
import dagger.*
import org.isoron.uhabits.core.*
import org.isoron.uhabits.core.commands.*
import org.isoron.uhabits.core.database.*
import org.isoron.uhabits.core.models.*
import org.isoron.uhabits.core.models.sqlite.*
import org.isoron.uhabits.core.preferences.*
import org.isoron.uhabits.core.reminders.*
import org.isoron.uhabits.core.tasks.*
import org.isoron.uhabits.core.ui.*
import org.isoron.uhabits.database.*
import org.isoron.uhabits.intents.*
import org.isoron.uhabits.notifications.*
import org.isoron.uhabits.preferences.*
import org.isoron.uhabits.utils.*
@Module
class HabitsModule {
@Provides
@AppScope
fun getPreferences(storage: SharedPreferencesStorage): Preferences {
return Preferences(storage)
}
@Provides
@AppScope
fun getReminderScheduler(
sys: IntentScheduler,
commandRunner: CommandRunner,
habitList: HabitList
): ReminderScheduler {
return ReminderScheduler(commandRunner, habitList, sys)
}
@Provides
@AppScope
fun getTray(
taskRunner: TaskRunner,
commandRunner: CommandRunner,
preferences: Preferences,
screen: AndroidNotificationTray
): NotificationTray {
return NotificationTray(taskRunner, commandRunner, preferences, screen)
}
@Provides
@AppScope
fun getWidgetPreferences(
storage: SharedPreferencesStorage
): WidgetPreferences {
return WidgetPreferences(storage)
}
@Provides
@AppScope
fun getModelFactory(): ModelFactory {
return SQLModelFactory(AndroidDatabase(DatabaseUtils.openDatabase()))
}
@Provides
@AppScope
fun getHabitList(list: SQLiteHabitList): HabitList {
return list
}
@Provides
@AppScope
fun getDatabaseOpener(opener: AndroidDatabaseOpener): DatabaseOpener {
return opener
}
}

@ -1,139 +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.preferences;
import android.content.*;
import android.preference.*;
import android.support.annotation.*;
import org.isoron.androidbase.*;
import org.isoron.uhabits.R;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.preferences.*;
import javax.inject.*;
@AppScope
public class SharedPreferencesStorage
implements SharedPreferences.OnSharedPreferenceChangeListener,
Preferences.Storage
{
@NonNull
private SharedPreferences sharedPrefs;
@Nullable
private Preferences preferences;
@Inject
public SharedPreferencesStorage(@AppContext Context context)
{
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
sharedPrefs.registerOnSharedPreferenceChangeListener(this);
PreferenceManager.setDefaultValues(context, R.xml.preferences, false);
}
public void clear()
{
sharedPrefs.edit().clear().apply();
}
@Override
public boolean getBoolean(String key, boolean defValue)
{
return sharedPrefs.getBoolean(key, defValue);
}
@Override
public int getInt(String key, int defValue)
{
return sharedPrefs.getInt(key, defValue);
}
@Override
public long getLong(String key, int defValue)
{
return sharedPrefs.getLong(key, defValue);
}
@Override
public String getString(String key, String defValue)
{
return sharedPrefs.getString(key, defValue);
}
@Override
public void onAttached(Preferences preferences)
{
this.preferences = preferences;
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key)
{
if(preferences == null) return;
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
switch (key)
{
case "pref_checkmark_reverse_order":
preferences.setCheckmarkSequenceReversed(getBoolean(key, false));
break;
case "pref_sticky_notifications":
preferences.setNotificationsSticky(getBoolean(key, false));
break;
case "pref_feature_sync":
preferences.setSyncEnabled(getBoolean(key, false));
break;
}
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void putBoolean(String key, boolean value)
{
sharedPrefs.edit().putBoolean(key, value).apply();
}
@Override
public void putInt(String key, int value)
{
sharedPrefs.edit().putInt(key, value).apply();
}
@Override
public void putLong(String key, long value)
{
sharedPrefs.edit().putLong(key, value).apply();
}
@Override
public void putString(String key, String value)
{
sharedPrefs.edit().putString(key, value).apply();
}
@Override
public void remove(String key)
{
sharedPrefs.edit().remove(key).apply();
}
}

@ -0,0 +1,93 @@
/*
* 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.preferences
import android.content.*
import android.preference.*
import org.isoron.androidbase.*
import org.isoron.uhabits.R
import org.isoron.uhabits.core.*
import org.isoron.uhabits.core.preferences.*
import javax.inject.*
@AppScope
class SharedPreferencesStorage
@Inject constructor(
@AppContext context: Context
) : SharedPreferences.OnSharedPreferenceChangeListener, Preferences.Storage {
private val sharedPrefs: SharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context)
private var preferences: Preferences? = null
init {
sharedPrefs.registerOnSharedPreferenceChangeListener(this)
PreferenceManager.setDefaultValues(context, R.xml.preferences, false)
}
override fun clear() = sharedPrefs.edit().clear().apply()
override fun getBoolean(key: String, defValue: Boolean) =
sharedPrefs.getBoolean(key, defValue)
override fun getInt(key: String, defValue: Int) =
sharedPrefs.getInt(key, defValue)
override fun getLong(key: String, defValue: Int) =
sharedPrefs.getLong(key, defValue.toLong())
override fun getString(key: String, defValue: String): String =
sharedPrefs.getString(key, defValue)
override fun onAttached(preferences: Preferences) {
this.preferences = preferences
}
override fun putBoolean(key: String, value: Boolean) =
sharedPrefs.edit().putBoolean(key, value).apply()
override fun putInt(key: String, value: Int) =
sharedPrefs.edit().putInt(key, value).apply()
override fun putLong(key: String, value: Long) =
sharedPrefs.edit().putLong(key, value).apply()
override fun putString(key: String, value: String) =
sharedPrefs.edit().putString(key, value).apply()
override fun remove(key: String) =
sharedPrefs.edit().remove(key).apply()
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences,
key: String) {
val preferences = this.preferences ?: return
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
when (key) {
"pref_checkmark_reverse_order" ->
preferences.isCheckmarkSequenceReversed = getBoolean(key, false)
"pref_sticky_notifications" ->
preferences.setNotificationsSticky(getBoolean(key, false))
"pref_feature_sync" ->
preferences.isSyncEnabled = getBoolean(key, false)
}
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
}
}

@ -1,52 +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.receivers;
import android.content.*;
import android.net.*;
import android.support.annotation.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.sync.*;
import static android.content.Context.*;
public class ConnectivityReceiver extends BroadcastReceiver
{
@Override
public void onReceive(@Nullable Context context, @Nullable Intent intent)
{
if (context == null) return;
if (intent == null) return;
HabitsApplicationComponent component =
((HabitsApplication) context.getApplicationContext()).getComponent();
NetworkInfo networkInfo =
((ConnectivityManager) context.getSystemService(
CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
boolean isConnected =
(networkInfo != null && networkInfo.isConnectedOrConnecting());
SyncManager syncManager = component.getSyncManager();
syncManager.onNetworkStatusChanged(isConnected);
}
}

@ -0,0 +1,38 @@
/*
* 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.receivers
import android.content.*
import android.content.Context.*
import android.net.*
import org.isoron.uhabits.*
class ConnectivityReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (context == null || intent == null) return
val app = context.applicationContext as HabitsApplication
val networkInfo = (context.getSystemService(CONNECTIVITY_SERVICE)
as ConnectivityManager).activeNetworkInfo
val isConnected = (networkInfo != null) &&
networkInfo.isConnectedOrConnecting
val syncManager = app.component.syncManager
syncManager.onNetworkStatusChanged(isConnected)
}
}

@ -76,7 +76,7 @@ public abstract class DatabaseUtils
public static String getDatabaseFilename()
{
String databaseFilename = Config.DATABASE_FILENAME;
if (HabitsApplication.isTestMode()) databaseFilename = "test.db";
if (HabitsApplication.Companion.isTestMode()) databaseFilename = "test.db";
return databaseFilename;
}

Loading…
Cancel
Save