diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-01-13 22:09:47 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-01-13 22:09:47 -0600 |
commit | cfd54183ba1401deece64cb92e352076a743dfae (patch) | |
tree | fdcf5001423401030d760003b0a427616a745358 /androidApp/src | |
parent | 64f2d3cd5619d6a7dccbaa08aeabca9c8b350a43 (diff) | |
download | etbsa-trackermap-mobile-cfd54183ba1401deece64cb92e352076a743dfae.tar.gz etbsa-trackermap-mobile-cfd54183ba1401deece64cb92e352076a743dfae.tar.bz2 etbsa-trackermap-mobile-cfd54183ba1401deece64cb92e352076a743dfae.zip |
Initial attempt to request WRITE_EXTERNAL_STORAGE permission
Diffstat (limited to 'androidApp/src')
3 files changed, 52 insertions, 0 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 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<ReportPeriod> = savedStateHandle.getLiveData("reportPeriod", null) private val _report: MutableLiveData<Report> = MutableLiveData() + private val _reportFile: MutableLiveData<ByteArray> = MutableLiveData() private val _geofences: MutableLiveData<Map<Int, Geofence>> = MutableLiveData() val deviceId: LiveData<Int> get() = _deviceId val reportType: LiveData<ReportType> get() = _reportType val reportPeriod: LiveData<ReportPeriod> get() = _reportPeriod val report: LiveData<Report> get() = _report + val reportFile: LiveData<ByteArray> get() = _reportFile val geofences: LiveData<Map<Int, Geofence>> 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) } } |