allow sharing multiple images at once. fixes #1090

This commit is contained in:
iNPUTmice 2015-04-11 14:53:10 +02:00
parent 332fe0fd19
commit 37d08276a8
3 changed files with 111 additions and 59 deletions

View file

@ -122,6 +122,13 @@
<data android:mimeType="*/*" /> <data android:mimeType="*/*" />
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity> </activity>
<activity <activity
android:name="de.duenndns.ssl.MemorizingActivity" android:name="de.duenndns.ssl.MemorizingActivity"

View file

@ -5,6 +5,7 @@ import android.app.ActionBar;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.FragmentTransaction; import android.app.FragmentTransaction;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ClipData;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
@ -30,6 +31,7 @@ import net.java.otr4j.session.SessionStatus;
import de.timroes.android.listview.EnhancedListView; import de.timroes.android.listview.EnhancedListView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
@ -69,8 +71,8 @@ public class ConversationActivity extends XmppActivity
private String mOpenConverstaion = null; private String mOpenConverstaion = null;
private boolean mPanelOpen = true; private boolean mPanelOpen = true;
private Uri mPendingImageUri = null; final private List<Uri> mPendingImageUris = new ArrayList<>();
private Uri mPendingFileUri = null; final private List<Uri> mPendingFileUris = new ArrayList<>();
private Uri mPendingGeoUri = null; private Uri mPendingGeoUri = null;
private View mContentView; private View mContentView;
@ -141,12 +143,13 @@ public class ConversationActivity extends XmppActivity
@Override @Override
protected void onCreate(final Bundle savedInstanceState) { protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (savedInstanceState != null) {mOpenConverstaion = savedInstanceState.getString( if (savedInstanceState != null) {
STATE_OPEN_CONVERSATION, null); mOpenConverstaion = savedInstanceState.getString(STATE_OPEN_CONVERSATION, null);
mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true); mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
String pending = savedInstanceState.getString(STATE_PENDING_URI, null); String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
if (pending != null) { if (pending != null) {
mPendingImageUri = Uri.parse(pending); mPendingImageUris.clear();
mPendingImageUris.add(Uri.parse(pending));
} }
} }
@ -409,13 +412,18 @@ public class ConversationActivity extends XmppActivity
switch (attachmentChoice) { switch (attachmentChoice) {
case ATTACHMENT_CHOICE_CHOOSE_IMAGE: case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
intent.setAction(Intent.ACTION_GET_CONTENT); intent.setAction(Intent.ACTION_GET_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
}
intent.setType("image/*"); intent.setType("image/*");
chooser = true; chooser = true;
break; break;
case ATTACHMENT_CHOICE_TAKE_PHOTO: case ATTACHMENT_CHOICE_TAKE_PHOTO:
mPendingImageUri = xmppConnectionService.getFileBackend().getTakePhotoUri(); Uri uri = xmppConnectionService.getFileBackend().getTakePhotoUri();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mPendingImageUri); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
mPendingImageUris.clear();
mPendingImageUris.add(uri);
break; break;
case ATTACHMENT_CHOICE_CHOOSE_FILE: case ATTACHMENT_CHOICE_CHOOSE_FILE:
chooser = true; chooser = true;
@ -857,8 +865,8 @@ public class ConversationActivity extends XmppActivity
} }
savedInstanceState.putBoolean(STATE_PANEL_OPEN, savedInstanceState.putBoolean(STATE_PANEL_OPEN,
isConversationsOverviewVisable()); isConversationsOverviewVisable());
if (this.mPendingImageUri != null) { if (this.mPendingImageUris.size() >= 1) {
savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUri.toString()); savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUris.get(0).toString());
} }
super.onSaveInstanceState(savedInstanceState); super.onSaveInstanceState(savedInstanceState);
} }
@ -897,21 +905,23 @@ public class ConversationActivity extends XmppActivity
this.mConversationFragment.reInit(getSelectedConversation()); this.mConversationFragment.reInit(getSelectedConversation());
} else { } else {
showConversationsOverview(); showConversationsOverview();
mPendingImageUri = null; mPendingImageUris.clear();
mPendingFileUri = null; mPendingFileUris.clear();
mPendingGeoUri = null; mPendingGeoUri = null;
setSelectedConversation(conversationList.get(0)); setSelectedConversation(conversationList.get(0));
this.mConversationFragment.reInit(getSelectedConversation()); this.mConversationFragment.reInit(getSelectedConversation());
} }
if (mPendingImageUri != null) { for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
attachImageToConversation(getSelectedConversation(),mPendingImageUri); attachImageToConversation(getSelectedConversation(),i.next());
mPendingImageUri = null; }
} else if (mPendingFileUri != null) {
attachFileToConversation(getSelectedConversation(),mPendingFileUri); for(Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
mPendingFileUri = null; attachFileToConversation(getSelectedConversation(),i.next());
} else if (mPendingGeoUri != null) { }
attachLocationToConversation(getSelectedConversation(),mPendingGeoUri);
if (mPendingGeoUri != null) {
attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
mPendingGeoUri = null; mPendingGeoUri = null;
} }
ExceptionHelper.checkForCrash(this, this.xmppConnectionService); ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
@ -963,6 +973,20 @@ public class ConversationActivity extends XmppActivity
xmppConnectionService.getNotificationService().setOpenConversation(null); xmppConnectionService.getNotificationService().setOpenConversation(null);
} }
private static List<Uri> extractUriFromIntent(final Intent intent) {
List<Uri> uris = new ArrayList<>();
Uri uri = intent.getData();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && uri == null) {
ClipData clipData = intent.getClipData();
for(int i = 0; i < clipData.getItemCount(); ++i) {
uris.add(clipData.getItemAt(i).getUri());
}
} else {
uris.add(uri);
}
return uris;
}
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, protected void onActivityResult(int requestCode, int resultCode,
final Intent data) { final Intent data) {
@ -972,25 +996,34 @@ public class ConversationActivity extends XmppActivity
mConversationFragment.hideSnackbar(); mConversationFragment.hideSnackbar();
mConversationFragment.updateMessages(); mConversationFragment.updateMessages();
} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) { } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
mPendingImageUri = data.getData(); mPendingImageUris.clear();
mPendingImageUris.addAll(extractUriFromIntent(data));
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
attachImageToConversation(getSelectedConversation(),mPendingImageUri); for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
mPendingImageUri = null; attachImageToConversation(getSelectedConversation(),i.next());
}
} }
} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) { } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) {
mPendingFileUri = data.getData(); mPendingFileUris.clear();
mPendingFileUris.addAll(extractUriFromIntent(data));
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
attachFileToConversation(getSelectedConversation(),mPendingFileUri); for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
mPendingFileUri = null; attachFileToConversation(getSelectedConversation(), i.next());
} }
} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO && mPendingImageUri != null) { }
} else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
if (mPendingImageUris.size() == 1) {
Uri uri = mPendingImageUris.get(0);
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
attachImageToConversation(getSelectedConversation(),mPendingImageUri); attachImageToConversation(getSelectedConversation(), uri);
mPendingImageUri = null; mPendingImageUris.clear();
} }
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(mPendingImageUri); intent.setData(uri);
sendBroadcast(intent); sendBroadcast(intent);
} else {
mPendingImageUris.clear();
}
} else if (requestCode == ATTACHMENT_CHOICE_LOCATION) { } else if (requestCode == ATTACHMENT_CHOICE_LOCATION) {
double latitude = data.getDoubleExtra("latitude",0); double latitude = data.getDoubleExtra("latitude",0);
double longitude = data.getDoubleExtra("longitude",0); double longitude = data.getDoubleExtra("longitude",0);
@ -1000,10 +1033,6 @@ public class ConversationActivity extends XmppActivity
this.mPendingGeoUri = null; this.mPendingGeoUri = null;
} }
} }
} else {
if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
mPendingImageUri = null;
}
} }
} }

View file

@ -18,6 +18,7 @@ import java.net.URLConnection;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.UnsupportedCharsetException; import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
@ -32,7 +33,7 @@ import eu.siacs.conversations.xmpp.jid.Jid;
public class ShareWithActivity extends XmppActivity { public class ShareWithActivity extends XmppActivity {
private class Share { private class Share {
public Uri uri; public List<Uri> uris = new ArrayList<>();
public boolean image; public boolean image;
public String account; public String account;
public String contact; public String contact;
@ -104,7 +105,7 @@ public class ShareWithActivity extends XmppActivity {
int position, long arg3) { int position, long arg3) {
Conversation conversation = mConversations.get(position); Conversation conversation = mConversations.get(position);
if (conversation.getMode() == Conversation.MODE_SINGLE if (conversation.getMode() == Conversation.MODE_SINGLE
|| share.uri == null) { || share.uris.size() == 0) {
share(mConversations.get(position)); share(mConversations.get(position));
} }
} }
@ -133,18 +134,32 @@ public class ShareWithActivity extends XmppActivity {
@Override @Override
public void onStart() { public void onStart() {
final String type = getIntent().getType(); super.onStart();
Intent intent = getIntent();
if (intent == null) {
return;
}
final String type = intent.getType();
if (Intent.ACTION_SEND.equals(intent.getAction())) {
final Uri uri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM); final Uri uri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
if (type != null && uri != null && !type.equalsIgnoreCase("text/plain")) { if (type != null && uri != null && !type.equalsIgnoreCase("text/plain")) {
this.share.uri = uri; this.share.uris.add(uri);
this.share.image = type.startsWith("image/") || isImage(uri); this.share.image = type.startsWith("image/") || isImage(uri);
} else { } else {
this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT); this.share.text = getIntent().getStringExtra(Intent.EXTRA_TEXT);
} }
if (xmppConnectionServiceBound) { } else if (Intent.ACTION_SEND_MULTIPLE.equals(intent.getAction())) {
xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uri == null); this.share.image = type != null && type.startsWith("image/");
if (!this.share.image) {
return;
} }
super.onStart();
this.share.uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
}
if (xmppConnectionServiceBound) {
xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.image);
}
} }
protected boolean isImage(Uri uri) { protected boolean isImage(Uri uri) {
@ -164,7 +179,7 @@ public class ShareWithActivity extends XmppActivity {
return; return;
} }
xmppConnectionService.populateWithOrderedConversations(mConversations, xmppConnectionService.populateWithOrderedConversations(mConversations,
this.share != null && this.share.uri == null); this.share != null && this.share.uris.size() == 0);
} }
private void share() { private void share() {
@ -188,7 +203,7 @@ public class ShareWithActivity extends XmppActivity {
} }
private void share(final Conversation conversation) { private void share(final Conversation conversation) {
if (share.uri != null) { if (share.uris.size() != 0) {
selectPresence(conversation, new OnPresenceSelected() { selectPresence(conversation, new OnPresenceSelected() {
@Override @Override
public void onPresenceSelected() { public void onPresenceSelected() {
@ -196,22 +211,23 @@ public class ShareWithActivity extends XmppActivity {
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
getText(R.string.preparing_image), getText(R.string.preparing_image),
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
for (Iterator<Uri> i = share.uris.iterator(); i.hasNext(); i.remove()) {
ShareWithActivity.this.xmppConnectionService ShareWithActivity.this.xmppConnectionService
.attachImageToConversation(conversation, share.uri, .attachImageToConversation(conversation, i.next(),
attachFileCallback); attachFileCallback);
}
} else { } else {
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
getText(R.string.preparing_file), getText(R.string.preparing_file),
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
ShareWithActivity.this.xmppConnectionService ShareWithActivity.this.xmppConnectionService
.attachFileToConversation(conversation, share.uri, .attachFileToConversation(conversation, share.uris.get(0),
attachFileCallback); attachFileCallback);
} }
switchToConversation(conversation, null, true); switchToConversation(conversation, null, true);
finish(); finish();
} }
}); });
} else { } else {
switchToConversation(conversation, this.share.text, true); switchToConversation(conversation, this.share.text, true);
finish(); finish();