diff options
author | Treeki <treeki@gmail.com> | 2014-02-18 02:54:06 +0100 |
---|---|---|
committer | Treeki <treeki@gmail.com> | 2014-02-18 02:54:06 +0100 |
commit | d57224505be243c80e9dd64e00f0a7c3a1d841f0 (patch) | |
tree | fe2b7d5cc9b7ca993b50f526f73bcb8716c0a60e /android/VulpIRC/src/main/java/net/brokenfox/vulpirc/WindowFragment.java | |
parent | 05568c427eff856d3049d4a18a707f1b0b358bd2 (diff) | |
download | bounce4-d57224505be243c80e9dd64e00f0a7c3a1d841f0.tar.gz bounce4-d57224505be243c80e9dd64e00f0a7c3a1d841f0.zip |
add Android client
Diffstat (limited to 'android/VulpIRC/src/main/java/net/brokenfox/vulpirc/WindowFragment.java')
-rw-r--r-- | android/VulpIRC/src/main/java/net/brokenfox/vulpirc/WindowFragment.java | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/WindowFragment.java b/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/WindowFragment.java new file mode 100644 index 0000000..da89e03 --- /dev/null +++ b/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/WindowFragment.java @@ -0,0 +1,149 @@ +package net.brokenfox.vulpirc; + +import android.app.Activity; +import android.content.Context; +import android.support.v4.app.Fragment; +import android.os.Bundle; +import android.text.InputType; +import android.util.Log; +import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.inputmethod.EditorInfo; +import android.widget.*; + +/** + * Created by ninji on 2/3/14. + */ +public class WindowFragment extends Fragment implements WindowData.WindowListener, TextView.OnEditorActionListener { + private MessagesAdapter mMessagesAdapter; + private ListView mMessagesList; + private EditText mInput; + protected WindowData mData = null; + + public WindowFragment() { + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + Context ctx = container.getContext(); + + LinearLayout l = new LinearLayout(ctx); + l.setOrientation(LinearLayout.VERTICAL); + + float density = getResources().getDisplayMetrics().density; + int padding = (int)(4 * density + 0.5f); + + mMessagesList = new ListView(ctx); + mMessagesList.setStackFromBottom(true); + mMessagesList.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_NORMAL); + mMessagesList.setDivider(null); + mMessagesList.setDividerHeight(0); + mMessagesList.setPadding(padding, padding, padding, padding); + + mInput = new EditText(ctx); + mInput.setImeOptions(EditorInfo.IME_ACTION_SEND | EditorInfo.IME_FLAG_NO_FULLSCREEN); + mInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL); + mInput.setOnEditorActionListener(this); + + LinearLayout.LayoutParams mlParams = new LinearLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT); + mlParams.weight = 1; + + LinearLayout.LayoutParams inputParams = new LinearLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + + l.addView(mMessagesList, mlParams); + l.addView(mInput, inputParams); + + return l; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setRetainInstance(true); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + Bundle b = getArguments(); + int winID = b.getInt("winID"); + mData = Connection.get().findWindowByID(winID); + + mMessagesAdapter = new MessagesAdapter(); + mMessagesList.setAdapter(mMessagesAdapter); + + mData.registerListener(this); + } + + @Override + public void onDestroy() { + if (mData != null) + mData.deregisterListener(this); + super.onDestroy(); + } + + + @Override + public void handleMessagesChanged() { + mMessagesAdapter.notifyDataSetChanged(); + } + + @Override + public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { + if (i == EditorInfo.IME_ACTION_SEND) { + sendEnteredThing(); + return true; + } + + return false; + } + + + + private void sendEnteredThing() { + CharSequence text = mInput.getText().toString(); + mInput.getText().clear(); + + mData.sendUserInput(text); + } + + + + private class MessagesAdapter extends BaseAdapter implements ListAdapter { + @Override + public int getCount() { + return mData.messages.size(); + } + + @Override + public Object getItem(int i) { + return mData.messages.get(i); + } + + @Override + public long getItemId(int i) { + return i; + } + + @Override + public View getView(int i, View view, ViewGroup viewGroup) { + TextView tv; + + if (view != null && view instanceof TextView) { + tv = (TextView)view; + } else { + tv = new TextView(viewGroup.getContext()); + } + + tv.setText(mData.messages.get(i)); + return tv; + } + } +}
\ No newline at end of file |