Skip to content

Commit

Permalink
Commented
Browse files Browse the repository at this point in the history
  • Loading branch information
ppanero committed Mar 23, 2015
1 parent f57f5b6 commit 8079875
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
3 changes: 0 additions & 3 deletions app/src/main/java/com/aware/plugin/io_panero/ContextCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public class ContextCard implements IContextCard {
private Context sContext;

//Set how often your card needs to refresh if the stream is visible (in milliseconds)
//private int alarm_frequency = Integer.parseInt(Aware.getSetting(sContext.getApplicationContext(), Settings.FREQUENCY_IO_METER));
//private int time_offset = 30 * 1000;
//private int refresh_interval = (alarm_frequency * 60 * 1000) + time_offset; //1 second = 1000 milliseconds
private int refresh_interval = 3 * 60 * 1000; //1 second = 1000 milliseconds

//Declare here all the UI elements you'll be accessing
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/com/aware/plugin/io_panero/IOAlarm.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@ public class IOAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
//Plugin.alarmSet = false;
Log.d("Alarm", "Alarm received, sensors on");
//Turn on the sensors
Aware.setSetting(context, Aware_Preferences.STATUS_ACCELEROMETER, true);
Aware.setSetting(context, Aware_Preferences.STATUS_MAGNETOMETER, true);
Aware.setSetting(context, Aware_Preferences.STATUS_LIGHT, true);
Aware.setSetting(context, Aware_Preferences.STATUS_LOCATION_GPS, true);
Aware.setSetting(context, Aware_Preferences.STATUS_BATTERY, true);

Intent apply = new Intent(Aware.ACTION_AWARE_REFRESH);
context.sendBroadcast(apply);
//unlock sensors
Plugin.lockOffAccelerometer();
Plugin.lockOffMagnetometer();
Plugin.lockOffLocation();
Plugin.lockOffLight();
Plugin.lockOffBattery();
//Apply settings
Intent apply = new Intent(Aware.ACTION_AWARE_REFRESH);
context.sendBroadcast(apply);


}

public void SetAlarm(Context context, int interval) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
//Plugin.alarmSet = true;
Intent i = new Intent(context, IOAlarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
//interval must come in minutes
Expand Down
34 changes: 30 additions & 4 deletions app/src/main/java/com/aware/plugin/io_panero/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,14 @@ private static void updateStatus(Context context) {
if (DEBUG) Log.d("Status updated", "The device is: " + io_status.toString() + " weight: " + overallWeight);
}

//Locking off the sensors

protected static void lockOffMagnetometer() {
lockMagnetometer = false;
}

protected static void lockOffAccelerometer() {
lockMagnetometer = false;
lockAccelerometer = false;
}

protected static void lockOffLight() {
Expand All @@ -292,6 +294,10 @@ protected static void lockOffBattery() {
lockBattery = false;
}

/*
Transforms to string in the form of: X h X min X sec
The input is the amount of time in seconds (Unix Time)
*/
public static String secondsToTime(long seconds){
long aux_seconds = seconds;
if(aux_seconds < 0)
Expand All @@ -317,30 +323,49 @@ public static String secondsToTime(long seconds){
return(sb.toString());
}

/*
Calculates the hour offset due to the time zone of the device.
*/
public static long timezoneOffset(){
return TimeUnit.MILLISECONDS.toSeconds(java.util.TimeZone.getDefault().getOffset(Calendar.ZONE_OFFSET));
}

/*
Calculates the corresponding weight of the light sensor according to the
received hour of the day.
*/
public static double getHourWeight(int hour){
return ((-0.5 / 11) * (hour % 12)) + 1;
}

/*
Sensors receiver, the way the y work is the following
Upon receiving a broadcast they get form the settings the amount of samples they have to process
and the interval time (frequency) at which the plugin is being run.
They check that the frequency is higher than zero and that they are not locked.
After they check if they need to process more samples, if so they do. If not
they turn of the sensor and lock. After that they check which location and weight is inferred
from the value they measured.
In the end they restart their variables (avg, value, and counter) and update the status
*/
public static class MagnetReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
//get frequency and aount of samples
int interval_min = Integer.parseInt(Aware.getSetting(context, Settings.FREQUENCY_IO_METER));
int samples = Integer.parseInt(Aware.getSetting(context, Settings.SAMPLES_MAGNETOMETER));

//Check interval and lock status
if (interval_min > 0 && !lockMagnetometer) {
if (magnet_counter < samples) {
if (magnet_counter < samples) { //Process samples
ContentValues values = (ContentValues) intent.getExtras().get(Magnetometer.EXTRA_DATA);
double value_x = Double.parseDouble(values.get(Magnetometer_Provider.Magnetometer_Data.VALUES_0).toString());
double value_y = Double.parseDouble(values.get(Magnetometer_Provider.Magnetometer_Data.VALUES_1).toString());
double value_z = Double.parseDouble(values.get(Magnetometer_Provider.Magnetometer_Data.VALUES_2).toString());
magnet_val += Math.sqrt(Math.pow(value_x, 2) + Math.pow(value_y, 2) + Math.pow(value_z, 2));
magnet_counter++;
} else {
} else {//Infer location type
//Turn off the sensor for computation
Aware.setSetting(context, Aware_Preferences.STATUS_MAGNETOMETER, false);
Intent apply = new Intent(Aware.ACTION_AWARE_REFRESH);
Expand Down Expand Up @@ -368,6 +393,7 @@ public void onReceive(Context context, Intent intent) {
decisionMatrix[1][0] = 0.5;
}
decisionMatrix[2][0] = avg_magnet;
//Restart variables
avg_magnet = 0;
magnet_val = 0;
magnet_counter = 0;
Expand Down

0 comments on commit 8079875

Please sign in to comment.