-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHingeAngleSensor.java
More file actions
100 lines (86 loc) · 3.15 KB
/
Copy pathHingeAngleSensor.java
File metadata and controls
100 lines (86 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package com.unity.lostcryptlargescreenexample;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static androidx.core.content.ContextCompat.getSystemService;
public final class HingeAngleSensor {
private static final String HINGE_ANGLE_SENSOR_NAME = "Hinge Angle".toLowerCase(); // works on some other devices/emulators, use sensor.getName()
private SensorManager mSensorManager;
private Sensor mHingeAngleSensor;
private SensorEventListener mSensorListener;
private float lastValue = -1f;
public void setupSensor() {
if (mHingeAngleSensor == null) {
// we haven't set it yet!
List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
for (Sensor sensor : sensorList) {
if (sensor.getName().toLowerCase().contains(HINGE_ANGLE_SENSOR_NAME)) {
mHingeAngleSensor = sensor;
}
}
mSensorListener = new SensorEventListener() {
@Override
public void onSensorChanged(final SensorEvent event) {
if (event.sensor == mHingeAngleSensor) {
lastValue = event.values[0];
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
//TODO: support accuracy change
}
};
mSensorManager.registerListener(mSensorListener, mHingeAngleSensor, SensorManager.SENSOR_DELAY_GAME);
}
}
/*
* Get the last measured hinge angle value (-2 if a problem occurred)
*/
public float getHingeAngle() {
if (mSensorListener == null) {
return -2f;
}
return lastValue;
}
public int isHingeEnabled() {
return mSensorListener != null ? 1 : 0;
}
/*
* Create the hinge angle sensor class (singleton)
*/
public static HingeAngleSensor getInstance(Activity currentActivity) {
// double check synchronization
HingeAngleSensor result = instance;
if (result == null) {
synchronized (SINGLETON_LOCK) {
result = instance;
if (result == null) {
result = new HingeAngleSensor();
result.mSensorManager = (SensorManager) currentActivity.getSystemService(Context.SENSOR_SERVICE);
instance = result;
}
}
}
return result;
}
private static final Object SINGLETON_LOCK = new Object();
private static volatile HingeAngleSensor instance;
private HingeAngleSensor() {
// Singleton
}
/*
* Unregister listener - must be called by Unity
*/
public void dispose() {
mSensorManager.unregisterListener(mSensorListener);
}
}