MainActivity.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package de.tu_darmstadt.informatik.tk.olir;
  2. import android.content.ContextWrapper;
  3. import android.content.Intent;
  4. import android.graphics.Color;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.KeyEvent;
  8. import android.view.Menu;
  9. import android.view.MenuInflater;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.widget.LinearLayout;
  13. import android.widget.TableLayout;
  14. import android.widget.TableRow;
  15. import android.widget.TextView;
  16. import java.io.BufferedReader;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.io.OutputStreamWriter;
  23. import java.util.ArrayList;
  24. public class MainActivity extends AppCompatActivity {
  25. private ArrayList<String> lines;
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30. getSupportActionBar().setTitle("Previous Sensors");
  31. parseHistory();
  32. }
  33. @Override
  34. public boolean onCreateOptionsMenu(Menu menu) {
  35. MenuInflater inflater = getMenuInflater();
  36. inflater.inflate(R.menu.menu, menu);
  37. return true;
  38. }
  39. @Override
  40. public boolean onOptionsItemSelected(MenuItem item) {
  41. if (item.getItemId() == R.id.clear_history) {
  42. try {
  43. OutputStreamWriter writer = new OutputStreamWriter(openFileOutput("history.txt", MODE_PRIVATE));
  44. String content = "";
  45. writer.write(content);
  46. writer.flush();
  47. writer.close();
  48. } catch (FileNotFoundException e) {
  49. e.printStackTrace();
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. TableLayout table = findViewById(R.id.tableLayout);
  54. table.removeAllViews();
  55. }
  56. return true;
  57. }
  58. private void parseHistory() {
  59. lines = new ArrayList<>();
  60. ContextWrapper cw = new ContextWrapper(this);
  61. File file = new File(cw.getFilesDir(), "history.txt");
  62. TableLayout table = findViewById(R.id.tableLayout);
  63. try {
  64. if (file.exists()) {
  65. FileInputStream is = new FileInputStream(file);
  66. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  67. String line = reader.readLine();
  68. while (line != null) {
  69. lines.add(line);
  70. line = reader.readLine();
  71. }
  72. }
  73. } catch (FileNotFoundException e) {
  74. e.printStackTrace();
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. for (int i = lines.size()-1; i >= 0; i--) {
  79. String line = lines.get(i);
  80. String name = line.substring(line.indexOf("|")+1);
  81. name = name.substring(0, name.indexOf("|"));
  82. TableRow row = new TableRow(this);
  83. row.setPadding(10,30,10,30);
  84. row.setTag(i);
  85. row.setOnClickListener(new View.OnClickListener() {
  86. @Override
  87. public void onClick(View v) {
  88. openHistoryEntry(v);
  89. }
  90. });
  91. TextView textView = new TextView(this);
  92. textView.setText(name);
  93. textView.setTextSize(20);
  94. row.addView(textView);
  95. table.addView(row);
  96. View v = new View(this);
  97. v.setLayoutParams(new LinearLayout.LayoutParams(
  98. LinearLayout.LayoutParams.MATCH_PARENT,
  99. 2
  100. ));
  101. v.setBackgroundColor(Color.parseColor("#000000"));
  102. table.addView(v);
  103. }
  104. }
  105. public void addObject(View view) {
  106. Intent intent = new Intent(this, ObjectsActivity.class);
  107. startActivity(intent);
  108. }
  109. @Override
  110. public boolean onKeyDown(int keyCode, KeyEvent event) {
  111. if (keyCode == KeyEvent.KEYCODE_BACK
  112. && event.getRepeatCount() == 0) {
  113. this.finishAffinity();
  114. return true;
  115. }
  116. return super.onKeyDown(keyCode, event);
  117. }
  118. public void openHistoryEntry(View view) {
  119. TableRow row = (TableRow) view;
  120. int index = Integer.valueOf((Integer) row.getTag());
  121. String line = lines.get(index);
  122. String date = line.substring(0, line.indexOf("|"));
  123. line = line.substring(line.indexOf("|") + 1);
  124. String name = line.substring(0, line.indexOf("|"));
  125. String result = line.substring(line.indexOf("|") + 1);
  126. Intent intent = new Intent(this, ResultActivity.class);
  127. intent.putExtra("time", date);
  128. intent.putExtra("name", name);
  129. intent.putExtra("result", Boolean.parseBoolean(result));
  130. startActivity(intent);
  131. }
  132. }