From cfd54183ba1401deece64cb92e352076a743dfae Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Thu, 13 Jan 2022 22:09:47 -0600 Subject: Initial attempt to request WRITE_EXTERNAL_STORAGE permission --- .../TrackerMap/android/details/DetailsActivity.kt | 32 ++++++++++++++++++++++ .../android/details/reports/UnitReportsFragment.kt | 15 ++++++++++ .../details/reports/UnitReportsViewModel.kt | 5 ++++ 3 files changed, 52 insertions(+) (limited to 'androidApp/src') 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 d942de2..c6a4b2e 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 @@ -1,8 +1,12 @@ package mx.trackermap.TrackerMap.android.details +import android.Manifest +import android.content.pm.PackageManager import android.os.Bundle import android.util.Log +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity +import androidx.core.app.ActivityCompat import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayoutMediator import mx.trackermap.TrackerMap.android.R @@ -75,5 +79,33 @@ class DetailsActivity : AppCompatActivity() { override fun onTabUnselected(tab: TabLayout.Tab?) {} override fun onTabReselected(tab: TabLayout.Tab?) {} }) + + requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) + } + + private fun requestPermission (permission: String) { + val launcher = registerForActivityResult( + ActivityResultContracts.RequestPermission() + ) { isGranted: Boolean -> + if (isGranted) { + Log.d("DetailsActivity", "$permission = PackageManager.PERMISSION_GRANTED") + } else { + Log.d("UnitReportsFragment", "$permission = PackageManager.PERMISSION_DENIED") + } + } + + when { + ActivityCompat.checkSelfPermission(this, permission) + == PackageManager.PERMISSION_GRANTED -> { + Log.d("DetailsActivity", "$permission = PackageManager.PERMISSION_GRANTED") + } + ActivityCompat.shouldShowRequestPermissionRationale(this, permission) -> { + Log.d("DetailsActivity", "shouldShowRequestPermissionRationale") + } + else -> { + Log.d("DetailsActivity", "Requesting $permission permission") + launcher.launch(permission) + } + } } } \ No newline at end of file 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 2cb2d9a..2c8e1c6 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,5 +1,7 @@ package mx.trackermap.TrackerMap.android.details.reports +import android.Manifest +import android.content.pm.PackageManager import android.os.Bundle import android.util.Log import android.view.LayoutInflater @@ -8,6 +10,7 @@ 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 @@ -121,6 +124,10 @@ class UnitReportsFragment : Fragment() { ) } + unitReportsViewModel.reportFile.observe(viewLifecycleOwner) { file -> + downloadFile(file, "report.xlsx") + } + unitReportsViewModel.geofences.observe(viewLifecycleOwner) { geofences -> Log.d("UnitReportsFragment", "Success $geofences") reportsMapFragment.displayGeofences(geofences.values.toTypedArray()) @@ -256,4 +263,12 @@ 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") + } + } } \ No newline at end of file diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt index 8db7cda..fe13ba0 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt @@ -42,12 +42,14 @@ class UnitReportsViewModel( private val _reportPeriod: MutableLiveData = savedStateHandle.getLiveData("reportPeriod", null) private val _report: MutableLiveData = MutableLiveData() + private val _reportFile: MutableLiveData = MutableLiveData() private val _geofences: MutableLiveData> = MutableLiveData() val deviceId: LiveData get() = _deviceId val reportType: LiveData get() = _reportType val reportPeriod: LiveData get() = _reportPeriod val report: LiveData get() = _report + val reportFile: LiveData get() = _reportFile val geofences: LiveData> get() = _geofences private val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.getDefault()) @@ -157,6 +159,7 @@ class UnitReportsViewModel( _deviceId.value!! ) Log.d("UnitReportsVM", "Positions report: $result") + _reportFile.postValue(result) } } @@ -195,6 +198,7 @@ class UnitReportsViewModel( _deviceId.value!! ) Log.d("UnitReportsVM", "Events report: $result") + _reportFile.postValue(result) } } @@ -216,6 +220,7 @@ class UnitReportsViewModel( _deviceId.value!! ) Log.d("UnitReportsVM", "Stops report: $result") + _reportFile.postValue(result) } } -- cgit v1.2.3