Removed deprecated hasTextContent and getTextContent functions + hide auto start service notification when Core has started

This commit is contained in:
Sylvain Berfini 2021-03-22 14:21:16 +01:00
parent 351ce7c9e5
commit 318ae8ee20
4 changed files with 26 additions and 16 deletions

View file

@ -196,7 +196,7 @@ class ChatMessagesListAdapter(
if (chatMessage.state != ChatMessage.State.NotDelivered) { if (chatMessage.state != ChatMessage.State.NotDelivered) {
popup.menu.removeItem(R.id.chat_message_menu_resend) popup.menu.removeItem(R.id.chat_message_menu_resend)
} }
if (!chatMessage.hasTextContent()) { if (chatMessage.contents.find { content -> content.isText } == null) {
popup.menu.removeItem(R.id.chat_message_menu_copy_text) popup.menu.removeItem(R.id.chat_message_menu_copy_text)
} }
if (chatMessageViewModel.contact.value != null) { if (chatMessageViewModel.contact.value != null) {
@ -250,12 +250,16 @@ class ChatMessagesListAdapter(
private fun copyTextToClipboard() { private fun copyTextToClipboard() {
val chatMessage = binding.viewModel?.chatMessage val chatMessage = binding.viewModel?.chatMessage
if (chatMessage != null && chatMessage.hasTextContent()) { if (chatMessage != null) {
val clipboard: ClipboardManager = coreContext.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager val content = chatMessage.contents.find { content -> content.isText }
val clip = ClipData.newPlainText("Message", chatMessage.textContent) if (content != null) {
val clipboard: ClipboardManager =
coreContext.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("Message", content.utf8Text)
clipboard.setPrimaryClip(clip) clipboard.setPrimaryClip(clip)
} }
} }
}
private fun forwardMessage() { private fun forwardMessage() {
val chatMessage = binding.viewModel?.chatMessage val chatMessage = binding.viewModel?.chatMessage

View file

@ -116,6 +116,7 @@ class CoreContext(val context: Context, coreConfig: Config) {
Log.i("[Context] Global state changed [$state]") Log.i("[Context] Global state changed [$state]")
if (state == GlobalState.On) { if (state == GlobalState.On) {
contactsManager.fetchContactsAsync() contactsManager.fetchContactsAsync()
notificationsManager.stopForegroundNotificationIfPossible()
} }
} }

View file

@ -35,7 +35,7 @@ class CoreService : CoreService() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent?.extras?.get("StartForeground") == true) { if (intent?.extras?.get("StartForeground") == true) {
Log.i("[Service] Starting as foreground") Log.i("[Service] Starting as foreground due to device boot or app update")
coreContext.notificationsManager.startForeground(this, true) coreContext.notificationsManager.startForeground(this, true)
} else if (corePreferences.keepServiceAlive) { } else if (corePreferences.keepServiceAlive) {
Log.i("[Service] Starting as foreground to keep app alive in background") Log.i("[Service] Starting as foreground to keep app alive in background")
@ -60,7 +60,7 @@ class CoreService : CoreService() {
override fun onTaskRemoved(rootIntent: Intent?) { override fun onTaskRemoved(rootIntent: Intent?) {
if (!corePreferences.keepServiceAlive) { if (!corePreferences.keepServiceAlive) {
if (coreContext.core.isInBackground()) { if (coreContext.core.isInBackground) {
Log.i("[Service] Task removed, stopping Core") Log.i("[Service] Task removed, stopping Core")
coreContext.stop() coreContext.stop()
} else { } else {

View file

@ -146,7 +146,9 @@ class NotificationsManager(private val context: Context) {
return return
} }
if (!message.hasTextContent() && message.fileTransferInformation == null) { if (message.contents.find { content ->
content.isFile or content.isFileTransfer or content.isText
} == null) {
Log.w("[Notifications Manager] Received message with neither text or attachment, do not notify") Log.w("[Notifications Manager] Received message with neither text or attachment, do not notify")
return return
} }
@ -290,7 +292,7 @@ class NotificationsManager(private val context: Context) {
} }
fun startForeground(coreService: CoreService, useAutoStartDescription: Boolean = true) { fun startForeground(coreService: CoreService, useAutoStartDescription: Boolean = true) {
Log.i("[Notifications Manager] Starting Service as foreground") Log.i("[Notifications Manager] Starting service as foreground")
if (serviceNotification == null) { if (serviceNotification == null) {
createServiceNotification(useAutoStartDescription) createServiceNotification(useAutoStartDescription)
} }
@ -301,7 +303,7 @@ class NotificationsManager(private val context: Context) {
private fun startForeground(notificationId: Int, callNotification: Notification) { private fun startForeground(notificationId: Int, callNotification: Notification) {
if (currentForegroundServiceNotificationId == 0 && service != null) { if (currentForegroundServiceNotificationId == 0 && service != null) {
Log.i("[Notifications Manager] Starting Service as foreground using call notification") Log.i("[Notifications Manager] Starting service as foreground using call notification")
currentForegroundServiceNotificationId = notificationId currentForegroundServiceNotificationId = notificationId
service?.startForeground(currentForegroundServiceNotificationId, callNotification) service?.startForeground(currentForegroundServiceNotificationId, callNotification)
} }
@ -309,7 +311,7 @@ class NotificationsManager(private val context: Context) {
private fun stopForegroundNotification() { private fun stopForegroundNotification() {
if (service != null) { if (service != null) {
Log.i("[Notifications Manager] Stopping Service as foreground") Log.i("[Notifications Manager] Stopping service as foreground")
service?.stopForeground(true) service?.stopForeground(true)
currentForegroundServiceNotificationId = 0 currentForegroundServiceNotificationId = 0
} }
@ -317,12 +319,14 @@ class NotificationsManager(private val context: Context) {
fun stopForegroundNotificationIfPossible() { fun stopForegroundNotificationIfPossible() {
if (service != null && currentForegroundServiceNotificationId == SERVICE_NOTIF_ID && !corePreferences.keepServiceAlive) { if (service != null && currentForegroundServiceNotificationId == SERVICE_NOTIF_ID && !corePreferences.keepServiceAlive) {
Log.i("[Notifications Manager] Stopping auto-started service notification")
stopForegroundNotification() stopForegroundNotification()
} }
} }
fun stopCallForeground() { fun stopCallForeground() {
if (service != null && currentForegroundServiceNotificationId != SERVICE_NOTIF_ID && !corePreferences.keepServiceAlive) { if (service != null && currentForegroundServiceNotificationId != SERVICE_NOTIF_ID && !corePreferences.keepServiceAlive) {
Log.i("[Notifications Manager] Stopping call notification used as foreground service")
stopForegroundNotification() stopForegroundNotification()
} }
} }
@ -582,14 +586,14 @@ class NotificationsManager(private val context: Context) {
val roundPicture = ImageUtils.getRoundBitmapFromUri(context, pictureUri) val roundPicture = ImageUtils.getRoundBitmapFromUri(context, pictureUri)
val displayName = contact?.fullName ?: LinphoneUtils.getDisplayName(message.fromAddress) val displayName = contact?.fullName ?: LinphoneUtils.getDisplayName(message.fromAddress)
val notifiable = getNotifiableForRoom(room) var text: String = message.contents.find { content -> content.isText }?.utf8Text ?: ""
var text = "" if (text.isEmpty()) {
if (message.hasTextContent()) text = message.textContent.orEmpty()
else {
for (content in message.contents) { for (content in message.contents) {
text += content.name text += content.name
} }
} }
val notifiable = getNotifiableForRoom(room)
val notifiableMessage = NotifiableMessage(text, contact, displayName, message.time, senderAvatar = roundPicture, isOutgoing = message.isOutgoing) val notifiableMessage = NotifiableMessage(text, contact, displayName, message.time, senderAvatar = roundPicture, isOutgoing = message.isOutgoing)
notifiable.messages.add(notifiableMessage) notifiable.messages.add(notifiableMessage)
@ -639,8 +643,9 @@ class NotificationsManager(private val context: Context) {
private fun displayReplyMessageNotification(message: ChatMessage, notifiable: Notifiable) { private fun displayReplyMessageNotification(message: ChatMessage, notifiable: Notifiable) {
Log.i("[Notifications Manager] Updating message notification with reply for notification ${notifiable.notificationId}") Log.i("[Notifications Manager] Updating message notification with reply for notification ${notifiable.notificationId}")
val text = message.contents.find { content -> content.isText }?.utf8Text ?: ""
val reply = NotifiableMessage( val reply = NotifiableMessage(
message.textContent.orEmpty(), text,
null, null,
notifiable.myself ?: LinphoneUtils.getDisplayName(message.fromAddress), notifiable.myself ?: LinphoneUtils.getDisplayName(message.fromAddress),
System.currentTimeMillis(), System.currentTimeMillis(),