Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35583a4cdd | ||
|
|
d22952d83b |
@@ -1,3 +0,0 @@
|
||||
Sensor als Service einrichten, der aus Activity heraus gestartet werden kann.
|
||||
Stichwort: Intent
|
||||
Siehe Skript Teil 1
|
||||
@@ -1,109 +0,0 @@
|
||||
package com.example.ueberwachungssystem.Detection;
|
||||
|
||||
import static java.lang.Math.sqrt;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
|
||||
|
||||
/**
|
||||
* Accelerometer inherits some methods from abstract Detector class (more info there)
|
||||
*
|
||||
*
|
||||
* USE FROM MAIN ACTIVITY:
|
||||
*
|
||||
* Accelerometer beschleunigungssensor = new Accelerometer(this);
|
||||
* onCreate:
|
||||
* //Accelerometer Setup
|
||||
* beschleunigungssensor = new Accelerometer(this, logger, textViewLog); //logger and textview only for debugging necessary
|
||||
* beschleunigungssensor.getSensor();
|
||||
*
|
||||
* //Starting Detection:
|
||||
* beschleunigungssensor.startDetection();
|
||||
* //Stopping Detection: also recommended at onPause to avoid unnecessary battery consumption
|
||||
* beschleunigungssensor.stopDetection();
|
||||
*
|
||||
* */
|
||||
|
||||
public class Accelerometer extends Detector implements SensorEventListener {
|
||||
|
||||
public SensorManager sensorManager;
|
||||
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION;
|
||||
private Sensor accelerometer;
|
||||
private Context context;
|
||||
boolean alarm = false;
|
||||
//Preallocate memory for the data of each axis of the acceleration sensor
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
float betrag; //Betrag aller drei Achsen sqrt(x*x + y*y + z*z)
|
||||
private DetectionReport detectionReport;
|
||||
|
||||
// In constructor pass Activity, Context and TextView from MainActivity in Accelerometer class
|
||||
public Accelerometer(Context context){
|
||||
super(context); //von Detektor
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void getSensor(){
|
||||
sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
|
||||
if(sensorManager.getSensorList(sensorType).size()==0) {
|
||||
accelerometer = null;
|
||||
}
|
||||
else {
|
||||
accelerometer = sensorManager.getSensorList(sensorType).get(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
try {
|
||||
checkAlarm(event);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void checkAlarm (SensorEvent event) throws InterruptedException {
|
||||
x = event.values[0];
|
||||
y = event.values[1];
|
||||
z = event.values[2];
|
||||
betrag = (float) sqrt(x*x + y*y + z*z);
|
||||
float threshold = 1.5F;
|
||||
|
||||
if (!alarm) {
|
||||
if (betrag > threshold) {
|
||||
alarm = true;
|
||||
detectionReport = new DetectionReport("Accelerometer1", "Bewegung", betrag);
|
||||
reportViolation("Accelo1", "Bewegung", betrag);
|
||||
}
|
||||
} else {
|
||||
if (betrag < threshold) {
|
||||
alarm = false;
|
||||
} else {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startDetection() {
|
||||
// entspricht void start()
|
||||
//getSensor();
|
||||
if (accelerometer != null) {
|
||||
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopDetection() {
|
||||
// entspricht void stop()
|
||||
sensorManager.unregisterListener(this, accelerometer);
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package com.example.ueberwachungssystem.Detection;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
/** Detection Report Class */
|
||||
public class DetectionReport {
|
||||
public String timeStamp;
|
||||
public String detectionType;
|
||||
public float detectedValue;
|
||||
public String detectorID;
|
||||
|
||||
public DetectionReport(String detectorID, String detectionType, float detectedAmplitude) {
|
||||
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
|
||||
this.detectionType = detectionType;
|
||||
this.detectedValue = detectedAmplitude;
|
||||
this.detectorID = detectorID;
|
||||
}
|
||||
|
||||
|
||||
/** Get Detection Report in String format */
|
||||
public String toString() {
|
||||
String time = "Time: " + "[" + this.timeStamp + "]";
|
||||
String type = "Type: " + "[" + this.detectionType + "]";
|
||||
String value = "Value: " + "[" + this.detectedValue + "]";
|
||||
String id = "ID: " + "[" + this.detectorID + "]";
|
||||
|
||||
return String.join("\t", time, type, value, id);
|
||||
}
|
||||
|
||||
/** Debug Report */
|
||||
public void log(String tag) {
|
||||
Log.d(tag, this.toString());
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.example.ueberwachungssystem.Detection;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
|
||||
abstract public class Detector {
|
||||
private OnDetectionListener listener;
|
||||
|
||||
/** Constructor - takes context of current activity */
|
||||
public Detector(Context context) {};
|
||||
|
||||
|
||||
/** On Detection Listener - runs when violation is reported */
|
||||
public interface OnDetectionListener {
|
||||
void onDetection(@NonNull DetectionReport detectionReport);
|
||||
}
|
||||
public void setOnDetectionListener(@NonNull OnDetectionListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
|
||||
/** Triggers onDetectionListener - call this to trigger violation/alarm */
|
||||
public void reportViolation(String detectorID, String detectionType, float amplitude) {
|
||||
if (listener != null) {
|
||||
DetectionReport detectionReport = new DetectionReport(detectorID, detectionType, amplitude);
|
||||
listener.onDetection(detectionReport);
|
||||
} else {
|
||||
throw new IllegalStateException("No listener set for violation reporting");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Starts Detection (abstract method: needs to be overridden in child class) */
|
||||
public abstract void startDetection();
|
||||
|
||||
/** Stops Detection (abstract method: needs to be overridden in child class) */
|
||||
public abstract void stopDetection();
|
||||
}
|
||||
@@ -1,72 +1,15 @@
|
||||
package com.example.ueberwachungssystem;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.ToggleButton;
|
||||
|
||||
import com.example.ueberwachungssystem.Detection.Accelerometer;
|
||||
import com.example.ueberwachungssystem.Detection.DetectionReport;
|
||||
import com.example.ueberwachungssystem.Detection.Detector;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
||||
|
||||
private Accelerometer beschleunigungssensor;
|
||||
private TextView textViewLog;
|
||||
private Button button1;
|
||||
ToggleButton toggleButton1;
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setTitle(this.getClass().getSimpleName());
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
|
||||
toggleButton1 = findViewById(R.id.toggleButton1); //togglebutton um Detektion zu steuern
|
||||
toggleButton1.setOnClickListener(this);
|
||||
|
||||
//Button to clear Log while Debugging
|
||||
button1 = (Button) findViewById(R.id.button1);
|
||||
button1.setOnClickListener(this);
|
||||
|
||||
//Accelerometer Setup
|
||||
beschleunigungssensor = new Accelerometer(this); //logger and textview only for debugging necessary
|
||||
beschleunigungssensor.getSensor();
|
||||
beschleunigungssensor.setOnDetectionListener(new Detector.OnDetectionListener() {
|
||||
@Override
|
||||
public void onDetection(@NonNull DetectionReport detectionReport) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
}
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
beschleunigungssensor.stopDetection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if(v == toggleButton1) {
|
||||
if (toggleButton1.isChecked()) {
|
||||
beschleunigungssensor.startDetection();
|
||||
|
||||
} else {
|
||||
beschleunigungssensor.stopDetection();
|
||||
}
|
||||
} else if (v == button1) {
|
||||
}
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -7,57 +7,12 @@
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textViewLog"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="32dp"
|
||||
android:layout_marginTop="60dp"
|
||||
android:layout_marginEnd="32dp"
|
||||
android:maxLines="25"
|
||||
android:scrollbars="vertical"
|
||||
android:text="TextViewLoggerMain"
|
||||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/textViewWorkerThread"
|
||||
app:layout_constraintVertical_bias="0.0" />
|
||||
|
||||
<ToggleButton
|
||||
android:id="@+id/toggleButton1"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="161dp"
|
||||
android:layout_marginTop="115dp"
|
||||
android:layout_marginEnd="162dp"
|
||||
android:text="ToggleButton"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textViewWorkerThread"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="32dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginEnd="32dp"
|
||||
android:text="textViewWorkerThreadTextView"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/toggleButton1" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button1"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="35dp"
|
||||
android:layout_marginTop="31dp"
|
||||
android:layout_marginEnd="282dp"
|
||||
android:layout_marginBottom="100dp"
|
||||
android:text="Clear Log"
|
||||
app:layout_constraintBottom_toTopOf="@+id/textViewWorkerThread"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
plugins {
|
||||
id 'com.android.application' version '8.0.0' apply false
|
||||
id 'com.android.library' version '8.0.0' apply false
|
||||
id 'com.android.application' version '7.4.2' apply false
|
||||
id 'com.android.library' version '7.4.2' apply false
|
||||
}
|
||||
+1
-3
@@ -18,6 +18,4 @@ android.useAndroidX=true
|
||||
# Enables namespacing of each library's R class so that its R class includes only the
|
||||
# resources declared in the library itself and none from the library's dependencies,
|
||||
# thereby reducing the size of the R class for that library
|
||||
android.nonTransitiveRClass=true
|
||||
android.defaults.buildfeatures.buildconfig=true
|
||||
android.nonFinalResIds=false
|
||||
android.nonTransitiveRClass=true
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#Thu May 11 15:04:30 CEST 2023
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
|
||||
distributionPath=wrapper/dists
|
||||
zipStorePath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
|
||||
Reference in New Issue
Block a user