Android Open Source - latrobe-datacapture-dir Remote Connectivity 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  w  w.j  a  v a  2 s .  c  o  m
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.example.DataCaptureApp.utils.DataEventHandler;
import com.example.DataCaptureApp.data.DataService;
import com.example.DataCaptureApp.data.FailedInitialisationException;
import com.example.DataCaptureApp.data.*;

/**
 * Created by Tom on 8/10/2014.
 */
public class RemoteConnectivityService extends DataService
{
    public static final int TIMEOUT = 15000;
    public static final int MAX_LEN = 1024;
    public static final String QUERY_PATH = "api/proclaim";
    public static final String SUBMIT_PATH = "api/session";
    public static final String KEY_SERVER_NAME = "serverName";
    public static final String KEY_SERVER_VERSION = "version";
    public static final String KEY_SERVER_HANDLES = "handles";
    public static final String KEY_RESPONSE_STATUS = "status";
    public static final String KEY_RESPONSE_MESSAGE = "message";
    public static final String KEY_RESPONSE_DATA = "data";

    public static final String CONFIG_URL = "url";
    public static final String CONFIG_ID_KEY = "idKey";
    public static final String CONFIG_HANDLE_TYPE = "handleType";

    private String mUrl;
    private String mIdKey;
    private String mHandleType;

    @Override
    public boolean isValidConfig(Data config)
    {
        boolean urlExists = config.contains(CONFIG_URL, String.class);
        boolean idKeyExists = config.contains(CONFIG_ID_KEY, String.class);
        boolean handleTypeExists = config.contains(CONFIG_HANDLE_TYPE, String.class);
        if(!(urlExists && idKeyExists && handleTypeExists))
            return false;
        return true;
    }

    @Override
    protected void doStart() throws FailedInitialisationException
    {
        // Check network connectivity
        ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null || !networkInfo.isConnected())
        {
            throw new FailedInitialisationException("Network connectivity not available!");
        }
        // Assign url
        mUrl = mConfig.get(CONFIG_URL);
        mIdKey = mConfig.get(CONFIG_ID_KEY);
        mHandleType = mConfig.get(CONFIG_HANDLE_TYPE);
        testConnection();
    }

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

    @Override
    public void onData(IDataSource source, Data data)
    {
        super.onData(source, data);
        if(source == this)
        {
            // Service should not receive data from HTTP thread...
        }
        else
        {
            // Handle submitting data to remote
            submitData(data);
        }
    }

    @Override
    public void onEvent(IEventSource source, Event event, Object obj)
    {
        if(source == this && mState != State.STOPPING && mState != State.FAILED)
        {
            if(event == Event.FAILED)
            {
                logd("HttpThread failed: " + (obj == null ? "Unknown" : obj.toString()));
                // Handle fail event from a HttpThread (obj is error message)
                failed((String)obj);
            }
            else if(event == Event.OK)
            {
                String[] arr = (String[])obj;
                String respJson = arr[0];
                logd("HttpThread ok [" + respJson + "]");
                Data response = Data.fromJson(respJson);
                if(arr[1] != null)
                {
                    Data submitData = Data.fromJson(arr[1]);
                    response.set(KEY_RESPONSE_DATA, submitData);
                }
                handleResponse(response);
            }
        }
    }

    private void testConnection()
    {
        String url = mUrl + '/' + QUERY_PATH;
        logd("Testing connection: " + url);
        DataEventHandler threadHandler = new DataEventHandler(this, this, this, this.getMainLooper());
        HttpThread thread = new HttpThread(url, null, threadHandler);
        thread.start();
        // Wait for callback to onEvent with response or error
    }

    private void handleResponse(Data resp)
    {
        if(mState == State.STARTING)
        {
            // Check connection test response
            String serverName = resp.get(KEY_SERVER_NAME);
            String version = resp.get(KEY_SERVER_VERSION).toString();
            Object[] handles = resp.get(KEY_SERVER_HANDLES);
            if(serverName == null || version == null || handles == null)
            {
                failed("Remote connection cannot be established!");
            }
            else
            {
                String debug = "";
                for (Object handle : handles)
                    debug += handle + " ";
                logd("ServerName: " + serverName + " | Verson: " + version + " | Handles: " + debug);
                boolean matched = false;
                for (Object handle : handles)
                {
                    if (handle.toString().equals(mHandleType))
                    {
                        changeState(State.STARTED);
                        matched = true;
                        break;
                    }
                }
                if (!matched)
                {
                    failed("Remote server cannot handle specified handle type! (" + mHandleType + ")");
                }
            }
        }
        else if(mState == State.STARTED)
        {
            // Check status and message
            String status = resp.get(KEY_RESPONSE_STATUS);
            String message = resp.get(KEY_RESPONSE_MESSAGE);
            mDataListener.onData(this, resp);
        }
    }

    private void submitData(Data data)
    {
        // To complete
        String json = data.toJson();
        String url = mUrl + '/' + SUBMIT_PATH + '/' + data.get(mIdKey);
        DataEventHandler threadHandler = new DataEventHandler(this, this, this, this.getMainLooper());
        HttpThread thread = new HttpThread(url, json, threadHandler);
        thread.start();
        // Wait for callback to onData with response, or onEvent with error
    }


}




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