-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathDW.Sensor.Android.pas
More file actions
232 lines (206 loc) · 6.82 KB
/
DW.Sensor.Android.pas
File metadata and controls
232 lines (206 loc) · 6.82 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
unit DW.Sensor.Android;
interface
uses
Androidapi.JNIBridge,
DW.Androidapi.JNI.SensorManager, DW.Sensor;
type
TPlatformSensor = class;
TSensorEventListener = class(TJavaLocal, JSensorEventListener)
private
FPlatformSensor: TPlatformSensor;
public
{ JSensorEventListener }
procedure onAccuracyChanged(sensor: JSensor; accuracy: Integer); cdecl;
procedure onSensorChanged(event: JSensorEvent); cdecl;
public
constructor Create(const APlatformSensor: TPlatformSensor);
end;
TPlatformSensor = class(TCustomPlatformSensor)
private
class var FSensorManager: JSensorManager;
class function GetDefaultSensor(const AType: Integer): JSensor;
class function GetPlatformSensorType(const ASensorType: TSensorType): Integer;
class function SensorManager: JSensorManager;
private
FIsActive: Boolean;
FListener: JSensorEventListener;
FSensorStart: TDateTime;
FTimestamp: Int64;
protected
procedure AccuracyChanged(sensor: JSensor; accuracy: Integer);
function GetIsActive: Boolean; override;
procedure SensorChanged(event: JSensorEvent);
procedure SetIsActive(const Value: Boolean); override;
procedure SetSensorType(const Value: TSensorType); override;
public
class function IsSensorTypeSupported(const ASensorType: TSensorType): Boolean;
public
constructor Create(const ASensor: TSensor); override;
destructor Destroy; override;
end;
implementation
uses
System.DateUtils, System.SysUtils,
Androidapi.JNI.JavaTypes, Androidapi.Helpers, AndroidApi.JNI.GraphicsContentViewText, Androidapi.JNI,
DW.Androidapi.JNI.System;
{ TSensorEventListener }
constructor TSensorEventListener.Create(const APlatformSensor: TPlatformSensor);
begin
inherited Create;
FPlatformSensor := APlatformSensor;
end;
procedure TSensorEventListener.onAccuracyChanged(sensor: JSensor; accuracy: Integer);
begin
FPlatformSensor.AccuracyChanged(sensor, accuracy);
end;
procedure TSensorEventListener.onSensorChanged(event: JSensorEvent);
begin
FPlatformSensor.SensorChanged(event);
end;
{ TPlatformSensor }
constructor TPlatformSensor.Create(const ASensor: TSensor);
begin
inherited;
FListener := TSensorEventListener.Create(Self);
end;
destructor TPlatformSensor.Destroy;
begin
FListener := nil;
inherited;
end;
class function TPlatformSensor.GetDefaultSensor(const AType: Integer): JSensor;
begin
Result := nil;
if AType <> -1 then
Result := SensorManager.getDefaultSensor(AType);
end;
function TPlatformSensor.GetIsActive: Boolean;
begin
Result := FIsActive;
end;
class function TPlatformSensor.GetPlatformSensorType(const ASensorType: TSensorType): Integer;
begin
Result := -1;
case ASensorType of
TSensorType.Accelerometer:
Result := TJSensor.JavaClass.TYPE_ACCELEROMETER;
TSensorType.AmbientTemperature:
Result := TJSensor.JavaClass.TYPE_AMBIENT_TEMPERATURE;
TSensorType.GameRotation:
Result := TJSensor.JavaClass.TYPE_GAME_ROTATION_VECTOR;
TSensorType.GeomagneticRotation:
Result := TJSensor.JavaClass.TYPE_GEOMAGNETIC_ROTATION_VECTOR;
TSensorType.Gravity:
Result := TJSensor.JavaClass.TYPE_GRAVITY;
TSensorType.Gyroscope:
Result := TJSensor.JavaClass.TYPE_GYROSCOPE;
TSensorType.HeartRate:
Result := TJSensor.JavaClass.TYPE_HEART_RATE;
TSensorType.Light:
Result := TJSensor.JavaClass.TYPE_LIGHT;
TSensorType.LinearAcceleration:
Result := TJSensor.JavaClass.TYPE_LINEAR_ACCELERATION;
TSensorType.MagneticField:
Result := TJSensor.JavaClass.TYPE_MAGNETIC_FIELD;
TSensorType.Orientation:
Result := TJSensor.JavaClass.TYPE_ORIENTATION;
TSensorType.Pressure:
Result := TJSensor.JavaClass.TYPE_PRESSURE;
TSensorType.Proximity:
Result := TJSensor.JavaClass.TYPE_PROXIMITY;
TSensorType.RelativeHumidity:
Result := TJSensor.JavaClass.TYPE_RELATIVE_HUMIDITY;
TSensorType.Rotation:
Result := TJSensor.JavaClass.TYPE_ROTATION_VECTOR;
TSensorType.SignificantMotion:
Result := TJSensor.JavaClass.TYPE_SIGNIFICANT_MOTION;
TSensorType.StepCounter:
Result := TJSensor.JavaClass.TYPE_STEP_COUNTER;
TSensorType.StepDetector:
Result := TJSensor.JavaClass.TYPE_STEP_DETECTOR;
TSensorType.Temperature:
Result := TJSensor.JavaClass.TYPE_TEMPERATURE;
end;
end;
class function TPlatformSensor.IsSensorTypeSupported(const ASensorType: TSensorType): Boolean;
begin
Result := GetDefaultSensor(GetPlatformSensorType(ASensorType)) <> nil;
end;
procedure TPlatformSensor.AccuracyChanged(sensor: JSensor; accuracy: Integer);
begin
//
end;
procedure TPlatformSensor.SensorChanged(event: JSensorEvent);
var
LValues: TSensorValues;
I: Integer;
LMillis: Int64;
LEventValues: TJavaArray<Single>;
begin
LEventValues := event.values;
try
SetLength(LValues, LEventValues.Length);
for I := 0 to LEventValues.Length - 1 do
LValues[I] := LEventValues[I];
finally
LEventValues.Free; // <-- Oct20: fix JNI memory leak that leads to app crash
end;
if FTimestamp = 0 then
begin
FSensorStart := Now;
LMillis := 0;
end
else
LMillis := (event.timestamp - FTimestamp) div 1000000;
ValuesChanged(LValues, IncMilliSecond(FSensorStart, LMillis));
// SetLength(LValues, 0); //Om: oct20: test
end;
class function TPlatformSensor.SensorManager: JSensorManager;
var
LService: JObject;
begin
if FSensorManager = nil then
begin
LService := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.SENSOR_SERVICE);
FSensorManager := TJSensorManager.Wrap((LService as ILocalObject).GetObjectID);
end;
Result := FSensorManager;
end;
procedure TPlatformSensor.SetIsActive(const Value: Boolean);
var
LSensor: JSensor;
begin
if Value = FIsActive then
Exit; // <======
if Value then
begin
LSensor := GetDefaultSensor(GetPlatformSensorType(FSensorType));
if LSensor <> nil then
begin
// Om: test: changed delay to try to evoid error in global reference table...
SensorManager.registerListener(FListener, LSensor, TJSensorManager.JavaClass.SENSOR_DELAY_NORMAL); //!!!! (Sampling period)
// SensorManager.registerListener(FListener, LSensor, TJSensorManager.JavaClass.SENSOR_DELAY_UI); //!!!! (Sampling period)
FIsActive := True;
end
else
FIsActive := False;
end
else
begin
SensorManager.unregisterListener(FListener);
FIsActive := False;
end;
end;
procedure TPlatformSensor.SetSensorType(const Value: TSensorType);
var
LIsActive: Boolean;
begin
if Value = FSensorType then
Exit; // <======
LIsActive := GetIsActive;
SetIsActive(False);
FSensorType := Value;
FTimestamp := 0;
SetIsActive(LIsActive);
end;
end.