project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
author koda
Fri, 17 Feb 2012 18:23:36 +0100
changeset 6700 e04da46ee43c
parent 6623 6bf169f1e97c
child 6842 2e6391f33204
permissions -rw-r--r--
the most important commit of the year
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.PackageInfo;
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 {
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    40
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    41
	private Button downloader, startGame;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    42
	private ProgressDialog assetsDialog;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    43
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    44
	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
    45
		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
    46
		setContentView(R.layout.main);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    47
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    48
		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
    49
		startGame = (Button)findViewById(R.id.startGame);
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);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    53
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    54
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    55
		String cacheDir = Utils.getCachePath(this);
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    56
		if(cacheDir == null){
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    57
			showDialog(0);
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    58
		}else{
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    59
			int versionCode = 0;
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    60
			try {
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    61
				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
    62
			} catch (NameNotFoundException e) {
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    63
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    64
			}
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    65
			boolean assetsCopied = PreferenceManager.getDefaultSharedPreferences(this).getInt("latestAssets", 0) >= versionCode;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    66
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    67
			if(!assetsCopied){
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    68
				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
    69
				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
    70
				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
    71
			}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    72
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    73
	}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    74
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    75
	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
    76
		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
    77
		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
    78
		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
    79
		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
    80
			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
    81
				finish();				
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    82
			}
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
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    85
		return builder.create();
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
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    88
	public void onAssetsDownloaded(boolean result){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    89
		if(result){
6623
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    90
			try {
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    91
				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
    92
				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
    93
			} catch (NameNotFoundException e) {}
6bf169f1e97c check if the latest assets have been moved to the sdcard (compare with versionCode)
Xeli
parents: 6456
diff changeset
    94
			
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    95
		}else{
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    96
			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    97
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    98
		assetsDialog.dismiss();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    99
	}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   100
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   101
	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
   102
		public void onClick(View v){
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   103
			//startActivityForResult(new Intent(getApplicationContext(), DownloadActivity.class), 0);
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
   104
			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
   105
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   106
	};
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   107
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   108
	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
   109
		public void onClick(View v){
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
   110
			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
   111
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   112
	};
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   113
}