From f30b56b9224759d16f7fd7caaadf61db9af9dfaf Mon Sep 17 00:00:00 2001 From: "Chris J. Karr" Date: Fri, 19 May 2017 09:40:50 -0500 Subject: [PATCH] Generator bug fixes --- .../generators/device/Battery.java | 2 + .../generators/wearables/WithingsDevice.java | 107 +++++++++--------- 2 files changed, 58 insertions(+), 51 deletions(-) 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 88fb162..56f7a1f 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 @@ -122,6 +122,8 @@ private void startGenerator() { @Override public void onReceive(final Context context, Intent intent) { if (me.mDatabase == null) { + PassiveDataKit.getInstance(context).start(); + return; } 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 62b5a7a..4daf542 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 @@ -17,7 +17,6 @@ import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.util.Base64; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -733,73 +732,77 @@ private void fetchIntradayActivities() { if (response.getInt("status") == 0) { JSONObject body = response.getJSONObject("body"); - JSONObject series = body.getJSONObject("series"); + try { + JSONObject series = body.getJSONObject("series"); - Iterator keys = series.keys(); + Iterator keys = series.keys(); - while (keys.hasNext()) { - String key = keys.next(); + while (keys.hasNext()) { + String key = keys.next(); - long timestamp = Long.parseLong(key); + long timestamp = Long.parseLong(key); - String where = WithingsDevice.INTRADAY_ACTIVITY_START + " = ?"; - String[] args = { "" + timestamp }; + String where = WithingsDevice.INTRADAY_ACTIVITY_START + " = ?"; + String[] args = { "" + timestamp }; - Cursor c = this.mDatabase.query(WithingsDevice.TABLE_INTRADAY_ACTIVITY_HISTORY, null, where, args, null, null, WithingsDevice.HISTORY_OBSERVED + " DESC"); + Cursor c = this.mDatabase.query(WithingsDevice.TABLE_INTRADAY_ACTIVITY_HISTORY, null, where, args, null, null, WithingsDevice.HISTORY_OBSERVED + " DESC"); - if (c.moveToNext() == false) { - JSONObject item = series.getJSONObject(key); + if (c.moveToNext() == false) { + JSONObject item = series.getJSONObject(key); - ContentValues values = new ContentValues(); - values.put(WithingsDevice.HISTORY_OBSERVED, now); - values.put(WithingsDevice.INTRADAY_ACTIVITY_START, timestamp); - values.put(WithingsDevice.INTRADAY_ACTIVITY_DURATION, item.getLong("duration")); + ContentValues values = new ContentValues(); + values.put(WithingsDevice.HISTORY_OBSERVED, now); + values.put(WithingsDevice.INTRADAY_ACTIVITY_START, timestamp); + values.put(WithingsDevice.INTRADAY_ACTIVITY_DURATION, item.getLong("duration")); - Bundle updated = new Bundle(); - updated.putLong(WithingsDevice.HISTORY_OBSERVED, System.currentTimeMillis()); - updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_START, timestamp); - updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_DURATION, item.getLong("duration")); + Bundle updated = new Bundle(); + updated.putLong(WithingsDevice.HISTORY_OBSERVED, System.currentTimeMillis()); + updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_START, timestamp); + updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_DURATION, item.getLong("duration")); - if (item.has("steps")) { - values.put(WithingsDevice.INTRADAY_ACTIVITY_STEPS, item.getLong("steps")); - updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_STEPS, item.getLong("steps")); - } + if (item.has("steps")) { + values.put(WithingsDevice.INTRADAY_ACTIVITY_STEPS, item.getLong("steps")); + updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_STEPS, item.getLong("steps")); + } - if (item.has("elevation")) { - values.put(WithingsDevice.INTRADAY_ACTIVITY_ELEVATION_CLIMBED, item.getLong("elevation")); - updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_ELEVATION_CLIMBED, item.getLong("elevation")); - } + if (item.has("elevation")) { + values.put(WithingsDevice.INTRADAY_ACTIVITY_ELEVATION_CLIMBED, item.getLong("elevation")); + updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_ELEVATION_CLIMBED, item.getLong("elevation")); + } - if (item.has("distance")) { - values.put(WithingsDevice.INTRADAY_ACTIVITY_DISTANCE, item.getLong("distance")); - updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_DISTANCE, item.getLong("distance")); - } + if (item.has("distance")) { + values.put(WithingsDevice.INTRADAY_ACTIVITY_DISTANCE, item.getLong("distance")); + updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_DISTANCE, item.getLong("distance")); + } - if (item.has("calories")) { - values.put(WithingsDevice.INTRADAY_ACTIVITY_CALORIES, item.getLong("calories")); - updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_CALORIES, item.getLong("calories")); - } + if (item.has("calories")) { + values.put(WithingsDevice.INTRADAY_ACTIVITY_CALORIES, item.getLong("calories")); + updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_CALORIES, item.getLong("calories")); + } - if (item.has("stroke")) { - values.put(WithingsDevice.INTRADAY_ACTIVITY_SWIM_STROKES, item.getLong("stroke")); - updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_SWIM_STROKES, item.getLong("stroke")); - } + if (item.has("stroke")) { + values.put(WithingsDevice.INTRADAY_ACTIVITY_SWIM_STROKES, item.getLong("stroke")); + updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_SWIM_STROKES, item.getLong("stroke")); + } - if (item.has("pool_lap")) { - values.put(WithingsDevice.INTRADAY_ACTIVITY_POOL_LAPS, item.getLong("pool_lap")); - updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_POOL_LAPS, item.getLong("pool_lap")); - } + if (item.has("pool_lap")) { + values.put(WithingsDevice.INTRADAY_ACTIVITY_POOL_LAPS, item.getLong("pool_lap")); + updated.putLong(WithingsDevice.INTRADAY_ACTIVITY_POOL_LAPS, item.getLong("pool_lap")); + } - this.mDatabase.insert(WithingsDevice.TABLE_INTRADAY_ACTIVITY_HISTORY, null, values); + this.mDatabase.insert(WithingsDevice.TABLE_INTRADAY_ACTIVITY_HISTORY, null, values); - this.annotateGeneratorReading(updated); + this.annotateGeneratorReading(updated); - updated.putString(WithingsDevice.DATASTREAM, WithingsDevice.DATASTREAM_INTRADAY_ACTIVITY); + updated.putString(WithingsDevice.DATASTREAM, WithingsDevice.DATASTREAM_INTRADAY_ACTIVITY); - Generators.getInstance(this.mContext).notifyGeneratorUpdated(WithingsDevice.GENERATOR_IDENTIFIER, updated); - } + Generators.getInstance(this.mContext).notifyGeneratorUpdated(WithingsDevice.GENERATOR_IDENTIFIER, updated); + } - c.close(); + c.close(); + } + } catch (JSONException e) { + // JSON type mismatch when day's data is empty. Do nothing... } } } catch (JSONException e) { @@ -1391,6 +1394,10 @@ private static String bindActivityPage(ViewGroup container, DataPointViewHolder entries.add(new PieEntry((long) intenseActivity, context.getString(R.string.generator_withings_intense_activities_label))); } + if (entries.size() == 0) { + entries.add(new PieEntry(1L, context.getString(R.string.generator_withings_soft_activities_label))); + } + PieDataSet set = new PieDataSet(entries, " "); int[] colors = { @@ -1686,8 +1693,6 @@ private static String bindBodyPage(ViewGroup container, DataPointViewHolder hold Cursor c = withings.mDatabase.query(WithingsDevice.TABLE_BODY_MEASURE_HISTORY, null, null, null, null, null, WithingsDevice.BODY_MEASURE_HISTORY_DATE + " DESC"); - Log.e("PDK", "MEASURE COUNT: " + c.getCount()); - while (c.moveToNext() && bodyValues.size() < labels.length) { String label = c.getString(c.getColumnIndex(WithingsDevice.BODY_MEASURE_HISTORY_TYPE));