Skip to content
This repository has been archived by the owner on Jun 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #205 from denzilferreira/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
denzilferreira authored Jul 10, 2017
2 parents 06d34e7 + a355861 commit b39173a
Show file tree
Hide file tree
Showing 72 changed files with 1,993 additions and 1,111 deletions.
10 changes: 4 additions & 6 deletions aware-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ artifacts {
}

dependencies {
compile "org.eclipse.paho:org.eclipse.paho.client.mqttv3:$mqtt_libs"
compile "com.koushikdutta.ion:ion:$ion_libs"
compile "com.android.support:cardview-v7:$support_libs"
compile "com.android.support:gridlayout-v7:$support_libs"
compile "com.android.support:appcompat-v7:$support_libs"
compile "com.android.support:design:$support_libs"
implementation "org.eclipse.paho:org.eclipse.paho.client.mqttv3:$mqtt_libs"
implementation "com.koushikdutta.ion:ion:$ion_libs"
implementation "com.android.support:gridlayout-v7:$support_libs"
implementation "com.android.support:appcompat-v7:$support_libs"
}

38 changes: 27 additions & 11 deletions aware-core/src/main/java/com/aware/Accelerometer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ public class Accelerometer extends Aware_Sensor implements SensorEventListener {
private static String LABEL = "";

private static Float[] LAST_VALUES = null;
private static long LAST_TS = 0;
private static long LAST_SAVE = 0;

private static int FREQUENCY = -1;
private static double THRESHOLD = 0;
private static boolean ENFORCE_FREQUENCY = false;

/**
* Broadcasted event: new accelerometer values
Expand Down Expand Up @@ -104,6 +107,9 @@ public void onSensorChanged(SensorEvent event) {
return;
}

long TS = System.currentTimeMillis();
if (ENFORCE_FREQUENCY && TS < LAST_TS + FREQUENCY/1000)
return;
if (LAST_VALUES != null && THRESHOLD > 0 && Math.abs(event.values[0] - LAST_VALUES[0]) < THRESHOLD
&& Math.abs(event.values[1] - LAST_VALUES[1]) < THRESHOLD
&& Math.abs(event.values[2] - LAST_VALUES[2]) < THRESHOLD) {
Expand All @@ -114,22 +120,23 @@ public void onSensorChanged(SensorEvent event) {

ContentValues rowData = new ContentValues();
rowData.put(Accelerometer_Data.DEVICE_ID, Aware.getSetting(getApplicationContext(), Aware_Preferences.DEVICE_ID));
rowData.put(Accelerometer_Data.TIMESTAMP, System.currentTimeMillis());
rowData.put(Accelerometer_Data.TIMESTAMP, TS);
rowData.put(Accelerometer_Data.VALUES_0, event.values[0]);
rowData.put(Accelerometer_Data.VALUES_1, event.values[1]);
rowData.put(Accelerometer_Data.VALUES_2, event.values[2]);
rowData.put(Accelerometer_Data.ACCURACY, event.accuracy);
rowData.put(Accelerometer_Data.LABEL, LABEL);

if (data_values.size() < 250) {
data_values.add(rowData);
data_values.add(rowData);
LAST_TS = TS;

Intent accelData = new Intent(ACTION_AWARE_ACCELEROMETER);
accelData.putExtra(EXTRA_DATA, rowData);
sendBroadcast(accelData);
Intent accelData = new Intent(ACTION_AWARE_ACCELEROMETER);
accelData.putExtra(EXTRA_DATA, rowData);
sendBroadcast(accelData);

if (Aware.DEBUG) Log.d(TAG, "Accelerometer: " + rowData.toString());
if (Aware.DEBUG) Log.d(TAG, "Accelerometer: " + rowData.toString());

if (data_values.size() < 250 && TS < LAST_SAVE + 300000) {
return;
}

Expand All @@ -146,6 +153,7 @@ public void onSensorChanged(SensorEvent event) {
if (Aware.DEBUG) Log.d(TAG, e.getMessage());
}
data_values.clear();
LAST_SAVE = TS;
}

/**
Expand Down Expand Up @@ -267,17 +275,25 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Aware.setSetting(this, Aware_Preferences.THRESHOLD_ACCELEROMETER, 0.0);
}

if (FREQUENCY != Integer.parseInt(Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_ACCELEROMETER))
|| THRESHOLD != Double.parseDouble(Aware.getSetting(getApplicationContext(), Aware_Preferences.THRESHOLD_ACCELEROMETER))) {
int new_frequency = Integer.parseInt(Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_ACCELEROMETER));
double new_threshold = Double.parseDouble(Aware.getSetting(getApplicationContext(), Aware_Preferences.THRESHOLD_ACCELEROMETER));
boolean new_enforce_frequency = (Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_ACCELEROMETER_ENFORCE).equals("true")
|| Aware.getSetting(getApplicationContext(), Aware_Preferences.ENFORCE_FREQUENCY_ALL).equals("true"));

if (FREQUENCY != new_frequency
|| THRESHOLD != new_threshold
|| ENFORCE_FREQUENCY != new_enforce_frequency){

sensorHandler.removeCallbacksAndMessages(null);
mSensorManager.unregisterListener(this, mAccelerometer);

FREQUENCY = Integer.parseInt(Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_ACCELEROMETER));
THRESHOLD = Double.parseDouble(Aware.getSetting(getApplicationContext(), Aware_Preferences.THRESHOLD_ACCELEROMETER));
FREQUENCY = new_frequency;
THRESHOLD = new_threshold;
ENFORCE_FREQUENCY = new_enforce_frequency;
}

mSensorManager.registerListener(this, mAccelerometer, Integer.parseInt(Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_ACCELEROMETER)), sensorHandler);
LAST_SAVE = System.currentTimeMillis();

if (Aware.DEBUG) Log.d(TAG, "Accelerometer service active: " + FREQUENCY + "ms");
}
Expand Down
41 changes: 21 additions & 20 deletions aware-core/src/main/java/com/aware/Applications.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
import android.app.ActivityManager;
import android.app.ActivityManager.ProcessErrorStateInfo;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
Expand All @@ -28,12 +25,10 @@
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.IntDef;
import android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.support.v4.view.accessibility.AccessibilityEventCompat;
import android.support.v4.view.accessibility.AccessibilityManagerCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
Expand Down Expand Up @@ -126,6 +121,11 @@ private String getApplicationName(String package_name) {
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getPackageName() == null) return;

if (!Aware.isServiceRunning(getApplicationContext(), Aware.class)) {
Intent aware = new Intent(this, Aware.class);
startService(aware);
}

if (Aware.getSetting(getApplicationContext(), Aware_Preferences.STATUS_NOTIFICATIONS).equals("true") && event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
Notification notificationDetails = (Notification) event.getParcelableData();
if (notificationDetails != null) {
Expand Down Expand Up @@ -288,10 +288,14 @@ protected void onServiceConnected() {
//This makes sure that plugins and apps can check if the accessibility service is active
Aware.setSetting(this, Applications.STATUS_AWARE_ACCESSIBILITY, true);

IntentFilter filter = new IntentFilter();
filter.addAction(Aware.ACTION_AWARE_SYNC_DATA);
filter.addAction(Aware.ACTION_AWARE_CLEAR_DATA);
registerReceiver(awareMonitor, filter);
IntentFilter webservices = new IntentFilter();
webservices.addAction(Aware.ACTION_AWARE_SYNC_DATA);
webservices.addAction(Aware.ACTION_AWARE_CLEAR_DATA);
registerReceiver(awareMonitor, webservices);

if (Aware.getSetting(getApplicationContext(), Aware_Preferences.FOREGROUND_PRIORITY).equals("true")) {
sendBroadcast(new Intent(Aware.ACTION_AWARE_PRIORITY_FOREGROUND));
}

if (Aware.getSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_APPLICATIONS).length() == 0) {
Aware.setSetting(getApplicationContext(), Aware_Preferences.FREQUENCY_APPLICATIONS, 1);
Expand Down Expand Up @@ -343,9 +347,7 @@ protected void onServiceConnected() {
public void onInterrupt() {
if (Aware.getSetting(getApplicationContext(), Applications.STATUS_AWARE_ACCESSIBILITY).equals("true")) {
try {
if (awareMonitor != null) {
unregisterReceiver(awareMonitor);
}
if (awareMonitor != null) unregisterReceiver(awareMonitor);
} catch (IllegalArgumentException e) {
}
}
Expand All @@ -360,9 +362,7 @@ public void onInterrupt() {
public boolean onUnbind(Intent intent) {
if (Aware.getSetting(getApplicationContext(), Applications.STATUS_AWARE_ACCESSIBILITY).equals("true")) {
try {
if (awareMonitor != null) {
unregisterReceiver(awareMonitor);
}
if (awareMonitor != null) unregisterReceiver(awareMonitor);
} catch (IllegalArgumentException e) {
}
}
Expand Down Expand Up @@ -454,7 +454,7 @@ public synchronized static boolean isAccessibilityServiceActive(Context c) {
if (!isAccessibilityEnabled(c)) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(c);
mBuilder.setSmallIcon(R.drawable.ic_stat_aware_accessibility);
mBuilder.setContentTitle("Please enable AWARE");
mBuilder.setContentTitle(c.getResources().getString(R.string.aware_activate_accessibility_title));
mBuilder.setContentText(c.getResources().getString(R.string.aware_activate_accessibility));
mBuilder.setAutoCancel(true);
mBuilder.setOnlyAlertOnce(true); //notify the user only once
Expand All @@ -480,7 +480,8 @@ public synchronized static boolean isAccessibilityServiceActive(Context c) {
* @author df
*/
private static final Applications_Broadcaster awareMonitor = new Applications_Broadcaster();
public static class Applications_Broadcaster extends BroadcastReceiver {

public static class Applications_Broadcaster extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

Expand All @@ -495,7 +496,7 @@ public void onReceive(Context context, Intent intent) {
webserviceHelper.putExtra(WebserviceHelper.EXTRA_TABLE, DATABASE_TABLES[i]);
webserviceHelper.putExtra(WebserviceHelper.EXTRA_FIELDS, TABLES_FIELDS[i]);
webserviceHelper.putExtra(WebserviceHelper.EXTRA_CONTENT_URI, CONTEXT_URIS[i].toString());
context.startService(webserviceHelper);
startWakefulService(context, webserviceHelper);
}
}

Expand All @@ -510,7 +511,7 @@ public void onReceive(Context context, Intent intent) {
Intent webserviceHelper = new Intent(context, WebserviceHelper.class);
webserviceHelper.setAction(WebserviceHelper.ACTION_AWARE_WEBSERVICE_CLEAR_TABLE);
webserviceHelper.putExtra(WebserviceHelper.EXTRA_TABLE, DATABASE_TABLES[i]);
context.startService(webserviceHelper);
startWakefulService(context, webserviceHelper);
}
}
}
Expand Down
Loading

0 comments on commit b39173a

Please sign in to comment.