AccelerationProvider.java

/*
 * Copyright © 2012 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.racer.sensor;

/*
 * A wrapper class that ensures that the midlet does not crash if gestures are
 * not supported by the device.
 */
public abstract class AccelerationProvider {

    private Listener listener;

    /**
     * Callback method for subclasses
     *
     * @param ax X acceleration
     * @param ay Y acceleration
     * @param az Z acceleration
     */
    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();

    /**
     *
     * @param listener Acceleration event listener
     * @return Acceleration provider implementation that provides acceleration
     * events.
     */
    public static AccelerationProvider getProvider(Listener listener) {
        AccelerationProvider provider = null;
        try {
            Class.forName("javax.microedition.sensor.SensorConnection");
            Class c = Class.forName("com.nokia.example.racer.sensor."
                + "AccelerationProviderImpl");
            provider = (AccelerationProvider) c.newInstance();
            provider.listener = listener;
        }
        catch (Exception e) {
        }
        return provider;
    }

    public interface Listener {

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