project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
author Medo <smaxein@googlemail.com>
Thu, 05 Jul 2012 22:22:48 +0200
changeset 7318 a446eafcddeb
parent 6842 2e6391f33204
child 7328 cd919a2ead4e
permissions -rw-r--r--
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
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;
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    23
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    24
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
    25
import android.app.Dialog;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    26
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
    27
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
    28
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
    29
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
    30
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
    31
import android.preference.PreferenceManager;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    32
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
    33
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
    34
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
    35
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
    36
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
    37
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    38
public class MainActivity extends FragmentActivity {
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    39
6350
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);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    49
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    50
		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
    51
		startGame.setOnClickListener(startGameClicker);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    52
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
    53
		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
    54
			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
    55
		} else {
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    56
			int versionCode = 0;
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    57
			try {
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    58
				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
    59
			} catch (NameNotFoundException e) {
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    60
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    61
			}
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    62
			boolean assetsCopied = PreferenceManager.getDefaultSharedPreferences(this).getInt("latestAssets", 0) >= versionCode;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    63
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    64
			if(!assetsCopied){
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    65
				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
    66
				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
    67
				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
    68
			}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    69
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    70
	}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    71
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    72
	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
    73
		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
    74
		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
    75
		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
    76
		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
    77
			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
    78
				finish();				
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    79
			}
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    80
		});
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    81
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    82
		return builder.create();
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    83
	}
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    84
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    85
	public void onAssetsDownloaded(boolean result){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    86
		if(result){
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    87
			try {
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    88
				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
    89
				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
    90
			} catch (NameNotFoundException e) {}
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    91
			
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    92
		}else{
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    93
			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    94
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    95
		assetsDialog.dismiss();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    96
	}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    97
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    98
	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
    99
		public void onClick(View v){
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   100
			//startActivityForResult(new Intent(getApplicationContext(), DownloadActivity.class), 0);
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   101
			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
   102
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   103
	};
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   104
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   105
	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
   106
		public void onClick(View v){
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   107
			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
   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
}