erisu commented on code in PR #78: URL: https://github.com/apache/cordova-plugin-device-orientation/pull/78#discussion_r957392864
########## src/android/CompassListener.java: ########## @@ -38,259 +40,306 @@ Licensed to the Apache Software Foundation (ASF) under one import android.os.Handler; import android.os.Looper; +import android.util.Log; // MG + /** * This class listens to the compass sensor and stores the latest heading value. */ public class CompassListener extends CordovaPlugin implements SensorEventListener { - public static int STOPPED = 0; - public static int STARTING = 1; - public static int RUNNING = 2; - public static int ERROR_FAILED_TO_START = 3; - - public long TIMEOUT = 30000; // Timeout in msec to shut off listener - - int status; // status of listener - float heading; // most recent heading value - long timeStamp; // time of most recent value - long lastAccessTime; // time the value was last retrieved - int accuracy; // accuracy of the sensor - - private SensorManager sensorManager;// Sensor manager - Sensor mSensor; // Compass sensor returned by sensor manager - - private CallbackContext callbackContext; - - /** - * Constructor. - */ - public CompassListener() { - this.heading = 0; - this.timeStamp = 0; - this.setStatus(CompassListener.STOPPED); - } - - /** - * Sets the context of the Command. This can then be used to do things like - * get file paths associated with the Activity. - * - * @param cordova The context of the main Activity. - * @param webView The CordovaWebView Cordova is running in. - */ - public void initialize(CordovaInterface cordova, CordovaWebView webView) { - super.initialize(cordova, webView); - this.sensorManager = (SensorManager) cordova.getActivity().getSystemService(Context.SENSOR_SERVICE); - } - - /** - * Executes the request and returns PluginResult. - * - * @param action The action to execute. - * @param args JSONArry of arguments for the plugin. - * @param callbackS=Context The callback id used when calling back into JavaScript. - * @return True if the action was valid. - * @throws JSONException - */ - public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { - if (action.equals("start")) { - this.start(); - } - else if (action.equals("stop")) { - this.stop(); + public static int STOPPED = 0; + public static int STARTING = 1; + public static int RUNNING = 2; + public static int ERROR_FAILED_TO_START = 3; + + public long TIMEOUT = 30000; // Timeout in msec to shut off listener + + int status; // status of listener + float heading; // most recent heading value + long timeStamp; // time of most recent value + long lastAccessTime; // time the value was last retrieved + int accuracy; // accuracy of the sensor + + private SensorManager sensorManager;// Sensor manager + Sensor mSensor; // Compass sensor returned by sensor manager + + Sensor accelerometer; + Sensor magnetometer; + + private CallbackContext callbackContext; + + /** + * Constructor. + */ + public CompassListener() { + this.heading = 0; + this.timeStamp = 0; + this.setStatus(CompassListener.STOPPED); + } + + /** + * Sets the context of the Command. This can then be used to do things like + * get file paths associated with the Activity. + * + * @param cordova The context of the main Activity. + * @param webView The CordovaWebView Cordova is running in. + */ + public void initialize(CordovaInterface cordova, CordovaWebView webView) { + super.initialize(cordova, webView); + this.sensorManager = (SensorManager) cordova.getActivity().getSystemService(Context.SENSOR_SERVICE); + } + + /** + * Executes the request and returns PluginResult. + * + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackS=Context The callback id used when calling back into + * JavaScript. + * @return True if the action was valid. + * @throws JSONException + */ + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + if (action.equals("start")) { + this.start(); + } else if (action.equals("stop")) { + this.stop(); + } else if (action.equals("getStatus")) { + int i = this.getStatus(); + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, i)); + } else if (action.equals("getHeading")) { + // If not running, then this is an async call, so don't worry about waiting + if (this.status != CompassListener.RUNNING) { + int r = this.start(); + if (r == CompassListener.ERROR_FAILED_TO_START) { + callbackContext.sendPluginResult( + new PluginResult(PluginResult.Status.IO_EXCEPTION, CompassListener.ERROR_FAILED_TO_START)); + return true; } - else if (action.equals("getStatus")) { - int i = this.getStatus(); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, i)); - } - else if (action.equals("getHeading")) { - // If not running, then this is an async call, so don't worry about waiting - if (this.status != CompassListener.RUNNING) { - int r = this.start(); - if (r == CompassListener.ERROR_FAILED_TO_START) { - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, CompassListener.ERROR_FAILED_TO_START)); - return true; - } - // Set a timeout callback on the main thread. - Handler handler = new Handler(Looper.getMainLooper()); - handler.postDelayed(new Runnable() { - public void run() { - CompassListener.this.timeout(); - } - }, 2000); - } - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getCompassHeading())); - } - else if (action.equals("setTimeout")) { - this.setTimeout(args.getLong(0)); - } - else if (action.equals("getTimeout")) { - long l = this.getTimeout(); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l)); - } else { - // Unsupported action - return false; - } - return true; - } - - /** - * Called when listener is to be shut down and object is being destroyed. - */ - public void onDestroy() { - this.stop(); + // Set a timeout callback on the main thread. + Handler handler = new Handler(Looper.getMainLooper()); + handler.postDelayed(new Runnable() { + public void run() { + CompassListener.this.timeout(); + } + }, 2000); + } + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getCompassHeading())); + } else if (action.equals("setTimeout")) { + this.setTimeout(args.getLong(0)); + } else if (action.equals("getTimeout")) { + long l = this.getTimeout(); + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, l)); + } else { + // Unsupported action + return false; } - - /** - * Called when app has navigated and JS listeners have been destroyed. - */ - public void onReset() { - this.stop(); - } - - //-------------------------------------------------------------------------- - // LOCAL METHODS - //-------------------------------------------------------------------------- - - /** - * Start listening for compass sensor. - * - * @return status of listener - */ - public int start() { - - // If already starting or running, then just return - if ((this.status == CompassListener.RUNNING) || (this.status == CompassListener.STARTING)) { - return this.status; - } - - // Get compass sensor from sensor manager - @SuppressWarnings("deprecation") - List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ORIENTATION); - - // If found, then register as listener - if (list != null && list.size() > 0) { - this.mSensor = list.get(0); - this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_NORMAL); - this.lastAccessTime = System.currentTimeMillis(); - this.setStatus(CompassListener.STARTING); - } - - // If error, then set status to error - else { - this.setStatus(CompassListener.ERROR_FAILED_TO_START); - } - - return this.status; + return true; + } + + /** + * Called when listener is to be shut down and object is being destroyed. + */ + public void onDestroy() { + this.stop(); + } + + /** + * Called when app has navigated and JS listeners have been destroyed. + */ + public void onReset() { + this.stop(); + } + + // -------------------------------------------------------------------------- + // LOCAL METHODS + // -------------------------------------------------------------------------- + + /** + * Start listening for compass sensor. + * + * @return status of listener + */ + public int start() { + + // If already starting or running, then just return + if ((this.status == CompassListener.RUNNING) || (this.status == CompassListener.STARTING)) { + return this.status; } - /** - * Stop listening to compass sensor. - */ - public void stop() { - if (this.status != CompassListener.STOPPED) { - this.sensorManager.unregisterListener(this); - } - this.setStatus(CompassListener.STOPPED); + // MG, use accelerometer & magnetometer Review Comment: ```suggestion // use accelerometer & magnetometer ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cordova.apache.org For additional commands, e-mail: commits-h...@cordova.apache.org