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