summaryrefslogtreecommitdiff
path: root/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/WindowFragment.java
blob: da89e033604df1d1fae50e7947112583e36258da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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;
		}
	}
}