|
1 package org.hedgewars.hedgeroid; |
|
2 |
|
3 import org.hedgewars.hedgeroid.R; |
|
4 import org.hedgewars.hedgeroid.NetplayStateFragment.NetplayStateListener; |
|
5 import org.hedgewars.hedgeroid.netplay.Netplay; |
|
6 import org.hedgewars.hedgeroid.netplay.Netplay.State; |
|
7 import org.hedgewars.hedgeroid.util.TextInputDialog; |
|
8 import org.hedgewars.hedgeroid.util.TextInputDialog.TextInputDialogListener; |
|
9 |
|
10 import android.content.Context; |
|
11 import android.content.Intent; |
|
12 import android.graphics.drawable.Drawable; |
|
13 import android.os.Bundle; |
|
14 import android.support.v4.app.FragmentActivity; |
|
15 import android.support.v4.app.FragmentTransaction; |
|
16 import android.view.LayoutInflater; |
|
17 import android.view.Menu; |
|
18 import android.view.MenuItem; |
|
19 import android.view.View; |
|
20 import android.widget.ImageView; |
|
21 import android.widget.LinearLayout; |
|
22 import android.widget.TabHost; |
|
23 import android.widget.TextView; |
|
24 |
|
25 public class LobbyActivity extends FragmentActivity implements TextInputDialogListener, NetplayStateListener { |
|
26 private static final int DIALOG_CREATE_ROOM = 0; |
|
27 |
|
28 private TabHost tabHost; |
|
29 private Netplay netplay; |
|
30 |
|
31 @Override |
|
32 protected void onCreate(Bundle icicle) { |
|
33 super.onCreate(icicle); |
|
34 |
|
35 setContentView(R.layout.activity_lobby); |
|
36 ChatFragment chatFragment = (ChatFragment)getSupportFragmentManager().findFragmentById(R.id.chatFragment); |
|
37 chatFragment.setInRoom(false); |
|
38 |
|
39 FragmentTransaction trans = getSupportFragmentManager().beginTransaction(); |
|
40 trans.add(new NetplayStateFragment(), "netplayFragment"); |
|
41 trans.commit(); |
|
42 |
|
43 netplay = Netplay.getAppInstance(getApplicationContext()); |
|
44 |
|
45 tabHost = (TabHost)findViewById(android.R.id.tabhost); |
|
46 if(tabHost != null) { |
|
47 tabHost.setup(); |
|
48 tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL); |
|
49 |
|
50 tabHost.addTab(tabHost.newTabSpec("rooms").setIndicator(createIndicatorView(tabHost, R.string.lobby_tab_rooms, getResources().getDrawable(R.drawable.roomlist_ingame))).setContent(R.id.roomListFragment)); |
|
51 tabHost.addTab(tabHost.newTabSpec("chat").setIndicator(createIndicatorView(tabHost, R.string.lobby_tab_chat, getResources().getDrawable(R.drawable.edit))).setContent(R.id.chatFragment)); |
|
52 tabHost.addTab(tabHost.newTabSpec("players").setIndicator(createIndicatorView(tabHost, R.string.lobby_tab_players, getResources().getDrawable(R.drawable.human))).setContent(R.id.playerListFragment)); |
|
53 |
|
54 if (icicle != null) { |
|
55 tabHost.setCurrentTabByTag(icicle.getString("currentTab")); |
|
56 } |
|
57 } |
|
58 } |
|
59 |
|
60 private View createIndicatorView(TabHost tabHost, int label, Drawable icon) { |
|
61 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
|
62 |
|
63 View tabIndicator = inflater.inflate(R.layout.tab_indicator, |
|
64 tabHost.getTabWidget(), // tab widget is the parent |
|
65 false); // no inflate params |
|
66 |
|
67 final TextView tv = (TextView) tabIndicator.findViewById(R.id.title); |
|
68 tv.setText(label); |
|
69 |
|
70 if(icon != null) { |
|
71 final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon); |
|
72 iconView.setImageDrawable(icon); |
|
73 } |
|
74 |
|
75 return tabIndicator; |
|
76 } |
|
77 |
|
78 @Override |
|
79 public boolean onCreateOptionsMenu(Menu menu) { |
|
80 super.onCreateOptionsMenu(menu); |
|
81 getMenuInflater().inflate(R.menu.lobby_options, menu); |
|
82 return true; |
|
83 } |
|
84 |
|
85 @Override |
|
86 public boolean onOptionsItemSelected(MenuItem item) { |
|
87 switch(item.getItemId()) { |
|
88 case R.id.room_create: |
|
89 TextInputDialog dialog = new TextInputDialog(DIALOG_CREATE_ROOM, R.string.dialog_create_room_title, 0, R.string.dialog_create_room_hint); |
|
90 dialog.show(getSupportFragmentManager(), "create_room_dialog"); |
|
91 return true; |
|
92 case R.id.disconnect: |
|
93 netplay.disconnect(); |
|
94 return true; |
|
95 default: |
|
96 return super.onOptionsItemSelected(item); |
|
97 } |
|
98 } |
|
99 |
|
100 @Override |
|
101 public void onBackPressed() { |
|
102 super.onBackPressed(); |
|
103 netplay.disconnect(); |
|
104 } |
|
105 |
|
106 @Override |
|
107 protected void onSaveInstanceState(Bundle icicle) { |
|
108 super.onSaveInstanceState(icicle); |
|
109 if(tabHost != null) { |
|
110 icicle.putString("currentTab", tabHost.getCurrentTabTag()); |
|
111 } |
|
112 } |
|
113 |
|
114 public void onTextInputDialogSubmitted(int dialogId, String text) { |
|
115 if(text != null && text.length()>0) { |
|
116 netplay.sendCreateRoom(text); |
|
117 } |
|
118 } |
|
119 |
|
120 public void onTextInputDialogCancelled(int dialogId) { |
|
121 } |
|
122 |
|
123 public void onNetplayStateChanged(State newState) { |
|
124 switch(newState) { |
|
125 case CONNECTING: |
|
126 case NOT_CONNECTED: |
|
127 finish(); |
|
128 break; |
|
129 case ROOM: |
|
130 case INGAME: |
|
131 startActivity(new Intent(getApplicationContext(), RoomActivity.class)); |
|
132 break; |
|
133 case LOBBY: |
|
134 // Do nothing |
|
135 break; |
|
136 default: |
|
137 throw new IllegalStateException("Unknown connection state: "+newState); |
|
138 } |
|
139 } |
|
140 } |