project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/LobbyChatFragment.java
author Medo <smaxein@googlemail.com>
Thu, 19 Jul 2012 22:55:36 +0200
changeset 7346 b0f67c5b4215
parent 7332 3f2e130f9715
child 7349 12fdfd2038d4
permissions -rw-r--r--
Hedgeroid: Lobby activity improvements
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;
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
     5
import org.hedgewars.hedgeroid.netplay.NetplayService.NetplayBinder;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
     7
import android.content.ComponentName;
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
     8
import android.content.Context;
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
     9
import android.content.Intent;
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    10
import android.content.ServiceConnection;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
import android.os.Bundle;
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    12
import android.os.IBinder;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
import android.support.v4.app.Fragment;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
import android.util.Log;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
import android.view.KeyEvent;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
import android.view.LayoutInflater;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
import android.view.View;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
import android.view.ViewGroup;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
import android.view.inputmethod.EditorInfo;
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    20
import android.widget.AbsListView;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    21
import android.widget.AbsListView.OnScrollListener;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import android.widget.EditText;
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    23
import android.widget.ListView;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import android.widget.TextView;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import android.widget.TextView.OnEditorActionListener;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
public class LobbyChatFragment extends Fragment {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
	private EditText editText;
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    29
	private ListView listView;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    30
	private ChatlogAdapter adapter;
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    31
	private Netconn netconn;
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    32
	
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
	private void commitText() {
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    34
		String text = editText.getText().toString();
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    35
		if(netconn != null && netconn.isConnected() && text.length()>0) {
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    36
			editText.setText("");
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    37
			netconn.sendChat(text);
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
		}
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
	}
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    40
	
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	@Override
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    42
	public void onCreate(Bundle savedInstanceState) {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    43
		super.onCreate(savedInstanceState);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    44
		adapter = new ChatlogAdapter(getActivity());
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    45
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    46
	
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    47
	@Override
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
	public void onStart() {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
		super.onStart();
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		getActivity().bindService(new Intent(getActivity(), NetplayService.class), serviceConnection,
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
	            Context.BIND_AUTO_CREATE);
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
	}
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    53
	
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
	@Override
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
			Bundle savedInstanceState) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
		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
    58
		editText = (EditText) view.findViewById(R.id.lobbyChatInput);
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    59
		listView = (ListView) view.findViewById(R.id.lobbyConsole);
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
		
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    61
		listView.setAdapter(adapter);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    62
		listView.setDivider(null);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    63
		listView.setDividerHeight(0);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    64
		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
    65
		
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
        editText.setOnEditorActionListener(new OnEditorActionListener() {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
				boolean handled = false;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
				if(actionId == EditorInfo.IME_ACTION_SEND) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
					commitText();
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
					handled = true;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
				}
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
				return handled;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
			}
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
		});
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    76
        
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
		return view;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
	}
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    79
	
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    80
	@Override
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    81
	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
    82
		super.onDestroy();
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    83
		getActivity().unbindService(serviceConnection);
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    84
	}
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    85
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
    private ServiceConnection serviceConnection = new ServiceConnection() {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
        public void onServiceConnected(ComponentName className, IBinder binder) {
7332
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    88
        	Log.d("LobbyChatFragment", "netconn received");
3f2e130f9715 Hedgeroid: Chat and player list work now, but everything is very much WIP
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    89
        	netconn = ((NetplayBinder) binder).getNetconn();
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    90
        	adapter.setLog(netconn.lobbyLog.getLog());
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    91
        	netconn.lobbyLog.registerObserver(adapter);
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
        }
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
        public void onServiceDisconnected(ComponentName className) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
        	// TODO navigate away
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents: 7332
diff changeset
    96
        	netconn.lobbyLog.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
    97
        	netconn = null;
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
        }
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
    };
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
}