
package com.uwetrottmann.shopr.utils;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;

import com.uwetrottmann.shopr.provider.ShoprContract.Stats;

/**
 * Stores data about the current task.
 */
public class Statistics {

    private static Statistics _instance;

    private long mStartTime;
    private int mCycleCount;
    private String mUserName;
    private boolean mIsDiversity;

    private boolean mIsStarted;

    private Statistics() {

    }

    public synchronized static Statistics get() {
        if (_instance == null) {
            _instance = new Statistics();
        }
        return _instance;
    }

    public synchronized void startTask(String username, boolean isDiversity) {
        mIsStarted = true;
        mUserName = username;
        mIsDiversity = isDiversity;
        mStartTime = System.currentTimeMillis();
        mCycleCount = 0;
    }

    public void incrementCycleCount() {
        mCycleCount++;
    }

    /**
     * Stops the task and writes all data to the database.
     * 
     * @return The {@link Uri} pointing to the new data set.
     */
    public synchronized Uri finishTask(Context context) {
        if (!mIsStarted) {
            return null;
        }

        mIsStarted = false;
        long duration = System.currentTimeMillis() - mStartTime;

        // Write to database
        ContentValues statValues = new ContentValues();
        statValues.put(Stats.USERNAME, mUserName);
        statValues.put(Stats.TASK_TYPE, mIsDiversity ? "div" : "sim");
        statValues.put(Stats.CYCLE_COUNT, mCycleCount);
        statValues.put(Stats.DURATION, duration);
        final Uri inserted = context.getContentResolver().insert(Stats.CONTENT_URI, statValues);

        return inserted;
    }

}

@@ -656,32 +656,156 @@
 nstance;%0A    %7D%0A%0A
+    /**%0A     * Saves the current time, user name and task type until%0A     * %7B@link #finishTask(Context)%7D is called.%0A     */%0A
     public synch
@@ -1045,22 +1045,105 @@
  %7D%0A%0A    
-public
+/**%0A     * Increases the recommendation cycle count by 1.%0A     */%0A    public synchronized
  void in
@@ -1328,16 +1328,108 @@
 data set
+ or %7B@code null%7D if%0A     *         %7B@link #startTask(String, boolean)%7D was not called before
 .%0A     *

Add documentation to stats methods.