project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/LobbyChatFragment.java
author Medo <smaxein@googlemail.com>
Thu, 26 Jul 2012 11:01:32 +0200
changeset 7358 57a508884052
parent 7355 5673e95ef647
child 7444 2e31f114f57e
permissions -rw-r--r--
Hedgeroid: Major overhaul of the network connection code. Now it is threaded and errors are handled properly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
package org.hedgewars.hedgeroid.netplay;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
     3
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
import org.hedgewars.hedgeroid.R;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
import android.os.Bundle;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
import android.support.v4.app.Fragment;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
import android.view.KeyEvent;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
import android.view.LayoutInflater;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
import android.view.View;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
import android.view.ViewGroup;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
import android.view.inputmethod.EditorInfo;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
import android.widget.EditText;
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    14
import android.widget.ListView;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
import android.widget.TextView;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
import android.widget.TextView.OnEditorActionListener;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
public class LobbyChatFragment extends Fragment {
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    19
	private ChatlogAdapter adapter;
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    20
	private Netplay netconn;
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    21
	
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
	@Override
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    23
	public void onCreate(Bundle savedInstanceState) {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    24
		super.onCreate(savedInstanceState);
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    25
		netconn = Netplay.getAppInstance(getActivity().getApplicationContext());
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    26
		adapter = new ChatlogAdapter(getActivity());
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    27
    	adapter.setLog(netconn.lobbyChatlog.getLog());
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    28
    	netconn.lobbyChatlog.registerObserver(adapter);
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    29
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    30
	
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    31
	@Override
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
	public void onStart() {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
		super.onStart();
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
	}
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    35
	
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
	@Override
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
			Bundle savedInstanceState) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
		View view = inflater.inflate(R.layout.lobby_chat_fragment, container, false);
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
		
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    41
		ListView listView = (ListView) view.findViewById(R.id.lobbyConsole);
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    42
		listView.setAdapter(adapter);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    43
		listView.setDivider(null);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    44
		listView.setDividerHeight(0);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    45
		listView.setVerticalFadingEdgeEnabled(true);
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    46
		
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    47
		EditText editText = (EditText) view.findViewById(R.id.lobbyChatInput);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    48
        editText.setOnEditorActionListener(new ChatSendListener());
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    49
        
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		return view;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
	}
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    52
	
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    53
	@Override
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    54
	public void onDestroy() {
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    55
		super.onDestroy();
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    56
		netconn.lobbyChatlog.unregisterObserver(adapter);
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    57
	}
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    58
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    59
	private final class ChatSendListener implements OnEditorActionListener {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    60
		public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    61
			boolean handled = false;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    62
			if(actionId == EditorInfo.IME_ACTION_SEND) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    63
				String text = v.getText().toString();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    64
				if(text.length()>0) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    65
					v.setText("");
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    66
					netconn.sendChat(text);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    67
					handled = true;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    68
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    69
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    70
			return handled;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    71
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    72
	}
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
}