Composed contacts activity dialogs

This commit is contained in:
Juha Heinanen
2025-04-14 17:39:56 +03:00
parent 8ebd60b4d5
commit 3543945c92
@@ -3,12 +3,10 @@ package com.tutpro.baresip
import android.app.Activity import android.app.Activity
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.os.SystemClock import android.os.SystemClock
import android.provider.ContactsContract import android.provider.ContactsContract
import android.view.MenuItem
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback import androidx.activity.OnBackPressedCallback
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
@@ -46,6 +44,8 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
@@ -59,7 +59,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import coil.compose.AsyncImage import coil.compose.AsyncImage
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.tutpro.baresip.CustomElements.AlertDialog
import com.tutpro.baresip.CustomElements.TextAvatar import com.tutpro.baresip.CustomElements.TextAvatar
import com.tutpro.baresip.CustomElements.verticalScrollbar import com.tutpro.baresip.CustomElements.verticalScrollbar
import java.io.File import java.io.File
@@ -191,7 +191,28 @@ class ContactsActivity : ComponentActivity() {
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun ContactsContent(ctx: Context, contentPadding: PaddingValues) { fun ContactsContent(ctx: Context, contentPadding: PaddingValues) {
val showDialog = remember { mutableStateOf(false) }
val dialogMessage = remember { mutableStateOf("") }
val positiveText = remember { mutableStateOf("") }
val positiveAction = remember { mutableStateOf({}) }
val neutralText = remember { mutableStateOf("") }
val neutralAction = remember { mutableStateOf({}) }
if (showDialog.value)
AlertDialog(
showDialog = showDialog,
title = getString(R.string.confirmation),
message = dialogMessage.value,
positiveButtonText = positiveText.value,
onPositiveClicked = positiveAction.value,
neutralButtonText = neutralText.value,
onNeutralClicked = neutralAction.value,
negativeButtonText = getString(R.string.cancel)
)
val lazyListState = rememberLazyListState() val lazyListState = rememberLazyListState()
LazyColumn( LazyColumn(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
@@ -258,117 +279,74 @@ class ContactsActivity : ComponentActivity() {
.padding(start = 10.dp) .padding(start = 10.dp)
.combinedClickable( .combinedClickable(
onClick = { onClick = {
val dialogClickListener = dialogMessage.value = String.format(
DialogInterface.OnClickListener { _, which -> ctx.getString(R.string.contact_action_question),
when (which) { name
DialogInterface.BUTTON_POSITIVE, DialogInterface.BUTTON_NEGATIVE -> { )
val i = Intent( positiveText.value = ctx.getString(R.string.call)
ctx, positiveAction.value = {
MainActivity::class.java val i = Intent(ctx, MainActivity::class.java)
) i.flags =
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP i.putExtra("action", "call")
if (which == DialogInterface.BUTTON_NEGATIVE) val ua = UserAgent.ofAor(aor)
i.putExtra("action", "call") if (ua == null)
else Log.w(TAG, "onClickListener did not find AoR $aor")
i.putExtra("action", "message") else {
val ua = UserAgent.ofAor(aor) BaresipService.activities.clear()
if (ua == null) { i.putExtra("uap", ua.uap)
Log.w( i.putExtra("peer", contact.uri)
TAG, (ctx as Activity).startActivity(i)
"onClickListener did not find AoR $aor"
)
} else {
BaresipService.activities.clear()
i.putExtra("uap", ua.uap)
i.putExtra("peer", contact.uri)
(ctx as Activity).startActivity(i)
}
}
DialogInterface.BUTTON_NEUTRAL -> {
}
}
}
if (SystemClock.elapsedRealtime() - lastClick > 1000) {
lastClick = SystemClock.elapsedRealtime()
with(
MaterialAlertDialogBuilder(ctx, R.style.AlertDialogTheme)
) {
setTitle(R.string.confirmation)
setMessage(
String.format(
ctx.getString(R.string.contact_action_question),
name
)
)
setNeutralButton(
ctx.getText(R.string.cancel),
dialogClickListener
)
setNegativeButton(
ctx.getText(R.string.call),
dialogClickListener
)
setPositiveButton(
ctx.getText(R.string.send_message),
dialogClickListener
)
show()
} }
} }
neutralText.value = ctx.getString(R.string.send_message)
neutralAction.value = {
val i = Intent(
ctx,
MainActivity::class.java
)
i.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
i.putExtra("action", "message")
val ua = UserAgent.ofAor(aor)
if (ua == null)
Log.w(TAG, "onClickListener did not find AoR $aor")
else {
BaresipService.activities.clear()
i.putExtra("uap", ua.uap)
i.putExtra("peer", contact.uri)
(ctx as Activity).startActivity(i)
}
}
showDialog.value = true
}, },
onLongClick = { onLongClick = {
val dialogClickListener = dialogMessage.value = String.format(
DialogInterface.OnClickListener { _, which -> ctx.getString(R.string.contact_delete_question),
when (which) { name
DialogInterface.BUTTON_POSITIVE -> { )
val id = contact.id positiveText.value = ctx.getString(R.string.delete)
val avatarFile = File( positiveAction.value = {
BaresipService.filesPath, val id = contact.id
"$id.png" val avatarFile = File(
) BaresipService.filesPath,
if (avatarFile.exists()) { "$id.png"
try { )
avatarFile.delete() if (avatarFile.exists()) {
} catch (e: IOException) { try {
Log.e( avatarFile.delete()
TAG, } catch (e: IOException) {
"Could not delete file $id.png: ${e.message}" Log.e(
) TAG,
} "Could not delete file $id.png: ${e.message}"
} )
Contact.removeBaresipContact(contact)
}
DialogInterface.BUTTON_NEUTRAL -> {
}
} }
} }
with( Contact.removeBaresipContact(contact)
MaterialAlertDialogBuilder(
ctx,
R.style.AlertDialogTheme
)
) {
setTitle(R.string.confirmation)
setMessage(
String.format(
ctx.getString(R.string.contact_delete_question),
name
)
)
setNeutralButton(
ctx.getText(R.string.cancel),
dialogClickListener
)
setPositiveButton(
ctx.getText(R.string.delete),
dialogClickListener
)
show()
} }
showDialog.value = true
} }
) )
) )
@@ -418,44 +396,19 @@ class ContactsActivity : ComponentActivity() {
} }
}, },
onLongClick = { onLongClick = {
val dialogClickListener = dialogMessage.value = String.format(
DialogInterface.OnClickListener { _, which -> ctx.getString(R.string.contact_delete_question),
when (which) { name
DialogInterface.BUTTON_POSITIVE -> { )
ctx.contentResolver.delete( positiveText.value = ctx.getString(R.string.delete)
ContactsContract.RawContacts.CONTENT_URI, positiveAction.value = {
ContactsContract.Contacts.DISPLAY_NAME + "='" + name + "'", ctx.contentResolver.delete(
null ContactsContract.RawContacts.CONTENT_URI,
) ContactsContract.Contacts.DISPLAY_NAME + "='" + name + "'",
} null
DialogInterface.BUTTON_NEUTRAL -> {
}
}
}
with(
MaterialAlertDialogBuilder(
ctx,
R.style.AlertDialogTheme
) )
) {
setTitle(R.string.confirmation)
setMessage(
String.format(
ctx.getString(R.string.contact_delete_question),
name
)
)
setNeutralButton(
ctx.getText(R.string.cancel),
dialogClickListener
)
setPositiveButton(
ctx.getText(R.string.delete),
dialogClickListener
)
show()
} }
showDialog.value = true
} }
) )
) )