project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/LobbyChatFragment.java
author Medo <smaxein@googlemail.com>
Mon, 16 Jul 2012 20:16:03 +0200
changeset 7330 867e4fda496e
child 7332 3f2e130f9715
permissions -rw-r--r--
Hedgeroid: Layout experiments for the lobby page
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
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
import org.hedgewars.hedgeroid.R;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
import android.os.Bundle;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
import android.os.Handler;
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.text.Html;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
import android.util.Log;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
import android.view.KeyEvent;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
import android.view.LayoutInflater;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
import android.view.View;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
import android.view.ViewGroup;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
import android.view.inputmethod.EditorInfo;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
import android.widget.EditText;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
import android.widget.ScrollView;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
import android.widget.TextView;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
import android.widget.TextView.OnEditorActionListener;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
public class LobbyChatFragment extends Fragment {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
	private TextView textView;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
	private EditText editText;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
	private ScrollView scrollView;
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
	private void commitText() {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
		String text = editText.getText().toString();
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
		int overhang = textView.getHeight()-scrollView.getHeight();
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
		boolean followBottom = overhang<=0 || Math.abs(overhang-scrollView.getScrollY())<5;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
		textView.append(Html.fromHtml("<b>Chatter:</b> " + text + "<br/>"));
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
		editText.setText("");
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
		if(followBottom) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
			new Handler().post(new Runnable() {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
				public void run() {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
					scrollView.fullScroll(ScrollView.FOCUS_DOWN);
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
				}
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
			});
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
		}
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
	/*
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	@Override
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	public void onStart() {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
		super.onStart();
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
		getActivity().bindService(new Intent(getActivity(), NetplayService.class), serviceConnection,
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
	            Context.BIND_AUTO_CREATE);
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
	}
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
	*/
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
	@Override
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
			Bundle savedInstanceState) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		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
    51
		textView = (TextView) view.findViewById(R.id.lobbyConsole);
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
		editText = (EditText) view.findViewById(R.id.lobbyChatInput);
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
		scrollView = (ScrollView) view.findViewById(R.id.lobbyConsoleScroll);
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
		
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
        editText.setOnEditorActionListener(new OnEditorActionListener() {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
				boolean handled = false;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
				if(actionId == EditorInfo.IME_ACTION_SEND) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
					commitText();
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
					handled = true;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
				}
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
				return handled;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
			}
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
		});
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
		
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
		return view;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	}
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
	/*
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
    private ServiceConnection serviceConnection = new ServiceConnection() {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
        public void onServiceConnected(ComponentName className, IBinder binder) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
        	netplayService = ((NetplayBinder) binder).getService();
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
        	try {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
				netplayService.connect("AndroidChatter");
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
			} catch (IOException e) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
				throw new RuntimeException(e);
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
			}
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
        }
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
        public void onServiceDisconnected(ComponentName className) {
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
        	// TODO navigate away
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
        	netplayService = null;
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
        }
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
    };
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
    */
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
}