Commit 8cfce439 authored by Oskars Linde's avatar Oskars Linde
Browse files

Cleaned code

parent 7c98a565
......@@ -29,7 +29,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -2,9 +2,6 @@ package com.example.oskars.xyzregister_v2;
// For data export
// C:\Users\Oskars\Downloads\adb>adb -d shell "run-as com.example.oskars.xyzregister_v2 cat /data/data/com.example.oskars.xyzregister_v2/files/accData.txt" > accData.txt
//Uzlikt ka failu uzmet uz 0, vai sāk jaunu ?
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
......@@ -39,208 +36,53 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
private static List<Float> x;
private static List<Float> y;
private static List<Float> z;
private TextView xValue;
private TextView yValue;
private TextView zValue;
private String xValueString;
private String yValueString;
private String zValueString;
private String dataLine;
private static int dataCounter;
private static boolean startDataRegister = false;
private static String filename;
private TextView test;
private TextView minutes;
private TextView hpButton;
private TextView orientationRecognitionTextview;
private EditText user;
private Switch walking;
private Switch up;
private Switch down;
private Switch standing;
private Switch limping;
private Switch headphones;
private Button one;
private Button five;
private Button ten;
private Button clear;
private Button orientationRecognitionButton;
private Switch walking, up, down, standing, limping, headphones;
private Button one, five, ten, clear, orientationRecognitionButton;
private static String activityType;
private static String oldActivityType;
private static float meanAverageX = 0;
private static float meanAverageY = 0;
private static float meanAverageZ = 0;
private static float oldX = 0;
private static float oldY = 0;
private static float oldZ = 0;
private static float alpha = 0.01f;
private static int faceUpYupCounter = 0 ;
private static int faceUpYdownCounter = 0 ;
private static boolean isOrientationRecognitionEnabled = false;
private static boolean stopKickback = false;
private TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
x = new ArrayList<>();
y = new ArrayList<>();
z = new ArrayList<>();
xValue = (TextView) findViewById(R.id.xValueTextview);
yValue = (TextView) findViewById(R.id.yValueTextview);
zValue = (TextView) findViewById(R.id.zValueTextview);
minutes = (TextView) findViewById(R.id.minutesTextview);
test = (TextView) findViewById(R.id.testValueTextview);
hpButton = (TextView) findViewById(R.id.buttonStateValueTextview);
orientationRecognitionTextview = (TextView) findViewById(R.id.orRecStateValueTextview);
walking = (Switch) findViewById(R.id.walkingSwitch);
up = (Switch) findViewById(R.id.upSwitch);
down = (Switch) findViewById(R.id.downSwitch);
standing = (Switch) findViewById(R.id.standingSwitch);
limping = (Switch) findViewById(R.id.limpingSwitch);
headphones = (Switch) findViewById(R.id.headphonesSwitch);
one = (Button) findViewById(R.id.oneButton);
five = (Button) findViewById(R.id.fiveButton);
ten = (Button) findViewById(R.id.tenButton);
clear = (Button) findViewById(R.id.clearButton);
orientationRecognitionButton = (Button) findViewById(R.id.bonus2Button);
user = (EditText) findViewById(R.id.userValueTextview);
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
minutes.setText("1");
}
});
five.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
minutes.setText("5");
}
});
ten.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
minutes.setText("10");
}
});
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
test.setText("FILE CLEARED");
writeToFile(getBaseContext());
}
});
orientationRecognitionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (orientationRecognitionTextview.getText() == "ON"){
orientationRecognitionTextview.setText("OFF");
isOrientationRecognitionEnabled = false;
}
else {
orientationRecognitionTextview.setText("ON");
isOrientationRecognitionEnabled = true;
}
faceUpYupCounter = 0;
faceUpYdownCounter = 0;
}
});
activityType = "0";
oldActivityType = "0";
textToSpeech = new TextToSpeech(this, this);
textToSpeech.setLanguage(Locale.US);
walking.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
activityType = "Walking";
up.setChecked(false);
down.setChecked(false);
standing.setChecked(false);
limping.setChecked(false);
}
}
});
up.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
activityType = "Upstairs";
walking.setChecked(false);
down.setChecked(false);
standing.setChecked(false);
limping.setChecked(false);
}
}
});
down.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
activityType = "Downstairs";
up.setChecked(false);
walking.setChecked(false);
standing.setChecked(false);
limping.setChecked(false);
}
}
});
standing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
activityType = "Standing";
up.setChecked(false);
walking.setChecked(false);
down.setChecked(false);
limping.setChecked(false);
}
}
});
limping.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
activityType = "Limping";
up.setChecked(false);
walking.setChecked(false);
down.setChecked(false);
standing.setChecked(false);
}
}
});
headphones.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
test.setText("Headphone mode enabled");
}
}
});
setButtons();
setSwitches();
setTextviews();
}
protected void onResume() {
super.onResume();
getSensorManager().registerListener(this, getSensorManager().getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
//getSensorManager().registerListener(this, getSensorManager().getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
getSensorManager().registerListener(this, getSensorManager().getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); //getSensorManager().registerListener(this, getSensorManager().getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_GAME);
}
protected void onPause() {
......@@ -263,11 +105,38 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
x.add(event.values[0]);
y.add(event.values[1]);
z.add(event.values[2]);
checkOrientation(event.values[0],event.values[1], event.values[2]);
xValueString = Float.toString(round(x.get(x.size() - 1), 2));
yValueString = Float.toString(round(y.get(y.size() - 1), 2));
zValueString = Float.toString(round(z.get(z.size() - 1), 2));
xValue.setText(xValueString);
yValue.setText(yValueString);
zValue.setText(zValueString);
checkDataWritingSwitches();
checkDataRegister();
}
meanAverageX = (event.values[0]*alpha + meanAverageX * (1-alpha));
meanAverageY = (event.values[1]*alpha + meanAverageY * (1-alpha));
meanAverageZ = (event.values[2]*alpha + meanAverageZ * (1-alpha));
private void checkDataWritingSwitches(){
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
if ((walking.isChecked() || up.isChecked() || down.isChecked() || standing.isChecked() || limping.isChecked()) && !headphones.isChecked()){
operateDataWriting(timestamp);
}
else if (headphones.isChecked() && (walking.isChecked() || up.isChecked() || down.isChecked() || standing.isChecked() || limping.isChecked()) && startDataRegister){
operateDataWritingWithHeadphones(timestamp);
}
else {
one.setEnabled(true);
five.setEnabled(true);
ten.setEnabled(true);
dataCounter = 0;
}
oldActivityType = activityType;
}
private void checkOrientation(float valueX,float valueY, float valueZ){
meanAverageX = (valueX*alpha + meanAverageX * (1-alpha));
meanAverageY = (valueY*alpha + meanAverageY * (1-alpha));
meanAverageZ = (valueZ*alpha + meanAverageZ * (1-alpha));
if (isOrientationRecognitionEnabled){
if(abs(meanAverageX) > abs(meanAverageY) && abs(meanAverageX) > abs(meanAverageZ)){
if (meanAverageX>0){
......@@ -291,53 +160,20 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
else {
orientationRecognitionTextview.setText("Face down Sleep");
}
}
else {
orientationRecognitionTextview.setText("ERROR");
}
oldX = event.values[0];
oldY = event.values[1];
oldZ = event.values[2];
}
//orientationRecognitionTextview.setText(String.format("%s,%s,%s", abs(round(meanAverageX, 1)) , round(meanAverageY, 1), round(meanAverageZ, 1)));
xValueString = Float.toString(round(x.get(x.size() - 1), 2));
yValueString = Float.toString(round(y.get(y.size() - 1), 2));
zValueString = Float.toString(round(z.get(z.size() - 1), 2));
xValue.setText(xValueString);
yValue.setText(yValueString);
zValue.setText(zValueString);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
if ((walking.isChecked() || up.isChecked() || down.isChecked() || standing.isChecked() || limping.isChecked()) && !headphones.isChecked()){
operateDataWriting(timestamp);
}
else if (headphones.isChecked() && (walking.isChecked() || up.isChecked() || down.isChecked() || standing.isChecked() || limping.isChecked()) && startDataRegister){
operateDataWritingWithHeadphones(timestamp);
}
else {
//activityType = "0";
//test.setText("");
one.setEnabled(true);
five.setEnabled(true);
ten.setEnabled(true);
dataCounter = 0;
}
oldActivityType = activityType;
}
private void checkDataRegister(){
if (startDataRegister){
hpButton.setText("Button pressed");
}
else {
hpButton.setText("Button not pressed");
}
}
private static float round(float d, int decimalPlace) {
......@@ -355,7 +191,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd") ;
filename = "accData-" + dateFormat.format(date) + ".txt";
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(filename, Context.MODE_APPEND));
outputStreamWriter.write(data);
outputStreamWriter.close();
......@@ -365,17 +200,14 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
}
}
//Šī funkcija ir lai notīrītu failu
private void writeToFile(Context context) {
private void cleanFile(Context context) {
try {
Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd") ;
filename = "accData-" + dateFormat.format(date) + ".txt";
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput(filename, Context.MODE_PRIVATE));
outputStreamWriter.write("");
outputStreamWriter.close();
test.setText("File (" + filename + ") cleaned");
}
catch (IOException e) {
......@@ -404,17 +236,13 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
}
private void operateDataWriting(Timestamp timestamp){
//33,Jogging,49105962326000,-0.6946377,12.680544,0.50395286
dataLine = String.format("%d,%s,%d,%s,%s,%s%n",33, activityType,
timestamp.getTime() , x.get(x.size() - 1) , y.get(y.size() - 1), z.get(z.size() - 1));
if (activityType.equals(oldActivityType)){
dataCounter++;
if (dataCounter == 170){
textToSpeech.speak("STARTING", TextToSpeech.QUEUE_ADD, null);
}
// STARTING TO TAKE DATA
if (dataCounter >= 200){
if ((dataCounter-200)%2950==0 && (dataCounter-200)/2950 != 0){
......@@ -428,16 +256,13 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
down.setChecked(false);
}
}
test.setText(dataLine);
writeToFile(dataLine , getBaseContext());
one.setEnabled(false);
five.setEnabled(false);
ten.setEnabled(false);
}
// END OF TAKING DATA
else {
test.setText(null);
}
......@@ -451,7 +276,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
private void operateDataWritingWithHeadphones(Timestamp timestamp){
dataLine = String.format("%s,%s,%d,%s,%s,%s%n",user.getText(), activityType,
timestamp.getTime() , x.get(x.size() - 1) , y.get(y.size() - 1), z.get(z.size() - 1));
test.setText(String.valueOf(dataCounter));
dataCounter++;
if (dataCounter == 20){
......@@ -462,5 +286,132 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
}
private void setTextviews(){
xValue = (TextView) findViewById(R.id.xValueTextview);
yValue = (TextView) findViewById(R.id.yValueTextview);
zValue = (TextView) findViewById(R.id.zValueTextview);
minutes = (TextView) findViewById(R.id.minutesTextview);
test = (TextView) findViewById(R.id.testValueTextview);
hpButton = (TextView) findViewById(R.id.buttonStateValueTextview);
orientationRecognitionTextview = (TextView) findViewById(R.id.orRecStateValueTextview);
}
private void setButtons(){
one = (Button) findViewById(R.id.oneButton);
five = (Button) findViewById(R.id.fiveButton);
ten = (Button) findViewById(R.id.tenButton);
clear = (Button) findViewById(R.id.clearButton);
orientationRecognitionButton = (Button) findViewById(R.id.bonus2Button);
}
private void setSwitches(){
walking = (Switch) findViewById(R.id.walkingSwitch);
up = (Switch) findViewById(R.id.upSwitch);
down = (Switch) findViewById(R.id.downSwitch);
standing = (Switch) findViewById(R.id.standingSwitch);
limping = (Switch) findViewById(R.id.limpingSwitch);
headphones = (Switch) findViewById(R.id.headphonesSwitch);
setListenersForSwitches();
}
private void setListenersForSwitches(){
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
minutes.setText("1");
}
});
five.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
minutes.setText("5");
}
});
ten.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
minutes.setText("10");
}
});
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
test.setText("FILE CLEARED");
cleanFile(getBaseContext());
}
});
orientationRecognitionButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (orientationRecognitionTextview.getText() == "ON"){
orientationRecognitionTextview.setText("OFF");
isOrientationRecognitionEnabled = false;
}
else {
orientationRecognitionTextview.setText("ON");
isOrientationRecognitionEnabled = true;
}
}
});
walking.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
activityType = "Walking";
up.setChecked(false);
down.setChecked(false);
standing.setChecked(false);
limping.setChecked(false);
}
}
});
up.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
activityType = "Upstairs";
walking.setChecked(false);
down.setChecked(false);
standing.setChecked(false);
limping.setChecked(false);
}
}
});
down.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
activityType = "Downstairs";
up.setChecked(false);
walking.setChecked(false);
standing.setChecked(false);
limping.setChecked(false);
}
}
});
standing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
activityType = "Standing";
up.setChecked(false);
walking.setChecked(false);
down.setChecked(false);
limping.setChecked(false);
}
}
});
limping.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
activityType = "Limping";
up.setChecked(false);
walking.setChecked(false);
down.setChecked(false);
standing.setChecked(false);
}
}
});
headphones.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
test.setText("Headphone mode enabled");
}
}
});
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment