Skip to content

Commit

Permalink
Merge pull request #544 from 07jasjeet/crash-fixes
Browse files Browse the repository at this point in the history
Crash fixes
  • Loading branch information
07jasjeet authored Feb 1, 2025
2 parents d50f742 + 6aff483 commit f0005b2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 27 deletions.
47 changes: 29 additions & 18 deletions app/src/main/java/org/listenbrainz/android/application/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import androidx.work.Configuration
import com.limurse.logger.Logger
import com.limurse.logger.config.Config
import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.listenbrainz.android.BuildConfig
import org.listenbrainz.android.R
import org.listenbrainz.android.repository.preferences.AppPreferences
Expand All @@ -32,6 +36,7 @@ class App : Application(), Configuration.Provider {
@Inject
lateinit var workerFactory: HiltWorkerFactory

@OptIn(DelicateCoroutinesApi::class)
override fun onCreate() {
super.onCreate()
val logDirectory = applicationContext.getExternalFilesDir(null)?.path.orEmpty()
Expand All @@ -49,15 +54,9 @@ class App : Application(), Configuration.Provider {
}

context = this

MainScope().launch {
if(
appPreferences.isNotificationServiceAllowed &&
appPreferences.lbAccessToken.get().isNotEmpty() &&
appPreferences.isListeningAllowed.get()
) {
startListenService()
}

GlobalScope.launch {
startListenService(appPreferences)
}

createChannels()
Expand Down Expand Up @@ -143,17 +142,29 @@ class App : Application(), Configuration.Provider {
lateinit var context: App
private set

fun startListenService() {
val intent = Intent(context, ListenSubmissionService::class.java)
if (!context.isServiceRunning(ListenSubmissionService::class.java)) {
val component = context.startService(intent)
if (component == null) {
Log.d("No running instances found, starting service.")
suspend fun startListenService(appPreferences: AppPreferences) = withContext(Dispatchers.Main) {
if (
appPreferences.isNotificationServiceAllowed &&
appPreferences.lbAccessToken.get().isNotEmpty() &&
appPreferences.isListeningAllowed.get()
) {
val intent = Intent(context, ListenSubmissionService::class.java)
if (!context.isServiceRunning(ListenSubmissionService::class.java)) {
val component = runCatching {
context.startService(intent)
}.getOrElse { error ->
Log.d(error)
null
}

if (component == null) {
Log.d("No running instances found, starting service.")
} else {
Log.d("Service already running with name: $component")
}
} else {
Log.d("Service already running with name: $component")
Log.d("Service already running")
}
} else {
Log.d("Service already running")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,11 @@ enum class FeedEventType (
Log.d(stringAnnotation.item)
uriHandler.openUri(stringAnnotation.item)
}
} catch (e: ActivityNotFoundException) {
} catch (e: IllegalArgumentException) {
Log.w("MyFeed: Notification link invalid.")
e.printStackTrace()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ListenServiceManagerImpl @Inject constructor(

/** Used to avoid repetitive submissions.*/
private var lastNotificationPostTs = System.currentTimeMillis()
private lateinit var whitelist: List<String>
private var whitelist: List<String> = emptyList()
private var isListeningAllowed: Boolean = true

init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,8 @@ class MainActivity : ComponentActivity() {

override fun onResume() {
super.onResume()
lifecycleScope.launch(Dispatchers.Main) {
if (dashBoardViewModel.isNotificationListenerServiceAllowed()) {
if (!isServiceRunning(ListenSubmissionService::class.java)) {
App.startListenService()
}
}
lifecycleScope.launch {
App.startListenService(appPreferences = dashBoardViewModel.appPreferences)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import javax.inject.Inject

@HiltViewModel
class DashBoardViewModel @Inject constructor(
private val appPreferences: AppPreferences,
val appPreferences: AppPreferences,
private val application: Application,
private val remotePlaybackHandler: RemotePlaybackHandler,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher
Expand Down

0 comments on commit f0005b2

Please sign in to comment.