package com.amitness.photon;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.TreeMap;

public class ReceiveActivity extends AppCompatActivity {

    private TextView mTextViewLightLabel;
    private SensorManager mSensorManager;
    private SensorEventListener mEventListenerLight;
    private float lastLightValue;
    private float bgValue = -1;
    private ArrayList<Float> values = new ArrayList<>();
    private TreeMap<Long, Float> records;

    private void updateUI() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                char bit;
                if(bgValue == -1){
                    bgValue = lastLightValue;
                    Log.d("Background Intensity", String.valueOf(bgValue));
                }
                if(lastLightValue > bgValue) {
                    bit = '1';
                }
                else {
                    bit = '0';
                }
                mTextViewLightLabel.append(""+bit);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);

        records = new TreeMap<Long, Float>();

        mTextViewLightLabel = (TextView) findViewById(R.id.sensorValue);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);

        mEventListenerLight = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                lastLightValue = event.values[0];
                long timestamp = event.timestamp;
                records.put(timestamp, lastLightValue);
                Log.d("Time Stamp:", String.valueOf(timestamp));
                values.add(lastLightValue);
                Log.d("Sensor Value", String.valueOf(lastLightValue));
                updateUI();
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        };
    }

    @Override
    public void onResume() {
        super.onResume();
        mSensorManager.registerListener(mEventListenerLight, mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onStop() {
        Log.d("Read all values:", String.valueOf(values));
        Log.d("Read all values:", String.valueOf(records));
        mSensorManager.unregisterListener(mEventListenerLight);
        super.onStop();
    }

    public void notifyStart(View v) {
        Log.d("startReceive", "Receive Started");
    }
}
@@ -749,16 +749,44 @@
 records;
+%0A    private long startTime;
 %0A%0A    pr
@@ -1856,24 +1856,261 @@
 nt event) %7B%0A
+                if(bgValue == -1)%7B%0A                  //startTime = System.currentTimeMillis();%0A                    startTime = event.timestamp;%0A                    Log.d(%22Start timestamp: %22, String.valueOf(startTime));%0A                %7D%0A
             
@@ -2234,16 +2234,28 @@
 imestamp
+ - startTime
 , lastLi

Use change in timestamp as TreeMap key