Skip to content

Commit 290e01c

Browse files
committed
Merge pull request NativeScript#2108 from sitefinitysteve/master
Add Method to see if camera is available
2 parents 64fcd06 + d4ffb76 commit 290e01c

File tree

6 files changed

+30
-0
lines changed

6 files changed

+30
-0
lines changed

apps/tests/camera-tests.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@
22
import camera = require("camera");
33
// << camera-require
44

5+
import TKUnit = require("./TKUnit");
6+
57
export var test_takePicture = function () {
68
// >> camera-take-picture
79
camera.takePicture().then(result => {
810
// result is ImageSource
911
});
1012
// << camera-take-picture
1113
};
14+
15+
export var test_isCameraAvailable = function () {
16+
// >> camera-is-availabile
17+
var availability = camera.isAvailable();
18+
// >> camera-is-availabile
19+
TKUnit.assertTrue(availability == true || availability == false, "Availability should return a Boolean");
20+
};

apps/tests/camera.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ Using a camera requires the camera module.
1010

1111
### Taking a picture.
1212
<snippet id='camera-take-picture'/>
13+
14+
### Check for availability
15+
<snippet id='camera-is-availabile'/>

camera/Readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ camera.takePicture({"cameraPosition": camera.CameraPosition.BACK, "flashMode": c
88
console.log('pic canceled');
99
});
1010
11+
if(camera.isAvailable()){
12+
console.log('you may take a picture');
13+
}
1114
```

camera/camera.android.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ export var takePicture = function (options?): Promise<any> {
106106
});
107107
}
108108

109+
export var isAvailable = function () {
110+
var utils: typeof utilsModule = require("utils/utils");
111+
112+
return utils.ad.getApplicationContext().getPackageManager().hasSystemFeature(android.content.pm.PackageManager.FEATURE_CAMERA)
113+
}
114+
109115
var calculateInSampleSize = function (imageWidth, imageHeight, reqWidth, reqHeight) {
110116
var sampleSize = 1;
111117
if (imageWidth > reqWidth && imageHeight > reqHeight) {

camera/camera.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ declare module "camera" {
1010
* @param options - Optional parameter for setting different camera options.
1111
*/
1212
export function takePicture(options?: CameraOptions): Promise<imageSource.ImageSource>;
13+
14+
/**
15+
* Is the camera available to use
16+
*/
17+
export function isAvailable(): Boolean;
1318

1419
export interface CameraOptions {
1520
/**

camera/camera.ios.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,7 @@ export var takePicture = function (options): Promise<any> {
124124
}
125125
});
126126
}
127+
128+
export var isAvailable = function () {
129+
return UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera);
130+
}

0 commit comments

Comments
 (0)