package hu.vmiklos.plees_tracker;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity
{
    private Date mStart = null;
    private Date mStop = null;

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

    @Override protected void onStart()
    {
        super.onStart();
        Log.d("plees", "onStart");
    }

    @Override protected void onStop()
    {
        super.onStop();
        Log.d("plees", "onStop");
    }

    public void startStop(View v)
    {
        if (mStart != null && mStop == null)
        {
            mStop = Calendar.getInstance().getTime();
        }
        else
        {
            mStart = Calendar.getInstance().getTime();
            mStop = null;
        }
        updateState();
    }

    private void updateState()
    {
        TextView state = (TextView)findViewById(R.id.state);
        Button startStop = (Button)findViewById(R.id.startStop);
        SimpleDateFormat sdf =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        if (mStart != null && mStop != null)
        {
            long durationMS = mStop.getTime() - mStart.getTime();
            String duration = formatDuration(durationMS / 1000);
            state.setText("Started on " + sdf.format(mStart) + ", stopped on " +
                          sdf.format(mStop) + ", slept for " + duration + ".");
            startStop.setText("Start again");
        }
        else if (mStart != null)
        {
            state.setText("Started on " + sdf.format(mStart) + ", tracking.");
            startStop.setText("Stop");
        }
        else
        {
            state.setText("Press start to begin tracking.");
            startStop.setText("Start");
        }
    }

    private static String formatDuration(long seconds)
    {
        return String.format("%d:%02d:%02d", seconds / 3600,
                             (seconds % 3600) / 60, seconds % 60);
    }
}

@@ -614,36 +614,837 @@
 n);%0A        
-updateState(
+if (savedInstanceState != null)%0A        %7B%0A            long start = savedInstanceState.getLong(%22mStart%22);%0A            if (start != 0)%0A            %7B%0A                mStart = new Date(start);%0A            %7D%0A            long stop = savedInstanceState.getLong(%22mStop%22);%0A            if (stop != 0)%0A            %7B%0A                mStop = new Date(stop);%0A            %7D%0A        %7D%0A        updateState();%0A    %7D%0A%0A    @Override protected void onSaveInstanceState(Bundle outState)%0A    %7B%0A        long start = 0;%0A        if (mStart != null)%0A        %7B%0A            start = mStart.getTime();%0A        %7D%0A        outState.putLong(%22mStart%22, start);%0A        long stop = 0;%0A        if (mStop != null)%0A        %7B%0A            stop = mStop.getTime();%0A        %7D%0A        outState.putLong(%22mStop%22, stop);%0A        super.onSaveInstanceState(outState
 );%0A    %7D%0A%0A  

save start/stop state