ViewLog.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.Date;
  6. import java.util.HashMap;
  7. import de.tudarmstadt.informatik.hostage.R;
  8. import de.tudarmstadt.informatik.hostage.logging.DatabaseHandler;
  9. import de.tudarmstadt.informatik.hostage.logging.SQLLogger;
  10. import android.annotation.SuppressLint;
  11. import android.app.Activity;
  12. import android.app.AlertDialog;
  13. import android.content.Context;
  14. import android.content.DialogInterface;
  15. import android.content.Intent;
  16. import android.content.SharedPreferences;
  17. import android.content.SharedPreferences.Editor;
  18. import android.os.Build;
  19. import android.os.Bundle;
  20. import android.support.v4.app.FragmentTransaction;
  21. import android.util.Log;
  22. import android.view.Gravity;
  23. import android.view.Menu;
  24. import android.view.MenuItem;
  25. import android.view.View;
  26. import android.widget.LinearLayout;
  27. import android.widget.TableLayout;
  28. import android.widget.TableRow;
  29. import android.widget.TextView;
  30. import android.widget.Toast;
  31. public class ViewLog extends Activity {
  32. DatabaseHandler dbh;
  33. private final Context context = this;
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_viewlog);
  38. dbh = new DatabaseHandler(getApplicationContext());
  39. initStatistic();
  40. }
  41. @Override
  42. public boolean onCreateOptionsMenu(Menu menu) {
  43. getMenuInflater().inflate(R.menu.main, menu);
  44. return true;
  45. }
  46. @Override
  47. public boolean onOptionsItemSelected(MenuItem item) {
  48. // Handle item selection
  49. switch (item.getItemId()) {
  50. case R.id.action_settings:
  51. startActivity(new Intent(this, SettingsActivity.class));
  52. default:
  53. return super.onOptionsItemSelected(item);
  54. }
  55. }
  56. public void exportDatabase(View view){
  57. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  58. builder.setTitle(R.string.export_dialog_title);
  59. builder.setItems(R.array.format, new DialogInterface.OnClickListener() {
  60. public void onClick(DialogInterface dialog, int position) {
  61. SQLLogger logger = new SQLLogger(context);
  62. logger.exportDatabase(position);
  63. }
  64. });
  65. builder.create();
  66. builder.show();
  67. }
  68. public void uploadDatabase(View view){
  69. SQLLogger log = new SQLLogger(this);
  70. log.uploadDatabase();
  71. }
  72. public void showLog(View view){
  73. startActivity(new Intent(this, ViewLogTable.class));
  74. }
  75. @SuppressLint("NewApi")
  76. public void deleteLog(View view){
  77. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  78. builder.setMessage(R.string.dialog_clear_database)
  79. .setPositiveButton(R.string.clear, new DialogInterface.OnClickListener() {
  80. public void onClick(DialogInterface dialog, int id) {
  81. // Clear all Data
  82. dbh.clearData();
  83. SharedPreferences pref = getSharedPreferences(MainActivity.SESSION_DATA, Context.MODE_PRIVATE);
  84. Editor editor = pref.edit();
  85. editor.clear();
  86. editor.commit();
  87. Toast.makeText(getApplicationContext(), "Database cleared!", Toast.LENGTH_SHORT).show();
  88. // Recreate the activity
  89. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  90. recreate();
  91. } else{
  92. Intent intent = getIntent();
  93. finish();
  94. startActivity(intent);
  95. }
  96. }
  97. })
  98. .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
  99. public void onClick(DialogInterface dialog, int id) {
  100. // User cancelled the dialog
  101. }
  102. });
  103. // Create the AlertDialog object
  104. builder.create();
  105. builder.show();
  106. }
  107. private void initStatistic() {
  108. TableLayout table = (TableLayout) findViewById(R.id.layoutContainer);
  109. ArrayList<String> protocols = new ArrayList<String>();
  110. protocols.add("Total");
  111. protocols.addAll(Arrays.asList(getResources().getStringArray(
  112. R.array.protocols)));
  113. TableRow tableHeader = new TableRow(this);
  114. TableRow tableContent = new TableRow(this);
  115. tableHeader.setBackgroundResource(R.color.dark_grey);
  116. tableContent.setBackgroundResource(R.color.light_grey);
  117. for (String protocol : protocols) {
  118. TextView protocolName = new TextView(this);
  119. protocolName.setText(protocol);
  120. protocolName.setTextAppearance(this, android.R.style.TextAppearance_Medium);
  121. protocolName.setPadding(3, 0, 3, 0);
  122. tableHeader.addView(protocolName);
  123. TextView value = new TextView(this);
  124. value.setTextAppearance(this, android.R.style.TextAppearance_Medium);
  125. value.setGravity(Gravity.CENTER);
  126. tableContent.addView(value);
  127. if(protocol.equals("Total")){
  128. value.setText("" + dbh.getAttackCount());
  129. }else{
  130. value.setText("" + dbh.getAttackPerProtokolCount(protocol));
  131. }
  132. }
  133. table.addView(tableHeader);
  134. table.addView(tableContent);
  135. setFirstAndLastAttack();
  136. }
  137. private void setFirstAndLastAttack(){
  138. int attackCount = dbh.getRecordCount();
  139. if(attackCount > 0){
  140. SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
  141. Date resultdate = new Date(dbh.getRecordOfAttackId(0).getTimestamp());
  142. TextView text = (TextView) findViewById(R.id.textFirstAttackValue);
  143. text.setText(sdf.format(resultdate));
  144. text = (TextView) findViewById(R.id.textLastAttackValue);
  145. resultdate = new Date(dbh.getRecordOfAttackId(dbh.getAttackCount() - 1).getTimestamp());
  146. text.setText(sdf.format(resultdate));
  147. } else {
  148. TextView text = (TextView) findViewById(R.id.textFirstAttackValue);
  149. text.setText("-");
  150. text = (TextView) findViewById(R.id.textLastAttackValue);
  151. text.setText("-");
  152. }
  153. }
  154. }