<commit_before>package com.iskrembilen.quasseldroid.gui.fragments;

import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.squareup.otto.Subscribe;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import com.iskrembilen.quasseldroid.Buffer;
import com.iskrembilen.quasseldroid.IrcMessage;
import com.iskrembilen.quasseldroid.IrcMessage.Type;
import com.iskrembilen.quasseldroid.NetworkCollection;
import com.iskrembilen.quasseldroid.Quasseldroid;
import com.iskrembilen.quasseldroid.R;
import com.iskrembilen.quasseldroid.events.BufferOpenedEvent;
import com.iskrembilen.quasseldroid.events.CompleteNickEvent;
import com.iskrembilen.quasseldroid.events.ConnectionChangedEvent;
import com.iskrembilen.quasseldroid.events.GetBacklogEvent;
import com.iskrembilen.quasseldroid.events.ManageChannelEvent;
import com.iskrembilen.quasseldroid.events.ManageChannelEvent.ChannelAction;
import com.iskrembilen.quasseldroid.events.ManageMessageEvent;
import com.iskrembilen.quasseldroid.events.ManageMessageEvent.MessageAction;
import com.iskrembilen.quasseldroid.events.NetworksAvailableEvent;
import com.iskrembilen.quasseldroid.events.SendMessageEvent;
import com.iskrembilen.quasseldroid.events.UpdateReadBufferEvent;
import com.iskrembilen.quasseldroid.util.BusProvider;
import com.iskrembilen.quasseldroid.util.InputHistoryHelper;
import com.iskrembilen.quasseldroid.util.NetsplitHelper;
import com.iskrembilen.quasseldroid.util.NickCompletionHelper;
import com.iskrembilen.quasseldroid.util.SenderColorHelper;
import com.iskrembilen.quasseldroid.util.ThemeUtil;

public class ChatFragment extends Fragment {

    public static final String BUFFER_ID = "bufferid";
    private static final String TAG = ChatFragment.class.getSimpleName();
    private static final String BUFFER_NAME = "buffername";
    SharedPreferences preferences;
    private BacklogAdapter adapter;
    private ListView backlogList;
    private EditText inputField;
    private ImageButton autoCompleteButton;
    private int dynamicBacklogAmount;
    private NickCompletionHelper nickCompletionHelper;
    private int bufferId = -1;
    private boolean connected;
    private NetworkCollection networks;

    public static ChatFragment newInstance() {
        return new ChatFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "Creating fragment");
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        adapter = new BacklogAdapter(getActivity(), null);
        preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        if (savedInstanceState != null && savedInstanceState.containsKey(BUFFER_ID)) {
            bufferId = savedInstanceState.getInt(BUFFER_ID);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_chat, container, false);
        backlogList = (ListView) root.findViewById(R.id.chat_backlog_list_view);
        inputField = (EditText) root.findViewById(R.id.chat_input_view);
        autoCompleteButton = (ImageButton) root.findViewById(R.id.chat_auto_complete_button);

        backlogList.setAdapter(adapter);
        backlogList.setOnScrollListener(new BacklogScrollListener(5));
        backlogList.setSelection(backlogList.getChildCount());
        backlogList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            private int lastItem;
            private long lastTime;

            private static final int DOUBLE_CLICK_TIMESPAN = 200;

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final long currentTime = System.currentTimeMillis();
                if (lastItem==i) {
                    if ((currentTime-lastTime)<DOUBLE_CLICK_TIMESPAN) {
                        IrcMessage msg = adapter.getItem(i);
                        inputField.getText().append(" "+msg.getNick());
                        nickCompletionHelper.completeNick(inputField);
                        lastTime = 0;
                    } else {
                        lastTime = currentTime;
                    }
                } else {
                    lastItem = i;
                    lastTime = currentTime;
                }
            }
        });

        autoCompleteButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                nickCompletionHelper.completeNick(inputField);
            }
        });

        inputField.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getAction() == KeyEvent.ACTION_DOWN &&
                        ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) || (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)))) {
                    String inputText = inputField.getText().toString();

                    if (!"" .equals(inputText.trim())) {
                        BusProvider.getInstance().post(new SendMessageEvent(adapter.buffer.getInfo().id, inputText));
                        InputHistoryHelper.addHistoryEntry(inputText);
                        inputField.setText("");
                        InputHistoryHelper.tempStoreCurrentEntry("");
                    }

                    return true;
                }
                return false;
            }
        });

        return root;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_chat, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_hide_events:
                if (adapter.buffer == null)
                    Toast.makeText(getActivity(), getString(R.string.not_available), Toast.LENGTH_SHORT).show();
                else showHideEventsDialog();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStart() {
        Log.d(TAG, "Starting fragment");
        super.onStart();
        dynamicBacklogAmount = Integer.parseInt(preferences.getString(getString(R.string.preference_dynamic_backlog), "10"));
        autoCompleteButton.setEnabled(false);
        inputField.setEnabled(false);<commit_msg>Reimplemented the onKeyListener in the ChatFragment<commit_after>package com.iskrembilen.quasseldroid.gui.fragments;

import android.app.DialogFragment;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.squareup.otto.Subscribe;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import com.iskrembilen.quasseldroid.Buffer;
import com.iskrembilen.quasseldroid.IrcMessage;
import com.iskrembilen.quasseldroid.IrcMessage.Type;
import com.iskrembilen.quasseldroid.NetworkCollection;
import com.iskrembilen.quasseldroid.Quasseldroid;
import com.iskrembilen.quasseldroid.R;
import com.iskrembilen.quasseldroid.events.BufferOpenedEvent;
import com.iskrembilen.quasseldroid.events.CompleteNickEvent;
import com.iskrembilen.quasseldroid.events.ConnectionChangedEvent;
import com.iskrembilen.quasseldroid.events.GetBacklogEvent;
import com.iskrembilen.quasseldroid.events.ManageChannelEvent;
import com.iskrembilen.quasseldroid.events.ManageChannelEvent.ChannelAction;
import com.iskrembilen.quasseldroid.events.ManageMessageEvent;
import com.iskrembilen.quasseldroid.events.ManageMessageEvent.MessageAction;
import com.iskrembilen.quasseldroid.events.NetworksAvailableEvent;
import com.iskrembilen.quasseldroid.events.SendMessageEvent;
import com.iskrembilen.quasseldroid.events.UpdateReadBufferEvent;
import com.iskrembilen.quasseldroid.util.BusProvider;
import com.iskrembilen.quasseldroid.util.InputHistoryHelper;
import com.iskrembilen.quasseldroid.util.NetsplitHelper;
import com.iskrembilen.quasseldroid.util.NickCompletionHelper;
import com.iskrembilen.quasseldroid.util.SenderColorHelper;
import com.iskrembilen.quasseldroid.util.ThemeUtil;

public class ChatFragment extends Fragment {

    public static final String BUFFER_ID = "bufferid";
    private static final String TAG = ChatFragment.class.getSimpleName();
    private static final String BUFFER_NAME = "buffername";
    SharedPreferences preferences;
    private BacklogAdapter adapter;
    private ListView backlogList;
    private EditText inputField;
    private ImageButton autoCompleteButton;
    private int dynamicBacklogAmount;
    private NickCompletionHelper nickCompletionHelper;
    private int bufferId = -1;
    private boolean connected;
    private NetworkCollection networks;

    public static ChatFragment newInstance() {
        return new ChatFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "Creating fragment");
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        adapter = new BacklogAdapter(getActivity(), null);
        preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        if (savedInstanceState != null && savedInstanceState.containsKey(BUFFER_ID)) {
            bufferId = savedInstanceState.getInt(BUFFER_ID);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_chat, container, false);
        backlogList = (ListView) root.findViewById(R.id.chat_backlog_list_view);
        inputField = (EditText) root.findViewById(R.id.chat_input_view);
        autoCompleteButton = (ImageButton) root.findViewById(R.id.chat_auto_complete_button);

        backlogList.setAdapter(adapter);
        backlogList.setOnScrollListener(new BacklogScrollListener(5));
        backlogList.setSelection(backlogList.getChildCount());
        backlogList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            private int lastItem;
            private long lastTime;

            private static final int DOUBLE_CLICK_TIMESPAN = 200;

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final long currentTime = System.currentTimeMillis();
                if (lastItem==i) {
                    if ((currentTime-lastTime)<DOUBLE_CLICK_TIMESPAN) {
                        IrcMessage msg = adapter.getItem(i);
                        inputField.getText().append(" "+msg.getNick());
                        nickCompletionHelper.completeNick(inputField);
                        lastTime = 0;
                    } else {
                        lastTime = currentTime;
                    }
                } else {
                    lastItem = i;
                    lastTime = currentTime;
                }
            }
        });

        autoCompleteButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                nickCompletionHelper.completeNick(inputField);
            }
        });

        inputField.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getAction() == KeyEvent.ACTION_DOWN &&
                        ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) || (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ENTER)))) {
                    String inputText = inputField.getText().toString();

                    if (!"" .equals(inputText.trim())) {
                        BusProvider.getInstance().post(new SendMessageEvent(adapter.buffer.getInfo().id, inputText));
                        InputHistoryHelper.addHistoryEntry(inputText);
                        inputField.setText("");
                        InputHistoryHelper.tempStoreCurrentEntry("");
                    }

                    return true;
                }
                return false;
            }
        });

        inputField.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_TAB && event.getAction() == KeyEvent.ACTION_DOWN) {
                    onNickComplete();
                    return true;
                }
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP && event.getAction() == KeyEvent.ACTION_DOWN) {
                    EditText text = (EditText) v;
                    InputHistoryHelper.tempStoreCurrentEntry(text.getText().toString());
                    text.setText(InputHistoryHelper.getNextHistoryEntry());
                    text.setSelection(text.getText().length());
                    return true;
                }
                if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && event.getAction() == KeyEvent.ACTION_DOWN) {
                    EditText text = (EditText) v;
                    if (InputHistoryHelper.isViewingHistory()) {
                        // Currently viewing history, so progress back down towards "entry zero"
                        text.setText(InputHistoryHelper.getPreviousHistoryEntry());
                    } else if (!text.getText().toString().equals("")) {
                        // Not viewing history, so push the current input text into the history and clear the input
                        InputHistoryHelper.addHistoryEntry(text.getText().toString());
                        text.setText("");
                    }
                    text.setSelection(text.getText().length());
                    return true;
                }
                return false;
            }
        });

        return root;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_chat, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_hide_events:
                if (adapter.buffer == null)
                    Toast.makeText(getActivity(), getString(R.string.not_available), Toast.LENGTH_SHORT).show();
                else showHideEventsDialog();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStart() {
        Log.d(TAG, "Starting fragment");
        super.onStart();
        dynamicBacklogAmount = Integer.parseInt(preferences.getString(getString(R.string.preference_dynamic_backlog), "10"));
        autoCompleteButton.setEnabled(false);
        inputField.setEnabled(false);
{'fraction_non_alphanumeric': 0.08917125191445913, 'fraction_numerical': 0.0009075954393329173, 'mean_word_length': 2.8935512367491167, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3877100', 'n_tokens_mistral': 4726, 'n_tokens_neox': 4536, 'n_words': 848}