1 package org.hedgewars.hedgeroid.netplay; |
1 package org.hedgewars.hedgeroid.netplay; |
2 |
2 |
3 import java.io.File; |
3 import java.util.ArrayList; |
4 import java.io.FileNotFoundException; |
|
5 import java.util.Collections; |
|
6 |
4 |
7 import org.hedgewars.hedgeroid.R; |
5 import org.hedgewars.hedgeroid.R; |
8 import org.hedgewars.hedgeroid.Utils; |
|
9 import org.hedgewars.hedgeroid.R.layout; |
|
10 |
6 |
11 import com.sun.jna.Library; |
7 import android.content.Context; |
12 import com.sun.jna.Native; |
|
13 import com.sun.jna.Pointer; |
|
14 |
|
15 import android.os.Bundle; |
8 import android.os.Bundle; |
16 import android.os.CountDownTimer; |
9 import android.support.v4.app.Fragment; |
17 import android.app.Activity; |
|
18 import android.support.v4.app.FragmentActivity; |
10 import android.support.v4.app.FragmentActivity; |
19 import android.text.Html; |
11 import android.support.v4.app.FragmentPagerAdapter; |
20 import android.util.Log; |
12 import android.support.v4.view.ViewPager; |
21 import android.view.KeyEvent; |
|
22 import android.view.View; |
13 import android.view.View; |
23 import android.view.View.OnClickListener; |
14 import android.view.ViewGroup; |
24 import android.view.inputmethod.EditorInfo; |
15 import android.widget.TabHost; |
25 import android.widget.Button; |
16 import android.widget.TabWidget; |
26 import android.widget.EditText; |
|
27 import android.widget.TextView; |
|
28 import android.widget.TextView.OnEditorActionListener; |
|
29 |
17 |
30 public class LobbyActivity extends FragmentActivity { |
18 public class LobbyActivity extends FragmentActivity { |
31 static { |
19 TabHost mTabHost; |
32 System.loadLibrary("SDL_net"); |
20 ViewPager mViewPager; |
33 } |
21 TabsAdapter mTabsAdapter; |
34 static final JnaFrontlib FRONTLIB = (JnaFrontlib)Native.loadLibrary("frontlib", JnaFrontlib.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, FrontlibTypeMapper.INSTANCE)); |
|
35 |
22 |
36 TextView textView; |
23 @Override |
37 EditText editText; |
24 protected void onCreate(Bundle savedInstanceState) { |
|
25 super.onCreate(savedInstanceState); |
|
26 |
|
27 setContentView(R.layout.activity_lobby); |
|
28 mTabHost = (TabHost)findViewById(android.R.id.tabhost); |
|
29 if(mTabHost != null) { |
|
30 mTabHost.setup(); |
38 |
31 |
39 boolean disconnected; |
32 mViewPager = (ViewPager)findViewById(R.id.pager); |
40 JnaFrontlib.NetconnPtr netconn; |
|
41 CountDownTimer timer; |
|
42 |
33 |
43 private void commitText() { |
34 mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager); |
44 if(!disconnected && netconn!=null) { |
35 |
45 String text = editText.getText().toString(); |
36 mTabsAdapter.addTab(mTabHost.newTabSpec("roomlist").setIndicator("Rooms"), |
46 editText.setText(""); |
37 RoomlistFragment.class, null); |
47 textView.append(Html.fromHtml("<b>AndroidChatter</b>: " + text + "<br/>")); |
38 mTabsAdapter.addTab(mTabHost.newTabSpec("chat").setIndicator("Chat"), |
48 FRONTLIB.flib_netconn_send_chat(netconn, text); |
39 LobbyChatFragment.class, null); |
|
40 mTabsAdapter.addTab(mTabHost.newTabSpec("players").setIndicator("Players"), |
|
41 PlayerlistFragment.class, null); |
|
42 |
|
43 if (savedInstanceState != null) { |
|
44 mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); |
|
45 } |
|
46 } |
|
47 } |
|
48 |
|
49 /*@Override |
|
50 protected void onCreate(Bundle arg0) { |
|
51 super.onCreate(arg0); |
|
52 setContentView(R.layout.activity_lobby); |
|
53 ViewPager pager = (ViewPager)findViewById(R.id.pager); |
|
54 if(pager != null) { |
|
55 pager.setAdapter(new Adapter(getSupportFragmentManager())); |
49 } |
56 } |
50 } |
57 } |
51 |
58 |
|
59 private static class Adapter extends FragmentPagerAdapter { |
|
60 public Adapter(FragmentManager mgr) { |
|
61 super(mgr); |
|
62 } |
|
63 |
|
64 @Override |
|
65 public int getCount() { |
|
66 return 3; |
|
67 } |
|
68 |
|
69 @Override |
|
70 public Fragment getItem(int arg0) { |
|
71 switch(arg0) { |
|
72 case 0: return new RoomlistFragment(); |
|
73 case 1: return new LobbyChatFragment(); |
|
74 case 2: return new PlayerlistFragment(); |
|
75 default: throw new IndexOutOfBoundsException(); |
|
76 } |
|
77 } |
|
78 }*/ |
|
79 |
52 @Override |
80 @Override |
53 public void onCreate(Bundle savedInstanceState) { |
81 protected void onSaveInstanceState(Bundle outState) { |
54 super.onCreate(savedInstanceState); |
82 super.onSaveInstanceState(outState); |
55 disconnected = false; |
83 if(mTabHost != null) { |
56 setContentView(R.layout.activity_lobby); |
84 outState.putString("tab", mTabHost.getCurrentTabTag()); |
57 textView = (TextView)findViewById(R.id.lobbyConsole); |
85 } |
58 editText = (EditText)findViewById(R.id.lobbyChatInput); |
|
59 |
|
60 editText.setOnEditorActionListener(new OnEditorActionListener() { |
|
61 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
|
62 boolean handled = false; |
|
63 if(actionId == EditorInfo.IME_ACTION_SEND) { |
|
64 commitText(); |
|
65 handled = true; |
|
66 } |
|
67 return handled; |
|
68 } |
|
69 }); |
|
70 |
|
71 FRONTLIB.flib_init(); |
|
72 try { |
|
73 JnaFrontlib.MetaschemePtr scheme = FRONTLIB.flib_metascheme_from_ini(new File(Utils.getDataPathFile(this), "metasettings.ini").getAbsolutePath()); |
|
74 netconn = FRONTLIB.flib_netconn_create("AndroidChatter", scheme, Utils.getDataPathFile(this).getAbsolutePath(), "140.247.62.101", 46631); |
|
75 Log.d("Netconn", "netconn is "+netconn); |
|
76 FRONTLIB.flib_metascheme_release(scheme); |
|
77 } catch (FileNotFoundException e) { |
|
78 throw new RuntimeException(e); |
|
79 } |
|
80 |
|
81 FRONTLIB.flib_netconn_onConnected(netconn, handleNetConnected, null); |
|
82 FRONTLIB.flib_netconn_onDisconnected(netconn, handleNetDisconnect, null); |
|
83 FRONTLIB.flib_netconn_onChat(netconn, handleChat, null); |
|
84 FRONTLIB.flib_netconn_onMessage(netconn, handleMessage, null); |
|
85 timer = new CountDownTimer(100000000, 100) { |
|
86 @Override |
|
87 public void onTick(long millisUntilFinished) { |
|
88 if(!disconnected) { |
|
89 FRONTLIB.flib_netconn_tick(netconn); |
|
90 } |
|
91 } |
|
92 |
|
93 @Override |
|
94 public void onFinish() { |
|
95 } |
|
96 }; |
|
97 timer.start(); |
|
98 } |
86 } |
|
87 |
|
88 /** |
|
89 * This is a helper class that implements the management of tabs and all |
|
90 * details of connecting a ViewPager with associated TabHost. It relies on a |
|
91 * trick. Normally a tab host has a simple API for supplying a View or |
|
92 * Intent that each tab will show. This is not sufficient for switching |
|
93 * between pages. So instead we make the content part of the tab host |
|
94 * 0dp high (it is not shown) and the TabsAdapter supplies its own dummy |
|
95 * view to show as the tab content. It listens to changes in tabs, and takes |
|
96 * care of switch to the correct paged in the ViewPager whenever the selected |
|
97 * tab changes. |
|
98 */ |
|
99 public static class TabsAdapter extends FragmentPagerAdapter |
|
100 implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener { |
|
101 private final Context mContext; |
|
102 private final TabHost mTabHost; |
|
103 private final ViewPager mViewPager; |
|
104 private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>(); |
99 |
105 |
100 @Override |
106 static final class TabInfo { |
101 protected void onPause() { |
107 private final Class<?> clss; |
102 super.onPause(); |
108 private final Bundle args; |
103 FRONTLIB.flib_netconn_send_quit(netconn, "Activity paused"); |
109 |
|
110 TabInfo(Class<?> _class, Bundle _args) { |
|
111 clss = _class; |
|
112 args = _args; |
|
113 } |
|
114 } |
|
115 |
|
116 static class DummyTabFactory implements TabHost.TabContentFactory { |
|
117 private final Context mContext; |
|
118 |
|
119 public DummyTabFactory(Context context) { |
|
120 mContext = context; |
|
121 } |
|
122 |
|
123 public View createTabContent(String tag) { |
|
124 View v = new View(mContext); |
|
125 v.setMinimumWidth(0); |
|
126 v.setMinimumHeight(0); |
|
127 return v; |
|
128 } |
|
129 } |
|
130 |
|
131 public TabsAdapter(FragmentActivity activity, TabHost tabHost, ViewPager pager) { |
|
132 super(activity.getSupportFragmentManager()); |
|
133 mContext = activity; |
|
134 mTabHost = tabHost; |
|
135 mViewPager = pager; |
|
136 mTabHost.setOnTabChangedListener(this); |
|
137 mViewPager.setAdapter(this); |
|
138 mViewPager.setOnPageChangeListener(this); |
|
139 } |
|
140 |
|
141 public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) { |
|
142 tabSpec.setContent(new DummyTabFactory(mContext)); |
|
143 |
|
144 TabInfo info = new TabInfo(clss, args); |
|
145 mTabs.add(info); |
|
146 mTabHost.addTab(tabSpec); |
|
147 notifyDataSetChanged(); |
|
148 } |
|
149 |
|
150 @Override |
|
151 public int getCount() { |
|
152 return mTabs.size(); |
|
153 } |
|
154 |
|
155 @Override |
|
156 public Fragment getItem(int position) { |
|
157 TabInfo info = mTabs.get(position); |
|
158 return Fragment.instantiate(mContext, info.clss.getName(), info.args); |
|
159 } |
|
160 |
|
161 public void onTabChanged(String tabId) { |
|
162 int position = mTabHost.getCurrentTab(); |
|
163 mViewPager.setCurrentItem(position); |
|
164 } |
|
165 |
|
166 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { |
|
167 } |
|
168 |
|
169 public void onPageSelected(int position) { |
|
170 // Unfortunately when TabHost changes the current tab, it kindly |
|
171 // also takes care of putting focus on it when not in touch mode. |
|
172 // The jerk. |
|
173 // This hack tries to prevent this from pulling focus out of our |
|
174 // ViewPager. |
|
175 TabWidget widget = mTabHost.getTabWidget(); |
|
176 int oldFocusability = widget.getDescendantFocusability(); |
|
177 widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); |
|
178 mTabHost.setCurrentTab(position); |
|
179 widget.setDescendantFocusability(oldFocusability); |
|
180 } |
|
181 |
|
182 public void onPageScrollStateChanged(int state) { |
|
183 } |
104 } |
184 } |
105 |
|
106 private JnaFrontlib.VoidCallback handleNetConnected = new JnaFrontlib.VoidCallback() { |
|
107 public void callback(Pointer context) { |
|
108 textView.append("Connected. You can chat now.\n"); |
|
109 } |
|
110 }; |
|
111 |
|
112 private JnaFrontlib.IntStrCallback handleNetDisconnect = new JnaFrontlib.IntStrCallback() { |
|
113 public void callback(Pointer context, int arg1, String arg2) { |
|
114 disconnected = true; |
|
115 timer.cancel(); |
|
116 FRONTLIB.flib_netconn_destroy(netconn); |
|
117 netconn.setPointer(Pointer.NULL); |
|
118 textView.append("You have been disconnected."); |
|
119 } |
|
120 }; |
|
121 |
|
122 private JnaFrontlib.StrStrCallback handleChat = new JnaFrontlib.StrStrCallback() { |
|
123 public void callback(Pointer context, String arg1, String arg2) { |
|
124 textView.append(arg1+": "+arg2+"\n"); |
|
125 } |
|
126 }; |
|
127 |
|
128 private JnaFrontlib.IntStrCallback handleMessage = new JnaFrontlib.IntStrCallback() { |
|
129 public void callback(Pointer context, int arg1, String arg2) { |
|
130 textView.append(Html.fromHtml(arg2+"<br/>")); |
|
131 } |
|
132 }; |
|
133 } |
185 } |