|
1 package org.hedgewars.hedgeroid.netplay; |
|
2 |
|
3 import org.hedgewars.hedgeroid.R; |
|
4 |
|
5 import android.os.Bundle; |
|
6 import android.os.Handler; |
|
7 import android.support.v4.app.Fragment; |
|
8 import android.text.Html; |
|
9 import android.util.Log; |
|
10 import android.view.KeyEvent; |
|
11 import android.view.LayoutInflater; |
|
12 import android.view.View; |
|
13 import android.view.ViewGroup; |
|
14 import android.view.inputmethod.EditorInfo; |
|
15 import android.widget.EditText; |
|
16 import android.widget.ScrollView; |
|
17 import android.widget.TextView; |
|
18 import android.widget.TextView.OnEditorActionListener; |
|
19 |
|
20 public class LobbyChatFragment extends Fragment { |
|
21 private TextView textView; |
|
22 private EditText editText; |
|
23 private ScrollView scrollView; |
|
24 |
|
25 private void commitText() { |
|
26 String text = editText.getText().toString(); |
|
27 int overhang = textView.getHeight()-scrollView.getHeight(); |
|
28 boolean followBottom = overhang<=0 || Math.abs(overhang-scrollView.getScrollY())<5; |
|
29 textView.append(Html.fromHtml("<b>Chatter:</b> " + text + "<br/>")); |
|
30 editText.setText(""); |
|
31 if(followBottom) { |
|
32 new Handler().post(new Runnable() { |
|
33 public void run() { |
|
34 scrollView.fullScroll(ScrollView.FOCUS_DOWN); |
|
35 } |
|
36 }); |
|
37 } |
|
38 } |
|
39 /* |
|
40 @Override |
|
41 public void onStart() { |
|
42 super.onStart(); |
|
43 getActivity().bindService(new Intent(getActivity(), NetplayService.class), serviceConnection, |
|
44 Context.BIND_AUTO_CREATE); |
|
45 } |
|
46 */ |
|
47 @Override |
|
48 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
|
49 Bundle savedInstanceState) { |
|
50 View view = inflater.inflate(R.layout.lobby_chat_fragment, container, false); |
|
51 textView = (TextView) view.findViewById(R.id.lobbyConsole); |
|
52 editText = (EditText) view.findViewById(R.id.lobbyChatInput); |
|
53 scrollView = (ScrollView) view.findViewById(R.id.lobbyConsoleScroll); |
|
54 |
|
55 editText.setOnEditorActionListener(new OnEditorActionListener() { |
|
56 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
|
57 boolean handled = false; |
|
58 if(actionId == EditorInfo.IME_ACTION_SEND) { |
|
59 commitText(); |
|
60 handled = true; |
|
61 } |
|
62 return handled; |
|
63 } |
|
64 }); |
|
65 |
|
66 return view; |
|
67 } |
|
68 /* |
|
69 private ServiceConnection serviceConnection = new ServiceConnection() { |
|
70 public void onServiceConnected(ComponentName className, IBinder binder) { |
|
71 netplayService = ((NetplayBinder) binder).getService(); |
|
72 try { |
|
73 netplayService.connect("AndroidChatter"); |
|
74 } catch (IOException e) { |
|
75 throw new RuntimeException(e); |
|
76 } |
|
77 } |
|
78 |
|
79 public void onServiceDisconnected(ComponentName className) { |
|
80 // TODO navigate away |
|
81 netplayService = null; |
|
82 } |
|
83 }; |
|
84 */ |
|
85 } |