Skip to content

Commit 6e00740

Browse files
authored
Merge pull request #2 from GeoTecINIT/fix-recog-listen
Fix abstract recognizer to rely on system status
2 parents 028aff7 + 53be906 commit 6e00740

File tree

4 files changed

+43
-69
lines changed

4 files changed

+43
-69
lines changed

demo/app/tests/internal/activity-recognition/recognizers/low-res/android/recognizer.android.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ describe("Android low resolution activity recognizer", () => {
4040

4141
spyOn(recognizerManager, "isReady").and.callThrough();
4242
spyOn(recognizerManager, "prepare");
43-
spyOn(recognizerManager, "stopListening");
4443

4544
spyOn(callbackManager, "add").and.callThrough();
4645
spyOn(callbackManager, "remove");
@@ -63,24 +62,21 @@ describe("Android low resolution activity recognizer", () => {
6362
spyOn(recognizerManager, "startListening");
6463
spyOn(recognizerState, "isActive")
6564
.withArgs(recognizerType)
66-
.and.returnValue(Promise.resolve(false));
65+
.and.returnValue(Promise.resolve(true));
6766
await recognizer.setup();
68-
expect(recognizerManager.startListening).not.toHaveBeenCalled();
67+
expect(recognizerManager.startListening).toHaveBeenCalled();
6968
});
7069

71-
it("does not restart listening when already active", async () => {
70+
it("does not (re)start listening when inactive", async () => {
7271
spyOn(recognizerManager, "startListening");
7372
spyOn(recognizerState, "isActive")
7473
.withArgs(recognizerType)
75-
.and.returnValue(Promise.resolve(true));
74+
.and.returnValue(Promise.resolve(false));
7675
await recognizer.setup();
77-
expect(recognizerManager.startListening).toHaveBeenCalled();
76+
expect(recognizerManager.startListening).not.toHaveBeenCalled();
7877
});
7978

8079
it("allows to start the recognition by activating the underlying subsystem", async () => {
81-
spyOn(recognizerState, "isActive")
82-
.withArgs(recognizerType)
83-
.and.returnValue(Promise.resolve(false));
8480
spyOn(recognizerManager, "startListening");
8581
await recognizer.startRecognizing();
8682
expect(recognizerManager.startListening).toHaveBeenCalled();
@@ -90,46 +86,37 @@ describe("Android low resolution activity recognizer", () => {
9086
});
9187

9288
it("does not mark the recognizer as active if the activation fails", async () => {
93-
spyOn(recognizerState, "isActive")
94-
.withArgs(recognizerType)
95-
.and.returnValue(Promise.resolve(false));
9689
const listenError = new Error("Could not start listening");
9790
spyOn(recognizerManager, "startListening").and.rejectWith(listenError);
9891
await expectAsync(recognizer.startRecognizing()).toBeRejectedWith(
9992
listenError
10093
);
101-
});
102-
103-
it("does not activate the underlying system if the recognizer is already active", async () => {
104-
spyOn(recognizerState, "isActive")
105-
.withArgs(recognizerType)
106-
.and.returnValue(Promise.resolve(true));
107-
spyOn(recognizerManager, "startListening");
108-
await recognizer.startRecognizing();
109-
expect(recognizerManager.startListening).not.toHaveBeenCalled();
11094
expect(recognizerState.markAsActive).not.toHaveBeenCalledWith(
11195
recognizerType
11296
);
11397
});
11498

11599
it("allows to stop the recognition by deactivating the underlying subsystem", async () => {
116-
spyOn(recognizerState, "isActive")
117-
.withArgs(recognizerType)
118-
.and.returnValue(Promise.resolve(true));
100+
spyOn(recognizerManager, "stopListening");
119101
await recognizer.stopRecognizing();
120102
expect(recognizerManager.stopListening).toHaveBeenCalled();
121103
expect(recognizerState.markAsInactive).toHaveBeenCalledWith(
122104
recognizerType
123105
);
124106
});
125107

126-
it("does not deactivate the underlying system if the recognizer is already inactive", async () => {
127-
spyOn(recognizerState, "isActive")
128-
.withArgs(recognizerType)
129-
.and.returnValue(Promise.resolve(false));
108+
it("marks the recognizer as inactive even though the deactivation fails", async () => {
109+
const deactivationError = new Error(
110+
"Could not stop listening due to permissions not granted"
111+
);
112+
spyOn(recognizerManager, "stopListening").and.rejectWith(
113+
deactivationError
114+
);
130115
await recognizer.stopRecognizing();
131-
expect(recognizerManager.stopListening).not.toHaveBeenCalled();
132-
expect(recognizerState.markAsInactive).not.toHaveBeenCalledWith(
116+
await expectAsync(recognizerManager.stopListening()).toBeRejectedWith(
117+
deactivationError
118+
);
119+
expect(recognizerState.markAsInactive).toHaveBeenCalledWith(
133120
recognizerType
134121
);
135122
});

demo/app/tests/internal/activity-recognition/recognizers/medium-res/android/recognizer.android.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ describe("Android medium resolution activity recognizer", () => {
4040

4141
spyOn(recognizerManager, "isReady").and.callThrough();
4242
spyOn(recognizerManager, "prepare");
43-
spyOn(recognizerManager, "stopListening");
4443

4544
spyOn(callbackManager, "add").and.callThrough();
4645
spyOn(callbackManager, "remove");
@@ -63,24 +62,21 @@ describe("Android medium resolution activity recognizer", () => {
6362
spyOn(recognizerManager, "startListening");
6463
spyOn(recognizerState, "isActive")
6564
.withArgs(recognizerType)
66-
.and.returnValue(Promise.resolve(false));
65+
.and.returnValue(Promise.resolve(true));
6766
await recognizer.setup();
68-
expect(recognizerManager.startListening).not.toHaveBeenCalled();
67+
expect(recognizerManager.startListening).toHaveBeenCalled();
6968
});
7069

71-
it("does not restart listening when already active", async () => {
70+
it("does not (re)start listening when inactive", async () => {
7271
spyOn(recognizerManager, "startListening");
7372
spyOn(recognizerState, "isActive")
7473
.withArgs(recognizerType)
75-
.and.returnValue(Promise.resolve(true));
74+
.and.returnValue(Promise.resolve(false));
7675
await recognizer.setup();
77-
expect(recognizerManager.startListening).toHaveBeenCalled();
76+
expect(recognizerManager.startListening).not.toHaveBeenCalled();
7877
});
7978

8079
it("allows to start the recognition by activating the underlying subsystem", async () => {
81-
spyOn(recognizerState, "isActive")
82-
.withArgs(recognizerType)
83-
.and.returnValue(Promise.resolve(false));
8480
spyOn(recognizerManager, "startListening");
8581
await recognizer.startRecognizing();
8682
expect(recognizerManager.startListening).toHaveBeenCalled();
@@ -90,46 +86,37 @@ describe("Android medium resolution activity recognizer", () => {
9086
});
9187

9288
it("does not mark the recognizer as active if the activation fails", async () => {
93-
spyOn(recognizerState, "isActive")
94-
.withArgs(recognizerType)
95-
.and.returnValue(Promise.resolve(false));
9689
const listenError = new Error("Could not start listening");
9790
spyOn(recognizerManager, "startListening").and.rejectWith(listenError);
9891
await expectAsync(recognizer.startRecognizing()).toBeRejectedWith(
9992
listenError
10093
);
101-
});
102-
103-
it("does not activate the underlying system if the recognizer is already active", async () => {
104-
spyOn(recognizerState, "isActive")
105-
.withArgs(recognizerType)
106-
.and.returnValue(Promise.resolve(true));
107-
spyOn(recognizerManager, "startListening");
108-
await recognizer.startRecognizing();
109-
expect(recognizerManager.startListening).not.toHaveBeenCalled();
11094
expect(recognizerState.markAsActive).not.toHaveBeenCalledWith(
11195
recognizerType
11296
);
11397
});
11498

11599
it("allows to stop the recognition by deactivating the underlying subsystem", async () => {
116-
spyOn(recognizerState, "isActive")
117-
.withArgs(recognizerType)
118-
.and.returnValue(Promise.resolve(true));
100+
spyOn(recognizerManager, "stopListening");
119101
await recognizer.stopRecognizing();
120102
expect(recognizerManager.stopListening).toHaveBeenCalled();
121103
expect(recognizerState.markAsInactive).toHaveBeenCalledWith(
122104
recognizerType
123105
);
124106
});
125107

126-
it("does not deactivate the underlying system if the recognizer is already inactive", async () => {
127-
spyOn(recognizerState, "isActive")
128-
.withArgs(recognizerType)
129-
.and.returnValue(Promise.resolve(false));
108+
it("marks the recognizer as inactive even though the deactivation fails", async () => {
109+
const deactivationError = new Error(
110+
"Could not stop listening due to permissions not granted"
111+
);
112+
spyOn(recognizerManager, "stopListening").and.rejectWith(
113+
deactivationError
114+
);
130115
await recognizer.stopRecognizing();
131-
expect(recognizerManager.stopListening).not.toHaveBeenCalled();
132-
expect(recognizerState.markAsInactive).not.toHaveBeenCalledWith(
116+
await expectAsync(recognizerManager.stopListening()).toBeRejectedWith(
117+
deactivationError
118+
);
119+
expect(recognizerState.markAsInactive).toHaveBeenCalledWith(
133120
recognizerType
134121
);
135122
});

src/internal/activity-recognition/recognizers/abstract-recognizer.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ export abstract class AbstractActivityRecognizer implements ActivityRecognizer {
2929
}
3030

3131
async startRecognizing(options: StartOptions = {}): Promise<void> {
32-
const active = await this.recognizerState.isActive(this.recognizerType);
33-
if (active) {
34-
return;
35-
}
3632
await this.recognitionManager.startListening(options);
3733
await this.recognizerState.markAsActive(this.recognizerType);
3834
}
3935

4036
async stopRecognizing(): Promise<void> {
41-
const active = await this.recognizerState.isActive(this.recognizerType);
42-
if (!active) {
43-
return;
37+
try {
38+
await this.recognitionManager.stopListening();
39+
} catch (e) {
40+
console.error(
41+
`Could not deactivate ${this.recognizerType} res activity recognizer: ${
42+
e.stack ? e.stack : e
43+
}`
44+
);
4445
}
45-
await this.recognitionManager.stopListening();
4646
await this.recognizerState.markAsInactive(this.recognizerType);
4747
}
4848

src/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)