Commit a405f276 authored by Grigory Fedorov's avatar Grigory Fedorov

Chat: work with input text simplified and refactored. Keyboard hided every chat change now.

parent 10a835c5
...@@ -14,20 +14,6 @@ ...@@ -14,20 +14,6 @@
*/ */
package com.xabber.android.data.extension.cs; package com.xabber.android.data.extension.cs;
import java.util.Calendar;
import java.util.Map;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smackx.ChatState;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.ChatStateExtension;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
...@@ -52,6 +38,20 @@ import com.xabber.android.data.roster.RosterManager; ...@@ -52,6 +38,20 @@ import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.receiver.ComposingPausedReceiver; import com.xabber.android.receiver.ComposingPausedReceiver;
import com.xabber.xmpp.address.Jid; import com.xabber.xmpp.address.Jid;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smackx.ChatState;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.packet.ChatStateExtension;
import java.util.Calendar;
import java.util.Map;
/** /**
* Provide information about chat state. * Provide information about chat state.
* *
...@@ -260,7 +260,7 @@ public class ChatStateManager implements OnDisconnectListener, ...@@ -260,7 +260,7 @@ public class ChatStateManager implements OnDisconnectListener,
pauseIntents.put(account, user, pendingIntent); pauseIntents.put(account, user, pendingIntent);
} }
public void onPaused(Intent intent, String account, String user) { public void onPaused(String account, String user) {
if (account == null || user == null) if (account == null || user == null)
return; return;
updateChatState(account, user, ChatState.paused); updateChatState(account, user, ChatState.paused);
......
...@@ -30,8 +30,7 @@ public class ComposingPausedReceiver extends BroadcastReceiver { ...@@ -30,8 +30,7 @@ public class ComposingPausedReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
ChatStateManager.getInstance().onPaused(intent, getAccount(intent), ChatStateManager.getInstance().onPaused(getAccount(intent), getUser(intent));
getUser(intent));
} }
public static Intent createIntent(Context context, String account, public static Intent createIntent(Context context, String account,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
*/ */
package com.xabber.android.ui; package com.xabber.android.ui;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Fragment; import android.app.Fragment;
import android.content.Context; import android.content.Context;
...@@ -24,6 +25,7 @@ import android.support.v4.view.ViewPager; ...@@ -24,6 +25,7 @@ import android.support.v4.view.ViewPager;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView; import android.widget.ImageView;
import com.xabber.android.data.ActivityManager; import com.xabber.android.data.ActivityManager;
...@@ -390,7 +392,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -390,7 +392,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
private void cleatInputText(String account, String user) { private void cleatInputText(String account, String user) {
for (ChatViewerFragment chat : registeredChats) { for (ChatViewerFragment chat : registeredChats) {
if (chat.isEqual(account, user)) { if (chat.isEqual(account, user)) {
chat.clearInputView(); chat.clearInputText();
} }
} }
} }
...@@ -600,6 +602,8 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -600,6 +602,8 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override @Override
public void onPageSelected(int position) { public void onPageSelected(int position) {
hideKeyboard(this);
AbstractChat selectedChat = chatViewerAdapter.getChatByPageNumber(position); AbstractChat selectedChat = chatViewerAdapter.getChatByPageNumber(position);
isChatSelected = selectedChat != null; isChatSelected = selectedChat != null;
...@@ -694,11 +698,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -694,11 +698,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
Fragment currentFragment = chatViewerAdapter.getCurrentFragment(); Fragment currentFragment = chatViewerAdapter.getCurrentFragment();
if (isChatSelected) { if (isChatSelected) {
if (!(currentFragment instanceof ChatViewerFragment)) { if (!(currentFragment instanceof ChatViewerFragment)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
...@@ -762,7 +761,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -762,7 +761,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
} }
@Override @Override
public void onRecentChatSelected(AbstractChat chat) { public void onChatSelected(AbstractChat chat) {
selectPage(chat.getAccount(), chat.getUser(), true); selectPage(chat.getAccount(), chat.getUser(), true);
} }
...@@ -773,4 +772,13 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -773,4 +772,13 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
public ChatViewerAdapter getChatViewerAdapter() { public ChatViewerAdapter getChatViewerAdapter() {
return chatViewerAdapter; return chatViewerAdapter;
} }
public static void hideKeyboard(Activity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
} }
...@@ -14,14 +14,12 @@ import android.view.MenuInflater; ...@@ -14,14 +14,12 @@ import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.SettingsManager; import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.account.AccountManager; import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.extension.cs.ChatStateManager; import com.xabber.android.data.extension.cs.ChatStateManager;
...@@ -96,32 +94,27 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl ...@@ -96,32 +94,27 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
}); });
inputView.setOnKeyListener(new View.OnKeyListener() { inputView.setOnKeyListener(new View.OnKeyListener() {
@Override @Override
public boolean onKey(View view, int keyCode, KeyEvent event) { public boolean onKey(View view, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN if (SettingsManager.chatsSendByEnter()
&& keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN
&& SettingsManager.chatsSendByEnter()) { && keyCode == KeyEvent.KEYCODE_ENTER) {
sendMessage(); sendMessage();
return true; return true;
} }
return false; return false;
} }
}); });
inputView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
inputView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override @Override
public boolean onEditorAction(TextView view, int actionId, public void onFocusChange(View v, boolean hasFocus) {
KeyEvent event) { if (!hasFocus) {
if (actionId == EditorInfo.IME_ACTION_SEND) { ChatStateManager.getInstance().onPaused(account, user);
sendMessage();
return true;
} }
return false;
} }
}); });
inputView.addTextChangedListener(new TextWatcher() { inputView.addTextChangedListener(new TextWatcher() {
@Override @Override
...@@ -134,12 +127,13 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl ...@@ -134,12 +127,13 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
@Override @Override
public void afterTextChanged(Editable text) { public void afterTextChanged(Editable text) {
if (skipOnTextChanges) { LogManager.i(this, "afterTextChanged");
return;
}
ChatStateManager.getInstance().onComposing(account, user, text);
setSendButtonColor(); setSendButtonColor();
if (!skipOnTextChanges) {
ChatStateManager.getInstance().onComposing(account, user, text);
}
} }
}); });
...@@ -178,7 +172,6 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl ...@@ -178,7 +172,6 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
inputView.setText(ChatManager.getInstance().getTypedMessage(account, user)); inputView.setText(ChatManager.getInstance().getTypedMessage(account, user));
inputView.setSelection(ChatManager.getInstance().getSelectionStart(account, user), inputView.setSelection(ChatManager.getInstance().getSelectionStart(account, user),
ChatManager.getInstance().getSelectionEnd(account, user)); ChatManager.getInstance().getSelectionEnd(account, user));
setSendButtonColor();
skipOnTextChanges = false; skipOnTextChanges = false;
...@@ -197,31 +190,16 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl ...@@ -197,31 +190,16 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
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() {
String text = inputView.getText().toString(); String text = inputView.getText().toString().trim();
int start = 0;
int end = text.length();
while (start < end && (text.charAt(start) == ' ' || text.charAt(start) == '\n')) { if (text.isEmpty()) {
start += 1;
}
while (start < end && (text.charAt(end - 1) == ' ' || text.charAt(end - 1) == '\n')) {
end -= 1;
}
text = text.substring(start, end);
if ("".equals(text)) {
return; return;
} }
skipOnTextChanges = true; clearInputText();
inputView.getText().clear();
setSendButtonColor();
skipOnTextChanges = false;
sendMessage(text); sendMessage(text);
...@@ -230,8 +208,7 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl ...@@ -230,8 +208,7 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
if (SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.always if (SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.always
|| (getActivity().getResources().getBoolean(R.bool.landscape) || (getActivity().getResources().getBoolean(R.bool.landscape)
&& SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.landscape)) { && SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.landscape)) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); ChatViewer.hideKeyboard(getActivity());
imm.hideSoftInputFromWindow(inputView.getWindowToken(), 0);
} }
} }
...@@ -300,22 +277,10 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl ...@@ -300,22 +277,10 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
} }
public void setInputText(String additional) { public void setInputText(String additional) {
String source = inputView.getText().toString(); skipOnTextChanges = true;
int selection = inputView.getSelectionEnd(); inputView.setText(additional);
if (selection == -1) { inputView.setSelection(additional.length());
selection = source.length(); skipOnTextChanges = false;
} else if (selection > source.length()) {
selection = source.length();
}
String before = source.substring(0, selection);
String after = source.substring(selection);
if (before.length() > 0 && !before.endsWith("\n")) {
additional = "\n" + additional;
}
inputView.setText(before + additional + after);
inputView.setSelection(selection + additional.length());
setSendButtonColor();
} }
public String getAccount() { public String getAccount() {
...@@ -326,8 +291,10 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl ...@@ -326,8 +291,10 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
return user; return user;
} }
public void clearInputView() { public void clearInputText() {
skipOnTextChanges = true;
inputView.getText().clear(); inputView.getText().clear();
skipOnTextChanges = false;
} }
public void scrollChat() { public void scrollChat() {
......
...@@ -92,12 +92,12 @@ public class RecentChatFragment extends ListFragment { ...@@ -92,12 +92,12 @@ public class RecentChatFragment extends ListFragment {
super.onListItemClick(l, v, position, id); super.onListItemClick(l, v, position, id);
if (null != listener) { if (null != listener) {
listener.onRecentChatSelected((AbstractChat) getListAdapter().getItem(position)); listener.onChatSelected((AbstractChat) getListAdapter().getItem(position));
} }
} }
public interface RecentChatFragmentInteractionListener { public interface RecentChatFragmentInteractionListener {
public void onRecentChatSelected(AbstractChat chat); public void onChatSelected(AbstractChat chat);
} }
public void updateChats(List<AbstractChat> chats) { public void updateChats(List<AbstractChat> chats) {
......
...@@ -184,6 +184,10 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter { ...@@ -184,6 +184,10 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter {
public void setPrimaryItem(ViewGroup container, int position, Object object) { public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object); super.setPrimaryItem(container, position, object);
if (currentFragment instanceof ChatViewerFragment) {
((ChatViewerFragment)currentFragment).saveInputState();
}
currentFragment = (Fragment) object; currentFragment = (Fragment) object;
} }
......
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