From e45d7bda3886989cf43be557cbb25889477c9068 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Fri, 7 Nov 2014 15:38:20 +0100 Subject: UI code refactoring --- .../conversations/ui/ConversationActivity.java | 93 ++++------ .../conversations/ui/ConversationFragment.java | 197 +++++++++------------ .../conversations/ui/adapter/MessageAdapter.java | 135 +++++++------- 3 files changed, 172 insertions(+), 253 deletions(-) (limited to 'src/main/java/eu') diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 7c4f2cde..bbefd5be 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -15,6 +15,7 @@ import android.os.SystemClock; import android.provider.MediaStore; import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener; +import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; @@ -31,6 +32,7 @@ import android.widget.Toast; import java.util.ArrayList; import java.util.List; +import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Conversation; @@ -72,8 +74,8 @@ public class ConversationActivity extends XmppActivity implements private List conversationList = new ArrayList(); private Conversation selectedConversation = null; private ListView listView; + private ConversationFragment mConversationFragment; - private boolean paneShouldBeOpen = true; private ArrayAdapter listAdapter; private Toast prepareImageToast; @@ -95,10 +97,6 @@ public class ConversationActivity extends XmppActivity implements return this.listView; } - public boolean shouldPaneBeOpen() { - return paneShouldBeOpen; - } - public void showConversationsOverview() { if (mContentView instanceof SlidingPaneLayout) { SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView; @@ -143,10 +141,14 @@ public class ConversationActivity extends XmppActivity implements @Override protected void onCreate(Bundle savedInstanceState) { + Log.d(Config.LOGTAG, "on create"); super.onCreate(savedInstanceState); if (savedInstanceState != null) { + Log.d(Config.LOGTAG, savedInstanceState.toString()); + mOpenConverstaion = savedInstanceState.getString( STATE_OPEN_CONVERSATION, null); + Log.d(Config.LOGTAG, "recovered " + mOpenConverstaion); mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true); String pending = savedInstanceState.getString(STATE_PENDING_URI, null); if (pending != null) { @@ -156,6 +158,11 @@ public class ConversationActivity extends XmppActivity implements setContentView(R.layout.fragment_conversations_overview); + this.mConversationFragment = new ConversationFragment(); + FragmentTransaction transaction = getFragmentManager().beginTransaction(); + transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation"); + transaction.commit(); + listView = (ListView) findViewById(R.id.list); getActionBar().setDisplayHomeAsUpEnabled(false); @@ -168,13 +175,11 @@ public class ConversationActivity extends XmppActivity implements @Override public void onItemClick(AdapterView arg0, View clickedView, int position, long arg3) { - paneShouldBeOpen = false; if (getSelectedConversation() != conversationList.get(position)) { setSelectedConversation(conversationList.get(position)); - swapConversationFragment(); - } else { - hideConversationsOverview(); + ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation()); } + hideConversationsOverview(); } }); mContentView = findViewById(R.id.content_view_spl); @@ -191,7 +196,6 @@ public class ConversationActivity extends XmppActivity implements @Override public void onPanelOpened(View arg0) { - paneShouldBeOpen = true; ActionBar ab = getActionBar(); if (ab != null) { ab.setDisplayHomeAsUpEnabled(false); @@ -209,7 +213,6 @@ public class ConversationActivity extends XmppActivity implements @Override public void onPanelClosed(View arg0) { - paneShouldBeOpen = false; if ((conversationList.size() > 0) && (getSelectedConversation() != null)) { openConversation(getSelectedConversation()); @@ -436,7 +439,6 @@ public class ConversationActivity extends XmppActivity implements public void endConversation(Conversation conversation) { conversation.setStatus(Conversation.STATUS_ARCHIVED); - paneShouldBeOpen = true; showConversationsOverview(); xmppConnectionService.archiveConversation(conversation); if (conversationList.size() > 0) { @@ -608,23 +610,6 @@ public class ConversationActivity extends XmppActivity implements builder.create().show(); } - protected ConversationFragment swapConversationFragment() { - ConversationFragment selectedFragment = new ConversationFragment(); - if (!isFinishing()) { - - FragmentTransaction transaction = getFragmentManager() - .beginTransaction(); - transaction.replace(R.id.selected_conversation, selectedFragment, - "conversation"); - try { - transaction.commitAllowingStateLoss(); - } catch (IllegalStateException e) { - return selectedFragment; - } - } - return selectedFragment; - } - @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { @@ -674,6 +659,7 @@ public class ConversationActivity extends XmppActivity implements public void onSaveInstanceState(Bundle savedInstanceState) { Conversation conversation = getSelectedConversation(); if (conversation != null) { + Log.d(Config.LOGTAG, "saving conversation: " + conversation.getName() + " " + conversation.getUuid()); savedInstanceState.putString(STATE_OPEN_CONVERSATION, conversation.getUuid()); } @@ -700,24 +686,18 @@ public class ConversationActivity extends XmppActivity implements handleViewConversationIntent(getIntent()); setIntent(null); } else if (mOpenConverstaion != null) { + Log.d(Config.LOGTAG, "open conversation: " + mOpenConverstaion); selectConversationByUuid(mOpenConverstaion); - paneShouldBeOpen = mPanelOpen; - if (paneShouldBeOpen) { + if (mPanelOpen) { showConversationsOverview(); } - swapConversationFragment(); + this.mConversationFragment.reInit(getSelectedConversation()); mOpenConverstaion = null; - } else { + } else if (getSelectedConversation() == null) { showConversationsOverview(); - ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager() - .findFragmentByTag("conversation"); - if (selectedFragment != null) { - selectedFragment.onBackendConnected(); - } else { - mPendingImageUri = null; - setSelectedConversation(conversationList.get(0)); - swapConversationFragment(); - } + mPendingImageUri = null; + setSelectedConversation(conversationList.get(0)); + this.mConversationFragment.reInit(getSelectedConversation()); } if (mPendingImageUri != null) { @@ -730,10 +710,12 @@ public class ConversationActivity extends XmppActivity implements private void handleViewConversationIntent(Intent intent) { String uuid = (String) intent.getExtras().get(CONVERSATION); - String text = intent.getExtras().getString(TEXT, null); + String text = intent.getExtras().getString(TEXT, ""); selectConversationByUuid(uuid); - paneShouldBeOpen = false; - swapConversationFragment().setText(text); + this.mConversationFragment.reInit(getSelectedConversation()); + this.mConversationFragment.appendText(text); + hideConversationsOverview(); + openConversation(getSelectedConversation()); } private void selectConversationByUuid(String uuid) { @@ -917,19 +899,12 @@ public class ConversationActivity extends XmppActivity implements @Override public void run() { updateConversationList(); - if (paneShouldBeOpen) { - if (conversationList.size() >= 1) { - swapConversationFragment(); - } else { + if (conversationList.size() == 0) { startActivity(new Intent(getApplicationContext(), StartConversationActivity.class)); finish(); - } - } - ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager() - .findFragmentByTag("conversation"); - if (selectedFragment != null) { - selectedFragment.updateMessages(); + } else { + ConversationActivity.this.mConversationFragment.updateMessages(); } } }); @@ -937,16 +912,12 @@ public class ConversationActivity extends XmppActivity implements @Override public void onRosterUpdate() { - final ConversationFragment fragment = (ConversationFragment) getFragmentManager() - .findFragmentByTag("conversation"); - if (fragment != null) { - runOnUiThread(new Runnable() { + runOnUiThread(new Runnable() { @Override public void run() { - fragment.updateMessages(); + ConversationActivity.this.mConversationFragment.updateMessages(); } }); - } } } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index f72b4d13..a49a70a6 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -9,8 +9,6 @@ import android.content.Intent; import android.content.IntentSender; import android.content.IntentSender.SendIntentException; import android.os.Bundle; -import android.text.Editable; -import android.text.Selection; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Gravity; @@ -60,80 +58,6 @@ import eu.siacs.conversations.utils.UIHelper; public class ConversationFragment extends Fragment { protected Conversation conversation; - protected ListView messagesView; - protected LayoutInflater inflater; - protected List messageList = new ArrayList(); - protected MessageAdapter messageListAdapter; - protected Contact contact; - - protected String queuedPqpMessage = null; - - private EditMessage mEditMessage; - private ImageButton mSendButton; - private String pastedText = null; - private RelativeLayout snackbar; - private TextView snackbarMessage; - private TextView snackbarAction; - - private boolean messagesLoaded = false; - - private IntentSender askForPassphraseIntent = null; - - private ConcurrentLinkedQueue mEncryptedMessages = new ConcurrentLinkedQueue(); - private boolean mDecryptJobRunning = false; - - private OnEditorActionListener mEditorActionListener = new OnEditorActionListener() { - - @Override - public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { - if (actionId == EditorInfo.IME_ACTION_SEND) { - InputMethodManager imm = (InputMethodManager) v.getContext() - .getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(v.getWindowToken(), 0); - sendMessage(); - return true; - } else { - return false; - } - } - }; - - private OnClickListener mSendButtonListener = new OnClickListener() { - - @Override - public void onClick(View v) { - sendMessage(); - } - }; - protected OnClickListener clickToDecryptListener = new OnClickListener() { - - @Override - public void onClick(View v) { - if (activity.hasPgp() && askForPassphraseIntent != null) { - try { - getActivity().startIntentSenderForResult( - askForPassphraseIntent, - ConversationActivity.REQUEST_DECRYPT_PGP, null, 0, - 0, 0); - } catch (SendIntentException e) { - // - } - } - } - }; - - private OnClickListener clickToMuc = new OnClickListener() { - - @Override - public void onClick(View v) { - Intent intent = new Intent(getActivity(), - ConferenceDetailsActivity.class); - intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC); - intent.putExtra("uuid", conversation.getUuid()); - startActivity(intent); - } - }; - private OnClickListener leaveMuc = new OnClickListener() { @Override @@ -141,7 +65,6 @@ public class ConversationFragment extends Fragment { activity.endConversation(conversation); } }; - private OnClickListener joinMuc = new OnClickListener() { @Override @@ -149,7 +72,6 @@ public class ConversationFragment extends Fragment { activity.xmppConnectionService.joinMuc(conversation); } }; - private OnClickListener enterPassword = new OnClickListener() { @Override @@ -169,7 +91,18 @@ public class ConversationFragment extends Fragment { }); } }; - + protected ListView messagesView; + protected LayoutInflater inflater; + protected List messageList = new ArrayList(); + protected MessageAdapter messageListAdapter; + protected Contact contact; + protected String queuedPqpMessage = null; + private EditMessage mEditMessage; + private ImageButton mSendButton; + private RelativeLayout snackbar; + private TextView snackbarMessage; + private TextView snackbarAction; + private boolean messagesLoaded = false; private OnScrollListener mOnScrollListener = new OnScrollListener() { @Override @@ -197,7 +130,58 @@ public class ConversationFragment extends Fragment { } } }; + private IntentSender askForPassphraseIntent = null; + protected OnClickListener clickToDecryptListener = new OnClickListener() { + + @Override + public void onClick(View v) { + if (activity.hasPgp() && askForPassphraseIntent != null) { + try { + getActivity().startIntentSenderForResult( + askForPassphraseIntent, + ConversationActivity.REQUEST_DECRYPT_PGP, null, 0, + 0, 0); + } catch (SendIntentException e) { + // + } + } + } + }; + private ConcurrentLinkedQueue mEncryptedMessages = new ConcurrentLinkedQueue(); + private boolean mDecryptJobRunning = false; + private OnEditorActionListener mEditorActionListener = new OnEditorActionListener() { + + @Override + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + if (actionId == EditorInfo.IME_ACTION_SEND) { + InputMethodManager imm = (InputMethodManager) v.getContext() + .getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(v.getWindowToken(), 0); + sendMessage(); + return true; + } else { + return false; + } + } + }; + private OnClickListener mSendButtonListener = new OnClickListener() { + + @Override + public void onClick(View v) { + sendMessage(); + } + }; + private OnClickListener clickToMuc = new OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(getActivity(), + ConferenceDetailsActivity.class); + intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC); + intent.putExtra("uuid", conversation.getUuid()); + startActivity(intent); + } + }; private ConversationActivity activity; private Message selectedMessage; @@ -466,15 +450,6 @@ public class ConversationFragment extends Fragment { } } - @Override - public void onStart() { - super.onStart(); - this.activity = (ConversationActivity) getActivity(); - if (activity.xmppConnectionServiceBound) { - this.onBackendConnected(); - } - } - @Override public void onStop() { mDecryptJobRunning = false; @@ -484,36 +459,18 @@ public class ConversationFragment extends Fragment { } } - public void onBackendConnected() { - this.activity = (ConversationActivity) getActivity(); - this.conversation = activity.getSelectedConversation(); - if (this.conversation == null) { - return; - } - String oldString = conversation.getNextMessage().trim(); - if (this.pastedText == null) { - this.mEditMessage.setText(oldString); - } else { - - if (oldString.isEmpty()) { - mEditMessage.setText(pastedText); - } else { - mEditMessage.setText(oldString + " " + pastedText); - } - pastedText = null; - } - int position = mEditMessage.length(); - Editable etext = mEditMessage.getText(); - Selection.setSelection(etext, position); - if (activity.isConversationsOverviewHideable()) { - if (!activity.shouldPaneBeOpen()) { - activity.hideConversationsOverview(); - activity.openConversation(conversation); - } + public void reInit(Conversation conversation) { + if (this.conversation != null) { + this.conversation.setNextMessage(mEditMessage.getText().toString()); } + this.activity = (ConversationActivity) getActivity(); + this.conversation = conversation; if (this.conversation.getMode() == Conversation.MODE_MULTI) { - conversation.setNextPresence(null); + this.conversation.setNextPresence(null); } + this.mEditMessage.setText(""); + this.mEditMessage.append(this.conversation.getNextMessage()); + this.messagesView.invalidate(); updateMessages(); } @@ -611,7 +568,7 @@ public class ConversationFragment extends Fragment { } getActivity().invalidateOptionsMenu(); updateChatMsgHint(); - if (!activity.shouldPaneBeOpen()) { + if (!activity.isConversationsOverviewVisable() || !activity.isConversationsOverviewHideable()) { activity.xmppConnectionService.markRead(conversation, true); activity.updateConversationList(); } @@ -888,8 +845,12 @@ public class ConversationFragment extends Fragment { } } - public void setText(String text) { - this.pastedText = text; + public void appendText(String text) { + String previous = this.mEditMessage.getText().toString(); + if (previous.length() != 0 && !previous.endsWith(" ")) { + text = " " + text; + } + this.mEditMessage.append(text); } public void clearInputField() { diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index a24f90d7..da7a548c 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -1,16 +1,5 @@ package eu.siacs.conversations.ui.adapter; -import java.util.List; - -import eu.siacs.conversations.Config; -import eu.siacs.conversations.R; -import eu.siacs.conversations.entities.Contact; -import eu.siacs.conversations.entities.Conversation; -import eu.siacs.conversations.entities.Downloadable; -import eu.siacs.conversations.entities.Message; -import eu.siacs.conversations.entities.Message.ImageParams; -import eu.siacs.conversations.ui.ConversationActivity; -import eu.siacs.conversations.utils.UIHelper; import android.content.Intent; import android.graphics.Typeface; import android.text.Spannable; @@ -19,9 +8,9 @@ import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; import android.util.DisplayMetrics; import android.view.View; -import android.view.ViewGroup; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; +import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ImageView; @@ -29,6 +18,18 @@ import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; +import java.util.List; + +import eu.siacs.conversations.Config; +import eu.siacs.conversations.R; +import eu.siacs.conversations.entities.Contact; +import eu.siacs.conversations.entities.Conversation; +import eu.siacs.conversations.entities.Downloadable; +import eu.siacs.conversations.entities.Message; +import eu.siacs.conversations.entities.Message.ImageParams; +import eu.siacs.conversations.ui.ConversationActivity; +import eu.siacs.conversations.utils.UIHelper; + public class MessageAdapter extends ArrayAdapter { private static final int SENT = 0; @@ -323,10 +324,6 @@ public class MessageAdapter extends ArrayAdapter { .findViewById(R.id.message_box); viewHolder.contact_picture = (ImageView) view .findViewById(R.id.message_photo); - viewHolder.contact_picture.setImageBitmap(activity - .avatarService().get( - item.getConversation().getAccount(), - activity.getPixel(48))); viewHolder.download_button = (Button) view .findViewById(R.id.download_button); viewHolder.indicator = (ImageView) view @@ -350,11 +347,6 @@ public class MessageAdapter extends ArrayAdapter { .findViewById(R.id.message_photo); viewHolder.download_button = (Button) view .findViewById(R.id.download_button); - if (item.getConversation().getMode() == Conversation.MODE_SINGLE) { - viewHolder.contact_picture.setImageBitmap(activity - .avatarService().get(item.getContact(), - activity.getPixel(48))); - } viewHolder.indicator = (ImageView) view .findViewById(R.id.security_indicator); viewHolder.image = (ImageView) view @@ -363,6 +355,8 @@ public class MessageAdapter extends ArrayAdapter { .findViewById(R.id.message_body); viewHolder.time = (TextView) view .findViewById(R.id.message_time); + viewHolder.indicatorReceived = (ImageView) view + .findViewById(R.id.indicator_received); view.setTag(viewHolder); break; case STATUS: @@ -370,30 +364,6 @@ public class MessageAdapter extends ArrayAdapter { R.layout.message_status, parent, false); viewHolder.contact_picture = (ImageView) view .findViewById(R.id.message_photo); - if (item.getConversation().getMode() == Conversation.MODE_SINGLE) { - - viewHolder.contact_picture.setImageBitmap(activity - .avatarService().get( - item.getConversation().getContact(), - activity.getPixel(32))); - viewHolder.contact_picture.setAlpha(0.5f); - viewHolder.contact_picture - .setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - String name = item.getConversation() - .getName(); - String read = getContext() - .getString( - R.string.contact_has_read_up_to_this_point, - name); - Toast.makeText(getContext(), read, - Toast.LENGTH_SHORT).show(); - } - }); - - } break; default: viewHolder = null; @@ -404,9 +374,32 @@ public class MessageAdapter extends ArrayAdapter { } if (type == STATUS) { + if (item.getConversation().getMode() == Conversation.MODE_SINGLE) { + + viewHolder.contact_picture.setImageBitmap(activity + .avatarService().get( + item.getConversation().getContact(), + activity.getPixel(32))); + viewHolder.contact_picture.setAlpha(0.5f); + viewHolder.contact_picture + .setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + String name = item.getConversation() + .getName(); + String read = getContext() + .getString( + R.string.contact_has_read_up_to_this_point, + name); + Toast.makeText(getContext(), read, + Toast.LENGTH_SHORT).show(); + } + }); + + } return view; - } - if (type == NULL) { + } else if (type == NULL) { if (position == getCount() - 1) { view.getLayoutParams().height = 1; } else { @@ -415,6 +408,19 @@ public class MessageAdapter extends ArrayAdapter { } view.setLayoutParams(view.getLayoutParams()); return view; + } else if (type == RECEIVED) { + Contact contact = item.getContact(); + if (contact != null) { + viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(contact, activity.getPixel(48))); + } else if (item.getConversation().getMode() == Conversation.MODE_MULTI) { + String name = item.getPresence(); + if (name == null) { + name = item.getCounterpart(); + } + viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(name, activity.getPixel(48))); + } + } else if (type == SENT) { + viewHolder.contact_picture.setImageBitmap(activity.avatarService().get(item.getConversation().getAccount(), activity.getPixel(48))); } if (viewHolder.contact_picture != null) { @@ -426,7 +432,6 @@ public class MessageAdapter extends ArrayAdapter { if (MessageAdapter.this.mOnContactPictureClickedListener != null) { MessageAdapter.this.mOnContactPictureClickedListener .onContactPictureClicked(item); - ; } } @@ -447,24 +452,6 @@ public class MessageAdapter extends ArrayAdapter { }); } - if (type == RECEIVED) { - if (item.getConversation().getMode() == Conversation.MODE_MULTI) { - Contact contact = item.getContact(); - if (contact != null) { - viewHolder.contact_picture.setImageBitmap(activity - .avatarService() - .get(contact, activity.getPixel(48))); - } else { - String name = item.getPresence(); - if (name == null) { - name = item.getCounterpart(); - } - viewHolder.contact_picture.setImageBitmap(activity - .avatarService().get(name, activity.getPixel(48))); - } - } - } - if (item.getType() == Message.TYPE_IMAGE || item.getDownloadable() != null) { Downloadable d = item.getDownloadable(); @@ -532,6 +519,14 @@ public class MessageAdapter extends ArrayAdapter { } } + public interface OnContactPictureClicked { + public void onContactPictureClicked(Message message); + } + + public interface OnContactPictureLongClicked { + public void onContactPictureLongClicked(Message message); + } + private static class ViewHolder { protected LinearLayout message_box; @@ -544,12 +539,4 @@ public class MessageAdapter extends ArrayAdapter { protected ImageView contact_picture; } - - public interface OnContactPictureClicked { - public void onContactPictureClicked(Message message); - } - - public interface OnContactPictureLongClicked { - public void onContactPictureLongClicked(Message message); - } } -- cgit v1.2.3