Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented performance improvements in data stream activity.
  • Loading branch information
audaciouscode committed May 24, 2017
1 parent 43e359f commit d30f540
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 37 deletions.
Expand Up @@ -30,7 +30,7 @@ protected void onCreate(Bundle savedInstanceState) {

this.mAdapter = new DataPointsAdapter();
this.mAdapter.setContext(this.getApplicationContext());
this.mAdapter.sortGenerators();
this.mAdapter.sortGenerators(true);

RecyclerView listView = (RecyclerView) this.findViewById(R.id.list_view);

Expand Down Expand Up @@ -65,7 +65,7 @@ protected void onPause() {
}

@Override
public void onGeneratorUpdated(String identifier, long timestamp, Bundle data) {
public void onGeneratorUpdated(final String identifier, long timestamp, Bundle data) {
final DataStreamActivity me = this;

if (me.mIsUpdating) {
Expand All @@ -77,10 +77,10 @@ public void onGeneratorUpdated(String identifier, long timestamp, Bundle data) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
me.mAdapter.sortGenerators();
me.mAdapter.sortGenerators(false);

try {
me.mAdapter.notifyDataSetChanged();
me.mAdapter.notifyDataSetChanged(identifier);
} catch (IllegalStateException e) {
// Do nothing - recycler is already updating...
}
Expand Down Expand Up @@ -145,6 +145,6 @@ private void toggleSortLock() {
e.putBoolean(DataPointsAdapter.SORT_BY_UPDATED, (sortEnabled == false));
e.apply();

this.mAdapter.sortGenerators();
this.mAdapter.sortGenerators(true);
}
}
Expand Up @@ -16,7 +16,6 @@
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class DataPointsAdapter extends RecyclerView.Adapter<DataPointViewHolder> {
Expand Down Expand Up @@ -60,7 +59,7 @@ private List<Class<? extends Generator>> getGenerators(Context context) {
this.mActiveGenerators = Generators.getInstance(context).activeGenerators();
}

this.sortGenerators();
this.sortGenerators(false);

return this.mActiveGenerators;
}
Expand Down Expand Up @@ -100,7 +99,7 @@ public int getItemCount() {
return this.mActiveGenerators.size();
}

public void sortGenerators() {
public void sortGenerators(boolean redrawAll) {
final Context context = this.mContext;

if (this.mActiveGenerators == null) {
Expand Down Expand Up @@ -153,6 +152,33 @@ public int compare(Class<? extends Generator> one, Class<? extends Generator> tw
}
});
}

if (redrawAll) {
this.notifyDataSetChanged();
}
}

public void notifyDataSetChanged(String identifier) {
int position = -1;

for (int i = 0; position == -1 && i < this.mActiveGenerators.size(); i++) {
Class<? extends Generator> generatorClass = this.mActiveGenerators.get(i);

try {
Method generatorIdentifier = generatorClass.getDeclaredMethod("generatorIdentifier", null);

if (identifier.equals(generatorIdentifier.invoke(null))) {
position = i;
}
} catch (Exception e) {
}
}

if (position != -1) {
this.notifyItemChanged(position);
} else {
this.notifyDataSetChanged();
}
}

public int getItemViewType (int position) {
Expand Down
Expand Up @@ -108,6 +108,10 @@ public class PhoneCalls extends Generator {
private SQLiteDatabase mDatabase = null;
private long mSampleInterval = 60000;

public static String generatorIdentifier() {
return PhoneCalls.GENERATOR_IDENTIFIER;
}

public static PhoneCalls getInstance(Context context) {
if (PhoneCalls.sInstance == null) {
PhoneCalls.sInstance = new PhoneCalls(context.getApplicationContext());
Expand Down
Expand Up @@ -86,6 +86,10 @@ public class TextMessages extends Generator {
private SQLiteDatabase mDatabase = null;
private long mSampleInterval = 60000;

public static String generatorIdentifier() {
return TextMessages.GENERATOR_IDENTIFIER;
}

public static TextMessages getInstance(Context context) {
if (TextMessages.sInstance == null) {
TextMessages.sInstance = new TextMessages(context.getApplicationContext());
Expand Down
Expand Up @@ -92,6 +92,10 @@ public class Battery extends Generator {
private long mCleanupInterval = (24 * 60 * 60 * 1000);
private long mLastCleanup = 0;

public static String generatorIdentifier() {
return Battery.GENERATOR_IDENTIFIER;
}

public static Battery getInstance(Context context) {
if (Battery.sInstance == null) {
Battery.sInstance = new Battery(context.getApplicationContext());
Expand Down
Expand Up @@ -64,6 +64,10 @@ public class ForegroundApplication extends Generator{
private AppChecker mAppChecker = null;
private long mLastTimestamp = 0;

public static String generatorIdentifier() {
return ForegroundApplication.GENERATOR_IDENTIFIER;
}

public static ForegroundApplication getInstance(Context context) {
if (ForegroundApplication.sInstance == null) {
ForegroundApplication.sInstance = new ForegroundApplication(context.getApplicationContext());
Expand Down
Expand Up @@ -129,6 +129,10 @@ public class Location extends Generator implements GoogleApiClient.ConnectionCal
public static final String HISTORY_LOCATION_TIMESTAMP = "location_timestamp";
public static final String HISTORY_ACCURACY = "accuracy";

public static String generatorIdentifier() {
return Location.GENERATOR_IDENTIFIER;
}

public static Location getInstance(Context context) {
if (Location.sInstance == null) {
Location.sInstance = new Location(context.getApplicationContext());
Expand Down Expand Up @@ -942,8 +946,6 @@ public android.location.Location getLastKnownLocation() {

c.close();



if (lastLocation != null) {
return lastLocation;
}
Expand Down Expand Up @@ -996,8 +998,10 @@ public android.location.Location getLastKnownLocation() {
public void setUpdateInterval(long interval) {
this.mUpdateInterval = interval;

this.stopGenerator();
this.startGenerator();
if (Location.isRunning(this.mContext)) {
this.stopGenerator();
this.startGenerator();
}
}

public Cursor queryHistory(String[] cols, String where, String[] args, String orderBy) {
Expand Down
Expand Up @@ -57,6 +57,10 @@ public class ScreenState extends Generator{

private SQLiteDatabase mDatabase = null;

public static String generatorIdentifier() {
return ScreenState.GENERATOR_IDENTIFIER;
}

public static ScreenState getInstance(Context context) {
if (ScreenState.sInstance == null) {
ScreenState.sInstance = new ScreenState(context.getApplicationContext());
Expand Down
Expand Up @@ -63,6 +63,10 @@ public class AppEvent extends Generator{

private int mPage = 0;

public static String generatorIdentifier() {
return AppEvent.GENERATOR_IDENTIFIER;
}

public static AppEvent getInstance(Context context) {
if (AppEvent.sInstance == null) {
AppEvent.sInstance = new AppEvent(context.getApplicationContext());
Expand Down
Expand Up @@ -73,6 +73,10 @@ public class SystemStatus extends Generator {
private long mLastTimestamp = 0;
private long mRefreshInterval = (5 * 60 * 1000);

public static String generatorIdentifier() {
return SystemStatus.GENERATOR_IDENTIFIER;
}

public static SystemStatus getInstance(Context context) {
if (SystemStatus.sInstance == null) {
SystemStatus.sInstance = new SystemStatus(context.getApplicationContext());
Expand Down
Expand Up @@ -46,10 +46,6 @@
import java.util.Date;
import java.util.List;

/**
* Created by cjkarr on 4/17/2017.
*/

public class Accelerometer extends SensorGenerator implements SensorEventListener {
private static final String GENERATOR_IDENTIFIER = "pdk-sensor-accelerometer";

Expand Down Expand Up @@ -108,6 +104,10 @@ public class Accelerometer extends SensorGenerator implements SensorEventListene
private long mLatestTimestamp = 0;
private Thread mIntervalThread = null;

public static String generatorIdentifier() {
return Accelerometer.GENERATOR_IDENTIFIER;
}

public static Accelerometer getInstance(Context context) {
if (Accelerometer.sInstance == null) {
Accelerometer.sInstance = new Accelerometer(context.getApplicationContext());
Expand Down Expand Up @@ -369,8 +369,8 @@ public static void bindViewHolder(final DataPointViewHolder holder) {
final long now = System.currentTimeMillis() / (1000 * 60 * 5);
final long start = now - (24 * 12); // * 60);

View cardContent = itemView.findViewById(R.id.card_content);
View cardEmpty = itemView.findViewById(R.id.card_empty);
final View cardContent = itemView.findViewById(R.id.card_content);
final View cardEmpty = itemView.findViewById(R.id.card_empty);
TextView dateLabel = (TextView) itemView.findViewById(R.id.generator_data_point_date);

if (context instanceof Activity) {
Expand All @@ -379,7 +379,7 @@ public static void bindViewHolder(final DataPointViewHolder holder) {

dateLabel.setText(Generator.formatTimestamp(context, Accelerometer.latestPointGenerated(generator.mContext) / 1000));

LineChart chart = (LineChart) holder.itemView.findViewById(R.id.accelerometer_chart);
final LineChart chart = (LineChart) holder.itemView.findViewById(R.id.accelerometer_chart);
chart.setNoDataText(context.getString(R.string.pdk_generator_chart_loading_data));
chart.setNoDataTextColor(0xFFE0E0E0);

Expand All @@ -395,7 +395,7 @@ public void run() {
final ArrayList<Entry> zLowValues = new ArrayList<>();
final ArrayList<Entry> zHighValues = new ArrayList<>();

final String where = Accelerometer.HISTORY_OBSERVED + " >= ? AND _id % 1024 = 0";
final String where = Accelerometer.HISTORY_OBSERVED + " >= ? AND _id % 256 = 0";
final String[] args = { "" + (System.currentTimeMillis() - (24 * 60 * 60 * 1000)) };

Cursor c = generator.mDatabase.query(Accelerometer.TABLE_HISTORY, null, where, args, null, null, Accelerometer.HISTORY_OBSERVED + " DESC");
Expand Down Expand Up @@ -562,8 +562,6 @@ public void run() {
chartData.addDataSet(set);
}

final LineChart chart = (LineChart) itemView.findViewById(R.id.accelerometer_chart);

if (chart != null) {
chart.setViewPortOffsets(0, 0, 0, 0);
chart.setHighlightPerDragEnabled(false);
Expand Down Expand Up @@ -610,6 +608,8 @@ public String getFormattedValue(float value, AxisBase axis) {
chart.setVisibleYRange((float) Math.floor(finalMinValue) - 1, (float) Math.ceil(finalMaxValue) + 1, YAxis.AxisDependency.LEFT);
chart.setNoDataText(context.getString(R.string.pdk_generator_chart_loading_data));
chart.setData(chartData);

chart.invalidate();
}

Accelerometer.sIsDrawing = false;
Expand Down
Expand Up @@ -68,7 +68,6 @@ public class AmbientLight extends SensorGenerator implements SensorEventListener
private static AmbientLight sInstance = null;
private static Handler sHandler = null;
private static boolean sIsDrawing = false;
private static long sLastDrawStart = 0;

private SQLiteDatabase mDatabase = null;

Expand All @@ -91,6 +90,10 @@ public class AmbientLight extends SensorGenerator implements SensorEventListener
long mBaseTimestamp = 0;
private long mLatestTimestamp = 0;

public static String generatorIdentifier() {
return AmbientLight.GENERATOR_IDENTIFIER;
}

public static AmbientLight getInstance(Context context) {
if (AmbientLight.sInstance == null) {
AmbientLight.sInstance = new AmbientLight(context.getApplicationContext());
Expand Down Expand Up @@ -174,9 +177,9 @@ public void run()
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.mContext);

if (prefs.getBoolean(AmbientLight.IGNORE_POWER_MANAGEMENT, AmbientLight.IGNORE_POWER_MANAGEMENT_DEFAULT)) {
Generators.getInstance(this.mContext).acquireWakeLock(Accelerometer.IDENTIFIER, PowerManager.PARTIAL_WAKE_LOCK);
Generators.getInstance(this.mContext).acquireWakeLock(AmbientLight.IDENTIFIER, PowerManager.PARTIAL_WAKE_LOCK);
} else {
Generators.getInstance(this.mContext).releaseWakeLock(Accelerometer.IDENTIFIER);
Generators.getInstance(this.mContext).releaseWakeLock(AmbientLight.IDENTIFIER);
}
} else {
this.stopGenerator();
Expand Down Expand Up @@ -265,14 +268,6 @@ public static void bindViewHolder(final DataPointViewHolder holder) {
return;
}

final long drawStart = System.currentTimeMillis();

if (drawStart - AmbientLight.sLastDrawStart < (30 * 1000)) {
return;
}

AmbientLight.sLastDrawStart = drawStart;

AmbientLight.sIsDrawing = true;

final Context context = holder.itemView.getContext();
Expand All @@ -292,7 +287,7 @@ public static void bindViewHolder(final DataPointViewHolder holder) {

dateLabel.setText(Generator.formatTimestamp(context, AmbientLight.latestPointGenerated(context) / 1000));

LineChart chart = (LineChart) holder.itemView.findViewById(R.id.light_chart);
final LineChart chart = (LineChart) holder.itemView.findViewById(R.id.light_chart);
chart.setNoDataText(context.getString(R.string.pdk_generator_chart_loading_data));
chart.setNoDataTextColor(0xFFE0E0E0);

Expand All @@ -310,7 +305,7 @@ public void run() {
float lowLevel = -1;
float highLevel = -1;

final String where = Accelerometer.HISTORY_OBSERVED + " >= ? AND _id";
final String where = AmbientLight.HISTORY_OBSERVED + " >= ?";
final String[] args = { "" + (System.currentTimeMillis() - (24 * 60 * 60 * 1000)) };

Cursor c = generator.mDatabase.query(AmbientLight.TABLE_HISTORY, null, where, args, null, null, AmbientLight.HISTORY_OBSERVED + " DESC");
Expand Down Expand Up @@ -370,7 +365,6 @@ public void run() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
final LineChart chart = (LineChart) holder.itemView.findViewById(R.id.light_chart);
chart.setViewPortOffsets(0,0,0,0);
chart.setHighlightPerDragEnabled(false);
chart.setHighlightPerTapEnabled(false);
Expand Down Expand Up @@ -450,6 +444,8 @@ public String getFormattedValue(float value, AxisBase axis) {
chart.setVisibleYRange((float) Math.floor(finalMinValue) - 1, (float) Math.ceil(finalMaxValue) + 1, YAxis.AxisDependency.LEFT);
chart.setData(chartData);

chart.invalidate();

AmbientLight.sIsDrawing = false;
}
});
Expand Down Expand Up @@ -480,7 +476,7 @@ public static long latestPointGenerated(Context context) {
AmbientLight me = AmbientLight.getInstance(context);

if (me.mLatestTimestamp == 0) {
Cursor c = me.mDatabase.query(AmbientLight.TABLE_HISTORY, null, null, null, null, null, Accelerometer.HISTORY_OBSERVED + " DESC", "1");
Cursor c = me.mDatabase.query(AmbientLight.TABLE_HISTORY, null, null, null, null, null, AmbientLight.HISTORY_OBSERVED + " DESC", "1");

if (c.moveToNext()) {
me.mLatestTimestamp = c.getLong(c.getColumnIndex(AmbientLight.HISTORY_OBSERVED) / (1000 * 1000));
Expand Down
Expand Up @@ -159,6 +159,10 @@ public class MicrosoftBand extends Generator
private ArrayList<MicrosoftBand.SkinTemperatureDataPoint> mSkinTemperatureDataPoints = new ArrayList<>();
private ArrayList<MicrosoftBand.UltravioletLightDataPoint> mUltravioletLightDataPoints = new ArrayList<>();

public static String generatorIdentifier() {
return MicrosoftBand.GENERATOR_IDENTIFIER;
}

public static MicrosoftBand getInstance(Context context) {
if (MicrosoftBand.sInstance == null) {
MicrosoftBand.sInstance = new MicrosoftBand(context.getApplicationContext());
Expand Down
Expand Up @@ -237,6 +237,10 @@ public class WithingsDevice extends Generator {

private int mPage = 0;

public static String generatorIdentifier() {
return WithingsDevice.GENERATOR_IDENTIFIER;
}

public static WithingsDevice getInstance(Context context) {
if (WithingsDevice.sInstance == null) {
WithingsDevice.sInstance = new WithingsDevice(context.getApplicationContext());
Expand Down

0 comments on commit d30f540

Please sign in to comment.