ObjectsActivity.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package de.tu_darmstadt.informatik.tk.olir;
  2. import android.content.Intent;
  3. import android.net.Uri;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.MenuItem;
  7. import android.view.View;
  8. import java.io.BufferedReader;
  9. import java.io.File;
  10. import java.io.FileInputStream;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.InputStreamReader;
  15. public class ObjectsActivity extends AppCompatActivity {
  16. static final int FILE_SELECT_CODE = 1;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_objects);
  21. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  22. getSupportActionBar().setTitle("1. Choose a Sensor");
  23. }
  24. @Override
  25. public boolean onOptionsItemSelected(MenuItem item) {
  26. if (item.getItemId() == android.R.id.home) {
  27. Intent intent = new Intent(this, MainActivity.class);
  28. startActivity(intent);
  29. return true;
  30. }
  31. return false;
  32. }
  33. public void openFile(View view) {
  34. Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
  35. chooseFile.setType("*/*");
  36. chooseFile = Intent.createChooser(chooseFile, "Choose a file");
  37. startActivityForResult(chooseFile, FILE_SELECT_CODE);
  38. }
  39. //FROM http://stackoverflow.com/questions/7856959/android-file-chooser 14.3.17 13:00
  40. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  41. switch (requestCode) {
  42. case FILE_SELECT_CODE:
  43. if (resultCode == RESULT_OK) {
  44. // Get the Uri of the selected file
  45. Uri uri = data.getData();
  46. // Get the path
  47. File file = new File(uri.getPath());
  48. String fileName = uri.toString();
  49. String extension = "";
  50. int i = fileName.lastIndexOf('.');
  51. if (i > 0) {
  52. extension = fileName.substring(i+1);
  53. }
  54. if (extension.equals("txt")) {
  55. try {
  56. openReadingActivityWith(new FileInputStream(file));
  57. } catch (FileNotFoundException e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. }
  62. break;
  63. }
  64. super.onActivityResult(requestCode, resultCode, data);
  65. }
  66. public void openLoad(View view) {
  67. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_load));
  68. }
  69. public void openPressure(View view) {
  70. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_pressure));
  71. }
  72. public void openAcceleration(View view) {
  73. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_acceleration));
  74. }
  75. public void openTilt90(View view) {
  76. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_tilt90));
  77. }
  78. public void openTilt180(View view) {
  79. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_tilt180));
  80. }
  81. public void openTempRising(View view) {
  82. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_temp_rising));
  83. }
  84. public void openTempFalling(View view) {
  85. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_temp_falling));
  86. }
  87. public void openReadingActivityWith(InputStream stream) {
  88. BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  89. String[] content = new String[5];
  90. String line;
  91. int i = 0;
  92. try {
  93. while ((line = reader.readLine()) != null) {
  94. content[i++] = line;
  95. }
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. Intent intent = new Intent(this, ReadingActivity.class);
  100. intent.putExtra("file", content);
  101. startActivity(intent);
  102. }
  103. }