Skip to content

Commit

Permalink
Implemented cached data flushing functionality across all generators.
Browse files Browse the repository at this point in the history
* Fixed performance issue with ForegroundApplication generator in data display.
  • Loading branch information
audaciouscode committed Jul 14, 2017
1 parent 057875f commit cb865b3
Show file tree
Hide file tree
Showing 16 changed files with 458 additions and 10 deletions.
Expand Up @@ -8,6 +8,7 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

Expand Down Expand Up @@ -69,6 +70,8 @@ protected void onPause() {

@Override
public void onGeneratorUpdated(final String identifier, long timestamp, Bundle data) {
Log.e("PDK", "GENERATOR UPDATED: " + identifier);

final DataStreamActivity me = this;

if (me.mIsUpdating) {
Expand Down
Expand Up @@ -22,7 +22,7 @@
public abstract class Generator
{
public static final String PDK_METADATA = "passive-data-metadata";
public static final java.lang.String IDENTIFIER = "generator-id";
public static final String IDENTIFIER = "generator-id";
public static final String TIMESTAMP = "timestamp";
public static final String GENERATOR = "generator";
public static final String SOURCE = "source";
Expand Down Expand Up @@ -184,4 +184,7 @@ protected void setDatabaseVersion(SQLiteDatabase db, int newVersion) {
db.insert(Generator.TABLE_METADATA, null, values);
}
}

protected abstract void flushCachedData();
public abstract void setCachedDataRetentionPeriod(long period);
}
Expand Up @@ -14,6 +14,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.provider.CallLog;
import android.support.v4.content.ContextCompat;
import android.util.Log;
Expand Down Expand Up @@ -53,6 +54,9 @@ public class PhoneCalls extends Generator {
private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.communication.PhoneCalls.ENABLED";
private static final boolean ENABLED_DEFAULT = true;

private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.communication.PhoneCalls.DATA_RETENTION_PERIOD";
private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000);

private static final String CALL_DATE_KEY = "call_timestamp";
private static final String CALL_DURATION_KEY = "duration";
private static final String CALL_IS_NEW_KEY = "is_new";
Expand Down Expand Up @@ -355,6 +359,8 @@ public void run() {
me.mHandler.post(checkLogs);

Generators.getInstance(this.mContext).registerCustomViewClass(PhoneCalls.GENERATOR_IDENTIFIER, PhoneCalls.class);

this.flushCachedData();
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -535,6 +541,30 @@ public List<Bundle> fetchPayloads() {
return new ArrayList<>();
}

@Override
protected void flushCachedData() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);

long retentionPeriod = prefs.getLong(PhoneCalls.DATA_RETENTION_PERIOD, PhoneCalls.DATA_RETENTION_PERIOD_DEFAULT);

long start = System.currentTimeMillis() - retentionPeriod;

String where = PhoneCalls.HISTORY_OBSERVED + " < ?";
String[] args = { "" + start };

this.mDatabase.delete(PhoneCalls.TABLE_HISTORY, where, args);
}

@Override
public void setCachedDataRetentionPeriod(long period) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);
SharedPreferences.Editor e = prefs.edit();

e.putLong(PhoneCalls.DATA_RETENTION_PERIOD, period);

e.apply();
}

@SuppressWarnings("unused")
public static View fetchView(ViewGroup parent)
{
Expand Down
Expand Up @@ -14,6 +14,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -54,6 +55,9 @@ public class TextMessages extends Generator {
private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.communication.TextMessages.ENABLED";
private static final boolean ENABLED_DEFAULT = true;

private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.communication.TextMessages.DATA_RETENTION_PERIOD";
private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000);

private static final Uri SMS_INBOX_URI = Uri.parse("content://sms/inbox");
private static final Uri SMS_SENT_URI = Uri.parse("content://sms/sent");

Expand Down Expand Up @@ -301,6 +305,8 @@ public void run() {
me.mHandler.post(checkLogs);

Generators.getInstance(this.mContext).registerCustomViewClass(TextMessages.GENERATOR_IDENTIFIER, TextMessages.class);

this.flushCachedData();
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -458,6 +464,30 @@ public String getFormattedValue(float value, Entry entry, int dataSetIndex, View
}
}

@Override
protected void flushCachedData() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);

long retentionPeriod = prefs.getLong(TextMessages.DATA_RETENTION_PERIOD, TextMessages.DATA_RETENTION_PERIOD_DEFAULT);

long start = System.currentTimeMillis() - retentionPeriod;

String where = TextMessages.HISTORY_OBSERVED + " < ?";
String[] args = { "" + start };

this.mDatabase.delete(TextMessages.TABLE_HISTORY, where, args);
}

@Override
public void setCachedDataRetentionPeriod(long period) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);
SharedPreferences.Editor e = prefs.edit();

e.putLong(TextMessages.DATA_RETENTION_PERIOD, period);

e.apply();
}

@Override
public List<Bundle> fetchPayloads() {
return new ArrayList<>();
Expand Down
Expand Up @@ -10,6 +10,7 @@
import android.database.sqlite.SQLiteDatabase;
import android.os.BatteryManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class Battery extends Generator {
private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.device.Battery.ENABLED";
private static final boolean ENABLED_DEFAULT = true;

private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.device.Battery.DATA_RETENTION_PERIOD";
private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000);

private static final String DATABASE_PATH = "pdk-device-battery.sqlite";
private static final int DATABASE_VERSION = 1;
Expand Down Expand Up @@ -102,6 +105,7 @@ public class Battery extends Generator {

private int mLastLevel = -1;
private int mLastStatus = BatteryManager.BATTERY_STATUS_UNKNOWN;
private int mLastScale = -1;

private int mLastHealth = BatteryManager.BATTERY_HEALTH_UNKNOWN;
private int mLastPlugged = -1;
Expand Down Expand Up @@ -278,8 +282,9 @@ public void onReceive(final Context context, Intent intent) {
values.put(Battery.HISTORY_LEVEL, level);
update.putInt(Battery.HISTORY_LEVEL, level);

values.put(Battery.HISTORY_SCALE, intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1));
update.putInt(Battery.HISTORY_SCALE, intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1));
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
values.put(Battery.HISTORY_SCALE, scale);
update.putInt(Battery.HISTORY_SCALE, scale);

values.put(Battery.HISTORY_TEMPERATURE, temperature);
update.putInt(Battery.HISTORY_TEMPERATURE, temperature);
Expand All @@ -306,6 +311,7 @@ public void onReceive(final Context context, Intent intent) {
}

me.mLastLevel = level;
me.mLastScale = scale;
me.mLastStatus = status;
me.mLastHealth = health;
me.mLastPlugged = plugged;
Expand Down Expand Up @@ -335,6 +341,8 @@ public void onReceive(final Context context, Intent intent) {

IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
this.mContext.registerReceiver(this.mReceiver, filter);

this.flushCachedData();
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -534,4 +542,36 @@ public void setMonitorsTemperature(boolean monitorTemperature) {
public void setMinUpdateInterval(long updateInterval) {
this.mMinUpdateInterval = updateInterval;
}

@Override
protected void flushCachedData() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);

long retentionPeriod = prefs.getLong(Battery.DATA_RETENTION_PERIOD, Battery.DATA_RETENTION_PERIOD_DEFAULT);

long start = System.currentTimeMillis() - retentionPeriod;

String where = Battery.HISTORY_OBSERVED + " < ?";
String[] args = { "" + start };

this.mDatabase.delete(Battery.TABLE_HISTORY, where, args);
}

@Override
public void setCachedDataRetentionPeriod(long period) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);
SharedPreferences.Editor e = prefs.edit();

e.putLong(Battery.DATA_RETENTION_PERIOD, period);

e.apply();
}

public int getLastLevel() {
return this.mLastLevel;
}

public int getLastScale() {
return this.mLastScale;
}
}
Expand Up @@ -12,8 +12,8 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -47,6 +47,9 @@ public class ForegroundApplication extends Generator{
private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.device.ForegroundApplication.ENABLED";
private static final boolean ENABLED_DEFAULT = true;

private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.device.ForegroundApplication.DATA_RETENTION_PERIOD";
private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000);

private static final int DATABASE_VERSION = 3;

private static final String TABLE_HISTORY = "history";
Expand Down Expand Up @@ -128,8 +131,6 @@ public void onForeground(String process) {
update.putLong(ForegroundApplication.HISTORY_DURATION, me.mSampleInterval);
update.putBoolean(ForegroundApplication.HISTORY_SCREEN_ACTIVE, screenActive);

Log.e("SLEEP-SIGHT", "TRANSMIT BUNDLE: " + update);

Generators.getInstance(me.mContext).notifyGeneratorUpdated(ForegroundApplication.GENERATOR_IDENTIFIER, update);
}
});
Expand Down Expand Up @@ -157,6 +158,8 @@ public void onForeground(String process) {
this.setDatabaseVersion(this.mDatabase, ForegroundApplication.DATABASE_VERSION);

Generators.getInstance(this.mContext).registerCustomViewClass(ForegroundApplication.GENERATOR_IDENTIFIER, ForegroundApplication.class);

this.flushCachedData();
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -212,15 +215,17 @@ public static void bindDisclosureViewHolder(final GeneratorViewHolder holder) {

@SuppressWarnings("unused")
public static void bindViewHolder(DataPointViewHolder holder) {
long start = System.currentTimeMillis();

final Context context = holder.itemView.getContext();

long lastTimestamp = 0;

ForegroundApplication generator = ForegroundApplication.getInstance(holder.itemView.getContext());

Cursor c = generator.mDatabase.query(ForegroundApplication.TABLE_HISTORY, null, null, null, null, null, ForegroundApplication.HISTORY_OBSERVED + " DESC");
Cursor c = generator.mDatabase.query(ForegroundApplication.TABLE_HISTORY, null, null, null, null, null, ForegroundApplication.HISTORY_OBSERVED + " DESC", "1");

while (c.moveToNext()) {
if (c.moveToNext()) {
if (lastTimestamp == 0) {
lastTimestamp = c.getLong(c.getColumnIndex(ForegroundApplication.HISTORY_OBSERVED));
}
Expand Down Expand Up @@ -407,13 +412,37 @@ public int compare(HashMap<String, Double> mapOne, HashMap<String, Double> mapTw

dateLabel.setText(R.string.label_never_pdk);
}
}
}

@Override
public List<Bundle> fetchPayloads() {
return new ArrayList<>();
}

@Override
protected void flushCachedData() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);

long retentionPeriod = prefs.getLong(ForegroundApplication.DATA_RETENTION_PERIOD, ForegroundApplication.DATA_RETENTION_PERIOD_DEFAULT);

long start = System.currentTimeMillis() - retentionPeriod;

String where = ForegroundApplication.HISTORY_OBSERVED + " < ?";
String[] args = { "" + start };

this.mDatabase.delete(ForegroundApplication.TABLE_HISTORY, where, args);
}

@Override
public void setCachedDataRetentionPeriod(long period) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);
SharedPreferences.Editor e = prefs.edit();

e.putLong(ForegroundApplication.DATA_RETENTION_PERIOD, period);

e.apply();
}

@SuppressWarnings("unused")
public static View fetchView(ViewGroup parent)
{
Expand Down
Expand Up @@ -81,6 +81,9 @@ public class Location extends Generator implements GoogleApiClient.ConnectionCal
private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.device.Location.ENABLED";
private static final boolean ENABLED_DEFAULT = true;

private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.device.Location.DATA_RETENTION_PERIOD";
private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000);

private static final String USE_GOOGLE_SERVICES = "com.audacious_software.passive_data_kit.generators.device.Location.USE_GOOGLE_SERVICES";
private static final boolean USE_GOOGLE_SERVICES_DEFAULT = true;

Expand Down Expand Up @@ -435,6 +438,8 @@ public void onLocationChanged(android.location.Location location) {
this.mDatabase.insert(Location.TABLE_HISTORY, null, values);

Generators.getInstance(this.mContext).notifyGeneratorUpdated(Location.GENERATOR_IDENTIFIER, updated);

this.flushCachedData();
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -1078,4 +1083,28 @@ public void setPreservesRandomVector(boolean preservesVector) {

e.apply();
}

@Override
protected void flushCachedData() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);

long retentionPeriod = prefs.getLong(Location.DATA_RETENTION_PERIOD, Location.DATA_RETENTION_PERIOD_DEFAULT);

long start = System.currentTimeMillis() - retentionPeriod;

String where = Location.HISTORY_OBSERVED + " < ?";
String[] args = { "" + start };

this.mDatabase.delete(Location.TABLE_HISTORY, where, args);
}

@Override
public void setCachedDataRetentionPeriod(long period) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);
SharedPreferences.Editor e = prefs.edit();

e.putLong(Location.DATA_RETENTION_PERIOD, period);

e.apply();
}
}

0 comments on commit cb865b3

Please sign in to comment.