-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reformat the source code to use only spaces
git-svn-id: https://android-motion-detection.googlecode.com/svn/trunk@28 996b93ed-3e4d-4b21-1044-74bd19560d1d
- Loading branch information
phishman3579
committed
Nov 19, 2012
1 parent
ed2ee81
commit f38e47d
Showing
11 changed files
with
1,120 additions
and
1,048 deletions.
There are no files selected for viewing
483 changes: 245 additions & 238 deletions
483
src/com/jwetherell/motion_detection/MotionDetectionActivity.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
|
||
import com.jwetherell.motion_detection.data.GlobalData; | ||
|
||
|
||
import android.app.Activity; | ||
import android.hardware.Sensor; | ||
import android.hardware.SensorEvent; | ||
|
@@ -16,33 +15,36 @@ | |
|
||
|
||
/** | ||
* This class extends Activity and processes sensor data and location data. It is used to | ||
* detect when the phone is in motion, so we do not try to detect motion. | ||
* This class extends Activity and processes sensor data and location data. It | ||
* is used to detect when the phone is in motion, so we do not try to detect | ||
* motion. | ||
* | ||
* @author Justin Wetherell <[email protected]> | ||
*/ | ||
public class SensorsActivity extends Activity implements SensorEventListener { | ||
|
||
private static final String TAG = "SensorsActivity"; | ||
private static final AtomicBoolean computing = new AtomicBoolean(false); | ||
private static final AtomicBoolean computing = new AtomicBoolean(false); | ||
|
||
private static final float grav[] = new float[3]; //Gravity (a.k.a accelerometer data) | ||
private static final float mag[] = new float[3]; //Magnetic | ||
private static final float grav[] = new float[3]; // Gravity (a.k.a | ||
// accelerometer data) | ||
private static final float mag[] = new float[3]; // Magnetic | ||
|
||
private static final float gravThreshold = 0.5f; | ||
private static final float magThreshold = 1.0f; | ||
|
||
private static SensorManager sensorMgr = null; | ||
private static List<Sensor> sensors = null; | ||
private static Sensor sensorGrav = null; | ||
private static Sensor sensorMag = null; | ||
|
||
private static float prevGrav = 0.0f; | ||
private static float prevMag = 0.0f; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
} | ||
|
@@ -55,13 +57,13 @@ public void onDestroy() { | |
super.onDestroy(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void onStart() { | ||
super.onStart(); | ||
|
||
try { | ||
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE); | ||
|
||
|
@@ -81,42 +83,42 @@ public void onStart() { | |
sensorMgr = null; | ||
} | ||
} catch (Exception ex2) { | ||
ex2.printStackTrace(); | ||
ex2.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void onStop() { | ||
super.onStop(); | ||
|
||
try { | ||
try { | ||
sensorMgr.unregisterListener(this, sensorGrav); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
ex.printStackTrace(); | ||
} | ||
try { | ||
sensorMgr.unregisterListener(this, sensorMag); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
ex.printStackTrace(); | ||
} | ||
sensorMgr = null; | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void onSensorChanged(SensorEvent evt) { | ||
if (!computing.compareAndSet(false, true)) return; | ||
if (!computing.compareAndSet(false, true)) return; | ||
|
||
if (evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { | ||
grav[0] = evt.values[0]; | ||
grav[1] = evt.values[1]; | ||
|
@@ -127,35 +129,33 @@ public void onSensorChanged(SensorEvent evt) { | |
mag[2] = evt.values[2]; | ||
} | ||
|
||
float gravity = grav[0]+grav[1]+grav[2]; | ||
float magnetic = mag[0]+mag[1]+mag[2]; | ||
float gravDiff = Math.abs(gravity-prevGrav); | ||
float magDiff = Math.abs(magnetic-prevMag); | ||
//Log.i(TAG, "gravDiff="+gravDiff+" magDiff="+magDiff); | ||
float gravity = grav[0] + grav[1] + grav[2]; | ||
float magnetic = mag[0] + mag[1] + mag[2]; | ||
|
||
float gravDiff = Math.abs(gravity - prevGrav); | ||
float magDiff = Math.abs(magnetic - prevMag); | ||
// Log.i(TAG, "gravDiff="+gravDiff+" magDiff="+magDiff); | ||
|
||
if ( (Float.compare(prevGrav,0.0f)!=0 && Float.compare(prevMag,0.0f)!=0) && | ||
(gravDiff>gravThreshold || magDiff>magThreshold) ) | ||
{ | ||
if ((Float.compare(prevGrav, 0.0f) != 0 && Float.compare(prevMag, 0.0f) != 0) && (gravDiff > gravThreshold || magDiff > magThreshold)) { | ||
GlobalData.setPhoneInMotion(true); | ||
} else { | ||
GlobalData.setPhoneInMotion(false); | ||
} | ||
|
||
prevGrav = gravity; | ||
prevMag = magnetic; | ||
|
||
computing.set(false); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void onAccuracyChanged(Sensor sensor, int accuracy) { | ||
if (sensor==null) throw new NullPointerException(); | ||
if(sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD && accuracy==SensorManager.SENSOR_STATUS_UNRELIABLE) { | ||
if (sensor == null) throw new NullPointerException(); | ||
|
||
if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD && accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) { | ||
Log.e(TAG, "Compass data unreliable"); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,23 @@ | |
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
|
||
/** | ||
* This class is used to store global data. | ||
* | ||
* @author Justin Wetherell <[email protected]> | ||
*/ | ||
public abstract class GlobalData { | ||
private GlobalData() { }; | ||
|
||
|
||
private GlobalData() { | ||
}; | ||
|
||
private static final AtomicBoolean phoneInMotion = new AtomicBoolean(false); | ||
|
||
public static boolean isPhoneInMotion() { | ||
return phoneInMotion.get(); | ||
} | ||
|
||
public static void setPhoneInMotion(boolean bool) { | ||
phoneInMotion.set(bool); | ||
} | ||
|
36 changes: 19 additions & 17 deletions
36
src/com/jwetherell/motion_detection/data/Preferences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
package com.jwetherell.motion_detection.data; | ||
|
||
|
||
/** | ||
* This class is used to store preferences on how to decode images and what to save. | ||
* This class is used to store preferences on how to decode images and what to | ||
* save. | ||
* | ||
* @author Justin Wetherell <[email protected]> | ||
*/ | ||
public abstract class Preferences { | ||
private Preferences() { } | ||
|
||
//Which motion detection to use | ||
public static boolean USE_RGB = true; | ||
public static boolean USE_LUMA = false; | ||
public static boolean USE_STATE = false; | ||
|
||
//Which photos to save | ||
public static boolean SAVE_PREVIOUS = false; | ||
public static boolean SAVE_ORIGINAL = true; | ||
public static boolean SAVE_CHANGES = true; | ||
|
||
//Time between saving photos | ||
public static int PICTURE_DELAY = 10000; | ||
public abstract class Preferences { | ||
|
||
private Preferences() { | ||
} | ||
|
||
// Which motion detection to use | ||
public static boolean USE_RGB = true; | ||
public static boolean USE_LUMA = false; | ||
public static boolean USE_STATE = false; | ||
|
||
// Which photos to save | ||
public static boolean SAVE_PREVIOUS = false; | ||
public static boolean SAVE_ORIGINAL = true; | ||
public static boolean SAVE_CHANGES = true; | ||
|
||
// Time between saving photos | ||
public static int PICTURE_DELAY = 10000; | ||
} |
Oops, something went wrong.