project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
changeset 7358 57a508884052
parent 7355 5673e95ef647
child 7444 2e31f114f57e
equal deleted inserted replaced
7355:5673e95ef647 7358:57a508884052
    23 import java.io.IOException;
    23 import java.io.IOException;
    24 
    24 
    25 import org.hedgewars.hedgeroid.Downloader.DownloadAssets;
    25 import org.hedgewars.hedgeroid.Downloader.DownloadAssets;
    26 import org.hedgewars.hedgeroid.Downloader.DownloadListActivity;
    26 import org.hedgewars.hedgeroid.Downloader.DownloadListActivity;
    27 import org.hedgewars.hedgeroid.netplay.LobbyActivity;
    27 import org.hedgewars.hedgeroid.netplay.LobbyActivity;
    28 import org.hedgewars.hedgeroid.netplay.NetplayService;
    28 import org.hedgewars.hedgeroid.netplay.Netplay;
       
    29 import org.hedgewars.hedgeroid.netplay.Netplay.State;
    29 
    30 
    30 import android.app.AlertDialog;
    31 import android.app.AlertDialog;
    31 import android.app.Dialog;
    32 import android.app.Dialog;
    32 import android.app.ProgressDialog;
    33 import android.app.ProgressDialog;
    33 import android.content.BroadcastReceiver;
    34 import android.content.BroadcastReceiver;
    34 import android.content.Context;
    35 import android.content.Context;
    35 import android.content.DialogInterface;
    36 import android.content.DialogInterface;
       
    37 import android.content.DialogInterface.OnCancelListener;
    36 import android.content.Intent;
    38 import android.content.Intent;
    37 import android.content.IntentFilter;
    39 import android.content.IntentFilter;
    38 import android.content.SharedPreferences;
    40 import android.content.SharedPreferences;
    39 import android.content.SharedPreferences.Editor;
    41 import android.content.SharedPreferences.Editor;
    40 import android.os.Bundle;
    42 import android.os.Bundle;
    41 import android.support.v4.app.FragmentActivity;
    43 import android.support.v4.app.FragmentActivity;
    42 import android.support.v4.content.LocalBroadcastManager;
    44 import android.support.v4.content.LocalBroadcastManager;
    43 import android.util.Log;
    45 import android.text.InputType;
       
    46 import android.text.method.PasswordTransformationMethod;
    44 import android.view.View;
    47 import android.view.View;
    45 import android.view.View.OnClickListener;
    48 import android.view.View.OnClickListener;
    46 import android.widget.Button;
    49 import android.widget.Button;
    47 import android.widget.EditText;
    50 import android.widget.EditText;
    48 import android.widget.Toast;
    51 import android.widget.Toast;
    49 
    52 
    50 public class MainActivity extends FragmentActivity {
    53 public class MainActivity extends FragmentActivity {
    51 	private static final int DIALOG_NO_SDCARD = 0;
    54 	private static final int DIALOG_NO_SDCARD = 0;
    52 	private static final int DIALOG_START_NETGAME = 1;
    55 	private static final int DIALOG_START_NETGAME = 1;
       
    56 	private static final int DIALOG_CONNECTING = 2;
       
    57 	private static final int DIALOG_PASSWORD = 3;
    53 	
    58 	
    54 	private static final String PREF_PLAYERNAME = "playerName";
    59 	private static final String PREF_PLAYERNAME = "playerName";
       
    60 	
       
    61 	private LocalBroadcastManager broadcastManager;
    55 	
    62 	
    56 	private Button downloader, startGame;
    63 	private Button downloader, startGame;
    57 	private ProgressDialog assetsDialog;
    64 	private ProgressDialog assetsDialog;
       
    65 	private String passwordedUsername; // TODO ugly - move dialogs to fragments to get rid of e.g. this
    58 
    66 
    59 	public void onCreate(Bundle sis){
    67 	public void onCreate(Bundle sis){
    60 		super.onCreate(sis);
    68 		super.onCreate(sis);
       
    69 		if(sis != null) {
       
    70 			passwordedUsername = sis.getString(PREF_PLAYERNAME);
       
    71 		}
    61 		setContentView(R.layout.main);
    72 		setContentView(R.layout.main);
    62 
    73 
       
    74 		broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
    63 		downloader = (Button)findViewById(R.id.downloader);
    75 		downloader = (Button)findViewById(R.id.downloader);
    64 		startGame = (Button)findViewById(R.id.startGame);
    76 		startGame = (Button)findViewById(R.id.startGame);
    65 		Button joinLobby = (Button)findViewById(R.id.joinLobby);
    77 		Button joinLobby = (Button)findViewById(R.id.joinLobby);
    66 
    78 
    67 		downloader.setOnClickListener(downloadClicker);
    79 		downloader.setOnClickListener(downloadClicker);
    68 		startGame.setOnClickListener(startGameClicker);
    80 		startGame.setOnClickListener(startGameClicker);
    69 		joinLobby.setOnClickListener(new OnClickListener() {
    81 		joinLobby.setOnClickListener(new OnClickListener() {
    70 			public void onClick(View v) {
    82 			public void onClick(View v) {
    71 				if(!NetplayService.isActive()) {
    83 				State state = Netplay.getAppInstance(getApplicationContext()).getState();
       
    84 				switch(state) {
       
    85 				case NOT_CONNECTED:
    72 					showDialog(DIALOG_START_NETGAME);
    86 					showDialog(DIALOG_START_NETGAME);
    73 				} else {
    87 					break;
       
    88 				case CONNECTING:
       
    89 					startWaitingForConnection();
       
    90 					break;
       
    91 				default:
    74 					startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
    92 					startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
       
    93 					break;
    75 				}
    94 				}
    76 			}
    95 			}
    77 		});
    96 		});
    78 
    97 
    79 		if(!Utils.isDataPathAvailable()){
    98 		if(!Utils.isDataPathAvailable()){
    98 				assetsAsyncTask.execute();
   117 				assetsAsyncTask.execute();
    99 			}
   118 			}
   100 		}
   119 		}
   101 	}
   120 	}
   102 
   121 
       
   122 	@Override
       
   123 	protected void onSaveInstanceState(Bundle outState) {
       
   124 		super.onSaveInstanceState(outState);
       
   125 		outState.putString(PREF_PLAYERNAME, passwordedUsername);
       
   126 	}
       
   127 	
       
   128 	@Override
       
   129 	protected void onDestroy() {
       
   130 		super.onDestroy();
       
   131 		stopWaitingForConnection();
       
   132 	}
       
   133 	
       
   134 	@Override
       
   135 	protected void onStart() {
       
   136 		super.onStart();
       
   137 		Netplay.getAppInstance(getApplicationContext()).requestFastTicks();
       
   138 	}
       
   139 	
       
   140 	@Override
       
   141 	protected void onStop() {
       
   142 		super.onStop();
       
   143 		Netplay netplay = Netplay.getAppInstance(getApplicationContext());
       
   144 		netplay.unrequestFastTicks();
       
   145 		if(netplay.getState() == State.CONNECTING) {
       
   146 			netplay.disconnect();
       
   147 		}
       
   148 	}
       
   149 	
   103 	public Dialog onCreateDialog(int id, Bundle args){
   150 	public Dialog onCreateDialog(int id, Bundle args){
   104 		switch(id) {
   151 		switch(id) {
   105 		case DIALOG_NO_SDCARD:
   152 		case DIALOG_NO_SDCARD:
   106 			return createNoSdcardDialog();
   153 			return createNoSdcardDialog();
   107 		case DIALOG_START_NETGAME:
   154 		case DIALOG_START_NETGAME:
   108 			return createStartNetgameDialog();
   155 			return createStartNetgameDialog();
       
   156 		case DIALOG_CONNECTING:
       
   157 			return createConnectingDialog();
       
   158 		case DIALOG_PASSWORD:
       
   159 			return createPasswordDialog();
   109 		default:
   160 		default:
   110 			throw new IndexOutOfBoundsException();
   161 			throw new IndexOutOfBoundsException();
   111 		}
   162 		}
   112 	}
   163 	}
   113 
   164 
   148 				if(playerName.length() > 0) {
   199 				if(playerName.length() > 0) {
   149 					Editor edit = prefs.edit();
   200 					Editor edit = prefs.edit();
   150 					edit.putString(PREF_PLAYERNAME, playerName);
   201 					edit.putString(PREF_PLAYERNAME, playerName);
   151 					edit.commit();
   202 					edit.commit();
   152 					
   203 					
   153 					Intent netplayServiceIntent = new Intent(getApplicationContext(), NetplayService.class);
   204 					startWaitingForConnection();
   154 					netplayServiceIntent.putExtra(NetplayService.EXTRA_PLAYERNAME, playerName);
   205 					Netplay.getAppInstance(getApplicationContext()).connectToDefaultServer(playerName);
   155 					startService(netplayServiceIntent);
       
   156 					
       
   157 					LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(connectedReceiver, new IntentFilter(NetplayService.ACTION_CONNECTED));
       
   158 				}
   206 				}
   159 			}
   207 			}
   160 		});
   208 		});
   161 
   209 
       
   210 		return builder.create();
       
   211 	}
       
   212 	
       
   213 	private Dialog createConnectingDialog() {
       
   214 		ProgressDialog dialog = new ProgressDialog(this);
       
   215 		dialog.setOnCancelListener(new OnCancelListener() {
       
   216 			public void onCancel(DialogInterface dialog) {
       
   217 				Netplay.getAppInstance(getApplicationContext()).disconnect();
       
   218 			}
       
   219 		});
       
   220 		dialog.setIndeterminate(true);
       
   221 		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
       
   222 		dialog.setTitle(R.string.dialog_connecting_title);
       
   223 		dialog.setMessage(getString(R.string.dialog_connecting_message));
       
   224 		return dialog;
       
   225 	}
       
   226 	
       
   227 	private Dialog createPasswordDialog() {
       
   228 		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
       
   229 		final EditText editText = new EditText(this);
       
   230 		editText.setHint(R.string.dialog_password_hint);
       
   231 		editText.setFreezesText(true);
       
   232 		editText.setId(android.R.id.text1);
       
   233 		editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
       
   234 		editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
       
   235 		builder.setView(editText);
       
   236 		builder.setTitle(R.string.dialog_password_title);
       
   237 		builder.setMessage(getString(R.string.dialog_password_message, passwordedUsername));
       
   238 		builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
       
   239 			public void onClick(DialogInterface dialog, int which) {
       
   240 				String password = editText.getText().toString();
       
   241 				editText.setText("");
       
   242 				Netplay.getAppInstance(getApplicationContext()).sendPassword(password);
       
   243 			}
       
   244 		});
       
   245 		builder.setOnCancelListener(new OnCancelListener() {
       
   246 			public void onCancel(DialogInterface dialog) {
       
   247 				Netplay.getAppInstance(getApplicationContext()).disconnect();
       
   248 			}
       
   249 		});
   162 		return builder.create();
   250 		return builder.create();
   163 	}
   251 	}
   164 	
   252 	
   165 	public void onAssetsDownloaded(boolean result){
   253 	public void onAssetsDownloaded(boolean result){
   166 		if(!result){
   254 		if(!result){
   179 		public void onClick(View v){
   267 		public void onClick(View v){
   180 			startActivity(new Intent(getApplicationContext(), StartGameActivity.class));
   268 			startActivity(new Intent(getApplicationContext(), StartGameActivity.class));
   181 		}
   269 		}
   182 	};
   270 	};
   183 	
   271 	
       
   272 	private void startWaitingForConnection() {
       
   273 		broadcastManager.registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
       
   274 		broadcastManager.registerReceiver(connectionFailedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
       
   275 		broadcastManager.registerReceiver(passwordRequestedReceiver, new IntentFilter(Netplay.ACTION_PASSWORD_REQUESTED));
       
   276 		showDialog(DIALOG_CONNECTING);
       
   277 	}
       
   278 	
       
   279 	private void stopWaitingForConnection() {
       
   280 		broadcastManager.unregisterReceiver(connectedReceiver);
       
   281 		broadcastManager.unregisterReceiver(connectionFailedReceiver);
       
   282 		broadcastManager.unregisterReceiver(passwordRequestedReceiver);
       
   283 	}
       
   284 	
   184 	private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
   285 	private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
   185 		@Override
   286 		@Override
   186 		public void onReceive(Context context, Intent intent) {
   287 		public void onReceive(Context context, Intent intent) {
       
   288 			stopWaitingForConnection();
       
   289 			dismissDialog(DIALOG_CONNECTING);
   187 			startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
   290 			startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
   188 		}
   291 		}
   189 	};
   292 	};
       
   293 	
       
   294 	private BroadcastReceiver connectionFailedReceiver = new BroadcastReceiver() {
       
   295 		@Override
       
   296 		public void onReceive(Context context, Intent intent) {
       
   297 			stopWaitingForConnection();
       
   298 			dismissDialog(DIALOG_CONNECTING);
       
   299 			if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) {
       
   300 				Toast.makeText(getApplicationContext(), intent.getStringExtra(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show();
       
   301 			}
       
   302 		}
       
   303 	};
       
   304 	
       
   305 	private BroadcastReceiver passwordRequestedReceiver = new BroadcastReceiver() {
       
   306 		@Override
       
   307 		public void onReceive(Context context, Intent intent) {
       
   308 			passwordedUsername = intent.getStringExtra(Netplay.EXTRA_PLAYERNAME);
       
   309 			showDialog(DIALOG_PASSWORD);
       
   310 		}
       
   311 	};
   190 }
   312 }