Skip to content

Commit

Permalink
handler added
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitrisraptis96 committed Feb 19, 2018
1 parent 2eec02c commit e71888b
Show file tree
Hide file tree
Showing 84 changed files with 657 additions and 491 deletions.
19 changes: 0 additions & 19 deletions FallDetector.iml

This file was deleted.

9 changes: 9 additions & 0 deletions FallDetector/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
22 changes: 22 additions & 0 deletions FallDetector/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions FallDetector/.idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions FallDetector/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions FallDetector/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions FallDetector/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions FallDetector/.idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dimitris.falldetector">

<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -17,7 +18,12 @@
</intent-filter>
</activity>
<activity android:name=".SetActivity" />
<activity android:name=".StartActivity"></activity>
<activity android:name=".StartActivity" />

<service
android:name=".AccelerometerService"
android:enabled="true"
android:exported="true"></service>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.example.dimitris.falldetector;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;


public class Accelerometer implements SensorEventListener {

public static final String TAG = "Accelerometer";

private Sensor mSensor;
private SensorManager mSensorManager;

private final Handler mHandler;

private long lastUpdate = -1;

private float accel_values[];
private float last_accel_values[];

private int fallThreshold = 10;

private float mAccelCurrent = SensorManager.GRAVITY_EARTH;
private float mAccelLast = SensorManager.GRAVITY_EARTH;
private float mAccel = 0.00f;

private final static int CHECK_INTERVAL = 100; // [msec]

public static final String Code = "codeKey";
public static final String Phone = "phoneKey";


public Accelerometer(SensorManager sm, Sensor s, Handler h){
mSensorManager = sm;
mSensor = s;
mHandler = h;
}

public void startListening(){
if (mSensor == null) {
Log.w(TAG, "Warning: no accelerometer");

// Send a failure message back to the Activity
Message msg = mHandler.obtainMessage(Constants.MESSAGE_TOAST);
Bundle bundle = new Bundle();
bundle.putString(Constants.TOAST, "Unable to find accelerometer");
msg.setData(bundle);
mHandler.sendMessage(msg);

} else {
Log.w(TAG, "Warning: yes accelerometer");

mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}

public void stopListening(){
mSensorManager.unregisterListener(this);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { /*Safe not to implement*/ }

@Override
public void onSensorChanged(SensorEvent event) {

long curTime = System.currentTimeMillis();

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// sampling frequency f= 10Hz.
if ((curTime - lastUpdate) > CHECK_INTERVAL) {

long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;

accel_values = event.values.clone();

if (last_accel_values != null) {

mAccelLast = mAccelCurrent;
mAccelCurrent =(float)Math.sqrt(accel_values[0]* accel_values[0] + accel_values[1]*accel_values[1]
+ accel_values[2]*accel_values[2]);

float delta = mAccelCurrent - mAccelLast;
mAccel = mAccel * 0.9f + delta;

// Send the value back to the Activity
Message msg = mHandler.obtainMessage(Constants.MESSAGE_CHANGED);
Bundle bundle = new Bundle();
bundle.putFloat(Constants.VALUE, mAccelCurrent);
msg.setData(bundle);
mHandler.sendMessage(msg);

if (mAccel > fallThreshold) {

Log.w(TAG, "acceleration greater than threshold");
// Send the value back to the Activity
msg = mHandler.obtainMessage(Constants.MESSAGE_EMERGENCY);
mHandler.sendMessage(msg);
}
}
last_accel_values = accel_values.clone();
}
}
}

}
Loading

0 comments on commit e71888b

Please sign in to comment.