Android Open Source - latrobe-datacapture-dir Data 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.data;
// w  w  w  . jav  a 2 s.c  o m
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

/**
 * Created by Tom on 23/09/2014.
 */
public abstract class DataService extends Service implements IDataEventListener, IDataSource, IEventSource
{
    private static final boolean DEBUG_DATA = false;
    protected LocalBinder mBinder = new LocalBinder();

    protected IDataListener mDataListener;
    protected IEventListener mEventListener;

    protected Data mConfig;
    protected State mState = State.UNINITIALISED;

    public class LocalBinder extends Binder
    {
        public DataService getService() { return DataService.this; }
    }

    public enum State
    {
        UNINITIALISED   ,
        STARTING (Event.STARTING),
        STARTED (Event.STARTED),
        STOPPING(Event.STOPPING),
        STOPPED(Event.STOPPED),
        FAILED (Event.FAILED);

        private Event event;
        private State()
        {
            this(null);
        }
        private State(Event event)
        {
            this.event = event;
        }

        public Event event()
        {
            return event;
        }
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return mBinder;
    }

    @Override
    public void onDestroy()
    {
        logd("Destroying");
        if(mEventListener != null)
            mEventListener.onEvent(this, Event.DESTROYED, null);
    }

    public void start(Data config) throws FailedInitialisationException
    {
        try
        {
            if (!isValidConfig(config))
                throw new FailedInitialisationException("Invalid configuration supplied!");
            mConfig = config;
            changeState(State.STARTING, null);
            doStart();
        }
        catch(FailedInitialisationException e)
        {
            failed(e.getMessage());
            throw e;
        }
    }

    protected abstract void doStart() throws FailedInitialisationException;

    public void stop()
    {
        if(mState != State.STOPPING && mState != State.STOPPED)
        {
            changeState(State.STOPPING, null);
            doStop();
            changeState(State.STOPPED, null);
            stopSelf();
        }
    }

    protected abstract void doStop();

    public void failed(String reason)
    {
        if(mState != State.FAILED)
        {
            changeState(State.FAILED, reason);
            stop();
        }
    }

    @Override
    public void onData(IDataSource source, Data data)
    {
        if(DEBUG_DATA)
        {
            logd("Data received: " + data);
        }
        // Do nothing
    }

    @Override
    public void onEvent(IEventSource source, Event event, Object arg)
    {
        // Do nothing
    }

    @Override
    public void setDataListener(IDataListener listener)
    {
        mDataListener = listener;
    }

    @Override
    public void setEventListener(IEventListener listener)
    {
        mEventListener = listener;
    }

    protected boolean isValidConfig(Data config)
    {
        // By default ignore config
        return true;
    }

    protected void logd(String msg)
    {
        Log.d(this.getClass().getName(), msg);
    }

    public State getState()
    {
        return mState;
    }

    protected void changeState(State state, String msg)
    {
        mState = state;
        logd("State: " + state);
        if(mEventListener != null)
        {
            mEventListener.onEvent(this, state.event(), msg);
        }
    }

    protected void changeState(State state)
    {
        changeState(state, null);
    }
}




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