conversations-classic/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java

423 lines
12 KiB
Java
Raw Normal View History

2014-10-22 16:38:44 +00:00
package eu.siacs.conversations.ui;
import android.app.PendingIntent;
import android.content.Intent;
2017-10-27 15:25:01 +00:00
import android.content.pm.PackageManager;
2014-10-22 16:38:44 +00:00
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
2014-10-22 16:38:44 +00:00
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.adapter.ConversationAdapter;
import eu.siacs.conversations.ui.service.EmojiService;
import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;
public class ShareWithActivity extends XmppActivity implements XmppConnectionService.OnConversationUpdate {
2017-10-27 15:25:01 +00:00
private static final int REQUEST_STORAGE_PERMISSION = 0x733f32;
2016-04-14 19:12:44 +00:00
private boolean mReturnToPrevious = false;
2017-10-27 15:25:01 +00:00
private Conversation mPendingConversation = null;
2016-04-14 19:12:44 +00:00
@Override
public void onConversationUpdate() {
refreshUi();
}
2014-10-22 16:38:44 +00:00
private class Share {
public List<Uri> uris = new ArrayList<>();
2014-12-17 06:12:38 +00:00
public boolean image;
2014-10-22 16:38:44 +00:00
public String account;
public String contact;
public String text;
public String uuid;
public boolean multiple = false;
2014-10-22 16:38:44 +00:00
}
private Share share;
private static final int REQUEST_START_NEW_CONVERSATION = 0x0501;
private ListView mListView;
private ConversationAdapter mAdapter;
private List<Conversation> mConversations = new ArrayList<>();
private Toast mToast;
private AtomicInteger attachmentCounter = new AtomicInteger(0);
2014-10-22 16:38:44 +00:00
private UiInformableCallback<Message> attachFileCallback = new UiInformableCallback<Message>() {
@Override
public void inform(final String text) {
runOnUiThread(new Runnable() {
@Override
public void run() {
replaceToast(text);
}
});
}
2014-10-22 16:38:44 +00:00
@Override
public void userInputRequried(PendingIntent pi, Message object) {
// TODO Auto-generated method stub
}
@Override
public void success(final Message message) {
2014-10-22 16:38:44 +00:00
xmppConnectionService.sendMessage(message);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (attachmentCounter.decrementAndGet() <=0 ) {
int resId;
if (share.image && share.multiple) {
resId = R.string.shared_images_with_x;
} else if (share.image) {
resId = R.string.shared_image_with_x;
} else {
resId = R.string.shared_file_with_x;
}
replaceToast(getString(resId, message.getConversation().getName()));
2016-04-14 19:12:44 +00:00
if (mReturnToPrevious) {
finish();
} else {
switchToConversation(message.getConversation());
}
}
}
});
2014-10-22 16:38:44 +00:00
}
@Override
public void error(final int errorCode, Message object) {
runOnUiThread(new Runnable() {
@Override
public void run() {
replaceToast(getString(errorCode));
if (attachmentCounter.decrementAndGet() <=0 ) {
finish();
}
}
});
2014-10-22 16:38:44 +00:00
}
};
protected void hideToast() {
if (mToast != null) {
mToast.cancel();
}
}
protected void replaceToast(String msg) {
hideToast();
mToast = Toast.makeText(this, msg ,Toast.LENGTH_LONG);
mToast.show();
}
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
2014-10-22 16:38:44 +00:00
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_START_NEW_CONVERSATION
&& resultCode == RESULT_OK) {
share.contact = data.getStringExtra("contact");
share.account = data.getStringExtra(EXTRA_ACCOUNT);
2014-10-22 16:38:44 +00:00
}
if (xmppConnectionServiceBound
&& share != null
&& share.contact != null
&& share.account != null) {
2014-10-22 16:38:44 +00:00
share();
}
}
2017-10-27 15:25:01 +00:00
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (grantResults.length > 0)
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (requestCode == REQUEST_STORAGE_PERMISSION) {
if (this.mPendingConversation != null) {
share(this.mPendingConversation);
} else {
Log.d(Config.LOGTAG,"unable to find stored conversation");
}
}
} else {
Toast.makeText(this, R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
}
}
2014-10-22 16:38:44 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new EmojiService(this).init();
if (getActionBar() != null) {
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setHomeButtonEnabled(false);
}
2014-10-22 16:38:44 +00:00
setContentView(R.layout.share_with);
setTitle(getString(R.string.title_activity_sharewith));
2017-10-27 15:25:01 +00:00
mListView = findViewById(R.id.choose_conversation_list);
mAdapter = new ConversationAdapter(this, this.mConversations);
2014-10-22 16:38:44 +00:00
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
share(mConversations.get(position));
2014-10-22 16:38:44 +00:00
}
});
this.share = new Share();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_with, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
2014-10-22 16:38:44 +00:00
switch (item.getItemId()) {
case R.id.action_add:
final Intent intent = new Intent(getApplicationContext(), ChooseContactActivity.class);
startActivityForResult(intent, REQUEST_START_NEW_CONVERSATION);
return true;
2014-10-22 16:38:44 +00:00
}
return super.onOptionsItemSelected(item);
}
@Override
public void onStart() {
super.onStart();
Intent intent = getIntent();
if (intent == null) {
return;
}
this.mReturnToPrevious = getPreferences().getBoolean("return_to_previous", getResources().getBoolean(R.bool.return_to_previous));
final String type = intent.getType();
2016-03-16 17:09:19 +00:00
final String action = intent.getAction();
Log.d(Config.LOGTAG, "action: "+action+ ", type:"+type);
share.uuid = intent.getStringExtra("uuid");
2016-03-16 17:09:19 +00:00
if (Intent.ACTION_SEND.equals(action)) {
final String text = intent.getStringExtra(Intent.EXTRA_TEXT);
final Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (type != null && uri != null && (text == null || !type.equals("text/plain"))) {
this.share.uris.clear();
this.share.uris.add(uri);
this.share.image = type.startsWith("image/") || isImage(uri);
} else {
2016-03-16 17:09:19 +00:00
this.share.text = text;
}
2016-03-16 17:09:19 +00:00
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
this.share.image = type != null && type.startsWith("image/");
if (!this.share.image) {
return;
}
this.share.uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
2014-10-22 16:38:44 +00:00
}
if (xmppConnectionServiceBound) {
if (share.uuid != null) {
share();
} else {
xmppConnectionService.populateWithOrderedConversations(mConversations, this.share.uris.size() == 0);
}
2014-10-22 16:38:44 +00:00
}
2014-10-22 16:38:44 +00:00
}
protected boolean isImage(Uri uri) {
try {
String guess = URLConnection.guessContentTypeFromName(uri.toString());
return (guess != null && guess.startsWith("image/"));
} catch (final StringIndexOutOfBoundsException ignored) {
return false;
}
}
2014-10-22 16:38:44 +00:00
@Override
void onBackendConnected() {
if (xmppConnectionServiceBound && share != null
&& ((share.contact != null && share.account != null) || share.uuid != null)) {
2014-10-22 16:38:44 +00:00
share();
return;
}
refreshUiReal();
2014-10-22 16:38:44 +00:00
}
private void share() {
final Conversation conversation;
if (share.uuid != null) {
conversation = xmppConnectionService.findConversationByUuid(share.uuid);
if (conversation == null) {
return;
}
}else{
Account account;
try {
account = xmppConnectionService.findAccountByJid(Jid.fromString(share.account));
} catch (final InvalidJidException e) {
account = null;
}
if (account == null) {
return;
}
try {
conversation = xmppConnectionService
.findOrCreateConversation(account, Jid.fromString(share.contact), false,true);
} catch (final InvalidJidException e) {
return;
}
}
share(conversation);
2014-10-22 16:38:44 +00:00
}
private void share(final Conversation conversation) {
2017-10-27 15:25:01 +00:00
if (share.uris.size() != 0 && !hasStoragePermission(REQUEST_STORAGE_PERMISSION)) {
mPendingConversation = conversation;
return;
}
final Account account = conversation.getAccount();
final XmppConnection connection = account.getXmppConnection();
final long max = connection == null ? -1 : connection.getFeatures().getMaxHttpUploadSize();
mListView.setEnabled(false);
if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP && !hasPgp()) {
if (share.uuid == null) {
showInstallPgpDialog();
} else {
Toast.makeText(this,R.string.openkeychain_not_installed,Toast.LENGTH_SHORT).show();
finish();
}
return;
}
if (share.uris.size() != 0) {
2018-01-01 10:56:31 +00:00
OnPresenceSelected callback = () -> {
attachmentCounter.set(share.uris.size());
if (share.image) {
share.multiple = share.uris.size() > 1;
replaceToast(getString(share.multiple ? R.string.preparing_images : R.string.preparing_image));
for (Iterator<Uri> i = share.uris.iterator(); i.hasNext(); i.remove()) {
xmppConnectionService.attachImageToConversation(conversation, i.next(), attachFileCallback);
}
2018-01-01 10:56:31 +00:00
} else {
replaceToast(getString(R.string.preparing_file));
xmppConnectionService.attachFileToConversation(conversation, share.uris.get(0), attachFileCallback);
2014-10-22 16:38:44 +00:00
}
};
if (account.httpUploadAvailable()
&& ((share.image && !neverCompressPictures())
|| conversation.getMode() == Conversation.MODE_MULTI
|| FileBackend.allFilesUnderSize(this, share.uris, max))
&& conversation.getNextEncryption() != Message.ENCRYPTION_OTR) {
callback.onPresenceSelected();
} else {
selectPresence(conversation, callback);
}
2014-10-22 16:38:44 +00:00
} else {
2016-04-14 19:12:44 +00:00
if (mReturnToPrevious && this.share.text != null && !this.share.text.isEmpty() ) {
final OnPresenceSelected callback = new OnPresenceSelected() {
private void finishAndSend(Message message) {
xmppConnectionService.sendMessage(message);
replaceToast(getString(R.string.shared_text_with_x, conversation.getName()));
finish();
}
private UiCallback<Message> messageEncryptionCallback = new UiCallback<Message>() {
@Override
public void success(final Message message) {
message.setEncryption(Message.ENCRYPTION_DECRYPTED);
runOnUiThread(new Runnable() {
@Override
public void run() {
finishAndSend(message);
}
});
}
@Override
public void error(final int errorCode, Message object) {
runOnUiThread(new Runnable() {
@Override
public void run() {
replaceToast(getString(errorCode));
finish();
}
});
}
@Override
public void userInputRequried(PendingIntent pi, Message object) {
finish();
}
};
2016-04-14 19:12:44 +00:00
@Override
public void onPresenceSelected() {
final int encryption = conversation.getNextEncryption();
Message message = new Message(conversation,share.text, encryption);
Log.d(Config.LOGTAG,"on presence selected encrpytion="+encryption);
if (encryption == Message.ENCRYPTION_PGP) {
replaceToast(getString(R.string.encrypting_message));
xmppConnectionService.getPgpEngine().encrypt(message,messageEncryptionCallback);
return;
}
if (encryption == Message.ENCRYPTION_OTR) {
2016-04-14 19:12:44 +00:00
message.setCounterpart(conversation.getNextCounterpart());
}
finishAndSend(message);
2016-04-14 19:12:44 +00:00
}
};
if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) {
selectPresence(conversation, callback);
} else {
callback.onPresenceSelected();
}
} else {
switchToConversation(conversation, this.share.text, true);
}
2014-10-22 16:38:44 +00:00
}
}
public void refreshUiReal() {
xmppConnectionService.populateWithOrderedConversations(mConversations, this.share != null && this.share.uris.size() == 0);
mAdapter.notifyDataSetChanged();
}
@Override
public void onBackPressed() {
if (attachmentCounter.get() >= 1) {
replaceToast(getString(R.string.sharing_files_please_wait));
} else {
super.onBackPressed();
}
}
}