7444
|
1 |
package org.hedgewars.hedgeroid;
|
|
2 |
|
|
3 |
import org.hedgewars.hedgeroid.netplay.Netplay;
|
|
4 |
|
|
5 |
import android.app.AlertDialog;
|
|
6 |
import android.app.Dialog;
|
|
7 |
import android.content.Context;
|
|
8 |
import android.content.DialogInterface;
|
|
9 |
import android.content.SharedPreferences;
|
|
10 |
import android.content.SharedPreferences.Editor;
|
|
11 |
import android.os.Bundle;
|
|
12 |
import android.support.v4.app.DialogFragment;
|
|
13 |
import android.view.KeyEvent;
|
|
14 |
import android.view.inputmethod.EditorInfo;
|
|
15 |
import android.widget.EditText;
|
|
16 |
import android.widget.TextView;
|
|
17 |
import android.widget.TextView.OnEditorActionListener;
|
|
18 |
|
|
19 |
public class StartNetgameDialog extends DialogFragment {
|
|
20 |
private static final String PREF_PLAYERNAME = "playerName";
|
|
21 |
|
|
22 |
@Override
|
|
23 |
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
24 |
SharedPreferences prefs = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE);
|
|
25 |
final String playerName = prefs.getString(PREF_PLAYERNAME, "Player");
|
|
26 |
final EditText editText = new EditText(getActivity());
|
|
27 |
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
|
28 |
|
|
29 |
editText.setText(playerName);
|
|
30 |
editText.setHint(R.string.start_netgame_dialog_playername_hint);
|
|
31 |
editText.setId(android.R.id.text1);
|
|
32 |
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
|
|
33 |
editText.setSingleLine();
|
|
34 |
|
|
35 |
builder.setTitle(R.string.start_netgame_dialog_title);
|
|
36 |
builder.setMessage(R.string.start_netgame_dialog_message);
|
|
37 |
builder.setView(editText);
|
|
38 |
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
|
39 |
public void onClick(DialogInterface dialog, int which) {
|
|
40 |
editText.setText(playerName);
|
|
41 |
}
|
|
42 |
});
|
|
43 |
|
|
44 |
editText.setOnEditorActionListener(new OnEditorActionListener() {
|
|
45 |
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
46 |
boolean handled = false;
|
|
47 |
if(actionId == EditorInfo.IME_ACTION_DONE) {
|
|
48 |
startConnection(v.getText().toString());
|
|
49 |
handled = true;
|
|
50 |
}
|
|
51 |
return handled;
|
|
52 |
}
|
|
53 |
});
|
|
54 |
|
|
55 |
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
56 |
public void onClick(DialogInterface dialog, int which) {
|
|
57 |
startConnection(editText.getText().toString());
|
|
58 |
}
|
|
59 |
});
|
|
60 |
|
|
61 |
return builder.create();
|
|
62 |
}
|
|
63 |
|
|
64 |
private void startConnection(String username) {
|
|
65 |
if(username.length() > 0) {
|
|
66 |
SharedPreferences prefs = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE);
|
|
67 |
Editor edit = prefs.edit();
|
|
68 |
edit.putString(PREF_PLAYERNAME, username);
|
|
69 |
edit.commit();
|
|
70 |
|
|
71 |
Netplay.getAppInstance(getActivity().getApplicationContext()).connectToDefaultServer(username);
|
|
72 |
getDialog().dismiss();
|
|
73 |
((MainActivity)getActivity()).onNetConnectingStarted();
|
|
74 |
}
|
|
75 |
}
|
|
76 |
}
|