Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright 2010 Daniel Kurka
 * 
 * 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.googlecode.gwtphonegap.client.geolocation.browser;

import com.google.gwt.core.client.Callback;
import com.google.gwt.geolocation.client.Geolocation.PositionOptions;
import com.google.gwt.user.client.Timer;
import com.googlecode.gwtphonegap.client.geolocation.Geolocation;
import com.googlecode.gwtphonegap.client.geolocation.GeolocationCallback;
import com.googlecode.gwtphonegap.client.geolocation.GeolocationOptions;
import com.googlecode.gwtphonegap.client.geolocation.GeolocationWatcher;
import com.googlecode.gwtphonegap.client.geolocation.PositionError;

public class GeolocationBrowserEmptyImpl implements Geolocation {

	private com.google.gwt.geolocation.client.Geolocation gwtGeoLocation;

	public GeolocationBrowserEmptyImpl() {
		gwtGeoLocation = com.google.gwt.geolocation.client.Geolocation.getIfSupported();

	}

	@Override
	public void getCurrentPosition(GeolocationCallback callback) {
		getCurrentPosition(callback, null);
	}

	@Override
	public void getCurrentPosition(final GeolocationCallback callback, GeolocationOptions options) {
		if (gwtGeoLocation == null) {
			callback.onFailure(new PostionErrorJavaImpl(PositionError.PERMISSION_DENIED, ""));
		} else {
			gwtGeoLocation.getCurrentPosition(new Callback<com.google.gwt.geolocation.client.Position, com.google.gwt.geolocation.client.PositionError>() {

				@Override
				public void onSuccess(com.google.gwt.geolocation.client.Position result) {
					PositionBrowserImpl positionBrowserImpl = createPosition(result);
					callback.onSuccess(positionBrowserImpl);

				}

				@Override
				public void onFailure(com.google.gwt.geolocation.client.PositionError reason) {

					callback.onFailure(new PostionErrorJavaImpl(reason.getCode(), reason.getMessage()));

				}
			});
		}

	}

	@Override
	public GeolocationWatcher watchPosition(GeolocationOptions options, final GeolocationCallback callback) {
		if (gwtGeoLocation == null) {
			return new GeolocationWatcherGwtTimerImpl(options, callback);
		} else {

			com.google.gwt.geolocation.client.Geolocation.PositionOptions opt = new PositionOptions();
			opt.setHighAccuracyEnabled(true);
			opt.setMaximumAge(options.getMaximumAge());
			opt.setTimeout(options.getTimeout());
			int watchPosition = fixGwtGeoLocation(new Callback<com.google.gwt.geolocation.client.Position, com.google.gwt.geolocation.client.PositionError>() {

				@Override
				public void onSuccess(com.google.gwt.geolocation.client.Position result) {
					PositionBrowserImpl positionBrowserImpl = createPosition(result);
					callback.onSuccess(positionBrowserImpl);

				}

				@Override
				public void onFailure(com.google.gwt.geolocation.client.PositionError reason) {

					callback.onFailure(new PostionErrorJavaImpl(reason.getCode(), reason.getMessage()));

				}
			}, opt);

			return new GwtLocationWatcher(watchPosition);
		}

	}

	/**
	 * See issue
	 * http://code.google.com/p/google-web-toolkit/issues/detail?id=6834
	 */
	// TODO remove this once gwt fixes the bug
	private native int fixGwtGeoLocation(Callback<com.google.gwt.geolocation.client.Position, com.google.gwt.geolocation.client.PositionError> callback, PositionOptions options) /*-{
		var opt = @com.google.gwt.geolocation.client.Geolocation::toJso(*)(options);

		var success = $entry(function(pos) {
			@com.google.gwt.geolocation.client.Geolocation::handleSuccess(*)(callback, pos);
		});

		var failure = $entry(function(err) {
			@com.google.gwt.geolocation.client.Geolocation::handleFailure(*)(callback, err.code, err.message);
		});

		var id = -1;
		if (@com.google.gwt.geolocation.client.Geolocation::isSupported()) {
			id = $wnd.navigator.geolocation
					.watchPosition(success, failure, opt);
		}
		return id;
	}-*/;

	@Override
	public void clearWatch(GeolocationWatcher watcher) {
		if ((watcher instanceof GeolocationWatcherGwtTimerImpl)) {
			GeolocationWatcherGwtTimerImpl timerImpl = (GeolocationWatcherGwtTimerImpl) watcher;
			timerImpl.cancel();

		} else {
			if (watcher instanceof GwtLocationWatcher) {
				GwtLocationWatcher gwtLocationWatcher = (GwtLocationWatcher) watcher;
				gwtGeoLocation.clearWatch(gwtLocationWatcher.getId());

			} else {
				throw new IllegalArgumentException();
			}
		}

	}

	/**
	 * @param result
	 * @return
	 */
	private PositionBrowserImpl createPosition(com.google.gwt.geolocation.client.Position result) {
		CoordinatesBrowserImpl co = new CoordinatesBrowserImpl();
		co.setAltitude(result.getCoordinates().getAltitude() != null ? result.getCoordinates().getAltitude() : 0);
		co.setAltitudeAccuracy(result.getCoordinates().getAltitudeAccuracy() != null ? result.getCoordinates().getAltitudeAccuracy() : 0);
		co.setHeading(result.getCoordinates().getHeading() != null ? result.getCoordinates().getHeading() : 0);
		co.setAccuracy(result.getCoordinates().getAccuracy());
		co.setLatidue(result.getCoordinates().getLatitude());
		co.setLongitude(result.getCoordinates().getLongitude());
		co.setSpeed(result.getCoordinates().getSpeed() != null ? result.getCoordinates().getSpeed() : 0);
		PositionBrowserImpl positionBrowserImpl = new PositionBrowserImpl(co, Math.round(result.getTimestamp()));
		return positionBrowserImpl;
	}

	private class GwtLocationWatcher implements GeolocationWatcher {
		private final int id;

		public GwtLocationWatcher(int id) {
			this.id = id;

		}

		public int getId() {
			return id;
		}
	}

	private class GeolocationWatcherGwtTimerImpl extends Timer implements GeolocationWatcher {

		private final GeolocationCallback callback;
		private final GeolocationOptions options;

		public GeolocationWatcherGwtTimerImpl(GeolocationOptions options, GeolocationCallback callback) {

			this.callback = callback;
			this.options = options;
			schedule((int) options.getFrequency());
		}

		@Override
		public void run() {

			schedule((int) options.getFrequency());

			callback.onFailure(new PostionErrorJavaImpl(PositionError.PERMISSION_DENIED, ""));

		}

	}

}

Commits for litesoft/trunk/GWT_Sandbox/SampleHello/src/com/googlecode/gwtphonegap/client/geolocation/browser/GeolocationBrowserEmptyImpl.java

Diff revisions: vs.
Revision Author Commited Message
666 GeorgeS picture GeorgeS Sun 13 May, 2012 18:11:48 +0000