keshav-space / safenotes

Safe Notes is a security project aimed at providing an encrypted, private note manager that works locally and protects notes from various threat actors.
https://safenotes.dev
GNU General Public License v3.0
233 stars 22 forks source link

Can't choose destination folder for backup [ Play Store Review ] #9

Closed keshav-space closed 2 years ago

thornySoap commented 1 year ago

I still can't on 2.1.0

keshav-space commented 1 year ago

Android is migrating towards more privacy-friendly Android APIs.

Below is a brief summary of how that impacts storage access

Android >= 11 ( New )

  1. No Storage Permission : Apps can create new files in the download folder without permission
  2. Media and File Permission : Apps can read media and files but can't modify or create files except in app-specific storage space [ moreover, the android will never expose the actual file location rather a cached file location is shared with apps]
  3. All File Permission : When MANAGE_EXTERNAL_STORAGE permission is granted, app can read/modify/create/delete files in external shared storage.

Android < 11 ( Old )

  1. No Storage Permission : App can not access any part of external shared storage
  2. File Permission : App can read, modify, create and delete files in external shared storage.

How Safe Notes will handle the storage access?

These access control will become more stringent in the coming year, something similar to iOS, and that's good for user privacy. Bottom line is that apps should not use All file Permission unless absolutely needed. Safe Notes will not use All File Permission, this means that users will not be able to choose the location of their choice for backup. Backup will be made inside the Download directory within the Safe Notes folder. [See videos below ]

The new feature is tested on Android 12, 11, 10, 9, 8.1, 8.0 and 7.1

Android 12 Android 12 Android 11 Android 10
Android 9 Android 8.1 Android 8.0 Android 7.1
thornySoap commented 1 year ago

Thanks for the reply, I understand.

I just would like to ask why Safe Notes doesn't use this method used by most modern apps to gain access to a specific directory.

keshav-space commented 1 year ago

I just would like to ask why Safe Notes doesn't use this method used by most modern apps to gain access to a specific directory.

Safe Notes is already using this for reading import files [ user can pick the import file from anywhere while importing backup]

This permission can't be used to write/modify existing files moreover the new files can only be created in the Download directory or app-specific storage location.

To write a file at an arbitrary [user chosen] location app would require MANAGE_EXTERNAL_STORAGE permission

There is no escape from this.

thornySoap commented 1 year ago

I am very sorry, I linked the wrong thing. This intent FLAG_GRANT_WRITE_URI_PERMISSION should be the right one (if it's not, I really have to apologize for annoying you without having any idea myself).

You may want to have a look at this notes app:

autoExportLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            val uri = result.data?.data
            if (result.resultCode == Activity.RESULT_OK && uri != null) {
                val output = try {
                    val cr = context.contentResolver
                    cr.takePersistableUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
                    cr.openOutputStream(uri)
                } catch (e: Exception) {
                    Log.i(TAG, "Data export failed", e)
                    null
                }
                if (output != null) {
                    viewModel.setupAutoExport(output, uri.toString())
                } else {
                    showMessage(R.string.export_fail)
                    autoExportPref.isChecked = false
                }
            }
        }

link to file

fun setupAutoExport(output: OutputStream, uri: String) {
        prefsManager.autoExportUri = uri
        viewModelScope.launch(Dispatchers.IO) {
            val jsonData = jsonManager.exportJsonData()
            try {
                output.use {
                    output.write(jsonData.toByteArray())
                }
                showMessage(R.string.export_success)

                val now = System.currentTimeMillis()
                prefsManager.lastAutoExportTime = now
                _lastAutoExport.postValue(now)
            } catch (e: Exception) {
                showMessage(R.string.export_fail)
            }
        }
    }

link to file

Here is a screen recording:

keshav-space commented 1 year ago

This intent FLAG_GRANT_WRITE_URI_PERMISSION should be the right one.

I see you're pointing to the Storage Access Framework and I could have used that, but the only problem I'm having is that the SAF gives you a content URI that doesn't reflect the exact location of the file on the filesystem.

Suppose user creates a backup file at a certain location, the app will receive the content URI of that file. The app can access and modify the content of that file using content URI, but the app has no clue about where the file is exactly stored, so the app can't show the location of the backup file inside the app. And this is a problem because, a few months down the line, user will forget where they created the backup file. (happens to me all the time, lol 😆) Since the app can't show the exact location of the file, user will end up creating a new backup and this will go on.

For an offline app, it's paramount that the user can independently access the backup file.


if it's not, I really have to apologize for annoying you without having any idea myself

Indulgence is much appreciated and always welcome, that's the point of having this open-source. 😊



TL;DR

SAF would work only if there was some way to show the exact location of backup file like this 👇

thornySoap commented 8 months ago

Hello again. This app (https://github.com/T8RIN/ImageToolbox) allows to pick a location and it is also able to show it. If there still is interest in working on this app, it may be worth a look.

https://github.com/keshav-space/safenotes/assets/115401023/4997811b-eeec-4791-a2e5-ed84ce62307c