Use new API for the network status at Android 13

This commit is contained in:
Jonas Lochmann 2022-09-19 02:00:00 +02:00
parent 2f71c301a0
commit 21fa778a32
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
8 changed files with 109 additions and 27 deletions

View file

@ -1,5 +1,5 @@
/*
* TimeLimit Copyright <C> 2019 Jonas Lochmann
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -26,7 +26,7 @@ import io.timelimit.android.data.backup.DatabaseBackup
import io.timelimit.android.integration.platform.android.AndroidIntegration
import io.timelimit.android.integration.time.RealTimeApi
import io.timelimit.android.sync.network.api.HttpServerApi
import io.timelimit.android.sync.websocket.NetworkStatusUtil
import io.timelimit.android.sync.websocket.networkstatus.NetworkStatusUtil
import io.timelimit.android.sync.websocket.SocketIoWebsocketClient
import java.util.concurrent.CountDownLatch
@ -55,7 +55,7 @@ object AndroidAppLogic {
}
)
},
networkStatus = NetworkStatusUtil(safeContext),
networkStatus = NetworkStatusUtil.createWith(safeContext),
websocketClientCreator = SocketIoWebsocketClient.creator,
context = safeContext,
isInitialized = isInitialized

View file

@ -28,7 +28,7 @@ import io.timelimit.android.livedata.*
import io.timelimit.android.logic.applist.SyncInstalledAppsLogic
import io.timelimit.android.sync.SyncUtil
import io.timelimit.android.sync.network.api.ServerApi
import io.timelimit.android.sync.websocket.NetworkStatusInterface
import io.timelimit.android.sync.websocket.networkstatus.NetworkStatusInterface
import io.timelimit.android.sync.websocket.WebsocketClientCreator
import io.timelimit.android.ui.widget.TimesWidgetProvider

View file

@ -1,5 +1,5 @@
/*
* TimeLimit Copyright <C> 2019 - 2021 Jonas Lochmann
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -24,8 +24,8 @@ import io.timelimit.android.integration.time.DummyTimeApi
import io.timelimit.android.livedata.liveDataFromNonNullValue
import io.timelimit.android.sync.network.api.DummyServerApi
import io.timelimit.android.sync.websocket.DummyWebsocketClient
import io.timelimit.android.sync.websocket.NetworkStatus
import io.timelimit.android.sync.websocket.NetworkStatusInterface
import io.timelimit.android.sync.websocket.networkstatus.NetworkStatus
import io.timelimit.android.sync.websocket.networkstatus.NetworkStatusInterface
class TestAppLogic(maximumProtectionLevel: ProtectionLevel, context: Context) {
val platformIntegration = DummyIntegration(maximumProtectionLevel)

View file

@ -1,5 +1,5 @@
/*
* TimeLimit Copyright <C> 2019 - 2020 Jonas Lochmann
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -23,7 +23,7 @@ import io.timelimit.android.coroutines.runAsyncExpectForever
import io.timelimit.android.data.model.ExperimentalFlags
import io.timelimit.android.data.model.UserType
import io.timelimit.android.livedata.*
import io.timelimit.android.sync.websocket.NetworkStatus
import io.timelimit.android.sync.websocket.networkstatus.NetworkStatus
import io.timelimit.android.sync.websocket.WebsocketClient
import io.timelimit.android.sync.websocket.WebsocketClientCreator
import io.timelimit.android.sync.websocket.WebsocketClientListener

View file

@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package io.timelimit.android.sync.websocket
package io.timelimit.android.sync.websocket.networkstatus
import android.content.BroadcastReceiver
import android.content.Context
@ -21,15 +21,12 @@ import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.net.NetworkInfo
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import io.timelimit.android.BuildConfig
import io.timelimit.android.async.Threads
import io.timelimit.android.extensions.registerNotExportedReceiver
import io.timelimit.android.livedata.castDown
import io.timelimit.android.livedata.ignoreUnchanged
class NetworkStatusUtil (context: Context): NetworkStatusInterface {
class LegacyNetworkStatusUtil (context: Context): NetworkStatusInterface {
companion object {
private val handler = Threads.mainThreadHandler
}
@ -62,7 +59,9 @@ class NetworkStatusUtil (context: Context): NetworkStatusInterface {
if (BuildConfig.hasServer) {
if (didRegister) context.applicationContext.unregisterReceiver(receiver)
context.applicationContext.registerNotExportedReceiver(receiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); didRegister = true
context.applicationContext.registerReceiver(receiver, IntentFilter(
ConnectivityManager.CONNECTIVITY_ACTION)
); didRegister = true
handler.postDelayed(refreshRunnable, 15 * 1000 /* 15 seconds */)
}
@ -70,13 +69,3 @@ class NetworkStatusUtil (context: Context): NetworkStatusInterface {
init { forceRefresh() }
}
enum class NetworkStatus {
Offline, Online
}
interface NetworkStatusInterface {
fun forceRefresh()
val status: LiveData<NetworkStatus>
}

View file

@ -0,0 +1,56 @@
/*
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
*
* This program 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 version 3 of the License.
*
* This program 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 <https://www.gnu.org/licenses/>.
*/
package io.timelimit.android.sync.websocket.networkstatus
import android.content.Context
import android.net.ConnectivityManager
import android.net.ConnectivityManager.NetworkCallback
import android.net.Network
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import androidx.lifecycle.MutableLiveData
import io.timelimit.android.async.Threads
import io.timelimit.android.livedata.castDown
@RequiresApi(Build.VERSION_CODES.O)
class ModernNetworkStatusUtil(context: Context): NetworkStatusInterface {
private val connectivityManager = context.getSystemService<ConnectivityManager>()!!
private val statusInternal = MutableLiveData<NetworkStatus>().apply { value = NetworkStatus.Offline }
override val status = statusInternal.castDown()
init {
connectivityManager.registerDefaultNetworkCallback(
object: NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
statusInternal.value = NetworkStatus.Online
}
override fun onLost(network: Network) {
super.onLost(network)
statusInternal.value = NetworkStatus.Offline
}
},
Threads.mainThreadHandler
)
}
override fun forceRefresh() {/* do nothing */}
}

View file

@ -0,0 +1,37 @@
/*
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
*
* This program 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 version 3 of the License.
*
* This program 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 <https://www.gnu.org/licenses/>.
*/
package io.timelimit.android.sync.websocket.networkstatus
import android.content.Context
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import androidx.lifecycle.LiveData
object NetworkStatusUtil {
fun createWith(context: Context): NetworkStatusInterface =
if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) ModernNetworkStatusUtil(context.applicationContext)
else LegacyNetworkStatusUtil(context.applicationContext)
}
enum class NetworkStatus {
Offline, Online
}
interface NetworkStatusInterface {
fun forceRefresh()
val status: LiveData<NetworkStatus>
}

View file

@ -1,5 +1,5 @@
/*
* TimeLimit Copyright <C> 2019 - 2021 Jonas Lochmann
* TimeLimit Copyright <C> 2019 - 2022 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -31,7 +31,7 @@ import io.timelimit.android.integration.platform.NetworkId
import io.timelimit.android.livedata.liveDataFromFunction
import io.timelimit.android.livedata.liveDataFromNullableValue
import io.timelimit.android.logic.DefaultAppLogic
import io.timelimit.android.sync.websocket.NetworkStatus
import io.timelimit.android.sync.websocket.networkstatus.NetworkStatus
import io.timelimit.android.ui.main.FragmentWithCustomTitle
class DiagnoseConnectionFragment : Fragment(), FragmentWithCustomTitle {