ViewLog.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package de.tudarmstadt.informatik.hostage.ui;
  2. import java.text.SimpleDateFormat;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7. import de.tudarmstadt.informatik.hostage.R;
  8. import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
  9. import de.tudarmstadt.informatik.hostage.logging.Record;
  10. import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
  11. import android.annotation.SuppressLint;
  12. import android.app.Activity;
  13. import android.app.AlertDialog;
  14. import android.app.DatePickerDialog;
  15. import android.app.Dialog;
  16. import android.content.Context;
  17. import android.content.DialogInterface;
  18. import android.content.Intent;
  19. import android.content.SharedPreferences;
  20. import android.content.SharedPreferences.Editor;
  21. import android.os.Build;
  22. import android.os.Bundle;
  23. import android.preference.PreferenceManager;
  24. import android.view.Gravity;
  25. import android.view.Menu;
  26. import android.view.MenuItem;
  27. import android.view.View;
  28. import android.widget.DatePicker;
  29. import android.widget.TableLayout;
  30. import android.widget.TableRow;
  31. import android.widget.TextView;
  32. import android.widget.TimePicker;
  33. import android.widget.Toast;
  34. public class ViewLog extends Activity {
  35. DatabaseHandler dbh;
  36. private final Context context = this;
  37. private final SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_viewlog);
  42. dbh = new DatabaseHandler(getApplicationContext());
  43. initStatistic();
  44. }
  45. @Override
  46. public boolean onCreateOptionsMenu(Menu menu) {
  47. getMenuInflater().inflate(R.menu.main, menu);
  48. return true;
  49. }
  50. @Override
  51. public boolean onOptionsItemSelected(MenuItem item) {
  52. // Handle item selection
  53. switch (item.getItemId()) {
  54. case R.id.action_settings:
  55. startActivity(new Intent(this, SettingsActivity.class));
  56. break;
  57. case R.id.action_about:
  58. startActivity(new Intent(this, AboutActivity.class));
  59. break;
  60. default:
  61. }
  62. return super.onOptionsItemSelected(item);
  63. }
  64. public void exportDatabase(View view) {
  65. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  66. builder.setTitle(R.string.export_dialog_title);
  67. builder.setItems(R.array.format, new DialogInterface.OnClickListener() {
  68. @Override
  69. public void onClick(DialogInterface dialog, int position) {
  70. SQLLogger logger = new SQLLogger(context);
  71. logger.exportDatabase(position);
  72. }
  73. });
  74. builder.create();
  75. builder.show();
  76. }
  77. public void uploadDatabase(View view) {
  78. SQLLogger log = new SQLLogger(this);
  79. log.uploadDatabase();
  80. }
  81. public void showLog(View view) {
  82. startActivity(new Intent(this, ViewLogTable.class));
  83. }
  84. @SuppressLint("NewApi")
  85. public void deleteLog(View view) {
  86. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  87. builder.setTitle(R.string.delete_dialog_title);
  88. builder.setItems(R.array.delete_criteria,
  89. new DialogInterface.OnClickListener() {
  90. @Override
  91. public void onClick(DialogInterface dialog, int position) {
  92. switch (position) {
  93. case 0:
  94. deleteByBSSID();
  95. break;
  96. case 1:
  97. deleteByDate();
  98. break;
  99. case 2:
  100. deleteAll();
  101. }
  102. }
  103. });
  104. builder.create();
  105. builder.show();
  106. }
  107. private void deleteByDate() {
  108. showDialog(0);
  109. }
  110. private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {
  111. @Override
  112. public void onDateSet(DatePicker view, int year, int monthOfYear,
  113. int dayOfMonth) {
  114. deleteByDate(year, monthOfYear, dayOfMonth);
  115. }
  116. };
  117. @Override
  118. protected Dialog onCreateDialog(int id) {
  119. switch (id) {
  120. case 0:
  121. final Calendar cal = Calendar.getInstance();
  122. int pYear = cal.get(Calendar.YEAR);
  123. int pMonth = cal.get(Calendar.MONTH);
  124. int pDay = cal.get(Calendar.DAY_OF_MONTH);
  125. return new DatePickerDialog(this, pDateSetListener, pYear, pMonth,
  126. pDay);
  127. }
  128. return null;
  129. }
  130. private void deleteByDate(int year, int monthOfYear, int dayOfMonth) {
  131. TimePicker timePicker = new TimePicker(context);
  132. final Calendar calendar = Calendar.getInstance();
  133. calendar.set(year, monthOfYear, dayOfMonth,
  134. timePicker.getCurrentHour(), timePicker.getCurrentMinute(), 0);
  135. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  136. builder.setTitle(R.string.dialog_clear_database_date)
  137. .setMessage(sdf.format(calendar.getTime()))
  138. .setPositiveButton(R.string.delete,
  139. new DialogInterface.OnClickListener() {
  140. @Override
  141. @SuppressLint("NewApi")
  142. public void onClick(DialogInterface dialog, int id) {
  143. long time = calendar.getTimeInMillis();
  144. // Delete Data
  145. dbh.deleteByDate(time);
  146. Toast.makeText(getApplicationContext(),
  147. "Data sets deleted!",
  148. Toast.LENGTH_SHORT).show();
  149. // Recreate the activity
  150. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  151. recreate();
  152. } else {
  153. Intent intent = getIntent();
  154. finish();
  155. startActivity(intent);
  156. }
  157. }
  158. })
  159. .setNegativeButton(R.string.cancel,
  160. new DialogInterface.OnClickListener() {
  161. @Override
  162. public void onClick(DialogInterface dialog, int id) {
  163. // User cancelled the dialog
  164. }
  165. });
  166. // Create the AlertDialog object
  167. builder.create();
  168. builder.show();
  169. }
  170. private void deleteByBSSID() {
  171. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  172. final String[] bssidArray = dbh.getAllBSSIDS();
  173. for(int i = 0; i < bssidArray.length; i++){
  174. bssidArray[i] = bssidArray[i] + " (" + dbh.getSSID(bssidArray[i]) +")";
  175. }
  176. builder.setTitle(R.string.delete_dialog_title);
  177. builder.setItems(bssidArray, new DialogInterface.OnClickListener() {
  178. @Override
  179. @SuppressLint("NewApi")
  180. public void onClick(DialogInterface dialog, int position) {
  181. dbh.deleteByBSSID(bssidArray[position]);
  182. Toast.makeText(
  183. getApplicationContext(),
  184. "All entries with bssid '" + bssidArray[position]
  185. + "' deleted.", Toast.LENGTH_SHORT).show();
  186. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  187. recreate();
  188. } else {
  189. Intent intent = getIntent();
  190. finish();
  191. startActivity(intent);
  192. }
  193. }
  194. });
  195. builder.create();
  196. builder.show();
  197. }
  198. private void deleteAll() {
  199. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  200. builder.setMessage(R.string.dialog_clear_database)
  201. .setPositiveButton(R.string.clear,
  202. new DialogInterface.OnClickListener() {
  203. @Override
  204. @SuppressLint("NewApi")
  205. public void onClick(DialogInterface dialog, int id) {
  206. // Clear all Data
  207. dbh.clearData();
  208. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
  209. Editor editor = pref.edit();
  210. editor.putInt("ATTACK_ID_COUNTER", 0);
  211. editor.putInt("LAST_UPLOADED_ATTACK_ID", -1);
  212. editor.commit();
  213. Toast.makeText(getApplicationContext(),
  214. "Database cleared!", Toast.LENGTH_SHORT)
  215. .show();
  216. // Recreate the activity
  217. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  218. recreate();
  219. } else {
  220. Intent intent = getIntent();
  221. finish();
  222. startActivity(intent);
  223. }
  224. }
  225. })
  226. .setNegativeButton(R.string.cancel,
  227. new DialogInterface.OnClickListener() {
  228. @Override
  229. public void onClick(DialogInterface dialog, int id) {
  230. // User cancelled the dialog
  231. }
  232. });
  233. // Create the AlertDialog object
  234. builder.create();
  235. builder.show();
  236. }
  237. private void initStatistic() {
  238. TableLayout table = (TableLayout) findViewById(R.id.layoutContainer);
  239. ArrayList<String> protocols = new ArrayList<String>();
  240. protocols.add("Total");
  241. protocols.addAll(Arrays.asList(getResources().getStringArray(
  242. R.array.protocols)));
  243. for (String protocol : protocols) {
  244. TableRow row = new TableRow(this);
  245. TextView protocolName = new TextView(this);
  246. protocolName.setBackgroundResource(R.color.dark_grey);
  247. protocolName.setText(protocol);
  248. protocolName.setTextAppearance(this,
  249. android.R.style.TextAppearance_Medium);
  250. protocolName.setPadding(6, 0, 0, 0);
  251. row.addView(protocolName);
  252. TextView value = new TextView(this);
  253. value.setBackgroundResource(R.color.light_grey);
  254. value.setTextAppearance(this, android.R.style.TextAppearance_Medium);
  255. value.setGravity(Gravity.RIGHT);
  256. value.setPadding(3, 0, 3, 0);
  257. row.addView(value);
  258. if (protocol.equals("Total")) {
  259. value.setText("" + dbh.getAttackCount());
  260. } else {
  261. value.setText("" + dbh.getAttackPerProtokolCount(protocol));
  262. }
  263. table.addView(row);
  264. }
  265. setFirstAndLastAttack();
  266. }
  267. private void setFirstAndLastAttack() {
  268. Record firstAttack = dbh.getRecordOfAttackId(dbh.getSmallestAttackId());
  269. Record lastAttack = dbh.getRecordOfAttackId(dbh.getHighestAttackId());
  270. if (firstAttack != null) {
  271. Date resultdate = new Date(firstAttack.getTimestamp());
  272. TextView text = (TextView) findViewById(R.id.textFirstAttackValue);
  273. text.setText(sdf.format(resultdate));
  274. text = (TextView) findViewById(R.id.textLastAttackValue);
  275. resultdate = new Date(lastAttack.getTimestamp());
  276. text.setText(sdf.format(resultdate));
  277. } else {
  278. TextView text = (TextView) findViewById(R.id.textFirstAttackValue);
  279. text.setText("-");
  280. text = (TextView) findViewById(R.id.textLastAttackValue);
  281. text.setText("-");
  282. }
  283. }
  284. }