package com.platypii.baseline.views.charts;

import com.platypii.baseline.events.ChartFocusEvent;
import com.platypii.baseline.measurements.MLocation;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import java.util.Collections;
import org.greenrobot.eventbus.EventBus;

public class TimeChartTouchable extends TimeChart {

    public TimeChartTouchable(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            final long millis = (long) plot.getXinverse(0, event.getX());
            // Find nearest data point
            final MLocation closest = findClosest(millis);
            // Emit chart focus event
            EventBus.getDefault().post(new ChartFocusEvent(closest));
        }
        return true; // if the event was handled
    }

    // Avoid creating new object just to binary search
    private final MLocation touchLocation = new MLocation();
    /**
     * Performs a binary search for the nearest data point
     */
    private MLocation findClosest(long millis) {
        touchLocation.millis = millis;
        int closest_index = Collections.binarySearch(trackData, touchLocation);
        if (closest_index < 0) closest_index = -closest_index;
        if (closest_index == trackData.size()) closest_index--;
        return trackData.get(closest_index);
    }

}

@@ -176,16 +176,60 @@
 ontext;%0A
+import android.support.annotation.Nullable;%0A
 import a
@@ -1218,16 +1218,30 @@
      */%0A
+    @Nullable%0A
     priv
@@ -1277,24 +1277,85 @@
 g millis) %7B%0A
+        if (trackData != null && !trackData.isEmpty()) %7B%0A    
         touc
@@ -1381,16 +1381,20 @@
 millis;%0A
+    
         
@@ -1465,32 +1465,36 @@
 ation);%0A        
+  
+  
 if (closest_inde
@@ -1532,16 +1532,20 @@
 _index;%0A
+    
         
@@ -1600,32 +1600,36 @@
 ndex--;%0A        
+   
+ 
 return trackData
@@ -1649,16 +1649,68 @@
 index);%0A
+        %7D else %7B%0A            return null;%0A        %7D%0A
     %7D%0A%0A%7D

Fix null pointer in time chart