34 import android.widget.EditText; |
34 import android.widget.EditText; |
35 import android.widget.TextView; |
35 import android.widget.TextView; |
36 import android.widget.TextView.OnEditorActionListener; |
36 import android.widget.TextView.OnEditorActionListener; |
37 |
37 |
38 public class StartNetgameDialog extends DialogFragment { |
38 public class StartNetgameDialog extends DialogFragment { |
39 private static final String PREF_PLAYERNAME = "playerName"; |
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 |
40 |
54 builder.setTitle(R.string.start_netgame_dialog_title); |
41 @Override |
55 builder.setMessage(R.string.start_netgame_dialog_message); |
42 public Dialog onCreateDialog(Bundle savedInstanceState) { |
56 builder.setView(editText); |
43 SharedPreferences prefs = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE); |
57 builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { |
44 final String playerName = prefs.getString(PREF_PLAYERNAME, "Player"); |
58 public void onClick(DialogInterface dialog, int which) { |
45 final EditText editText = new EditText(getActivity()); |
59 editText.setText(playerName); |
46 final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
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 |
47 |
80 return builder.create(); |
48 editText.setText(playerName); |
81 } |
49 editText.setHint(R.string.start_netgame_dialog_playername_hint); |
82 |
50 editText.setId(android.R.id.text1); |
83 private void startConnection(String username) { |
51 editText.setImeOptions(EditorInfo.IME_ACTION_DONE); |
84 if(username.length() > 0) { |
52 editText.setSingleLine(); |
85 SharedPreferences prefs = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE); |
53 |
86 Editor edit = prefs.edit(); |
54 builder.setTitle(R.string.start_netgame_dialog_title); |
87 edit.putString(PREF_PLAYERNAME, username); |
55 builder.setMessage(R.string.start_netgame_dialog_message); |
88 edit.commit(); |
56 builder.setView(editText); |
89 |
57 builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { |
90 Netplay.getAppInstance(getActivity().getApplicationContext()).connectToDefaultServer(username); |
58 public void onClick(DialogInterface dialog, int which) { |
91 getDialog().dismiss(); |
59 editText.setText(playerName); |
92 ((MainActivity)getActivity()).onNetConnectingStarted(); |
60 } |
93 } |
61 }); |
94 } |
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 } |
95 } |