mirror of
https://github.com/chararomandroid/android_device_samsung_a20
synced 2026-08-23 12:35:33 -04:00
universal7885: Add aidl for interacting with EKCamera
Also address deprecation
This commit is contained in:
parent
1a96e0b036
commit
df3f4dea62
3 changed files with 53 additions and 44 deletions
|
|
@ -2,6 +2,7 @@ android_app {
|
||||||
name: "CameraLightSensor",
|
name: "CameraLightSensor",
|
||||||
srcs: [
|
srcs: [
|
||||||
"src/**/*.kt",
|
"src/**/*.kt",
|
||||||
|
"src/**/I*.aidl",
|
||||||
],
|
],
|
||||||
platform_apis: true,
|
platform_apis: true,
|
||||||
privileged: true,
|
privileged: true,
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,7 @@ package com.eurekateam.cameralightsensor
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.app.*
|
import android.app.*
|
||||||
import android.content.BroadcastReceiver
|
import android.content.*
|
||||||
import android.content.Context
|
|
||||||
import android.content.Intent
|
|
||||||
import android.content.IntentFilter
|
|
||||||
import android.content.pm.ServiceInfo
|
import android.content.pm.ServiceInfo
|
||||||
import android.database.ContentObserver
|
import android.database.ContentObserver
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
|
|
@ -13,6 +10,8 @@ import android.graphics.BitmapFactory
|
||||||
import android.graphics.Color
|
import android.graphics.Color
|
||||||
import android.graphics.ImageFormat
|
import android.graphics.ImageFormat
|
||||||
import android.hardware.camera2.*
|
import android.hardware.camera2.*
|
||||||
|
import android.hardware.camera2.params.OutputConfiguration
|
||||||
|
import android.hardware.camera2.params.SessionConfiguration
|
||||||
import android.media.Image
|
import android.media.Image
|
||||||
import android.media.ImageReader
|
import android.media.ImageReader
|
||||||
import android.os.*
|
import android.os.*
|
||||||
|
|
@ -20,25 +19,29 @@ import android.provider.Settings
|
||||||
import android.provider.Settings.SettingNotFoundException
|
import android.provider.Settings.SettingNotFoundException
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
|
import java.util.concurrent.Executors
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor
|
import java.util.concurrent.ScheduledThreadPoolExecutor
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
class CameraLightSensorService : Service() {
|
class CameraLightSensorService : Service() {
|
||||||
private lateinit var screenStateFilter: IntentFilter
|
private lateinit var screenStateFilter: IntentFilter
|
||||||
lateinit var mContext: Context
|
private lateinit var mContext: Context
|
||||||
private var cameraDevice: CameraDevice? = null
|
private lateinit var cameraDevice: CameraDevice
|
||||||
private var session: CameraCaptureSession? = null
|
private lateinit var mSession: CameraCaptureSession
|
||||||
private lateinit var manager: CameraManager
|
private val manager: CameraManager by lazy {
|
||||||
|
getSystemService(CAMERA_SERVICE) as CameraManager
|
||||||
|
}
|
||||||
private lateinit var mCameraHandler: Handler
|
private lateinit var mCameraHandler: Handler
|
||||||
|
private val mExecutor = Executors.newSingleThreadExecutor()
|
||||||
private fun pushNotification(): Notification {
|
private fun pushNotification(): Notification {
|
||||||
val nm = mContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
val nm = mContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||||
val channel = NotificationChannel(
|
val channel = NotificationChannel(
|
||||||
mContext.basePackageName, "CameraLightSensor",
|
mContext.packageName, "CameraLightSensor",
|
||||||
NotificationManager.IMPORTANCE_NONE
|
NotificationManager.IMPORTANCE_NONE
|
||||||
)
|
)
|
||||||
channel.isBlockable = true
|
channel.isBlockable = true
|
||||||
nm.createNotificationChannel(channel)
|
nm.createNotificationChannel(channel)
|
||||||
val builder = NotificationCompat.Builder(mContext, mContext.basePackageName)
|
val builder = NotificationCompat.Builder(mContext, mContext.packageName)
|
||||||
val notificationIntent = Intent(mContext, CameraLightSensorService::class.java)
|
val notificationIntent = Intent(mContext, CameraLightSensorService::class.java)
|
||||||
val contentIntent = PendingIntent.getActivity(
|
val contentIntent = PendingIntent.getActivity(
|
||||||
mContext, 50,
|
mContext, 50,
|
||||||
|
|
@ -48,7 +51,7 @@ class CameraLightSensorService : Service() {
|
||||||
builder.setContentIntent(contentIntent)
|
builder.setContentIntent(contentIntent)
|
||||||
builder.setSmallIcon(R.drawable.ic_brightness)
|
builder.setSmallIcon(R.drawable.ic_brightness)
|
||||||
builder.setContentTitle("Camera Light Sensor Service")
|
builder.setContentTitle("Camera Light Sensor Service")
|
||||||
builder.setChannelId(mContext.basePackageName)
|
builder.setChannelId(mContext.packageName)
|
||||||
return builder.build()
|
return builder.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,6 +61,7 @@ class CameraLightSensorService : Service() {
|
||||||
mRegistered = false
|
mRegistered = false
|
||||||
imageReader.close()
|
imageReader.close()
|
||||||
stopForeground(true)
|
stopForeground(true)
|
||||||
|
unbindService(mConnection)
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,6 +69,18 @@ class CameraLightSensorService : Service() {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private var mIAutoBrightness: IAutoBrightness? = null
|
||||||
|
|
||||||
|
private val mConnection = object : ServiceConnection {
|
||||||
|
override fun onServiceConnected(p0: ComponentName?, service: IBinder?) {
|
||||||
|
mIAutoBrightness = IAutoBrightness.Stub.asInterface(service)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onServiceDisconnected(p0: ComponentName?) {
|
||||||
|
mIAutoBrightness = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private val mScreenStateReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
private val mScreenStateReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context, intent: Intent) {
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
if (intent.action == Intent.ACTION_USER_PRESENT) {
|
if (intent.action == Intent.ACTION_USER_PRESENT) {
|
||||||
|
|
@ -141,17 +157,17 @@ class CameraLightSensorService : Service() {
|
||||||
private var sessionStateCallback: CameraCaptureSession.StateCallback =
|
private var sessionStateCallback: CameraCaptureSession.StateCallback =
|
||||||
object : CameraCaptureSession.StateCallback() {
|
object : CameraCaptureSession.StateCallback() {
|
||||||
override fun onReady(session: CameraCaptureSession) {
|
override fun onReady(session: CameraCaptureSession) {
|
||||||
this@CameraLightSensorService.session = session
|
this@CameraLightSensorService.mSession = session
|
||||||
try {
|
try {
|
||||||
if (createCaptureRequest() == null) return
|
if (createCaptureRequest() == null) return
|
||||||
try {
|
try {
|
||||||
session.capture(createCaptureRequest(), null, mCameraHandler)
|
session.capture(createCaptureRequest()!!, null, mCameraHandler)
|
||||||
} catch (e: CameraAccessException) {
|
} catch (e: CameraAccessException) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
} catch (e: IllegalStateException) {
|
} catch (e: IllegalStateException) {
|
||||||
Log.w(TAG, "onReady: Session is NULL")
|
Log.w(TAG, "onReady: Session is NULL")
|
||||||
}
|
}
|
||||||
cameraDevice!!.close()
|
cameraDevice.close()
|
||||||
session.close()
|
session.close()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Camera is in use")
|
Log.e(TAG, "Camera is in use")
|
||||||
|
|
@ -179,30 +195,16 @@ class CameraLightSensorService : Service() {
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
fun readyCamera() {
|
fun readyCamera() {
|
||||||
manager = getSystemService(CAMERA_SERVICE) as CameraManager
|
if (mIAutoBrightness != null){
|
||||||
|
if (!mIAutoBrightness!!.CameraIsFree()) return
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
val pickedCamera = getCamera(manager)
|
manager.openCamera("1", cameraStateCallback, mCameraHandler)
|
||||||
manager.openCamera(pickedCamera, cameraStateCallback, mCameraHandler)
|
|
||||||
imageReader.setOnImageAvailableListener(onImageAvailableListener, mCameraHandler)
|
imageReader.setOnImageAvailableListener(onImageAvailableListener, mCameraHandler)
|
||||||
if (DEBUG) Log.d(TAG, "imageReader created")
|
if (DEBUG) Log.d(TAG, "imageReader created")
|
||||||
} catch (e: CameraAccessException) {
|
|
||||||
Log.e(TAG, e.message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getCamera(manager: CameraManager?): String? {
|
|
||||||
try {
|
|
||||||
for (cameraId in manager!!.cameraIdList) {
|
|
||||||
val characteristics = manager.getCameraCharacteristics(cameraId)
|
|
||||||
val cOrientation = characteristics.get(CameraCharacteristics.LENS_FACING)
|
|
||||||
if (cOrientation == CAMERA_CHOICE) {
|
|
||||||
return cameraId
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e: CameraAccessException) {
|
} catch (e: CameraAccessException) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
|
|
@ -210,6 +212,9 @@ class CameraLightSensorService : Service() {
|
||||||
mCameraHandlerThread.start()
|
mCameraHandlerThread.start()
|
||||||
mCameraHandler = Handler(mCameraHandlerThread.looper)
|
mCameraHandler = Handler(mCameraHandlerThread.looper)
|
||||||
mContext = this
|
mContext = this
|
||||||
|
bindService(Intent(mContext, IAutoBrightness::class.java).apply {
|
||||||
|
setClassName("com.eurekateam.camera", "CameraAIDL")
|
||||||
|
}, mConnection, Context.BIND_AUTO_CREATE)
|
||||||
@Suppress("SameParameterValue")
|
@Suppress("SameParameterValue")
|
||||||
startForeground(50, pushNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA)
|
startForeground(50, pushNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA)
|
||||||
mRegistered = false
|
mRegistered = false
|
||||||
|
|
@ -241,13 +246,11 @@ class CameraLightSensorService : Service() {
|
||||||
|
|
||||||
fun actOnReadyCameraDevice() {
|
fun actOnReadyCameraDevice() {
|
||||||
try {
|
try {
|
||||||
cameraDevice!!.createCaptureSession(
|
cameraDevice.createCaptureSession(
|
||||||
listOf(imageReader.surface),
|
SessionConfiguration(SessionConfiguration.SESSION_REGULAR,
|
||||||
sessionStateCallback,
|
listOf(OutputConfiguration(imageReader.surface)), mExecutor, sessionStateCallback))
|
||||||
mCameraHandler
|
|
||||||
)
|
|
||||||
} catch (e: CameraAccessException) {
|
} catch (e: CameraAccessException) {
|
||||||
Log.e(TAG, e.message)
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -261,13 +264,13 @@ class CameraLightSensorService : Service() {
|
||||||
adjustBrightness(brightness)
|
adjustBrightness(brightness)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun createCaptureRequest(): CaptureRequest? {
|
fun createCaptureRequest(): CaptureRequest? {
|
||||||
return try {
|
return try {
|
||||||
val builder = cameraDevice!!.createCaptureRequest(CameraDevice.TEMPLATE_RECORD)
|
val builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD)
|
||||||
builder.addTarget(imageReader!!.surface)
|
builder.addTarget(imageReader.surface)
|
||||||
builder.build()
|
builder.build()
|
||||||
} catch (e: CameraAccessException) {
|
} catch (e: CameraAccessException) {
|
||||||
Log.e(TAG, e.message)
|
e.printStackTrace()
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -318,9 +321,8 @@ class CameraLightSensorService : Service() {
|
||||||
companion object {
|
companion object {
|
||||||
private val imageReader =
|
private val imageReader =
|
||||||
ImageReader.newInstance(50, 50, ImageFormat.JPEG, 2 /* images buffered */)
|
ImageReader.newInstance(50, 50, ImageFormat.JPEG, 2 /* images buffered */)
|
||||||
protected val TAG: String = CameraLightSensorService::class.java.simpleName
|
val TAG: String = CameraLightSensorService::class.java.simpleName
|
||||||
const val DEBUG = false
|
const val DEBUG = false
|
||||||
const val CAMERA_CHOICE = CameraCharacteristics.LENS_FACING_FRONT
|
|
||||||
private var mPoolExecutor: ScheduledThreadPoolExecutor? = null
|
private var mPoolExecutor: ScheduledThreadPoolExecutor? = null
|
||||||
private var mRegistered = false
|
private var mRegistered = false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
// IAutoBrightness.aidl
|
||||||
|
package com.eurekateam.cameralightsensor;
|
||||||
|
|
||||||
|
interface IAutoBrightness {
|
||||||
|
boolean CameraIsFree();
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue