Fixes the lack of permission when using Android's PhotoPicker to upload videos (thumbnail fails to load and videos don't play because the Playback service doesn't have permission to run)

This commit is contained in:
Vitor Pamplona
2023-09-08 11:39:14 -04:00
parent 854469cc7d
commit 939eb1bd8d
3 changed files with 21 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
package com.vitorpamplona.amethyst.ui package com.vitorpamplona.amethyst.ui
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.ConnectivityManager import android.net.ConnectivityManager
@@ -65,7 +66,10 @@ class MainActivity : AppCompatActivity() {
themeViewModel.onChange(LocalPreferences.getTheme()) themeViewModel.onChange(LocalPreferences.getTheme())
AmethystTheme(themeViewModel) { AmethystTheme(themeViewModel) {
// A surface container using the 'background' color from the theme // A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) { Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
val accountStateViewModel: AccountStateViewModel = viewModel { val accountStateViewModel: AccountStateViewModel = viewModel {
AccountStateViewModel(this@MainActivity) AccountStateViewModel(this@MainActivity)
} }
@@ -81,7 +85,8 @@ class MainActivity : AppCompatActivity() {
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.build() .build()
val connectivityManager = getSystemService(ConnectivityManager::class.java) as ConnectivityManager val connectivityManager =
getSystemService(ConnectivityManager::class.java) as ConnectivityManager
connectivityManager.requestNetwork(networkRequest, networkCallback) connectivityManager.requestNetwork(networkRequest, networkCallback)
} }
@@ -153,7 +158,8 @@ class MainActivity : AppCompatActivity() {
super.onCapabilitiesChanged(network, networkCapabilities) super.onCapabilitiesChanged(network, networkCapabilities)
GlobalScope.launch(Dispatchers.IO) { GlobalScope.launch(Dispatchers.IO) {
val hasMobileData = networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) val hasMobileData =
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
val hasWifi = networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) val hasWifi = networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
Log.d("NETWORKCALLBACK", "onCapabilitiesChanged: hasMobileData $hasMobileData") Log.d("NETWORKCALLBACK", "onCapabilitiesChanged: hasMobileData $hasMobileData")
Log.d("NETWORKCALLBACK", "onCapabilitiesChanged: hasWifi $hasWifi") Log.d("NETWORKCALLBACK", "onCapabilitiesChanged: hasWifi $hasWifi")
@@ -178,9 +184,15 @@ class MainActivity : AppCompatActivity() {
class GetMediaActivityResultContract : ActivityResultContracts.GetContent() { class GetMediaActivityResultContract : ActivityResultContracts.GetContent() {
@SuppressLint("MissingSuperCall")
override fun createIntent(context: Context, input: String): Intent { override fun createIntent(context: Context, input: String): Intent {
return super.createIntent(context, input).apply {
// Force only images and videos to be selectable // Force only images and videos to be selectable
// Force OPEN Document because of the resulting URI must be passed to the
// Playback service and the picker's permissions only allow the activity to read the URI
return Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
// Force only images and videos to be selectable
type = "*/*"
putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "video/*")) putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "video/*"))
} }
} }

View File

@@ -3,6 +3,7 @@ package com.vitorpamplona.amethyst.ui.actions
import android.graphics.Bitmap import android.graphics.Bitmap
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.util.Log
import android.util.Size import android.util.Size
import android.widget.Toast import android.widget.Toast
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
@@ -215,7 +216,8 @@ fun ImageVideoPost(postViewModel: NewMediaModel, accountViewModel: AccountViewMo
try { try {
bitmap = resolver.loadThumbnail(it, Size(1200, 1000), null) bitmap = resolver.loadThumbnail(it, Size(1200, 1000), null)
} catch (e: Exception) { } catch (e: Exception) {
postViewModel.imageUploadingError.emit("Unable to load file") postViewModel.imageUploadingError.emit("Unable to load thumbnail, but the video can be uploaded")
Log.w("NewPostView", "Couldn't create thumbnail, but the video can be uploaded", e)
} }
} }
} }

View File

@@ -1370,8 +1370,8 @@ fun ImageVideoDescription(
try { try {
bitmap = resolver.loadThumbnail(uri, Size(1200, 1000), null) bitmap = resolver.loadThumbnail(uri, Size(1200, 1000), null)
} catch (e: Exception) { } catch (e: Exception) {
onError("Unable to load file") onError("Unable to load thumbnail")
Log.e("NewPostView", "Couldn't create thumbnail for $uri") Log.w("NewPostView", "Couldn't create thumbnail, but the video can be uploaded", e)
} }
} }
} }