project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
author Medo <smaxein@googlemail.com>
Mon, 20 Aug 2012 20:19:35 +0200
changeset 7582 714310efad8f
parent 7508 763d3961400b
child 7584 7831c84cc644
permissions -rw-r--r--
Hedgeroid: Final sprint to the deadline - Changed local game setup to use the new fragments from the netroom - Pulled team editing into the main activity menu / action bar - Cleanups, renames, anything to make the code look better ;)
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;
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    27
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
    28
import org.hedgewars.hedgeroid.netplay.Netplay.State;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    29
import org.hedgewars.hedgeroid.util.FileUtils;
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;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    37
import android.content.Intent;
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    38
import android.content.IntentFilter;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    39
import android.os.Bundle;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    40
import android.support.v4.app.FragmentActivity;
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    41
import android.support.v4.app.FragmentManager;
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
    42
import android.support.v4.content.LocalBroadcastManager;
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    43
import android.view.Menu;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    44
import android.view.MenuItem;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    45
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
    46
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
    47
import android.widget.Button;
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.widget.Toast;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    49
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    50
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
    51
	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
    52
	
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    53
	private LocalBroadcastManager broadcastManager;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    54
	private ProgressDialog assetsDialog;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    55
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    56
	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
    57
		super.onCreate(sis);
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    58
		setContentView(R.layout.activity_main);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    59
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    60
		broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    61
		Button startLocalGame = (Button)findViewById(R.id.startGame);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    62
		Button startNetGame = (Button)findViewById(R.id.joinLobby);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    63
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    64
		startLocalGame.setOnClickListener(startGameListener);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    65
		startNetGame.setOnClickListener(startNetGameListener);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    66
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    67
		if(!FileUtils.isDataPathAvailable()){
7352
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
    68
			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
    69
		} else {
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    70
			String existingVersion = "";
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    71
			try {
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    72
				File versionFile = new File(FileUtils.getCachePath(this), "assetsversion.txt");
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    73
				existingVersion = FileUtils.readToString(new FileInputStream(versionFile));
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    74
			} catch(IOException e) {
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    75
			}
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    76
			
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    77
			String newVersion = "";
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    78
			try {
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    79
				newVersion = FileUtils.readToString(getAssets().open("assetsversion.txt"));
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    80
			} catch(IOException e) {
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    81
			}
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    82
			
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    83
			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
    84
				DownloadAssets assetsAsyncTask = new DownloadAssets(this);
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7328
diff changeset
    85
				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
    86
				assetsAsyncTask.execute();
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    87
			}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    88
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    89
	}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    90
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    91
	@Override
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    92
	protected void onResume() {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    93
		super.onResume();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    94
		broadcastManager.registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED));
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    95
		broadcastManager.registerReceiver(connectionFailedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED));
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
    96
		broadcastManager.registerReceiver(passwordRequestedReceiver, new IntentFilter(Netplay.ACTION_PASSWORD_REQUESTED));
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    97
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    98
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
    99
	@Override
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   100
	protected void onPause() {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   101
		super.onPause();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   102
		broadcastManager.unregisterReceiver(connectedReceiver);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   103
		broadcastManager.unregisterReceiver(connectionFailedReceiver);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   104
		broadcastManager.unregisterReceiver(passwordRequestedReceiver);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   105
		Netplay netplay = Netplay.getAppInstance(getApplicationContext());
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   106
		if(netplay.getState() == State.CONNECTING) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   107
			netplay.disconnect();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   108
		}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   109
	}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   110
	
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   111
	@Override
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   112
	public boolean onCreateOptionsMenu(Menu menu) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   113
		super.onCreateOptionsMenu(menu);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   114
		getMenuInflater().inflate(R.menu.main_options, menu);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   115
		return true;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   116
	}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   117
	
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   118
	@Override
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   119
	public boolean onOptionsItemSelected(MenuItem item) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   120
		switch(item.getItemId()) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   121
		case R.id.download:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   122
			startActivityForResult(new Intent(this, DownloadListActivity.class), 0);
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   123
			return true;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   124
		case R.id.preferences:
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   125
			Toast.makeText(this, R.string.not_implemented_yet, Toast.LENGTH_SHORT).show();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   126
			return true;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   127
		case R.id.edit_weaponsets:
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   128
			startActivity(new Intent(this, WeaponsetListActivity.class));
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   129
			return true;
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   130
		case R.id.edit_teams:
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   131
			startActivity(new Intent(this, TeamListActivity.class));
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
   132
			return true;
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   133
		default:
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   134
			return super.onOptionsItemSelected(item);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   135
		}
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   136
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   137
	
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   138
	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
   139
		switch(id) {
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   140
		case DIALOG_NO_SDCARD:
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   141
			return createNoSdcardDialog();
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   142
		default:
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   143
			throw new IndexOutOfBoundsException();
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   144
		}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   145
	}
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   146
641f11cdd319 Hedgeroid: Reworked player and room lists, added menus, added playername query
Medo <smaxein@googlemail.com>
parents: 7349
diff changeset
   147
	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
   148
		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
   149
		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
   150
		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
   151
		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
   152
			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
   153
				finish();				
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   154
			}
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   155
		});
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   156
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   157
		return builder.create();
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
   158
	}
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   159
	
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   160
	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
   161
		if(!result){
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
   162
			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show();
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   163
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   164
		assetsDialog.dismiss();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   165
	}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   166
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   167
	private final OnClickListener startGameListener = new OnClickListener(){
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   168
		public void onClick(View v){
7582
714310efad8f Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
   169
			startActivity(new Intent(getApplicationContext(), LocalRoomActivity.class));
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   170
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   171
	};
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   172
	
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   173
	private final OnClickListener startNetGameListener = new OnClickListener() {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   174
		public void onClick(View v) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   175
			State state = Netplay.getAppInstance(getApplicationContext()).getState();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   176
			switch(state) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   177
			case NOT_CONNECTED:
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   178
		        FragmentManager fm = getSupportFragmentManager();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   179
		        StartNetgameDialog startNetgameDialog = new StartNetgameDialog();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   180
		        startNetgameDialog.show(fm, "start_netgame_dialog");
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   181
				break;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   182
			case CONNECTING:
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   183
				onNetConnectingStarted();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   184
				break;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   185
			default:
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   186
				startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   187
				break;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   188
			}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   189
		}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   190
	};
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   191
	
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   192
	private BroadcastReceiver connectedReceiver = new BroadcastReceiver() {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   193
		@Override
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   194
		public void onReceive(Context context, Intent intent) {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   195
			startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   196
		}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents: 7352
diff changeset
   197
	};
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   198
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   199
	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
   200
		@Override
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   201
		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
   202
			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
   203
				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
   204
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   205
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   206
	};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   207
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   208
	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
   209
		@Override
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   210
		public void onReceive(Context context, Intent intent) {
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   211
	        FragmentManager fm = getSupportFragmentManager();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   212
	        PasswordDialog passwordDialog = new PasswordDialog(intent.getStringExtra(Netplay.EXTRA_PLAYERNAME));
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   213
	        passwordDialog.show(fm, "fragment_password_dialog");
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   214
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents: 7355
diff changeset
   215
	};
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   216
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   217
	public void onNetConnectingStarted() {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   218
        FragmentManager fm = getSupportFragmentManager();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   219
        ConnectingDialog connectingDialog = new ConnectingDialog();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   220
        connectingDialog.show(fm, "fragment_connecting_dialog");
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents: 7358
diff changeset
   221
	}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   222
}