From b5bf7540cff002f2fd9d903d42a80eb50cf75d34 Mon Sep 17 00:00:00 2001 From: CraZyLegenD Date: Sat, 9 Jan 2021 20:25:06 +0100 Subject: [PATCH] Closes https://github.com/FunkyMuse/Vigilante/issues/64 --- app/build.gradle | 3 ++ .../vigilante/camera/CameraProcessor.kt | 33 +++++++++++++++---- build.gradle | 1 + 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c08cf4c7..faf7184b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -123,6 +123,9 @@ dependencies { implementation "androidx.room:room-ktx:$room_version" kapt "androidx.room:room-compiler:$room_version" + //cameraX + implementation "androidx.camera:camera-camera2:$camerax_version" + //paging implementation "androidx.paging:paging-runtime-ktx:$paging_version" diff --git a/app/src/main/java/com/crazylegend/vigilante/camera/CameraProcessor.kt b/app/src/main/java/com/crazylegend/vigilante/camera/CameraProcessor.kt index abb42e25..19fef8ec 100644 --- a/app/src/main/java/com/crazylegend/vigilante/camera/CameraProcessor.kt +++ b/app/src/main/java/com/crazylegend/vigilante/camera/CameraProcessor.kt @@ -1,9 +1,12 @@ package com.crazylegend.vigilante.camera +import android.annotation.SuppressLint import android.content.Context import android.hardware.camera2.CameraManager +import androidx.camera.camera2.internal.compat.CameraManagerCompat import androidx.lifecycle.ServiceLifecycleDispatcher -import com.crazylegend.kotlinextensions.context.cameraManager +import androidx.lifecycle.coroutineScope +import com.crazylegend.coroutines.mainDispatcher import com.crazylegend.kotlinextensions.context.notificationManager import com.crazylegend.kotlinextensions.ifTrue import com.crazylegend.vigilante.R @@ -13,17 +16,23 @@ import com.crazylegend.vigilante.di.providers.UserNotificationsProvider import com.crazylegend.vigilante.di.qualifiers.ServiceContext import com.crazylegend.vigilante.service.VigilanteService import dagger.hilt.android.scopes.ServiceScoped +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch +import java.util.concurrent.Executors import javax.inject.Inject /** * Created by crazy on 10/15/20 to long live and prosper ! */ @ServiceScoped +@SuppressLint("RestrictedApi") class CameraProcessor @Inject constructor( @ServiceContext private val context: Context, private val userNotificationsProvider: UserNotificationsProvider, private val prefsProvider: PrefsProvider) : ServiceManagersCoroutines { + private lateinit var manager: CameraManagerCompat + //lifecycle override val serviceLifecycleDispatcher = ServiceLifecycleDispatcher(this) @@ -33,14 +42,15 @@ class CameraProcessor @Inject constructor( override fun initVars() { cameraCallback = cameraListener() + manager = CameraManagerCompat.from(context) } override fun registerCallbacks() { - context.cameraManager.registerAvailabilityCallback(cameraCallback, null) + manager.registerAvailabilityCallback(Executors.newSingleThreadExecutor(), cameraCallback) } override fun disposeResources() { - context.cameraManager.unregisterAvailabilityCallback(cameraCallback) + manager.unregisterAvailabilityCallback(cameraCallback) } //region private @@ -48,13 +58,17 @@ class CameraProcessor @Inject constructor( object : CameraManager.AvailabilityCallback() { override fun onCameraAvailable(cameraId: String) { super.onCameraAvailable(cameraId) - setCameraNotUsed() - stopNotificationIfUserEnabled() + serviceLifecycleDispatcher.updateUI { + setCameraNotUsed() + stopNotificationIfUserEnabled() + } } override fun onCameraUnavailable(cameraId: String) { super.onCameraUnavailable(cameraId) - setCameraUsed() + serviceLifecycleDispatcher.updateUI { + setCameraUsed() + } } } @@ -75,6 +89,10 @@ class CameraProcessor @Inject constructor( } } + private inline fun ServiceLifecycleDispatcher.updateUI(crossinline function: () -> Unit) = + lifecycle.coroutineScope.launch(mainDispatcher + SupervisorJob()) { + function() + } private fun setCameraNotUsed() { VigilanteService.serviceLayoutListener?.hideCamera() @@ -82,4 +100,5 @@ class CameraProcessor @Inject constructor( //endregion -} \ No newline at end of file +} + diff --git a/build.gradle b/build.gradle index d2230cfc..7a63a55d 100644 --- a/build.gradle +++ b/build.gradle @@ -11,6 +11,7 @@ ext { crashyVersion = '1.0.9' room_version = "2.3.0-alpha04" paging_version = "3.0.0-alpha11" + camerax_version = '1.0.0-rc01' } buildscript {