From aba36ca6f8263452699502c2c95936bf03cf6621 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Mon, 24 Feb 2020 22:03:00 +0100 Subject: [PATCH] Compose activity fixes (#1707) * cleanup media keyboard code * fix crash in ComposeActivity when rotating phone while taking photo * fix poll button in ComposeActivity being invisible when disabled --- .../components/compose/ComposeActivity.kt | 58 +++++++------------ .../main/res/values-night/theme_colors.xml | 2 +- 2 files changed, 22 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt index 339bb3c5..a798f4ff 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt @@ -103,8 +103,6 @@ class ComposeActivity : BaseActivity(), // this only exists when a status is trying to be sent, but uploads are still occurring private var finishingUploadDialog: ProgressDialog? = null - private var currentInputContentInfo: InputContentInfoCompat? = null - private var currentFlags: Int = 0 private var photoUploadUri: Uri? = null @VisibleForTesting var maximumTootCharacters = DEFAULT_CHARACTER_LIMIT @@ -147,6 +145,8 @@ class ComposeActivity : BaseActivity(), subscribeToUpdates(mediaAdapter) setupButtons() + photoUploadUri = savedInstanceState?.getParcelable(PHOTO_UPLOAD_URI_KEY) + /* If the composer is started up as a reply to another post, override the "starting" state * based on what the intent from the reply request passes. */ if (intent != null) { @@ -473,14 +473,7 @@ class ComposeActivity : BaseActivity(), } override fun onSaveInstanceState(outState: Bundle) { - if (currentInputContentInfo != null) { - outState.putParcelable("commitContentInputContentInfo", - currentInputContentInfo!!.unwrap() as Parcelable?) - outState.putInt("commitContentFlags", currentFlags) - } - currentInputContentInfo = null - currentFlags = 0 - outState.putParcelable("photoUploadUri", photoUploadUri) + outState.putParcelable(PHOTO_UPLOAD_URI_KEY, photoUploadUri) super.onSaveInstanceState(outState) } @@ -708,38 +701,25 @@ class ComposeActivity : BaseActivity(), } /** This is for the fancy keyboards which can insert images and stuff. */ - override fun onCommitContent(inputContentInfo: InputContentInfoCompat, flags: Int, opts: Bundle): Boolean { - try { - currentInputContentInfo?.releasePermission() - } catch (e: Exception) { - Log.e(TAG, "InputContentInfoCompat#releasePermission() failed." + e.message) - } finally { - currentInputContentInfo = null - } - + override fun onCommitContent(inputContentInfo: InputContentInfoCompat, flags: Int, opts: Bundle?): Boolean { // Verify the returned content's type is of the correct MIME type val supported = inputContentInfo.description.hasMimeType("image/*") - return supported && onCommitContentInternal(inputContentInfo, flags) - } - - private fun onCommitContentInternal(inputContentInfo: InputContentInfoCompat, flags: Int): Boolean { - if (flags and InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION != 0) { - try { - inputContentInfo.requestPermission() - } catch (e: Exception) { - Log.e(TAG, "InputContentInfoCompat#requestPermission() failed." + e.message) - return false + if(supported) { + val lacksPermission = (flags and InputConnectionCompat.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0 + if(lacksPermission) { + try { + inputContentInfo.requestPermission() + } catch (e: Exception) { + Log.e(TAG, "InputContentInfoCompat#requestPermission() failed." + e.message) + return false + } } + pickMedia(inputContentInfo.contentUri, inputContentInfo) + return true } - // Determine the file size before putting handing it off to be put in the queue. - pickMedia(inputContentInfo.contentUri) - - currentInputContentInfo = inputContentInfo - currentFlags = flags - - return true + return false } private fun sendStatus() { @@ -849,9 +829,12 @@ class ComposeActivity : BaseActivity(), } } - private fun pickMedia(uri: Uri) { + private fun pickMedia(uri: Uri, contentInfoCompat: InputContentInfoCompat? = null) { withLifecycleContext { viewModel.pickMedia(uri).observe { exceptionOrItem -> + + contentInfoCompat?.releasePermission() + exceptionOrItem.asLeftOrNull()?.let { val errorId = when (it) { is VideoSizeException -> { @@ -1026,6 +1009,7 @@ class ComposeActivity : BaseActivity(), private const val PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1 private const val COMPOSE_OPTIONS_EXTRA = "COMPOSE_OPTIONS" + private const val PHOTO_UPLOAD_URI_KEY = "PHOTO_UPLOAD_URI" // Mastodon only counts URLs as this long in terms of status character limits @VisibleForTesting diff --git a/app/src/main/res/values-night/theme_colors.xml b/app/src/main/res/values-night/theme_colors.xml index 11ad69c3..f30787f6 100644 --- a/app/src/main/res/values-night/theme_colors.xml +++ b/app/src/main/res/values-night/theme_colors.xml @@ -10,7 +10,7 @@ @color/white @color/tusky_grey_90 @color/tusky_grey_70 - @color/tusky_grey_30 + @color/tusky_grey_40 @color/tusky_grey_70