Prevent exception causing crash if no Activity is available to create document

This commit is contained in:
Sylvain Berfini 2025-07-26 11:44:00 +02:00
parent 802503f82b
commit 42f1e31fa5
3 changed files with 27 additions and 14 deletions

View file

@ -1,5 +1,6 @@
package org.linphone.ui.fileviewer package org.linphone.ui.fileviewer
import android.content.ActivityNotFoundException
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.util.DisplayMetrics import android.util.DisplayMetrics
@ -127,23 +128,13 @@ class FileViewerActivity : GenericActivity() {
viewModel.exportPlainTextFileEvent.observe(this) { viewModel.exportPlainTextFileEvent.observe(this) {
it.consume { name -> it.consume { name ->
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { exportFile(name, "text/plain")
addCategory(Intent.CATEGORY_OPENABLE)
type = "text/plain"
putExtra(Intent.EXTRA_TITLE, name)
}
startActivityForResult(intent, EXPORT_FILE_AS_DOCUMENT)
} }
} }
viewModel.exportPdfEvent.observe(this) { viewModel.exportPdfEvent.observe(this) {
it.consume { name -> it.consume { name ->
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply { exportFile(name, "application/pdf")
addCategory(Intent.CATEGORY_OPENABLE)
type = "application/pdf"
putExtra(Intent.EXTRA_TITLE, name)
}
startActivityForResult(intent, EXPORT_FILE_AS_DOCUMENT)
} }
} }
} }
@ -212,4 +203,17 @@ class FileViewerActivity : GenericActivity() {
} }
} }
} }
private fun exportFile(name: String, mimeType: String) {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = mimeType
putExtra(Intent.EXTRA_TITLE, name)
}
try {
startActivityForResult(intent, EXPORT_FILE_AS_DOCUMENT)
} catch (exception: ActivityNotFoundException) {
Log.e("$TAG No activity found to handle intent ACTION_CREATE_DOCUMENT: $exception")
}
}
} }

View file

@ -1560,6 +1560,10 @@ open class ConversationFragment : SlidingPaneChildFragment() {
type = mime type = mime
putExtra(Intent.EXTRA_TITLE, name) putExtra(Intent.EXTRA_TITLE, name)
} }
startActivityForResult(intent, EXPORT_FILE_AS_DOCUMENT) try {
startActivityForResult(intent, EXPORT_FILE_AS_DOCUMENT)
} catch (exception: ActivityNotFoundException) {
Log.e("$TAG No activity found to handle intent ACTION_CREATE_DOCUMENT: $exception")
}
} }
} }

View file

@ -19,6 +19,7 @@
*/ */
package org.linphone.ui.main.sso.fragment package org.linphone.ui.main.sso.fragment
import android.content.ActivityNotFoundException
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
@ -75,7 +76,11 @@ class SingleSignOnFragment : GenericMainFragment() {
viewModel.startAuthIntentEvent.observe(viewLifecycleOwner) { viewModel.startAuthIntentEvent.observe(viewLifecycleOwner) {
it.consume { intent -> it.consume { intent ->
Log.i("$TAG Starting auth intent activity") Log.i("$TAG Starting auth intent activity")
startActivityForResult(intent, ACTIVITY_RESULT_ID) try {
startActivityForResult(intent, ACTIVITY_RESULT_ID)
} catch (exception: ActivityNotFoundException) {
Log.e("$TAG No activity found to handle intent: $exception")
}
} }
} }