aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-01-14 00:38:49 -0600
committerIván Ávalos <avalos@disroot.org>2022-01-14 00:38:49 -0600
commitdf3e96c914880f319bc9844e6b9f2f1bcc906ccd (patch)
tree8fcb9fe817970109c812a564427af190b02541d0 /androidApp/src/main/java
parent1f6201d8cd0dd389c697e542f221e2b98332a98e (diff)
downloadetbsa-trackermap-mobile-df3e96c914880f319bc9844e6b9f2f1bcc906ccd.tar.gz
etbsa-trackermap-mobile-df3e96c914880f319bc9844e6b9f2f1bcc906ccd.tar.bz2
etbsa-trackermap-mobile-df3e96c914880f319bc9844e6b9f2f1bcc906ccd.zip
Save downloaded XLSX to downloads folder
Diffstat (limited to 'androidApp/src/main/java')
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt14
-rw-r--r--androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt22
2 files changed, 19 insertions, 17 deletions
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt
index c6a4b2e..2c20c70 100644
--- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt
+++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/DetailsActivity.kt
@@ -80,19 +80,13 @@ class DetailsActivity : AppCompatActivity() {
override fun onTabReselected(tab: TabLayout.Tab?) {}
})
- requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
+ requestPermission {}
}
- private fun requestPermission (permission: String) {
+ private fun requestPermission (callback: (Boolean) -> Unit) {
+ val permission = Manifest.permission.WRITE_EXTERNAL_STORAGE
val launcher = registerForActivityResult(
- ActivityResultContracts.RequestPermission()
- ) { isGranted: Boolean ->
- if (isGranted) {
- Log.d("DetailsActivity", "$permission = PackageManager.PERMISSION_GRANTED")
- } else {
- Log.d("UnitReportsFragment", "$permission = PackageManager.PERMISSION_DENIED")
- }
- }
+ ActivityResultContracts.RequestPermission(), callback)
when {
ActivityCompat.checkSelfPermission(this, permission)
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 2c8e1c6..ed4af2e 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,8 +1,7 @@
package mx.trackermap.TrackerMap.android.details.reports
-import android.Manifest
-import android.content.pm.PackageManager
import android.os.Bundle
+import android.os.Environment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
@@ -10,7 +9,6 @@ import android.view.ViewGroup
import android.widget.PopupMenu
import android.widget.TableRow
import android.widget.TextView
-import androidx.core.app.ActivityCompat
import androidx.core.view.setMargins
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
@@ -25,6 +23,9 @@ 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
@DelicateCoroutinesApi
@@ -265,10 +266,17 @@ class UnitReportsFragment : Fragment() {
}
private fun downloadFile(file: ByteArray, filename: String) {
- val permission = Manifest.permission.WRITE_EXTERNAL_STORAGE
- if (ActivityCompat.checkSelfPermission(activity!!, permission)
- == PackageManager.PERMISSION_GRANTED) {
- Log.d("UnitReportsFragment", "Downloading file $filename: $file")
+ 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()
+ }
}
}
} \ No newline at end of file