-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add a new rotation provider: from display listener
- Loading branch information
1 parent
0aef739
commit 06541a1
Showing
6 changed files
with
139 additions
and
18 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...va/io/github/thibaultbee/streampack/core/streamers/orientation/DisplayRotationProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright (C) 2024 Thibault B. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.thibaultbee.streampack.core.streamers.orientation | ||
|
||
import android.content.Context | ||
import android.hardware.display.DisplayManager | ||
import io.github.thibaultbee.streampack.core.internal.utils.extensions.displayRotation | ||
|
||
/** | ||
* A [RotationProvider] that provides display rotation. | ||
* It uses [DisplayManager] to get display rotation. | ||
* It follows the orientation of the display only. If device or application are locked in a specific | ||
* orientation, it will not change. | ||
* | ||
* It will notify listeners when the display orientation changes. | ||
* | ||
* @param context The application context | ||
*/ | ||
class DisplayRotationProvider(val context: Context) : RotationProvider() { | ||
private val lock = Any() | ||
private val displayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager | ||
private var _rotation = context.displayRotation | ||
|
||
private val displayListener = object : DisplayManager.DisplayListener { | ||
override fun onDisplayAdded(displayId: Int) = Unit | ||
override fun onDisplayRemoved(displayId: Int) = Unit | ||
override fun onDisplayChanged(displayId: Int) { | ||
val newRotation = context.displayRotation | ||
|
||
if (_rotation != newRotation) { | ||
_rotation = newRotation | ||
|
||
synchronized(lock) { | ||
listeners.forEach { it.onOrientationChanged(newRotation) } | ||
} | ||
} | ||
} | ||
} | ||
|
||
override val rotation: Int | ||
get() = _rotation | ||
|
||
override fun addListener(listener: IRotationProvider.Listener) { | ||
synchronized(lock) { | ||
val mustRegister = listeners.isEmpty() | ||
super.addListener(listener) | ||
if (mustRegister) { | ||
displayManager.registerDisplayListener(displayListener, null) | ||
} | ||
} | ||
} | ||
|
||
override fun removeListener(listener: IRotationProvider.Listener) { | ||
synchronized(lock) { | ||
super.removeListener(listener) | ||
if (listeners.isEmpty()) { | ||
displayManager.unregisterDisplayListener(displayListener) | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../java/io/github/thibaultbee/streampack/core/streamers/orientation/RotationFlowProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (C) 2024 Thibault B. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.github.thibaultbee.streampack.core.streamers.orientation | ||
|
||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
|
||
|
||
/** | ||
* A [RotationProvider] that provides device rotation as a [Flow] instead of a callback. | ||
* | ||
* @param rotationProvider The rotation provider to get rotation from | ||
*/ | ||
class RotationFlowProvider(private val rotationProvider: RotationProvider) : IRotationFlowProvider { | ||
override val rotationFlow: Flow<Int> = callbackFlow { | ||
val listener = object : IRotationProvider.Listener { | ||
override fun onOrientationChanged(rotation: Int) { | ||
trySend(rotation) | ||
} | ||
} | ||
rotationProvider.addListener(listener) | ||
awaitClose { rotationProvider.removeListener(listener) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters