project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
author Medo <smaxein@googlemail.com>
Wed, 11 Jul 2012 01:52:01 +0200
changeset 7328 cd919a2ead4e
parent 7318 a446eafcddeb
child 7330 867e4fda496e
permissions -rw-r--r--
Hedgeroid: Added experimental lobby chat
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
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    21
import org.hedgewars.hedgeroid.Downloader.DownloadAssets;
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    22
import org.hedgewars.hedgeroid.Downloader.DownloadListActivity;
7328
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    23
import org.hedgewars.hedgeroid.netplay.LobbyActivity;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    24
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    25
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
    26
import android.app.Dialog;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    27
import android.app.ProgressDialog;
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    28
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
    29
import android.content.Intent;
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    30
import android.content.pm.PackageManager.NameNotFoundException;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    31
import android.os.Bundle;
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    32
import android.preference.PreferenceManager;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    33
import android.support.v4.app.FragmentActivity;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    34
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
    35
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
    36
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
    37
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
    38
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    39
public class MainActivity extends FragmentActivity {
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    40
	private Button downloader, startGame;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    41
	private ProgressDialog assetsDialog;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    42
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    43
	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
    44
		super.onCreate(sis);
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    45
		setContentView(R.layout.main);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    46
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    47
		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
    48
		startGame = (Button)findViewById(R.id.startGame);
7328
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    49
		Button joinLobby = (Button)findViewById(R.id.joinLobby);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    50
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    51
		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
    52
		startGame.setOnClickListener(startGameClicker);
7328
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    53
		joinLobby.setOnClickListener(new OnClickListener() {
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    54
			public void onClick(View v) {
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    55
				startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    56
			}
cd919a2ead4e Hedgeroid: Added experimental lobby chat
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    57
		});
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    58
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
    59
		if(!Utils.isDataPathAvailable()){
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    60
			showDialog(0);
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
    61
		} else {
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    62
			int versionCode = 0;
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    63
			try {
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    64
				versionCode = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode;
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    65
			} catch (NameNotFoundException e) {
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    66
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    67
			}
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    68
			boolean assetsCopied = PreferenceManager.getDefaultSharedPreferences(this).getInt("latestAssets", 0) >= versionCode;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    69
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    70
			if(!assetsCopied){
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    71
				DownloadAssets assetsAsyncTask = new DownloadAssets(this);
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    72
				assetsDialog = ProgressDialog.show(this, "Please wait a moment", "Moving assets...");
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    73
				assetsAsyncTask.execute((Object[])null);
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    74
			}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    75
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    76
	}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    77
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    78
	public Dialog onCreateDialog(int id, Bundle args){
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    79
		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
    80
		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
    81
		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
    82
		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
    83
			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
    84
				finish();				
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    85
			}
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    86
		});
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    87
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    88
		return builder.create();
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    89
	}
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    90
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    91
	public void onAssetsDownloaded(boolean result){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    92
		if(result){
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    93
			try {
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    94
				int versionCode = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode;
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    95
				PreferenceManager.getDefaultSharedPreferences(this).edit().putInt("latestAssets", versionCode).commit();
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    96
			} catch (NameNotFoundException e) {}
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    97
			
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    98
		}else{
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    99
			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   100
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   101
		assetsDialog.dismiss();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   102
	}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   103
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   104
	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
   105
		public void onClick(View v){
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   106
			//startActivityForResult(new Intent(getApplicationContext(), DownloadActivity.class), 0);
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   107
			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
   108
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   109
	};
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   110
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   111
	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
   112
		public void onClick(View v){
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   113
			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
   114
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   115
	};
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   116
}