project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/LobbyActivity.java
author unc0rr
Sat, 14 Nov 2015 22:19:05 +0300
changeset 11381 437a60995fe1
parent 10017 de822cd3df3a
permissions -rw-r--r--
Rating updater, alpha version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     1
/*
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     4
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     5
 * This program is free software; you can redistribute it and/or
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     6
 * modify it under the terms of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     8
 * of the License, or (at your option) any later version.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
     9
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    13
 * GNU General Public License for more details.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    14
 *
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    15
 * You should have received a copy of the GNU General Public License
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    16
 * along with this program; if not, write to the Free Software
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    18
 */
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import org.hedgewars.hedgeroid.NetplayStateFragment.NetplayStateListener;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import org.hedgewars.hedgeroid.netplay.Netplay;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import org.hedgewars.hedgeroid.netplay.Netplay.State;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import org.hedgewars.hedgeroid.util.TextInputDialog;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import org.hedgewars.hedgeroid.util.TextInputDialog.TextInputDialogListener;
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    27
import org.hedgewars.hedgeroid.util.UiUtils;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.content.Intent;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.os.Bundle;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.support.v4.app.FragmentActivity;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.support.v4.app.FragmentTransaction;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.view.Menu;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.view.MenuItem;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.widget.LinearLayout;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import android.widget.TabHost;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    38
/**
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    39
 * Activity for the server lobby of a hedgewars server. Allows you to chat, join
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    40
 * and create rooms and interact with a list of players.
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    41
 *
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    42
 * Most of the functionality is handled by various fragments.
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    43
 */
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
public class LobbyActivity extends FragmentActivity implements TextInputDialogListener, NetplayStateListener {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    45
    private static final int DIALOG_CREATE_ROOM = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    46
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
    private TabHost tabHost;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
    private Netplay netplay;
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    49
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
    @Override
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
    protected void onCreate(Bundle icicle) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
        super.onCreate(icicle);
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    53
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
        setContentView(R.layout.activity_lobby);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
        ChatFragment chatFragment = (ChatFragment)getSupportFragmentManager().findFragmentById(R.id.chatFragment);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
        chatFragment.setInRoom(false);
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    57
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
        FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
        trans.add(new NetplayStateFragment(), "netplayFragment");
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
        trans.commit();
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    61
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
        netplay = Netplay.getAppInstance(getApplicationContext());
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    63
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    64
        // Set up a tabbed UI for medium and small screens
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
        if(tabHost != null) {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    67
            tabHost.setup();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    68
            tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL);
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    70
            tabHost.addTab(tabHost.newTabSpec("rooms").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_rooms, R.drawable.roomlist_ingame)).setContent(R.id.roomListFragment));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    71
            tabHost.addTab(tabHost.newTabSpec("chat").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_chat, R.drawable.edit)).setContent(R.id.chatFragment));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    72
            tabHost.addTab(tabHost.newTabSpec("players").setIndicator(UiUtils.createVerticalTabIndicator(tabHost, R.string.lobby_tab_players, R.drawable.human)).setContent(R.id.playerListFragment));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    73
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    74
            if (icicle != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    75
                tabHost.setCurrentTabByTag(icicle.getString("currentTab"));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    76
            }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
        }
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
    }
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    79
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    80
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    81
    public boolean onCreateOptionsMenu(Menu menu) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    82
        super.onCreateOptionsMenu(menu);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    83
        getMenuInflater().inflate(R.menu.lobby_options, menu);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    84
        return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    85
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    86
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    87
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    88
    public boolean onOptionsItemSelected(MenuItem item) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    89
        switch(item.getItemId()) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    90
        case R.id.room_create:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    91
            TextInputDialog dialog = new TextInputDialog(DIALOG_CREATE_ROOM, R.string.dialog_create_room_title, 0, R.string.dialog_create_room_hint);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    92
            dialog.show(getSupportFragmentManager(), "create_room_dialog");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    93
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    94
        case R.id.disconnect:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    95
            netplay.disconnect();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    96
            return true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    97
        default:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    98
            return super.onOptionsItemSelected(item);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    99
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   100
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   101
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   102
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   103
    public void onBackPressed() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   104
        netplay.disconnect();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   105
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   106
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
    @Override
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
    protected void onSaveInstanceState(Bundle icicle) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   109
        super.onSaveInstanceState(icicle);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
        if(tabHost != null) {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   111
            icicle.putString("currentTab", tabHost.getCurrentTabTag());
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
        }
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
    }
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   114
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   115
    public void onTextInputDialogSubmitted(int dialogId, String text) {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   116
        if(text != null && text.length()>0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   117
            netplay.sendCreateRoom(text);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   118
        }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
    }
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   120
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
    public void onTextInputDialogCancelled(int dialogId) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
    }
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   123
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   124
    public void onNetplayStateChanged(State newState) {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   125
        switch(newState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   126
        case CONNECTING:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   127
        case NOT_CONNECTED:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   128
            finish();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   129
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   130
        case ROOM:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   131
            startActivity(new Intent(getApplicationContext(), NetRoomActivity.class));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   132
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   133
        case LOBBY:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   134
            // Do nothing
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   135
            break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   136
        default:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   137
            throw new IllegalStateException("Unknown connection state: "+newState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
   138
        }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   139
    }
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   140
}