Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ConnectionManager by properly unregistering callback #233

Merged
merged 1 commit into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package io.horizontalsystems.ethereumkit.core

import android.app.Application
import android.content.Context
import android.os.Handler
import android.os.Looper
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import io.horizontalsystems.ethereumkit.api.core.*
Expand Down Expand Up @@ -123,24 +121,14 @@ class EthereumKit(
started = false
blockchain.stop()
state.clear()
connectionManager.stop()
}

fun refresh() {
blockchain.refresh()
transactionSyncManager.sync()
}

fun onEnterForeground() {
connectionManager.onEnterForeground()
Handler(Looper.getMainLooper()).postDelayed({
blockchain.refresh()
}, 1000)
}

fun onEnterBackground() {
connectionManager.onEnterBackground()
}

fun getFullTransactionsFlowable(tags: List<List<String>>): Flowable<List<FullTransaction>> {
return transactionManager.getFullTransactionsFlowable(tags)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,15 @@ class ConnectionManager(context: Context) {
private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

var listener: Listener? = null
var isConnected = false
var isConnected = getInitialConnectionStatus()
private var hasValidInternet = false
private var hasConnection = false
private var callback = ConnectionStatusCallback()

init {
onEnterForeground()
}

fun onEnterForeground() {
setInitialValues()
try {
connectivityManager.unregisterNetworkCallback(callback)
} catch (e: Exception) {
//was not registered, or already unregistered
}
connectivityManager.registerNetworkCallback(NetworkRequest.Builder().build(), callback)
}

fun onEnterBackground() {
try {
connectivityManager.unregisterNetworkCallback(callback)
} catch (e: Exception) {
//already unregistered
}
}

private fun setInitialValues() {
isConnected = getInitialConnectionStatus()
}

private fun getInitialConnectionStatus(): Boolean {
val network = connectivityManager.activeNetwork ?: return false

Expand Down Expand Up @@ -93,4 +71,12 @@ class ConnectionManager(context: Context) {
listener?.onConnectionChange()
}
}

fun stop() {
try {
connectivityManager.unregisterNetworkCallback(callback)
} catch (e: Exception) {
//already unregistered
}
}
}