Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix storage permission related crash in Android 10 #87

Open
wants to merge 1 commit into
base: feat/path_selection_improvement
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class PermissionContract : ActivityResultContract<String, Boolean>() {
override fun createIntent(
context: Context,
input: String,
) = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, Uri.parse(input))
) = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, Uri.parse(input)).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}

@RequiresApi(Build.VERSION_CODES.R)
override fun parseResult(
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/dev/arkbuilders/arkmemo/models/NoteType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.arkbuilders.arkmemo.models

enum class NoteType {
TEXT,
VOICE,
GRAPHIC,
}
3 changes: 3 additions & 0 deletions app/src/main/java/dev/arkbuilders/arkmemo/repo/NotesRepo.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.arkbuilders.arkmemo.repo

import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arkmemo.models.SaveNoteResult

interface NotesRepo<Note> {
Expand All @@ -15,4 +16,6 @@ interface NotesRepo<Note> {
suspend fun delete(note: Note)

suspend fun delete(notes: List<Note>)

suspend fun findNote(id: ResourceId): Note?
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.graphics.Canvas
import android.os.Environment
import android.util.Log
import dagger.hilt.android.qualifiers.ApplicationContext
import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arklib.computeId
import dev.arkbuilders.arklib.data.index.Resource
import dev.arkbuilders.arkmemo.R
Expand Down Expand Up @@ -116,33 +117,20 @@ class GraphicNotesRepo
private suspend fun readStorage() =
withContext(iODispatcher) {
root.listFiles(SVG_EXT) { path ->
val svg = SVG.parse(path)
if (svg == null) {
Log.w(GRAPHICS_REPO, "Skipping invalid SVG: " + path)
}
val size = path.fileSize()
val id = computeId(size, path)
val resource =
Resource(
id = id,
name = path.fileName.name,
extension = path.extension,
modified = path.getLastModifiedTime(),
)

val userNoteProperties = helper.readProperties(id, "")
val bitmap = exportBitmapFromSvg(fileName = id.toString(), svg = svg)

GraphicNote(
title = userNoteProperties.title,
description = userNoteProperties.description,
svg = svg,
resource = resource,
thumb = bitmap,
)
path.toGraphicNote(helper = helper)
}.filter { graphicNote -> graphicNote.svg != null }
}

override suspend fun findNote(id: ResourceId): GraphicNote? {
return withContext(iODispatcher) {
root.listFiles(SVG_EXT) { path ->
path.toGraphicNote(helper = helper)
}.firstOrNull { graphicNote ->
graphicNote.svg != null && id == graphicNote.resource?.id
}
}
}

private fun exportBitmapFromSvg(
fileName: String,
svg: SVG?,
Expand Down Expand Up @@ -204,6 +192,33 @@ class GraphicNotesRepo
return null
}
}

private fun Path.toGraphicNote(helper: NotesRepoHelper): GraphicNote {
val svg = SVG.parse(this)
if (svg == null) {
Log.w(GRAPHICS_REPO, "Skipping invalid SVG: " + this)
}
val size = this.fileSize()
val id = computeId(size, this)
val resource =
Resource(
id = id,
name = this.fileName.name,
extension = this.extension,
modified = this.getLastModifiedTime(),
)

val userNoteProperties = helper.readProperties(id, "")
val bitmap = exportBitmapFromSvg(fileName = id.toString(), svg = svg)

return GraphicNote(
title = userNoteProperties.title,
description = userNoteProperties.description,
svg = svg,
resource = resource,
thumb = bitmap,
)
}
}

private const val GRAPHICS_REPO = "GraphicNotesRepo"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.arkbuilders.arkmemo.repo.text

import android.util.Log
import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arklib.computeId
import dev.arkbuilders.arklib.data.index.Resource
import dev.arkbuilders.arkmemo.di.IO_DISPATCHER
Expand Down Expand Up @@ -56,6 +57,10 @@ class TextNotesRepo
readStorage()
}

override suspend fun findNote(id: ResourceId): TextNote? {
return null
}

private suspend fun write(
note: TextNote,
callback: (SaveNoteResult) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.arkbuilders.arkmemo.repo.voices

import android.util.Log
import dev.arkbuilders.arklib.ResourceId
import dev.arkbuilders.arklib.computeId
import dev.arkbuilders.arklib.data.index.Resource
import dev.arkbuilders.arkmemo.di.IO_DISPATCHER
Expand Down Expand Up @@ -39,6 +40,16 @@ class VoiceNotesRepo
readStorage()
}

override suspend fun findNote(id: ResourceId): VoiceNote? {
return withContext(iODispatcher) {
root.listFiles(VOICE_EXT) { path ->
path.toVoiceNote(helper = helper)
}.firstOrNull { voiceNote ->
voiceNote.duration.isNotEmpty() && id == voiceNote.resource?.id
}
}
}

override suspend fun delete(notes: List<VoiceNote>) {
helper.deleteNotes(notes)
}
Expand Down Expand Up @@ -101,25 +112,29 @@ class VoiceNotesRepo
private suspend fun readStorage(): List<VoiceNote> =
withContext(iODispatcher) {
root.listFiles(VOICE_EXT) { path ->
val id = computeId(path.fileSize(), path)
val resource =
Resource(
id = id,
name = path.name,
extension = path.extension,
modified = path.getLastModifiedTime(),
)

val userNoteProperties = helper.readProperties(id, "")
VoiceNote(
title = userNoteProperties.title,
description = userNoteProperties.description,
path = path,
duration = extractDuration(path.pathString),
resource = resource,
)
path.toVoiceNote(helper)
}.filter { voiceNote -> voiceNote.duration.isNotEmpty() }
}

private fun Path.toVoiceNote(helper: NotesRepoHelper): VoiceNote {
val id = computeId(this.fileSize(), this)
val resource =
Resource(
id = id,
name = this.name,
extension = this.extension,
modified = this.getLastModifiedTime(),
)

val userNoteProperties = helper.readProperties(id, "")
return VoiceNote(
title = userNoteProperties.title,
description = userNoteProperties.description,
path = this,
duration = extractDuration(this.pathString),
resource = resource,
)
}
}

private const val VOICES_REPO = "VoiceNotesRepo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dev.arkbuilders.arkmemo.ui.activities
import android.content.Intent
import android.os.Bundle
import android.view.WindowManager
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
Expand All @@ -13,7 +12,6 @@ import androidx.fragment.app.Fragment
import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.contracts.PermissionContract
import dev.arkbuilders.arkmemo.databinding.ActivityMainBinding
import dev.arkbuilders.arkmemo.models.RootNotFound
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
Expand All @@ -22,6 +20,7 @@ import dev.arkbuilders.arkmemo.ui.dialogs.FilePickerDialog
import dev.arkbuilders.arkmemo.ui.fragments.BaseFragment
import dev.arkbuilders.arkmemo.ui.fragments.EditTextNotesFragment
import dev.arkbuilders.arkmemo.ui.fragments.NotesFragment
import dev.arkbuilders.arkmemo.utils.PermissionManager
import dev.arkbuilders.components.filepicker.onArkPathPicked
import javax.inject.Inject
import kotlin.io.path.exists
Expand All @@ -37,25 +36,10 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
private val fragContainer = R.id.container

var fragment: Fragment = NotesFragment()
val permissionManager = PermissionManager(activity = this)

init {
FilePickerDialog.readPermLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) {
FilePickerDialog.show()
} else {
finish()
}
}

FilePickerDialog.readPermLauncherSdkR =
registerForActivityResult(PermissionContract()) { isGranted ->
if (isGranted) {
FilePickerDialog.show()
} else {
finish()
}
}
FilePickerDialog.permissionManager = permissionManager
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -72,21 +56,27 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
showFragment(savedInstanceState)
}

val storageFolderExisting = memoPreferences.getNotesStorage().exists()
if (memoPreferences.storageNotAvailable()) {
if (!storageFolderExisting) {
showNoNoteStorageDialog(RootNotFound(rootPath = memoPreferences.getPath()))
} else {
FilePickerDialog.show(this, supportFragmentManager)
}
} else {
if (memoPreferences.isLastLaunchSuccess()) {
showFragment(savedInstanceState)
permissionManager.askForWriteStorage { granted ->
if (!granted) {
finish()
} else {
showRetrySelectRootDialog(
rootPath = memoPreferences.getPath(),
savedInstanceState = savedInstanceState,
)
val storageFolderExisting = memoPreferences.getNotesStorage().exists()
if (memoPreferences.storageNotAvailable()) {
if (!storageFolderExisting) {
showNoNoteStorageDialog(RootNotFound(rootPath = memoPreferences.getPath()))
} else {
FilePickerDialog.show(this, supportFragmentManager)
}
} else {
if (memoPreferences.isLastLaunchSuccess()) {
showFragment(savedInstanceState)
} else {
showRetrySelectRootDialog(
rootPath = memoPreferences.getPath(),
savedInstanceState = savedInstanceState,
)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package dev.arkbuilders.arkmemo.ui.dialogs

import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import dagger.hilt.android.AndroidEntryPoint
import dev.arkbuilders.arkmemo.BuildConfig
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.utils.Permission
import dev.arkbuilders.arkmemo.utils.PermissionManager
import dev.arkbuilders.components.filepicker.ArkFilePickerConfig
import dev.arkbuilders.components.filepicker.ArkFilePickerFragment
import dev.arkbuilders.components.filepicker.ArkFilePickerMode
Expand All @@ -39,8 +34,7 @@ class FilePickerDialog : ArkFilePickerFragment() {
companion object {
private const val TAG = "file_picker"
private lateinit var fragmentManager: FragmentManager
var readPermLauncher: ActivityResultLauncher<String>? = null
var readPermLauncherSdkR: ActivityResultLauncher<String>? = null
var permissionManager: PermissionManager? = null

fun show() {
newInstance(getFilePickerConfig()).show(fragmentManager, TAG)
Expand All @@ -51,10 +45,16 @@ class FilePickerDialog : ArkFilePickerFragment() {
fragmentManager: FragmentManager,
) {
Companion.fragmentManager = fragmentManager
if (isReadPermissionGranted(activity)) {
if (Permission.hasStoragePermission(activity)) {
show()
} else {
askForReadPermissions()
permissionManager?.askForWriteStorage { granted ->
if (granted) {
show()
} else {
activity.finish()
}
}
}
}

Expand All @@ -63,24 +63,6 @@ class FilePickerDialog : ArkFilePickerFragment() {
setConfig(config)
}

private fun isReadPermissionGranted(activity: AppCompatActivity): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Environment.isExternalStorageManager()
} else {
ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_GRANTED
}
}

private fun askForReadPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val packageUri = "package:" + BuildConfig.APPLICATION_ID
readPermLauncherSdkR?.launch(packageUri)
} else {
readPermLauncher?.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
}

private fun getFilePickerConfig() =
ArkFilePickerConfig(
mode = ArkFilePickerMode.FOLDER,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class ArkMediaPlayerFragment : BaseEditNoteFragment() {
return false
}

override fun onViewRestoredWithNote(note: Note) {
}

private fun initUI() {
binding.toolbar.ivRightActionIcon.setOnClickListener {
showDeleteNoteDialog(note)
Expand Down
Loading