Various fixes & improvements related to chat room shorcuts

This commit is contained in:
Sylvain Berfini 2023-12-14 16:19:33 +01:00
parent 70b1c67f90
commit 6338fb65d1
4 changed files with 77 additions and 32 deletions

View file

@ -55,6 +55,7 @@ import org.linphone.ui.main.help.fragment.DebugFragmentDirections
import org.linphone.ui.main.viewmodel.MainViewModel
import org.linphone.ui.main.viewmodel.SharedMainViewModel
import org.linphone.ui.welcome.WelcomeActivity
import org.linphone.utils.Event
import org.linphone.utils.FileUtils
import org.linphone.utils.LinphoneUtils
import org.linphone.utils.ToastUtils
@ -453,25 +454,6 @@ class MainActivity : GenericActivity() {
deferred.add(async { FileUtils.getFilePath(this@MainActivity, uri, false) })
}
val shortcutId = intent.getStringExtra("android.intent.extra.shortcut.ID") // Intent.EXTRA_SHORTCUT_ID
if (shortcutId != null) {
Log.i("$TAG Found shortcut ID [$shortcutId]")
val pair = LinphoneUtils.getLocalAndPeerSipUrisFromChatRoomId(shortcutId)
if (pair != null) {
val localSipUri = pair.first
val remoteSipUri = pair.second
Log.i(
"$TAG Navigating to conversation with local [$localSipUri] and peer [$remoteSipUri] addresses, computed from shortcut ID"
)
intent.putExtra("LocalSipUri", localSipUri)
intent.putExtra("RemoteSipUri", remoteSipUri)
} else {
Log.e("$TAG Failed to parse shortcut ID, going to conversations list")
}
} else {
Log.i("$TAG Going into conversations list as no shortcut ID as found")
}
if (binding.drawerMenu.isOpen) {
Log.i("$TAG Drawer menu is opened, closing it")
closeDrawerMenu()
@ -497,9 +479,28 @@ class MainActivity : GenericActivity() {
Log.i(
"$TAG App is already started and in debug fragment, navigating to conversations list"
)
val pair = parseShortcutIfAny(intent)
if (pair != null) {
Log.i(
"$TAG Navigating to conversation with local [${pair.first}] and peer [${pair.second}] addresses, computed from shortcut ID"
)
sharedViewModel.showConversationEvent.value = Event(pair)
}
val action = DebugFragmentDirections.actionDebugFragmentToConversationsListFragment()
findNavController().navigate(action)
} else {
val pair = parseShortcutIfAny(intent)
if (pair != null) {
val localSipUri = pair.first
val remoteSipUri = pair.second
Log.i(
"$TAG Navigating to conversation with local [$localSipUri] and peer [$remoteSipUri] addresses, computed from shortcut ID"
)
intent.putExtra("LocalSipUri", localSipUri)
intent.putExtra("RemoteSipUri", remoteSipUri)
}
val navGraph = findNavController().navInflater.inflate(R.navigation.main_nav_graph)
navGraph.setStartDestination(R.id.conversationsListFragment)
findNavController().setGraph(navGraph, intent.extras)
@ -507,6 +508,18 @@ class MainActivity : GenericActivity() {
}
}
@MainThread
private fun parseShortcutIfAny(intent: Intent): Pair<String, String>? {
val shortcutId = intent.getStringExtra("android.intent.extra.shortcut.ID") // Intent.EXTRA_SHORTCUT_ID
if (shortcutId != null) {
Log.i("$TAG Found shortcut ID [$shortcutId]")
return LinphoneUtils.getLocalAndPeerSipUrisFromChatRoomId(shortcutId)
} else {
Log.i("$TAG No shortcut ID as found")
}
return null
}
@MainThread
private fun handleCallIntent(intent: Intent) {
val uri = intent.data?.toString()

View file

@ -299,7 +299,7 @@ class ConversationsListFragment : AbstractTopBarFragment() {
sharedViewModel.textToShareFromIntent.observe(viewLifecycleOwner) { textToShare ->
if (textToShare.isNotEmpty()) {
val message = getString(R.string.toast_text_waiting_to_be_shared)
val icon = R.drawable.file
val icon = R.drawable.file_text
(requireActivity() as MainActivity).showGreenToast(message, icon)
Log.i("$TAG Found text waiting to be shared")
}

View file

@ -48,7 +48,7 @@ class LinphoneUtils {
private const val TAG = "[Linphone Utils]"
private const val RECORDING_DATE_PATTERN = "dd-MM-yyyy-HH-mm-ss"
private const val CHAT_ROOM_ID_SEPARATOR = "~"
private const val CHAT_ROOM_ID_SEPARATOR = "#~#"
@WorkerThread
fun getDefaultAccount(): Account? {
@ -254,7 +254,9 @@ class LinphoneUtils {
)
return Pair(localAddress, peerAddress)
} else {
Log.e("$TAG Failed to parse conversation id [$id]")
Log.e(
"$TAG Failed to parse conversation id [$id] with separator [$CHAT_ROOM_ID_SEPARATOR]"
)
}
return null
}

View file

@ -38,6 +38,7 @@ import org.linphone.core.ChatRoom
import org.linphone.core.tools.Log
import org.linphone.mediastream.Version
import org.linphone.ui.main.MainActivity
import org.linphone.ui.main.model.isInSecureMode
class ShortcutUtils {
companion object {
@ -56,21 +57,50 @@ class ShortcutUtils {
Log.e("$TAG Rate limiting is active, aborting")
return
}
Log.i("$TAG Creating launcher shortcuts for conversations")
Log.i("$TAG Creating dynamic shortcuts for conversations")
val defaultAccount = coreContext.core.defaultAccount
if (defaultAccount == null) {
Log.w("$TAG No default account found, skipping...")
return
}
var count = 0
for (room in coreContext.core.chatRooms) {
val shortcut: ShortcutInfoCompat? = createChatRoomShortcut(context, room)
for (chatRoom in defaultAccount.chatRooms) {
if (defaultAccount.isInSecureMode() && !chatRoom.currentParams.isEncryptionEnabled) {
Log.w(
"$TAG Account is in secure mode, skipping not encrypted conversation [${LinphoneUtils.getChatRoomId(
chatRoom
)}]"
)
continue
}
if (isShortcutToChatRoomAlreadyCreated(context, chatRoom)) {
continue
}
if (count >= 5) {
Log.i("$TAG We already created [$count] shortcuts, stopping here")
break
}
val shortcut: ShortcutInfoCompat? = createChatRoomShortcut(context, chatRoom)
if (shortcut != null) {
Log.i("$TAG Created launcher shortcut for ${shortcut.shortLabel}")
val keepGoing = ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
if (!keepGoing) {
count += 1
} else {
break
Log.i("$TAG Created dynamic shortcut for ${shortcut.shortLabel}")
try {
val keepGoing = ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)
if (keepGoing) {
count += 1
} else {
break
}
} catch (e: Exception) {
Log.e("$TAG Failed to push dynamic shortcut for ${shortcut.shortLabel}: $e")
}
}
}
Log.i("$TAG Created $count launcher shortcuts")
Log.i("$TAG Created $count dynamic shortcuts")
}
@WorkerThread