
package com.uwetrottmann.shopr.utils;

/**
 * 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++;
    }

    public synchronized void stopTask() {
        if (!mIsStarted) {
            return;
        }
        mIsStarted = false;
        long duration = System.currentTimeMillis() - mStartTime;
        // TODO: write to database
    }

}

@@ -33,16 +33,172 @@
 utils;%0A%0A
+import android.content.ContentValues;%0Aimport android.content.Context;%0Aimport android.net.Uri;%0A%0Aimport com.uwetrottmann.shopr.provider.ShoprContract.Stats;%0A%0A
 /**%0A * S
@@ -995,42 +995,202 @@
     
-public synchronized void stopTask(
+/**%0A     * Stops the task and writes all data to the database.%0A     * %0A     * @return The %7B@link Uri%7D pointing to the new data set.%0A     */%0A    public synchronized Uri finishTask(Context context
 ) %7B%0A
@@ -1234,16 +1234,21 @@
   return
+ null
 ;%0A      
@@ -1243,32 +1243,33 @@
 null;%0A        %7D%0A
+%0A
         mIsStart
@@ -1341,24 +1341,25 @@
 mStartTime;%0A
+%0A
         // T
@@ -1361,31 +1361,432 @@
  // 
-TODO: write to database
+Write to database%0A        ContentValues statValues = new ContentValues();%0A        statValues.put(Stats.USERNAME, mUserName);%0A        statValues.put(Stats.TASK_TYPE, mIsDiversity ? %22div%22 : %22sim%22);%0A        statValues.put(Stats.CYCLE_COUNT, mCycleCount);%0A        statValues.put(Stats.DURATION, duration);%0A        final Uri inserted = context.getContentResolver().insert(Stats.CONTENT_URI, statValues);%0A%0A        return inserted;
 %0A   

Store stats when finishing task.