-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(InstallScreen): use wakelock while running
- Loading branch information
1 parent
82096ad
commit 1b1316d
Showing
3 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
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
39 changes: 39 additions & 0 deletions
39
app/src/main/kotlin/com/aliucord/manager/ui/components/Wakelock.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,39 @@ | ||
package com.aliucord.manager.ui.components | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.ContextWrapper | ||
import android.view.WindowManager | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.ui.platform.LocalContext | ||
|
||
/** | ||
* Maintain an active screen wakelock as long as [active] is true and this component is in scope. | ||
*/ | ||
@Composable | ||
fun Wakelock(active: Boolean = false) { | ||
val context = LocalContext.current | ||
DisposableEffect(active) { | ||
val window = context.findActivity()?.window | ||
|
||
if (active) { | ||
window?.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) | ||
} else { | ||
window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) | ||
} | ||
|
||
onDispose { | ||
window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) | ||
} | ||
} | ||
} | ||
|
||
private fun Context.findActivity(): Activity? { | ||
var context = this | ||
while (context is ContextWrapper) { | ||
if (context is Activity) return context | ||
context = context.baseContext | ||
} | ||
return null | ||
} |
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