1 package org.hedgewars.hedgeroid.netplay; |
1 package org.hedgewars.hedgeroid.netplay; |
2 |
2 |
3 import java.util.ArrayList; |
3 import org.hedgewars.hedgeroid.R; |
|
4 import org.hedgewars.hedgeroid.netplay.NetplayService.NetplayBinder; |
4 |
5 |
5 import org.hedgewars.hedgeroid.R; |
6 import android.content.BroadcastReceiver; |
6 |
7 import android.content.ComponentName; |
7 import android.content.Context; |
8 import android.content.Context; |
|
9 import android.content.Intent; |
|
10 import android.content.IntentFilter; |
|
11 import android.content.ServiceConnection; |
|
12 import android.graphics.drawable.Drawable; |
8 import android.os.Bundle; |
13 import android.os.Bundle; |
9 import android.support.v4.app.Fragment; |
14 import android.os.IBinder; |
10 import android.support.v4.app.FragmentActivity; |
15 import android.support.v4.app.FragmentActivity; |
11 import android.support.v4.app.FragmentPagerAdapter; |
16 import android.support.v4.content.LocalBroadcastManager; |
12 import android.support.v4.view.ViewPager; |
17 import android.view.LayoutInflater; |
|
18 import android.view.Menu; |
|
19 import android.view.MenuItem; |
13 import android.view.View; |
20 import android.view.View; |
14 import android.view.ViewGroup; |
21 import android.widget.ImageView; |
|
22 import android.widget.LinearLayout; |
15 import android.widget.TabHost; |
23 import android.widget.TabHost; |
16 import android.widget.TabWidget; |
24 import android.widget.TextView; |
|
25 import android.widget.Toast; |
17 |
26 |
18 public class LobbyActivity extends FragmentActivity { |
27 public class LobbyActivity extends FragmentActivity { |
19 TabHost mTabHost; |
28 private TabHost tabHost; |
20 ViewPager mViewPager; |
29 private NetplayService service; |
21 TabsAdapter mTabsAdapter; |
30 |
22 |
31 private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() { |
|
32 @Override |
|
33 public void onReceive(Context context, Intent intent) { |
|
34 String message = intent.getStringExtra(NetplayService.EXTRA_MESSAGE); |
|
35 Toast.makeText(getApplicationContext(), "Disconnected: "+message, Toast.LENGTH_LONG).show(); |
|
36 finish(); |
|
37 } |
|
38 }; |
|
39 |
23 @Override |
40 @Override |
24 protected void onCreate(Bundle savedInstanceState) { |
41 protected void onCreate(Bundle icicle) { |
25 super.onCreate(savedInstanceState); |
42 super.onCreate(icicle); |
|
43 LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(disconnectReceiver, new IntentFilter(NetplayService.ACTION_DISCONNECTED)); |
|
44 bindService(new Intent(this, NetplayService.class), serviceConnection, 0); |
26 |
45 |
27 setContentView(R.layout.activity_lobby); |
46 setContentView(R.layout.activity_lobby); |
28 mTabHost = (TabHost)findViewById(android.R.id.tabhost); |
47 tabHost = (TabHost)findViewById(android.R.id.tabhost); |
29 if(mTabHost != null) { |
48 if(tabHost != null) { |
30 mTabHost.setup(); |
49 tabHost.setup(); |
|
50 tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL); |
|
51 |
|
52 tabHost.addTab(tabHost.newTabSpec("rooms").setIndicator(createIndicatorView(tabHost, "Rooms", null)).setContent(R.id.roomListFragment)); |
|
53 tabHost.addTab(tabHost.newTabSpec("chat").setIndicator(createIndicatorView(tabHost, "Chat", null)).setContent(R.id.chatFragment)); |
|
54 tabHost.addTab(tabHost.newTabSpec("players").setIndicator(createIndicatorView(tabHost, "Players", null)).setContent(R.id.playerListFragment)); |
31 |
55 |
32 mViewPager = (ViewPager)findViewById(R.id.pager); |
56 if (icicle != null) { |
33 |
57 tabHost.setCurrentTabByTag(icicle.getString("currentTab")); |
34 mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager); |
|
35 |
|
36 mTabsAdapter.addTab(mTabHost.newTabSpec("roomlist").setIndicator("Rooms"), |
|
37 RoomlistFragment.class, null); |
|
38 mTabsAdapter.addTab(mTabHost.newTabSpec("chat").setIndicator("Chat"), |
|
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 } |
58 } |
46 } |
59 } |
47 } |
60 } |
|
61 |
|
62 @Override |
|
63 protected void onDestroy() { |
|
64 super.onDestroy(); |
|
65 LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(disconnectReceiver); |
|
66 unbindService(serviceConnection); |
|
67 } |
|
68 |
|
69 private View createIndicatorView(TabHost tabHost, CharSequence label, Drawable icon) { |
|
70 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
71 |
|
72 View tabIndicator = inflater.inflate(R.layout.tab_indicator, |
|
73 tabHost.getTabWidget(), // tab widget is the parent |
|
74 false); // no inflate params |
|
75 |
|
76 final TextView tv = (TextView) tabIndicator.findViewById(R.id.title); |
|
77 tv.setText(label); |
|
78 |
|
79 if(icon != null) { |
|
80 final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon); |
|
81 iconView.setImageDrawable(icon); |
|
82 } |
|
83 |
|
84 return tabIndicator; |
|
85 } |
|
86 |
|
87 |
|
88 @Override |
|
89 public boolean onCreateOptionsMenu(Menu menu) { |
|
90 super.onCreateOptionsMenu(menu); |
|
91 getMenuInflater().inflate(R.menu.lobby_options, menu); |
|
92 return true; |
|
93 } |
|
94 |
|
95 @Override |
|
96 public boolean onOptionsItemSelected(MenuItem item) { |
|
97 switch(item.getItemId()) { |
|
98 case R.id.room_create: |
|
99 Toast.makeText(this, R.string.not_implemented_yet, Toast.LENGTH_SHORT).show(); |
|
100 return true; |
|
101 case R.id.disconnect: |
|
102 if(service != null && service.isConnected()) { |
|
103 service.disconnect(); |
|
104 } |
|
105 return true; |
|
106 default: |
|
107 return super.onOptionsItemSelected(item); |
|
108 } |
|
109 } |
48 |
110 |
49 /*@Override |
111 /*@Override |
50 protected void onCreate(Bundle arg0) { |
112 protected void onCreate(Bundle arg0) { |
51 super.onCreate(arg0); |
113 super.onCreate(arg0); |
52 setContentView(R.layout.activity_lobby); |
114 setContentView(R.layout.activity_lobby); |
76 } |
138 } |
77 } |
139 } |
78 }*/ |
140 }*/ |
79 |
141 |
80 @Override |
142 @Override |
81 protected void onSaveInstanceState(Bundle outState) { |
143 protected void onSaveInstanceState(Bundle icicle) { |
82 super.onSaveInstanceState(outState); |
144 super.onSaveInstanceState(icicle); |
83 if(mTabHost != null) { |
145 if(tabHost != null) { |
84 outState.putString("tab", mTabHost.getCurrentTabTag()); |
146 icicle.putString("currentTab", tabHost.getCurrentTabTag()); |
85 } |
147 } |
86 } |
148 } |
87 |
149 |
88 /** |
150 private ServiceConnection serviceConnection = new ServiceConnection() { |
89 * This is a helper class that implements the management of tabs and all |
151 public void onServiceConnected(ComponentName className, IBinder binder) { |
90 * details of connecting a ViewPager with associated TabHost. It relies on a |
152 service = ((NetplayBinder) binder).getService(); |
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>(); |
|
105 |
|
106 static final class TabInfo { |
|
107 private final Class<?> clss; |
|
108 private final Bundle args; |
|
109 |
|
110 TabInfo(Class<?> _class, Bundle _args) { |
|
111 clss = _class; |
|
112 args = _args; |
|
113 } |
|
114 } |
153 } |
115 |
154 |
116 static class DummyTabFactory implements TabHost.TabContentFactory { |
155 public void onServiceDisconnected(ComponentName className) { |
117 private final Context mContext; |
156 service = null; |
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 } |
157 } |
130 |
158 }; |
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 } |
|
184 } |
|
185 } |
159 } |