project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/RoomlistAdapter.java
author Medo <smaxein@googlemail.com>
Mon, 20 Aug 2012 21:05:57 +0200
changeset 7584 7831c84cc644
parent 7508 763d3961400b
child 10017 de822cd3df3a
permissions -rw-r--r--
License change: With the agreement of Xeli, I changed the Hedgeroid license to GPLv2+ (from GPLv2).
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: 7508
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
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: 7508
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: 7508
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
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: 7508
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: 7508
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: 7508
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: 7508
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
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: 7508
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: 7508
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: 7508
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: 7508
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
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: 7508
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: 7508
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: 7508
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    20
package org.hedgewars.hedgeroid;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    22
import java.util.Comparator;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import org.hedgewars.hedgeroid.R;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    25
import org.hedgewars.hedgeroid.Datastructures.Room;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    26
import org.hedgewars.hedgeroid.Datastructures.RoomWithId;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    27
import org.hedgewars.hedgeroid.util.ObservableTreeMapAdapter;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.content.Context;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.content.res.Resources;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.view.LayoutInflater;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.view.View;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.view.ViewGroup;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.widget.TextView;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    36
/**
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    37
 * 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: 7508
diff changeset
    38
 */
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    39
public class RoomlistAdapter extends ObservableTreeMapAdapter<String, RoomWithId> {
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	private Context context;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	
7461
38acbfdb484f Hedgeroid: Started to implement RoomActivity
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    42
	public RoomlistAdapter(Context context) {
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
		this.context = context;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
	}
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
	
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7461
diff changeset
    46
	@Override
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    47
	protected Comparator<RoomWithId> getEntryOrder() {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    48
		return RoomWithId.NEWEST_FIRST_ORDER;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
	}
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7461
diff changeset
    50
	
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    51
	@Override
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    52
	public long getItemId(int position) {
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    53
		return getItem(position).id;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
	}
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    55
	
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    56
	@Override
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
	public boolean hasStableIds() {
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
		return true;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
	}
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    60
	
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    61
	private static CharSequence formatExtra(Resources res, Room room) {
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
		String ownermsg = res.getString(R.string.roomlist_owner, room.owner);
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    63
		String mapmsg = res.getString(R.string.roomlist_map, room.formatMapName(res));
7349
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    64
		String scheme = room.scheme.equals(room.weapons) ? room.scheme : room.scheme + " / " + room.weapons;
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    65
		String schememsg = res.getString(R.string.roomlist_scheme, scheme);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    66
		return ownermsg + ". " + mapmsg + ", " + schememsg;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	}
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
	
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
	public View getView(int position, View convertView, ViewGroup parent) {
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
		View v = convertView;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
		if (v == null) {
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
			LayoutInflater vi = LayoutInflater.from(context);
7349
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    73
			v = vi.inflate(R.layout.listview_room, null);
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
		}
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
		
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7491
diff changeset
    76
		Room room = getItem(position).room;
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
		int iconRes = room.inProgress ? R.drawable.roomlist_ingame : R.drawable.roomlist_preparing;
7349
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    78
		
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    79
		if(v.findViewById(android.R.id.text1) == null) {
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    80
			// Tabular room list
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    81
			TextView roomnameView = (TextView)v.findViewById(R.id.roomname);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    82
			TextView playerCountView = (TextView)v.findViewById(R.id.playercount);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    83
			TextView teamCountView = (TextView)v.findViewById(R.id.teamcount);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    84
			TextView ownerView = (TextView)v.findViewById(R.id.owner);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    85
			TextView mapView = (TextView)v.findViewById(R.id.map);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    86
			TextView schemeView = (TextView)v.findViewById(R.id.scheme);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    87
			TextView weaponView = (TextView)v.findViewById(R.id.weapons);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    88
			
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    89
			roomnameView.setCompoundDrawablesWithIntrinsicBounds(iconRes, 0, 0, 0);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    90
			roomnameView.setText(room.name);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    91
			if(playerCountView != null) {
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    92
				playerCountView.setText(String.valueOf(room.playerCount));
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    93
			}
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    94
			if(teamCountView != null) {
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    95
				teamCountView.setText(String.valueOf(room.teamCount));
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    96
			}
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    97
			ownerView.setText(room.owner);
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    98
			mapView.setText(room.formatMapName(context.getResources()));
7349
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
    99
			schemeView.setText(room.scheme);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   100
			weaponView.setText(room.weapons);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   101
		} else {
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   102
			// Small room list
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   103
			TextView v1 = (TextView)v.findViewById(android.R.id.text1);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   104
			TextView v2 = (TextView)v.findViewById(android.R.id.text2);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   105
			
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   106
			v1.setCompoundDrawablesWithIntrinsicBounds(iconRes, 0, 0, 0);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   107
			v1.setText(room.name);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   108
			v2.setText(formatExtra(context.getResources(), room));
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   109
		}
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7342
diff changeset
   110
		
7342
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   111
		return v;
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
	}
0e29eec2df5c Hedgeroid: Got the roomlist working... more or less.
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
}