6 |
6 |
7 import android.content.ComponentName; |
7 import android.content.ComponentName; |
8 import android.content.Context; |
8 import android.content.Context; |
9 import android.content.Intent; |
9 import android.content.Intent; |
10 import android.content.ServiceConnection; |
10 import android.content.ServiceConnection; |
11 import android.content.res.Configuration; |
|
12 import android.os.Bundle; |
11 import android.os.Bundle; |
13 import android.os.Handler; |
|
14 import android.os.IBinder; |
12 import android.os.IBinder; |
15 import android.support.v4.app.Fragment; |
13 import android.support.v4.app.Fragment; |
16 import android.text.Spanned; |
|
17 import android.text.method.LinkMovementMethod; |
|
18 import android.util.Log; |
14 import android.util.Log; |
19 import android.view.KeyEvent; |
15 import android.view.KeyEvent; |
20 import android.view.LayoutInflater; |
16 import android.view.LayoutInflater; |
21 import android.view.View; |
17 import android.view.View; |
22 import android.view.ViewGroup; |
18 import android.view.ViewGroup; |
23 import android.view.inputmethod.EditorInfo; |
19 import android.view.inputmethod.EditorInfo; |
|
20 import android.widget.AbsListView; |
|
21 import android.widget.AbsListView.OnScrollListener; |
24 import android.widget.EditText; |
22 import android.widget.EditText; |
25 import android.widget.ScrollView; |
23 import android.widget.ListView; |
26 import android.widget.TextView; |
24 import android.widget.TextView; |
27 import android.widget.TextView.OnEditorActionListener; |
25 import android.widget.TextView.OnEditorActionListener; |
28 |
26 |
29 public class LobbyChatFragment extends Fragment { |
27 public class LobbyChatFragment extends Fragment { |
30 private TextView textView; |
|
31 private EditText editText; |
28 private EditText editText; |
32 private ScrollView scrollView; |
29 private ListView listView; |
|
30 private ChatlogAdapter adapter; |
33 private Netconn netconn; |
31 private Netconn netconn; |
34 |
32 |
35 private void scrollDown() { |
|
36 scrollView.post(new Runnable() { |
|
37 public void run() { |
|
38 scrollView.smoothScrollTo(0, textView.getBottom()); |
|
39 } |
|
40 }); |
|
41 } |
|
42 |
|
43 private void commitText() { |
33 private void commitText() { |
44 if(netconn != null && netconn.isConnected()) { |
34 String text = editText.getText().toString(); |
45 String text = editText.getText().toString(); |
35 if(netconn != null && netconn.isConnected() && text.length()>0) { |
46 editText.setText(""); |
36 editText.setText(""); |
47 netconn.sendChat(text); |
37 netconn.sendChat(text); |
48 } |
38 } |
|
39 } |
|
40 |
|
41 @Override |
|
42 public void onCreate(Bundle savedInstanceState) { |
|
43 super.onCreate(savedInstanceState); |
|
44 adapter = new ChatlogAdapter(getActivity()); |
49 } |
45 } |
50 |
46 |
51 @Override |
47 @Override |
52 public void onStart() { |
48 public void onStart() { |
53 super.onStart(); |
49 super.onStart(); |
54 getActivity().bindService(new Intent(getActivity(), NetplayService.class), serviceConnection, |
50 getActivity().bindService(new Intent(getActivity(), NetplayService.class), serviceConnection, |
55 Context.BIND_AUTO_CREATE); |
51 Context.BIND_AUTO_CREATE); |
56 Log.d("LobbyChatFragment", "started"); |
|
57 } |
52 } |
58 |
53 |
59 @Override |
54 @Override |
60 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
55 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
61 Bundle savedInstanceState) { |
56 Bundle savedInstanceState) { |
62 View view = inflater.inflate(R.layout.lobby_chat_fragment, container, false); |
57 View view = inflater.inflate(R.layout.lobby_chat_fragment, container, false); |
63 textView = (TextView) view.findViewById(R.id.lobbyConsole); |
|
64 editText = (EditText) view.findViewById(R.id.lobbyChatInput); |
58 editText = (EditText) view.findViewById(R.id.lobbyChatInput); |
65 scrollView = (ScrollView) view.findViewById(R.id.lobbyConsoleScroll); |
59 listView = (ListView) view.findViewById(R.id.lobbyConsole); |
66 |
60 |
67 textView.setMovementMethod(LinkMovementMethod.getInstance()); |
61 listView.setAdapter(adapter); |
|
62 listView.setDivider(null); |
|
63 listView.setDividerHeight(0); |
|
64 listView.setVerticalFadingEdgeEnabled(true); |
68 |
65 |
69 editText.setOnEditorActionListener(new OnEditorActionListener() { |
66 editText.setOnEditorActionListener(new OnEditorActionListener() { |
70 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
67 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
71 boolean handled = false; |
68 boolean handled = false; |
72 if(actionId == EditorInfo.IME_ACTION_SEND) { |
69 if(actionId == EditorInfo.IME_ACTION_SEND) { |
88 |
85 |
89 private ServiceConnection serviceConnection = new ServiceConnection() { |
86 private ServiceConnection serviceConnection = new ServiceConnection() { |
90 public void onServiceConnected(ComponentName className, IBinder binder) { |
87 public void onServiceConnected(ComponentName className, IBinder binder) { |
91 Log.d("LobbyChatFragment", "netconn received"); |
88 Log.d("LobbyChatFragment", "netconn received"); |
92 netconn = ((NetplayBinder) binder).getNetconn(); |
89 netconn = ((NetplayBinder) binder).getNetconn(); |
93 netconn.lobbyLog.observe(observer); |
90 adapter.setLog(netconn.lobbyLog.getLog()); |
|
91 netconn.lobbyLog.registerObserver(adapter); |
94 } |
92 } |
95 |
93 |
96 public void onServiceDisconnected(ComponentName className) { |
94 public void onServiceDisconnected(ComponentName className) { |
97 // TODO navigate away |
95 // TODO navigate away |
98 netconn.lobbyLog.unobserve(observer); |
96 netconn.lobbyLog.unregisterObserver(adapter); |
99 netconn = null; |
97 netconn = null; |
100 } |
98 } |
101 }; |
99 }; |
102 |
|
103 private MessageLog.Observer observer = new MessageLog.Observer() { |
|
104 public void textChanged(Spanned text) { |
|
105 if(textView != null) { |
|
106 int overhang = textView.getHeight()-scrollView.getHeight(); |
|
107 boolean followBottom = overhang<=0 || Math.abs(overhang-scrollView.getScrollY())<5; |
|
108 textView.setText(text); |
|
109 if(followBottom) { |
|
110 scrollDown(); |
|
111 } |
|
112 } |
|
113 } |
|
114 }; |
|
115 } |
100 } |