Skip to content

Commit

Permalink
Implemented ambient light sensor.
Browse files Browse the repository at this point in the history
* Implemented accelerometer sensor.
* Added lock functionality to Data Stream view.
* Updated Foreground App generator to only count when screen is active.
  • Loading branch information
audaciouscode committed May 11, 2017
1 parent f7c0b2c commit fa714a9
Show file tree
Hide file tree
Showing 33 changed files with 1,244 additions and 109 deletions.
Binary file added res/drawable-hdpi/ic_ambient_light.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-hdpi/ic_pdk_accelerometer.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-hdpi/ic_pdk_action_lock.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-hdpi/ic_pdk_action_unlock.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_ambient_light.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_pdk_accelerometer.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_pdk_action_lock.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_pdk_action_unlock.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_ambient_light.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_pdk_accelerometer.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_pdk_action_lock.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_pdk_action_unlock.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/ic_ambient_light.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/ic_pdk_accelerometer.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/ic_pdk_action_lock.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/ic_pdk_action_unlock.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxxhdpi/ic_ambient_light.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxxhdpi/ic_pdk_accelerometer.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxxhdpi/ic_pdk_action_lock.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxxhdpi/ic_pdk_action_unlock.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions res/layout/card_generator_sensors_accelerometer.xml
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
android:layout_margin="8dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/generator_accelerometer_header"
android:padding="8dp"
android:baselineAligned="false">
<ImageView android:src="@drawable/ic_pdk_accelerometer"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginRight="8dp" />
<TextView android:text="@string/generator_sensors_accelerometer"
android:gravity="center_vertical|start"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_weight="1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@android:color/white" />
<TextView android:id="@+id/generator_data_point_date"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:textSize="12sp"
android:textColor="@android:color/white"
android:layout_marginRight="8dp"/>
</LinearLayout>
<LinearLayout android:id="@+id/card_content"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/accelerometer_chart"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginBottom="0dp"/>
</LinearLayout>
<TextView android:id="@+id/card_empty"
android:text="@string/message_generator_accelerometer_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
63 changes: 63 additions & 0 deletions res/layout/card_generator_sensors_ambient_light.xml
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
android:layout_margin="8dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/generator_ambient_light_header"
android:padding="8dp"
android:baselineAligned="false">
<ImageView android:src="@drawable/ic_ambient_light"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginRight="8dp" />
<TextView android:text="@string/generator_sensors_ambient_light"
android:gravity="center_vertical|start"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_weight="1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="@android:color/black" />
<TextView android:id="@+id/generator_data_point_date"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="24dp"
android:textSize="12sp"
android:textColor="@android:color/black"
android:layout_marginRight="8dp"/>
</LinearLayout>
<LinearLayout android:id="@+id/card_content"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/light_chart"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginBottom="0dp"/>
</LinearLayout>
<TextView android:id="@+id/card_empty"
android:text="@string/message_generator_ambient_light_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
8 changes: 8 additions & 0 deletions res/menu/activity_data_stream.xml
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_pdk_toggle_sort_lock"
android:icon="@drawable/ic_pdk_action_unlock"
android:title="@string/action_pdk_toggle_sort_lock"
app:showAsAction="always" />
</menu>
8 changes: 7 additions & 1 deletion res/values/databases.xml
Expand Up @@ -27,8 +27,14 @@
<!-- Battery Generator -->
<string name="pdk_generator_device_battery_create_history_table">CREATE TABLE history(_id INTEGER PRIMARY KEY AUTOINCREMENT, fetched INTEGER, transmitted INTEGER, observed INTEGER, health TEXT, level INTERGER, plugged TEXT, present INTEGER, scale INTEGER, temperature INTEGER, voltage INTEGER, technology TEXT, status TEXT);</string>

<!-- Foreground Applications -->
<!-- Foreground Applications Generator -->
<string name="pdk_generator_foreground_applications_create_history_table">CREATE TABLE history(_id INTEGER PRIMARY KEY AUTOINCREMENT, fetched INTEGER, transmitted INTEGER, observed INTEGER, application TEXT);</string>
<string name="pdk_generator_foreground_applications_history_table_add_duration">ALTER TABLE history ADD duration REAL;</string>
<string name="pdk_generator_foreground_applications_history_table_add_screen_active">ALTER TABLE history ADD screen_active INTEGER;</string>

<!-- Ambient Light Sensor Generator -->
<string name="pdk_generator_ambient_light_create_history_table">CREATE TABLE history(_id INTEGER PRIMARY KEY AUTOINCREMENT, fetched INTEGER, transmitted INTEGER, observed INTEGER, light_level REAL, raw_timestamp BIGINT, accuracy INTEGER);</string>

<!-- Accelerometer Sensor Generator -->
<string name="pdk_generator_accelerometer_create_history_table">CREATE TABLE history(_id INTEGER PRIMARY KEY AUTOINCREMENT, fetched INTEGER, transmitted INTEGER, observed INTEGER, x REAL, y REAL, z REAL, raw_timestamp BIGINT, accuracy INTEGER);</string>
</resources>
21 changes: 20 additions & 1 deletion res/values/generators.xml
Expand Up @@ -158,4 +158,23 @@
<string name="generator_foreground_application_top_apps">Most Used (Last 24 Hours)</string>
<string name="generator_foreground_application_recent_apps">Recently Used</string>
<string name="generator_foreground_application_app_name_duration">%1$s: %2$.0fm</string>
</resources>

<!-- Ambient Light Sensor -->
<string name="generator_sensors_ambient_light">Ambient Light</string>
<string name="message_generator_ambient_light_empty">No ambient light levels have been recorded yet.</string>
<color name="generator_ambient_light_header">#FFD600</color>
<color name="generator_ambient_light_low">#80FFEB3B</color>
<color name="generator_ambient_light_high">#ffFFEB3B</color>

<!-- Accelerometer Sensor -->
<string name="generator_sensors_accelerometer">Accelerometer</string>
<string name="message_generator_accelerometer_empty">No accelerometer readings have been recorded yet.</string>
<color name="generator_accelerometer_header">#3F51B5</color>
<color name="generator_accelerometer_x_low">#80F44336</color>
<color name="generator_accelerometer_x_high">#ffF44336</color>
<color name="generator_accelerometer_y_low">#804CAF50</color>
<color name="generator_accelerometer_y_high">#ff4CAF50</color>
<color name="generator_accelerometer_z_low">#802196F3</color>
<color name="generator_accelerometer_z_high">#ff2196F3</color>
</resources>

2 changes: 2 additions & 0 deletions res/values/strings.xml
Expand Up @@ -6,6 +6,8 @@
<string name="activity_data_stream">Data Stream</string>
<string name="action_continue">Continue</string>

<string name="action_pdk_toggle_sort_lock">Toggle Sort Lock</string>

<plurals name="activity_data_stream_subtitle">
<item quantity="one">%d generator</item>
<item quantity="other">%d generators</item>
Expand Down
@@ -1,11 +1,16 @@
package com.audacious_software.passive_data_kit.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;

import com.audacious_software.passive_data_kit.activities.generators.DataPointsAdapter;
import com.audacious_software.passive_data_kit.generators.Generators;
Expand All @@ -15,6 +20,7 @@

public class DataStreamActivity extends AppCompatActivity implements Generators.GeneratorUpdatedListener {
private DataPointsAdapter mAdapter = null;
private Menu mMenu = null;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -70,4 +76,50 @@ public void run() {
}
});
}

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
this.getMenuInflater().inflate(R.menu.activity_data_stream, menu);

this.mMenu = menu;

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_pdk_toggle_sort_lock) {
this.toggleSortLock();

return true;
}

return true;
}

private void toggleSortLock() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

boolean locked = prefs.getBoolean(DataPointsAdapter.SORT_BY_UPDATED, DataPointsAdapter.SORT_BY_UPDATED_DEFAULT);

MenuItem lockedItem = this.mMenu.findItem(R.id.action_pdk_toggle_sort_lock);

if (locked) {
lockedItem.setIcon(R.drawable.ic_pdk_action_unlock);
} else {
lockedItem.setIcon(R.drawable.ic_pdk_action_lock);
}

SharedPreferences.Editor e = prefs.edit();
e.putBoolean(DataPointsAdapter.SORT_BY_UPDATED, (locked == false));
e.apply();

this.mAdapter.notifyDataSetChanged();
}
}
@@ -1,6 +1,8 @@
package com.audacious_software.passive_data_kit.activities.generators;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
Expand All @@ -18,6 +20,9 @@
import java.util.List;

public class DataPointsAdapter extends RecyclerView.Adapter<DataPointViewHolder> {
public static final String SORT_BY_UPDATED = "com.audacious_software.passive_data_kit.activities.generators.DataPointsAdapter";
public static final boolean SORT_BY_UPDATED_DEFAULT = true;

private Context mContext = null;

@Override
Expand Down Expand Up @@ -85,46 +90,57 @@ public int getItemCount() {
}

private void sortGenerators(final Context context, List<Class<? extends Generator>> generators) {
Collections.sort(generators, new Comparator<Class<? extends Generator>>() {
@Override
public int compare(Class<? extends Generator> one, Class<? extends Generator> two) {
long oneUpdated = 0;

try {
Method oneGenerated = one.getDeclaredMethod("latestPointGenerated", Context.class);

oneUpdated = (long) oneGenerated.invoke(null, context);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

if (prefs.getBoolean(DataPointsAdapter.SORT_BY_UPDATED, DataPointsAdapter.SORT_BY_UPDATED_DEFAULT)) {
Collections.sort(generators, new Comparator<Class<? extends Generator>>() {
@Override
public int compare(Class<? extends Generator> one, Class<? extends Generator> two) {
long oneUpdated = 0;

try {
Method oneGenerated = one.getDeclaredMethod("latestPointGenerated", Context.class);

oneUpdated = (long) oneGenerated.invoke(null, context);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

long twoUpdated = 0;

try {
Method twoGenerated = two.getDeclaredMethod("latestPointGenerated", Context.class);

twoUpdated = (long) twoGenerated.invoke(null, context);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

if (oneUpdated < twoUpdated) {
return 1;
} else if (oneUpdated > twoUpdated) {
return -1;
}

return 0;
}

long twoUpdated = 0;

try {
Method twoGenerated = two.getDeclaredMethod("latestPointGenerated", Context.class);

twoUpdated = (long) twoGenerated.invoke(null, context);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
});
} else {
Collections.sort(generators, new Comparator<Class<? extends Generator>>() {
@Override
public int compare(Class<? extends Generator> one, Class<? extends Generator> two) {
return one.getCanonicalName().compareTo(two.getCanonicalName());
}

if (oneUpdated < twoUpdated) {
return 1;
} else if (oneUpdated > twoUpdated) {
return -1;
}

return 0;
}
});
});
}
}

public int getItemViewType (int position) {
Expand Down

0 comments on commit fa714a9

Please sign in to comment.