author | Wuzzy <almikes@aol.com> |
Fri, 29 Sep 2017 02:10:37 +0200 | |
changeset 12589 | 46a10d3fa619 |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
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 | 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 { |
|
10017 | 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 |
}); |
|
7444 | 62 |
|
10017 | 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 |
}); |
|
7444 | 79 |
|
10017 | 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 |
} |
|
7444 | 95 |
} |