diff --git a/src/com/audacious_software/passive_data_kit/activities/DataStreamActivity.java b/src/com/audacious_software/passive_data_kit/activities/DataStreamActivity.java index ec746cb..cf46906 100755 --- a/src/com/audacious_software/passive_data_kit/activities/DataStreamActivity.java +++ b/src/com/audacious_software/passive_data_kit/activities/DataStreamActivity.java @@ -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; @@ -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) { diff --git a/src/com/audacious_software/passive_data_kit/generators/Generator.java b/src/com/audacious_software/passive_data_kit/generators/Generator.java index 24a926c..dffde32 100755 --- a/src/com/audacious_software/passive_data_kit/generators/Generator.java +++ b/src/com/audacious_software/passive_data_kit/generators/Generator.java @@ -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"; @@ -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); } diff --git a/src/com/audacious_software/passive_data_kit/generators/communication/PhoneCalls.java b/src/com/audacious_software/passive_data_kit/generators/communication/PhoneCalls.java index c6db0e9..79be732 100755 --- a/src/com/audacious_software/passive_data_kit/generators/communication/PhoneCalls.java +++ b/src/com/audacious_software/passive_data_kit/generators/communication/PhoneCalls.java @@ -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; @@ -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"; @@ -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") @@ -535,6 +541,30 @@ public List 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) { diff --git a/src/com/audacious_software/passive_data_kit/generators/communication/TextMessages.java b/src/com/audacious_software/passive_data_kit/generators/communication/TextMessages.java index dce5d84..f162b47 100755 --- a/src/com/audacious_software/passive_data_kit/generators/communication/TextMessages.java +++ b/src/com/audacious_software/passive_data_kit/generators/communication/TextMessages.java @@ -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; @@ -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"); @@ -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") @@ -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 fetchPayloads() { return new ArrayList<>(); diff --git a/src/com/audacious_software/passive_data_kit/generators/device/Battery.java b/src/com/audacious_software/passive_data_kit/generators/device/Battery.java index 0cb78e6..aee2a23 100755 --- a/src/com/audacious_software/passive_data_kit/generators/device/Battery.java +++ b/src/com/audacious_software/passive_data_kit/generators/device/Battery.java @@ -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; @@ -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; @@ -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; @@ -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); @@ -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; @@ -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") @@ -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; + } } diff --git a/src/com/audacious_software/passive_data_kit/generators/device/ForegroundApplication.java b/src/com/audacious_software/passive_data_kit/generators/device/ForegroundApplication.java index ac93f94..b7faa20 100755 --- a/src/com/audacious_software/passive_data_kit/generators/device/ForegroundApplication.java +++ b/src/com/audacious_software/passive_data_kit/generators/device/ForegroundApplication.java @@ -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; @@ -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"; @@ -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); } }); @@ -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") @@ -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)); } @@ -407,13 +412,37 @@ public int compare(HashMap mapOne, HashMap mapTw dateLabel.setText(R.string.label_never_pdk); } - } + } @Override public List 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) { diff --git a/src/com/audacious_software/passive_data_kit/generators/device/Location.java b/src/com/audacious_software/passive_data_kit/generators/device/Location.java index 174bf7a..05851f9 100755 --- a/src/com/audacious_software/passive_data_kit/generators/device/Location.java +++ b/src/com/audacious_software/passive_data_kit/generators/device/Location.java @@ -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; @@ -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") @@ -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(); + } } diff --git a/src/com/audacious_software/passive_data_kit/generators/device/ScreenState.java b/src/com/audacious_software/passive_data_kit/generators/device/ScreenState.java index 6935449..d7faf1d 100755 --- a/src/com/audacious_software/passive_data_kit/generators/device/ScreenState.java +++ b/src/com/audacious_software/passive_data_kit/generators/device/ScreenState.java @@ -10,6 +10,7 @@ import android.database.sqlite.SQLiteDatabase; import android.os.Build; import android.os.Bundle; +import android.preference.PreferenceManager; import android.view.Display; import android.view.LayoutInflater; import android.view.View; @@ -39,6 +40,9 @@ public class ScreenState extends Generator{ private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.device.ScreenState.ENABLED"; private static final boolean ENABLED_DEFAULT = true; + private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.device.ScreenState.DATA_RETENTION_PERIOD"; + private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000); + @SuppressWarnings("WeakerAccess") public static final String STATE_DOZE = "doze"; @@ -172,6 +176,8 @@ public void onReceive(final Context context, Intent intent) { } this.setDatabaseVersion(this.mDatabase, ScreenState.DATABASE_VERSION); + + this.flushCachedData(); } @SuppressWarnings("unused") @@ -476,4 +482,28 @@ public static long latestPointGenerated(Context context) { public Cursor queryHistory(String[] cols, String where, String[] args, String orderBy) { return this.mDatabase.query(ScreenState.TABLE_HISTORY, cols, where, args, null, null, orderBy); } + + @Override + protected void flushCachedData() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + + long retentionPeriod = prefs.getLong(ScreenState.DATA_RETENTION_PERIOD, ScreenState.DATA_RETENTION_PERIOD_DEFAULT); + + long start = System.currentTimeMillis() - retentionPeriod; + + String where = ScreenState.HISTORY_OBSERVED + " < ?"; + String[] args = { "" + start }; + + this.mDatabase.delete(ScreenState.TABLE_HISTORY, where, args); + } + + @Override + public void setCachedDataRetentionPeriod(long period) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + SharedPreferences.Editor e = prefs.edit(); + + e.putLong(ScreenState.DATA_RETENTION_PERIOD, period); + + e.apply(); + } } diff --git a/src/com/audacious_software/passive_data_kit/generators/diagnostics/AppEvent.java b/src/com/audacious_software/passive_data_kit/generators/diagnostics/AppEvent.java index 0a9d47a..df3e75f 100755 --- a/src/com/audacious_software/passive_data_kit/generators/diagnostics/AppEvent.java +++ b/src/com/audacious_software/passive_data_kit/generators/diagnostics/AppEvent.java @@ -6,6 +6,7 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; @@ -41,6 +42,9 @@ public class AppEvent extends Generator{ private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.diagnostics.AppEvent.ENABLED"; private static final boolean ENABLED_DEFAULT = true; + private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.diagnostics.AppEvent.DATA_RETENTION_PERIOD"; + private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000); + private static final String DATABASE_PATH = "pdk-app-event.sqlite"; private static final int DATABASE_VERSION = 1; @@ -107,6 +111,8 @@ private void startGenerator() { } this.setDatabaseVersion(this.mDatabase, AppEvent.DATABASE_VERSION); + + this.flushCachedData(); } @SuppressWarnings("unused") @@ -441,4 +447,28 @@ public static View getDisclosureDataView(final GeneratorViewHolder holder) { return disclosureView; } + + @Override + protected void flushCachedData() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + + long retentionPeriod = prefs.getLong(AppEvent.DATA_RETENTION_PERIOD, AppEvent.DATA_RETENTION_PERIOD_DEFAULT); + + long start = System.currentTimeMillis() - retentionPeriod; + + String where = AppEvent.HISTORY_OBSERVED + " < ?"; + String[] args = { "" + start }; + + this.mDatabase.delete(AppEvent.TABLE_HISTORY, where, args); + } + + @Override + public void setCachedDataRetentionPeriod(long period) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + SharedPreferences.Editor e = prefs.edit(); + + e.putLong(AppEvent.DATA_RETENTION_PERIOD, period); + + e.apply(); + } } diff --git a/src/com/audacious_software/passive_data_kit/generators/diagnostics/SystemStatus.java b/src/com/audacious_software/passive_data_kit/generators/diagnostics/SystemStatus.java index 32d9750..ab32e66 100755 --- a/src/com/audacious_software/passive_data_kit/generators/diagnostics/SystemStatus.java +++ b/src/com/audacious_software/passive_data_kit/generators/diagnostics/SystemStatus.java @@ -14,6 +14,7 @@ import android.os.Build; import android.os.Bundle; import android.os.StatFs; +import android.preference.PreferenceManager; import android.support.v4.content.ContextCompat; import android.view.LayoutInflater; import android.view.View; @@ -51,6 +52,9 @@ public class SystemStatus extends Generator { private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.diagnostics.SystemStatus.ENABLED"; private static final boolean ENABLED_DEFAULT = true; + private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.diagnostics.SystemStatus.DATA_RETENTION_PERIOD"; + private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000); + private static final String ACTION_HEARTBEAT = "com.audacious_software.passive_data_kit.generators.diagnostics.SystemStatus.ACTION_HEARTBEAT"; private static final String DATABASE_PATH = "pdk-system-status.sqlite"; @@ -186,6 +190,8 @@ public void onReceive(Context context, Intent intent) { IntentFilter filter = new IntentFilter(SystemStatus.ACTION_HEARTBEAT); this.mContext.registerReceiver(this.mReceiver, filter); + + this.flushCachedData(); } @SuppressWarnings("unused") @@ -488,4 +494,28 @@ public static long latestPointGenerated(Context context) { public Cursor queryHistory(String[] cols, String where, String[] args, String orderBy) { return this.mDatabase.query(SystemStatus.TABLE_HISTORY, cols, where, args, null, null, orderBy); } + + @Override + protected void flushCachedData() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + + long retentionPeriod = prefs.getLong(SystemStatus.DATA_RETENTION_PERIOD, SystemStatus.DATA_RETENTION_PERIOD_DEFAULT); + + long start = System.currentTimeMillis() - retentionPeriod; + + String where = SystemStatus.HISTORY_OBSERVED + " < ?"; + String[] args = { "" + start }; + + this.mDatabase.delete(SystemStatus.TABLE_HISTORY, where, args); + } + + @Override + public void setCachedDataRetentionPeriod(long period) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + SharedPreferences.Editor e = prefs.edit(); + + e.putLong(SystemStatus.DATA_RETENTION_PERIOD, period); + + e.apply(); + } } diff --git a/src/com/audacious_software/passive_data_kit/generators/sensors/Accelerometer.java b/src/com/audacious_software/passive_data_kit/generators/sensors/Accelerometer.java index 6d97746..ba12cb4 100755 --- a/src/com/audacious_software/passive_data_kit/generators/sensors/Accelerometer.java +++ b/src/com/audacious_software/passive_data_kit/generators/sensors/Accelerometer.java @@ -54,6 +54,9 @@ public class Accelerometer extends SensorGenerator implements SensorEventListene private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.device.Accelerometer.ENABLED"; private static final boolean ENABLED_DEFAULT = true; + private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.sensors.Accelerometer.DATA_RETENTION_PERIOD"; + private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000); + private static final String IGNORE_POWER_MANAGEMENT = "com.audacious_software.passive_data_kit.generators.sensors.Accelerometer.IGNORE_POWER_MANAGEMENT"; private static final boolean IGNORE_POWER_MANAGEMENT_DEFAULT = true; @@ -270,6 +273,8 @@ public void run() { } else { this.stopGenerator(); } + + this.flushCachedData(); } private void stopGenerator() { @@ -778,4 +783,28 @@ public void run() { public void onAccuracyChanged(Sensor sensor, int newAccuracy) { // Do nothing... } + + @Override + protected void flushCachedData() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + + long retentionPeriod = prefs.getLong(Accelerometer.DATA_RETENTION_PERIOD, Accelerometer.DATA_RETENTION_PERIOD_DEFAULT); + + long start = System.currentTimeMillis() - retentionPeriod; + + String where = Accelerometer.HISTORY_OBSERVED + " < ?"; + String[] args = { "" + start }; + + this.mDatabase.delete(Accelerometer.TABLE_HISTORY, where, args); + } + + @Override + public void setCachedDataRetentionPeriod(long period) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + SharedPreferences.Editor e = prefs.edit(); + + e.putLong(Accelerometer.DATA_RETENTION_PERIOD, period); + + e.apply(); + } } diff --git a/src/com/audacious_software/passive_data_kit/generators/sensors/AmbientLight.java b/src/com/audacious_software/passive_data_kit/generators/sensors/AmbientLight.java index 54b23bb..a629dee 100755 --- a/src/com/audacious_software/passive_data_kit/generators/sensors/AmbientLight.java +++ b/src/com/audacious_software/passive_data_kit/generators/sensors/AmbientLight.java @@ -54,6 +54,9 @@ public class AmbientLight extends SensorGenerator implements SensorEventListener private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.device.AmbientLight.ENABLED"; private static final boolean ENABLED_DEFAULT = true; + private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.sensors.AmbientLight.DATA_RETENTION_PERIOD"; + private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000); + private static final String IGNORE_POWER_MANAGEMENT = "com.audacious_software.passive_data_kit.generators.sensors.AmbientLight.IGNORE_POWER_MANAGEMENT"; private static final boolean IGNORE_POWER_MANAGEMENT_DEFAULT = true; @@ -191,6 +194,8 @@ public void run() } else { this.stopGenerator(); } + + this.flushCachedData(); } private void stopGenerator() { @@ -610,4 +615,28 @@ public void run() { public void onAccuracyChanged(Sensor sensor, int newAccuracy) { // Do nothing... } + + @Override + protected void flushCachedData() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + + long retentionPeriod = prefs.getLong(AmbientLight.DATA_RETENTION_PERIOD, AmbientLight.DATA_RETENTION_PERIOD_DEFAULT); + + long start = System.currentTimeMillis() - retentionPeriod; + + String where = AmbientLight.HISTORY_OBSERVED + " < ?"; + String[] args = { "" + start }; + + this.mDatabase.delete(AmbientLight.TABLE_HISTORY, where, args); + } + + @Override + public void setCachedDataRetentionPeriod(long period) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + SharedPreferences.Editor e = prefs.edit(); + + e.putLong(AmbientLight.DATA_RETENTION_PERIOD, period); + + e.apply(); + } } diff --git a/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java b/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java index bcb78cb..beb2123 100755 --- a/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java +++ b/src/com/audacious_software/passive_data_kit/generators/services/GoogleAwareness.java @@ -8,9 +8,11 @@ import android.os.Bundle; import android.os.Handler; import android.os.Looper; +import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; +import android.util.Log; import com.audacious_software.passive_data_kit.activities.generators.RequestPermissionActivity; import com.audacious_software.passive_data_kit.diagnostics.DiagnosticAction; @@ -32,6 +34,10 @@ public class GoogleAwareness extends Generator implements GoogleApiClient.Connec private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.services.GoogleAwareness.ENABLED"; private static final boolean ENABLED_DEFAULT = true; + + private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.services.GoogleAwareness.DATA_RETENTION_PERIOD"; + private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000); + private static final long SENSING_INTERVAL = 60 * 1000; private static GoogleAwareness sInstance = null; @@ -81,6 +87,8 @@ public void run() { t.start(); Generators.getInstance(this.mContext).registerCustomViewClass(GoogleAwareness.GENERATOR_IDENTIFIER, GoogleAwareness.class); + + this.flushCachedData(); } @SuppressWarnings("unused") @@ -204,4 +212,30 @@ public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { public List fetchPayloads() { return new ArrayList<>(); } + + @Override + protected void flushCachedData() { + Log.e("PDK", "TODO: Implement data cache flush in GoogleAwareness!"); + +// SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); +// +// long retentionPeriod = prefs.getLong(GoogleAwareness.DATA_RETENTION_PERIOD, GoogleAwareness.DATA_RETENTION_PERIOD_DEFAULT); +// +// long start = System.currentTimeMillis() - retentionPeriod; +// +// String where = GoogleAwareness.HISTORY_OBSERVED + " < ?"; +// String[] args = { "" + start }; +// +// this.mDatabase.delete(GoogleAwareness.TABLE_HISTORY, where, args); + } + + @Override + public void setCachedDataRetentionPeriod(long period) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + SharedPreferences.Editor e = prefs.edit(); + + e.putLong(GoogleAwareness.DATA_RETENTION_PERIOD, period); + + e.apply(); + } } diff --git a/src/com/audacious_software/passive_data_kit/generators/vr/DaydreamViewController.java b/src/com/audacious_software/passive_data_kit/generators/vr/DaydreamViewController.java index 9921689..bbd8b5b 100755 --- a/src/com/audacious_software/passive_data_kit/generators/vr/DaydreamViewController.java +++ b/src/com/audacious_software/passive_data_kit/generators/vr/DaydreamViewController.java @@ -4,6 +4,8 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -26,6 +28,9 @@ public class DaydreamViewController extends Generator { private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.vr.DaydreamViewController.ENABLED"; private static final boolean ENABLED_DEFAULT = true; + private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.vr.DaydreamViewController.DATA_RETENTION_PERIOD"; + private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000); + private static DaydreamViewController sInstance = null; private BroadcastReceiver mReceiver = null; @@ -84,6 +89,8 @@ public void onUpdate() { private void startGenerator() { Generators.getInstance(this.mContext).registerCustomViewClass(DaydreamViewController.GENERATOR_IDENTIFIER, DaydreamViewController.class); + + this.flushCachedData(); } @SuppressWarnings("unused") @@ -187,4 +194,31 @@ public List fetchPayloads() { public static void broadcastLatestDataPoint(Context context) { // Generators.getInstance(context).transmitData(DaydreamViewController.GENERATOR_IDENTIFIER, new Bundle()); } + + @Override + protected void flushCachedData() { + Log.e("PDK", "TODO: Implement data cache flush in DaydreamViewController!"); + +// SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); +// +// long retentionPeriod = prefs.getLong(GoogleAwareness.DATA_RETENTION_PERIOD, GoogleAwareness.DATA_RETENTION_PERIOD_DEFAULT); +// +// long start = System.currentTimeMillis() - retentionPeriod; +// +// String where = GoogleAwareness.HISTORY_OBSERVED + " < ?"; +// String[] args = { "" + start }; +// +// this.mDatabase.delete(GoogleAwareness.TABLE_HISTORY, where, args); + } + + @Override + public void setCachedDataRetentionPeriod(long period) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + SharedPreferences.Editor e = prefs.edit(); + + e.putLong(DaydreamViewController.DATA_RETENTION_PERIOD, period); + + e.apply(); + } + } diff --git a/src/com/audacious_software/passive_data_kit/generators/wearables/MicrosoftBand.java b/src/com/audacious_software/passive_data_kit/generators/wearables/MicrosoftBand.java index 1b9e88d..4346a20 100755 --- a/src/com/audacious_software/passive_data_kit/generators/wearables/MicrosoftBand.java +++ b/src/com/audacious_software/passive_data_kit/generators/wearables/MicrosoftBand.java @@ -6,7 +6,9 @@ import android.os.Bundle; import android.os.Handler; import android.os.Looper; +import android.preference.PreferenceManager; import android.support.annotation.NonNull; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -71,9 +73,13 @@ @SuppressWarnings("SimplifiableIfStatement") public class MicrosoftBand extends Generator { + private static final String GENERATOR_IDENTIFIER = "pdk-microsoft-band"; + private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.wearables.MicrosoftBand.ENABLED"; private static final boolean ENABLED_DEFAULT = true; - private static final String GENERATOR_IDENTIFIER = "pdk-microsoft-band"; + + private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.wearables.MicrosoftBand.DATA_RETENTION_PERIOD"; + private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000); private static final String TIMESTAMP_KEY = "timestamps"; private static final String X_KEY = "xs"; @@ -1445,4 +1451,31 @@ public static void bindViewHolder(DataPointViewHolder holder, Bundle dataPoint) public List fetchPayloads() { return new ArrayList<>(); } + + @Override + protected void flushCachedData() { + Log.e("PDK", "TODO: Implement data cache flush in MicrosoftBand!"); + +// SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); +// +// long retentionPeriod = prefs.getLong(MicrosoftBand.DATA_RETENTION_PERIOD, MicrosoftBand.DATA_RETENTION_PERIOD_DEFAULT); +// +// long start = System.currentTimeMillis() - retentionPeriod; +// +// String where = MicrosoftBand.HISTORY_OBSERVED + " < ?"; +// String[] args = { "" + start }; +// +// this.mDatabase.delete(MicrosoftBand.TABLE_HISTORY, where, args); + } + + @Override + public void setCachedDataRetentionPeriod(long period) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + SharedPreferences.Editor e = prefs.edit(); + + e.putLong(MicrosoftBand.DATA_RETENTION_PERIOD, period); + + e.apply(); + } + } diff --git a/src/com/audacious_software/passive_data_kit/generators/wearables/WithingsDevice.java b/src/com/audacious_software/passive_data_kit/generators/wearables/WithingsDevice.java index cb9e9b3..cebd42f 100755 --- a/src/com/audacious_software/passive_data_kit/generators/wearables/WithingsDevice.java +++ b/src/com/audacious_software/passive_data_kit/generators/wearables/WithingsDevice.java @@ -80,6 +80,9 @@ public class WithingsDevice extends Generator { private static final String ENABLED = "com.audacious_software.passive_data_kit.generators.wearables.WithingsDevice.ENABLED"; private static final boolean ENABLED_DEFAULT = true; + private static final String DATA_RETENTION_PERIOD = "com.audacious_software.passive_data_kit.generators.wearables.WithingsDevice.DATA_RETENTION_PERIOD"; + private static final long DATA_RETENTION_PERIOD_DEFAULT = (60 * 24 * 60 * 60 * 1000); + private static final String DATASTREAM = "datastream"; private static final String DATASTREAM_ACTIVITY_MEASURES = "activity-measures"; private static final String DATASTREAM_INTRADAY_ACTIVITY = "intraday-activity"; @@ -386,6 +389,8 @@ public void run() { me.mHandler.post(fetchData); Generators.getInstance(this.mContext).registerCustomViewClass(WithingsDevice.GENERATOR_IDENTIFIER, WithingsDevice.class); + + this.flushCachedData(); } private String getProperty(String key) { @@ -1866,4 +1871,34 @@ public void enableServerFetch(boolean enable) { e.apply(); } + + @Override + protected void flushCachedData() { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + + long retentionPeriod = prefs.getLong(WithingsDevice.DATA_RETENTION_PERIOD, WithingsDevice.DATA_RETENTION_PERIOD_DEFAULT); + + long start = System.currentTimeMillis() - retentionPeriod; + + String where = WithingsDevice.HISTORY_OBSERVED + " < ?"; + String[] args = { "" + start }; + + this.mDatabase.delete(WithingsDevice.TABLE_ACTIVITY_MEASURE_HISTORY, where, args); + this.mDatabase.delete(WithingsDevice.TABLE_SLEEP_MEASURE_HISTORY, where, args); + this.mDatabase.delete(WithingsDevice.TABLE_BODY_MEASURE_HISTORY, where, args); + this.mDatabase.delete(WithingsDevice.TABLE_INTRADAY_ACTIVITY_HISTORY, where, args); + this.mDatabase.delete(WithingsDevice.TABLE_SLEEP_SUMMARY_HISTORY, where, args); + this.mDatabase.delete(WithingsDevice.TABLE_WORKOUT_HISTORY, where, args); + } + + @Override + public void setCachedDataRetentionPeriod(long period) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext); + SharedPreferences.Editor e = prefs.edit(); + + e.putLong(WithingsDevice.DATA_RETENTION_PERIOD, period); + + e.apply(); + } } +