aboutsummaryrefslogtreecommitdiff
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
commit3692d0de20a85ef7e22a31aa9f842078fb774bbe (patch)
tree7ba6c4d9b70507545d5fc0c1e2982e8353e3342e
parent638e3bc6aec499caaf0308c36cace663dc4de3ac (diff)
downloadetbsa-trackermap-mobile-3692d0de20a85ef7e22a31aa9f842078fb774bbe.tar.gz
etbsa-trackermap-mobile-3692d0de20a85ef7e22a31aa9f842078fb774bbe.tar.bz2
etbsa-trackermap-mobile-3692d0de20a85ef7e22a31aa9f842078fb774bbe.zip
Switched to proper Android create file API
-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