project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
changeset 7358 57a508884052
parent 7355 5673e95ef647
child 7444 2e31f114f57e
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java	Tue Jul 24 16:57:48 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java	Thu Jul 26 11:01:32 2012 +0200
@@ -25,7 +25,8 @@
 import org.hedgewars.hedgeroid.Downloader.DownloadAssets;
 import org.hedgewars.hedgeroid.Downloader.DownloadListActivity;
 import org.hedgewars.hedgeroid.netplay.LobbyActivity;
-import org.hedgewars.hedgeroid.netplay.NetplayService;
+import org.hedgewars.hedgeroid.netplay.Netplay;
+import org.hedgewars.hedgeroid.netplay.Netplay.State;
 
 import android.app.AlertDialog;
 import android.app.Dialog;
@@ -33,6 +34,7 @@
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.DialogInterface.OnCancelListener;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.SharedPreferences;
@@ -40,7 +42,8 @@
 import android.os.Bundle;
 import android.support.v4.app.FragmentActivity;
 import android.support.v4.content.LocalBroadcastManager;
-import android.util.Log;
+import android.text.InputType;
+import android.text.method.PasswordTransformationMethod;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
@@ -50,16 +53,25 @@
 public class MainActivity extends FragmentActivity {
 	private static final int DIALOG_NO_SDCARD = 0;
 	private static final int DIALOG_START_NETGAME = 1;
+	private static final int DIALOG_CONNECTING = 2;
+	private static final int DIALOG_PASSWORD = 3;
 	
 	private static final String PREF_PLAYERNAME = "playerName";
 	
+	private LocalBroadcastManager broadcastManager;
+	
 	private Button downloader, startGame;
 	private ProgressDialog assetsDialog;
+	private String passwordedUsername; // TODO ugly - move dialogs to fragments to get rid of e.g. this
 
 	public void onCreate(Bundle sis){
 		super.onCreate(sis);
+		if(sis != null) {
+			passwordedUsername = sis.getString(PREF_PLAYERNAME);
+		}
 		setContentView(R.layout.main);
 
+		broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
 		downloader = (Button)findViewById(R.id.downloader);
 		startGame = (Button)findViewById(R.id.startGame);
 		Button joinLobby = (Button)findViewById(R.id.joinLobby);
@@ -68,10 +80,17 @@
 		startGame.setOnClickListener(startGameClicker);
 		joinLobby.setOnClickListener(new OnClickListener() {
 			public void onClick(View v) {
-				if(!NetplayService.isActive()) {
+				State state = Netplay.getAppInstance(getApplicationContext()).getState();
+				switch(state) {
+				case NOT_CONNECTED:
 					showDialog(DIALOG_START_NETGAME);
-				} else {
+					break;
+				case CONNECTING:
+					startWaitingForConnection();
+					break;
+				default:
 					startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
+					break;
 				}
 			}
 		});
@@ -100,12 +119,44 @@
 		}
 	}
 
+	@Override
+	protected void onSaveInstanceState(Bundle outState) {
+		super.onSaveInstanceState(outState);
+		outState.putString(PREF_PLAYERNAME, passwordedUsername);
+	}
+	
+	@Override
+	protected void onDestroy() {
+		super.onDestroy();
+		stopWaitingForConnection();
+	}
+	
+	@Override
+	protected void onStart() {
+		super.onStart();
+		Netplay.getAppInstance(getApplicationContext()).requestFastTicks();
+	}
+	
+	@Override
+	protected void onStop() {
+		super.onStop();
+		Netplay netplay = Netplay.getAppInstance(getApplicationContext());
+		netplay.unrequestFastTicks();
+		if(netplay.getState() == State.CONNECTING) {
+			netplay.disconnect();
+		}
+	}
+	
 	public Dialog onCreateDialog(int id, Bundle args){
 		switch(id) {
 		case DIALOG_NO_SDCARD:
 			return createNoSdcardDialog();
 		case DIALOG_START_NETGAME:
 			return createStartNetgameDialog();
+		case DIALOG_CONNECTING:
+			return createConnectingDialog();
+		case DIALOG_PASSWORD:
+			return createPasswordDialog();
 		default:
 			throw new IndexOutOfBoundsException();
 		}
@@ -150,11 +201,8 @@
 					edit.putString(PREF_PLAYERNAME, playerName);
 					edit.commit();
 					
-					Intent netplayServiceIntent = new Intent(getApplicationContext(), NetplayService.class);
-					netplayServiceIntent.putExtra(NetplayService.EXTRA_PLAYERNAME, playerName);
-					startService(netplayServiceIntent);
-					
-					LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(connectedReceiver, new IntentFilter(NetplayService.ACTION_CONNECTED));
+					startWaitingForConnection();
+					Netplay.getAppInstance(getApplicationContext()).connectToDefaultServer(playerName);
 				}
 			}
 		});
@@ -162,6 +210,46 @@
 		return builder.create();
 	}
 	
+	private Dialog createConnectingDialog() {
+		ProgressDialog dialog = new ProgressDialog(this);
+		dialog.setOnCancelListener(new OnCancelListener() {
+			public void onCancel(DialogInterface dialog) {
+				Netplay.getAppInstance(getApplicationContext()).disconnect();
+			}
+		});
+		dialog.setIndeterminate(true);
+		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
+		dialog.setTitle(R.string.dialog_connecting_title);
+		dialog.setMessage(getString(R.string.dialog_connecting_message));
+		return dialog;
+	}
+	
+	private Dialog createPasswordDialog() {
+		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
+		final EditText editText = new EditText(this);
+		editText.setHint(R.string.dialog_password_hint);
+		editText.setFreezesText(true);
+		editText.setId(android.R.id.text1);
+		editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
+		editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
+		builder.setView(editText);
+		builder.setTitle(R.string.dialog_password_title);
+		builder.setMessage(getString(R.string.dialog_password_message, passwordedUsername));
+		builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+			public void onClick(DialogInterface dialog, int which) {
+				String password = editText.getText().toString();
+				editText.setText("");
+				Netplay.getAppInstance(getApplicationContext()).sendPassword(password);
+			}
+		});
+		builder.setOnCancelListener(new OnCancelListener() {
+			public void onCancel(DialogInterface dialog) {
+				Netplay.getAppInstance(getApplicationContext()).disconnect();
+			}
+		});
+		return builder.create();
+	}
+	
 	public void onAssetsDownloaded(boolean result){
 		if(!result){
 			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show();
@@ -181,10 +269,44 @@
 		}
 	};
 	
+	private void startWaitingForConnection() {
+		broadcastManager.registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
+		broadcastManager.registerReceiver(connectionFailedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
+		broadcastManager.registerReceiver(passwordRequestedReceiver, new IntentFilter(Netplay.ACTION_PASSWORD_REQUESTED));
+		showDialog(DIALOG_CONNECTING);
+	}
+	
+	private void stopWaitingForConnection() {
+		broadcastManager.unregisterReceiver(connectedReceiver);
+		broadcastManager.unregisterReceiver(connectionFailedReceiver);
+		broadcastManager.unregisterReceiver(passwordRequestedReceiver);
+	}
+	
 	private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
 		@Override
 		public void onReceive(Context context, Intent intent) {
+			stopWaitingForConnection();
+			dismissDialog(DIALOG_CONNECTING);
 			startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
 		}
 	};
+	
+	private BroadcastReceiver connectionFailedReceiver = new BroadcastReceiver() {
+		@Override
+		public void onReceive(Context context, Intent intent) {
+			stopWaitingForConnection();
+			dismissDialog(DIALOG_CONNECTING);
+			if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) {
+				Toast.makeText(getApplicationContext(), intent.getStringExtra(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show();
+			}
+		}
+	};
+	
+	private BroadcastReceiver passwordRequestedReceiver = new BroadcastReceiver() {
+		@Override
+		public void onReceive(Context context, Intent intent) {
+			passwordedUsername = intent.getStringExtra(Netplay.EXTRA_PLAYERNAME);
+			showDialog(DIALOG_PASSWORD);
+		}
+	};
 }