Compare commits

..
20 changed files with 587 additions and 188 deletions

No files matched your search

+18 -2
View File
@@ -2,8 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.greenwatch"> package="com.example.greenwatch">
<uses-permission android:name="android.permission.INTERNET" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
@@ -11,6 +9,24 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.GreenWatch"> android:theme="@style/Theme.GreenWatch">
<activity
android:name=".ConnectionActivity"
android:exported="false" />
<activity
android:name=".VideodetectionAndAccelerometerActivity"
android:exported="false" />
<activity
android:name=".AudiodetectionAndAccelerometerActivity"
android:exported="false" />
<activity
android:name=".VideodetectionActivity"
android:exported="false" />
<activity
android:name=".AudiodetectionActivity"
android:exported="false" />
<activity
android:name=".AccelerometerActivity"
android:exported="false" />
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true"> android:exported="true">
@@ -0,0 +1,28 @@
package com.example.greenwatch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AccelerometerActivity extends AppCompatActivity {
private Button backToMainActivityButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accelerometer);
backToMainActivityButton = (Button) findViewById(R.id.accelerometerBackToMainActivity);
backToMainActivityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
@@ -0,0 +1,27 @@
package com.example.greenwatch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AudiodetectionActivity extends AppCompatActivity {
private Button backToMainActivityButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audiodetection);
backToMainActivityButton = (Button) findViewById(R.id.audiodetectorBackToMainActivity);
backToMainActivityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
@@ -0,0 +1,27 @@
package com.example.greenwatch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AudiodetectionAndAccelerometerActivity extends AppCompatActivity {
private Button backToMainActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audiodetection_and_accelerometer);
backToMainActivity = (Button) findViewById(R.id.audiodetectionAndAccelerometerBackToMainActivity);
backToMainActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
@@ -0,0 +1,27 @@
package com.example.greenwatch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ConnectionActivity extends AppCompatActivity {
private Button backToMainActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connection);
backToMainActivity = (Button) findViewById(R.id.connectionBackToMainActivity);
backToMainActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
@@ -2,55 +2,91 @@ package com.example.greenwatch;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.net.SocketException;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
WifiCommunication communication; //Member WifiCommunication
TextView tvReceivedData; //Test Textview um empfangene Daten anzuzeigen -> Gegen finale Textview in der Anzeige austauschen
//ToDo: Falls finale Textview zum Anzeigen der Nachricht anders heißt, muss das in der Klasse WifiCommunication.java angepasst werden!
// Wo anpassen: Receive Thread, Zeile 58
private EditText etSendMessage; //Test Edittext um eine Nachricht zu senden -> gegen Nachricht von den Sensoren austauschen
private Button btnSend; //Test Sendebutton
private String sendmsg = "default"; private Button audiodetectionButton;
private Button videodetectionButton;
private Button accelerometerButton;
private Button audiodetectionAndAccelerometerButton;
private Button videodetectionAndAccelerometerButton;
private Button connectionButton;
@SuppressLint("MissingInflatedId")
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
xmlToJava(); //Funktionsaufruf, wo die Testbuttons etc angelegt sind audiodetectionButton = (Button) findViewById(R.id.audiodetectionButton);
videodetectionButton = (Button) findViewById(R.id.videodetectionButton);
accelerometerButton = (Button) findViewById(R.id.accelerometerButton);
audiodetectionAndAccelerometerButton = (Button) findViewById(R.id.audiodetectionAndAccelerometerButton);
videodetectionAndAccelerometerButton = (Button) findViewById(R.id.videodetectionAndAccelerometerButton);
connectionButton = (Button) findViewById(R.id.connectionButton);
audiodetectionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openAudiodetectionActivity();
}
});
videodetectionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openVideodetectionActivity();
}
});
accelerometerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openAccelerometerActivity();
}
});
audiodetectionAndAccelerometerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openAudiodetectionAndAccelerometerActivity();
}
});
videodetectionAndAccelerometerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openVideodetectionAndAccelerometerActivity();
}
});
connectionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openConnectionActivity();
}
});
}
public void openAudiodetectionActivity(){
Intent intent = new Intent(this, AudiodetectionActivity.class);
startActivity(intent);
}
public void openVideodetectionActivity(){
Intent intent = new Intent(this, VideodetectionActivity.class);
startActivity(intent);
}
public void openAccelerometerActivity(){
Intent intent = new Intent(this, AccelerometerActivity.class);
startActivity(intent);
}
public void openAudiodetectionAndAccelerometerActivity(){
Intent intent = new Intent(this, AudiodetectionAndAccelerometerActivity.class);
startActivity(intent);
}
public void openVideodetectionAndAccelerometerActivity(){
Intent intent = new Intent(this, VideodetectionAndAccelerometerActivity.class);
startActivity(intent);
}
public void openConnectionActivity(){
Intent intent = new Intent(this, ConnectionActivity.class);
startActivity(intent);
} }
public void xmlToJava(){
tvReceivedData = findViewById(R.id.tvRecTxt); //Test Textview
etSendMessage = findViewById(R.id.sendMessage); //Test Nachricht
btnSend = findViewById(R.id.btnSend); //Test Sendebefehl
}
public void onClickSend(View view) throws SocketException{ //ToDo: Sendebefehl mit boolscher Variable lösen --> wo setzen?
sendmsg = etSendMessage.getText().toString(); //ToDo: hier String von Sensoren übergeben
communication.setMessage(sendmsg, true); //übergibt zu sendenden String und setzt die entscheidende Boolsche Variable fürs Senden
}
@Override
protected void onResume() {
super.onResume();
//Erstellen des Members
communication = new WifiCommunication(MainActivity.this, MainActivity.this, 1234);
}
@Override
protected void onPause() {
super.onPause();
//schließt socket
communication.stopCommunication();
}
} }
@@ -0,0 +1,27 @@
package com.example.greenwatch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class VideodetectionActivity extends AppCompatActivity {
private Button backToMainActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videodetection);
backToMainActivity = (Button) findViewById(R.id.videodetectorBackToMainActivity);
backToMainActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
@@ -0,0 +1,27 @@
package com.example.greenwatch;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class VideodetectionAndAccelerometerActivity extends AppCompatActivity {
private Button backToMainActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videodetection_and_accelerometer);
backToMainActivity = (Button) findViewById(R.id.videodetectionAndAccelerometerBackToMainActivity);
backToMainActivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
@@ -1,124 +0,0 @@
package com.example.greenwatch;
import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.Toast;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class WifiCommunication {
private final MainActivity mainActivity;
private final InetAddress address;
private final int port;
private final Context context;
private boolean newMsg;
private String rxString;
private String sendMsg;
//private String sendMsg = "default";
private final DatagramSocket socket;
volatile private boolean running;
@SuppressLint("SetTextI18n")
public WifiCommunication(MainActivity mainActivity, Context context, int port) {
this.mainActivity = mainActivity;
this.port = port;
this.context = context;
try {
socket = new DatagramSocket(this.port);
socket.setBroadcast(true);
address = InetAddress.getByName("255.255.255.255");
running = true;
new ReceiveThread().start();
new SendThread().start();
} catch (SocketException | UnknownHostException e) {
throw new RuntimeException(e);
}
mainActivity.runOnUiThread(() -> Toast.makeText(context,"Communication running", Toast.LENGTH_SHORT).show());
}
private class ReceiveThread extends Thread {
private String rxString = "";
private String previousRxString = "";
@Override
public void run() {
try {
do {
byte[] rxBuffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(rxBuffer, rxBuffer.length);
socket.receive(packet);
rxString = new String(packet.getData(), 0, packet.getLength());
if(!previousRxString.equals(rxString))
mainActivity.runOnUiThread(() -> mainActivity.tvReceivedData.setText(rxString));
previousRxString = rxString;
} while (running);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
public String showMessage(){
return rxString;
}
public void setMessage(String message, boolean newMsg){
this.sendMsg=message;
this.newMsg = newMsg;
}
private class SendThread extends Thread {
private int tmpCnt = 0;
@Override
public void run() {
try {
do {
if(newMsg == true)
{
newMsg = false;
String txString = getLocalIpAddress() + " sends. #" + tmpCnt++ + sendMsg;
byte[] txBuffer = txString.getBytes();
DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port);
for(int i = 0; i < 20; i++) {
socket.send(txPacket);
}
}
} while (running);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement();
for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
InetAddress inetAddress = addresses.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
public void stopCommunication() {
running = false;
socket.close();
}
}
@@ -0,0 +1,29 @@
package com.example.greenwatch.mvc;
public class Device {
private String sensor;
private Boolean isActive;
public Device(String Sensor, Boolean Status) {
this.sensor = Sensor;
this.isActive = Status;
}
public void setSensor(String Sensor) {
this.sensor = Sensor;
}
public String getSensor() {
return sensor;
}
public void setIsActive(Boolean Status) {
this.isActive = Status;
}
public Boolean getIsActive() {
return isActive;
}
}
@@ -0,0 +1,33 @@
package com.example.greenwatch.mvc;
public class DeviceController {
private DeviceModel deviceModel;
private DeviceView deviceView;
public DeviceController(DeviceView view) {
this.deviceModel = DeviceModel.getInstance();
this.deviceView = view;
}
public void createDevice(String Sensor, Boolean Status) {
Device device = new Device(Sensor, Status);
deviceModel.addDevice(device);
// todo: Inform view and sender about changes
}
public void setDeviceSensor(String UUID, String Sensor) {
deviceModel.setDeviceSensor(UUID, Sensor);
}
public String getDeviceSensor(String UUID) {
return deviceModel.getDeviceSensor(UUID);
}
public void setDeviceStatus(String UUID, Boolean Status) {
deviceModel.setDeviceStatus(UUID, Status);
}
public Boolean getDeviceStatus(String UUID) {
return deviceModel.getDeviceStatus(UUID);
}
}
@@ -0,0 +1,47 @@
package com.example.greenwatch.mvc;
import java.util.Vector;
public class DeviceModel {
private static DeviceModel itemModelInstance;
// todo: should be a hashmap with UUID Keys
private Vector<Device> connectetDevicesList;
private DeviceModel() {
connectetDevicesList = new Vector<Device>();
}
public static synchronized DeviceModel getInstance() {
if (itemModelInstance == null){
itemModelInstance = new DeviceModel();
}
return itemModelInstance;
}
public void addDevice(Device device) {
// todo: Check if divece already exist
connectetDevicesList.add(device);
}
public Vector<Device> getConnectetDevicesList() {
return connectetDevicesList;
}
public void setDeviceSensor(String UUID, String Sensor) {
// todo: iterate through HashMap and find Divece for UUID
}
public String getDeviceSensor(String UUID) {
// todo: iterate through HashMap and find Divece for UUID
return "hasToBeDone";
}
public void setDeviceStatus(String UUID, Boolean Status) {
// todo: iterate through HashMap and find Divece for UUID
}
public Boolean getDeviceStatus(String UUID) {
// todo: iterate through HashMap and find Divece for UUID
return false;
}
}
@@ -0,0 +1,10 @@
package com.example.greenwatch.mvc;
import java.util.Vector;
public class DeviceView {
public void updateView(Vector<Device> deviceVector) {
// todo: what should be displayed
}
}
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".AccelerometerActivity">
<TextView
android:id="@+id/tvAccelerometerStatusmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Accelerometer Activity">
</TextView>
<Button
android:id="@+id/accelerometerBackToMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to MainActivity">
</Button>
</LinearLayout>
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".AudiodetectionActivity">
<TextView
android:id="@+id/tvAudiodetectionStatusmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Audiodetection Activity">
</TextView>
<Button
android:id="@+id/audiodetectorBackToMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to MainActivity">
</Button>
</LinearLayout>
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".AudiodetectionAndAccelerometerActivity">
<TextView
android:id="@+id/tvAudiodetectionAndAccelerometerStatusmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Audiodetection + Accelerometer Activity">
</TextView>
<Button
android:id="@+id/audiodetectionAndAccelerometerBackToMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to MainActivity">
</Button>
</LinearLayout>
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".ConnectionActivity">
<TextView
android:id="@+id/tvConnectionStatusmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Connection Activity">
</TextView>
<Button
android:id="@+id/connectionBackToMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to MainActivity">
</Button>
</LinearLayout>
+64 -25
View File
@@ -1,36 +1,75 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity"> tools:context=".MainActivity">
<TextView <TextView
android:id="@+id/tvRecTxt" android:id="@+id/tvStatusmessage"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:text="Received Message" android:text="Hello World!"
app:layout_constraintTop_toBottomOf="@id/sendMessage" android:layout_weight="1">
app:layout_constraintStart_toStartOf="parent"/> </TextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<EditText <Button
android:id="@+id/sendMessage" android:id="@+id/audiodetectionButton"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="wrap_content"
android:ems="10" android:layout_weight="1"
android:inputType="textPersonName" android:text="Audiodetection">
android:text="Send Message" </Button>
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<Button <Button
android:id="@+id/btnSend" android:id="@+id/videodetectionButton"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Send" android:layout_weight="1"
android:onClick="onClickSend" android:text="Videodetection">
app:layout_constraintTop_toTopOf="parent" </Button>
app:layout_constraintStart_toEndOf="@id/sendMessage"/>
</androidx.constraintlayout.widget.ConstraintLayout> <Button
android:id="@+id/accelerometerButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Accelerometer">
</Button>
<Button
android:id="@+id/audiodetectionAndAccelerometerButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Audiodetection + Accelerometer">
</Button>
<Button
android:id="@+id/videodetectionAndAccelerometerButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Videodetection + Accelerometer">
</Button>
<Button
android:id="@+id/connectionButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Connect to Devices">
</Button>
</LinearLayout>
</LinearLayout>
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".VideodetectionActivity">
<TextView
android:id="@+id/tvVideodetectionStatusmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Videodetection Activity">
</TextView>
<Button
android:id="@+id/videodetectorBackToMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to MainActivity">
</Button>
</LinearLayout>
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".VideodetectionAndAccelerometerActivity">
<TextView
android:id="@+id/tvVideodetectionAndAccelerometerStatusmessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Videodetection + Accelerometer Activity">
</TextView>
<Button
android:id="@+id/videodetectionAndAccelerometerBackToMainActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back to MainActivity">
</Button>
</LinearLayout>