Use labels from pasted images as alt text

This commit is contained in:
Grishka 2022-04-08 05:23:59 +03:00
parent d063eb32d2
commit 684b025ccf
3 changed files with 22 additions and 14 deletions

View File

@ -3,6 +3,7 @@ package org.joinmastodon.android.api.requests.statuses;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.text.TextUtils;
import org.joinmastodon.android.MastodonApp; import org.joinmastodon.android.MastodonApp;
import org.joinmastodon.android.api.ContentUriRequestBody; import org.joinmastodon.android.api.ContentUriRequestBody;
@ -21,15 +22,17 @@ public class UploadAttachment extends MastodonAPIRequest<Attachment>{
private Uri uri; private Uri uri;
private ProgressListener progressListener; private ProgressListener progressListener;
private int maxImageSize; private int maxImageSize;
private String description;
public UploadAttachment(Uri uri){ public UploadAttachment(Uri uri){
super(HttpMethod.POST, "/media", Attachment.class); super(HttpMethod.POST, "/media", Attachment.class);
this.uri=uri; this.uri=uri;
} }
public UploadAttachment(Uri uri, int maxImageSize){ public UploadAttachment(Uri uri, int maxImageSize, String description){
this(uri); this(uri);
this.maxImageSize=maxImageSize; this.maxImageSize=maxImageSize;
this.description=description;
} }
public UploadAttachment setProgressListener(ProgressListener progressListener){ public UploadAttachment setProgressListener(ProgressListener progressListener){
@ -39,9 +42,11 @@ public class UploadAttachment extends MastodonAPIRequest<Attachment>{
@Override @Override
public RequestBody getRequestBody() throws IOException{ public RequestBody getRequestBody() throws IOException{
return new MultipartBody.Builder() MultipartBody.Builder builder=new MultipartBody.Builder()
.setType(MultipartBody.FORM) .setType(MultipartBody.FORM)
.addFormDataPart("file", UiUtils.getFileName(uri), maxImageSize>0 ? new ResizedImageRequestBody(uri, maxImageSize, progressListener) : new ContentUriRequestBody(uri, progressListener)) .addFormDataPart("file", UiUtils.getFileName(uri), maxImageSize>0 ? new ResizedImageRequestBody(uri, maxImageSize, progressListener) : new ContentUriRequestBody(uri, progressListener));
.build(); if(!TextUtils.isEmpty(description))
builder.addFormDataPart("description", description);
return builder.build();
} }
} }

View File

@ -463,7 +463,7 @@ public class ComposeFragment extends ToolbarFragment implements OnBackPressedLis
ArrayList<Uri> mediaUris=getArguments().getParcelableArrayList("mediaAttachments"); ArrayList<Uri> mediaUris=getArguments().getParcelableArrayList("mediaAttachments");
if(mediaUris!=null && !mediaUris.isEmpty()){ if(mediaUris!=null && !mediaUris.isEmpty()){
for(Uri uri:mediaUris){ for(Uri uri:mediaUris){
addMediaAttachment(uri); addMediaAttachment(uri, null);
} }
} }
} }
@ -689,17 +689,17 @@ public class ComposeFragment extends ToolbarFragment implements OnBackPressedLis
if(requestCode==MEDIA_RESULT && resultCode==Activity.RESULT_OK){ if(requestCode==MEDIA_RESULT && resultCode==Activity.RESULT_OK){
Uri single=data.getData(); Uri single=data.getData();
if(single!=null){ if(single!=null){
addMediaAttachment(single); addMediaAttachment(single, null);
}else{ }else{
ClipData clipData=data.getClipData(); ClipData clipData=data.getClipData();
for(int i=0;i<clipData.getItemCount();i++){ for(int i=0;i<clipData.getItemCount();i++){
addMediaAttachment(clipData.getItemAt(i).getUri()); addMediaAttachment(clipData.getItemAt(i).getUri(), null);
} }
} }
} }
} }
private boolean addMediaAttachment(Uri uri){ private boolean addMediaAttachment(Uri uri, String description){
if(getMediaAttachmentsCount()==MAX_ATTACHMENTS){ if(getMediaAttachmentsCount()==MAX_ATTACHMENTS){
showMediaAttachmentError(getResources().getQuantityString(R.plurals.cant_add_more_than_x_attachments, MAX_ATTACHMENTS, MAX_ATTACHMENTS)); showMediaAttachmentError(getResources().getQuantityString(R.plurals.cant_add_more_than_x_attachments, MAX_ATTACHMENTS, MAX_ATTACHMENTS));
return false; return false;
@ -729,6 +729,7 @@ public class ComposeFragment extends ToolbarFragment implements OnBackPressedLis
pollBtn.setEnabled(false); pollBtn.setEnabled(false);
DraftMediaAttachment draft=new DraftMediaAttachment(); DraftMediaAttachment draft=new DraftMediaAttachment();
draft.uri=uri; draft.uri=uri;
draft.description=description;
attachmentsView.addView(createMediaAttachmentView(draft)); attachmentsView.addView(createMediaAttachmentView(draft));
allAttachments.add(draft); allAttachments.add(draft);
@ -810,7 +811,7 @@ public class ComposeFragment extends ToolbarFragment implements OnBackPressedLis
if(contentType!=null && contentType.startsWith("image/")){ if(contentType!=null && contentType.startsWith("image/")){
maxSize=2_073_600; // TODO get this from instance configuration when it gets added there maxSize=2_073_600; // TODO get this from instance configuration when it gets added there
} }
attachment.uploadRequest=(UploadAttachment) new UploadAttachment(attachment.uri, maxSize) attachment.uploadRequest=(UploadAttachment) new UploadAttachment(attachment.uri, maxSize, attachment.description)
.setProgressListener(new ProgressListener(){ .setProgressListener(new ProgressListener(){
@Override @Override
public void onProgress(long transferred, long total){ public void onProgress(long transferred, long total){
@ -1079,8 +1080,8 @@ public class ComposeFragment extends ToolbarFragment implements OnBackPressedLis
} }
@Override @Override
public boolean onAddMediaAttachmentFromEditText(Uri uri){ public boolean onAddMediaAttachmentFromEditText(Uri uri, String description){
return addMediaAttachment(uri); return addMediaAttachment(uri, description);
} }
private void startAutocomplete(ComposeAutocompleteSpan span){ private void startAutocomplete(ComposeAutocompleteSpan span){

View File

@ -16,6 +16,8 @@ import android.view.inputmethod.InputConnectionWrapper;
import android.view.inputmethod.InputContentInfo; import android.view.inputmethod.InputContentInfo;
import android.widget.EditText; import android.widget.EditText;
import java.util.Objects;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
public class ComposeEditText extends EditText{ public class ComposeEditText extends EditText{
@ -90,7 +92,7 @@ public class ComposeEditText extends EditText{
Uri uri=clip.getItemAt(i).getUri(); Uri uri=clip.getItemAt(i).getUri();
if(uri!=null){ if(uri!=null){
processedAny=true; processedAny=true;
selectionListener.onAddMediaAttachmentFromEditText(uri); selectionListener.onAddMediaAttachmentFromEditText(uri, Objects.toString(clip.getItemAt(i).getText(), null));
} }
} }
return processedAny; return processedAny;
@ -99,7 +101,7 @@ public class ComposeEditText extends EditText{
public interface SelectionListener{ public interface SelectionListener{
void onSelectionChanged(int start, int end); void onSelectionChanged(int start, int end);
String[] onGetAllowedMediaMimeTypes(); String[] onGetAllowedMediaMimeTypes();
boolean onAddMediaAttachmentFromEditText(Uri uri); boolean onAddMediaAttachmentFromEditText(Uri uri, String description);
} }
private class MediaAcceptingInputConnection extends InputConnectionWrapper{ private class MediaAcceptingInputConnection extends InputConnectionWrapper{
@ -114,7 +116,7 @@ public class ComposeEditText extends EditText{
if(contentUri==null) if(contentUri==null)
return false; return false;
inputContentInfo.requestPermission(); inputContentInfo.requestPermission();
return selectionListener.onAddMediaAttachmentFromEditText(contentUri); return selectionListener.onAddMediaAttachmentFromEditText(contentUri, Objects.toString(inputContentInfo.getDescription().getLabel(), null));
} }
} }
} }