Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 3858c44

Browse files
Merge pull request #893 from Soarc/feature/ml-kit-camera-app-lifecycle
[Android] MLKit camera resume when app resumes from background.
2 parents d0f3f3d + c5d07c8 commit 3858c44

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

publish/pack.cmd

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@echo off
2+
SET SOURCE_DIR=.\..\src
3+
SET TO_SOURCE_DIR=.\src
4+
SET PACK_DIR=.\package
5+
SET ROOT_DIR=.\..
6+
SET PUBLISH=--publish
7+
call npm i
8+
9+
echo Clearing /src and /package...
10+
@RD /S /Q %TO_SOURCE_DIR%
11+
@RD /S /Q %PACK_DIR%
12+
13+
REM copy src
14+
echo 'Copying src...'
15+
xcopy /s %SOURCE_DIR% %TO_SOURCE_DIR%\
16+
17+
REM copy README & LICENSE to src
18+
echo 'Copying README and LICENSE to /src...'
19+
copy %ROOT_DIR%\LICENSE %TO_SOURCE_DIR%\LICENSE
20+
copy %ROOT_DIR%\README.md %TO_SOURCE_DIR%\README.md
21+
22+
REM compile package and copy files required by npm
23+
echo 'Building /src...'
24+
cd %TO_SOURCE_DIR%
25+
call node_modules/.bin/tsc
26+
cd ..
27+
28+
echo 'Creating package...'
29+
REM create package dir
30+
mkdir %PACK_DIR%
31+
32+
REM create the package
33+
cd %PACK_DIR%
34+
call npm pack ..\%TO_SOURCE_DIR%
35+
36+
REM delete source directory used to create the package
37+
cd ..
38+
@RD /S /Q %TO_SOURCE_DIR%

src/mlkit/mlkit-cameraview.android.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
9090
return nativeView;
9191
}
9292

93+
initNativeView(): void {
94+
application.on("resume", arg => {
95+
this.runCamera();
96+
} );
97+
super.initNativeView();
98+
}
99+
93100
private hasCamera() {
94101
return !!utils.ad
95102
.getApplicationContext()
@@ -106,6 +113,10 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
106113
nativeView.addView(this.surfaceView);
107114

108115
// Note that surfaceview callbacks didn't seem to work, so using a good old timeout (https://github.com/firebase/quickstart-android/blob/0f4c86877fc5f771cac95797dffa8bd026dd9dc7/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/CameraSourcePreview.java#L47)
116+
this.runCamera();
117+
}
118+
119+
private runCamera(): void {
109120
setTimeout(() => {
110121
const surfaceHolder = this.surfaceView.getHolder();
111122
const cameraFacingRequested = android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK;
@@ -232,7 +243,7 @@ export abstract class MLKitCameraView extends MLKitCameraViewBase {
232243
return new com.google.android.gms.tasks.OnFailureListener({
233244
onFailure: exception => console.log(exception.getMessage())
234245
});
235-
};
246+
}
236247

237248
private generateValidPreviewSizeList(camera): Array<SizePair> {
238249
let parameters = camera.getParameters();

0 commit comments

Comments
 (0)