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