· 8 years ago · Dec 03, 2017, 04:28 PM
1package com.example.patryk.sensory;
2
3import android.database.Cursor;
4import android.database.sqlite.SQLiteException;
5import android.os.Bundle;
6import android.support.design.widget.FloatingActionButton;
7import android.support.design.widget.Snackbar;
8import android.support.v7.app.AppCompatActivity;
9import android.support.v7.widget.Toolbar;
10import android.util.Log;
11import android.view.View;
12import android.view.Menu;
13import android.view.MenuItem;
14import android.content.Context;
15import android.widget.Button;
16import android.widget.TextView;
17import android.database.sqlite.SQLiteDatabase;
18import android.hardware.Sensor;
19import android.hardware.SensorEvent;
20import android.hardware.SensorEventListener;
21import android.hardware.SensorManager;
22import android.widget.Toast;
23
24public class MainActivity extends AppCompatActivity {
25 public Context context;
26 private TextView zyrox,zyroy,zyroz,accx,accy,accz,lightx,pressure;
27 private final String DB_NAME = "Baza.db"; //Nazwa dla nowej bazy
28 SQLiteDatabase baza = null;
29 private static SensorManager sensorService;
30 private Sensor sensor;
31 private static SensorManager sensorService2;
32 private Sensor sensor2;
33 private static SensorManager sensorService3;
34 private Sensor sensor3;
35 private static SensorManager sensorService4;
36 private Sensor sensor4;
37
38 @SuppressWarnings("deprecation")
39
40
41 @Override
42 protected void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 setContentView(R.layout.activity_main);
45 // Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
46 //setSupportActionBar(toolbar);
47 setContentView(R.layout.activity_main);
48
49 context = getApplicationContext();
50 try {
51 baza = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
52 baza.execSQL("CREATE TABLE IF NOT EXISTS Sensory (ZyroskopX FLOAT, ZyroskopY FLOAT,ZyroskopZ FLOAT, AkcelerX FLOAT, AkcelerY FLOAT, AkcelerZ FLOAT, Swiatlo FLOAT, Temperatura INTEGRER)");
53 //baza.execSQL("DELETE FROM Sensory");
54 } catch (SQLiteException e) {
55 Log.e(getClass().getSimpleName(), "Could not create or Open the database");
56 }
57 /* FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
58 fab.setOnClickListener(new View.OnClickListener() {
59 @Override
60 public void onClick(View view) {
61 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
62 .setAction("Action", null).show();
63 }
64 });*/
65
66 sensorService = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
67 sensor = sensorService.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
68 sensorService2 = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
69 sensor2 = sensorService2.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
70 sensorService3 = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
71 sensor3 = sensorService3.getDefaultSensor(Sensor.TYPE_LIGHT);
72 sensorService4 = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
73 sensor4 = sensorService4.getDefaultSensor(Sensor.TYPE_PRESSURE);
74 sensorService.registerListener(mySensorEventListener, sensor, SensorManager.SENSOR_DELAY_FASTEST);
75 sensorService2.registerListener(mySensorEventListener2, sensor2, SensorManager.SENSOR_DELAY_FASTEST);
76 sensorService3.registerListener(mySensorEventListener3, sensor3, SensorManager.SENSOR_DELAY_FASTEST);
77 sensorService4.registerListener(mySensorEventListener4, sensor4, SensorManager.SENSOR_DELAY_FASTEST);
78 zyrox = findViewById(R.id.textView);
79 zyroy = findViewById(R.id.textView2);
80 zyroz = findViewById(R.id.textView3);
81 accx = findViewById(R.id.textView4);
82 accy = findViewById(R.id.textView5);
83 accz = findViewById(R.id.textView6);
84 lightx = findViewById(R.id.textView7);
85 pressure = findViewById(R.id.textView8);
86 }
87
88 private SensorEventListener mySensorEventListener = new SensorEventListener() {
89
90 @Override
91 public void onAccuracyChanged(Sensor sensor, int accuracy) {
92 }
93
94 @Override
95 public void onSensorChanged(SensorEvent event) {
96 if (sensor != null) {
97 float x = event.values[0];
98 float y = event.values[1];
99 float z = event.values[2];
100 baza.execSQL("INSERT INTO Sensory Values( " + x + "," + y + "," + z + ",'','','','','');");
101 String xf = Float.toString(x); String yf = Float.toString(y); String zf = Float.toString(z);
102 zyrox.setText("x:" + xf); zyroy.setText("y: " + yf); zyroz.setText("z: "+zf);
103 }
104 else{
105 String error = "BRAK CZUJNIKA";
106 zyrox.setText(error); zyroy.setText(error); zyroz.setText(error);
107 }
108
109 }
110 };
111 private SensorEventListener mySensorEventListener2 = new SensorEventListener() {
112
113 @Override
114 public void onAccuracyChanged(Sensor sensor, int accuracy) {
115 }
116
117 @Override
118 public void onSensorChanged(SensorEvent event2) {
119 if (sensor != null) {
120 float x2 = event2.values[0];
121 float y2 = event2.values[1];
122 float z2 = event2.values[2];
123 baza.execSQL("INSERT INTO Sensory Values( '','',''," + x2 + "," + y2 + "," + z2 + ",'','');");
124 String xf2 = Float.toString(x2); String yf2 = Float.toString(y2); String zf2 = Float.toString(z2);
125 zyrox.setText("x:" + xf2); zyroy.setText("y: " + yf2); zyroz.setText("z: "+zf2);
126 }
127 else
128 {
129 String error2 = "BRAK CZUJNIKA";
130 accx.setText(error2); accy.setText(error2); accz.setText(error2);
131 }
132 }
133 };
134 private SensorEventListener mySensorEventListener3 = new SensorEventListener() {
135
136 @Override
137 public void onAccuracyChanged(Sensor sensor, int accuracy) {
138 }
139
140 @Override
141 public void onSensorChanged(SensorEvent event3) {
142 if (sensor != null) {
143 float x3 = event3.values[0];
144 float y3 = sensor3.getMaximumRange();
145 baza.execSQL("INSERT INTO Sensory Values('','','','','',''," + x3 + ",'');");
146 String xf3 = Float.toString(x3); String yf3 = Float.toString(y3);
147 lightx.setText(xf3 + yf3);
148 } else {
149 String error3 = "BRAK CZUJNIKA";
150 lightx.setText(error3);
151 }
152 }
153 };
154 private SensorEventListener mySensorEventListener4 = new SensorEventListener() {
155
156 @Override
157 public void onAccuracyChanged(Sensor sensor, int accuracy) {
158 }
159
160 @Override
161 public void onSensorChanged(SensorEvent event4) {
162 if (sensor != null) {
163 float x4 = event4.values[0];
164 baza.execSQL("INSERT INTO Sensory Values( '','','','','','',''," + x4 + ");");
165 String xf4 = Float.toString(x4);
166 pressure.setText(xf4);
167 }
168 else
169 {
170 String error4 = "BRAK CZUJNIKA";
171 pressure.setText(error4);
172 }
173 }
174 };
175 @Override
176 public boolean onCreateOptionsMenu(Menu menu) {
177 // Inflate the menu; this adds items to the action bar if it is present.
178 getMenuInflater().inflate(R.menu.menu_main, menu);
179 return true;
180 }
181
182 @Override
183 public boolean onOptionsItemSelected(MenuItem item) {
184 // Handle action bar item clicks here. The action bar will
185 // automatically handle clicks on the Home/Up button, so long
186 // as you specify a parent activity in AndroidManifest.xml.
187 int id = item.getItemId();
188
189 //noinspection SimplifiableIfStatement
190 if (id == R.id.action_settings) {
191 return true;
192 }
193
194 return super.onOptionsItemSelected(item);
195 }
196}