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.net.Uri;
import android.provider.OpenableColumns;
import android.text.TextUtils;
import org.joinmastodon.android.MastodonApp;
import org.joinmastodon.android.api.ContentUriRequestBody;
@ -21,15 +22,17 @@ public class UploadAttachment extends MastodonAPIRequest<Attachment>{
private Uri uri;
private ProgressListener progressListener;
private int maxImageSize;
private String description;
public UploadAttachment(Uri uri){
super(HttpMethod.POST, "/media", Attachment.class);
this.uri=uri;
}
public UploadAttachment(Uri uri, int maxImageSize){
public UploadAttachment(Uri uri, int maxImageSize, String description){
this(uri);
this.maxImageSize=maxImageSize;
this.description=description;
}
public UploadAttachment setProgressListener(ProgressListener progressListener){
@ -39,9 +42,11 @@ public class UploadAttachment extends MastodonAPIRequest<Attachment>{
@Override
public RequestBody getRequestBody() throws IOException{
return new MultipartBody.Builder()
MultipartBody.Builder builder=new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", UiUtils.getFileName(uri), maxImageSize>0 ? new ResizedImageRequestBody(uri, maxImageSize, progressListener) : new ContentUriRequestBody(uri, progressListener))
.build();
.addFormDataPart("file", UiUtils.getFileName(uri), maxImageSize>0 ? new ResizedImageRequestBody(uri, maxImageSize, progressListener) : new ContentUriRequestBody(uri, progressListener));
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");
if(mediaUris!=null && !mediaUris.isEmpty()){
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){
Uri single=data.getData();
if(single!=null){
addMediaAttachment(single);
addMediaAttachment(single, null);
}else{
ClipData clipData=data.getClipData();
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){
showMediaAttachmentError(getResources().getQuantityString(R.plurals.cant_add_more_than_x_attachments, MAX_ATTACHMENTS, MAX_ATTACHMENTS));
return false;
@ -729,6 +729,7 @@ public class ComposeFragment extends ToolbarFragment implements OnBackPressedLis
pollBtn.setEnabled(false);
DraftMediaAttachment draft=new DraftMediaAttachment();
draft.uri=uri;
draft.description=description;
attachmentsView.addView(createMediaAttachmentView(draft));
allAttachments.add(draft);
@ -810,7 +811,7 @@ public class ComposeFragment extends ToolbarFragment implements OnBackPressedLis
if(contentType!=null && contentType.startsWith("image/")){
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(){
@Override
public void onProgress(long transferred, long total){
@ -1079,8 +1080,8 @@ public class ComposeFragment extends ToolbarFragment implements OnBackPressedLis
}
@Override
public boolean onAddMediaAttachmentFromEditText(Uri uri){
return addMediaAttachment(uri);
public boolean onAddMediaAttachmentFromEditText(Uri uri, String description){
return addMediaAttachment(uri, description);
}
private void startAutocomplete(ComposeAutocompleteSpan span){

View File

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