Skip to content

Commit 95efb44

Browse files
author
jesus
committed
-moving openSystemOverlaySettings to system
- moving the dpToPx function to the ScreenSizeHelpers.kt
1 parent 7056833 commit 95efb44

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

libraries/androidutils/src/main/kotlin/io/element/android/libraries/androidutils/system/SystemUtils.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,24 @@ fun Context.openGooglePlay(
196196
}
197197
}
198198

199+
/**
200+
* Opens the system settings for the current app to allow drawing over other apps.
201+
*/
202+
fun Context.openSystemOverlaySettings() {
203+
val intent = Intent(
204+
Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
205+
"package:$packageName".toUri()
206+
).apply {
207+
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
208+
}
209+
try {
210+
startActivity(intent)
211+
} catch (e: ActivityNotFoundException) {
212+
toast(getString(R.string.error_no_compatible_app_found))
213+
}
214+
}
215+
216+
199217
// Not in KTX anymore
200218
fun Context.toast(resId: Int) {
201219
Toast.makeText(this, resId, Toast.LENGTH_SHORT).show()

libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/floatingvideo/FloatingVideoService.kt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ import androidx.savedstate.SavedStateRegistry
3131
import androidx.savedstate.SavedStateRegistryController
3232
import androidx.savedstate.SavedStateRegistryOwner
3333
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
34-
import androidx.core.net.toUri
34+
import io.element.android.libraries.androidutils.system.openSystemOverlaySettings
3535
import io.element.android.libraries.mediaviewer.impl.floatingvideo.ui.FloatingVideoOverlay
3636
import io.element.android.libraries.mediaviewer.impl.floatingvideo.util.getScreenHeight
3737
import dev.zacsweers.metro.Inject
3838
import io.element.android.libraries.architecture.bindings
39+
import io.element.android.libraries.mediaviewer.impl.floatingvideo.util.dpToPx
3940
import io.element.android.libraries.mediaviewer.impl.floatingvideo.util.getUri
4041
import io.element.android.libraries.mediaviewer.impl.floatingvideo.util.maximizeWindowHelper
4142
import io.element.android.libraries.mediaviewer.impl.floatingvideo.util.minimizeWindowHelper
@@ -59,6 +60,8 @@ class FloatingVideoService : Service(), LifecycleOwner, SavedStateRegistryOwner
5960
const val EXTRA_VIDEO_ID = "video_id"
6061
const val EXTRA_POSITION = "position"
6162

63+
private const val INITIAL_FLOATING_WINDOW_OFFSET_Y = 300
64+
6265
@SuppressLint("ObsoleteSdkInt")
6366
fun startFloating(
6467
context: Context, videoData: MediaViewerPageData.MediaViewerData, position: Long = 0L
@@ -69,13 +72,7 @@ class FloatingVideoService : Service(), LifecycleOwner, SavedStateRegistryOwner
6972
Toast.makeText(context, "To show the floating video, please allow 'Display over other apps' permission.", Toast.LENGTH_LONG).show()
7073

7174
// Request overlay permission
72-
val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION).apply {
73-
data = "package:${context.packageName}".toUri()
74-
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
75-
}
76-
77-
78-
context.startActivity(intent)
75+
context.openSystemOverlaySettings()
7976
return
8077
}
8178

@@ -160,7 +157,6 @@ class FloatingVideoService : Service(), LifecycleOwner, SavedStateRegistryOwner
160157

161158
private fun createFloatingView() {
162159
removeFloatingView()
163-
windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
164160

165161
windowLayoutParams = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
166162
WindowManager.LayoutParams(
@@ -183,7 +179,7 @@ class FloatingVideoService : Service(), LifecycleOwner, SavedStateRegistryOwner
183179

184180
windowLayoutParams.gravity = Gravity.TOP or Gravity.START
185181
windowLayoutParams.x = 0
186-
windowLayoutParams.y = windowManager.getScreenHeight() - dpToPx(300)
182+
windowLayoutParams.y = windowManager.getScreenHeight() - dpToPx(INITIAL_FLOATING_WINDOW_OFFSET_Y)
187183

188184
val composeView = ComposeView(this).apply {
189185
setViewTreeLifecycleOwner(this@FloatingVideoService)
@@ -250,10 +246,6 @@ class FloatingVideoService : Service(), LifecycleOwner, SavedStateRegistryOwner
250246
onVideoComplete()
251247
}
252248

253-
private fun dpToPx(dp: Int): Int {
254-
return (dp * resources.displayMetrics.density).toInt()
255-
}
256-
257249
private fun onVideoComplete() {
258250
removeFloatingView()
259251
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)

libraries/mediaviewer/impl/src/main/kotlin/io/element/android/libraries/mediaviewer/impl/floatingvideo/util/ScreenSizeHelpers.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package io.element.android.libraries.mediaviewer.impl.floatingvideo.util
99

10+
import android.content.Context
1011
import android.graphics.Point
1112
import android.os.Build
1213
import android.util.DisplayMetrics
@@ -144,3 +145,7 @@ private fun calculateDimensions(
144145
val height = (width / aspectRatio).toInt()
145146
return Point(width, height)
146147
}
148+
149+
fun Context.dpToPx(dp: Int): Int {
150+
return (dp * resources.displayMetrics.density).toInt()
151+
}

0 commit comments

Comments
 (0)