aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-01-14 01:32:01 -0600
committerIván Ávalos <avalos@disroot.org>2022-01-14 01:32:01 -0600
commitc16c9f5c280300a2f74cb728fdca627a132fc38e (patch)
tree7ba6c4d9b70507545d5fc0c1e2982e8353e3342e /androidApp/src/main
parentdf3e96c914880f319bc9844e6b9f2f1bcc906ccd (diff)
downloadetbsa-trackermap-mobile-c16c9f5c280300a2f74cb728fdca627a132fc38e.tar.gz
etbsa-trackermap-mobile-c16c9f5c280300a2f74cb728fdca627a132fc38e.tar.bz2
etbsa-trackermap-mobile-c16c9f5c280300a2f74cb728fdca627a132fc38e.zip
Switched to proper Android create file API
Diffstat (limited to 'androidApp/src/main')
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt53
1 files changed, 38 insertions, 15 deletions
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt
index ed4af2e..b67dd94 100644
--- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt
@@ -1,7 +1,11 @@
package mx.trackermap.TrackerMap.android.details.reports
+import android.app.Activity
+import android.content.Intent
+import android.os.Build
import android.os.Bundle
import android.os.Environment
+import android.provider.DocumentsContract
import android.util.Log
import android.view.LayoutInflater
import android.view.View
@@ -9,6 +13,7 @@ import android.view.ViewGroup
import android.widget.PopupMenu
import android.widget.TableRow
import android.widget.TextView
+import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.view.setMargins
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
@@ -23,8 +28,6 @@ import mx.trackermap.TrackerMap.client.models.Position
import mx.trackermap.TrackerMap.client.models.Stop
import mx.trackermap.TrackerMap.utils.Formatter
import org.koin.androidx.viewmodel.ext.android.viewModel
-import java.io.File
-import java.io.FileOutputStream
import java.lang.Exception
import kotlin.math.max
@@ -125,8 +128,8 @@ class UnitReportsFragment : Fragment() {
)
}
- unitReportsViewModel.reportFile.observe(viewLifecycleOwner) { file ->
- downloadFile(file, "report.xlsx")
+ unitReportsViewModel.reportFile.observe(viewLifecycleOwner) {
+ downloadFile("report.xlsx")
}
unitReportsViewModel.geofences.observe(viewLifecycleOwner) { geofences ->
@@ -265,18 +268,38 @@ class UnitReportsFragment : Fragment() {
}
}
- private fun downloadFile(file: ByteArray, filename: String) {
- Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).let { filesDir ->
- Log.d("UnitReportsFragment", "Downloading file $filename into ${filesDir.absolutePath}")
- try {
- val outputFile = File(filesDir, filename)
- val outputStream = FileOutputStream(outputFile)
- outputStream.write(file)
- outputStream.flush()
- outputStream.close()
- } catch (e: Exception) {
- e.printStackTrace()
+ private fun downloadFile(filename: String) {
+ val filesDir = requireContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
+ filesDir?.let {
+ val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
+ addCategory(Intent.CATEGORY_OPENABLE)
+ type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
+ putExtra(Intent.EXTRA_TITLE, filename)
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ putExtra(DocumentsContract.EXTRA_INITIAL_URI, filesDir)
+ }
}
+ createDocumentResult.launch(intent)
}
}
+
+ private val createDocumentResult =
+ registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
+ if (it.resultCode == Activity.RESULT_OK) {
+ it?.data?.data?.also { uri ->
+ Log.d("UnitReportsFragment", "Downloading file into ${uri.path}")
+ val outputStream = context?.contentResolver?.openOutputStream(uri)
+ outputStream?.let {
+ try {
+ outputStream.write(unitReportsViewModel.reportFile.value)
+ outputStream.flush()
+ outputStream.close()
+ Log.d("UnitReportsFragment", "Wrote file into ${uri.path}")
+ } catch (e: Exception) {
+ e.printStackTrace()
+ }
+ }
+ }
+ }
+ }
} \ No newline at end of file