mirror of https://github.com/iSoron/uhabits.git
parent
9743b05a78
commit
f6699fbfda
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2020 Á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.util.*
|
||||||
|
import org.isoron.uhabits.core.io.*
|
||||||
|
|
||||||
|
class AndroidLogging : Logging {
|
||||||
|
override fun getLogger(name: String): Logger {
|
||||||
|
return AndroidLogger(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class AndroidLogger(val name: String) : Logger {
|
||||||
|
override fun info(msg: String) {
|
||||||
|
Log.i(name, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun debug(msg: String) {
|
||||||
|
Log.d(name, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun error(msg: String) {
|
||||||
|
Log.e(name, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun error(exception: Exception) {
|
||||||
|
Log.e(name, "Exception", exception)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2020 Á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.sync
|
||||||
|
|
||||||
|
import android.content.*
|
||||||
|
import android.net.*
|
||||||
|
import org.isoron.uhabits.core.sync.*
|
||||||
|
|
||||||
|
class AndroidNetworkManager(
|
||||||
|
val context: Context,
|
||||||
|
) : NetworkManager, ConnectivityManager.NetworkCallback() {
|
||||||
|
|
||||||
|
val listeners = mutableListOf<NetworkManager.Listener>()
|
||||||
|
var connected = false
|
||||||
|
|
||||||
|
init {
|
||||||
|
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
|
cm.registerNetworkCallback(NetworkRequest.Builder().build(), this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun addListener(listener: NetworkManager.Listener) {
|
||||||
|
if (connected) listener.onNetworkAvailable()
|
||||||
|
else listener.onNetworkLost()
|
||||||
|
listeners.add(listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun remoteListener(listener: NetworkManager.Listener) {
|
||||||
|
listeners.remove(listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onAvailable(network: Network) {
|
||||||
|
connected = true
|
||||||
|
for (l in listeners) l.onNetworkAvailable()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onLost(network: Network) {
|
||||||
|
connected = false
|
||||||
|
for (l in listeners) l.onNetworkLost()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2020 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.core.io
|
||||||
|
|
||||||
|
import java.lang.Exception
|
||||||
|
|
||||||
|
interface Logging {
|
||||||
|
fun getLogger(name: String): Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Logger {
|
||||||
|
fun info(msg: String)
|
||||||
|
fun debug(msg: String)
|
||||||
|
fun error(msg: String)
|
||||||
|
fun error(exception: Exception)
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2020 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.core.sync
|
||||||
|
|
||||||
|
interface NetworkManager {
|
||||||
|
fun addListener(listener: Listener)
|
||||||
|
fun remoteListener(listener: Listener)
|
||||||
|
interface Listener {
|
||||||
|
fun onNetworkAvailable()
|
||||||
|
fun onNetworkLost()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue