-
-
Notifications
You must be signed in to change notification settings - Fork 179
feat: update system favorites on Android 11+ when favoriting media #872
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
Open
terofeev
wants to merge
9
commits into
FossifyOrg:main
Choose a base branch
from
terofeev:update-favorites-using-system-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b262369
Update system favorites collection on Android 11+ when favoriting med…
terofeev d03bacb
Merge remote-tracking branch 'origin/master' into update-favorites-us…
terofeev 8036659
Merge branch 'main' into update-favorites-using-system-api
naveensingh eee4b70
Merge branch 'main' into update-favorites-using-system-api
terofeev 8ce83af
Fix review comments.
terofeev 9eca5fc
Fix favorite updates without "All files access".
terofeev af5db1e
Merge remote-tracking branch 'origin/master' into update-favorites-us…
terofeev eed9510
Fix svg handling.
terofeev 24a7794
Fix svg handling, add whitelist.
terofeev 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,11 @@ package org.fossify.gallery.extensions | |
|
|
||
| import android.app.Activity | ||
| import android.content.ContentProviderOperation | ||
| import android.content.ContentResolver | ||
| import android.content.ContentValues | ||
| import android.content.Intent | ||
| import android.content.pm.PackageManager | ||
| import android.os.Process | ||
| import android.graphics.Bitmap | ||
| import android.graphics.Bitmap.CompressFormat | ||
| import android.graphics.BitmapFactory | ||
|
|
@@ -12,12 +15,14 @@ import android.graphics.Point | |
| import android.graphics.drawable.Drawable | ||
| import android.graphics.drawable.LayerDrawable | ||
| import android.net.Uri | ||
| import android.os.Build | ||
| import android.os.Environment | ||
| import android.provider.MediaStore | ||
| import android.provider.MediaStore.Files | ||
| import android.provider.MediaStore.Images | ||
| import android.provider.Settings | ||
| import android.util.DisplayMetrics | ||
| import androidx.annotation.RequiresApi | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.exifinterface.media.ExifInterface | ||
| import com.bumptech.glide.Glide | ||
|
|
@@ -1072,3 +1077,60 @@ fun Activity.proposeNewFilePath(uri: Uri): Pair<String, Boolean> { | |
|
|
||
| return Pair(newPath, shouldAppendFilename) | ||
| } | ||
|
|
||
| fun Activity.updateFavorite(path: String, isFavorite: Boolean) { | ||
| try { | ||
| if (isFavorite) { | ||
| favoritesDB.insert(getFavoriteFromPath(path)) | ||
| } else { | ||
| favoritesDB.deleteFavoritePath(path) | ||
| } | ||
| // Update media in favorites collection for Android 11+ (API level 30) | ||
| if (isRPlus()) { | ||
| val uri = getFilePublicUri(File(path), BuildConfig.APPLICATION_ID) | ||
| if (isMedia(contentResolver, uri)) { | ||
| updateFavoriteInMediaStore(uri, isFavorite) | ||
| } | ||
| } | ||
| } catch (_: Exception) { | ||
| toast(org.fossify.commons.R.string.unknown_error_occurred) | ||
| } | ||
| } | ||
|
|
||
| private const val FAVORITE_REQUEST_CODE = 100 | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.R) | ||
| private fun Activity.updateFavoriteInMediaStore(uri: Uri, isFavorite: Boolean) { | ||
| if (checkUriPermission( | ||
| uri, Process.myPid(), Process.myUid(), | ||
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION | ||
| ) != PackageManager.PERMISSION_GRANTED | ||
| ) { | ||
| val pendingIntent = MediaStore.createFavoriteRequest(contentResolver, listOf(uri), isFavorite) | ||
| startIntentSenderForResult(pendingIntent.intentSender, FAVORITE_REQUEST_CODE, null, 0, 0, 0) | ||
| } else { | ||
| val values = ContentValues().apply { | ||
| put(MediaStore.MediaColumns.IS_FAVORITE, if (isFavorite) 1 else 0) | ||
| } | ||
| contentResolver.update(uri, values, null) | ||
| } | ||
| } | ||
|
|
||
| private fun isMedia(contentResolver: ContentResolver, uri: Uri): Boolean { | ||
| return try { | ||
| val type = contentResolver.getType(uri) ?: return false | ||
| val supportedImageTypes = setOf( | ||
| "image/jpeg", | ||
| "image/png", | ||
| "image/webp", | ||
| "image/gif", | ||
| "image/bmp", | ||
| "image/x-ms-bmp" | ||
| ) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about other formats such as avif, JXL, etc? Are those failing too? |
||
| val isImage = type in supportedImageTypes | ||
| val isVideo = type.startsWith("video/") | ||
| isImage || isVideo | ||
| } catch (_: Exception) { | ||
| false | ||
| } | ||
| } | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not reuse helpers from the commons lib here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. Using
path.isImageFast()would throw exceptions for AVIF, JXL, etc. (fails on my Android 15 test device).Given Android's scoped storage restrictions, when users grant "media only" access they only see specific MIME types - which is exactly what we should check for in the
isMediafunction.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://developer.android.com/media/platform/supported-formats#image-formats
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested many formats but only
image/jpegconsistently works with the media picker. Propose reducingsupportedImageTypesto justimage/jpeg. What do you think?