Skip to content

Commit

Permalink
Visualization performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
audaciouscode committed May 13, 2017
1 parent fa714a9 commit b0ccf25
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 215 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -9,7 +9,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.android.tools.build:gradle:2.3.2'
}
}

Expand Down
Expand Up @@ -9,6 +9,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 All @@ -21,15 +22,19 @@
public class DataStreamActivity extends AppCompatActivity implements Generators.GeneratorUpdatedListener {
private DataPointsAdapter mAdapter = null;
private Menu mMenu = null;
private boolean mIsUpdating = false;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.layout_data_stream_pdk);
this.setTitle(R.string.activity_data_stream);
this.getSupportActionBar().setSubtitle(this.getResources().getQuantityString(R.plurals.activity_data_stream_subtitle, 0, 0));

this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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

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

Expand Down Expand Up @@ -67,12 +72,21 @@ protected void onPause() {
public void onGeneratorUpdated(String identifier, long timestamp, Bundle data) {
final DataStreamActivity me = this;

if (me.mIsUpdating) {
return;
}

me.mIsUpdating = true;

this.runOnUiThread(new Runnable() {
@Override
public void run() {
me.mAdapter.sortGenerators();
me.mAdapter.notifyDataSetChanged();
int count = me.mAdapter.getItemCount();
me.getSupportActionBar().setSubtitle(me.getResources().getQuantityString(R.plurals.activity_data_stream_subtitle, count, count));

me.mIsUpdating = false;
}
});
}
Expand All @@ -83,20 +97,29 @@ public boolean onCreateOptionsMenu(Menu menu) {

this.mMenu = menu;

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean sortEnabled = prefs.getBoolean(DataPointsAdapter.SORT_BY_UPDATED, DataPointsAdapter.SORT_BY_UPDATED_DEFAULT);

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

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

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) {
if (id == android.R.id.home) {
this.finish();
return true;
} else if (id == R.id.action_pdk_toggle_sort_lock) {
this.toggleSortLock();

return true;
}

Expand All @@ -106,20 +129,20 @@ public boolean onOptionsItemSelected(MenuItem item) {
private void toggleSortLock() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

boolean locked = prefs.getBoolean(DataPointsAdapter.SORT_BY_UPDATED, DataPointsAdapter.SORT_BY_UPDATED_DEFAULT);
boolean sortEnabled = 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 {
if (sortEnabled) {
lockedItem.setIcon(R.drawable.ic_pdk_action_lock);
} else {
lockedItem.setIcon(R.drawable.ic_pdk_action_unlock);
}

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

this.mAdapter.notifyDataSetChanged();
this.mAdapter.sortGenerators();
}
}
Expand Up @@ -24,6 +24,7 @@ public class DataPointsAdapter extends RecyclerView.Adapter<DataPointViewHolder>
public static final boolean SORT_BY_UPDATED_DEFAULT = true;

private Context mContext = null;
private List<Class<? extends Generator>> mActiveGenerators = null;

@Override
public DataPointViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Expand Down Expand Up @@ -55,13 +56,21 @@ public DataPointViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}

private List<Class<? extends Generator>> getGenerators(Context context) {
if (this.mActiveGenerators == null) {
this.mActiveGenerators = Generators.getInstance(context).activeGenerators();
}

this.sortGenerators();

return this.mActiveGenerators;
}

@Override
public void onBindViewHolder(final DataPointViewHolder holder, int position) {
List<Class<? extends Generator>> activeGenerators = Generators.getInstance(holder.itemView.getContext()).activeGenerators();

this.sortGenerators(this.mContext, activeGenerators);
this.getGenerators(holder.itemView.getContext());

Class<? extends Generator> generatorClass = activeGenerators.get(position);
Class<? extends Generator> generatorClass = this.mActiveGenerators.get(position);

try {
Method bindViewHolder = generatorClass.getDeclaredMethod("bindViewHolder", DataPointViewHolder.class);
Expand All @@ -86,14 +95,24 @@ public void onBindViewHolder(final DataPointViewHolder holder, int position) {

@Override
public int getItemCount() {
return Generators.getInstance(null).activeGenerators().size();
this.getGenerators(this.mContext);

return this.mActiveGenerators.size();
}

private void sortGenerators(final Context context, List<Class<? extends Generator>> generators) {
public void sortGenerators() {
final Context context = this.mContext;

if (this.mActiveGenerators == null) {
this.mActiveGenerators = Generators.getInstance(this.mContext).activeGenerators();
}

final DataPointsAdapter me = this;

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>>() {
Collections.sort(me.mActiveGenerators, new Comparator<Class<? extends Generator>>() {
@Override
public int compare(Class<? extends Generator> one, Class<? extends Generator> two) {
long oneUpdated = 0;
Expand Down Expand Up @@ -133,22 +152,13 @@ public int compare(Class<? extends Generator> one, Class<? extends Generator> tw
return 0;
}
});
} 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());
}
});
}
}

public int getItemViewType (int position) {
List<Class<? extends Generator>> activeGenerators = Generators.getInstance(this.mContext).activeGenerators();

this.sortGenerators(this.mContext, activeGenerators);
this.getGenerators(this.mContext);

Class<? extends Generator> generatorClass = activeGenerators.get(position);
Class<? extends Generator> generatorClass = this.mActiveGenerators.get(position);

return generatorClass.hashCode();
}
Expand Down
Expand Up @@ -11,6 +11,7 @@
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -91,6 +92,8 @@ public class Battery extends Generator {

private SQLiteDatabase mDatabase = null;

private long mLastTimestamp = 0;

public static Battery getInstance(Context context) {
if (Battery.sInstance == null) {
Battery.sInstance = new Battery(context.getApplicationContext());
Expand All @@ -110,11 +113,13 @@ public static void start(final Context context) {
private void startGenerator() {
final Battery me = this;

final long now = System.currentTimeMillis();

me.mLastTimestamp = now;

this.mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, Intent intent) {
long now = System.currentTimeMillis();

ContentValues values = new ContentValues();
values.put(Battery.HISTORY_OBSERVED, now);

Expand Down Expand Up @@ -271,6 +276,8 @@ public static void bindViewHolder(DataPointViewHolder holder) {

Cursor c = generator.mDatabase.query(Battery.TABLE_HISTORY, null, where, args, null, null, Battery.HISTORY_OBSERVED + " DESC");

Log.e("PDK", "BATTERY COUNT: " + c.getCount());

View cardContent = holder.itemView.findViewById(R.id.card_content);
View cardEmpty = holder.itemView.findViewById(R.id.card_empty);
TextView dateLabel = (TextView) holder.itemView.findViewById(R.id.generator_data_point_date);
Expand Down Expand Up @@ -334,9 +341,12 @@ public String getFormattedValue(float value, AxisBase axis) {

long lastLevel = -1;

int observedIndex = c.getColumnIndex(Battery.HISTORY_OBSERVED);
int levelIndex = c.getColumnIndex(Battery.HISTORY_LEVEL);

while (c.moveToNext()) {
long when = c.getLong(c.getColumnIndex(Battery.HISTORY_OBSERVED));
long level = c.getLong(c.getColumnIndex(Battery.HISTORY_LEVEL));
long when = c.getLong(observedIndex);
long level = c.getLong(levelIndex);

if (level != lastLevel) {
values.add(0, new Entry(when, level));
Expand Down Expand Up @@ -379,19 +389,19 @@ public List<Bundle> fetchPayloads() {
}

public static long latestPointGenerated(Context context) {
long timestamp = 0;

Battery me = Battery.getInstance(context);

Cursor c = me.mDatabase.query(Battery.TABLE_HISTORY, null, null, null, null, null, Battery.HISTORY_OBSERVED + " DESC");
if (me.mLastTimestamp == 0) {
Cursor c = me.mDatabase.query(Battery.TABLE_HISTORY, null, null, null, null, null, Battery.HISTORY_OBSERVED + " DESC");

if (c.moveToNext()) {
timestamp = c.getLong(c.getColumnIndex(Battery.HISTORY_OBSERVED));
}
if (c.moveToNext()) {
me.mLastTimestamp = c.getLong(c.getColumnIndex(Battery.HISTORY_OBSERVED));
}

c.close();
c.close();
}

return timestamp;
return me.mLastTimestamp;
}

public Cursor queryHistory(String[] cols, String where, String[] args, String orderBy) {
Expand Down
Expand Up @@ -65,6 +65,7 @@ public class ForegroundApplication extends Generator{
private SQLiteDatabase mDatabase = null;
private long mSampleInterval = 15000;
private AppChecker mAppChecker = null;
private long mLastTimestamp = 0;

public static ForegroundApplication getInstance(Context context) {
if (ForegroundApplication.sInstance == null) {
Expand Down Expand Up @@ -389,18 +390,18 @@ public static View fetchView(ViewGroup parent)
}

public static long latestPointGenerated(Context context) {
long timestamp = 0;

ForegroundApplication me = ForegroundApplication.getInstance(context);

Cursor c = me.mDatabase.query(ForegroundApplication.TABLE_HISTORY, null, null, null, null, null, ForegroundApplication.HISTORY_OBSERVED + " DESC");
if (me.mLastTimestamp == 0) {
Cursor c = me.mDatabase.query(ForegroundApplication.TABLE_HISTORY, null, null, null, null, null, ForegroundApplication.HISTORY_OBSERVED + " DESC");

if (c.moveToNext()) {
timestamp = c.getLong(c.getColumnIndex(ForegroundApplication.HISTORY_OBSERVED));
}
if (c.moveToNext()) {
me.mLastTimestamp = c.getLong(c.getColumnIndex(ForegroundApplication.HISTORY_OBSERVED));
}

c.close();
c.close();
}

return timestamp;
return me.mLastTimestamp;
}
}

0 comments on commit b0ccf25

Please sign in to comment.