project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
author Medo <smaxein@googlemail.com>
Thu, 26 Jul 2012 11:01:32 +0200
changeset 7358 57a508884052
parent 7355 5673e95ef647
child 7444 2e31f114f57e
permissions -rw-r--r--
Hedgeroid: Major overhaul of the network connection code. Now it is threaded and errors are handled properly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     1
/*
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
6700
e04da46ee43c the most important commit of the year
koda
parents: 6623
diff changeset
     3
 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     4
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     8
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    12
 * GNU General Public License for more details.
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    13
 *
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    17
 */
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    18
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    19
package org.hedgewars.hedgeroid;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    20
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    21
import java.io.File;
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    22
import java.io.FileInputStream;
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    23
import java.io.IOException;
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    24
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    25
import org.hedgewars.hedgeroid.Downloader.DownloadAssets;
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    26
import org.hedgewars.hedgeroid.Downloader.DownloadListActivity;
7328
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    27
import org.hedgewars.hedgeroid.netplay.LobbyActivity;
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    28
import org.hedgewars.hedgeroid.netplay.Netplay;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    29
import org.hedgewars.hedgeroid.netplay.Netplay.State;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    30
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    31
import android.app.AlertDialog;
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    32
import android.app.Dialog;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    33
import android.app.ProgressDialog;
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    34
import android.content.BroadcastReceiver;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    35
import android.content.Context;
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    36
import android.content.DialogInterface;
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    37
import android.content.DialogInterface.OnCancelListener;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    38
import android.content.Intent;
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    39
import android.content.IntentFilter;
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    40
import android.content.SharedPreferences;
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    41
import android.content.SharedPreferences.Editor;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    42
import android.os.Bundle;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    43
import android.support.v4.app.FragmentActivity;
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    44
import android.support.v4.content.LocalBroadcastManager;
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    45
import android.text.InputType;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    46
import android.text.method.PasswordTransformationMethod;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    47
import android.view.View;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    48
import android.view.View.OnClickListener;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    49
import android.widget.Button;
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    50
import android.widget.EditText;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    51
import android.widget.Toast;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    52
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    53
public class MainActivity extends FragmentActivity {
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    54
	private static final int DIALOG_NO_SDCARD = 0;
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    55
	private static final int DIALOG_START_NETGAME = 1;
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    56
	private static final int DIALOG_CONNECTING = 2;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    57
	private static final int DIALOG_PASSWORD = 3;
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    58
	
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    59
	private static final String PREF_PLAYERNAME = "playerName";
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    60
	
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    61
	private LocalBroadcastManager broadcastManager;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    62
	
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    63
	private Button downloader, startGame;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    64
	private ProgressDialog assetsDialog;
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    65
	private String passwordedUsername; // TODO ugly - move dialogs to fragments to get rid of e.g. this
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    66
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    67
	public void onCreate(Bundle sis){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    68
		super.onCreate(sis);
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    69
		if(sis != null) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    70
			passwordedUsername = sis.getString(PREF_PLAYERNAME);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    71
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    72
		setContentView(R.layout.main);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    73
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    74
		broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    75
		downloader = (Button)findViewById(R.id.downloader);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    76
		startGame = (Button)findViewById(R.id.startGame);
7328
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    77
		Button joinLobby = (Button)findViewById(R.id.joinLobby);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    78
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    79
		downloader.setOnClickListener(downloadClicker);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    80
		startGame.setOnClickListener(startGameClicker);
7328
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    81
		joinLobby.setOnClickListener(new OnClickListener() {
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    82
			public void onClick(View v) {
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    83
				State state = Netplay.getAppInstance(getApplicationContext()).getState();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    84
				switch(state) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    85
				case NOT_CONNECTED:
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    86
					showDialog(DIALOG_START_NETGAME);
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    87
					break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    88
				case CONNECTING:
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    89
					startWaitingForConnection();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    90
					break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    91
				default:
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    92
					startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    93
					break;
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    94
				}
7328
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    95
			}
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    96
		});
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    97
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6842
diff changeset
    98
		if(!Utils.isDataPathAvailable()){
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    99
			showDialog(DIALOG_NO_SDCARD);
7318
a446eafcddeb Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents: 6842
diff changeset
   100
		} else {
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   101
			String existingVersion = "";
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
   102
			try {
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   103
				File versionFile = new File(Utils.getCachePath(this), "assetsversion.txt");
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   104
				existingVersion = Utils.readToString(new FileInputStream(versionFile));
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   105
			} catch(IOException e) {
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
   106
			}
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   107
			
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   108
			String newVersion = "";
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   109
			try {
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   110
				newVersion = Utils.readToString(getAssets().open("assetsversion.txt"));
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   111
			} catch(IOException e) {
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   112
			}
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   113
			
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   114
			if(!existingVersion.equals(newVersion)) {
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   115
				DownloadAssets assetsAsyncTask = new DownloadAssets(this);
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7328
diff changeset
   116
				assetsDialog = ProgressDialog.show(this, "Please wait a moment", "Moving assets to SD card...");
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7328
diff changeset
   117
				assetsAsyncTask.execute();
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   118
			}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   119
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   120
	}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   121
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   122
	@Override
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   123
	protected void onSaveInstanceState(Bundle outState) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   124
		super.onSaveInstanceState(outState);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   125
		outState.putString(PREF_PLAYERNAME, passwordedUsername);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   126
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   127
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   128
	@Override
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   129
	protected void onDestroy() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   130
		super.onDestroy();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   131
		stopWaitingForConnection();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   132
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   133
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   134
	@Override
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   135
	protected void onStart() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   136
		super.onStart();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   137
		Netplay.getAppInstance(getApplicationContext()).requestFastTicks();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   138
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   139
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   140
	@Override
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   141
	protected void onStop() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   142
		super.onStop();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   143
		Netplay netplay = Netplay.getAppInstance(getApplicationContext());
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   144
		netplay.unrequestFastTicks();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   145
		if(netplay.getState() == State.CONNECTING) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   146
			netplay.disconnect();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   147
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   148
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   149
	
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   150
	public Dialog onCreateDialog(int id, Bundle args){
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   151
		switch(id) {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   152
		case DIALOG_NO_SDCARD:
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   153
			return createNoSdcardDialog();
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   154
		case DIALOG_START_NETGAME:
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   155
			return createStartNetgameDialog();
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   156
		case DIALOG_CONNECTING:
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   157
			return createConnectingDialog();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   158
		case DIALOG_PASSWORD:
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   159
			return createPasswordDialog();
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   160
		default:
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   161
			throw new IndexOutOfBoundsException();
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   162
		}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   163
	}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   164
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   165
	private Dialog createNoSdcardDialog() {
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   166
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   167
		builder.setTitle(R.string.sdcard_not_mounted_title);
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   168
		builder.setMessage(R.string.sdcard_not_mounted);
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   169
		builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener(){
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   170
			public void onClick(DialogInterface dialog, int which) {
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   171
				finish();				
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   172
			}
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   173
		});
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   174
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   175
		return builder.create();
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   176
	}
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   177
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   178
	private Dialog createStartNetgameDialog() {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   179
		final SharedPreferences prefs = getPreferences(MODE_PRIVATE);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   180
		final String playerName = prefs.getString(PREF_PLAYERNAME, "Player");
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   181
		final EditText editText = new EditText(this);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   182
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   183
		
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   184
		editText.setText(playerName);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   185
		editText.setHint(R.string.start_netgame_dialog_playername_hint);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   186
		editText.setId(android.R.id.text1);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   187
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   188
		builder.setTitle(R.string.start_netgame_dialog_title);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   189
		builder.setMessage(R.string.start_netgame_dialog_message);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   190
		builder.setView(editText);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   191
		builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   192
			public void onClick(DialogInterface dialog, int which) {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   193
				editText.setText(playerName);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   194
			}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   195
		});
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   196
		builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   197
			public void onClick(DialogInterface dialog, int which) {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   198
				String playerName = editText.getText().toString();
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   199
				if(playerName.length() > 0) {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   200
					Editor edit = prefs.edit();
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   201
					edit.putString(PREF_PLAYERNAME, playerName);
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   202
					edit.commit();
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   203
					
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   204
					startWaitingForConnection();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   205
					Netplay.getAppInstance(getApplicationContext()).connectToDefaultServer(playerName);
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   206
				}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   207
			}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   208
		});
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   209
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   210
		return builder.create();
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   211
	}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   212
	
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   213
	private Dialog createConnectingDialog() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   214
		ProgressDialog dialog = new ProgressDialog(this);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   215
		dialog.setOnCancelListener(new OnCancelListener() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   216
			public void onCancel(DialogInterface dialog) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   217
				Netplay.getAppInstance(getApplicationContext()).disconnect();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   218
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   219
		});
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   220
		dialog.setIndeterminate(true);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   221
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   222
		dialog.setTitle(R.string.dialog_connecting_title);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   223
		dialog.setMessage(getString(R.string.dialog_connecting_message));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   224
		return dialog;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   225
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   226
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   227
	private Dialog createPasswordDialog() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   228
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   229
		final EditText editText = new EditText(this);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   230
		editText.setHint(R.string.dialog_password_hint);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   231
		editText.setFreezesText(true);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   232
		editText.setId(android.R.id.text1);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   233
		editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   234
		editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   235
		builder.setView(editText);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   236
		builder.setTitle(R.string.dialog_password_title);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   237
		builder.setMessage(getString(R.string.dialog_password_message, passwordedUsername));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   238
		builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   239
			public void onClick(DialogInterface dialog, int which) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   240
				String password = editText.getText().toString();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   241
				editText.setText("");
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   242
				Netplay.getAppInstance(getApplicationContext()).sendPassword(password);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   243
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   244
		});
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   245
		builder.setOnCancelListener(new OnCancelListener() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   246
			public void onCancel(DialogInterface dialog) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   247
				Netplay.getAppInstance(getApplicationContext()).disconnect();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   248
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   249
		});
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   250
		return builder.create();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   251
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   252
	
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   253
	public void onAssetsDownloaded(boolean result){
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   254
		if(!result){
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   255
			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show();
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   256
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   257
		assetsDialog.dismiss();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   258
	}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   259
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   260
	private OnClickListener downloadClicker = new OnClickListener(){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   261
		public void onClick(View v){
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   262
			startActivityForResult(new Intent(getApplicationContext(), DownloadListActivity.class), 0);
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   263
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   264
	};
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   265
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   266
	private OnClickListener startGameClicker = new OnClickListener(){
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   267
		public void onClick(View v){
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   268
			startActivity(new Intent(getApplicationContext(), StartGameActivity.class));
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   269
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   270
	};
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   271
	
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   272
	private void startWaitingForConnection() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   273
		broadcastManager.registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   274
		broadcastManager.registerReceiver(connectionFailedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   275
		broadcastManager.registerReceiver(passwordRequestedReceiver, new IntentFilter(Netplay.ACTION_PASSWORD_REQUESTED));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   276
		showDialog(DIALOG_CONNECTING);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   277
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   278
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   279
	private void stopWaitingForConnection() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   280
		broadcastManager.unregisterReceiver(connectedReceiver);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   281
		broadcastManager.unregisterReceiver(connectionFailedReceiver);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   282
		broadcastManager.unregisterReceiver(passwordRequestedReceiver);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   283
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   284
	
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   285
	private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   286
		@Override
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   287
		public void onReceive(Context context, Intent intent) {
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   288
			stopWaitingForConnection();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   289
			dismissDialog(DIALOG_CONNECTING);
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   290
			startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   291
		}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   292
	};
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   293
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   294
	private BroadcastReceiver connectionFailedReceiver = new BroadcastReceiver() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   295
		@Override
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   296
		public void onReceive(Context context, Intent intent) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   297
			stopWaitingForConnection();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   298
			dismissDialog(DIALOG_CONNECTING);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   299
			if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   300
				Toast.makeText(getApplicationContext(), intent.getStringExtra(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   301
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   302
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   303
	};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   304
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   305
	private BroadcastReceiver passwordRequestedReceiver = new BroadcastReceiver() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   306
		@Override
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   307
		public void onReceive(Context context, Intent intent) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   308
			passwordedUsername = intent.getStringExtra(Netplay.EXTRA_PLAYERNAME);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   309
			showDialog(DIALOG_PASSWORD);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   310
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   311
	};
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   312
}