project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
changeset 7352 641f11cdd319
parent 7349 12fdfd2038d4
child 7355 5673e95ef647
equal deleted inserted replaced
7349:12fdfd2038d4 7352:641f11cdd319
    29 import android.app.AlertDialog;
    29 import android.app.AlertDialog;
    30 import android.app.Dialog;
    30 import android.app.Dialog;
    31 import android.app.ProgressDialog;
    31 import android.app.ProgressDialog;
    32 import android.content.DialogInterface;
    32 import android.content.DialogInterface;
    33 import android.content.Intent;
    33 import android.content.Intent;
       
    34 import android.content.SharedPreferences;
       
    35 import android.content.SharedPreferences.Editor;
    34 import android.os.Bundle;
    36 import android.os.Bundle;
    35 import android.support.v4.app.FragmentActivity;
    37 import android.support.v4.app.FragmentActivity;
       
    38 import android.util.Log;
    36 import android.view.View;
    39 import android.view.View;
    37 import android.view.View.OnClickListener;
    40 import android.view.View.OnClickListener;
    38 import android.widget.Button;
    41 import android.widget.Button;
       
    42 import android.widget.EditText;
    39 import android.widget.Toast;
    43 import android.widget.Toast;
    40 
    44 
    41 public class MainActivity extends FragmentActivity {
    45 public class MainActivity extends FragmentActivity {
       
    46 	private static final int DIALOG_NO_SDCARD = 0;
       
    47 	private static final int DIALOG_START_NETGAME = 1;
       
    48 	
       
    49 	private static final String PREF_PLAYERNAME = "playerName";
       
    50 	
    42 	private Button downloader, startGame;
    51 	private Button downloader, startGame;
    43 	private ProgressDialog assetsDialog;
    52 	private ProgressDialog assetsDialog;
    44 
    53 
    45 	public void onCreate(Bundle sis){
    54 	public void onCreate(Bundle sis){
    46 		super.onCreate(sis);
    55 		super.onCreate(sis);
    52 
    61 
    53 		downloader.setOnClickListener(downloadClicker);
    62 		downloader.setOnClickListener(downloadClicker);
    54 		startGame.setOnClickListener(startGameClicker);
    63 		startGame.setOnClickListener(startGameClicker);
    55 		joinLobby.setOnClickListener(new OnClickListener() {
    64 		joinLobby.setOnClickListener(new OnClickListener() {
    56 			public void onClick(View v) {
    65 			public void onClick(View v) {
    57 				startActivity(new Intent(getApplicationContext(), LobbyActivity.class));
    66 				showDialog(DIALOG_START_NETGAME);
    58 			}
    67 			}
    59 		});
    68 		});
    60 
    69 
    61 		if(!Utils.isDataPathAvailable()){
    70 		if(!Utils.isDataPathAvailable()){
    62 			showDialog(0);
    71 			showDialog(DIALOG_NO_SDCARD);
    63 		} else {
    72 		} else {
    64 			String existingVersion = "";
    73 			String existingVersion = "";
    65 			try {
    74 			try {
    66 				File versionFile = new File(Utils.getCachePath(this), "assetsversion.txt");
    75 				File versionFile = new File(Utils.getCachePath(this), "assetsversion.txt");
    67 				existingVersion = Utils.readToString(new FileInputStream(versionFile));
    76 				existingVersion = Utils.readToString(new FileInputStream(versionFile));
    81 			}
    90 			}
    82 		}
    91 		}
    83 	}
    92 	}
    84 
    93 
    85 	public Dialog onCreateDialog(int id, Bundle args){
    94 	public Dialog onCreateDialog(int id, Bundle args){
       
    95 		switch(id) {
       
    96 		case DIALOG_NO_SDCARD:
       
    97 			return createNoSdcardDialog();
       
    98 		case DIALOG_START_NETGAME:
       
    99 			return createStartNetgameDialog();
       
   100 		default:
       
   101 			throw new IndexOutOfBoundsException();
       
   102 		}
       
   103 	}
       
   104 
       
   105 	private Dialog createNoSdcardDialog() {
    86 		AlertDialog.Builder builder = new AlertDialog.Builder(this);
   106 		AlertDialog.Builder builder = new AlertDialog.Builder(this);
    87 		builder.setTitle(R.string.sdcard_not_mounted_title);
   107 		builder.setTitle(R.string.sdcard_not_mounted_title);
    88 		builder.setMessage(R.string.sdcard_not_mounted);
   108 		builder.setMessage(R.string.sdcard_not_mounted);
    89 		builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener(){
   109 		builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener(){
    90 			public void onClick(DialogInterface dialog, int which) {
   110 			public void onClick(DialogInterface dialog, int which) {
    93 		});
   113 		});
    94 
   114 
    95 		return builder.create();
   115 		return builder.create();
    96 	}
   116 	}
    97 
   117 
       
   118 	private Dialog createStartNetgameDialog() {
       
   119 		final SharedPreferences prefs = getPreferences(MODE_PRIVATE);
       
   120 		final String playerName = prefs.getString(PREF_PLAYERNAME, "Player");
       
   121 		final EditText editText = new EditText(this);
       
   122 		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
       
   123 		
       
   124 		editText.setText(playerName);
       
   125 		editText.setHint(R.string.start_netgame_dialog_playername_hint);
       
   126 		editText.setId(android.R.id.text1);
       
   127 
       
   128 		builder.setTitle(R.string.start_netgame_dialog_title);
       
   129 		builder.setMessage(R.string.start_netgame_dialog_message);
       
   130 		builder.setView(editText);
       
   131 		builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
       
   132 			public void onClick(DialogInterface dialog, int which) {
       
   133 				editText.setText(playerName);
       
   134 			}
       
   135 		});
       
   136 		builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
       
   137 			public void onClick(DialogInterface dialog, int which) {
       
   138 				String playerName = editText.getText().toString();
       
   139 				if(playerName.length() > 0) {
       
   140 					Editor edit = prefs.edit();
       
   141 					edit.putString(PREF_PLAYERNAME, playerName);
       
   142 					edit.commit();
       
   143 					
       
   144 					// TODO actually use that name
       
   145 					Intent netplayIntent = new Intent(getApplicationContext(), LobbyActivity.class);
       
   146 					netplayIntent.putExtra("playerName", playerName);
       
   147 					startActivity(netplayIntent);
       
   148 				}
       
   149 			}
       
   150 		});
       
   151 
       
   152 		return builder.create();
       
   153 	}
       
   154 	
    98 	public void onAssetsDownloaded(boolean result){
   155 	public void onAssetsDownloaded(boolean result){
    99 		if(!result){
   156 		if(!result){
   100 			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show();
   157 			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show();
   101 		}
   158 		}
   102 		assetsDialog.dismiss();
   159 		assetsDialog.dismiss();