Android Open Source - activity-tracker Workout Data Source






From Project

Back to project page activity-tracker.

License

The source code is released under:

MIT License

If you think the Android project activity-tracker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.hopto.mjancola.utility;
/*from   w  w w .java2s.  c  o  m*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class WorkoutDataSource
{

    // Database fields
    private SQLiteDatabase database;
    private MySQLiteHelper dbHelper;
    private String[] allColumns = { MySQLiteHelper.COLUMN_WORKOUT_ID,
                                    MySQLiteHelper.COLUMN_WORKOUT_TYPE,
                                    MySQLiteHelper.COLUMN_WORKOUT_START_MS,
                                    MySQLiteHelper.COLUMN_WORKOUT_DISTANCE_M,
                                    MySQLiteHelper.COLUMN_WORKOUT_DURATION_MS,
                                    MySQLiteHelper.COLUMN_WORKOUT_AVG_SPEED,
                                    MySQLiteHelper.COLUMN_WORKOUT_START_REASON,
                                    MySQLiteHelper.COLUMN_WORKOUT_END_REASON
    };

    public WorkoutDataSource( Context context )
    {
        dbHelper = new MySQLiteHelper( context );
    }

    public void open() throws SQLException
    {
        database = dbHelper.getWritableDatabase();
    }

    public void close()
    {
        dbHelper.close();
    }

    public WorkoutDAO createWorkout( String type, long start, long distance, long duration, long avgSpeed, String startReason, String endReason )
    {
        if (( type == null ))
        {
            return null;
        }
        else
        {
            ContentValues values = new ContentValues();
            values.put( MySQLiteHelper.COLUMN_WORKOUT_TYPE, type);
            values.put(MySQLiteHelper.COLUMN_WORKOUT_START_MS, start);
            values.put(MySQLiteHelper.COLUMN_WORKOUT_DISTANCE_M, distance);
            values.put(MySQLiteHelper.COLUMN_WORKOUT_DURATION_MS, duration);
            values.put( MySQLiteHelper.COLUMN_WORKOUT_AVG_SPEED, avgSpeed);
            values.put(MySQLiteHelper.COLUMN_WORKOUT_START_REASON, startReason);
            values.put(MySQLiteHelper.COLUMN_WORKOUT_END_REASON, endReason);

            long insertId = database.insert( MySQLiteHelper.TABLE_WORKOUTS, null,
                                             values );
            Cursor cursor = database.query( MySQLiteHelper.TABLE_WORKOUTS,
                                            allColumns, MySQLiteHelper.COLUMN_WORKOUT_ID + " = " + insertId, null,
                                            null, null, null );
            cursor.moveToFirst();
            WorkoutDAO workoutDAO = cursorToWorkoutDAO( cursor );
            cursor.close();
            return workoutDAO;
        }
    }

    public void deleteWorkout( WorkoutDAO workoutDAO )
    {
        long id = workoutDAO.getId();
        System.out.println( "Workout deleted with id: " + id );
        database.delete( MySQLiteHelper.TABLE_WORKOUTS, MySQLiteHelper.COLUMN_WORKOUT_ID
                                                     + " = " + id, null );
    }

    public List<WorkoutDAO> getAllWorkoutDAOs()
    {
        List<WorkoutDAO> AllWorkoutDAOs = new ArrayList<WorkoutDAO>();


        Cursor cursor = database.query( MySQLiteHelper.TABLE_WORKOUTS,
                                        allColumns, null, null, null, null, null );

        cursor.moveToLast();
        while ( !cursor.isBeforeFirst() )
        {
            WorkoutDAO workoutDAO = cursorToWorkoutDAO( cursor );
            AllWorkoutDAOs.add( workoutDAO );
            cursor.moveToPrevious();
        }
        // Make sure to close the cursor
        cursor.close();

        //AllWorkoutDAOs.re
        return AllWorkoutDAOs;
    }

    // TODO - hardcoded order
//          + COLUMN_WORKOUT_ID + " integer primary key autoincrement, "
//            + COLUMN_WORKOUT_TYPE + " text not null, "
//            + COLUMN_WORKOUT_START_MS + " integer not null, "
//            + COLUMN_WORKOUT_DURATION_MS + " integer not null, "
//            + COLUMN_WORKOUT_DISTANCE_M + " integer not null, "
//            + COLUMN_WORKOUT_AVG_SPEED + " integer not null, "
//            + COLUMN_WORKOUT_START_REASON + " text, "
//            + COLUMN_WORKOUT_END_REASON + " text);";
    private WorkoutDAO cursorToWorkoutDAO( Cursor cursor )
    {
        WorkoutDAO workoutDAO = new WorkoutDAO(cursor.getLong(0),
                                               cursor.getString(1),
                                               cursor.getLong(2),
                                               cursor.getLong(3),
                                               cursor.getLong(4),
                                               cursor.getLong(5),
                                               cursor.getString(6),
                                               cursor.getString(7));

        return workoutDAO;
    }

    public static WorkoutDataSource create( Context context )
    {
        WorkoutDataSource workoutDataSource = new WorkoutDataSource(context);
        workoutDataSource.open();
        return workoutDataSource;
    }
}




Java Source Code List

org.hopto.mjancola.ActivityTrackerApplication.java
org.hopto.mjancola.activity.ActivityRecognitionActivity.java
org.hopto.mjancola.activity.ActivityTracker.java
org.hopto.mjancola.activity.DemoDetailsList.java
org.hopto.mjancola.activity.DemoDetails.java
org.hopto.mjancola.activity.EditWorkoutDAOActivity.java
org.hopto.mjancola.activity.FeatureView.java
org.hopto.mjancola.activity.LegalInfoActivity.java
org.hopto.mjancola.activity.ListWorkoutsActivity.java
org.hopto.mjancola.activity.MyLocationDemoActivity.java
org.hopto.mjancola.activity.SpeedActivity.java
org.hopto.mjancola.activity.UserSettingsActivity.java
org.hopto.mjancola.adapter.WorkoutDAOAdapter.java
org.hopto.mjancola.fragment.EditWorkoutDAOFragment.java
org.hopto.mjancola.model.DataPoint.java
org.hopto.mjancola.model.GenericMovement.java
org.hopto.mjancola.model.GenericWorkout.java
org.hopto.mjancola.model.LearningMode.java
org.hopto.mjancola.model.MyMovement.java
org.hopto.mjancola.model.WorkoutTotal.java
org.hopto.mjancola.model.WorkoutType.java
org.hopto.mjancola.model.Workout.java
org.hopto.mjancola.service.ActivityRecognitionService.java
org.hopto.mjancola.service.MovementListenerService.java
org.hopto.mjancola.service.MovementTrackerService.java
org.hopto.mjancola.utility.Converter.java
org.hopto.mjancola.utility.MySQLiteHelper.java
org.hopto.mjancola.utility.SettingsHelper.java
org.hopto.mjancola.utility.WorkoutDAO.java
org.hopto.mjancola.utility.WorkoutDataSource.java