Allow inserting multiple images

This commit is contained in:
M66B 2020-03-28 09:12:00 +01:00
parent f5775f178e
commit 1f3f463b4c
1 changed files with 45 additions and 39 deletions

View File

@ -460,7 +460,7 @@ public class FragmentCompose extends FragmentBase {
etBody.setInputContentListener(new EditTextCompose.IInputContentListener() { etBody.setInputContentListener(new EditTextCompose.IInputContentListener() {
@Override @Override
public void onInputContent(Uri uri) { public void onInputContent(Uri uri) {
onAddAttachment(uri, true, 0); onAddAttachment(Arrays.asList(uri), true, 0);
} }
}); });
@ -1589,22 +1589,19 @@ public class FragmentCompose extends FragmentBase {
onAddImage(data.getBundleExtra("args").getBoolean("photo")); onAddImage(data.getBundleExtra("args").getBoolean("photo"));
break; break;
case REQUEST_IMAGE_FILE: case REQUEST_IMAGE_FILE:
if (resultCode == RESULT_OK && data != null) { if (resultCode == RESULT_OK && data != null)
Uri uri = data.getData(); onAddImageFile(getUris(data));
if (uri != null)
onAddImageFile(uri);
}
break; break;
case REQUEST_TAKE_PHOTO: case REQUEST_TAKE_PHOTO:
if (resultCode == RESULT_OK) { if (resultCode == RESULT_OK) {
if (photoURI != null) if (photoURI != null)
onAddImageFile(photoURI); onAddImageFile(Arrays.asList(photoURI));
} }
break; break;
case REQUEST_ATTACHMENT: case REQUEST_ATTACHMENT:
case REQUEST_RECORD_AUDIO: case REQUEST_RECORD_AUDIO:
if (resultCode == RESULT_OK && data != null) if (resultCode == RESULT_OK && data != null)
onAddMedia(data); onAddAttachment(getUris(data), false, 0);
break; break;
case REQUEST_OPENPGP: case REQUEST_OPENPGP:
if (resultCode == RESULT_OK && data != null) if (resultCode == RESULT_OK && data != null)
@ -1767,6 +1764,7 @@ public class FragmentCompose extends FragmentBase {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE); intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*"); intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
PackageManager pm = getContext().getPackageManager(); PackageManager pm = getContext().getPackageManager();
if (intent.resolveActivity(pm) == null) if (intent.resolveActivity(pm) == null)
noStorageAccessFramework(); noStorageAccessFramework();
@ -1775,7 +1773,7 @@ public class FragmentCompose extends FragmentBase {
} }
} }
private void onAddImageFile(Uri uri) { private void onAddImageFile(List<Uri> uri) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean add_inline = prefs.getBoolean("add_inline", true); boolean add_inline = prefs.getBoolean("add_inline", true);
boolean resize_images = prefs.getBoolean("resize_images", true); boolean resize_images = prefs.getBoolean("resize_images", true);
@ -1783,10 +1781,10 @@ public class FragmentCompose extends FragmentBase {
onAddAttachment(uri, add_inline, resize_images ? resize : 0); onAddAttachment(uri, add_inline, resize_images ? resize : 0);
} }
private void onAddAttachment(Uri uri, boolean image, int resize) { private void onAddAttachment(List<Uri> uris, boolean image, int resize) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putLong("id", working); args.putLong("id", working);
args.putParcelable("uri", uri); args.putParcelableArrayList("uris", new ArrayList<>(uris));
args.putBoolean("image", image); args.putBoolean("image", image);
args.putInt("resize", resize); args.putInt("resize", resize);
args.putCharSequence("body", etBody.getText()); args.putCharSequence("body", etBody.getText());
@ -1796,29 +1794,36 @@ public class FragmentCompose extends FragmentBase {
@Override @Override
protected Spanned onExecute(Context context, Bundle args) throws IOException { protected Spanned onExecute(Context context, Bundle args) throws IOException {
long id = args.getLong("id"); long id = args.getLong("id");
Uri uri = args.getParcelable("uri"); List<Uri> uris = args.getParcelableArrayList("uris");
boolean image = args.getBoolean("image"); boolean image = args.getBoolean("image");
int resize = args.getInt("resize"); int resize = args.getInt("resize");
EntityAttachment attachment = addAttachment(context, id, uri, image, resize);
if (!image)
return null;
File file = attachment.getFile(context);
Drawable d = Drawable.createFromPath(file.getAbsolutePath());
if (d == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_image));
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
CharSequence body = args.getCharSequence("body"); CharSequence body = args.getCharSequence("body");
int start = args.getInt("start"); int start = args.getInt("start");
Uri cid = Uri.parse("cid:" + BuildConfig.APPLICATION_ID + "." + attachment.id);
SpannableStringBuilder s = new SpannableStringBuilder(body); SpannableStringBuilder s = new SpannableStringBuilder(body);
s.insert(start, " ");
ImageSpan is = new ImageSpan(context, cid); for (Uri uri : uris) {
s.setSpan(is, start + 1, start + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); EntityAttachment attachment = addAttachment(context, id, uri, image, resize);
if (!image)
continue;
File file = attachment.getFile(context);
Uri cid = Uri.parse("cid:" + BuildConfig.APPLICATION_ID + "." + attachment.id);
Drawable d = Drawable.createFromPath(file.getAbsolutePath());
if (d == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_image));
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
s.insert(start, " ");
ImageSpan is = new ImageSpan(context, cid);
s.setSpan(is, start + 1, start + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
start += 3;
}
if (!image)
return null;
return HtmlHelper.fromHtml(HtmlHelper.toHtml(s), new Html.ImageGetter() { return HtmlHelper.fromHtml(HtmlHelper.toHtml(s), new Html.ImageGetter() {
@Override @Override
@ -1830,13 +1835,13 @@ public class FragmentCompose extends FragmentBase {
@Override @Override
protected void onExecuted(Bundle args, final Spanned body) { protected void onExecuted(Bundle args, final Spanned body) {
if (body == null) if (body != null) {
return; int start = args.getInt("start");
int start = args.getInt("start");
etBody.setText(body); etBody.setText(body);
if (start < body.length()) if (start < body.length())
etBody.setSelection(start); etBody.setSelection(start);
}
// Save text & update remote draft // Save text & update remote draft
onAction(R.id.action_save); onAction(R.id.action_save);
@ -1855,23 +1860,24 @@ public class FragmentCompose extends FragmentBase {
}.execute(this, args, "compose:attachment:add"); }.execute(this, args, "compose:attachment:add");
} }
private void onAddMedia(Intent data) { private List<Uri> getUris(Intent data) {
Log.i("Add media data=" + data); List<Uri> result = new ArrayList<>();
Log.logExtras(data);
ClipData clipData = data.getClipData(); ClipData clipData = data.getClipData();
if (clipData == null) { if (clipData == null) {
Uri uri = data.getData(); Uri uri = data.getData();
if (uri != null) if (uri != null)
onAddAttachment(uri, false, 0); result.add(uri);
} else { } else {
for (int i = 0; i < clipData.getItemCount(); i++) { for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i); ClipData.Item item = clipData.getItemAt(i);
Uri uri = item.getUri(); Uri uri = item.getUri();
if (uri != null) if (uri != null)
onAddAttachment(uri, false, 0); result.add(uri);
} }
} }
return result;
} }
private void onPgp(Intent data) { private void onPgp(Intent data) {