Skip to content

Commit

Permalink
Request for permissions on CSV export (#211)
Browse files Browse the repository at this point in the history
Closes #186
  • Loading branch information
Ohior authored Apr 4, 2023
1 parent 9334d74 commit 749eafc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MainActivity : AbstractBaseActivity(), SwipeRefreshLayout.OnRefreshListene
) { isGranted: Map<String, Boolean> ->
if (isGranted.all { it.value }) {
// phone permissions granted
if(isGranted.containsKey(CALL_PHONE) || isGranted.containsKey(READ_PHONE_STATE)){
if (isGranted.containsKey(CALL_PHONE) || isGranted.containsKey(READ_PHONE_STATE)) {
binding.swiperefresh.isRefreshing = true
setDefaultSubscriptionId()
onRefresh()
Expand All @@ -58,6 +58,21 @@ class MainActivity : AbstractBaseActivity(), SwipeRefreshLayout.OnRefreshListene
}
}

private val requestStoragePermission = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
// Because of api version some device need permission to save file in storage
// so a contract is created
val canRead = permissions[READ_EXTERNAL_STORAGE] ?: false
val canWrite = permissions[WRITE_EXTERNAL_STORAGE] ?: false
if (canRead && canWrite) {
// when user grant read and write permission,
// export the file to download folder
exportAsCsv()
} else {
// User did not grant permission so an error is displayed for the user to see
showSnackbar(R.string.export_error_saving_file)
}
}

override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
// Apply dynamic colors here to avoid losing them due to splash screen: https://github.com/material-components/material-components-android/issues/2555
Expand Down Expand Up @@ -117,15 +132,28 @@ class MainActivity : AbstractBaseActivity(), SwipeRefreshLayout.OnRefreshListene

override fun onOptionsItemSelected(item: MenuItem): Boolean {
Log.d(TAG, "onOptionsItemSelected($item)")
return when(item.itemId) {
return when (item.itemId) {
R.id.preferences -> {
Intent(this, PreferenceActivity::class.java).apply {
startActivity(this)
}
true
}
R.id.export -> {
exportAsCsv()
if (
Build.VERSION.SDK_INT <= Build.VERSION_CODES.P &&
!hasPermissions(READ_EXTERNAL_STORAGE) &&
!hasPermissions(WRITE_EXTERNAL_STORAGE)
) {
// this if statement check if this device is with in the version that needs
// permission to save file. It also check if read and write permission not granted.
// It then launch the contract for the user to grant the permission
requestStoragePermission.launch(arrayOf(WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE))
} else {
// modern device does not need permission to save file in public folder like
// download
exportAsCsv()
}
true
}
android.R.id.home -> {
Expand All @@ -136,20 +164,20 @@ class MainActivity : AbstractBaseActivity(), SwipeRefreshLayout.OnRefreshListene
}
}

/**
* Export a CSV file to the Downloads folder.
*/
private fun exportAsCsv() {
launch {
val content = buildCsv()

try {
val content = buildCsv()
val filename = "prepaid-balance-${System.currentTimeMillis()}.csv"
writeToFileInDownloads(content, filename)
showSnackbar(getString(R.string.export_saved_file, filename))
return@launch
} catch (e: Exception) {
Log.e(TAG, "Error saving file", e)
showSnackbar(R.string.export_error_saving_file)
}

showSnackbar(R.string.export_error_saving_file)
}
}

Expand Down

0 comments on commit 749eafc

Please sign in to comment.