| 86 |
Kevin |
1 |
package org.vt.ece4564.latmb;
|
|
|
2 |
|
|
|
3 |
import android.app.Activity;
|
|
|
4 |
import android.app.AlertDialog;
|
|
|
5 |
import android.content.DialogInterface;
|
|
|
6 |
import android.content.Intent;
|
|
|
7 |
import android.content.SharedPreferences;
|
|
|
8 |
import android.os.Bundle;
|
|
|
9 |
import android.preference.PreferenceManager;
|
|
|
10 |
import android.view.Menu;
|
|
|
11 |
import android.view.MenuInflater;
|
|
|
12 |
import android.view.MenuItem;
|
|
|
13 |
import android.view.View;
|
|
|
14 |
import android.view.View.OnClickListener;
|
|
|
15 |
import android.widget.Button;
|
|
|
16 |
import android.widget.CheckBox;
|
|
|
17 |
import android.widget.CompoundButton;
|
|
|
18 |
import android.widget.CompoundButton.OnCheckedChangeListener;
|
|
|
19 |
import android.widget.EditText;
|
|
|
20 |
import android.widget.Toast;
|
|
|
21 |
|
|
|
22 |
public class LATMBActivity extends Activity implements OnClickListener {
|
|
|
23 |
private EditText server_;
|
|
|
24 |
private EditText port_;
|
|
|
25 |
private EditText username_;
|
|
|
26 |
private EditText chatroom_;
|
|
|
27 |
private CheckBox checkGPS_;
|
|
|
28 |
private EditText radius_;
|
|
|
29 |
private Button connect_;
|
|
|
30 |
|
|
|
31 |
@Override
|
|
|
32 |
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
33 |
MenuInflater inflater = getMenuInflater();
|
|
|
34 |
inflater.inflate(R.menu.mainmenu, menu);
|
|
|
35 |
return true;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
@Override
|
|
|
39 |
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
40 |
// Handle item selection
|
|
|
41 |
switch (item.getItemId()) {
|
|
|
42 |
case R.id.menuMainPreference:
|
|
|
43 |
startActivity(new Intent(this, PreferenceActivity.class));
|
|
|
44 |
return true;
|
|
|
45 |
default:
|
|
|
46 |
return super.onOptionsItemSelected(item);
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/** Called when the activity is first created. */
|
|
|
51 |
@Override
|
|
|
52 |
public void onCreate(Bundle savedInstanceState) {
|
|
|
53 |
super.onCreate(savedInstanceState);
|
|
|
54 |
setContentView(R.layout.main);
|
|
|
55 |
|
|
|
56 |
server_ = (EditText) findViewById(R.id.editServer);
|
|
|
57 |
port_ = (EditText) findViewById(R.id.editPort);
|
|
|
58 |
username_ = (EditText) findViewById(R.id.editUser);
|
|
|
59 |
chatroom_ = (EditText) findViewById(R.id.editChatroom);
|
|
|
60 |
checkGPS_ = (CheckBox) findViewById(R.id.checkGPS);
|
|
|
61 |
connect_ = (Button) findViewById(R.id.buttonConnect);
|
|
|
62 |
radius_ = (EditText) findViewById(R.id.editRadius);
|
|
|
63 |
radius_.setEnabled(false);
|
|
|
64 |
|
|
|
65 |
connect_.setOnClickListener(this);
|
|
|
66 |
|
|
|
67 |
// Disable fields depending on if use GPS is checked or not
|
|
|
68 |
checkGPS_.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
|
|
69 |
|
|
|
70 |
@Override
|
|
|
71 |
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
|
|
|
72 |
if (checkGPS_.isChecked()) {
|
|
|
73 |
radius_.setEnabled(true);
|
|
|
74 |
chatroom_.setEnabled(false);
|
|
|
75 |
} else {
|
|
|
76 |
radius_.setEnabled(false);
|
|
|
77 |
chatroom_.setEnabled(true);
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
});
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
@Override
|
|
|
84 |
public void onResume() {
|
|
|
85 |
super.onResume();
|
|
|
86 |
|
|
|
87 |
// Pull the shared preferences and set fields to saved values
|
|
|
88 |
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
89 |
server_.setText(sharedPrefs.getString("pref_Default_IP", ""));
|
|
|
90 |
port_.setText(sharedPrefs.getString("pref_Default_Port", ""));
|
|
|
91 |
username_.setText(sharedPrefs.getString("pref_Default_Username", ""));
|
|
|
92 |
chatroom_.setText(sharedPrefs.getString("pref_Default_Chatroom", ""));
|
|
|
93 |
if (sharedPrefs.getBoolean("pref_Use_GPS", false)) {
|
|
|
94 |
checkGPS_.setChecked(true);
|
|
|
95 |
}
|
|
|
96 |
radius_.setText(sharedPrefs.getString("pref_Search_Radius", "1"));
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
@Override
|
|
|
100 |
public void onClick(View arg0) {
|
|
|
101 |
// Check to make sure that all required fields are filled out
|
|
|
102 |
if (server_.getText().toString().isEmpty() || port_.getText().toString().isEmpty()) {
|
|
|
103 |
Toast.makeText(this, "The server IP and port must be specified to continue", Toast.LENGTH_SHORT).show();
|
|
|
104 |
} else if (chatroom_.getText().toString().isEmpty() && !checkGPS_.isChecked()) {
|
|
|
105 |
Toast.makeText(this, "A chatroom must be specified or use GPS must be selected", Toast.LENGTH_SHORT).show();
|
|
|
106 |
} else {
|
|
|
107 |
// If everything is ok, start the next intent
|
|
|
108 |
Intent i = new Intent(this, MessageBoardActivity.class);
|
|
|
109 |
i.putExtra("server", server_.getText().toString());
|
|
|
110 |
i.putExtra("port", port_.getText().toString());
|
|
|
111 |
i.putExtra("username", username_.getText().toString());
|
|
|
112 |
i.putExtra("chatroom", chatroom_.getText().toString());
|
|
|
113 |
i.putExtra("radius", Double.parseDouble(radius_.getText().toString()));
|
|
|
114 |
i.putExtra("gps", checkGPS_.isChecked());
|
|
|
115 |
|
|
|
116 |
startActivity(i);
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
}
|