project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/LobbyActivity.java
changeset 10017 de822cd3df3a
parent 7586 33924ff4af50
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    36 import android.widget.TabHost;
    36 import android.widget.TabHost;
    37 
    37 
    38 /**
    38 /**
    39  * Activity for the server lobby of a hedgewars server. Allows you to chat, join
    39  * Activity for the server lobby of a hedgewars server. Allows you to chat, join
    40  * and create rooms and interact with a list of players.
    40  * and create rooms and interact with a list of players.
    41  * 
    41  *
    42  * Most of the functionality is handled by various fragments.
    42  * Most of the functionality is handled by various fragments.
    43  */
    43  */
    44 public class LobbyActivity extends FragmentActivity implements TextInputDialogListener, NetplayStateListener {
    44 public class LobbyActivity extends FragmentActivity implements TextInputDialogListener, NetplayStateListener {
    45 	private static final int DIALOG_CREATE_ROOM = 0;
    45     private static final int DIALOG_CREATE_ROOM = 0;
    46 	
    46 
    47     private TabHost tabHost;
    47     private TabHost tabHost;
    48     private Netplay netplay;
    48     private Netplay netplay;
    49     
    49 
    50     @Override
    50     @Override
    51     protected void onCreate(Bundle icicle) {
    51     protected void onCreate(Bundle icicle) {
    52         super.onCreate(icicle);
    52         super.onCreate(icicle);
    53         
    53 
    54         setContentView(R.layout.activity_lobby);
    54         setContentView(R.layout.activity_lobby);
    55         ChatFragment chatFragment = (ChatFragment)getSupportFragmentManager().findFragmentById(R.id.chatFragment);
    55         ChatFragment chatFragment = (ChatFragment)getSupportFragmentManager().findFragmentById(R.id.chatFragment);
    56         chatFragment.setInRoom(false);
    56         chatFragment.setInRoom(false);
    57         
    57 
    58         FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
    58         FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
    59         trans.add(new NetplayStateFragment(), "netplayFragment");
    59         trans.add(new NetplayStateFragment(), "netplayFragment");
    60         trans.commit();
    60         trans.commit();
    61         
    61 
    62         netplay = Netplay.getAppInstance(getApplicationContext());
    62         netplay = Netplay.getAppInstance(getApplicationContext());
    63         
    63 
    64         // Set up a tabbed UI for medium and small screens
    64         // Set up a tabbed UI for medium and small screens
    65         tabHost = (TabHost)findViewById(android.R.id.tabhost);
    65         tabHost = (TabHost)findViewById(android.R.id.tabhost);
    66         if(tabHost != null) {
    66         if(tabHost != null) {
    67 	        tabHost.setup();
    67             tabHost.setup();
    68 	        tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL);
    68             tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL);
    69 
    69 
    70 	        tabHost.addTab(tabHost.newTabSpec("rooms").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_rooms, R.drawable.roomlist_ingame)).setContent(R.id.roomListFragment));
    70             tabHost.addTab(tabHost.newTabSpec("rooms").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_rooms, R.drawable.roomlist_ingame)).setContent(R.id.roomListFragment));
    71 	        tabHost.addTab(tabHost.newTabSpec("chat").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_chat, R.drawable.edit)).setContent(R.id.chatFragment));
    71             tabHost.addTab(tabHost.newTabSpec("chat").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_chat, R.drawable.edit)).setContent(R.id.chatFragment));
    72 	        tabHost.addTab(tabHost.newTabSpec("players").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_players, R.drawable.human)).setContent(R.id.playerListFragment));
    72             tabHost.addTab(tabHost.newTabSpec("players").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_players, R.drawable.human)).setContent(R.id.playerListFragment));
    73 	
    73 
    74 	        if (icicle != null) {
    74             if (icicle != null) {
    75 	            tabHost.setCurrentTabByTag(icicle.getString("currentTab"));
    75                 tabHost.setCurrentTabByTag(icicle.getString("currentTab"));
    76 	        }
    76             }
    77         }
    77         }
    78     }
    78     }
    79     
    79 
    80 	@Override
    80     @Override
    81 	public boolean onCreateOptionsMenu(Menu menu) {
    81     public boolean onCreateOptionsMenu(Menu menu) {
    82 		super.onCreateOptionsMenu(menu);
    82         super.onCreateOptionsMenu(menu);
    83 		getMenuInflater().inflate(R.menu.lobby_options, menu);
    83         getMenuInflater().inflate(R.menu.lobby_options, menu);
    84 		return true;
    84         return true;
    85 	}
    85     }
    86 	
    86 
    87 	@Override
    87     @Override
    88 	public boolean onOptionsItemSelected(MenuItem item) {
    88     public boolean onOptionsItemSelected(MenuItem item) {
    89 		switch(item.getItemId()) {
    89         switch(item.getItemId()) {
    90 		case R.id.room_create:
    90         case R.id.room_create:
    91 	        TextInputDialog dialog = new TextInputDialog(DIALOG_CREATE_ROOM, R.string.dialog_create_room_title, 0, R.string.dialog_create_room_hint);
    91             TextInputDialog dialog = new TextInputDialog(DIALOG_CREATE_ROOM, R.string.dialog_create_room_title, 0, R.string.dialog_create_room_hint);
    92 	        dialog.show(getSupportFragmentManager(), "create_room_dialog");
    92             dialog.show(getSupportFragmentManager(), "create_room_dialog");
    93 			return true;
    93             return true;
    94 		case R.id.disconnect:
    94         case R.id.disconnect:
    95 			netplay.disconnect();
    95             netplay.disconnect();
    96 			return true;
    96             return true;
    97 		default:
    97         default:
    98 			return super.onOptionsItemSelected(item);
    98             return super.onOptionsItemSelected(item);
    99 		}
    99         }
   100 	}
   100     }
   101 	
   101 
   102 	@Override
   102     @Override
   103 	public void onBackPressed() {
   103     public void onBackPressed() {
   104 		netplay.disconnect();
   104         netplay.disconnect();
   105 	}
   105     }
   106 	
   106 
   107     @Override
   107     @Override
   108     protected void onSaveInstanceState(Bundle icicle) {
   108     protected void onSaveInstanceState(Bundle icicle) {
   109         super.onSaveInstanceState(icicle);
   109         super.onSaveInstanceState(icicle);
   110         if(tabHost != null) {
   110         if(tabHost != null) {
   111         	icicle.putString("currentTab", tabHost.getCurrentTabTag());
   111             icicle.putString("currentTab", tabHost.getCurrentTabTag());
   112         }
   112         }
   113     }
   113     }
   114     
   114 
   115     public void onTextInputDialogSubmitted(int dialogId, String text) {
   115     public void onTextInputDialogSubmitted(int dialogId, String text) {
   116     	if(text != null && text.length()>0) {
   116         if(text != null && text.length()>0) {
   117     		netplay.sendCreateRoom(text);
   117             netplay.sendCreateRoom(text);
   118     	}
   118         }
   119     }
   119     }
   120     
   120 
   121     public void onTextInputDialogCancelled(int dialogId) {
   121     public void onTextInputDialogCancelled(int dialogId) {
   122     }
   122     }
   123     
   123 
   124     public void onNetplayStateChanged(State newState) {
   124     public void onNetplayStateChanged(State newState) {
   125     	switch(newState) {
   125         switch(newState) {
   126     	case CONNECTING:
   126         case CONNECTING:
   127     	case NOT_CONNECTED:
   127         case NOT_CONNECTED:
   128     		finish();
   128             finish();
   129     		break;
   129             break;
   130     	case ROOM:
   130         case ROOM:
   131     		startActivity(new Intent(getApplicationContext(), NetRoomActivity.class));
   131             startActivity(new Intent(getApplicationContext(), NetRoomActivity.class));
   132     		break;
   132             break;
   133     	case LOBBY:
   133         case LOBBY:
   134     		// Do nothing
   134             // Do nothing
   135     		break;
   135             break;
   136 		default:
   136         default:
   137 			throw new IllegalStateException("Unknown connection state: "+newState);
   137             throw new IllegalStateException("Unknown connection state: "+newState);
   138     	}
   138         }
   139     }
   139     }
   140 }
   140 }