project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/StartNetgameDialog.java
author Medo <smaxein@googlemail.com>
Thu, 23 Aug 2012 18:28:33 +0200
changeset 7588 27e5857da6af
parent 7584 7831c84cc644
child 10017 de822cd3df3a
permissions -rw-r--r--
Hedgeroid improvements: - Moved frontlib initialization to a slightly better place - Added new JNA type ByteArrayPtr to move more JNA code into the frontlib package and to improve pointer type safety
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     8
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    13
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    15
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    16
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7444
diff changeset
    19
7444
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import org.hedgewars.hedgeroid.netplay.Netplay;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import android.app.AlertDialog;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import android.app.Dialog;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import android.content.Context;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import android.content.DialogInterface;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import android.content.SharedPreferences;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.content.SharedPreferences.Editor;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import android.os.Bundle;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import android.support.v4.app.DialogFragment;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import android.view.KeyEvent;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
import android.view.inputmethod.EditorInfo;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import android.widget.EditText;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import android.widget.TextView;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import android.widget.TextView.OnEditorActionListener;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
public class StartNetgameDialog extends DialogFragment {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
	private static final String PREF_PLAYERNAME = "playerName";
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	@Override
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	public Dialog onCreateDialog(Bundle savedInstanceState) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
		SharedPreferences prefs = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
		final String playerName = prefs.getString(PREF_PLAYERNAME, "Player");
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
		final EditText editText = new EditText(getActivity());
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
		final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
		
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
		editText.setText(playerName);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
		editText.setHint(R.string.start_netgame_dialog_playername_hint);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		editText.setId(android.R.id.text1);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
		editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
		editText.setSingleLine();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
		builder.setTitle(R.string.start_netgame_dialog_title);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
		builder.setMessage(R.string.start_netgame_dialog_message);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
		builder.setView(editText);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
		builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
			public void onClick(DialogInterface dialog, int which) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
				editText.setText(playerName);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
			}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
		});
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
		
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
		editText.setOnEditorActionListener(new OnEditorActionListener() {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
				boolean handled = false;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
				if(actionId == EditorInfo.IME_ACTION_DONE) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
					startConnection(v.getText().toString());
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
					handled = true;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
				}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
				return handled;
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
			}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
		});
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
		
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
		builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
			public void onClick(DialogInterface dialog, int which) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
				startConnection(editText.getText().toString());
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
			}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
		});
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
		return builder.create();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
	}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
	
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
	private void startConnection(String username) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
		if(username.length() > 0) {
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
			SharedPreferences prefs = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
			Editor edit = prefs.edit();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
			edit.putString(PREF_PLAYERNAME, username);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
			edit.commit();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
			
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
			Netplay.getAppInstance(getActivity().getApplicationContext()).connectToDefaultServer(username);
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
			getDialog().dismiss();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
			((MainActivity)getActivity()).onNetConnectingStarted();
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
		}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
	}
2e31f114f57e Hedgeroid: various improvements:
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
}