AccelerationProvider.java

/**
* Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners. 
* See LICENSE.TXT for license information.
*/

package com.nokia.example.explonoid.sensors;

/**
 * Abstract class for acceleration sensor use.
 */
public abstract class AccelerationProvider {

    private Listener listener;

    /**
     * Callback for subclasses
     * @param ax
     * @param ay
     * @param az 
     */
    protected final void dataReceived(double ax, double ay, double az) {
        if (listener != null) {
            listener.dataReceived(ax, ay, az);
        }
    }

    /**
     * Closes the sensor connection
     */
    public abstract void close();

    public static AccelerationProvider getProvider(Listener listener) {
        AccelerationProvider provider = null;
        try {
            if(sensorsSupported()) {
                Class c = Class.forName("com.nokia.example.explonoid.sensors."
                        + "AccelerationProviderImpl");
                provider = (AccelerationProvider) c.newInstance();
                provider.listener = listener;
            }
        }
        catch (Exception e) {
        }
        return provider;
    }

    public interface Listener {

        void dataReceived(double ax, double ay, double az);
    }

    public static boolean sensorsSupported() {
        try {
            Class.forName("javax.microedition.sensor.SensorConnection");
            return true;
        }
        catch (Exception e) {
            return false;
        }
    }
}