From e919e98b68373b65450ea1450828554b6fd9e332 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 24 Dec 2022 16:23:46 +0100 Subject: [PATCH] Fix issue #665 - Adding description failed when sharing --- .../app/fedilab/android/BaseMainActivity.java | 13 +++++++--- .../android/activities/ComposeActivity.java | 23 +++++++++--------- .../fedilab/android/helper/CacheHelper.java | 2 +- .../app/fedilab/android/helper/Helper.java | 24 ++++++++++++------- .../imageeditor/EditImageActivity.java | 8 +------ .../android/ui/drawer/ComposeAdapter.java | 11 +++++++-- 6 files changed, 47 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index a69de626..cffdb745 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -1076,8 +1076,12 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); if (imageUri != null) { Bundle b = new Bundle(); - b.putParcelable(Helper.ARG_SHARE_URI, imageUri); - CrossActionHelper.doCrossShare(BaseMainActivity.this, b); + List uris = new ArrayList<>(); + uris.add(imageUri); + Helper.createAttachmentFromUri(BaseMainActivity.this, uris, attachments -> { + b.putSerializable(Helper.ARG_MEDIA_ATTACHMENTS, new ArrayList<>(attachments)); + CrossActionHelper.doCrossShare(BaseMainActivity.this, b); + }); } else { Toasty.warning(BaseMainActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show(); } @@ -1087,7 +1091,10 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt ArrayList imageList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); if (imageList != null) { Bundle b = new Bundle(); - b.putParcelableArrayList(Helper.ARG_SHARE_URI_LIST, imageList); + Helper.createAttachmentFromUri(BaseMainActivity.this, imageList, attachments -> { + b.putSerializable(Helper.ARG_MEDIA_ATTACHMENTS, new ArrayList<>(attachments)); + CrossActionHelper.doCrossShare(BaseMainActivity.this, b); + }); CrossActionHelper.doCrossShare(BaseMainActivity.this, b); } else { Toasty.warning(BaseMainActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java b/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java index 3e344a94..88addf67 100644 --- a/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/ComposeActivity.java @@ -109,6 +109,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana private ComposeAdapter composeAdapter; private boolean promptSaveDraft; private boolean restoredDraft; + private List sharedAttachments; private final BroadcastReceiver imageReceiver = new BroadcastReceiver() { @@ -147,8 +148,6 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana private app.fedilab.android.client.entities.api.Account accountMention; private String statusReplyId; private app.fedilab.android.client.entities.api.Account mentionBooster; - private ArrayList sharedUriList = new ArrayList<>(); - private Uri sharedUri; private String sharedSubject, sharedContent, sharedTitle, sharedDescription, shareURL, sharedUrlMedia; private String editMessageId; @@ -483,8 +482,7 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana mentionBooster = (app.fedilab.android.client.entities.api.Account) b.getSerializable(Helper.ARG_MENTION_BOOSTER); accountMention = (app.fedilab.android.client.entities.api.Account) b.getSerializable(Helper.ARG_ACCOUNT_MENTION); //Shared elements - sharedUriList = b.getParcelableArrayList(Helper.ARG_SHARE_URI_LIST); - sharedUri = b.getParcelable(Helper.ARG_SHARE_URI); + sharedAttachments = (ArrayList) b.getSerializable(Helper.ARG_MEDIA_ATTACHMENTS); sharedUrlMedia = b.getString(Helper.ARG_SHARE_URL_MEDIA); sharedSubject = b.getString(Helper.ARG_SHARE_SUBJECT, null); sharedContent = b.getString(Helper.ARG_SHARE_CONTENT, null); @@ -678,18 +676,19 @@ public class ComposeActivity extends BaseActivity implements ComposeAdapter.Mana }, 0, 10000); } - if (sharedUriList != null && sharedUriList.size() > 0) { - List uris = new ArrayList<>(sharedUriList); - Helper.createAttachmentFromUri(ComposeActivity.this, uris, attachment -> { + if (sharedAttachments != null && sharedAttachments.size() > 0) { + for (Attachment attachment : sharedAttachments) { composeAdapter.addAttachment(-1, attachment); - }); - } else if (sharedUri != null && !sharedUri.toString().startsWith("http")) { + } + } /*else if (sharedUri != null && !sharedUri.toString().startsWith("http")) { List uris = new ArrayList<>(); uris.add(sharedUri); - Helper.createAttachmentFromUri(ComposeActivity.this, uris, attachment -> { - composeAdapter.addAttachment(-1, attachment); + Helper.createAttachmentFromUri(ComposeActivity.this, uris, attachments -> { + for(Attachment attachment: attachments) { + composeAdapter.addAttachment(-1, attachment); + } }); - } else if (shareURL != null) { + } */ else if (shareURL != null) { Helper.download(ComposeActivity.this, sharedUrlMedia, new OnDownloadInterface() { @Override diff --git a/app/src/main/java/app/fedilab/android/helper/CacheHelper.java b/app/src/main/java/app/fedilab/android/helper/CacheHelper.java index ca328240..46ecb5c9 100644 --- a/app/src/main/java/app/fedilab/android/helper/CacheHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/CacheHelper.java @@ -96,7 +96,7 @@ public class CacheHelper { String[] children = dir.list(); assert children != null; for (String aChildren : children) { - if (!aChildren.equals("databases") && !aChildren.equals("shared_prefs")) { + if (!aChildren.equals("databases") && !aChildren.equals("shared_prefs") && !aChildren.equals(Helper.TEMP_MEDIA_DIRECTORY)) { boolean success = deleteDir(new File(dir, aChildren)); if (!success) { return false; diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index b710f0b5..f405065f 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -235,10 +235,8 @@ public class Helper { public static final String ARG_MINIFIED = "ARG_MINIFIED"; public static final String ARG_STATUS_REPORT = "ARG_STATUS_REPORT"; public static final String ARG_STATUS_MENTION = "ARG_STATUS_MENTION"; - public static final String ARG_SHARE_URI = "ARG_SHARE_URI"; public static final String ARG_SHARE_URL_MEDIA = "ARG_SHARE_URL_MEDIA"; public static final String ARG_SHARE_URL = "ARG_SHARE_URL"; - public static final String ARG_SHARE_URI_LIST = "ARG_SHARE_URI_LIST"; public static final String ARG_SHARE_TITLE = "ARG_SHARE_TITLE"; public static final String ARG_SHARE_SUBJECT = "ARG_SHARE_SUBJECT"; public static final String ARG_SHARE_DESCRIPTION = "ARG_SHARE_DESCRIPTION"; @@ -258,6 +256,7 @@ public class Helper { public static final String ARG_TAG_TIMELINE = "ARG_TAG_TIMELINE"; public static final String ARG_MEDIA_POSITION = "ARG_MEDIA_POSITION"; public static final String ARG_MEDIA_ATTACHMENT = "ARG_MEDIA_ATTACHMENT"; + public static final String ARG_MEDIA_ATTACHMENTS = "ARG_MEDIA_ATTACHMENTS"; public static final String ARG_SHOW_REPLIES = "ARG_SHOW_REPLIES"; public static final String ARG_SHOW_REBLOGS = "ARG_SHOW_REBLOGS"; public static final String ARG_INITIALIZE_VIEW = "ARG_INITIALIZE_VIEW"; @@ -1220,6 +1219,7 @@ public class Helper { public static void createAttachmentFromUri(Context context, List uris, OnAttachmentCopied callBack) { new Thread(() -> { + List attachments = new ArrayList<>(); for (Uri uri : uris) { Attachment attachment = new Attachment(); attachment.filename = Helper.getFileName(context, uri); @@ -1274,11 +1274,11 @@ public class Helper { e.printStackTrace(); } } - - Handler mainHandler = new Handler(Looper.getMainLooper()); - Runnable myRunnable = () -> callBack.onAttachmentCopied(attachment); - mainHandler.post(myRunnable); + attachments.add(attachment); } + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> callBack.onAttachmentCopied(attachments); + mainHandler.post(myRunnable); }).start(); } @@ -1328,6 +1328,7 @@ public class Helper { public static void createAttachmentFromPAth(String path, OnAttachmentCopied callBack) { new Thread(() -> { + List attachmentList = new ArrayList<>(); Attachment attachment = new Attachment(); attachment.mimeType = "image/*"; String extension = "jpg"; @@ -1336,7 +1337,8 @@ public class Helper { Date now = new Date(); attachment.filename = formatter.format(now) + "." + extension; Handler mainHandler = new Handler(Looper.getMainLooper()); - Runnable myRunnable = () -> callBack.onAttachmentCopied(attachment); + attachmentList.add(attachment); + Runnable myRunnable = () -> callBack.onAttachmentCopied(attachmentList); mainHandler.post(myRunnable); }).start(); } @@ -1733,7 +1735,11 @@ public class Helper { fileName = FileNameCleaner.cleanFileName(fileName); // opens input stream from the HTTP connection InputStream inputStream = httpURLConnection.getInputStream(); - File saveDir = context.getCacheDir(); + final File saveDir = new File(context.getCacheDir(), TEMP_MEDIA_DIRECTORY); + boolean isCertCacheDirExists = saveDir.exists(); + if (!isCertCacheDirExists) { + saveDir.mkdirs(); + } final String saveFilePath = saveDir + File.separator + fileName; // opens an output stream to save into file FileOutputStream outputStream = new FileOutputStream(saveFilePath); @@ -1979,7 +1985,7 @@ public class Helper { } public interface OnAttachmentCopied { - void onAttachmentCopied(Attachment attachment); + void onAttachmentCopied(List attachments); } public interface OnFileCopied { diff --git a/app/src/main/java/app/fedilab/android/imageeditor/EditImageActivity.java b/app/src/main/java/app/fedilab/android/imageeditor/EditImageActivity.java index ecdee197..65c5545e 100644 --- a/app/src/main/java/app/fedilab/android/imageeditor/EditImageActivity.java +++ b/app/src/main/java/app/fedilab/android/imageeditor/EditImageActivity.java @@ -10,7 +10,6 @@ import android.graphics.Typeface; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; -import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.animation.AnticipateOvershootInterpolator; @@ -179,8 +178,6 @@ public class EditImageActivity extends BaseActivity implements OnPhotoEditorList } uri = resultUri; } - } else { - Log.e(Helper.TAG, "onActivityResult...Error CropImage: " + result.getError()); } }); mPhotoEditor.setFilterEffect(PhotoFilter.NONE); @@ -299,7 +296,7 @@ public class EditImageActivity extends BaseActivity implements OnPhotoEditorList int imgHeightInEditor; int imgWidthInEditor; //If the original image has its height greater than width => heights are equals - float focusX = -2, focusY = -2; + float focusX, focusY; if (imageHeight > imageWidth) { imgHeightInEditor = pHeight; float ratio = (float) pHeight / (float) imageHeight; @@ -325,7 +322,6 @@ public class EditImageActivity extends BaseActivity implements OnPhotoEditorList intentImage.putExtra("focusY", focusY); } - LocalBroadcastManager.getInstance(EditImageActivity.this).sendBroadcast(intentImage); finish(); } @@ -415,8 +411,6 @@ public class EditImageActivity extends BaseActivity implements OnPhotoEditorList } uri = resultUri; } - } else { - Log.e(Helper.TAG, "onActivityResult...Error CropImage: " + result.getError()); } }); cropImageContractOptionsActivityResultLauncher.launch(cropImageContractOptions); diff --git a/app/src/main/java/app/fedilab/android/ui/drawer/ComposeAdapter.java b/app/src/main/java/app/fedilab/android/ui/drawer/ComposeAdapter.java index 88ae8424..9f9ce7d4 100644 --- a/app/src/main/java/app/fedilab/android/ui/drawer/ComposeAdapter.java +++ b/app/src/main/java/app/fedilab/android/ui/drawer/ComposeAdapter.java @@ -221,8 +221,10 @@ public class ComposeAdapter extends RecyclerView.Adapter { - statusList.get(finalPosition).media_attachments.add(attachment); + Helper.createAttachmentFromUri(context, uris, attachments -> { + for (Attachment attachment : attachments) { + statusList.get(finalPosition).media_attachments.add(attachment); + } notifyItemChanged(finalPosition); }); } @@ -1098,6 +1100,11 @@ public class ComposeAdapter extends RecyclerView.Adapter dialog.dismiss()); if (attachment.description != null) {