renamed folders with lower cases and renamed user preferences to local settings

This commit is contained in:
Stanimir Karoserov
2014-05-13 17:59:30 +03:00
parent b6313517db
commit ceff7bb368
58 changed files with 227 additions and 225 deletions

23
location/Readme.md Normal file
View File

@@ -0,0 +1,23 @@
Initializing location:
```
var LocationManager = require("Location").LocationManager;
console.log('is location enabled: ' + LocationManager.isLocationEnabled());
this.locationManager = new LocationManager();
console.dump(this.locationManager.getLastKnownLocation());
this.locationManager.startLocationMonitoring(function(location) {
console.dump(location);
}, function(error) {
console.error(error);
});
```
Stopping location:
```
this.locationManager.stopLocationMonitoring();
```

2
location/index.ts Normal file
View File

@@ -0,0 +1,2 @@
declare var module, require;
module.exports = require("location/location");

View File

@@ -0,0 +1,171 @@
import types = require("location/location_types");
import appModule = require("application/application");
// merge the exports of the types module with the exports of this file
declare var exports;
require("utils/module_merge").merge(types, exports);
export class LocationManager {
// in meters
public desiredAccuracy: number;
// The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
public updateDistance: number;
// minimum time interval between location updates, in milliseconds (android only)
public minimumUpdateTime: number;
public isStarted: boolean;
private androidLocationManager: any;
private locationListener: any;
private static locationFromAndroidLocation(androidLocation: android.location.Location): types.Location {
var location = new types.Location();
location.latitude = androidLocation.getLatitude();
location.longitude = androidLocation.getLongitude();
location.altitude = androidLocation.getAltitude();
location.horizontalAccuracy = androidLocation.getAccuracy();
location.verticalAccuracy = androidLocation.getAccuracy();
location.speed = androidLocation.getSpeed();
location.direction = androidLocation.getBearing();
location.timestamp = new Date(androidLocation.getTime());
location.android = androidLocation;
//console.dump(location);
return location;
}
private static androidLocationFromLocation(location: types.Location): android.location.Location {
var androidLocation = new android.location.Location('custom');
androidLocation.setLatitude(location.latitude);
androidLocation.setLongitude(location.longitude);
if (location.altitude)
androidLocation.setAltitude(location.altitude);
if (location.speed)
androidLocation.setSpeed(float(location.speed));
if (location.direction)
androidLocation.setBearing(float(location.direction));
if (location.timestamp) {
try {
androidLocation.setTime(long(location.timestamp.getTime()));
}
catch (e) {
console.error('invalid location timestamp');
}
}
return androidLocation;
}
public static isEnabled(): boolean {
var criteria = new android.location.Criteria();
criteria.setAccuracy(1); // low ? fine ? who knows what 1 means (bug in android docs?)
var lm = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
return (lm.getBestProvider(criteria, true) != null) ? true : false;
}
public static distance(loc1: types.Location, loc2: types.Location): number {
if (!loc1.android) {
loc1.android = LocationManager.androidLocationFromLocation(loc1);
}
if (!loc2.android) {
loc2.android = LocationManager.androidLocationFromLocation(loc2);
}
return loc1.android.distanceTo(loc2.android);
}
constructor() {
// put some defaults
this.desiredAccuracy = types.Accuracy.HIGH;
this.updateDistance = 0;
this.minimumUpdateTime = 200;
this.isStarted = false;
this.androidLocationManager = appModule.android.context.getSystemService(android.content.Context.LOCATION_SERVICE);
}
////////////////////////
// monitoring
////////////////////////
public startLocationMonitoring(onLocation: (location: types.Location) => any, onError?: (error: Error) => any, options?: types.Options) {
if (!this.isStarted) {
var criteria = new android.location.Criteria();
criteria.setAccuracy((this.desiredAccuracy === types.Accuracy.HIGH) ? 1 : 2);
this.locationListener = <any>new android.location.LocationListener({
onLocationChanged: function (location: android.location.Location) {
if (this._onLocation) {
this._onLocation(LocationManager.locationFromAndroidLocation(location));
}
},
onProviderDisabled: function (provider: string) {
},
onProviderEnabled: function (provider: string) {
},
onStatusChanged: function (arg1: string, arg2: number, arg3: android.os.Bundle): void {
}
});
if (options) {
if (options.desiredAccuracy)
this.desiredAccuracy = options.desiredAccuracy;
if (options.updateDistance)
this.updateDistance = options.updateDistance;
if (options.minimumUpdateTime)
this.minimumUpdateTime = options.minimumUpdateTime;
}
this.locationListener._onLocation = onLocation;
this.locationListener._onError = onError;
try {
this.androidLocationManager.requestLocationUpdates(long(this.minimumUpdateTime), float(this.updateDistance), criteria, this.locationListener, null);
this.isStarted = true;
}
catch (e) {
if (onError) {
onError(e);
}
}
}
else if (onError) {
onError(new Error('location monitoring already started'));
}
}
public stopLocationMonitoring() {
if (this.isStarted) {
this.androidLocationManager.removeUpdates(this.locationListener);
this.isStarted = false;
}
}
////////////////////////
// other
////////////////////////
get lastKnownLocation(): types.Location {
var criteria = new android.location.Criteria();
criteria.setAccuracy((this.desiredAccuracy === types.Accuracy.HIGH) ? 1 : 2);
try {
var providers = this.androidLocationManager.getProviders(criteria, false);
var it = providers.iterator();
while (it.hasNext()) {
var element = it.next();
//console.log('found provider: ' + element);
var location = this.androidLocationManager.getLastKnownLocation(element);
if (location) {
return LocationManager.locationFromAndroidLocation(location);
}
}
}
catch (e) {
console.error(e.message);
}
return null;
}
}

100
location/location.d.ts vendored Normal file
View File

@@ -0,0 +1,100 @@
export declare enum Accuracy {
// in meters
ANY,
HIGH,
}
// For future usage
//export declare class LocationRegion {
// public latitude: number;
// public longitude: number;
// public raduis: number; // radius in meters
//}
export declare class Location {
latitude: number;
longitude: number;
altitude: number; // in meters
horizontalAccuracy: number; // in meters
verticalAccuracy: number; // in meters
speed: number; // in m/s
direction: number; // in degrees
timestamp: Date;
public android: any; // android Location
public ios: any; // iOS CLLocation
}
export declare class Options {
/**
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
*/
desiredAccuracy: number;
/**
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
*/
updateDistance: number;
/**
* Minimum time interval between location updates, in milliseconds (android only)
*/
minimumUpdateTime: number;
}
export declare class LocationManager {
/**
* Report are location services switched ON for this device (on Android) or application (iOS)
*/
static isEnabled(): boolean;
/**
* Measure distance in meters between two locations
*/
static distance(loc1: Location, loc2: Location): number;
/**
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
*/
desiredAccuracy: number;
/**
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
*/
updateDistance: number;
/**
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
*/
minimumUpdateTime: number;
/**
* True if location listener is already started. In this case all other start requests will be ignored
*/
isStarted: boolean;
// monitoring
/**
* Starts location monitoring.
*/
startLocationMonitoring(onLocation: (location: Location) => any, onError?: (error: Error) => any, options?: Options);
/**
* Stops location monitoring
*/
stopLocationMonitoring();
// other
/**
* Returns last known location from device's location services or null of no known last location
*/
lastKnownLocation: Location;
}

141
location/location.ios.ts Normal file
View File

@@ -0,0 +1,141 @@
import types = require("location/location_types");
// merge the exports of the types module with the exports of this file
declare var exports;
require("utils/module_merge").merge(types, exports);
export class LocationManager {
// in meters
// we might need some predefined values here like 'any' and 'high'
public desiredAccuracy: number;
// The minimum distance (measured in meters) a device must move horizontally before an update event is generated.
public updateDistance: number;
public isStarted: boolean;
private iosLocationManager: CoreLocation.CLLocationManager;
private listener: any;
private static locationFromCLLocation(clLocation: CoreLocation.CLLocation): types.Location {
var location = new types.Location();
location.latitude = clLocation.coordinate.latitude;
location.longitude = clLocation.coordinate.longitude;
location.altitude = clLocation.altitude;
location.horizontalAccuracy = clLocation.horizontalAccuracy;
location.verticalAccuracy = clLocation.verticalAccuracy;
location.speed = clLocation.speed;
location.direction = clLocation.course;
location.timestamp = new Date(clLocation.timestamp.timeIntervalSince1970() * 1000);
location.ios = clLocation;
//console.dump(location);
return location;
}
private static iosLocationFromLocation(location: types.Location): CoreLocation.CLLocation {
var hAccuracy = location.horizontalAccuracy ? location.horizontalAccuracy : -1;
var vAccuracy = location.verticalAccuracy ? location.verticalAccuracy : -1;
var speed = location.speed ? location.speed : -1;
var course = location.direction ? location.direction : -1;
var altitude = location.altitude ? location.altitude : -1;
var timestamp = location.timestamp ? Foundation.NSDate.dateWithTimeIntervalSince1970(location.timestamp.getTime()) : null;
var iosLocation = CoreLocation.CLLocation.initWithCoordinateAltitudeHorizontalAccuracyVerticalAccuracyCourseSpeedTimestamp(CoreLocation.CLLocationCoordinate2DMake(location.latitude, location.longitude), altitude, hAccuracy, vAccuracy, course, speed, timestamp);
return iosLocation;
}
public static isEnabled(): boolean {
if (CoreLocation.CLLocationManager.locationServicesEnabled()) {
//return CoreLocation.CLLocationManager.authorizationStatus() === CoreLocation.CLAuthorizationStatus.kCLAuthorizationStatusAuthorized;
// FIXME: issue reported https://github.com/telerik/Kimera/issues/122
return true;
}
return false;
}
public static distance(loc1: types.Location, loc2: types.Location): number {
if (!loc1.ios) {
loc1.ios = LocationManager.iosLocationFromLocation(loc1);
}
if (!loc2.ios) {
loc2.ios = LocationManager.iosLocationFromLocation(loc2);
}
return loc1.ios.distanceFromLocation(loc2.ios);
}
constructor() {
this.isStarted = false;
this.desiredAccuracy = types.Accuracy.HIGH;
this.updateDistance = -1; // kCLDistanceFilterNone
this.iosLocationManager = new CoreLocation.CLLocationManager();
}
// monitoring
public startLocationMonitoring(onLocation: (location: types.Location) => any, onError?: (error: Error) => any, options?: types.Options) {
if (!this.isStarted) {
var LocationListener = Foundation.NSObject.extends({
setupWithFunctions: function (onLocation, onError) {
this.onLocation = onLocation;
this.onError = onError;
}
}, {}).implements({
protocol: "CLLocationManagerDelegate",
implementation: {
locationManagerDidUpdateLocations: function (manager, locations) {
//console.log('location received: ' + locations.count());
for (var i = 0; i < locations.count(); i++) {
this.onLocation(LocationManager.locationFromCLLocation(locations.objectAtIndex(i)));
}
},
locationManagerDidFailWithError: function (manager, error) {
console.error('location error received ' + error.localizedDescription());
if (this.onError) {
this.onError(new Error(error.localizedDescription()));
}
}
}
});
if (options) {
if (options.desiredAccuracy)
this.desiredAccuracy = options.desiredAccuracy;
if (options.updateDistance)
this.updateDistance = options.updateDistance;
}
this.listener = new LocationListener();
this.listener.setupWithFunctions(onLocation, onError);
this.iosLocationManager.delegate = this.listener;
this.iosLocationManager.desiredAccuracy = this.desiredAccuracy;
this.iosLocationManager.distanceFilter = this.updateDistance;
this.iosLocationManager.startUpdatingLocation();
this.isStarted = true;
}
else if (onError) {
onError(new Error('location monitoring already started'));
}
}
public stopLocationMonitoring() {
if (this.isStarted) {
this.iosLocationManager.stopUpdatingLocation();
this.iosLocationManager.delegate = null;
this.listener = null;
this.isStarted = false;
}
}
// other
get lastKnownLocation(): types.Location {
var clLocation = this.iosLocationManager.location;
if (null != clLocation) {
return LocationManager.locationFromCLLocation(clLocation);
}
return null;
}
}

View File

@@ -0,0 +1,48 @@
export enum Accuracy {
// in meters
ANY = 300,
HIGH = 3,
}
export class Location {
public latitude: number;
public longitude: number;
public altitude: number;
public horizontalAccuracy: number;
public verticalAccuracy: number;
public speed: number; // in m/s ?
public direction: number; // in degrees
public timestamp: Date;
public android: any; // android Location
public ios: any; // iOS native location
}
export class Options {
/**
* Specifies desired accuracy in meters. Defaults to DesiredAccuracy.HIGH
*/
public desiredAccuracy: number;
/**
* Update distance filter in meters. Specifies how often to update. Default on iOS is no filter, on Android it is 0 meters
*/
public updateDistance: number;
/**
* Minimum time interval between location updates, in milliseconds (ignored on iOS)
*/
public minimumUpdateTime: number;
}
export class LocationRegion {
public latitude: number;
public longitude: number;
public raduis: number; // radius in meters
}