|
1 package org.hedgewars.hedgeroid.netplay; |
|
2 |
|
3 import java.io.File; |
|
4 import java.io.FileNotFoundException; |
|
5 import java.util.Collections; |
|
6 |
|
7 import org.hedgewars.hedgeroid.R; |
|
8 import org.hedgewars.hedgeroid.Utils; |
|
9 import org.hedgewars.hedgeroid.R.layout; |
|
10 |
|
11 import com.sun.jna.Library; |
|
12 import com.sun.jna.Native; |
|
13 import com.sun.jna.Pointer; |
|
14 |
|
15 import android.os.Bundle; |
|
16 import android.os.CountDownTimer; |
|
17 import android.app.Activity; |
|
18 import android.util.Log; |
|
19 import android.view.KeyEvent; |
|
20 import android.view.View; |
|
21 import android.view.View.OnClickListener; |
|
22 import android.view.inputmethod.EditorInfo; |
|
23 import android.widget.Button; |
|
24 import android.widget.EditText; |
|
25 import android.widget.TextView; |
|
26 import android.widget.TextView.OnEditorActionListener; |
|
27 |
|
28 public class LobbyActivity extends Activity { |
|
29 static { |
|
30 System.loadLibrary("SDL_net"); |
|
31 } |
|
32 static final JnaFrontlib FRONTLIB = (JnaFrontlib)Native.loadLibrary("frontlib", JnaFrontlib.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, FrontlibTypeMapper.INSTANCE)); |
|
33 |
|
34 TextView textView; |
|
35 EditText editText; |
|
36 Button button; |
|
37 |
|
38 boolean disconnected; |
|
39 JnaFrontlib.NetconnPtr netconn; |
|
40 CountDownTimer timer; |
|
41 |
|
42 private void commitText() { |
|
43 if(!disconnected && netconn!=null) { |
|
44 String text = editText.getText().toString(); |
|
45 editText.setText(""); |
|
46 textView.append("AndroidChatter: " + text + "\n"); |
|
47 FRONTLIB.flib_netconn_send_chat(netconn, text); |
|
48 } |
|
49 } |
|
50 |
|
51 @Override |
|
52 public void onCreate(Bundle savedInstanceState) { |
|
53 super.onCreate(savedInstanceState); |
|
54 disconnected = false; |
|
55 setContentView(R.layout.activity_lobby); |
|
56 textView = (TextView)findViewById(R.id.textbox); |
|
57 editText = (EditText)findViewById(R.id.editText); |
|
58 button = (Button)findViewById(R.id.lobbySendButton); |
|
59 |
|
60 button.setOnClickListener(new OnClickListener() { |
|
61 public void onClick(View v) { |
|
62 commitText(); |
|
63 } |
|
64 }); |
|
65 |
|
66 FRONTLIB.flib_init(); |
|
67 try { |
|
68 JnaFrontlib.MetaschemePtr scheme = FRONTLIB.flib_metascheme_from_ini(new File(Utils.getDataPathFile(this), "metasettings.ini").getAbsolutePath()); |
|
69 netconn = FRONTLIB.flib_netconn_create("AndroidChatter", scheme, Utils.getDataPathFile(this).getAbsolutePath(), "140.247.62.101", 46631); |
|
70 Log.d("Netconn", "netconn is "+netconn); |
|
71 FRONTLIB.flib_metascheme_release(scheme); |
|
72 } catch (FileNotFoundException e) { |
|
73 throw new RuntimeException(e); |
|
74 } |
|
75 |
|
76 FRONTLIB.flib_netconn_onConnected(netconn, handleNetConnected, null); |
|
77 FRONTLIB.flib_netconn_onDisconnected(netconn, handleNetDisconnect, null); |
|
78 FRONTLIB.flib_netconn_onChat(netconn, handleChat, null); |
|
79 FRONTLIB.flib_netconn_onMessage(netconn, handleMessage, null); |
|
80 timer = new CountDownTimer(100000000, 100) { |
|
81 @Override |
|
82 public void onTick(long millisUntilFinished) { |
|
83 if(!disconnected) { |
|
84 FRONTLIB.flib_netconn_tick(netconn); |
|
85 } |
|
86 } |
|
87 |
|
88 @Override |
|
89 public void onFinish() { |
|
90 } |
|
91 }; |
|
92 timer.start(); |
|
93 } |
|
94 |
|
95 @Override |
|
96 protected void onPause() { |
|
97 super.onPause(); |
|
98 FRONTLIB.flib_netconn_send_quit(netconn, "Activity paused"); |
|
99 } |
|
100 |
|
101 private JnaFrontlib.VoidCallback handleNetConnected = new JnaFrontlib.VoidCallback() { |
|
102 public void callback(Pointer context) { |
|
103 textView.append("Connected. You can chat now.\n"); |
|
104 } |
|
105 }; |
|
106 |
|
107 private JnaFrontlib.IntStrCallback handleNetDisconnect = new JnaFrontlib.IntStrCallback() { |
|
108 public void callback(Pointer context, int arg1, String arg2) { |
|
109 disconnected = true; |
|
110 timer.cancel(); |
|
111 FRONTLIB.flib_netconn_destroy(netconn); |
|
112 netconn.setPointer(Pointer.NULL); |
|
113 textView.append("You have been disconnected."); |
|
114 } |
|
115 }; |
|
116 |
|
117 private JnaFrontlib.StrStrCallback handleChat = new JnaFrontlib.StrStrCallback() { |
|
118 public void callback(Pointer context, String arg1, String arg2) { |
|
119 textView.append(arg1+": "+arg2+"\n"); |
|
120 } |
|
121 }; |
|
122 |
|
123 private JnaFrontlib.IntStrCallback handleMessage = new JnaFrontlib.IntStrCallback() { |
|
124 public void callback(Pointer context, int arg1, String arg2) { |
|
125 textView.append(arg2+"\n"); |
|
126 } |
|
127 }; |
|
128 } |