Android Open Source - latrobe-datacapture-dir Data Store Service






From Project

Back to project page latrobe-datacapture-dir.

License

The source code is released under:

MIT License

If you think the Android project latrobe-datacapture-dir 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 com.example.DataCaptureApp.services;
//from   w  ww  .j a  v a 2  s .c o  m
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.DataCaptureApp.services.DataDbContract.DataDb;
import com.example.DataCaptureApp.data.DataService;
import com.example.DataCaptureApp.utils.SerialisationUtils;
import com.example.DataCaptureApp.data.*;

/**
 * Created by Tom on 21/09/2014.
 */
public class DataStoreService extends DataService
{
    public static final String CONFIG_TIMESTAMP_FIELD = "timestamp";
    public static final String CONFIG_SESSION_FIELD = "session";
    private DataDbHelper mHelper;
    private SQLiteDatabase mDatabase;

    private String mTimestampField;
    private String mSessionField;

    @Override
    protected void doStart()
    {
        mHelper = new DataDbHelper(this);
        mDatabase = mHelper.getWritableDatabase();
        mTimestampField = mConfig.get(CONFIG_TIMESTAMP_FIELD);
        mSessionField = mConfig.get(CONFIG_SESSION_FIELD);
        changeState(State.STARTED);
    }

    @Override
    protected void doStop()
    {
        // Nothing
    }

    @Override
    protected boolean isValidConfig(Data config)
    {
        boolean timestampFieldExists = config.contains(CONFIG_TIMESTAMP_FIELD, String.class);
        boolean sessionFieldExists = config.contains(CONFIG_SESSION_FIELD, String.class);
        return timestampFieldExists && sessionFieldExists;
    }

    @Override
    public void onData(IDataSource source, Data data)
    {
        super.onData(source, data);
        persist(data); // Persist into database
        if(mDataListener != null)
            mDataListener.onData(this, data); // Pass on data object unchanged
    }

    public void persist(Data data)
    {
        long timestamp = data.get(mTimestampField);
        String session = data.get(mSessionField);
        byte[] bytes = SerialisationUtils.serialise(data);
        if(bytes == null)
            logd("Null data bytes!");
        ContentValues values = new ContentValues();
        values.put(DataDb.COLUMN_NAME_TIMESTAMP, timestamp);
        values.put(DataDb.COLUMN_NAME_DATA, bytes);
        long row = mDatabase.insert(DataDb.TABLE_NAME, null, values);
    }

    public Data[] retrieve(String sessionId, long startTimestamp, long endTimestamp)
    {
        String selection = DataDb.COLUMN_NAME_TIMESTAMP + " >= " + startTimestamp +
                " AND " + DataDb.COLUMN_NAME_TIMESTAMP + " <= " + endTimestamp +
                " AND " + DataDb.COLUMN_NAME_SESSION + " = '" + sessionId + "'";
        Cursor c = mDatabase.query(DataDb.TABLE_NAME, null, selection, null, null, null, null);
        c.moveToFirst();
        int count = c.getCount();
        Data[] dataArr = new Data[count];
        for(int i = 0; i < count; ++i)
        {
            byte[] bytes = c.getBlob(1);
            dataArr[i] = (Data)SerialisationUtils.deserialise(bytes);
            c.move(1);
        }
        return dataArr;
    }

    public Data[] retrieve(String sessionId, long endTimestamp)
    {
        return retrieve(sessionId, 0, endTimestamp);
    }

    public void delete(String sessionId, long startTimestamp, long endTimestamp)
    {
        String selection = DataDb.COLUMN_NAME_TIMESTAMP + " >= " + startTimestamp +
                " AND " + DataDb.COLUMN_NAME_TIMESTAMP + " <= " + endTimestamp +
                " AND " + DataDb.COLUMN_NAME_SESSION + " = '" + sessionId + "'";
        int rows = mDatabase.delete(DataDb.TABLE_NAME, selection, null);
    }

    public void delete(String sessionId, long endTimestamp)
    {
        delete(sessionId, 0, endTimestamp);
    }
}




Java Source Code List

com.example.DataCaptureApp.AdvConfigActivity.java
com.example.DataCaptureApp.ConfigActivity.java
com.example.DataCaptureApp.MasterActivity.java
com.example.DataCaptureApp.MasterService.java
com.example.DataCaptureApp.SlaveActivity.java
com.example.DataCaptureApp.SlaveService.java
com.example.DataCaptureApp.data.DataServiceConnection.java
com.example.DataCaptureApp.data.DataService.java
com.example.DataCaptureApp.data.DataTransform.java
com.example.DataCaptureApp.data.Data.java
com.example.DataCaptureApp.data.Event.java
com.example.DataCaptureApp.data.FailedInitialisationException.java
com.example.DataCaptureApp.data.IDataEventListener.java
com.example.DataCaptureApp.data.IDataListener.java
com.example.DataCaptureApp.data.IDataSource.java
com.example.DataCaptureApp.data.IDataTransform.java
com.example.DataCaptureApp.data.IEventListener.java
com.example.DataCaptureApp.data.IEventSource.java
com.example.DataCaptureApp.services.BluetoothConnectivityService.java
com.example.DataCaptureApp.services.BluetoothThread.java
com.example.DataCaptureApp.services.DataDbContract.java
com.example.DataCaptureApp.services.DataDbHelper.java
com.example.DataCaptureApp.services.DataStoreService.java
com.example.DataCaptureApp.services.HttpThread.java
com.example.DataCaptureApp.services.RemoteConnectivityService.java
com.example.DataCaptureApp.services.SensorSampleService.java
com.example.DataCaptureApp.services.SensorSampler.java
com.example.DataCaptureApp.testing.BluetoothActivity.java
com.example.DataCaptureApp.testing.DataStoreActivity.java
com.example.DataCaptureApp.testing.DataTester.java
com.example.DataCaptureApp.testing.IServiceListener.java
com.example.DataCaptureApp.testing.MainService.java
com.example.DataCaptureApp.testing.MasterTestActivity.java
com.example.DataCaptureApp.testing.RandomService.java
com.example.DataCaptureApp.testing.RemoteActivity.java
com.example.DataCaptureApp.testing.SensorSampleActivity.java
com.example.DataCaptureApp.testing.ServiceTestActivity.java
com.example.DataCaptureApp.testing.SlaveTestActivity.java
com.example.DataCaptureApp.testing.TestActivity.java
com.example.DataCaptureApp.transforms.AggregatorDataTransform.java
com.example.DataCaptureApp.transforms.ArithmeticDataTransform.java
com.example.DataCaptureApp.transforms.ArrayCollectDataTransform.java
com.example.DataCaptureApp.transforms.ArraySplitDataTransform.java
com.example.DataCaptureApp.transforms.DeserialiseDataTransform.java
com.example.DataCaptureApp.transforms.FieldCopyDataTransform.java
com.example.DataCaptureApp.transforms.FieldModifyDataTransform.java
com.example.DataCaptureApp.transforms.FieldRenameDataTransform.java
com.example.DataCaptureApp.transforms.IntervalAggregatorDataTransform.java
com.example.DataCaptureApp.transforms.PackDataTransform.java
com.example.DataCaptureApp.transforms.QuaternionDifferenceDataTransform.java
com.example.DataCaptureApp.transforms.RemoveDataTransform.java
com.example.DataCaptureApp.transforms.SetDataTransform.java
com.example.DataCaptureApp.transforms.UnpackDataTransform.java
com.example.DataCaptureApp.utils.BroadcastDataSource.java
com.example.DataCaptureApp.utils.ByteUtils.java
com.example.DataCaptureApp.utils.DataEventHandler.java
com.example.DataCaptureApp.utils.DataHandlerThread.java
com.example.DataCaptureApp.utils.JSONReader.java
com.example.DataCaptureApp.utils.Quaternion.java
com.example.DataCaptureApp.utils.SerialisationUtils.java