From 557d535e5a38323b4479d072878a68d79ae18ea6 Mon Sep 17 00:00:00 2001 From: Tyler Saunders Date: Tue, 3 Jan 2023 17:21:22 +0000 Subject: [PATCH 1/2] Update ComposeFragment to use the photopicker. The android platform has a great photopicker, and this change checks for the current device's sdk version, and uses the photopicker if it's available on the device. For pre Android R sdkrev2 devices, the experience remains unchanged. --- .../android/fragments/ComposeFragment.java | 53 ++++++++++++++++--- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java index 232c20487..c13bf37ec 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java @@ -1,5 +1,7 @@ package org.joinmastodon.android.fragments; +import static android.os.ext.SdkExtensions.getExtensionVersion; + import android.animation.ObjectAnimator; import android.annotation.SuppressLint; import android.app.Activity; @@ -20,6 +22,7 @@ import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; +import android.provider.MediaStore; import android.provider.OpenableColumns; import android.text.Editable; import android.text.InputFilter; @@ -782,14 +785,50 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr .show(); } + /** + * Check to see if Android platform photopicker is available on the device\ + * @return whether the device supports photopicker intents. + */ + private boolean isPhotoPickerAvailable() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + return true; + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + return getExtensionVersion(Build.VERSION_CODES.R) >= 2; + } else + return false; + } + + /** + * Builds the correct intent for the device version to select media. + * + *

For Device version > T or R_SDK_v2, use the android platform photopicker via + * {@link MediaStore#ACTION_PICK_IMAGES} + * + *

For earlier versions use the built in docs ui via {@link Intent#ACTION_GET_CONTENT} + */ private void openFilePicker(){ - Intent intent=new Intent(Intent.ACTION_GET_CONTENT); - intent.addCategory(Intent.CATEGORY_OPENABLE); - intent.setType("*/*"); - if(instance.configuration!=null && instance.configuration.mediaAttachments!=null && instance.configuration.mediaAttachments.supportedMimeTypes!=null && !instance.configuration.mediaAttachments.supportedMimeTypes.isEmpty()){ - intent.putExtra(Intent.EXTRA_MIME_TYPES, instance.configuration.mediaAttachments.supportedMimeTypes.toArray(new String[0])); - }else{ - intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"}); + Intent intent; + boolean usePhotoPicker = isPhotoPickerAvailable(); + if (usePhotoPicker) { + intent = new Intent(MediaStore.ACTION_PICK_IMAGES); + intent.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, MediaStore.getPickImagesMaxLimit()); + } else { + intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.addCategory(Intent.CATEGORY_OPENABLE); + intent.setType("*/*"); + } + if (!usePhotoPicker && instance.configuration != null && + instance.configuration.mediaAttachments != null && + instance.configuration.mediaAttachments.supportedMimeTypes != null && + !instance.configuration.mediaAttachments.supportedMimeTypes.isEmpty()) { + intent.putExtra(Intent.EXTRA_MIME_TYPES, + instance.configuration.mediaAttachments.supportedMimeTypes.toArray( + new String[0])); + } else { + if (!usePhotoPicker) { + // If photo picker is being used these are the default mimetypes. + intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*"}); + } } intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); startActivityForResult(intent, MEDIA_RESULT); From d667b8fa984fa8783cbd4f6fe87ee142d344d76e Mon Sep 17 00:00:00 2001 From: Grishka Date: Wed, 11 Jan 2023 13:06:40 +0300 Subject: [PATCH 2/2] Fix #498 --- .../android/fragments/ComposeFragment.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java index c13bf37ec..1efee3a37 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java @@ -413,6 +413,8 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr mainEditText.setSelectionListener(this); mainEditText.addTextChangedListener(new TextWatcher(){ + private int lastChangeStart, lastChangeCount; + @Override public void beforeTextChanged(CharSequence s, int start, int count, int after){ @@ -422,6 +424,14 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr public void onTextChanged(CharSequence s, int start, int before, int count){ if(s.length()==0) return; + lastChangeStart=start; + lastChangeCount=count; + } + + @Override + public void afterTextChanged(Editable s){ + int start=lastChangeStart; + int count=lastChangeCount; // offset one char back to catch an already typed '@' or '#' or ':' int realStart=start; start=Math.max(0, start-1); @@ -467,10 +477,7 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr editable.removeSpan(span); } } - } - @Override - public void afterTextChanged(Editable s){ updateCharCounter(); } });