ShareWithActivity: refactor for java 8

This commit is contained in:
Christian Schneppe 2018-01-17 23:54:32 +01:00
parent 92b2b4df3b
commit 017e99d40e

View file

@ -8,9 +8,6 @@ import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
@ -116,13 +113,10 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
@Override @Override
public void error(final int errorCode, Message object) { public void error(final int errorCode, Message object) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override replaceToast(getString(errorCode));
public void run() { if (attachmentCounter.decrementAndGet() <= 0) {
replaceToast(getString(errorCode)); finish();
if (attachmentCounter.decrementAndGet() <= 0) {
finish();
}
} }
}); });
} }
@ -187,13 +181,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
mListView = findViewById(R.id.choose_conversation_list); mListView = findViewById(R.id.choose_conversation_list);
mAdapter = new ConversationAdapter(this, this.mConversations); mAdapter = new ConversationAdapter(this, this.mConversations);
mListView.setAdapter(mAdapter); mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new OnItemClickListener() { mListView.setOnItemClickListener((arg0, arg1, position, arg3) -> share(mConversations.get(position)));
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
share(mConversations.get(position));
}
});
if (savedInstanceState != null) { if (savedInstanceState != null) {
SharingIsRunning = savedInstanceState.getBoolean(STATE_SHARING_IS_RUNNING, false); SharingIsRunning = savedInstanceState.getBoolean(STATE_SHARING_IS_RUNNING, false);
@ -351,26 +339,20 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
return; return;
} }
if (share.uris.size() != 0) { if (share.uris.size() != 0) {
OnPresenceSelected callback = new OnPresenceSelected() { OnPresenceSelected callback = () -> {
@Override attachmentCounter.set(share.uris.size());
public void onPresenceSelected() { if (share.image) {
attachmentCounter.set(share.uris.size()); Log.d(Config.LOGTAG, "ShareWithActivity share() image " + share.uris.size() + " uri(s) " + share.uris.toString());
if (share.image) { share.multiple = share.uris.size() > 1;
Log.d(Config.LOGTAG, "ShareWithActivity share() image " + share.uris.size() + " uri(s) " + share.uris.toString()); replaceToast(getString(share.multiple ? R.string.preparing_images : R.string.preparing_image));
share.multiple = share.uris.size() > 1; for (Iterator<Uri> i = share.uris.iterator(); i.hasNext(); i.remove()) {
replaceToast(getString(share.multiple ? R.string.preparing_images : R.string.preparing_image)); ShareWithActivity.this.xmppConnectionService.attachImageToConversation(conversation, i.next(), attachFileCallback);
for (Iterator<Uri> i = share.uris.iterator(); i.hasNext(); i.remove()) {
ShareWithActivity.this.xmppConnectionService
.attachImageToConversation(conversation, i.next(),
attachFileCallback);
}
} else {
Log.d(Config.LOGTAG, "ShareWithActivity share() file " + share.uris.size() + " uri(s) " + share.uris.toString());
replaceToast(getString(R.string.preparing_file));
ShareWithActivity.this.xmppConnectionService
.attachFileToConversation(conversation, share.uris.get(0), attachFileCallback);
finish();
} }
} else {
Log.d(Config.LOGTAG, "ShareWithActivity share() file " + share.uris.size() + " uri(s) " + share.uris.toString());
replaceToast(getString(R.string.preparing_file));
ShareWithActivity.this.xmppConnectionService.attachFileToConversation(conversation, share.uris.get(0), attachFileCallback);
finish();
} }
}; };
if (account.httpUploadAvailable() if (account.httpUploadAvailable()
@ -395,22 +377,14 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
@Override @Override
public void success(final Message message) { public void success(final Message message) {
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
runOnUiThread(new Runnable() { runOnUiThread(() -> finishAndSend(message));
@Override
public void run() {
finishAndSend(message);
}
});
} }
@Override @Override
public void error(final int errorCode, Message object) { public void error(final int errorCode, Message object) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override replaceToast(getString(errorCode));
public void run() { finish();
replaceToast(getString(errorCode));
finish();
}
}); });
} }
@ -445,17 +419,14 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
callback.onPresenceSelected(); callback.onPresenceSelected();
} }
} else { } else {
final OnPresenceSelected callback = new OnPresenceSelected() { final OnPresenceSelected callback = () -> {
@Override Message message = new Message(conversation, share.text, conversation.getNextEncryption());
public void onPresenceSelected() { if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) {
Message message = new Message(conversation, share.text, conversation.getNextEncryption()); message.setCounterpart(conversation.getNextCounterpart());
if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) {
message.setCounterpart(conversation.getNextCounterpart());
}
xmppConnectionService.sendMessage(message);
replaceToast(getString(R.string.shared_text_with_x, conversation.getName()));
switchToConversation(message.getConversation());
} }
xmppConnectionService.sendMessage(message);
replaceToast(getString(R.string.shared_text_with_x, conversation.getName()));
switchToConversation(message.getConversation());
}; };
if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) { if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) {
selectPresence(conversation, callback); selectPresence(conversation, callback);