Commit f6e4a957 authored by Grigory Fedorov's avatar Grigory Fedorov

ChatViewerFragment: message context menu called on short click also.

parent df2dd03e
...@@ -31,7 +31,7 @@ import com.xabber.android.data.message.chat.ChatManager; ...@@ -31,7 +31,7 @@ import com.xabber.android.data.message.chat.ChatManager;
import com.xabber.android.ui.adapter.ChatMessageAdapter; import com.xabber.android.ui.adapter.ChatMessageAdapter;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
public class ChatViewerFragment extends Fragment { public class ChatViewerFragment extends Fragment implements AdapterView.OnItemClickListener {
public static final String ARGUMENT_ACCOUNT = "ARGUMENT_ACCOUNT"; public static final String ARGUMENT_ACCOUNT = "ARGUMENT_ACCOUNT";
public static final String ARGUMENT_USER = "ARGUMENT_USER"; public static final String ARGUMENT_USER = "ARGUMENT_USER";
...@@ -74,6 +74,7 @@ public class ChatViewerFragment extends Fragment { ...@@ -74,6 +74,7 @@ public class ChatViewerFragment extends Fragment {
listView = (ListView) view.findViewById(android.R.id.list); listView = (ListView) view.findViewById(android.R.id.list);
listView.setAdapter(chatMessageAdapter); listView.setAdapter(chatMessageAdapter);
listView.setOnItemClickListener(this);
ColorDrawable background = new ColorDrawable(getActivity().getResources().getIntArray( ColorDrawable background = new ColorDrawable(getActivity().getResources().getIntArray(
R.array.account_chat_background)[AccountManager.getInstance().getColorLevel(account)]); R.array.account_chat_background)[AccountManager.getInstance().getColorLevel(account)]);
...@@ -132,8 +133,9 @@ public class ChatViewerFragment extends Fragment { ...@@ -132,8 +133,9 @@ public class ChatViewerFragment extends Fragment {
@Override @Override
public void afterTextChanged(Editable text) { public void afterTextChanged(Editable text) {
if (skipOnTextChanges) if (skipOnTextChanges) {
return; return;
}
ChatStateManager.getInstance().onComposing(account, user, text); ChatStateManager.getInstance().onComposing(account, user, text);
} }
...@@ -147,15 +149,11 @@ public class ChatViewerFragment extends Fragment { ...@@ -147,15 +149,11 @@ public class ChatViewerFragment extends Fragment {
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
((ChatViewer)getActivity()).registerChat(this); ((ChatViewer)getActivity()).registerChat(this);
registerForContextMenu(listView);
restoreInputState(); restoreInputState();
} }
private void restoreInputState() { public void restoreInputState() {
skipOnTextChanges = true; skipOnTextChanges = true;
inputView.setText(ChatManager.getInstance().getTypedMessage(account, user)); inputView.setText(ChatManager.getInstance().getTypedMessage(account, user));
...@@ -163,22 +161,24 @@ public class ChatViewerFragment extends Fragment { ...@@ -163,22 +161,24 @@ public class ChatViewerFragment extends Fragment {
ChatManager.getInstance().getSelectionEnd(account, user)); ChatManager.getInstance().getSelectionEnd(account, user));
skipOnTextChanges = false; skipOnTextChanges = false;
if (!inputView.getText().toString().isEmpty()) {
inputView.requestFocus();
}
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
saveInputState(); saveInputState();
((ChatViewer)getActivity()).unregisterChat(this); ((ChatViewer)getActivity()).unregisterChat(this);
unregisterForContextMenu(listView);
} }
public void saveInputState() { public void saveInputState() {
ChatManager.getInstance().setTyped(account, user, inputView.getText().toString(), ChatManager.getInstance().setTyped(account, user, inputView.getText().toString(),
inputView.getSelectionStart(), inputView.getSelectionEnd()); inputView.getSelectionStart(), inputView.getSelectionEnd());
inputView.clearFocus();
} }
private void sendMessage() { private void sendMessage() {
...@@ -281,14 +281,16 @@ public class ChatViewerFragment extends Fragment { ...@@ -281,14 +281,16 @@ public class ChatViewerFragment extends Fragment {
public void setInputText(String additional) { public void setInputText(String additional) {
String source = inputView.getText().toString(); String source = inputView.getText().toString();
int selection = inputView.getSelectionEnd(); int selection = inputView.getSelectionEnd();
if (selection == -1) if (selection == -1) {
selection = source.length(); selection = source.length();
else if (selection > source.length()) } else if (selection > source.length()) {
selection = source.length(); selection = source.length();
}
String before = source.substring(0, selection); String before = source.substring(0, selection);
String after = source.substring(selection); String after = source.substring(selection);
if (before.length() > 0 && !before.endsWith("\n")) if (before.length() > 0 && !before.endsWith("\n")) {
additional = "\n" + additional; additional = "\n" + additional;
}
inputView.setText(before + additional + after); inputView.setText(before + additional + after);
inputView.setSelection(selection + additional.length()); inputView.setSelection(selection + additional.length());
} }
...@@ -311,4 +313,11 @@ public class ChatViewerFragment extends Fragment { ...@@ -311,4 +313,11 @@ public class ChatViewerFragment extends Fragment {
listView.setSelection(size - 1); listView.setSelection(size - 1);
} }
} }
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
registerForContextMenu(listView);
listView.showContextMenuForChild(view);
unregisterForContextMenu(listView);
}
} }
...@@ -17,7 +17,6 @@ package com.xabber.android.ui.adapter; ...@@ -17,7 +17,6 @@ package com.xabber.android.ui.adapter;
import android.app.Activity; import android.app.Activity;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.CharacterStyle; import android.text.style.CharacterStyle;
import android.text.style.TextAppearanceSpan; import android.text.style.TextAppearanceSpan;
import android.view.View; import android.view.View;
...@@ -200,10 +199,11 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter ...@@ -200,10 +199,11 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
view = activity.getLayoutInflater().inflate(layoutId, parent, false); view = activity.getLayoutInflater().inflate(layoutId, parent, false);
} }
return setUpMessageView(messageItem, view); setUpMessageView(messageItem, view);
return view;
} }
private View setUpMessageView(MessageItem messageItem, View view) { private void setUpMessageView(MessageItem messageItem, View view) {
final boolean incoming = messageItem.isIncoming(); final boolean incoming = messageItem.isIncoming();
...@@ -240,7 +240,6 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter ...@@ -240,7 +240,6 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
textView.setTextAppearance(activity, appearanceStyle); textView.setTextAppearance(activity, appearanceStyle);
textView.getBackground().setAlpha(127); textView.getBackground().setAlpha(127);
textView.setText(builder); textView.setText(builder);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String time = StringUtils.getSmartTimeText(activity, messageItem.getTimestamp()); String time = StringUtils.getSmartTimeText(activity, messageItem.getTimestamp());
...@@ -255,7 +254,6 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter ...@@ -255,7 +254,6 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
if (incoming) { if (incoming) {
setUpAvatar(messageItem, view); setUpAvatar(messageItem, view);
} }
return view;
} }
private void setStatusIcon(MessageItem messageItem, View view) { private void setStatusIcon(MessageItem messageItem, View view) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment