diff --git a/BCL.csproj b/BCL.csproj index 85257e659..c92b1e051 100644 --- a/BCL.csproj +++ b/BCL.csproj @@ -178,6 +178,9 @@ + + + commonjs True diff --git a/Image/Readme.md b/Image/Readme.md index 4bfd8e245..9cbaea654 100644 --- a/Image/Readme.md +++ b/Image/Readme.md @@ -1,5 +1,6 @@ The way we get local path here is different for now. Maybe we should get it from FS module in the future samples. Sample code Android: ``` -// TODO: Update code sample using require and BCL File/Folder +var Image = require("Image").Image; +var baseImage = Image.imageFromResource('foxie'); ``` diff --git a/Image/image.android.ts b/Image/image.android.ts index e8a4c2494..0cfaaa2e0 100644 --- a/Image/image.android.ts +++ b/Image/image.android.ts @@ -12,6 +12,26 @@ export class Image { this.android = null; } + public static imageFromResource(name: string): Image { + var image = new Image(); + return image.loadFromResource(name) ? image : null; + } + + public static imageFromFile(path: string): Image { + var image = new Image(); + return image.loadFromFile(path) ? image : null; + } + + public static imageFromData(data: any): Image { + var image = new Image(); + return image.loadFromData(data) ? image : null; + } + + public static imageFromNativeBitmap(source: any): Image { + var image = new Image(); + return image.loadFromNativeBitmap(source) ? image : null; + } + public loadFromResource(name: string): boolean { var androidApp = app_module.tk.ui.Application.current.android; var res = androidApp.context.getResources(); @@ -35,7 +55,7 @@ export class Image { return (this.android != null); } - public loadFromBitmap(source: any): boolean { + public loadFromNativeBitmap(source: any): boolean { this.android = source; return (this.android != null); } diff --git a/Image/image.d.ts b/Image/image.d.ts index 7c5452049..0513c5453 100644 --- a/Image/image.d.ts +++ b/Image/image.d.ts @@ -4,10 +4,15 @@ } export declare class Image { + static imageFromResource(name: string): Image; + static imageFromFile(path: string): Image; + static imageFromData(data: any): Image; + static imageFromNativeBitmap(source: any): Image; + loadFromResource(name: string): boolean; loadFromFile(path: string): boolean; loadFromData(data: any): boolean; - loadFromBitmap(source: any): boolean; + loadFromNativeBitmap(source: any): boolean; saveToFile(path: string, format: ImageType, quality?: number): boolean; getHeight(): number; diff --git a/Image/image.ios.ts b/Image/image.ios.ts index dfaf8497a..c0d762254 100644 --- a/Image/image.ios.ts +++ b/Image/image.ios.ts @@ -10,6 +10,26 @@ export class Image { this.ios = null; } + public static imageFromResource(name: string): Image { + var image = new Image(); + return image.loadFromResource(name) ? image : null; + } + + public static imageFromFile(path: string): Image { + var image = new Image(); + return image.loadFromFile(path) ? image : null; + } + + public static imageFromData(data: any): Image { + var image = new Image(); + return image.loadFromData(data) ? image : null; + } + + public static imageFromNativeBitmap(source: any): Image { + var image = new Image(); + return image.loadFromNativeBitmap(source) ? image : null; + } + public loadFromResource(name: string): boolean { this.ios = UIKit.UIImage.imageNamed(name); return (this.ios != null); @@ -25,7 +45,7 @@ export class Image { return (this.ios != null); } - public loadFromBitmap(source: any): boolean { + public loadFromNativeBitmap(source: any): boolean { this.ios = source; return (this.ios != null); } diff --git a/Location/Readme.md b/Location/Readme.md new file mode 100644 index 000000000..50c48df5c --- /dev/null +++ b/Location/Readme.md @@ -0,0 +1,21 @@ +Initializing location: + +``` +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(); +``` \ No newline at end of file diff --git a/Location/location.android.ts b/Location/location.android.ts index 320369515..1eda848c3 100644 --- a/Location/location.android.ts +++ b/Location/location.android.ts @@ -1,87 +1,136 @@ -import types_module = require("Location/location_types"); +import types = require("Location/location_types"); import app_module = require("Application/application"); export class LocationManager { - - //public regions: LocationRegion[]; - - private _locationManager: any; - private _locationListener: android.location.LocationListener; - - public isLocationEnabled(): boolean { - // TODO add proper implementation - return true; - } - - constructor() { - //this.regions = []; - this.desiredAccuracy = types_module.DesiredAccuracy.ANY; - this._locationManager = app_module.tk.ui.Application.current.android.context.getSystemService('location'); - Log('location manager: ' + this._locationManager); - - this._locationListener = new android.location.LocationListener({ - onLocationChanged: function (location: android.location.Location) { - }, - - onProviderDisabled: function (provider: string) { - }, - - onProviderEnabled: function (provider: string) { - }, - - onStatusChanged: function(arg1: string, arg2: number, arg3: android.os.Bundle): void { - } - }); - } - // in meters // we might need some predefined values here like 'any' and 'high' public desiredAccuracy: number; - // listeners - public locationChangeListener: types_module.LocationChangeListener; + // The minimum distance (measured in meters) a device must move horizontally before an update event is generated. + public updateDistance: number; - // public regionChangeListener: RegionChangeListener; + // minimum time interval between location updates, in milliseconds (android only) + public minimumUpdateTime: number; + public isStarted: boolean; + private androidLocationManager: any; - /* // regions - public addRegion(region: LocationRegion) { - this.regions.push(region); - } + private _locationListener: any; - public removeRegion(region: LocationRegion) { + public static isLocationEnabled(): boolean { + var criteria = new android.location.Criteria(); + criteria.setAccuracy(1); // low ? fine ? who knows what 1 means (bug in android docs?) + var lm = app_module.tk.ui.Application.current.android.context.getSystemService('location'); + return (lm.getBestProvider(criteria, true) != null) ? true : false; + } - } + constructor() { + // put some defaults + this.desiredAccuracy = types.DesiredAccuracy.HIGH; + this.updateDistance = 10; + this.minimumUpdateTime = 200; + this.isStarted = false; - public clearRegions() { + this.androidLocationManager = app_module.tk.ui.Application.current.android.context.getSystemService('location'); + } - }*/ + 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()); + //console.dump(location); + return location; + } // monitoring - public startLocationMonitoring() { - var criteria = new android.location.Criteria(); - criteria.setAccuracy((this.desiredAccuracy === types_module.DesiredAccuracy.HIGH) ? 3 : 1); - var providers = this._locationManager.getProviders(criteria, false); - var it = providers.iterator(); - while (it.hasNext()) { - var element = it.next(); - Log('found provider: ' + element); - this._locationManager.requestLocationUpdates(element, 200, 10, this._locationListener); + public startLocationMonitoring(onLocation: (location: types.Location) => any, onError?: (error: string) => any) { + if (!this.isStarted) { + var criteria = new android.location.Criteria(); + criteria.setAccuracy((this.desiredAccuracy === types.DesiredAccuracy.HIGH) ? 1 : 2); +/* // We could provide 'true' for the second parameter here and get only enabled providers. + // However this would exclude the case when user enables the provider later. + // Another option is to get the best provider, but then again, this would + // exclude all other providers matching our criteria + var providers = this.androidLocationManager.getProviders(criteria, false); + var it = providers.iterator(); + while (it.hasNext()) { + var element = it.next(); + console.log('found provider: ' + element); + this.androidLocationManager.requestLocationUpdates(element, 200, 10, this._locationListener); + }*/ + this._locationListener = 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 { + } + }); + 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.message); + } + } } + else if (onError) { + onError('location monitoring already started'); + } + } public stopLocationMonitoring() { - this._locationManager.removeUpdates(this._locationListener); + if (this.isStarted) { + this.androidLocationManager.removeUpdates(this._locationListener); + this.isStarted = false; + } } // other - public getLastKnownLocation(): types_module.Location { + public getLastKnownLocation(): types.Location { + var criteria = new android.location.Criteria(); + criteria.setAccuracy((this.desiredAccuracy === types.DesiredAccuracy.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; } - public distanceInMeters(loc1: types_module.Location, loc2: types_module.Location): number { + public distanceInMeters(loc1: types.Location, loc2: types.Location): number { return 0; } } \ No newline at end of file diff --git a/Location/location.d.ts b/Location/location.d.ts index b2eb7a55b..c79a99c96 100644 --- a/Location/location.d.ts +++ b/Location/location.d.ts @@ -28,19 +28,19 @@ export declare class LocationRegion { } export declare class Location { - public latitude: number; - public longitude: number; + latitude: number; + longitude: number; - public altitude: number; + altitude: number; - public horizontalAccuracy: number; - public verticalAccuracy: number; + horizontalAccuracy: number; + verticalAccuracy: number; - public speed: number; // in m/s ? + speed: number; // in m/s ? - public direction: number; // in degrees + direction: number; // in degrees - public timestamp: Date; + timestamp: Date; } export declare class LocationChangeListener { @@ -54,9 +54,12 @@ export declare class RegionChangeListener { } export declare class LocationManager { - isLocationEnabled(): boolean; + static isLocationEnabled(): boolean; desiredAccuracy: number; updateDistance: number; + // minimum time interval between location updates, in milliseconds (android only) + minimumUpdateTime: number; + isStarted: boolean; // listeners locationChangeListener: LocationChangeListener; diff --git a/Location/location.ios.ts b/Location/location.ios.ts index 9d847b860..71944fd58 100644 --- a/Location/location.ios.ts +++ b/Location/location.ios.ts @@ -2,20 +2,6 @@ export class LocationManager { - private isStarted: boolean; - private locationManager: CoreLocation.CLLocationManager; - - public isLocationEnabled(): boolean { - return CoreLocation.CLLocationManager.locationServicesEnabled(); - } - - constructor() { - this.isStarted = false; - this.desiredAccuracy = types.DesiredAccuracy.HIGH; - this.updateDistance = -1; // kCLDistanceFilterNone - this.locationManager = new CoreLocation.CLLocationManager(); - } - // in meters // we might need some predefined values here like 'any' and 'high' public desiredAccuracy: number; @@ -23,10 +9,19 @@ export class LocationManager { // The minimum distance (measured in meters) a device must move horizontally before an update event is generated. public updateDistance: number; - // listeners - //public locationChangeListener: types.LocationChangeListener; + public isStarted: boolean; + private iosLocationManager: CoreLocation.CLLocationManager; - // monitoring + public static isLocationEnabled(): boolean { + return CoreLocation.CLLocationManager.locationServicesEnabled(); + } + + constructor() { + this.isStarted = false; + this.desiredAccuracy = types.DesiredAccuracy.HIGH; + this.updateDistance = -1; // kCLDistanceFilterNone + this.iosLocationManager = new CoreLocation.CLLocationManager(); + } private static locationFromCLLocation(clLocation: CoreLocation.CLLocation): types.Location { var location = new types.Location(); @@ -38,10 +33,11 @@ export class LocationManager { location.speed = clLocation.speed; location.direction = clLocation.course; location.timestamp = new Date(clLocation.timestamp.timeIntervalSince1970() * 1000); - console.dump(location); + //console.dump(location); return location; } + // monitoring public startLocationMonitoring(onLocation: (location: types.Location) => any, onError?: (error: string) => any) { if (!this.isStarted) { var LocationListener = Foundation.NSObject.extends({ @@ -73,10 +69,10 @@ export class LocationManager { var listener = new LocationListener(); listener.setupWithFunctions(onLocation, onError); - this.locationManager.delegate = listener; - this.locationManager.desiredAccuracy = this.desiredAccuracy; - this.locationManager.distanceFilter = this.updateDistance; - this.locationManager.startUpdatingLocation(); + this.iosLocationManager.delegate = listener; + this.iosLocationManager.desiredAccuracy = this.desiredAccuracy; + this.iosLocationManager.distanceFilter = this.updateDistance; + this.iosLocationManager.startUpdatingLocation(); } else if (onError) { onError('location monitoring already started'); @@ -85,7 +81,7 @@ export class LocationManager { public stopLocationMonitoring() { if (this.isStarted) { - this.locationManager.stopUpdatingLocation(); + this.iosLocationManager.stopUpdatingLocation(); this.isStarted = false; } } @@ -93,7 +89,7 @@ export class LocationManager { // other public getLastKnownLocation(): types.Location { - var clLocation = this.locationManager.location; + var clLocation = this.iosLocationManager.location; if (null != clLocation) { return LocationManager.locationFromCLLocation(clLocation); } diff --git a/net/http_client.android.ts b/net/http_client.android.ts index 6b4baa803..6d334546b 100644 --- a/net/http_client.android.ts +++ b/net/http_client.android.ts @@ -32,7 +32,7 @@ export class http { var d = promises.defer(); http.get(url, r => { var image = new image_module.Image(); - image.loadFromBitmap(r); + image.loadFromNativeBitmap(r); d.resolve(image); }, e => d.reject(e)); return d.promise();