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