Browse Source

Vibration added.

TODO: Write / Fetch Database Values & Calculate Time Difference
master
Patrick Halboth 3 years ago
parent
commit
b60c167cc8

+ 1
- 1
app/src/main/AndroidManifest.xml View File

@@ -10,7 +10,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/schockicon"

+ 85
- 1
app/src/main/java/de/edotzlaff/schockwelle/SensorMapsActivity.java View File

@@ -1,14 +1,20 @@
package de.edotzlaff.schockwelle;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
@@ -52,6 +58,9 @@ public class SensorMapsActivity extends FragmentActivity implements OnMapReadyCa

private DatabaseReference mDatenbank;

//Vibration
private Boolean mDeviceCanVibrate = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -63,9 +72,10 @@ public class SensorMapsActivity extends FragmentActivity implements OnMapReadyCa

getDataBaseValues(); // Hier werden die Daten von der DB abgefragt //TODO Edward: Geht wieder :D

getVibrationAbility();
//distance(currentLocation.getLatitude(), currentLocation.getLongitude(),breitengrad,laengengrad); //Übergabe zur Berechnung der Distanz zwischen Auslöser und aktuellem Standort

//TODO Patrick: die Funktion distance gibt Meter zurück, könntest Du das mit dem Zeitstempel und der Berechnung bis zur Vibration machen?
setVibrationTimer(10000,1000,255);
}

private void getLocationPermission() {
@@ -78,6 +88,80 @@ public class SensorMapsActivity extends FragmentActivity implements OnMapReadyCa
}
}


private void getVibrationAbility()
{
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Output yes if can vibrate, no otherwise
if (v.hasVibrator()) {
Log.v("Can Vibrate", "YES");
// Log.v("Can Control Amplitude", v.hasAmplitudeControl() ? "YES" : "NO");
mDeviceCanVibrate = true;
}
else
{

Log.v("Can Vibrate", "NO");
mDeviceCanVibrate = false;
}
}

private long getTimeStampDifference(Date vibrationTime)
{
long diff= 0;
//TODO Zeitdifferenz in Millisekunden zwischen aktuellen Uhrzeit und Vibratonszeitstempel berechnen
return diff;
}
private void setVibrationTimer(long msDelay, int duration, int amplitude)
{

new CountDownTimer(msDelay, 1000) {
public void onTick(long millisUntilFinished) {
((TextView) findViewById(R.id.txtSensor)).setText("Earthquake hits in " + millisUntilFinished / 1000 + " s");
}

public void onFinish() {

Toast.makeText(getApplicationContext(), "The Ground is shaking!", Toast.LENGTH_SHORT).show();
performVibration(duration, amplitude);
( (TextView) findViewById(R.id.txtSensor)).setText("No Earthquake upcoming");
}
}.start();
}

public void performVibration(int duration, int amplitude) {
if(!mDeviceCanVibrate)
return;

Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {
if(duration == 0)
{
v.cancel(); //stop vibration if still running
Toast.makeText(this, "Vibration has been stopped", Toast.LENGTH_SHORT).show();
return;
}

Toast.makeText(this, "Ampl: " + amplitude + ", Dur: " + duration, Toast.LENGTH_SHORT).show();

v.vibrate(VibrationEffect.createOneShot(duration,amplitude));


} else {

if(duration == 0)
{
v.cancel(); //stop vibration if still running

return;
}

v.vibrate(duration);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

Loading…
Cancel
Save