project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/RoomlistFragment.java
author koda
Tue, 21 Jan 2014 22:43:06 +0100
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
permissions -rw-r--r--
fixwhitespace and dos2unix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     1
/*
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
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     8
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    13
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    15
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    16
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    20
package org.hedgewars.hedgeroid;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import org.hedgewars.hedgeroid.R;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    23
import org.hedgewars.hedgeroid.netplay.Netplay;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import android.os.Bundle;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    26
import android.os.CountDownTimer;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import android.support.v4.app.ListFragment;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import android.view.LayoutInflater;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.view.View;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.view.ViewGroup;
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    31
import android.widget.AdapterView;
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    32
import android.widget.AdapterView.OnItemClickListener;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    34
/**
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    35
 * Displays the list of all rooms in the lobby
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    36
 */
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    37
public class RoomlistFragment extends ListFragment implements OnItemClickListener {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    38
    private static final int AUTO_REFRESH_INTERVAL_MS = 15000;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    39
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    40
    private Netplay netplay;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    41
    private RoomlistAdapter adapter;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    42
    private CountDownTimer autoRefreshTimer = new CountDownTimer(Long.MAX_VALUE, AUTO_REFRESH_INTERVAL_MS) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    43
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    44
        public void onTick(long millisUntilFinished) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    45
            netplay.sendRoomlistRequest();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    46
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    47
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    48
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    49
        public void onFinish() { }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    50
    };
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    52
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    53
    public void onCreate(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    54
        super.onCreate(savedInstanceState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    55
        netplay = Netplay.getAppInstance(getActivity().getApplicationContext());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    56
        adapter = new RoomlistAdapter(getActivity());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    57
        adapter.setSource(netplay.roomList);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    58
        setListAdapter(adapter);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    59
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    60
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    61
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    62
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    63
            Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    64
        return inflater.inflate(R.layout.fragment_roomlist, container, false);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    65
    }
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    67
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    68
    public void onActivityCreated(Bundle savedInstanceState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    69
        super.onActivityCreated(savedInstanceState);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    70
        getListView().setOnItemClickListener(this);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    71
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    72
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    73
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    74
    public void onResume() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    75
        super.onResume();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    76
        autoRefreshTimer.start();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    77
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    78
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    79
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    80
    public void onPause() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    81
        super.onPause();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    82
        autoRefreshTimer.cancel();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    83
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    84
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    85
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    86
    public void onDestroy() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    87
        super.onDestroy();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    88
        adapter.invalidate();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    89
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    90
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    91
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    92
        netplay.sendJoinRoom(adapter.getItem(position).room.name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7584
diff changeset
    93
    }
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
}