aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src
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
commit638e3bc6aec499caaf0308c36cace663dc4de3ac (patch)
tree8fcb9fe817970109c812a564427af190b02541d0 /androidApp/src
parent07cbed18c86307c96fe5e40ff04827f3747d6018 (diff)
downloadetbsa-trackermap-mobile-638e3bc6aec499caaf0308c36cace663dc4de3ac.tar.gz
etbsa-trackermap-mobile-638e3bc6aec499caaf0308c36cace663dc4de3ac.tar.bz2
etbsa-trackermap-mobile-638e3bc6aec499caaf0308c36cace663dc4de3ac.zip
Save downloaded XLSX to downloads folder
Diffstat (limited to 'androidApp/src')
-rw-r--r--androidApp/src/main/AndroidManifest.xml1
-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
3 files changed, 20 insertions, 17 deletions
diff --git a/androidApp/src/main/AndroidManifest.xml b/androidApp/src/main/AndroidManifest.xml
index 6501532..23e24bc 100644
--- a/androidApp/src/main/AndroidManifest.xml
+++ b/androidApp/src/main/AndroidManifest.xml
@@ -4,6 +4,7 @@
package="mx.trackermap.TrackerMap.android">
<uses-permission android:name="android.permission.INTERNET" />
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name=".TrackerApp"
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