Allow opening files in chat bubble
This commit is contained in:
parent
99d9ec8508
commit
fdbced67c2
1 changed files with 48 additions and 0 deletions
|
@ -19,8 +19,11 @@
|
||||||
*/
|
*/
|
||||||
package org.linphone.activities.chat_bubble
|
package org.linphone.activities.chat_bubble
|
||||||
|
|
||||||
|
import android.content.ActivityNotFoundException
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.webkit.MimeTypeMap
|
||||||
import androidx.databinding.DataBindingUtil
|
import androidx.databinding.DataBindingUtil
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
@ -28,6 +31,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import org.linphone.LinphoneApplication
|
||||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||||
import org.linphone.R
|
import org.linphone.R
|
||||||
import org.linphone.activities.GenericActivity
|
import org.linphone.activities.GenericActivity
|
||||||
|
@ -39,6 +43,7 @@ import org.linphone.core.ChatRoom
|
||||||
import org.linphone.core.Factory
|
import org.linphone.core.Factory
|
||||||
import org.linphone.core.tools.Log
|
import org.linphone.core.tools.Log
|
||||||
import org.linphone.databinding.ChatBubbleActivityBinding
|
import org.linphone.databinding.ChatBubbleActivityBinding
|
||||||
|
import org.linphone.utils.FileUtils
|
||||||
|
|
||||||
class ChatBubbleActivity : GenericActivity() {
|
class ChatBubbleActivity : GenericActivity() {
|
||||||
private lateinit var binding: ChatBubbleActivityBinding
|
private lateinit var binding: ChatBubbleActivityBinding
|
||||||
|
@ -107,6 +112,12 @@ class ChatBubbleActivity : GenericActivity() {
|
||||||
// Disable context menu on each message
|
// Disable context menu on each message
|
||||||
adapter.disableContextMenu()
|
adapter.disableContextMenu()
|
||||||
|
|
||||||
|
adapter.openContentEvent.observe(this, {
|
||||||
|
it.consume { path ->
|
||||||
|
openFile(path)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
val layoutManager = LinearLayoutManager(this)
|
val layoutManager = LinearLayoutManager(this)
|
||||||
layoutManager.stackFromEnd = true
|
layoutManager.stackFromEnd = true
|
||||||
binding.chatMessagesList.layoutManager = layoutManager
|
binding.chatMessagesList.layoutManager = layoutManager
|
||||||
|
@ -163,4 +174,41 @@ class ChatBubbleActivity : GenericActivity() {
|
||||||
binding.chatMessagesList.scrollToPosition(adapter.itemCount - 1)
|
binding.chatMessagesList.scrollToPosition(adapter.itemCount - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun openFile(contentFilePath: String) {
|
||||||
|
val intent = Intent(Intent.ACTION_VIEW)
|
||||||
|
val contentUri: Uri = FileUtils.getPublicFilePath(this, contentFilePath)
|
||||||
|
val filePath: String = contentUri.toString()
|
||||||
|
Log.i("[Chat Bubble] Trying to open file: $filePath")
|
||||||
|
var type: String? = null
|
||||||
|
val extension = FileUtils.getExtensionFromFileName(filePath)
|
||||||
|
|
||||||
|
if (extension.isNotEmpty()) {
|
||||||
|
Log.i("[Chat Bubble] Found extension $extension")
|
||||||
|
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
|
||||||
|
} else {
|
||||||
|
Log.e("[Chat Bubble] Couldn't find extension")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type != null) {
|
||||||
|
Log.i("[Chat Bubble] Found matching MIME type $type")
|
||||||
|
} else {
|
||||||
|
type = "file/$extension"
|
||||||
|
Log.e("[Chat Bubble] Can't get MIME type from extension: $extension, will use $type")
|
||||||
|
}
|
||||||
|
|
||||||
|
intent.setDataAndType(contentUri, type)
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||||
|
|
||||||
|
try {
|
||||||
|
startActivity(intent)
|
||||||
|
|
||||||
|
if (LinphoneApplication.corePreferences.enableAnimations) {
|
||||||
|
overridePendingTransition(R.anim.enter_right, R.anim.exit_left)
|
||||||
|
}
|
||||||
|
} catch (anfe: ActivityNotFoundException) {
|
||||||
|
Log.e("[Chat Bubble] Couldn't find an activity to handle MIME type: $type")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue