Fix crashes on older Android versions for save

This commit is contained in:
Matthieu 2024-01-03 16:15:14 +01:00
parent b975a255f9
commit 27413cd08e
30 changed files with 77 additions and 33 deletions

View File

@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="29" />
<uses-feature
android:name="android.hardware.camera.any"

View File

@ -5,7 +5,9 @@ import android.util.Log
import android.view.View
import android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.pixeldroid.app.R
import org.pixeldroid.app.databinding.ActivityPostBinding
import org.pixeldroid.app.posts.feeds.uncachedFeeds.comments.CommentFragment
@ -43,7 +45,10 @@ class PostActivity : BaseActivity() {
val holder = StatusViewHolder(binding.postFragmentSingle)
holder.bind(status, apiHolder, db, lifecycleScope, displayDimensionsInPx(), isActivity = true)
holder.bind(
status, apiHolder, db, lifecycleScope, displayDimensionsInPx(),
requestPermissionDownloadPic, isActivity = true
)
activateCommenter()
initCommentsFragment(domain = user?.instance_uri.orEmpty())
@ -60,6 +65,17 @@ class PostActivity : BaseActivity() {
}
}
private val requestPermissionDownloadPic =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (!isGranted) {
MaterialAlertDialogBuilder(this)
.setMessage(R.string.write_permission_download_pic)
.setNegativeButton(android.R.string.ok) { _, _ -> }
.show()
}
}
private fun activateCommenter() {
//Activate commenter
binding.submitComment.setOnClickListener {

View File

@ -4,10 +4,13 @@ import android.annotation.SuppressLint
import android.app.Activity
import android.content.ClipData
import android.content.Intent
import android.content.pm.PackageManager.PERMISSION_DENIED
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.graphics.Typeface
import android.graphics.drawable.AnimatedVectorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.Looper
import android.text.method.LinkMovementMethod
import android.util.Log
@ -16,6 +19,7 @@ import android.view.Menu
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
@ -70,7 +74,11 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
private var status: Status? = null
fun bind(status: Status?, pixelfedAPI: PixelfedAPIHolder, db: AppDatabase, lifecycleScope: LifecycleCoroutineScope, displayDimensionsInPx: Pair<Int, Int>, isActivity: Boolean = false) {
fun bind(
status: Status?, pixelfedAPI: PixelfedAPIHolder, db: AppDatabase,
lifecycleScope: LifecycleCoroutineScope, displayDimensionsInPx: Pair<Int, Int>,
requestPermissionDownloadPic: ActivityResultLauncher<String>, isActivity: Boolean = false
) {
this.itemView.visibility = View.VISIBLE
this.status = status
@ -99,7 +107,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
setupPost(picRequest, user.instance_uri, isActivity)
activateButtons(pixelfedAPI, db, lifecycleScope, isActivity)
activateButtons(pixelfedAPI, db, lifecycleScope, isActivity, requestPermissionDownloadPic)
}
@ -227,6 +235,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
db: AppDatabase,
lifecycleScope: LifecycleCoroutineScope,
isActivity: Boolean,
requestPermissionDownloadPic: ActivityResultLauncher<String>,
){
//Set the special HTML text
setDescription(apiHolder, lifecycleScope)
@ -256,7 +265,7 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
showComments(lifecycleScope, isActivity)
activateMoreButton(apiHolder, db, lifecycleScope)
activateMoreButton(apiHolder, db, lifecycleScope, requestPermissionDownloadPic)
}
private fun activateReblogger(
@ -358,7 +367,12 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
return null
}
private fun activateMoreButton(apiHolder: PixelfedAPIHolder, db: AppDatabase, lifecycleScope: LifecycleCoroutineScope){
private fun activateMoreButton(
apiHolder: PixelfedAPIHolder,
db: AppDatabase,
lifecycleScope: LifecycleCoroutineScope,
requestPermissionDownloadPic: ActivityResultLauncher<String>
){
var bookmarked: Boolean? = null
binding.statusMore.setOnClickListener {
PopupMenu(it.context, it).apply {
@ -396,12 +410,18 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold
true
}
R.id.post_more_menu_save_to_gallery -> {
// Check permissions on old Android versions: on new versions it is not
// needed when storing a file.
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.Q && ContextCompat.checkSelfPermission(binding.root.context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PERMISSION_DENIED) {
requestPermissionDownloadPic.launch(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
} else {
status?.downloadImage(
binding.root.context,
status?.media_attachments?.getOrNull(binding.postPager.currentItem)?.url
?: "",
binding.root
)
}
true
}

View File

@ -74,6 +74,7 @@ class PostFeedFragment<T: FeedContentDatabase>: CachedFeedFragment<T>() {
return view
}
inner class PostsAdapter(private val displayDimensionsInPx: Pair<Int, Int>) : PagingDataAdapter<T, RecyclerView.ViewHolder>(
object : DiffUtil.ItemCallback<T>() {
override fun areItemsTheSame (oldItem: T, newItem: T): Boolean = oldItem.id == newItem.id
@ -94,7 +95,9 @@ class PostFeedFragment<T: FeedContentDatabase>: CachedFeedFragment<T>() {
)
val uiModel = getItem(position) as Status?
uiModel?.let {
(holder as StatusViewHolder).bind(it, apiHolder, db, lifecycleScope, displayDimensionsInPx)
(holder as StatusViewHolder).bind(
it, apiHolder, db, lifecycleScope, displayDimensionsInPx, requestPermissionDownloadPic
)
}
}
}

View File

@ -85,7 +85,9 @@ class UncachedPostsFragment : UncachedFeedFragment<Status>() {
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
getItem(position)?.let {
(holder as StatusViewHolder).bind(it, apiHolder, db, lifecycleScope, displayDimensionsInPx)
(holder as StatusViewHolder).bind(
it, apiHolder, db, lifecycleScope, displayDimensionsInPx, requestPermissionDownloadPic
)
}
}
}

View File

@ -178,8 +178,10 @@ class ProfileFeedFragment : UncachedFeedFragment<FeedContent>() {
deleteFromCollection
)
} else {
(holder as StatusViewHolder).bind(it as Status, apiHolder, db,
lifecycleScope, requireContext().displayDimensionsInPx())
(holder as StatusViewHolder).bind(
it as Status, apiHolder, db, lifecycleScope,
requireContext().displayDimensionsInPx(), requestPermissionDownloadPic
)
}
}

View File

@ -1,7 +1,10 @@
package org.pixeldroid.app.utils
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.pixeldroid.app.R
import org.pixeldroid.app.utils.db.AppDatabase
import org.pixeldroid.app.utils.di.PixelfedAPIHolder
import javax.inject.Inject
@ -22,4 +25,18 @@ open class BaseFragment: Fragment() {
(requireActivity().application as PixelDroidApplication).getAppComponent().inject(this)
}
internal val requestPermissionDownloadPic =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (!isGranted) {
context?.let {
MaterialAlertDialogBuilder(it)
.setMessage(R.string.write_permission_download_pic)
.setNegativeButton(android.R.string.ok) { _, _ -> }
.show()
}
}
}
}

View File

@ -1,8 +1,10 @@
package org.pixeldroid.app.utils.api.objects
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.app.DownloadManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.database.Cursor
import android.net.Uri
import android.os.Environment
@ -11,6 +13,7 @@ import androidx.core.net.toUri
import com.google.android.material.snackbar.Snackbar
import org.pixeldroid.app.R
import org.pixeldroid.app.posts.getDomain
import org.pixeldroid.app.utils.getMimeType
import java.io.File
import java.io.Serializable
import java.time.Instant
@ -148,11 +151,13 @@ open class Status(
)
val file = path.toUri()
val shareIntent: Intent = Intent.createChooser(Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, file)
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
type = "image/$ext"
type = file.getMimeType(context.contentResolver)
}, null)
context.startActivity(shareIntent)

View File

@ -52,7 +52,6 @@
<string name="comment_posted">التعليق: تم نشر%1$s!</string>
<string name="comment_error">خطأ في التعليق!</string>
<string name="share_image">مشاركة الصورة</string>
<string name="write_permission_share_pic">يجب عليك منح تصريح للكتابة قصد مشاركة الصور!</string>
<string name="write_permission_download_pic">تحتاج إلى منح إذن الكتابة لتنزيل الصور!</string>
<string name="empty_comment">لا يجب ان يكون التعليق فارغًا!</string>
<string name="posted_on">نُشِر في %1$s</string>

View File

@ -60,7 +60,6 @@
<string name="comment_posted">Comentari: %1$s publicat!</string>
<string name="comment_error">Error de comentari!</string>
<string name="share_image">Compartir imatge</string>
<string name="write_permission_share_pic">Necessites concedir permís descriptura per compartir imatges!</string>
<string name="write_permission_download_pic">Has de concedir permís descriptura per baixar imatges!</string>
<string name="empty_comment">El comentari no ha de estar buit!</string>
<string name="posted_on">Publica\'t el %1$s</string>

View File

@ -69,7 +69,6 @@
<string name="posted_on">Zveřejněno na %1$s</string>
<string name="NoCommentsToShow">U tohoto příspěvku nejsou žádné komentáře…</string>
<string name="empty_comment">Komentář nesmí být prázdný!</string>
<string name="write_permission_share_pic">Pro sdílení obrázků musíte udělit práva k zápisu!</string>
<string name="share_image">Sdílet obrázek</string>
<string name="comment_error">Chyba komentáře!</string>
<string name="comment_posted">Komentář: %1$s zveřejněn!</string>

View File

@ -79,7 +79,6 @@
<string name="feed_failed">Feed konnte nicht geladen werden</string>
<string name="posted_on">Erstellt am %1$s</string>
<string name="write_permission_download_pic">Für das Herunterladen von Bildern müssen Sie eine Schreibgenehmigung erteilen!</string>
<string name="write_permission_share_pic">Für die Teilen von Bildern müssen Sie eine Schreibgenehmigung erteilen!</string>
<string name="follow_status_failed">Konnte den Folgestatus nicht erhalten</string>
<string name="retry">Wiederholen</string>
<string name="nothing_to_see_here">Hier gibt\'s nichts zu sehen!</string>

View File

@ -64,7 +64,6 @@
<string name="comment_posted">¡Comentario: %1$s publicado!</string>
<string name="comment_error">¡Error al comentar!</string>
<string name="share_image">Compartir imagen</string>
<string name="write_permission_share_pic">¡Tienes que dar permiso de escritura para compartir fotos!</string>
<string name="write_permission_download_pic">¡Tienes que dar permiso de escritura para descargar fotos!</string>
<string name="empty_comment">¡Los comentarios no deben estar vacíos!</string>
<string name="posted_on">Publicado en %1$s</string>

View File

@ -76,7 +76,6 @@
<string name="follow_button_failed">Ezin izan da jarraipen-botoia erakutsi</string>
<string name="follow_status_failed">Ezin izan da segimendu-egoera lortu</string>
<string name="comment">Iruzkin</string>
<string name="write_permission_share_pic">Idazteko baimena eman behar duzu argazkiak partekatzeko!</string>
<string name="posted_on">%1$s(e)n argitaratua</string>
<string name="hashtags">TRAOLAK</string>
<string name="accounts">KONTUAK</string>

View File

@ -56,7 +56,6 @@
<string name="posted_on">منتشر شده در %1$s</string>
<string name="empty_comment">نظر نمی‌تواند خالی باشد!</string>
<string name="write_permission_download_pic">برای بارگیری تصاویر بایستی مجوز نوشتن را بدهید!</string>
<string name="write_permission_share_pic">برای هم‌رسانی تصاویر باید مجوز نوشتن را بدهید!</string>
<string name="share_image">هم‌رسانی تصویر</string>
<string name="comment_error">خطا در درج نظر!</string>
<string name="comment_posted">نظر: %1$s منتشر کرد!</string>

View File

@ -49,8 +49,7 @@
<string name="upload_post_success">Téléversement de la publication avec succès</string>
<string name="posted_on">Publié le %1$s</string>
<string name="empty_comment">Le commentaire ne doit pas être vide !</string>
<string name="write_permission_share_pic">Vous devez accorder une autorisation d\'écriture pour partager des photos !</string>
<string name="default_nfollowers">-
<string name="default_nfollowers">-
\nAbonné·e·s</string>
<string name="upload_post_error">Le téléversement de la publication a échoué</string>
<string name="loading_toast">Une erreur s\'est produite lors du chargement</string>

View File

@ -61,7 +61,6 @@
<string name="comment_posted">Comentario: %1$s publicado!</string>
<string name="comment_error">Fallo ao comentar!</string>
<string name="share_image">Compartir Imaxe</string>
<string name="write_permission_share_pic">Tes que dar permiso de escritura para compartir fotos!</string>
<string name="write_permission_download_pic">Tes que conceder permiso de escritura para descargar fotos!</string>
<string name="empty_comment">O comentario non debe quedar baleiro!</string>
<string name="posted_on">Publicado o %1$s</string>

View File

@ -106,7 +106,6 @@
<string name="comment_posted">Hozzászólás: %1$s közzétéve!</string>
<string name="comment_error">Hozzászólási hiba!</string>
<string name="share_image">Kép megosztása</string>
<string name="write_permission_share_pic">Írási engedélyt kell adnia a képek megosztásához!</string>
<string name="write_permission_download_pic">Írási engedélyt kell adnia a képek letöltéséhez!</string>
<string name="empty_comment">A hozzászólás nem lehet üres!</string>
<string name="NoCommentsToShow">Nincsenek hozzászólások a bejegyzésnél…</string>

View File

@ -53,7 +53,6 @@
<string name="no_description">Nessuna descrizione</string>
<string name="posted_on">Postato su %1$s</string>
<string name="empty_comment">Il commento non deve essere vuoto!</string>
<string name="write_permission_share_pic">È necessario concedere i permessi di scrittura per condividere le immagini!</string>
<string name="share_image">Condividi immagine</string>
<string name="comment_error">Errore nel commento!</string>
<string name="comment_posted">Commento: %1$s postato!</string>

View File

@ -39,7 +39,6 @@
<string name="instance_error">インスタンス情報が取得できませんでした</string>
<string name="posted_on">%1$s の投稿</string>
<string name="accounts">アカウント</string>
<string name="write_permission_share_pic">画像を共有するためには書き込みの権限を更新する必要があります</string>
<string name="write_permission_download_pic">画像をダウンロードするには書き込みの権限を更新する必要があります</string>
<string name="empty_comment">コメントは空欄にできません</string>
<string name="comment_posted">コメント: %1$s が投稿されました</string>

View File

@ -100,7 +100,6 @@
<string name="api_not_enabled_dialog">എപിഐ ഈ ഇൻസ്റ്റൻസിൽ സജീവമല്ലാ.ഇത് സജീവമാക്കാൻ നിങ്ങളുടെ അഡ്മിനിസ്ട്രേറ്ററെ ബന്ധപ്പെടുക.</string>
<string name="feed_failed">ഫീഡ് ലഭ്യമാക്കാൻ സാധിച്ചില്ല</string>
<string name="write_permission_download_pic">ചിത്രങ്ങൾ ഡൗൺലോഡ് ചെയ്യാൻ നിങ്ങൾ എഴുത്ത് അനുമതി നൽകണം!</string>
<string name="write_permission_share_pic">ചിത്രങ്ങൾ പങ്കിടാൻ നിങ്ങൾ എഴുത്ത് അനുമതി നൽകണം!</string>
<plurals name="number_comments">
<item quantity="one">%d</item>
<item quantity="other"></item>

View File

@ -70,7 +70,6 @@
<string name="comment_posted">Commentaar: %1$s gepost!</string>
<string name="comment_error">Fout met commentaar!</string>
<string name="share_image">Afbeelding delen</string>
<string name="write_permission_share_pic">Je moet schrijven toestaan om afbeeldingen te delen!</string>
<string name="write_permission_download_pic">Je moet schrijven toestaan om afbeeldingen te downloaden!</string>
<string name="follow_status_failed">Kon volgstatus niet ophalen</string>
<string name="follow_error">Kon niet volgen</string>

View File

@ -40,7 +40,6 @@
<string name="no_description">Bez opisu</string>
<string name="posted_on">Opublikowano %1$s</string>
<string name="empty_comment">Komentarz nie może być pusty!</string>
<string name="write_permission_share_pic">Musisz nadać uprawnienia do zapisu, aby móc udostępniać obrazki!</string>
<string name="share_image">Udostępnij</string>
<string name="comment_error">Błąd komentarza!</string>
<string name="comment_posted">Komentarz: %1$s opublikowany!</string>

View File

@ -146,7 +146,6 @@
<string name="switch_to_grid">Mudar para visualização em grade</string>
<string name="comment_posted">Comentário: %1$s publicado!</string>
<string name="comment_error">Erro no comentário!</string>
<string name="write_permission_share_pic">Você precisa conceder permissão de gravação para compartilhar imagens!</string>
<string name="language">Idioma</string>
<string name="delete_dialog">Excluir esta publicação\?</string>
<string name="something_went_wrong">Algo deu errado…</string>

View File

@ -84,7 +84,6 @@
<string name="share_image">Partilhar imagem</string>
<string name="comment_error">Erro ao comentar!</string>
<string name="write_permission_download_pic">Precisa adicionar a permissão para transferir imagens!</string>
<string name="write_permission_share_pic">Precisa adicionar permissão para partilhar imagens!</string>
<string name="comment_posted">Comentário: %1$s publicou!</string>
<string name="comment">Comentário</string>
<string name="add_comment">Adicionar um comentário</string>

View File

@ -42,7 +42,6 @@
<string name="comment_error">Ошибка комментирования!</string>
<string name="share_image">Поделиться изображением</string>
<string name="write_permission_download_pic">Вам необходимо предоставить разрешение на запись чтобы скачивать фотографии!</string>
<string name="write_permission_share_pic">Вы должны дать разрешение на запись чтобы делиться фотографиями!</string>
<string name="empty_comment">Комментарий не должен быть пустым!</string>
<string name="posted_on">Опубликовано в %1$s</string>
<string name="no_description">Описание отсутствует</string>

View File

@ -64,7 +64,6 @@
<string name="comment_posted">Kommentar: %1$s inlagd!</string>
<string name="comment_error">Kommentarsfel!</string>
<string name="share_image">Dela bild</string>
<string name="write_permission_share_pic">Du måste tillåta skrivrättigheter för att dela bilder!</string>
<string name="write_permission_download_pic">Du måste tillåta skrivrättigheter för att ladda ned bilder!</string>
<string name="empty_comment">Kommentaren får inte vara tom!</string>
<string name="posted_on">Inlagt på %1$s</string>

View File

@ -165,7 +165,6 @@
<string name="share_image">Поширити зображення</string>
<string name="liked_notification">%1$s хвалить ваш допис</string>
<string name="file_not_found">Файлу %1$s не знайдено</string>
<string name="write_permission_share_pic">Для поширення зображень потрібен дозвіл на запис!</string>
<string name="picture_format_error">Помилка вивантаження: хибний формат зображення.</string>
<string name="unfollow">Відписатись</string>
<string name="media_upload_failed">Не вдалося вивантажити медіа, повторіть спробу чи перевірте мережне з\'єднання</string>

View File

@ -64,7 +64,6 @@
<string name="comment_posted">评论: %1$s 已发布!</string>
<string name="comment_error">评论错误!</string>
<string name="share_image">分享图像</string>
<string name="write_permission_share_pic">您需要允许读写权限才能共享图片!</string>
<string name="write_permission_download_pic">您需要允许读写权限才能下载图片!</string>
<string name="empty_comment">评论不能为空!</string>
<string name="posted_on">发表于 %1$s</string>

View File

@ -150,7 +150,6 @@ For more info about Pixelfed, you can check here: https://pixelfed.org"</string>
<string name="NoCommentsToShow">No comments on this post…</string>
<string name="empty_comment">Comment must not be empty!</string>
<string name="write_permission_download_pic">You need to grant write permission to download pictures!</string>
<string name="write_permission_share_pic">You need to grant write permission to share pictures!</string>
<string name="share_image">Share Image</string>
<string name="comment_error">Comment error!</string>
<string name="comment_posted">"Comment: %1$s posted!"</string>