ObjectsActivity.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. }
  23. @Override
  24. public boolean onOptionsItemSelected(MenuItem item) {
  25. if (item.getItemId() == android.R.id.home) {
  26. Intent intent = new Intent(this, MainActivity.class);
  27. startActivity(intent);
  28. return true;
  29. }
  30. return false;
  31. }
  32. public void openFile(View view) {
  33. Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
  34. chooseFile.setType("*/*");
  35. chooseFile = Intent.createChooser(chooseFile, "Choose a file");
  36. startActivityForResult(chooseFile, FILE_SELECT_CODE);
  37. // Create the ACTION_GET_CONTENT Intent
  38. /*Intent getContentIntent = FileUtils.createGetContentIntent();
  39. Intent intent = Intent.createChooser(getContentIntent, "1. Select a file");
  40. startActivityForResult(intent, FILE_SELECT_CODE);*/
  41. }
  42. //FROM http://stackoverflow.com/questions/7856959/android-file-chooser 14.3.17 13:00
  43. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  44. switch (requestCode) {
  45. case FILE_SELECT_CODE:
  46. if (resultCode == RESULT_OK) {
  47. // Get the Uri of the selected file
  48. Uri uri = data.getData();
  49. // Get the path
  50. File file = new File(uri.getPath());
  51. String fileName = uri.toString();
  52. String extension = "";
  53. int i = fileName.lastIndexOf('.');
  54. if (i > 0) {
  55. extension = fileName.substring(i+1);
  56. }
  57. if (extension.equals("txt")) {
  58. try {
  59. openReadingActivityWith(new FileInputStream(file));
  60. } catch (FileNotFoundException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. /* Parser.parseFile(file, drawView, (TextView) this.findViewById(R.id.textView), (Button) this.findViewById(R.id.button2), scale);
  65. TextView text = (TextView)this.findViewById(R.id.textView2);
  66. text.setText(stringFalse);
  67. text.setTextColor(Color.BLACK);*/
  68. }
  69. break;
  70. }
  71. super.onActivityResult(requestCode, resultCode, data);
  72. }
  73. public void openLoad(View view) {
  74. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_load));
  75. }
  76. public void openPressure(View view) {
  77. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_pressure));
  78. }
  79. public void openAcceleration(View view) {
  80. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_acceleration));
  81. }
  82. public void openTilt90(View view) {
  83. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_tilt90));
  84. }
  85. public void openTilt180(View view) {
  86. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_tilt180));
  87. }
  88. public void openTempRising(View view) {
  89. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_temp_rising));
  90. }
  91. public void openTempFalling(View view) {
  92. openReadingActivityWith(getResources().openRawResource(R.raw.object_specs_temp_falling));
  93. }
  94. public void openReadingActivityWith(InputStream stream) {
  95. BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  96. String[] content = new String[5];
  97. String line;
  98. int i = 0;
  99. try {
  100. while ((line = reader.readLine()) != null) {
  101. content[i++] = line;
  102. }
  103. } catch (IOException e) {
  104. e.printStackTrace();
  105. }
  106. Intent intent = new Intent(this, ReadingActivity.class);
  107. intent.putExtra("file", content);
  108. startActivity(intent);
  109. }
  110. }