|
1 package org.hedgewars.hedgeroid; |
|
2 |
|
3 import org.hedgewars.hedgeroid.R; |
|
4 import org.hedgewars.hedgeroid.netplay.MessageLog; |
|
5 import org.hedgewars.hedgeroid.netplay.Netplay; |
|
6 |
|
7 import android.os.Bundle; |
|
8 import android.support.v4.app.Fragment; |
|
9 import android.view.KeyEvent; |
|
10 import android.view.LayoutInflater; |
|
11 import android.view.View; |
|
12 import android.view.ViewGroup; |
|
13 import android.widget.EditText; |
|
14 import android.widget.ListView; |
|
15 import android.widget.TextView; |
|
16 import android.widget.TextView.OnEditorActionListener; |
|
17 |
|
18 public class ChatFragment extends Fragment { |
|
19 public static final String ARGUMENT_INROOM = "inRoom"; |
|
20 |
|
21 private ChatlogAdapter adapter; |
|
22 private Netplay netconn; |
|
23 private MessageLog messageLog; |
|
24 private boolean inRoom; |
|
25 |
|
26 public void setInRoom(boolean inRoom) { |
|
27 this.inRoom = inRoom; |
|
28 } |
|
29 |
|
30 @Override |
|
31 public void onCreate(Bundle savedInstanceState) { |
|
32 super.onCreate(savedInstanceState); |
|
33 netconn = Netplay.getAppInstance(getActivity().getApplicationContext()); |
|
34 adapter = new ChatlogAdapter(getActivity()); |
|
35 } |
|
36 |
|
37 @Override |
|
38 public void onStart() { |
|
39 super.onStart(); |
|
40 messageLog = inRoom ? netconn.roomChatlog : netconn.lobbyChatlog; |
|
41 adapter.setLog(messageLog.getLog()); |
|
42 messageLog.registerObserver(adapter); |
|
43 } |
|
44 |
|
45 @Override |
|
46 public void onStop() { |
|
47 super.onStop(); |
|
48 messageLog.unregisterObserver(adapter); |
|
49 } |
|
50 |
|
51 @Override |
|
52 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
|
53 Bundle savedInstanceState) { |
|
54 View view = inflater.inflate(R.layout.fragment_chat, container, false); |
|
55 |
|
56 ListView listView = (ListView) view.findViewById(R.id.chatConsole); |
|
57 listView.setAdapter(adapter); |
|
58 listView.setDivider(null); |
|
59 listView.setDividerHeight(0); |
|
60 listView.setVerticalFadingEdgeEnabled(true); |
|
61 |
|
62 EditText editText = (EditText) view.findViewById(R.id.chatInput); |
|
63 editText.setOnEditorActionListener(new ChatSendListener()); |
|
64 |
|
65 return view; |
|
66 } |
|
67 |
|
68 private final class ChatSendListener implements OnEditorActionListener { |
|
69 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
|
70 String text = v.getText().toString(); |
|
71 if(text.length()>0) { |
|
72 v.setText(""); |
|
73 netconn.sendChat(text); |
|
74 } |
|
75 return true; |
|
76 } |
|
77 } |
|
78 } |