Skip to content
Snippets Groups Projects
Commit 3d7f70e6 authored by Geoffrey Métais's avatar Geoffrey Métais Committed by Nicolas Pomepuy
Browse files

Workaround for IllegalStateException on Pie

Android 9 has a bug, sometimes, service cannot be started as soon as app
is:
https://issuetracker.google.com/issues/113122354

This fix creates a suspend function which will wait for the app to go
foreground, and use it to launch the service only when we can
parent 9445e1ec
No related branches found
No related tags found
1 merge request!319Workaround for IllegalStateException on Pie
Pipeline #11228 passed with stage
in 3 minutes and 54 seconds
package org.videolan.tools
import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
......@@ -79,3 +81,26 @@ fun Context.copy(label: String, text: String) {
setPrimaryClip(ClipData.newPlainText(label, text))
}
}
suspend fun retry (
times: Int = 3,
delayTime: Long = 500L,
block: suspend () -> Boolean): Boolean
{
repeat(times - 1) {
if (block()) return true
if (delayTime > 0L) delay(delayTime)
}
return block() // last attempt
}
suspend fun Context.awaitAppIsForegroung() : Boolean {
val activityManager = applicationContext.getSystemService(Context.ACTIVITY_SERVICE) as? ActivityManager ?: return false
repeat(times = 2) {
if (activityManager.isAppForeground()) return true
else yield() //dispatch next try
}
return activityManager.isAppForeground()
}
private fun ActivityManager.isAppForeground() = runningAppProcesses[0].importance <= RunningAppProcessInfo.IMPORTANCE_FOREGROUND
\ No newline at end of file
......@@ -22,7 +22,11 @@ package org.videolan.vlc
import kotlinx.coroutines.*
import org.videolan.libvlc.RendererDiscoverer
import org.videolan.libvlc.RendererItem
import org.videolan.vlc.util.*
import org.videolan.tools.retry
import org.videolan.vlc.util.AppScope
import org.videolan.vlc.util.LiveDataset
import org.videolan.vlc.util.VLCInstance
import org.videolan.vlc.util.isAppStarted
import java.util.*
@ObsoleteCoroutinesApi
......
......@@ -26,6 +26,7 @@ package org.videolan.vlc
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.MediaStore
import android.text.TextUtils
......@@ -35,6 +36,7 @@ import androidx.fragment.app.FragmentActivity
import kotlinx.coroutines.*
import org.videolan.libvlc.util.AndroidUtil
import org.videolan.medialibrary.MLServiceLocator
import org.videolan.tools.awaitAppIsForegroung
import org.videolan.vlc.gui.BetaWelcomeActivity
import org.videolan.vlc.gui.MainActivity
import org.videolan.vlc.gui.SearchActivity
......@@ -196,6 +198,12 @@ class StartActivity : FragmentActivity(), CoroutineScope by MainScope() {
}
private fun startPlaybackFromApp(intent: Intent) = launch(start = CoroutineStart.UNDISPATCHED) {
// workaround for a Android 9 bug
// https://issuetracker.google.com/issues/113122354
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P && !awaitAppIsForegroung()) {
finish()
return@launch
}
if (Permissions.canReadStorage(applicationContext) || getStoragePermission()) when {
intent.type?.startsWith("video") == true -> try {
startActivity(intent.setClass(this@StartActivity, VideoPlayerActivity::class.java))
......
......@@ -33,8 +33,12 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.videolan.medialibrary.interfaces.AbstractMedialibrary
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.tools.retry
import org.videolan.vlc.providers.HeaderProvider
import org.videolan.vlc.util.*
import org.videolan.vlc.util.MEDIALIBRARY_PAGE_SIZE
import org.videolan.vlc.util.ModelsHelper
import org.videolan.vlc.util.Settings
import org.videolan.vlc.util.SortModule
import org.videolan.vlc.viewmodels.SortableModel
abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context, val model: SortableModel) : HeaderProvider(),
......
......@@ -66,18 +66,6 @@ inline fun <reified T : ViewModel> Fragment.getModelWithActivity() = ViewModelPr
inline fun <reified T : ViewModel> Fragment.getModel() = ViewModelProviders.of(this).get(T::class.java)
inline fun <reified T : ViewModel> FragmentActivity.getModel() = ViewModelProviders.of(this).get(T::class.java)
suspend fun retry (
times: Int = 3,
delayTime: Long = 500L,
block: suspend () -> Boolean): Boolean
{
repeat(times - 1) {
if (block()) return true
delay(delayTime)
}
return block() // last attempt
}
fun Media?.canExpand() = this != null && (type == Media.Type.Directory || type == Media.Type.Playlist)
suspend fun AppCompatActivity.share(media: AbstractMediaWrapper) {
val intentShareFile = Intent(Intent.ACTION_SEND)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment