Skip to content

Commit

Permalink
#15 Add a 'requestPermissions' method
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Aug 26, 2017
1 parent 18f964d commit a054a22
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 64 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ before_script:

script:
- npm run setup
- npm run demo.android
- npm run test.android
- npm run tslint
- npm run demo.android
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* ability to receive partial results on Android ([d971d3e](https://github.com/EddyVerbruggen/nativescript-speech-recognition/commit/d971d3e))
* Added YouTube link to an Angular video ([ee45704](https://github.com/EddyVerbruggen/nativescript-speech-recognition/commit/ee45704))
* bump ([7603a65](https://github.com/EddyVerbruggen/nativescript-speech-recognition/commit/7603a65))
* changelog ([d804b5b](https://github.com/EddyVerbruggen/nativescript-speech-recognition/commit/d804b5b))
* Device or Emulator? #9 ([4de4425](https://github.com/EddyVerbruggen/nativescript-speech-recognition/commit/4de4425))
* ignore some more ([0c37443](https://github.com/EddyVerbruggen/nativescript-speech-recognition/commit/0c37443))

Expand Down
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,28 @@ speechRecognition.available().then(
// import the plugin
import { SpeechRecognition } from "nativescript-speech-recognition";

// instantiate the plugin (assuming the code below is inside a Class)
private speechRecognition = new SpeechRecognition();

public checkAvailability(): void {
this.speechRecognition.available().then(
(available: boolean) => console.log(available ? "YES!" : "NO"),
(err: string) => console.log(err)
);
class SomeClass {
private speechRecognition = new SpeechRecognition();

public checkAvailability(): void {
this.speechRecognition.available().then(
(available: boolean) => console.log(available ? "YES!" : "NO"),
(err: string) => console.log(err)
);
}
}
```

### `requestPermission`
You can either let `startListening` handle permissions when needed, but if you want to have more control
over when the permission popups are shown, you can use this function:

```typescript
this.speechRecognition.requestPermission().then((granted: boolean) => {
console.log("Granted? " + granted);
});
```

### `startListening`

On iOS this will trigger two prompts:
Expand Down
2 changes: 1 addition & 1 deletion demo/app/app.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import * as application from 'application';
import * as application from "tns-core-modules/application";
application.start({ moduleName: "main-page" });
6 changes: 3 additions & 3 deletions demo/app/main-page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as observable from 'data/observable';
import * as pages from 'ui/page';
import {HelloWorldModel} from './main-view-model';
import * as observable from "tns-core-modules/data/observable";
import * as pages from "tns-core-modules/ui/page";
import {HelloWorldModel} from "./main-view-model";

// Event handler for Page 'loaded' event attached in main-page.xml
export function pageLoaded(args: observable.EventData) {
Expand Down
12 changes: 11 additions & 1 deletion demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable } from "data/observable";
import { Observable } from "tns-core-modules/data/observable";
import { SpeechRecognition, SpeechRecognitionTranscription } from "nativescript-speech-recognition";

export class HelloWorldModel extends Observable {
Expand All @@ -10,6 +10,9 @@ export class HelloWorldModel extends Observable {
constructor() {
super();
this.speechRecognition = new SpeechRecognition();

// Testing manual persmission. You don't need to do this.
setTimeout(() => this.requestPermission(), 1500);
}

public startListeningNL(): void {
Expand Down Expand Up @@ -55,4 +58,11 @@ export class HelloWorldModel extends Observable {
console.log(`Error while trying to stop listening: ${errorMessage}`);
});
}

public requestPermission(): void {
let that = this;
this.speechRecognition.requestPermission().then((granted: boolean) => {
console.log("Granted? " + granted);
});
}
}
10 changes: 5 additions & 5 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"nativescript": {
"id": "org.nativescript.speechrecognition",
"tns-ios": {
"version": "3.0.1"
"version": "3.1.0"
},
"tns-android": {
"version": "3.0.0"
"version": "3.1.1"
}
},
"dependencies": {
"nativescript-speech-recognition": "file:../",
"nativescript-theme-core": "^1.0.4",
"nativescript-unit-test-runner": "^0.3.4",
"tns-core-modules": "^3.0.1"
"tns-core-modules": "^3.1.0"
},
"devDependencies": {
"babel-traverse": "6.12.0",
Expand All @@ -24,7 +24,7 @@
"karma-jasmine": "^1.0.2",
"karma-nativescript-launcher": "^0.4.0",
"lazy": "1.0.11",
"nativescript-dev-typescript": "^0.4.0",
"typescript": "^2.3.0"
"nativescript-dev-typescript": "libs",
"typescript": "~2.3.0"
}
}
1 change: 0 additions & 1 deletion demo/references.d.ts

This file was deleted.

12 changes: 3 additions & 9 deletions demo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"dom",
"es6"
"es6",
"dom"
],
"pretty": true,
"allowUnreachableCode": false,
Expand All @@ -20,15 +20,9 @@
"noImplicitReturns": true,
"noImplicitUseStrict": false,
"noFallthroughCasesInSwitch": true,
"typeRoots": [
"./node_modules/@types",
"./node_modules"
],
"types": [],
"baseUrl": ".",
"paths": {
"*": [
"./node_modules/tns-core-modules/*",
"./node_modules/*"
]
}
Expand All @@ -38,4 +32,4 @@
"platforms"
],
"compileOnSave": false
}
}
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './speech-recognition.ios';
export * from "./speech-recognition.ios";

export * from './speech-recognition.common';
export * from "./speech-recognition.common";
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
},
"scripts": {
"build": "tsc",
"build": "tsc -skipLibCheck",
"demo.ios": "npm run preparedemo && cd demo && tns run ios --emulator",
"demo.ios.device": "npm run preparedemo && cd demo && tns run ios",
"demo.android": "npm run preparedemo && cd demo && tns run android",
Expand All @@ -22,7 +22,8 @@
"setup": "npm i && cd demo && npm i && cd .. && npm run build && cd demo && tns plugin add .. && cd ..",
"changelog": "conventional-changelog -i CHANGELOG.md -s -r 0",
"changelog:add": "git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md'",
"prepublish": "npm run build"
"prepublishOnly": "npm run build",
"tslint": "tslint \"**/*.ts\" --config tslint.json --exclude \"**/node_modules/**\" --exclude \"demo/platforms/**\""
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -56,11 +57,10 @@
"homepage": "https://github.com/EddyVerbruggen/nativescript-speech-recognition",
"readmeFilename": "README.md",
"devDependencies": {
"conventional-changelog-cli": "^1.2.0",
"prompt": "^1.0.0",
"rimraf": "^2.5.0",
"tns-core-modules": "^2.5.0",
"tns-platform-declarations": "^2.5.0",
"typescript": "^2.1.0"
"conventional-changelog-cli": "^1.3.2",
"tns-core-modules": "^3.1.0",
"tns-platform-declarations": "^3.1.0",
"tslint": "^5.0.0",
"typescript": "~2.3.0"
}
}
4 changes: 2 additions & 2 deletions platforms/ios/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<plist version="1.0">
<dict>
<key>NSSpeechRecognitionUsageDescription</key>
<string/>
<string> </string>
<key>NSMicrophoneUsageDescription</key>
<string/>
<string> </string>
</dict>
</plist>
2 changes: 0 additions & 2 deletions references.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
/// <reference path="./node_modules/tns-core-modules/tns-core-modules.es2016.d.ts" />

/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
23 changes: 16 additions & 7 deletions speech-recognition.android.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SpeechRecognitionApi, SpeechRecognitionOptions } from "./speech-recognition.common";
import * as application from "application";
import * as utils from "utils/utils";
import * as application from "tns-core-modules/application";
import * as utils from "tns-core-modules/utils/utils";

declare let android: any;

Expand Down Expand Up @@ -39,7 +39,16 @@ export class SpeechRecognition implements SpeechRecognitionApi {
return new Promise((resolve, reject) => {
resolve(android.speech.SpeechRecognizer.isRecognitionAvailable(application.android.context));
});
};
}

requestPermission(): Promise<boolean> {
console.log(">> requestPermission");
return new Promise((resolve, reject) => {
this._requestPermission(
() => resolve(true),
() => resolve(false));
});
}

startListening(options: SpeechRecognitionOptions): Promise<boolean> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -124,7 +133,7 @@ export class SpeechRecognition implements SpeechRecognitionApi {
text: transcripts.get(0), // the first one has the highest confidence
// confidence: confidences[0],
finished: true
})
});
}
},

Expand Down Expand Up @@ -161,7 +170,7 @@ export class SpeechRecognition implements SpeechRecognitionApi {
};

if (!this.wasPermissionGranted()) {
this.requestPermission(onPermissionGranted, reject);
this._requestPermission(onPermissionGranted, reject);
return;
}

Expand Down Expand Up @@ -198,9 +207,9 @@ export class SpeechRecognition implements SpeechRecognitionApi {
android.Manifest.permission.RECORD_AUDIO);
}
return hasPermission;
};
}

private requestPermission(onPermissionGranted: Function, reject): void {
private _requestPermission(onPermissionGranted: Function, reject): void {
this.onPermissionGranted = onPermissionGranted;
this.onPermissionRejected = reject;
android.support.v4.app.ActivityCompat.requestPermissions(
Expand Down
1 change: 1 addition & 0 deletions speech-recognition.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface SpeechRecognitionOptions {

export interface SpeechRecognitionApi {
available(): Promise<boolean>;
requestPermission(): Promise<boolean>;
startListening(options: SpeechRecognitionOptions): Promise<boolean>;
stopListening(): Promise<any>;
}
16 changes: 15 additions & 1 deletion speech-recognition.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ export class SpeechRecognition implements SpeechRecognitionApi {
return new Promise((resolve, reject) => {
resolve(SFSpeechRecognizer.new().available);
});
};
}

requestPermission(): Promise<boolean> {
return new Promise((resolve, reject) => {
SFSpeechRecognizer.requestAuthorization((status: SFSpeechRecognizerAuthorizationStatus) => {
if (status !== SFSpeechRecognizerAuthorizationStatus.Authorized) {
resolve(false);
return;
}
AVAudioSession.sharedInstance().requestRecordPermission((granted: boolean) => {
resolve(granted);
});
});
});
}

startListening(options: SpeechRecognitionOptions): Promise<boolean> {
return new Promise((resolve, reject) => {
Expand Down
18 changes: 6 additions & 12 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
"noLib": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2016"
],
"lib": ["es6", "dom"],
"sourceMap": true,
"pretty": true,
"allowUnreachableCode": false,
Expand All @@ -19,17 +17,13 @@
"noImplicitAny": false,
"noImplicitReturns": true,
"noImplicitUseStrict": false,
"noFallthroughCasesInSwitch": true,
"typeRoots": [
"./node_modules/@types",
"./node_modules"
],
"types": [
]
"noFallthroughCasesInSwitch": true
},
"exclude": [
"demo",
"node_modules"
"demo/node_modules",
"demo/platforms",
"node_modules",
"platforms"
],
"compileOnSave": false
}
Loading

0 comments on commit a054a22

Please sign in to comment.