/*
* Copyright (c) 2010 Zhihua (Dennis) Jiang
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.gwtmobile.phonegap.client;
import java.util.Date;
import com.google.gwt.core.client.JavaScriptObject;
public class Compass {
public static native void getCurrentHeading(Callback callback) /*-{
$wnd.navigator.compass.getCurrentHeading(function(heading) {
callback.@com.gwtmobile.phonegap.client.Compass.Callback::onSuccess(Lcom/gwtmobile/phonegap/client/Compass$CompassHeading;)(heading);
}, function() {
callback.@com.gwtmobile.phonegap.client.Compass.Callback::onError()();
});
}-*/;
public static String watchHeading(Callback callback) {
return watchHeading(callback, null);
}
public final static native String watchHeading(Callback callback, Options options) /*-{
var id = $wnd.navigator.compass.watchHeading(function(heading) {
callback.@com.gwtmobile.phonegap.client.Compass.Callback::onSuccess(Lcom/gwtmobile/phonegap/client/Compass$CompassHeading;)(heading);
}, function() {
callback.@com.gwtmobile.phonegap.client.Compass.Callback::onError()();
}, options);
return id;
}-*/;
public static native void clearWatch(String watchId) /*-{
$wnd.navigator.compass.clearWatch(watchId);
}-*/;
public interface Callback {
void onSuccess(CompassHeading heading);
void onError();
}
public static class Options extends JavaScriptObject {
protected Options() {};
public static Options newInstance() {
return (Options) JavaScriptObject.createObject();
}
public final native Options frequency(int f) /*-{
this.frequency = f;
return this;
}-*/;
}
public static class CompassHeading extends JavaScriptObject {
protected CompassHeading() {}
public native final double getMagneticHeading() /*-{
return this.magneticHeading;
}-*/;
public native final double getTrueHeading() /*-{
return this.trueHeading;
}-*/;
public native final double getHeadingAccuracy() /*-{
return this.headingAccuracy;
}-*/;
public final Date getTimestamp() {
return new Date((long) getTimestampNative());
}
private native final double getTimestampNative() /*-{
return this.timestamp;
}-*/;
}
}