Repository navigation
fix: preserve GPS metadata in uploaded photos and videos #234
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
Merged
kulmann
merged 3 commits into
opencloud-eu:main
from
bluecasita:fix/preserve_gps_exif_on_upload
Nov 11, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...ncloud/android/presentation/settings/automaticuploads/MediaLocationPermissionRequester.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /** | ||
| * openCloud Android client application | ||
| * | ||
| * Copyright (C) 2026 OpenCloud GmbH. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package eu.opencloud.android.presentation.settings.automaticuploads | ||
|
|
||
| import android.Manifest | ||
| import androidx.activity.result.contract.ActivityResultContracts | ||
| import androidx.fragment.app.Fragment | ||
| import eu.opencloud.android.R | ||
| import eu.opencloud.android.data.providers.SharedPreferencesProvider | ||
| import eu.opencloud.android.extensions.showMessageInSnackbar | ||
| import eu.opencloud.android.utils.MediaLocationUtils | ||
| import org.koin.android.ext.android.inject | ||
|
|
||
| /** | ||
| * Asks for [Manifest.permission.ACCESS_MEDIA_LOCATION] from the picture and video automatic | ||
| * upload settings. Without it, Android strips the GPS metadata from the media we read (API >= 29). | ||
| * | ||
| * The system dialog is only launched when Android will really show it, so the user is never | ||
| * prompted on every visit and the "denied" hint only follows an answer the user actually gave. | ||
| * | ||
| * Must be created while the fragment is being initialized (it registers an activity result launcher). | ||
| */ | ||
| class MediaLocationPermissionRequester(private val fragment: Fragment) { | ||
|
|
||
| private val preferencesProvider: SharedPreferencesProvider by fragment.inject() | ||
|
|
||
| private val requestPermissionLauncher = | ||
| fragment.registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted -> | ||
| if (!isGranted && fragment.isAdded) { | ||
| fragment.showMessageInSnackbar(fragment.getString(R.string.media_location_permission_denied)) | ||
| } | ||
| } | ||
|
|
||
| // True between the user switching uploads on and the "Important" dialog being dismissed, | ||
| // so the system dialog does not open on top of it. | ||
| private var isPostponed = false | ||
|
|
||
| /** | ||
| * The screen shows enabled automatic uploads: ask, but only if the user was never asked before. | ||
| */ | ||
| fun requestIfNeverAsked() { | ||
| if (isPostponed) return | ||
| request(userInitiated = false) | ||
| } | ||
|
|
||
| /** | ||
| * The user is switching automatic uploads on. Holds back [requestIfNeverAsked] until | ||
| * [requestAfterEnabling] is called. | ||
| */ | ||
| fun postpone() { | ||
| isPostponed = true | ||
| } | ||
|
|
||
| /** | ||
| * The user switched automatic uploads on and has dismissed the "Important" dialog. Asks again | ||
| * after an earlier denial as long as Android still shows the dialog. | ||
| */ | ||
| fun requestAfterEnabling() { | ||
| isPostponed = false | ||
| request(userInitiated = true) | ||
| } | ||
|
|
||
| private fun request(userInitiated: Boolean) { | ||
| val context = fragment.context ?: return | ||
| if (!fragment.isAdded) return | ||
|
|
||
| val shouldRequest = shouldRequest( | ||
| isGranted = MediaLocationUtils.isAccessMediaLocationGranted(context), | ||
| alreadyRequested = preferencesProvider.getBoolean(PREFERENCE_MEDIA_LOCATION_PERMISSION_REQUESTED, false), | ||
| shouldShowRationale = fragment.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_MEDIA_LOCATION), | ||
| userInitiated = userInitiated, | ||
| ) | ||
| if (!shouldRequest) return | ||
|
|
||
| preferencesProvider.putBoolean(PREFERENCE_MEDIA_LOCATION_PERMISSION_REQUESTED, true) | ||
| requestPermissionLauncher.launch(Manifest.permission.ACCESS_MEDIA_LOCATION) | ||
| } | ||
|
|
||
| companion object { | ||
| // Shared by pictures and videos: it is one permission, asked once. | ||
| internal const val PREFERENCE_MEDIA_LOCATION_PERMISSION_REQUESTED = "PREFERENCE_MEDIA_LOCATION_PERMISSION_REQUESTED" | ||
|
|
||
| /** | ||
| * - Never asked: ask. | ||
| * - Asked and denied once: Android reports a rationale and still shows the dialog. Ask again | ||
| * only when the user just enabled uploads, not on every visit to the screen. | ||
| * - Asked and denied for good: Android answers "denied" by itself without showing anything, | ||
| * so do not launch at all. | ||
| */ | ||
| internal fun shouldRequest( | ||
| isGranted: Boolean, | ||
| alreadyRequested: Boolean, | ||
| shouldShowRationale: Boolean, | ||
| userInitiated: Boolean, | ||
| ): Boolean = !isGranted && (!alreadyRequested || (userInitiated && shouldShowRationale)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
opencloudApp/src/main/java/eu/opencloud/android/utils/MediaLocationUtils.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /** | ||
| * openCloud Android client application | ||
| * | ||
| * Copyright (C) 2026 OpenCloud GmbH. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2, | ||
| * as published by the Free Software Foundation. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package eu.opencloud.android.utils | ||
|
|
||
| import android.Manifest | ||
| import android.content.ContentResolver | ||
| import android.content.Context | ||
| import android.content.pm.PackageManager | ||
| import android.net.Uri | ||
| import android.os.Build | ||
| import android.provider.DocumentsContract | ||
| import android.provider.MediaStore | ||
| import androidx.annotation.RequiresApi | ||
| import androidx.core.content.ContextCompat | ||
| import timber.log.Timber | ||
| import java.io.InputStream | ||
|
|
||
| /** | ||
| * Since Android 10 the system redacts location metadata (GPS EXIF) from photos and videos | ||
| * read through the media store or the storage access framework, unless the app holds | ||
| * [Manifest.permission.ACCESS_MEDIA_LOCATION] AND asks for the original via | ||
| * [MediaStore.setRequireOriginal]. Without this, uploads silently lose their location. | ||
| */ | ||
| object MediaLocationUtils { | ||
|
|
||
| @JvmStatic | ||
| fun isAccessMediaLocationGranted(context: Context): Boolean = | ||
| Build.VERSION.SDK_INT < Build.VERSION_CODES.Q || hasMediaLocationPermission(context) | ||
|
|
||
| /** | ||
| * Returns a URI that yields the unredacted original of [uri] (location metadata intact), | ||
| * or [uri] itself when that is not possible: older Android, permission not granted, or a | ||
| * content URI that has no media store counterpart (non-media documents, cloud providers...). | ||
| * | ||
| * SAF document URIs (what automatic uploads use) are translated to their media store URI | ||
| * with [MediaStore.getMediaUri]; access is granted through the same document grant. | ||
| */ | ||
| @JvmStatic | ||
| fun getOriginalMediaUri(context: Context, uri: Uri): Uri = | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) getOriginalMediaUriApi29(context, uri) else uri | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.Q) | ||
| private fun hasMediaLocationPermission(context: Context): Boolean = | ||
| ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_MEDIA_LOCATION) == PackageManager.PERMISSION_GRANTED | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.Q) | ||
| private fun getOriginalMediaUriApi29(context: Context, uri: Uri): Uri { | ||
| if (!hasMediaLocationPermission(context)) return uri | ||
|
|
||
| val mediaUri: Uri = when { | ||
| uri.authority == MediaStore.AUTHORITY -> uri | ||
| DocumentsContract.isDocumentUri(context, uri) -> try { | ||
| MediaStore.getMediaUri(context, uri) | ||
| } catch (e: Exception) { | ||
| Timber.d(e, "No media store URI for %s, uploading it as is", uri) | ||
| null | ||
| } ?: return uri | ||
| else -> return uri | ||
| } | ||
| return MediaStore.setRequireOriginal(mediaUri) | ||
| } | ||
|
|
||
| /** | ||
| * Opens [uri] for reading, preferring the unredacted original (see [getOriginalMediaUri]). | ||
| * Falls back to opening [uri] directly if the original can't be opened, so an upload never | ||
| * fails because of the location metadata. | ||
| */ | ||
| @JvmStatic | ||
| fun openInputStreamPreservingLocation(context: Context, contentResolver: ContentResolver, uri: Uri): InputStream? { | ||
| val originalUri = getOriginalMediaUri(context, uri) | ||
| if (originalUri != uri) { | ||
| try { | ||
| contentResolver.openInputStream(originalUri)?.let { return it } | ||
| } catch (e: Exception) { | ||
| Timber.w(e, "Could not open original media for %s, falling back to redacted copy", uri) | ||
| } | ||
| } | ||
| return contentResolver.openInputStream(uri) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.