Fixed crash related to redirect popup

This commit is contained in:
partisan 2025-05-06 20:34:28 +02:00
parent fc1f048e2c
commit c8d65dda5f
4 changed files with 34 additions and 22 deletions

View file

@ -11,8 +11,8 @@ android {
applicationId = "partisan.weforge.xyz.pulse" applicationId = "partisan.weforge.xyz.pulse"
minSdk = 29 minSdk = 29
targetSdk = 34 targetSdk = 34
versionCode = 10 versionCode = 11
versionName = "1.3.1" versionName = "1.3.2"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
} }

View file

@ -9,6 +9,7 @@ import android.net.Uri
import android.view.Gravity import android.view.Gravity
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.WindowManager import android.view.WindowManager
import android.view.ContextThemeWrapper
import android.widget.TextView import android.widget.TextView
import androidx.annotation.RequiresPermission import androidx.annotation.RequiresPermission
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
@ -16,14 +17,14 @@ import java.util.*
import kotlin.concurrent.timerTask import kotlin.concurrent.timerTask
class PopupWindow( class PopupWindow(
private val ctx: Context, ctx: Context,
private val service: WeakReference<CallRedirectionService>?, private val service: WeakReference<CallRedirectionService>?,
) { ) {
private val prefs = Preferences(ctx) private val themedCtx = ContextThemeWrapper(ctx, R.style.Theme_Pulse)
private val windowManager = ctx.getSystemService(WindowManager::class.java) private val prefs = Preferences(themedCtx)
private val audioManager = ctx.getSystemService(AudioManager::class.java) private val windowManager = themedCtx.getSystemService(WindowManager::class.java)
@Suppress("InflateParams") private val audioManager = themedCtx.getSystemService(AudioManager::class.java)
private val view = LayoutInflater.from(ctx).inflate(R.layout.popup, null) private val view = LayoutInflater.from(themedCtx).inflate(R.layout.popup, null)
private val layoutParams = WindowManager.LayoutParams().apply { private val layoutParams = WindowManager.LayoutParams().apply {
format = PixelFormat.TRANSLUCENT format = PixelFormat.TRANSLUCENT
flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
@ -45,7 +46,7 @@ class PopupWindow(
fun preview() { fun preview() {
remove() remove()
layoutParams.y = prefs.popupPosition layoutParams.y = prefs.popupPosition
val destinations = mutableListOf( val destinations = listOf(
R.string.destination_signal, R.string.destination_signal,
R.string.destination_telegram, R.string.destination_telegram,
R.string.destination_threema, R.string.destination_threema,
@ -87,33 +88,37 @@ class PopupWindow(
} }
private fun setDescription(id: Int) { private fun setDescription(id: Int) {
view.findViewById<TextView>(R.id.description).text = ctx.getString( view.findViewById<TextView>(R.id.description).text = themedCtx.getString(
R.string.popup, R.string.popup,
ctx.getString(id), themedCtx.getString(id),
) )
} }
@RequiresPermission(Manifest.permission.CALL_PHONE) @RequiresPermission(Manifest.permission.CALL_PHONE)
private fun call(data: Uri) { private fun call(data: Uri) {
Intent(Intent.ACTION_VIEW).let { Intent(Intent.ACTION_VIEW).apply {
it.data = data this.data = data
it.flags = Intent.FLAG_ACTIVITY_NEW_TASK flags = Intent.FLAG_ACTIVITY_NEW_TASK
ctx.startActivity(it) themedCtx.startActivity(this)
} }
} }
private fun add(): Boolean { private fun add(): Boolean {
try { try {
windowManager?.addView(view, layoutParams) windowManager?.addView(view, layoutParams)
} catch (exc: WindowManager.BadTokenException) { return false } } catch (exc: WindowManager.BadTokenException) {
return false
}
return true return true
} }
private fun remove(): Boolean { private fun remove(): Boolean {
try { try {
windowManager?.removeView(view) windowManager?.removeView(view)
} catch (exc: IllegalArgumentException) { } catch (_: IllegalArgumentException) {
} catch (exc: WindowManager.BadTokenException) { return false } } catch (_: WindowManager.BadTokenException) {
return false
}
return true return true
} }

View file

@ -1,11 +1,14 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android" <com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:cardCornerRadius="24dp" app:cardCornerRadius="24dp"
app:cardElevation="4dp" app:cardElevation="4dp"
app:cardBackgroundColor="?attr/colorSurface"> app:cardBackgroundColor="?attr/colorSurface"
android:background="@android:color/white">
<TextView <TextView
android:id="@+id/description" android:id="@+id/description"
@ -15,5 +18,6 @@
android:textColor="?attr/colorOnSurface" android:textColor="?attr/colorOnSurface"
android:textSize="16sp" android:textSize="16sp"
android:textAppearance="?attr/textAppearanceBodyMedium" android:textAppearance="?attr/textAppearanceBodyMedium"
android:textAlignment="center" /> android:textAlignment="center"
tools:text="Popup text" />
</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>

View file

@ -7,6 +7,9 @@
<!-- Secondary brand color. --> <!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item> <item name="colorSecondary">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item> <item name="colorOnSecondary">@color/black</item>
<!-- Customize your theme here. -->
<item name="colorSurface">@color/white</item>
<item name="colorOnSurface">@color/black</item>
<item name="textAppearanceBodyMedium">@style/TextAppearance.Material3.BodyMedium</item>
</style> </style>
</resources> </resources>