Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
Closes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
CraZyLegenD committed Jan 9, 2021
1 parent 347445f commit b5bf754
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -33,28 +42,33 @@ 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
private fun cameraListener(): CameraManager.AvailabilityCallback =
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()
}
}
}

Expand All @@ -75,11 +89,16 @@ class CameraProcessor @Inject constructor(
}
}

private inline fun ServiceLifecycleDispatcher.updateUI(crossinline function: () -> Unit) =
lifecycle.coroutineScope.launch(mainDispatcher + SupervisorJob()) {
function()
}

private fun setCameraNotUsed() {
VigilanteService.serviceLayoutListener?.hideCamera()
}
//endregion


}
}

1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b5bf754

Please sign in to comment.