32 import android.app.Dialog; |
32 import android.app.Dialog; |
33 import android.app.ProgressDialog; |
33 import android.app.ProgressDialog; |
34 import android.content.BroadcastReceiver; |
34 import android.content.BroadcastReceiver; |
35 import android.content.Context; |
35 import android.content.Context; |
36 import android.content.DialogInterface; |
36 import android.content.DialogInterface; |
37 import android.content.DialogInterface.OnCancelListener; |
|
38 import android.content.Intent; |
37 import android.content.Intent; |
39 import android.content.IntentFilter; |
38 import android.content.IntentFilter; |
40 import android.content.SharedPreferences; |
|
41 import android.content.SharedPreferences.Editor; |
|
42 import android.os.Bundle; |
39 import android.os.Bundle; |
43 import android.support.v4.app.FragmentActivity; |
40 import android.support.v4.app.FragmentActivity; |
|
41 import android.support.v4.app.FragmentManager; |
44 import android.support.v4.content.LocalBroadcastManager; |
42 import android.support.v4.content.LocalBroadcastManager; |
45 import android.text.InputType; |
43 import android.view.Menu; |
46 import android.text.method.PasswordTransformationMethod; |
44 import android.view.MenuItem; |
47 import android.view.View; |
45 import android.view.View; |
48 import android.view.View.OnClickListener; |
46 import android.view.View.OnClickListener; |
49 import android.widget.Button; |
47 import android.widget.Button; |
50 import android.widget.EditText; |
|
51 import android.widget.Toast; |
48 import android.widget.Toast; |
52 |
49 |
53 public class MainActivity extends FragmentActivity { |
50 public class MainActivity extends FragmentActivity { |
54 private static final int DIALOG_NO_SDCARD = 0; |
51 private static final int DIALOG_NO_SDCARD = 0; |
55 private static final int DIALOG_START_NETGAME = 1; |
|
56 private static final int DIALOG_CONNECTING = 2; |
|
57 private static final int DIALOG_PASSWORD = 3; |
|
58 |
|
59 private static final String PREF_PLAYERNAME = "playerName"; |
|
60 |
52 |
61 private LocalBroadcastManager broadcastManager; |
53 private LocalBroadcastManager broadcastManager; |
62 |
|
63 private Button downloader, startGame; |
|
64 private ProgressDialog assetsDialog; |
54 private ProgressDialog assetsDialog; |
65 private String passwordedUsername; // TODO ugly - move dialogs to fragments to get rid of e.g. this |
|
66 |
55 |
67 public void onCreate(Bundle sis){ |
56 public void onCreate(Bundle sis){ |
68 super.onCreate(sis); |
57 super.onCreate(sis); |
69 if(sis != null) { |
|
70 passwordedUsername = sis.getString(PREF_PLAYERNAME); |
|
71 } |
|
72 setContentView(R.layout.main); |
58 setContentView(R.layout.main); |
73 |
59 |
74 broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext()); |
60 broadcastManager = LocalBroadcastManager.getInstance(getApplicationContext()); |
75 downloader = (Button)findViewById(R.id.downloader); |
61 Button startLocalGame = (Button)findViewById(R.id.startGame); |
76 startGame = (Button)findViewById(R.id.startGame); |
62 Button startNetGame = (Button)findViewById(R.id.joinLobby); |
77 Button joinLobby = (Button)findViewById(R.id.joinLobby); |
63 |
78 |
64 startLocalGame.setOnClickListener(startGameListener); |
79 downloader.setOnClickListener(downloadClicker); |
65 startNetGame.setOnClickListener(startNetGameListener); |
80 startGame.setOnClickListener(startGameClicker); |
|
81 joinLobby.setOnClickListener(new OnClickListener() { |
|
82 public void onClick(View v) { |
|
83 State state = Netplay.getAppInstance(getApplicationContext()).getState(); |
|
84 switch(state) { |
|
85 case NOT_CONNECTED: |
|
86 showDialog(DIALOG_START_NETGAME); |
|
87 break; |
|
88 case CONNECTING: |
|
89 startWaitingForConnection(); |
|
90 break; |
|
91 default: |
|
92 startActivity(new Intent(getApplicationContext(), LobbyActivity.class)); |
|
93 break; |
|
94 } |
|
95 } |
|
96 }); |
|
97 |
66 |
98 if(!Utils.isDataPathAvailable()){ |
67 if(!Utils.isDataPathAvailable()){ |
99 showDialog(DIALOG_NO_SDCARD); |
68 showDialog(DIALOG_NO_SDCARD); |
100 } else { |
69 } else { |
101 String existingVersion = ""; |
70 String existingVersion = ""; |
118 } |
87 } |
119 } |
88 } |
120 } |
89 } |
121 |
90 |
122 @Override |
91 @Override |
123 protected void onSaveInstanceState(Bundle outState) { |
92 protected void onResume() { |
124 super.onSaveInstanceState(outState); |
93 super.onResume(); |
125 outState.putString(PREF_PLAYERNAME, passwordedUsername); |
94 broadcastManager.registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED)); |
126 } |
95 broadcastManager.registerReceiver(connectionFailedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED)); |
127 |
96 broadcastManager.registerReceiver(passwordRequestedReceiver, new IntentFilter(Netplay.ACTION_PASSWORD_REQUESTED)); |
128 @Override |
97 } |
129 protected void onDestroy() { |
98 |
130 super.onDestroy(); |
99 @Override |
131 stopWaitingForConnection(); |
100 protected void onPause() { |
|
101 super.onPause(); |
|
102 broadcastManager.unregisterReceiver(connectedReceiver); |
|
103 broadcastManager.unregisterReceiver(connectionFailedReceiver); |
|
104 broadcastManager.unregisterReceiver(passwordRequestedReceiver); |
|
105 Netplay netplay = Netplay.getAppInstance(getApplicationContext()); |
|
106 if(netplay.getState() == State.CONNECTING) { |
|
107 netplay.disconnect(); |
|
108 } |
|
109 } |
|
110 |
|
111 @Override |
|
112 public boolean onCreateOptionsMenu(Menu menu) { |
|
113 super.onCreateOptionsMenu(menu); |
|
114 getMenuInflater().inflate(R.menu.main_options, menu); |
|
115 return true; |
|
116 } |
|
117 |
|
118 @Override |
|
119 public boolean onOptionsItemSelected(MenuItem item) { |
|
120 switch(item.getItemId()) { |
|
121 case R.id.download: |
|
122 startActivityForResult(new Intent(getApplicationContext(), DownloadListActivity.class), 0); |
|
123 return true; |
|
124 case R.id.preferences: |
|
125 Toast.makeText(this, R.string.not_implemented_yet, Toast.LENGTH_SHORT).show(); |
|
126 return true; |
|
127 default: |
|
128 return super.onOptionsItemSelected(item); |
|
129 } |
132 } |
130 } |
133 |
131 |
134 @Override |
132 @Override |
135 protected void onStart() { |
133 protected void onStart() { |
136 super.onStart(); |
134 super.onStart(); |
172 } |
160 } |
173 }); |
161 }); |
174 |
162 |
175 return builder.create(); |
163 return builder.create(); |
176 } |
164 } |
177 |
|
178 private Dialog createStartNetgameDialog() { |
|
179 final SharedPreferences prefs = getPreferences(MODE_PRIVATE); |
|
180 final String playerName = prefs.getString(PREF_PLAYERNAME, "Player"); |
|
181 final EditText editText = new EditText(this); |
|
182 final AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
183 |
|
184 editText.setText(playerName); |
|
185 editText.setHint(R.string.start_netgame_dialog_playername_hint); |
|
186 editText.setId(android.R.id.text1); |
|
187 |
|
188 builder.setTitle(R.string.start_netgame_dialog_title); |
|
189 builder.setMessage(R.string.start_netgame_dialog_message); |
|
190 builder.setView(editText); |
|
191 builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { |
|
192 public void onClick(DialogInterface dialog, int which) { |
|
193 editText.setText(playerName); |
|
194 } |
|
195 }); |
|
196 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
|
197 public void onClick(DialogInterface dialog, int which) { |
|
198 String playerName = editText.getText().toString(); |
|
199 if(playerName.length() > 0) { |
|
200 Editor edit = prefs.edit(); |
|
201 edit.putString(PREF_PLAYERNAME, playerName); |
|
202 edit.commit(); |
|
203 |
|
204 startWaitingForConnection(); |
|
205 Netplay.getAppInstance(getApplicationContext()).connectToDefaultServer(playerName); |
|
206 } |
|
207 } |
|
208 }); |
|
209 |
|
210 return builder.create(); |
|
211 } |
|
212 |
|
213 private Dialog createConnectingDialog() { |
|
214 ProgressDialog dialog = new ProgressDialog(this); |
|
215 dialog.setOnCancelListener(new OnCancelListener() { |
|
216 public void onCancel(DialogInterface dialog) { |
|
217 Netplay.getAppInstance(getApplicationContext()).disconnect(); |
|
218 } |
|
219 }); |
|
220 dialog.setIndeterminate(true); |
|
221 dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); |
|
222 dialog.setTitle(R.string.dialog_connecting_title); |
|
223 dialog.setMessage(getString(R.string.dialog_connecting_message)); |
|
224 return dialog; |
|
225 } |
|
226 |
|
227 private Dialog createPasswordDialog() { |
|
228 final AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
229 final EditText editText = new EditText(this); |
|
230 editText.setHint(R.string.dialog_password_hint); |
|
231 editText.setFreezesText(true); |
|
232 editText.setId(android.R.id.text1); |
|
233 editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); |
|
234 editText.setTransformationMethod(PasswordTransformationMethod.getInstance()); |
|
235 builder.setView(editText); |
|
236 builder.setTitle(R.string.dialog_password_title); |
|
237 builder.setMessage(getString(R.string.dialog_password_message, passwordedUsername)); |
|
238 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
|
239 public void onClick(DialogInterface dialog, int which) { |
|
240 String password = editText.getText().toString(); |
|
241 editText.setText(""); |
|
242 Netplay.getAppInstance(getApplicationContext()).sendPassword(password); |
|
243 } |
|
244 }); |
|
245 builder.setOnCancelListener(new OnCancelListener() { |
|
246 public void onCancel(DialogInterface dialog) { |
|
247 Netplay.getAppInstance(getApplicationContext()).disconnect(); |
|
248 } |
|
249 }); |
|
250 return builder.create(); |
|
251 } |
|
252 |
165 |
253 public void onAssetsDownloaded(boolean result){ |
166 public void onAssetsDownloaded(boolean result){ |
254 if(!result){ |
167 if(!result){ |
255 Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show(); |
168 Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show(); |
256 } |
169 } |
257 assetsDialog.dismiss(); |
170 assetsDialog.dismiss(); |
258 } |
171 } |
259 |
172 |
260 private OnClickListener downloadClicker = new OnClickListener(){ |
173 private final OnClickListener startGameListener = new OnClickListener(){ |
261 public void onClick(View v){ |
|
262 startActivityForResult(new Intent(getApplicationContext(), DownloadListActivity.class), 0); |
|
263 } |
|
264 }; |
|
265 |
|
266 private OnClickListener startGameClicker = new OnClickListener(){ |
|
267 public void onClick(View v){ |
174 public void onClick(View v){ |
268 startActivity(new Intent(getApplicationContext(), StartGameActivity.class)); |
175 startActivity(new Intent(getApplicationContext(), StartGameActivity.class)); |
269 } |
176 } |
270 }; |
177 }; |
271 |
178 |
272 private void startWaitingForConnection() { |
179 private final OnClickListener startNetGameListener = new OnClickListener() { |
273 broadcastManager.registerReceiver(connectedReceiver, new IntentFilter(Netplay.ACTION_CONNECTED)); |
180 public void onClick(View v) { |
274 broadcastManager.registerReceiver(connectionFailedReceiver, new IntentFilter(Netplay.ACTION_DISCONNECTED)); |
181 State state = Netplay.getAppInstance(getApplicationContext()).getState(); |
275 broadcastManager.registerReceiver(passwordRequestedReceiver, new IntentFilter(Netplay.ACTION_PASSWORD_REQUESTED)); |
182 switch(state) { |
276 showDialog(DIALOG_CONNECTING); |
183 case NOT_CONNECTED: |
277 } |
184 FragmentManager fm = getSupportFragmentManager(); |
278 |
185 StartNetgameDialog startNetgameDialog = new StartNetgameDialog(); |
279 private void stopWaitingForConnection() { |
186 startNetgameDialog.show(fm, "start_netgame_dialog"); |
280 broadcastManager.unregisterReceiver(connectedReceiver); |
187 break; |
281 broadcastManager.unregisterReceiver(connectionFailedReceiver); |
188 case CONNECTING: |
282 broadcastManager.unregisterReceiver(passwordRequestedReceiver); |
189 onNetConnectingStarted(); |
283 } |
190 break; |
|
191 default: |
|
192 startActivity(new Intent(getApplicationContext(), LobbyActivity.class)); |
|
193 break; |
|
194 } |
|
195 } |
|
196 }; |
284 |
197 |
285 private BroadcastReceiver connectedReceiver = new BroadcastReceiver() { |
198 private BroadcastReceiver connectedReceiver = new BroadcastReceiver() { |
286 @Override |
199 @Override |
287 public void onReceive(Context context, Intent intent) { |
200 public void onReceive(Context context, Intent intent) { |
288 stopWaitingForConnection(); |
|
289 dismissDialog(DIALOG_CONNECTING); |
|
290 startActivity(new Intent(getApplicationContext(), LobbyActivity.class)); |
201 startActivity(new Intent(getApplicationContext(), LobbyActivity.class)); |
291 } |
202 } |
292 }; |
203 }; |
293 |
204 |
294 private BroadcastReceiver connectionFailedReceiver = new BroadcastReceiver() { |
205 private BroadcastReceiver connectionFailedReceiver = new BroadcastReceiver() { |
295 @Override |
206 @Override |
296 public void onReceive(Context context, Intent intent) { |
207 public void onReceive(Context context, Intent intent) { |
297 stopWaitingForConnection(); |
|
298 dismissDialog(DIALOG_CONNECTING); |
|
299 if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) { |
208 if(intent.getBooleanExtra(Netplay.EXTRA_HAS_ERROR, true)) { |
300 Toast.makeText(getApplicationContext(), intent.getStringExtra(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show(); |
209 Toast.makeText(getApplicationContext(), intent.getStringExtra(Netplay.EXTRA_MESSAGE), Toast.LENGTH_LONG).show(); |
301 } |
210 } |
302 } |
211 } |
303 }; |
212 }; |
304 |
213 |
305 private BroadcastReceiver passwordRequestedReceiver = new BroadcastReceiver() { |
214 private BroadcastReceiver passwordRequestedReceiver = new BroadcastReceiver() { |
306 @Override |
215 @Override |
307 public void onReceive(Context context, Intent intent) { |
216 public void onReceive(Context context, Intent intent) { |
308 passwordedUsername = intent.getStringExtra(Netplay.EXTRA_PLAYERNAME); |
217 FragmentManager fm = getSupportFragmentManager(); |
309 showDialog(DIALOG_PASSWORD); |
218 PasswordDialog passwordDialog = new PasswordDialog(intent.getStringExtra(Netplay.EXTRA_PLAYERNAME)); |
310 } |
219 passwordDialog.show(fm, "fragment_password_dialog"); |
311 }; |
220 } |
|
221 }; |
|
222 |
|
223 public void onNetConnectingStarted() { |
|
224 FragmentManager fm = getSupportFragmentManager(); |
|
225 ConnectingDialog connectingDialog = new ConnectingDialog(); |
|
226 connectingDialog.show(fm, "fragment_connecting_dialog"); |
|
227 } |
312 } |
228 } |