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 ConnectivityManager crash #558

Merged
merged 1 commit into from
Jan 19, 2023
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
8 changes: 8 additions & 0 deletions android/src/main/kotlin/com/twidere/twiderex/TwidereApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
package com.twidere.twiderex

import android.content.Context
import android.net.ConnectivityManager
import androidx.startup.AppInitializer
import com.twidere.twiderex.initializer.DirectMessageInitializer
import com.twidere.twiderex.initializer.NotificationChannelInitializer
import com.twidere.twiderex.initializer.NotificationInitializer
import com.twidere.twiderex.initializer.TwidereServiceInitializer
import com.twidere.twiderex.utils.TwiderexLogger
import com.twidere.twiderex.utils.asIsActiveNetworkFlow
import io.github.aakira.napier.Napier
import kotlinx.coroutines.flow.Flow

class TwidereApp : TwidereApplication() {
override fun onCreate() {
Expand All @@ -46,9 +49,14 @@ class TwidereApp : TwidereApplication() {
initializeComponent(DirectMessageInitializer::class.java)
initializeComponent(TwidereServiceInitializer::class.java)
}
isNetworkActiveFlow = (this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager).asIsActiveNetworkFlow()
}

interface MissingSplitsChecker {
fun requiredSplits(context: Context): Boolean
}

companion object {
lateinit var isNetworkActiveFlow: Flow<Boolean>
}
}
15 changes: 3 additions & 12 deletions android/src/main/kotlin/com/twidere/twiderex/TwidereXActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import android.Manifest
import android.content.ContentResolver.SCHEME_ANDROID_RESOURCE
import android.content.Intent
import android.content.res.Configuration
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
Expand Down Expand Up @@ -69,6 +68,7 @@ import com.twidere.twiderex.component.PermissionCheck
import com.twidere.twiderex.component.foundation.LocalInAppNotification
import com.twidere.twiderex.compose.LocalResLoader
import com.twidere.twiderex.di.ext.get
import com.twidere.twiderex.extensions.flowWithLifecycle
import com.twidere.twiderex.extensions.observeAsState
import com.twidere.twiderex.kmp.LocalPlatformWindow
import com.twidere.twiderex.kmp.LocalRemoteNavigator
Expand All @@ -87,10 +87,6 @@ import com.twidere.twiderex.ui.LocalIsActiveNetworkMetered
import com.twidere.twiderex.utils.BrowserLoginDeepLinksChannel
import com.twidere.twiderex.utils.LocalPlatformResolver
import com.twidere.twiderex.utils.PlatformResolver
import com.twidere.twiderex.utils.asIsActiveNetworkFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import moe.tlaster.kfilepicker.FilePicker
import moe.tlaster.precompose.lifecycle.PreComposeActivity
import moe.tlaster.precompose.lifecycle.setContent
Expand All @@ -114,23 +110,18 @@ class TwidereXActivity : PreComposeActivity(), KoinComponent {

private val inAppNotification: InAppNotification by inject()

private val connectivityManager: ConnectivityManager by inject()

private val platformResolver: PlatformResolver by inject()

private val remoteNavigator: RemoteNavigator by inject()

private val isActiveNetworkMetered = MutableStateFlow(false)
private val isActiveNetworkMetered =
TwidereApp.isNetworkActiveFlow.flowWithLifecycle(lifecycle)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
FilePicker.init(activityResultRegistry, this, contentResolver)
WindowCompat.setDecorFitsSystemWindows(window, false)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
connectivityManager.asIsActiveNetworkFlow()
.flowWithLifecycle(getLifecycle())
.onEach { isActiveNetworkMetered.value = it }
.launchIn(lifecycleScope)
setContent {
var showSplash by rememberSaveable { mutableStateOf(true) }
LaunchedEffect(Unit) {
Expand Down