project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/PlayerlistFragment.java
changeset 7352 641f11cdd319
parent 7346 b0f67c5b4215
child 7355 5673e95ef647
equal deleted inserted replaced
7349:12fdfd2038d4 7352:641f11cdd319
     8 import android.content.Intent;
     8 import android.content.Intent;
     9 import android.content.ServiceConnection;
     9 import android.content.ServiceConnection;
    10 import android.os.Bundle;
    10 import android.os.Bundle;
    11 import android.os.IBinder;
    11 import android.os.IBinder;
    12 import android.support.v4.app.ListFragment;
    12 import android.support.v4.app.ListFragment;
       
    13 import android.view.ContextMenu;
    13 import android.view.LayoutInflater;
    14 import android.view.LayoutInflater;
       
    15 import android.view.MenuInflater;
       
    16 import android.view.MenuItem;
    14 import android.view.View;
    17 import android.view.View;
    15 import android.view.ViewGroup;
    18 import android.view.ViewGroup;
       
    19 import android.view.ContextMenu.ContextMenuInfo;
       
    20 import android.widget.Toast;
       
    21 import android.widget.AdapterView.AdapterContextMenuInfo;
    16 
    22 
    17 public class PlayerlistFragment extends ListFragment {
    23 public class PlayerlistFragment extends ListFragment {
    18 	private Netconn netconn;
    24 	private Netconn netconn;
    19 	private PlayerListAdapter playerListAdapter;
    25 	private PlayerListAdapter playerListAdapter;
    20 	
    26 	
    26 		playerListAdapter = new PlayerListAdapter(getActivity());
    32 		playerListAdapter = new PlayerListAdapter(getActivity());
    27 		setListAdapter(playerListAdapter);
    33 		setListAdapter(playerListAdapter);
    28 	}
    34 	}
    29 
    35 
    30 	@Override
    36 	@Override
       
    37 	public void onActivityCreated(Bundle savedInstanceState) {
       
    38 		super.onActivityCreated(savedInstanceState);
       
    39 		registerForContextMenu(getListView());
       
    40 	}
       
    41 
       
    42 	@Override
       
    43 	public void onCreateContextMenu(ContextMenu menu, View v,
       
    44 			ContextMenuInfo menuInfo) {
       
    45 		super.onCreateContextMenu(menu, v, menuInfo);
       
    46 		MenuInflater inflater = getActivity().getMenuInflater();
       
    47 		inflater.inflate(R.menu.lobby_playerlist_context, menu);
       
    48 	}
       
    49 	
       
    50 	@Override
       
    51 	public boolean onContextItemSelected(MenuItem item) {
       
    52 		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
       
    53 		switch(item.getItemId()) {
       
    54 		case R.id.player_info:
       
    55 			Player p = playerListAdapter.getItem(info.position);
       
    56 			if(netconn != null) {
       
    57 				netconn.sendPlayerInfoQuery(p.name);
       
    58 			}
       
    59 			return true;
       
    60 		case R.id.player_follow:
       
    61 			Toast.makeText(getActivity(), R.string.not_implemented_yet, Toast.LENGTH_SHORT).show();
       
    62 			return true;
       
    63 		default:
       
    64 			return super.onContextItemSelected(item);
       
    65 		}
       
    66 	}
       
    67 	
       
    68 	@Override
    31 	public void onDestroy() {
    69 	public void onDestroy() {
    32 		super.onDestroy();
    70 		super.onDestroy();
    33 		getActivity().unbindService(serviceConnection);
    71 		getActivity().unbindService(serviceConnection);
    34 	}
    72 	}
    35 	
    73 	
    40 	}
    78 	}
    41 	
    79 	
    42     private ServiceConnection serviceConnection = new ServiceConnection() {
    80     private ServiceConnection serviceConnection = new ServiceConnection() {
    43         public void onServiceConnected(ComponentName className, IBinder binder) {
    81         public void onServiceConnected(ComponentName className, IBinder binder) {
    44         	netconn = ((NetplayBinder) binder).getNetconn();
    82         	netconn = ((NetplayBinder) binder).getNetconn();
    45         	playerListAdapter.setPlayerList(netconn.playerList.getValues());
    83         	playerListAdapter.setList(netconn.playerList);
    46         	netconn.playerList.observe(playerListAdapter);
       
    47         }
    84         }
    48 
    85 
    49         public void onServiceDisconnected(ComponentName className) {
    86         public void onServiceDisconnected(ComponentName className) {
    50         	// TODO navigate away
    87         	// TODO navigate away
    51         	netconn.playerList.unobserve(playerListAdapter);
    88         	playerListAdapter.invalidate();
    52         	netconn = null;
    89         	netconn = null;
    53         }
    90         }
    54     };
    91     };
    55 }
    92 }