diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-03-22 20:31:43 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-03-22 20:31:43 +0100 |
commit | f4dd056ddc008d65500f0d72d908a17df1ef12e0 (patch) | |
tree | 8d9c40fad1a21dc037ec5e4b2927747f0f956e69 /src/main/java | |
parent | 28c26877fb51a3d45d6248abf51daf765affb7eb (diff) |
refactoring. move functionality from activity to fragment
Diffstat (limited to 'src/main/java')
3 files changed, 132 insertions, 110 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/ConversationActivity.java b/src/main/java/de/pixart/messenger/ui/ConversationActivity.java index dbfc61a0c..bf049fe70 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationActivity.java @@ -80,6 +80,7 @@ import de.pixart.messenger.services.XmppConnectionService.OnRosterUpdate; import de.pixart.messenger.ui.adapter.ConversationAdapter; import de.pixart.messenger.utils.ExceptionHelper; import de.pixart.messenger.utils.FileUtils; +import de.pixart.messenger.utils.SendButtonAction; import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.xmpp.OnUpdateBlocklist; import de.pixart.messenger.xmpp.XmppConnection; @@ -97,6 +98,7 @@ public class ConversationActivity extends XmppActivity public static final String ACTION_DESTROY_MUC = "de.pixart.messenger.DESTROY_MUC"; public static final String CONVERSATION = "conversationUuid"; public static final String EXTRA_DOWNLOAD_UUID = "de.pixart.messenger.download_uuid"; + public static final String RECENTLY_USED_QUICK_ACTION = "recently_used_quick_action"; public static final String TEXT = "text"; public static final String NICK = "nick"; public static final String PRIVATE_MESSAGE = "pm"; @@ -678,19 +680,12 @@ public class ConversationActivity extends XmppActivity return; } } - switch (attachmentChoice) { - case ATTACHMENT_CHOICE_LOCATION: - getPreferences().edit().putString("recently_used_quick_action", "location").apply(); - break; - case ATTACHMENT_CHOICE_RECORD_VOICE: - getPreferences().edit().putString("recently_used_quick_action", "voice").apply(); - break; - case ATTACHMENT_CHOICE_TAKE_FROM_CAMERA: - getPreferences().edit().putString("recently_used_quick_action", "photo").apply(); - break; - case ATTACHMENT_CHOICE_CHOOSE_IMAGE: - getPreferences().edit().putString("recently_used_quick_action", "picture").apply(); - break; + try { + getPreferences().edit() + .putString(RECENTLY_USED_QUICK_ACTION, SendButtonAction.of(attachmentChoice).toString()) + .apply(); + } catch (IllegalArgumentException e) { + //just do not save } final Conversation conversation = getSelectedConversation(); final int encryption = conversation.getNextEncryption(); @@ -806,19 +801,11 @@ public class ConversationActivity extends XmppActivity handleEncryptionSelection(item); break; case R.id.attach_choose_picture: - attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE); - break; case R.id.attach_take_picture: - attachFile(ATTACHMENT_CHOICE_TAKE_FROM_CAMERA); - break; case R.id.attach_choose_file: - attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE); - break; case R.id.attach_record_voice: - attachFile(ATTACHMENT_CHOICE_RECORD_VOICE); - break; case R.id.attach_location: - attachFile(ATTACHMENT_CHOICE_LOCATION); + handleAttachmentSelection(item); break; case R.id.action_archive_chat: this.endConversation(getSelectedConversation()); @@ -898,24 +885,38 @@ public class ConversationActivity extends XmppActivity } builder.setView(dialogView); builder.setNegativeButton(getString(R.string.cancel), null); - builder.setPositiveButton(getString(R.string.delete_messages), - (dialog, which) -> { - ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation); - if (conversation.getMode() == Conversation.MODE_SINGLE) { - if (endConversationCheckBox.isChecked()) { - endConversation(conversation); - } else { - updateConversationList(); - ConversationActivity.this.mConversationFragment.updateMessages(); - } - } else { - updateConversationList(); - ConversationActivity.this.mConversationFragment.updateMessages(); - } - }); + builder.setPositiveButton(getString(R.string.delete_messages), (dialog, which) -> { + ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation); + if (endConversationCheckBox.isChecked()) { + endConversation(conversation); + } else { + updateConversationList(); + ConversationActivity.this.mConversationFragment.updateMessages(); + } + }); builder.create().show(); } + private void handleAttachmentSelection(MenuItem item) { + switch (item.getItemId()) { + case R.id.attach_choose_picture: + attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE); + break; + case R.id.attach_take_picture: + attachFile(ATTACHMENT_CHOICE_TAKE_FROM_CAMERA); + break; + case R.id.attach_choose_file: + attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE); + break; + case R.id.attach_record_voice: + attachFile(ATTACHMENT_CHOICE_RECORD_VOICE); + break; + case R.id.attach_location: + attachFile(ATTACHMENT_CHOICE_LOCATION); + break; + } + } + public void verifyOtrSessionDialog(final Conversation conversation, View view) { if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) { Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show(); @@ -1667,22 +1668,12 @@ public class ConversationActivity extends XmppActivity @Override public void inform(final String text) { hidePrepareFileToast(prepareFileToast); - runOnUiThread(new Runnable() { - @Override - public void run() { - replaceToast(text); - } - }); + runOnUiThread(() -> replaceToast(text)); } @Override public void success(Message message) { - runOnUiThread(new Runnable() { - @Override - public void run() { - hideToast(); - } - }); + runOnUiThread(() -> hideToast()); hidePrepareFileToast(prepareFileToast); xmppConnectionService.sendMessage(message); } @@ -1690,12 +1681,7 @@ public class ConversationActivity extends XmppActivity @Override public void error(final int errorCode, Message message) { hidePrepareFileToast(prepareFileToast); - runOnUiThread(new Runnable() { - @Override - public void run() { - replaceToast(getString(errorCode)); - } - }); + runOnUiThread(() -> replaceToast(getString(errorCode))); } @@ -1901,13 +1887,7 @@ public class ConversationActivity extends XmppActivity private void hidePrepareFileToast(final Toast prepareFileToast) { if (prepareFileToast != null) { - runOnUiThread(new Runnable() { - - @Override - public void run() { - prepareFileToast.cancel(); - } - }); + runOnUiThread(() -> prepareFileToast.cancel()); } } @@ -2067,12 +2047,7 @@ public class ConversationActivity extends XmppActivity @Override public void onShowErrorToast(final int resId) { - runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(ConversationActivity.this, resId, Toast.LENGTH_SHORT).show(); - } - }); + runOnUiThread(() -> Toast.makeText(ConversationActivity.this, resId, Toast.LENGTH_SHORT).show()); } public boolean highlightSelectedConversations() { diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index e4c648e25..c00be0ffb 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -1,7 +1,6 @@ package de.pixart.messenger.ui; import android.app.Activity; -import android.support.v7.app.AlertDialog; import android.app.Fragment; import android.app.PendingIntent; import android.content.ActivityNotFoundException; @@ -14,6 +13,7 @@ import android.os.Handler; import android.os.SystemClock; import android.support.v13.view.inputmethod.InputConnectionCompat; import android.support.v13.view.inputmethod.InputContentInfoCompat; +import android.support.v7.app.AlertDialog; import android.text.Editable; import android.text.InputType; import android.text.TextWatcher; @@ -81,6 +81,7 @@ import de.pixart.messenger.ui.adapter.MessageAdapter; import de.pixart.messenger.ui.widget.EditMessage; import de.pixart.messenger.utils.MessageUtils; import de.pixart.messenger.utils.NickValidityChecker; +import de.pixart.messenger.utils.SendButtonAction; import de.pixart.messenger.utils.StylingHelper; import de.pixart.messenger.utils.UIHelper; import de.pixart.messenger.xmpp.XmppConnection; @@ -407,16 +408,10 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa SendButtonAction action = (SendButtonAction) tag; switch (action) { case TAKE_FROM_CAMERA: - activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_TAKE_FROM_CAMERA); - break; case SEND_LOCATION: - activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_LOCATION); - break; case RECORD_VOICE: - activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_RECORD_VOICE); - break; case CHOOSE_PICTURE: - activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_CHOOSE_IMAGE); + activity.attachFile(action.toChoice()); break; case CANCEL: if (conversation != null) { @@ -1381,26 +1376,14 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa } else { String setting = activity.getPreferences().getString("quick_action", activity.getResources().getString(R.string.quick_action)); if (!setting.equals("none") && UIHelper.receivedLocationQuestion(conversation.getLatestMessage())) { - setting = "location"; - } else if (setting.equals("recent")) { - setting = activity.getPreferences().getString("recently_used_quick_action", "text"); - } - switch (setting) { - case "photo": - action = SendButtonAction.TAKE_FROM_CAMERA; - break; - case "location": - action = SendButtonAction.SEND_LOCATION; - break; - case "voice": - action = SendButtonAction.RECORD_VOICE; - break; - case "picture": - action = SendButtonAction.CHOOSE_PICTURE; - break; - default: - action = SendButtonAction.TEXT; - break; + action = SendButtonAction.SEND_LOCATION; + } else { + if (setting.equals("recent")) { + setting = activity.getPreferences().getString(ConversationActivity.RECENTLY_USED_QUICK_ACTION, SendButtonAction.TEXT.toString()); + action = SendButtonAction.valueOfOrDefault(setting, SendButtonAction.TEXT); + } else { + action = SendButtonAction.valueOfOrDefault(setting, SendButtonAction.TEXT); + } } } } else { @@ -1854,8 +1837,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa } @Override - public void onActivityResult(int requestCode, int resultCode, - final Intent data) { + public void onActivityResult(int requestCode, int resultCode, final Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == ConversationActivity.REQUEST_DECRYPT_PGP) { activity.getSelectedConversation().getAccount().getPgpDecryptionService().continueDecryption(data); @@ -1947,16 +1929,4 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa private boolean messageContainsQuery(Message m, String q) { return m != null && m.getMergedBody().toString().toLowerCase().contains(q.toLowerCase()); } - - enum SendButtonAction { - TEXT, TAKE_FROM_CAMERA, SEND_LOCATION, RECORD_VOICE, CANCEL, CHOOSE_PICTURE; - - public static SendButtonAction valueOfOrDefault(String setting, SendButtonAction text) { - try { - return valueOf(setting); - } catch (IllegalArgumentException e) { - return TEXT; - } - } - } } diff --git a/src/main/java/de/pixart/messenger/utils/SendButtonAction.java b/src/main/java/de/pixart/messenger/utils/SendButtonAction.java new file mode 100644 index 000000000..a3d548c15 --- /dev/null +++ b/src/main/java/de/pixart/messenger/utils/SendButtonAction.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2018, Daniel Gultsch All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors + * may be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package de.pixart.messenger.utils; + +import static de.pixart.messenger.ui.ConversationActivity.ATTACHMENT_CHOICE_CHOOSE_IMAGE; +import static de.pixart.messenger.ui.ConversationActivity.ATTACHMENT_CHOICE_LOCATION; +import static de.pixart.messenger.ui.ConversationActivity.ATTACHMENT_CHOICE_RECORD_VOICE; +import static de.pixart.messenger.ui.ConversationActivity.ATTACHMENT_CHOICE_TAKE_FROM_CAMERA; + +public enum SendButtonAction { + TEXT, TAKE_FROM_CAMERA, SEND_LOCATION, RECORD_VOICE, CANCEL, CHOOSE_PICTURE; + + public static SendButtonAction valueOfOrDefault(String setting, SendButtonAction text) { + try { + return valueOf(setting); + } catch (IllegalArgumentException e) { + return TEXT; + } + } + + public static SendButtonAction of(int attachmentChoice) { + switch (attachmentChoice) { + case ATTACHMENT_CHOICE_LOCATION: + return SEND_LOCATION; + case ATTACHMENT_CHOICE_RECORD_VOICE: + return RECORD_VOICE; + case ATTACHMENT_CHOICE_TAKE_FROM_CAMERA: + return TAKE_FROM_CAMERA; + case ATTACHMENT_CHOICE_CHOOSE_IMAGE: + return CHOOSE_PICTURE; + default: + throw new IllegalArgumentException("Not a known attachment choice"); + } + } + + public int toChoice() { + switch (this) { + case TAKE_FROM_CAMERA: + return ATTACHMENT_CHOICE_TAKE_FROM_CAMERA; + case SEND_LOCATION: + return ATTACHMENT_CHOICE_LOCATION; + case RECORD_VOICE: + return ATTACHMENT_CHOICE_RECORD_VOICE; + case CHOOSE_PICTURE: + return ATTACHMENT_CHOICE_CHOOSE_IMAGE; + default: + return 0; + } + } +}
\ No newline at end of file |