ViewLog.java 9.5 KB

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