project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/LobbyActivity.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 import org.hedgewars.hedgeroid.R;
     3 import org.hedgewars.hedgeroid.R;
     4 import org.hedgewars.hedgeroid.netplay.NetplayService.NetplayBinder;
       
     5 
     4 
     6 import android.content.BroadcastReceiver;
     5 import android.content.BroadcastReceiver;
     7 import android.content.ComponentName;
       
     8 import android.content.Context;
     6 import android.content.Context;
     9 import android.content.Intent;
     7 import android.content.Intent;
    10 import android.content.IntentFilter;
     8 import android.content.IntentFilter;
    11 import android.content.ServiceConnection;
       
    12 import android.graphics.drawable.Drawable;
     9 import android.graphics.drawable.Drawable;
    13 import android.os.Bundle;
    10 import android.os.Bundle;
    14 import android.os.IBinder;
       
    15 import android.support.v4.app.FragmentActivity;
    11 import android.support.v4.app.FragmentActivity;
    16 import android.support.v4.content.LocalBroadcastManager;
    12 import android.support.v4.content.LocalBroadcastManager;
    17 import android.view.LayoutInflater;
    13 import android.view.LayoutInflater;
    18 import android.view.Menu;
    14 import android.view.Menu;
    19 import android.view.MenuItem;
    15 import android.view.MenuItem;
    24 import android.widget.TextView;
    20 import android.widget.TextView;
    25 import android.widget.Toast;
    21 import android.widget.Toast;
    26 
    22 
    27 public class LobbyActivity extends FragmentActivity {
    23 public class LobbyActivity extends FragmentActivity {
    28     private TabHost tabHost;
    24     private TabHost tabHost;
    29     private NetplayService service;
    25     private Netplay netconn;
       
    26     private boolean isInForeground;
    30     
    27     
    31     private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() {
    28     private final BroadcastReceiver disconnectReceiver = new BroadcastReceiver() {
    32 		@Override
    29 		@Override
    33 		public void onReceive(Context context, Intent intent) {
    30 		public void onReceive(Context context, Intent intent) {
    34 			String message = intent.getStringExtra(NetplayService.EXTRA_MESSAGE);
    31 			if(isInForeground && intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) {
    35 			Toast.makeText(getApplicationContext(), "Disconnected: "+message, Toast.LENGTH_LONG).show();
    32 				String message = intent.getStringExtra(Netplay.EXTRA_MESSAGE);
       
    33 				Toast.makeText(getApplicationContext(), "Disconnected: "+message, Toast.LENGTH_LONG).show();
       
    34 			}
    36 			finish();
    35 			finish();
    37 		}
    36 		}
    38 	};
    37 	};
    39     
    38     
    40     @Override
    39     @Override
    41     protected void onCreate(Bundle icicle) {
    40     protected void onCreate(Bundle icicle) {
    42         super.onCreate(icicle);
    41         super.onCreate(icicle);
    43         LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(disconnectReceiver, new IntentFilter(NetplayService.ACTION_DISCONNECTED));
    42         LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(disconnectReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
    44 		bindService(new Intent(this, NetplayService.class), serviceConnection, 0);
    43         netconn = Netplay.getAppInstance(getApplicationContext());
    45 
    44         
    46         setContentView(R.layout.activity_lobby);
    45         setContentView(R.layout.activity_lobby);
    47         tabHost = (TabHost)findViewById(android.R.id.tabhost);
    46         tabHost = (TabHost)findViewById(android.R.id.tabhost);
    48         if(tabHost != null) {
    47         if(tabHost != null) {
    49 	        tabHost.setup();
    48 	        tabHost.setup();
    50 	        tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL);
    49 	        tabHost.getTabWidget().setOrientation(LinearLayout.VERTICAL);
    61     
    60     
    62     @Override
    61     @Override
    63     protected void onDestroy() {
    62     protected void onDestroy() {
    64     	super.onDestroy();
    63     	super.onDestroy();
    65     	LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(disconnectReceiver);
    64     	LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(disconnectReceiver);
    66     	unbindService(serviceConnection);
       
    67     }
    65     }
    68     
    66     
    69     private View createIndicatorView(TabHost tabHost, CharSequence label, Drawable icon) {
    67     private View createIndicatorView(TabHost tabHost, CharSequence label, Drawable icon) {
    70         LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    68         LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    71 
    69 
    82         }
    80         }
    83         
    81         
    84         return tabIndicator;
    82         return tabIndicator;
    85     }
    83     }
    86 
    84 
       
    85     @Override
       
    86     protected void onStart() {
       
    87     	super.onStart();
       
    88     	isInForeground = true;
       
    89     	Netplay.getAppInstance(getApplicationContext()).requestFastTicks();
       
    90     }
       
    91     
       
    92     @Override
       
    93     protected void onStop() {
       
    94     	super.onStop();
       
    95     	isInForeground = false;
       
    96     	Netplay.getAppInstance(getApplicationContext()).unrequestFastTicks();
       
    97     }
    87     
    98     
    88 	@Override
    99 	@Override
    89 	public boolean onCreateOptionsMenu(Menu menu) {
   100 	public boolean onCreateOptionsMenu(Menu menu) {
    90 		super.onCreateOptionsMenu(menu);
   101 		super.onCreateOptionsMenu(menu);
    91 		getMenuInflater().inflate(R.menu.lobby_options, menu);
   102 		getMenuInflater().inflate(R.menu.lobby_options, menu);
    97 		switch(item.getItemId()) {
   108 		switch(item.getItemId()) {
    98 		case R.id.room_create:
   109 		case R.id.room_create:
    99 			Toast.makeText(this, R.string.not_implemented_yet, Toast.LENGTH_SHORT).show();
   110 			Toast.makeText(this, R.string.not_implemented_yet, Toast.LENGTH_SHORT).show();
   100 			return true;
   111 			return true;
   101 		case R.id.disconnect:
   112 		case R.id.disconnect:
   102 			if(service != null && service.isConnected()) {
   113 			netconn.disconnect();
   103 				service.disconnect();
       
   104 			}
       
   105 			return true;
   114 			return true;
   106 		default:
   115 		default:
   107 			return super.onOptionsItemSelected(item);
   116 			return super.onOptionsItemSelected(item);
   108 		}
   117 		}
       
   118 	}
       
   119 	
       
   120 	@Override
       
   121 	public void onBackPressed() {
       
   122 		netconn.disconnect();
       
   123 		super.onBackPressed();
   109 	}
   124 	}
   110     
   125     
   111 	/*@Override
   126 	/*@Override
   112 	protected void onCreate(Bundle arg0) {
   127 	protected void onCreate(Bundle arg0) {
   113 		super.onCreate(arg0);
   128 		super.onCreate(arg0);
   144         super.onSaveInstanceState(icicle);
   159         super.onSaveInstanceState(icicle);
   145         if(tabHost != null) {
   160         if(tabHost != null) {
   146         	icicle.putString("currentTab", tabHost.getCurrentTabTag());
   161         	icicle.putString("currentTab", tabHost.getCurrentTabTag());
   147         }
   162         }
   148     }
   163     }
   149     
       
   150     private ServiceConnection serviceConnection = new ServiceConnection() {
       
   151         public void onServiceConnected(ComponentName className, IBinder binder) {
       
   152         	service = ((NetplayBinder) binder).getService();
       
   153         }
       
   154 
       
   155         public void onServiceDisconnected(ComponentName className) {
       
   156         	service = null;
       
   157         }
       
   158     };
       
   159 }
   164 }