project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
author Xeli
Mon, 28 Nov 2011 16:20:48 +0100
changeset 6456 fed715edc3ee
parent 6350 41b0a9955c47
child 6623 6bf169f1e97c
permissions -rw-r--r--
android: check if the sdcard directory is writable, if it isn't close the app
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
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
     3
 * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
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;
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.os.Bundle;
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.preference.PreferenceManager;
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    31
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
    32
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
    33
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
    34
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
    35
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
    36
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    37
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
    38
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    39
	private Button downloader, startGame;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    40
	private ProgressDialog assetsDialog;
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    41
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    42
	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
    43
		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
    44
		setContentView(R.layout.main);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    45
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    46
		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
    47
		startGame = (Button)findViewById(R.id.startGame);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    48
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    49
		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
    50
		startGame.setOnClickListener(startGameClicker);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    51
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    52
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    53
		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
    54
		if(cacheDir == null){
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    55
			showDialog(0);
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    56
		}else{
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    57
			boolean assetsCopied = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("assetscopied", false);
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    58
6456
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    59
			if(!assetsCopied){
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    60
				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
    61
				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
    62
				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
    63
			}
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    64
		}
6047
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    65
	}
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
	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
    68
		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
    69
		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
    70
		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
    71
		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
    72
			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
    73
				finish();				
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    74
			}
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    75
		});
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    76
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    77
		return builder.create();
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    78
	}
fed715edc3ee android: check if the sdcard directory is writable, if it isn't close the app
Xeli
parents: 6350
diff changeset
    79
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    80
	public void onAssetsDownloaded(boolean result){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    81
		if(result){
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    82
			PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("assetscopied", true).commit();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    83
		}else{
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    84
			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG);
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    85
		}
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    86
		assetsDialog.dismiss();
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    87
	}
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
	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
    90
		public void onClick(View v){
6343
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    91
			//startActivityForResult(new Intent(getApplicationContext(), DownloadActivity.class), 0);
9df5a486f41e first part of the new downloader implementation
Xeli
parents: 6049
diff changeset
    92
			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
    93
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    94
	};
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    95
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
    96
	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
    97
		public void onClick(View v){
6350
41b0a9955c47 new download manager \o/
Xeli
parents: 6343
diff changeset
    98
			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
    99
		}
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   100
	};
10011f051f9c Changed package name from org.hedgewars.mobile to org.hedgewars.hedgeroid to upload it to the market
Xeli
parents:
diff changeset
   101
}