11using System . Runtime . Versioning ;
22using Android . Content ;
3- using Android . Provider ;
43using Android . Runtime ;
54using Android . Views ;
65using AndroidX . Camera . Core ;
@@ -63,8 +62,48 @@ public async Task SetExtensionMode(int mode, CancellationToken token)
6362
6463 public void Dispose ( )
6564 {
66- Dispose ( true ) ;
67- GC . SuppressFinalize ( this ) ;
65+ CleanupVideoRecordingResources ( ) ;
66+
67+ camera ? . Dispose ( ) ;
68+ camera = null ;
69+
70+ cameraControl ? . Dispose ( ) ;
71+ cameraControl = null ;
72+
73+ cameraPreview ? . Dispose ( ) ;
74+ cameraPreview = null ;
75+
76+ cameraExecutor ? . Dispose ( ) ;
77+ cameraExecutor = null ;
78+
79+ imageCapture ? . Dispose ( ) ;
80+ imageCapture = null ;
81+
82+ videoCapture ? . Dispose ( ) ;
83+ videoCapture = null ;
84+
85+ imageCallback ? . Dispose ( ) ;
86+ imageCallback = null ;
87+
88+ previewView ? . Dispose ( ) ;
89+ previewView = null ;
90+
91+ processCameraProvider ? . UnbindAll ( ) ;
92+ processCameraProvider ? . Dispose ( ) ;
93+ processCameraProvider = null ;
94+
95+ resolutionSelector ? . Dispose ( ) ;
96+ resolutionSelector = null ;
97+
98+ resolutionFilter ? . Dispose ( ) ;
99+ resolutionFilter = null ;
100+
101+ orientationListener ? . Disable ( ) ;
102+ orientationListener ? . Dispose ( ) ;
103+ orientationListener = null ;
104+
105+ videoRecordingStream ? . Dispose ( ) ;
106+ videoRecordingStream = null ;
68107 }
69108
70109 // IN the future change the return type to be an alias
@@ -138,55 +177,7 @@ public async partial ValueTask UpdateCaptureResolution(Size resolution, Cancella
138177 }
139178 }
140179
141- protected virtual void Dispose ( bool disposing )
142- {
143- if ( disposing )
144- {
145- CleanupVideoRecordingResources ( ) ;
146-
147- camera ? . Dispose ( ) ;
148- camera = null ;
149-
150- cameraControl ? . Dispose ( ) ;
151- cameraControl = null ;
152-
153- cameraPreview ? . Dispose ( ) ;
154- cameraPreview = null ;
155-
156- cameraExecutor ? . Dispose ( ) ;
157- cameraExecutor = null ;
158-
159- imageCapture ? . Dispose ( ) ;
160- imageCapture = null ;
161-
162- videoCapture ? . Dispose ( ) ;
163- videoCapture = null ;
164-
165- imageCallback ? . Dispose ( ) ;
166- imageCallback = null ;
167-
168- previewView ? . Dispose ( ) ;
169- previewView = null ;
170-
171- processCameraProvider ? . Dispose ( ) ;
172- processCameraProvider = null ;
173-
174- resolutionSelector ? . Dispose ( ) ;
175- resolutionSelector = null ;
176-
177- resolutionFilter ? . Dispose ( ) ;
178- resolutionFilter = null ;
179-
180- orientationListener ? . Disable ( ) ;
181- orientationListener ? . Dispose ( ) ;
182- orientationListener = null ;
183-
184- videoRecordingStream ? . Dispose ( ) ;
185- videoRecordingStream = null ;
186- }
187- }
188-
189- protected virtual async partial Task PlatformConnectCamera ( CancellationToken token )
180+ private async partial Task PlatformConnectCamera ( CancellationToken token )
190181 {
191182 var cameraProviderFuture = ProcessCameraProvider . GetInstance ( context ) ;
192183 if ( previewView is null )
@@ -200,16 +191,6 @@ protected virtual async partial Task PlatformConnectCamera(CancellationToken tok
200191 {
201192 processCameraProvider = ( ProcessCameraProvider ) ( cameraProviderFuture . Get ( ) ?? throw new CameraException ( $ "Unable to retrieve { nameof ( ProcessCameraProvider ) } ") ) ;
202193
203- if ( cameraProvider . AvailableCameras is null )
204- {
205- await cameraProvider . RefreshAvailableCameras ( token ) ;
206-
207- if ( cameraProvider . AvailableCameras is null )
208- {
209- throw new CameraException ( "Unable to refresh available cameras" ) ;
210- }
211- }
212-
213194 await StartUseCase ( token ) ;
214195
215196 cameraProviderTCS . SetResult ( ) ;
@@ -218,7 +199,7 @@ protected virtual async partial Task PlatformConnectCamera(CancellationToken tok
218199 await cameraProviderTCS . Task . WaitAsync ( token ) ;
219200 }
220201
221- protected async Task StartUseCase ( CancellationToken token )
202+ async Task StartUseCase ( CancellationToken token )
222203 {
223204 if ( resolutionSelector is null || cameraExecutor is null )
224205 {
@@ -265,22 +246,14 @@ protected async Task StartUseCase(CancellationToken token)
265246 await StartCameraPreview ( token ) ;
266247 }
267248
268- protected virtual async partial Task PlatformStartCameraPreview ( CancellationToken token )
249+ private async partial Task PlatformStartCameraPreview ( CancellationToken token )
269250 {
270251 if ( previewView is null || processCameraProvider is null || cameraPreview is null || imageCapture is null || videoCapture is null )
271252 {
272253 return ;
273254 }
274255
275- if ( cameraView . SelectedCamera is null )
276- {
277- if ( cameraProvider . AvailableCameras is null )
278- {
279- await cameraProvider . RefreshAvailableCameras ( token ) ;
280- }
281-
282- cameraView . SelectedCamera = cameraProvider . AvailableCameras ? . FirstOrDefault ( ) ?? throw new CameraException ( "No camera available on device" ) ;
283- }
256+ cameraView . SelectedCamera ??= cameraProvider . AvailableCameras ? . FirstOrDefault ( ) ?? throw new CameraException ( "No camera available on device" ) ;
284257
285258 camera = await RebindCamera ( processCameraProvider , cameraView . SelectedCamera , token , cameraPreview , imageCapture , videoCapture ) ;
286259 cameraControl = camera . CameraControl ;
@@ -293,7 +266,7 @@ protected virtual async partial Task PlatformStartCameraPreview(CancellationToke
293266 OnLoaded . Invoke ( ) ;
294267 }
295268
296- protected virtual partial void PlatformStopCameraPreview ( )
269+ private partial void PlatformStopCameraPreview ( )
297270 {
298271 if ( processCameraProvider is null )
299272 {
@@ -304,11 +277,11 @@ protected virtual partial void PlatformStopCameraPreview()
304277 IsInitialized = false ;
305278 }
306279
307- protected virtual partial void PlatformDisconnect ( )
280+ private partial void PlatformDisconnect ( )
308281 {
309282 }
310283
311- protected virtual partial ValueTask PlatformTakePicture ( CancellationToken token )
284+ private partial ValueTask PlatformTakePicture ( CancellationToken token )
312285 {
313286 ArgumentNullException . ThrowIfNull ( cameraExecutor ) ;
314287 ArgumentNullException . ThrowIfNull ( imageCallback ) ;
@@ -317,7 +290,7 @@ protected virtual partial ValueTask PlatformTakePicture(CancellationToken token)
317290 return ValueTask . CompletedTask ;
318291 }
319292
320- protected virtual async partial Task PlatformStartVideoRecording ( Stream stream , CancellationToken token )
293+ private async partial Task PlatformStartVideoRecording ( Stream stream , CancellationToken token )
321294 {
322295 if ( previewView is null
323296 || processCameraProvider is null
@@ -332,15 +305,7 @@ protected virtual async partial Task PlatformStartVideoRecording(Stream stream,
332305
333306 videoRecordingStream = stream ;
334307
335- if ( cameraView . SelectedCamera is null )
336- {
337- if ( cameraProvider . AvailableCameras is null )
338- {
339- await cameraProvider . RefreshAvailableCameras ( token ) ;
340- }
341-
342- cameraView . SelectedCamera = cameraProvider . AvailableCameras ? . FirstOrDefault ( ) ?? throw new CameraException ( "No camera available on device" ) ;
343- }
308+ cameraView . SelectedCamera ??= cameraProvider . AvailableCameras ? . FirstOrDefault ( ) ?? throw new CameraException ( "No camera available on device" ) ;
344309
345310 if ( camera is null || ! IsVideoCaptureAlreadyBound ( ) )
346311 {
@@ -367,7 +332,7 @@ protected virtual async partial Task PlatformStartVideoRecording(Stream stream,
367332 // https://developer.android.com/reference/androidx/camera/video/Recorder#prepareRecording(android.content.Context,androidx.camera.video.MediaStoreOutputOptions)
368333 }
369334
370- protected virtual async partial Task < Stream > PlatformStopVideoRecording ( CancellationToken token )
335+ private async partial Task < Stream > PlatformStopVideoRecording ( CancellationToken token )
371336 {
372337 ArgumentNullException . ThrowIfNull ( cameraExecutor ) ;
373338 if ( videoRecording is null
0 commit comments