project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/LobbyChatFragment.java
changeset 7358 57a508884052
parent 7355 5673e95ef647
child 7444 2e31f114f57e
equal deleted inserted replaced
7355:5673e95ef647 7358:57a508884052
     1 package org.hedgewars.hedgeroid.netplay;
     1 package org.hedgewars.hedgeroid.netplay;
     2 
     2 
     3 
     3 
     4 import org.hedgewars.hedgeroid.R;
     4 import org.hedgewars.hedgeroid.R;
     5 import org.hedgewars.hedgeroid.netplay.NetplayService.NetplayBinder;
       
     6 
     5 
     7 import android.content.ComponentName;
       
     8 import android.content.Context;
       
     9 import android.content.Intent;
       
    10 import android.content.ServiceConnection;
       
    11 import android.os.Bundle;
     6 import android.os.Bundle;
    12 import android.os.IBinder;
       
    13 import android.support.v4.app.Fragment;
     7 import android.support.v4.app.Fragment;
    14 import android.util.Log;
       
    15 import android.view.KeyEvent;
     8 import android.view.KeyEvent;
    16 import android.view.LayoutInflater;
     9 import android.view.LayoutInflater;
    17 import android.view.View;
    10 import android.view.View;
    18 import android.view.ViewGroup;
    11 import android.view.ViewGroup;
    19 import android.view.inputmethod.EditorInfo;
    12 import android.view.inputmethod.EditorInfo;
    21 import android.widget.ListView;
    14 import android.widget.ListView;
    22 import android.widget.TextView;
    15 import android.widget.TextView;
    23 import android.widget.TextView.OnEditorActionListener;
    16 import android.widget.TextView.OnEditorActionListener;
    24 
    17 
    25 public class LobbyChatFragment extends Fragment {
    18 public class LobbyChatFragment extends Fragment {
    26 	private EditText editText;
       
    27 	private ListView listView;
       
    28 	private ChatlogAdapter adapter;
    19 	private ChatlogAdapter adapter;
    29 	private NetplayService service;
    20 	private Netplay netconn;
    30 	
       
    31 	private void commitText() {
       
    32 		String text = editText.getText().toString();
       
    33 		if(service != null && service.isConnected() && text.length()>0) {
       
    34 			editText.setText("");
       
    35 			service.sendChat(text);
       
    36 		}
       
    37 	}
       
    38 	
    21 	
    39 	@Override
    22 	@Override
    40 	public void onCreate(Bundle savedInstanceState) {
    23 	public void onCreate(Bundle savedInstanceState) {
    41 		super.onCreate(savedInstanceState);
    24 		super.onCreate(savedInstanceState);
       
    25 		netconn = Netplay.getAppInstance(getActivity().getApplicationContext());
    42 		adapter = new ChatlogAdapter(getActivity());
    26 		adapter = new ChatlogAdapter(getActivity());
       
    27     	adapter.setLog(netconn.lobbyChatlog.getLog());
       
    28     	netconn.lobbyChatlog.registerObserver(adapter);
    43 	}
    29 	}
    44 	
    30 	
    45 	@Override
    31 	@Override
    46 	public void onStart() {
    32 	public void onStart() {
    47 		super.onStart();
    33 		super.onStart();
    48 		getActivity().bindService(new Intent(getActivity(), NetplayService.class), serviceConnection,
       
    49 	            Context.BIND_AUTO_CREATE);
       
    50 	}
    34 	}
    51 	
    35 	
    52 	@Override
    36 	@Override
    53 	public View onCreateView(LayoutInflater inflater, ViewGroup container,
    37 	public View onCreateView(LayoutInflater inflater, ViewGroup container,
    54 			Bundle savedInstanceState) {
    38 			Bundle savedInstanceState) {
    55 		View view = inflater.inflate(R.layout.lobby_chat_fragment, container, false);
    39 		View view = inflater.inflate(R.layout.lobby_chat_fragment, container, false);
    56 		editText = (EditText) view.findViewById(R.id.lobbyChatInput);
       
    57 		listView = (ListView) view.findViewById(R.id.lobbyConsole);
       
    58 		
    40 		
       
    41 		ListView listView = (ListView) view.findViewById(R.id.lobbyConsole);
    59 		listView.setAdapter(adapter);
    42 		listView.setAdapter(adapter);
    60 		listView.setDivider(null);
    43 		listView.setDivider(null);
    61 		listView.setDividerHeight(0);
    44 		listView.setDividerHeight(0);
    62 		listView.setVerticalFadingEdgeEnabled(true);
    45 		listView.setVerticalFadingEdgeEnabled(true);
    63 		
    46 		
    64         editText.setOnEditorActionListener(new OnEditorActionListener() {
    47 		EditText editText = (EditText) view.findViewById(R.id.lobbyChatInput);
    65 			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    48         editText.setOnEditorActionListener(new ChatSendListener());
    66 				boolean handled = false;
       
    67 				if(actionId == EditorInfo.IME_ACTION_SEND) {
       
    68 					commitText();
       
    69 					handled = true;
       
    70 				}
       
    71 				return handled;
       
    72 			}
       
    73 		});
       
    74         
    49         
    75 		return view;
    50 		return view;
    76 	}
    51 	}
    77 	
    52 	
    78 	@Override
    53 	@Override
    79 	public void onDestroy() {
    54 	public void onDestroy() {
    80 		super.onDestroy();
    55 		super.onDestroy();
    81 		getActivity().unbindService(serviceConnection);
    56 		netconn.lobbyChatlog.unregisterObserver(adapter);
    82 	}
    57 	}
    83 
    58 
    84     private ServiceConnection serviceConnection = new ServiceConnection() {
    59 	private final class ChatSendListener implements OnEditorActionListener {
    85         public void onServiceConnected(ComponentName className, IBinder binder) {
    60 		public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    86         	Log.d("LobbyChatFragment", "netconn received");
    61 			boolean handled = false;
    87         	service = ((NetplayBinder) binder).getService();
    62 			if(actionId == EditorInfo.IME_ACTION_SEND) {
    88         	adapter.setLog(service.lobbyChatlog.getLog());
    63 				String text = v.getText().toString();
    89         	service.lobbyChatlog.registerObserver(adapter);
    64 				if(text.length()>0) {
    90         }
    65 					v.setText("");
    91 
    66 					netconn.sendChat(text);
    92         public void onServiceDisconnected(ComponentName className) {
    67 					handled = true;
    93         	// TODO navigate away
    68 				}
    94         	service.lobbyChatlog.unregisterObserver(adapter);
    69 			}
    95         	service = null;
    70 			return handled;
    96         }
    71 		}
    97     };
    72 	}
    98 }
    73 }