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