• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the
11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12  * KIND, either express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 package com.android.systemui.unfold.updates.hinge
16 
17 import android.hardware.Sensor
18 import android.hardware.SensorEvent
19 import android.hardware.SensorEventListener
20 import android.hardware.SensorManager
21 import android.os.Trace
22 import androidx.core.util.Consumer
23 import com.android.systemui.unfold.dagger.UnfoldSingleThreadBg
24 import java.util.concurrent.Executor
25 import javax.inject.Inject
26 
27 internal class HingeSensorAngleProvider
28 @Inject
29 constructor(
30     private val sensorManager: SensorManager,
31     @UnfoldSingleThreadBg private val singleThreadBgExecutor: Executor
32 ) : HingeAngleProvider {
33 
34     private val sensorListener = HingeAngleSensorListener()
35     private val listeners: MutableList<Consumer<Float>> = arrayListOf()
36     var started = false
37 
startnull38     override fun start() {
39         singleThreadBgExecutor.execute {
40             if (started) return@execute
41             Trace.beginSection("HingeSensorAngleProvider#start")
42             val sensor = sensorManager.getDefaultSensor(Sensor.TYPE_HINGE_ANGLE)
43             sensorManager.registerListener(
44                 sensorListener,
45                 sensor,
46                 SensorManager.SENSOR_DELAY_FASTEST
47             )
48             Trace.endSection()
49 
50             started = true
51         }
52     }
53 
stopnull54     override fun stop() {
55         singleThreadBgExecutor.execute {
56             if (!started) return@execute
57             sensorManager.unregisterListener(sensorListener)
58             started = false
59         }
60     }
61 
removeCallbacknull62     override fun removeCallback(listener: Consumer<Float>) {
63         listeners.remove(listener)
64     }
65 
addCallbacknull66     override fun addCallback(listener: Consumer<Float>) {
67         listeners.add(listener)
68     }
69 
70     private inner class HingeAngleSensorListener : SensorEventListener {
71 
onAccuracyChangednull72         override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {}
73 
onSensorChangednull74         override fun onSensorChanged(event: SensorEvent) {
75             listeners.forEach { it.accept(event.values[0]) }
76         }
77     }
78 }
79