Skip to content

Commit

Permalink
Allow configuring stability threshold time and ensure there is no mor…
Browse files Browse the repository at this point in the history
…e than 1 job active at a time for SimpleOrientationListener.
  • Loading branch information
usertoroot committed Sep 10, 2024
1 parent f20a708 commit 0f4e4a7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
14 changes: 9 additions & 5 deletions app/src/main/java/com/futo/platformplayer/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -478,18 +478,22 @@ class Settings : FragmentedStorageFileJson() {
@DropdownFieldOptionsId(R.array.rotation_zone)
var rotationZone: Int = 2;

@FormField(R.string.full_autorotate_lock, FieldForm.TOGGLE, R.string.full_autorotate_lock_description, 16)
@FormField(R.string.stability_threshold_time, FieldForm.DROPDOWN, R.string.stability_threshold_time_description, 16)
@DropdownFieldOptionsId(R.array.rotation_threshold_time)
var stabilityThresholdTime: Int = 1;

@FormField(R.string.full_autorotate_lock, FieldForm.TOGGLE, R.string.full_autorotate_lock_description, 17)
var fullAutorotateLock: Boolean = false;

@FormField(R.string.prefer_webm, FieldForm.TOGGLE, R.string.prefer_webm_description, 17)
@FormField(R.string.prefer_webm, FieldForm.TOGGLE, R.string.prefer_webm_description, 18)
var preferWebmVideo: Boolean = false;
@FormField(R.string.prefer_webm_audio, FieldForm.TOGGLE, R.string.prefer_webm_audio_description, 18)
@FormField(R.string.prefer_webm_audio, FieldForm.TOGGLE, R.string.prefer_webm_audio_description, 19)
var preferWebmAudio: Boolean = false;

@FormField(R.string.allow_under_cutout, FieldForm.TOGGLE, R.string.allow_under_cutout_description, 19)
@FormField(R.string.allow_under_cutout, FieldForm.TOGGLE, R.string.allow_under_cutout_description, 20)
var allowVideoToGoUnderCutout: Boolean = true;

@FormField(R.string.autoplay, FieldForm.TOGGLE, R.string.autoplay, 20)
@FormField(R.string.autoplay, FieldForm.TOGGLE, R.string.autoplay, 21)
var autoplay: Boolean = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.futo.platformplayer.constructs.Event1
import com.futo.platformplayer.logging.Logger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

Expand All @@ -17,13 +18,23 @@ class SimpleOrientationListener(
) {
private var lastOrientation: Int = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
private var lastStableOrientation: Int = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
private val stabilityThresholdTime = 500L
private var _currentJob: Job? = null

val onOrientationChanged = Event1<Int>()

private val orientationListener = object : OrientationEventListener(activity, SensorManager.SENSOR_DELAY_UI) {
override fun onOrientationChanged(orientation: Int) {
//val rotationZone = 45
val stabilityThresholdTime = when (Settings.instance.playback.stabilityThresholdTime) {
0 -> 100L
1 -> 500L
2 -> 750L
3 -> 1000L
4 -> 1500L
5 -> 2000L
else -> 500L
}

val rotationZone = when (Settings.instance.playback.rotationZone) {
0 -> 15
1 -> 30
Expand All @@ -42,7 +53,8 @@ class SimpleOrientationListener(
if (newOrientation != lastStableOrientation) {
lastStableOrientation = newOrientation

lifecycleScope.launch(Dispatchers.Main) {
_currentJob?.cancel()
_currentJob = lifecycleScope.launch(Dispatchers.Main) {
try {
delay(stabilityThresholdTime)
if (newOrientation == lastStableOrientation) {
Expand All @@ -63,6 +75,8 @@ class SimpleOrientationListener(
}

fun stopListening() {
_currentJob?.cancel()
_currentJob = null
orientationListener.disable()
}

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@
<string name="reverse_portrait_description">Allow app to flip into reverse portrait</string>
<string name="rotation_zone">Rotation zone</string>
<string name="rotation_zone_description">Specify the sensitivity of rotation zones (decrease to make less sensitive)</string>
<string name="stability_threshold_time">Stability threshold time</string>
<string name="stability_threshold_time_description">Specify the duration the orientation needs to be the same to trigger a rotation</string>
<string name="prefer_webm">Prefer Webm Video Codecs</string>
<string name="prefer_webm_description">If player should prefer Webm codecs (vp9/opus) over mp4 codecs (h264/AAC), may result in worse compatibility.</string>
<string name="full_autorotate_lock">Full auto rotate lock</string>
Expand Down Expand Up @@ -968,4 +970,12 @@
<item>30</item>
<item>45</item>
</string-array>
<string-array name="rotation_threshold_time">
<item>100</item>
<item>500</item>
<item>750</item>
<item>1000</item>
<item>1500</item>
<item>2000</item>
</string-array>
</resources>

0 comments on commit 0f4e4a7

Please sign in to comment.