Skip to content

Commit 14ae49c

Browse files
authored
Merge pull request #9 from holzgeist/feat-add-proximity-screen-off
Feat add proximity screen off
2 parents 67c27a0 + 0e19ea4 commit 14ae49c

File tree

9 files changed

+173
-50
lines changed

9 files changed

+173
-50
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@ Regarding permissions, you may need the following settings in your `AndroidManif
3434
<uses-permission android:name="android.hardware.sensor.proximity"/>
3535
<uses-permission android:name="android.permission.BODY_SENSORS_BACKGROUND"/>
3636
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
37+
<uses-permission android:name="android.permission.WAKE_LOCK"/>
38+
39+
NB: the WAKE_LOCK permission is only needed if you run `setProximityScreenOff(true)` before listening to events
3740

3841
**Some recent devices use virtual proximity sensors. There are no physical sensors. I found it hard to trust the sensor information in this case.**

android/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<uses-permission android:name="android.hardware.sensor.proximity"/>
44
<uses-permission android:name="android.permission.BODY_SENSORS_BACKGROUND"/>
55
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
6+
<uses-permission android:name="android.permission.WAKE_LOCK"/>
67
</manifest>

android/src/main/kotlin/dev/jeremyko/proximity_sensor/ProximitySensorPlugin.kt

+27-9
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11

22
package dev.jeremyko.proximity_sensor
33

4-
import androidx.annotation.NonNull
54
import io.flutter.embedding.engine.plugins.FlutterPlugin
65
import io.flutter.plugin.common.EventChannel
6+
import io.flutter.plugin.common.MethodCall
7+
import io.flutter.plugin.common.MethodChannel
8+
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
9+
import io.flutter.plugin.common.MethodChannel.Result
710

811
////////////////////////////////////////////////////////////////////////////////////////////////////
9-
class ProximitySensorPlugin: FlutterPlugin {
10-
private lateinit var channel : EventChannel
12+
class ProximitySensorPlugin: FlutterPlugin, MethodCallHandler {
13+
private lateinit var methodChannel: MethodChannel
14+
private lateinit var eventChannel : EventChannel
1115
private lateinit var streamHandler : ProximityStreamHandler
1216

13-
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
14-
channel = EventChannel(flutterPluginBinding.binaryMessenger, "proximity_sensor")
15-
streamHandler = ProximityStreamHandler( flutterPluginBinding.applicationContext,
16-
flutterPluginBinding.binaryMessenger)
17-
channel.setStreamHandler(streamHandler)
17+
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
18+
methodChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "proximity_sensor_enable")
19+
methodChannel.setMethodCallHandler(this)
20+
21+
eventChannel = EventChannel(flutterPluginBinding.binaryMessenger, "proximity_sensor")
22+
streamHandler = ProximityStreamHandler( flutterPluginBinding.applicationContext)
23+
eventChannel.setStreamHandler(streamHandler)
1824
}
1925

2026
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
21-
channel.setStreamHandler(null)
27+
eventChannel.setStreamHandler(null)
28+
}
29+
30+
override fun onMethodCall(call: MethodCall, result: Result) {
31+
if (call.method == "enableProximityScreenOff") {
32+
val enabled: Boolean? = call.argument("enabled")
33+
if (enabled == null) {
34+
result.error("INVALID_ARGUMENTS", "'enabled' cannot be null", null)
35+
} else {
36+
streamHandler.setScreenOffEnabled(enabled)
37+
result.success(null);
38+
}
39+
}
2240
}
2341
}
2442

android/src/main/kotlin/dev/jeremyko/proximity_sensor/ProximityStreamHandler.kt

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,55 @@
11
package dev.jeremyko.proximity_sensor
22

3+
import android.annotation.SuppressLint
34
import android.content.Context
45
import android.hardware.Sensor
56
import android.hardware.SensorEvent
67
import android.hardware.SensorEventListener
78
import android.hardware.SensorManager
8-
import io.flutter.plugin.common.BinaryMessenger
9+
import android.os.Build
10+
import android.os.PowerManager
911
import io.flutter.plugin.common.EventChannel
10-
import java.io.IOException
1112
import java.lang.UnsupportedOperationException
1213

1314
////////////////////////////////////////////////////////////////////////////////////////////////////
1415
class ProximityStreamHandler(
1516
private val applicationContext: Context,
16-
private val messenger: BinaryMessenger
1717
): EventChannel.StreamHandler, SensorEventListener {
1818

1919
private var eventSink: EventChannel.EventSink? = null
2020
private lateinit var sensorManager: SensorManager
2121
private var proximitySensor: Sensor? = null
2222

23+
private lateinit var powerManager: PowerManager
24+
private var wakeLock: PowerManager.WakeLock? = null;
25+
private var enableScreenOff: Boolean = false;
26+
27+
@SuppressLint("WakelockTimeout")
2328
override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
2429
eventSink = events
2530
sensorManager = applicationContext.getSystemService(Context.SENSOR_SERVICE) as SensorManager
2631
proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY) ?:
2732
throw UnsupportedOperationException("proximity sensor unavailable")
2833

2934
sensorManager.registerListener(this, proximitySensor, SensorManager.SENSOR_DELAY_NORMAL)
35+
powerManager = applicationContext.getSystemService(Context.POWER_SERVICE) as
36+
PowerManager
37+
38+
if (enableScreenOff && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
39+
if (wakeLock == null) {
40+
wakeLock = powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "dev.jeremyko.proximity_sensor:lock")
41+
}
42+
if (!wakeLock!!.isHeld) {
43+
wakeLock!!.acquire()
44+
}
45+
}
3046
}
3147

3248
override fun onCancel(arguments: Any?) {
3349
sensorManager.unregisterListener(this, proximitySensor)
50+
if (wakeLock != null && wakeLock!!.isHeld) {
51+
wakeLock!!.release()
52+
}
3453
}
3554

3655
override fun onSensorChanged(event: SensorEvent?) {
@@ -49,4 +68,11 @@ class ProximityStreamHandler(
4968
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
5069
//do nothing
5170
}
71+
72+
fun setScreenOffEnabled(enabled: Boolean) {
73+
enableScreenOff = enabled;
74+
if (!enabled && wakeLock != null && wakeLock!!.isHeld) {
75+
wakeLock!!.release()
76+
}
77+
}
5278
}

example/android/app/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<uses-permission android:name="android.hardware.sensor.proximity"/>
44
<uses-permission android:name="android.permission.BODY_SENSORS_BACKGROUND"/>
55
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
6+
<uses-permission android:name="android.permission.WAKE_LOCK"/>
67
<application
78
android:label="proximity_sensor_example"
89
android:icon="@mipmap/ic_launcher">

example/lib/main.dart

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class _MyAppState extends State<MyApp> {
3737
FlutterError.dumpErrorToConsole(details);
3838
}
3939
};
40+
await ProximitySensor.setProximityScreenOff(true).onError((error, stackTrace) {
41+
print("could not enable screen off functionality");
42+
return null;
43+
});
4044
_streamSubscription = ProximitySensor.events.listen((int event) {
4145
setState(() {
4246
_isNear = (event > 0) ? true : false;

example/pubspec.lock

+84-34
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,56 @@ packages:
55
dependency: transitive
66
description:
77
name: async
8-
url: "https://pub.dartlang.org"
8+
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
9+
url: "https://pub.dev"
910
source: hosted
10-
version: "2.9.0"
11+
version: "2.11.0"
1112
boolean_selector:
1213
dependency: transitive
1314
description:
1415
name: boolean_selector
15-
url: "https://pub.dartlang.org"
16+
sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66"
17+
url: "https://pub.dev"
1618
source: hosted
17-
version: "2.1.0"
19+
version: "2.1.1"
1820
characters:
1921
dependency: transitive
2022
description:
2123
name: characters
22-
url: "https://pub.dartlang.org"
24+
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
25+
url: "https://pub.dev"
2326
source: hosted
24-
version: "1.2.1"
27+
version: "1.3.0"
2528
clock:
2629
dependency: transitive
2730
description:
2831
name: clock
29-
url: "https://pub.dartlang.org"
32+
sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
33+
url: "https://pub.dev"
3034
source: hosted
3135
version: "1.1.1"
3236
collection:
3337
dependency: transitive
3438
description:
3539
name: collection
36-
url: "https://pub.dartlang.org"
40+
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
41+
url: "https://pub.dev"
3742
source: hosted
38-
version: "1.16.0"
43+
version: "1.18.0"
3944
cupertino_icons:
4045
dependency: "direct main"
4146
description:
4247
name: cupertino_icons
43-
url: "https://pub.dartlang.org"
48+
sha256: caac504f942f41dfadcf45229ce8c47065b93919a12739f20d6173a883c5ec73
49+
url: "https://pub.dev"
4450
source: hosted
4551
version: "1.0.2"
4652
fake_async:
4753
dependency: transitive
4854
description:
4955
name: fake_async
50-
url: "https://pub.dartlang.org"
56+
sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
57+
url: "https://pub.dev"
5158
source: hosted
5259
version: "1.3.1"
5360
flutter:
@@ -60,41 +67,69 @@ packages:
6067
description: flutter
6168
source: sdk
6269
version: "0.0.0"
70+
leak_tracker:
71+
dependency: transitive
72+
description:
73+
name: leak_tracker
74+
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
75+
url: "https://pub.dev"
76+
source: hosted
77+
version: "10.0.0"
78+
leak_tracker_flutter_testing:
79+
dependency: transitive
80+
description:
81+
name: leak_tracker_flutter_testing
82+
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
83+
url: "https://pub.dev"
84+
source: hosted
85+
version: "2.0.1"
86+
leak_tracker_testing:
87+
dependency: transitive
88+
description:
89+
name: leak_tracker_testing
90+
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
91+
url: "https://pub.dev"
92+
source: hosted
93+
version: "2.0.1"
6394
matcher:
6495
dependency: transitive
6596
description:
6697
name: matcher
67-
url: "https://pub.dartlang.org"
98+
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
99+
url: "https://pub.dev"
68100
source: hosted
69-
version: "0.12.12"
101+
version: "0.12.16+1"
70102
material_color_utilities:
71103
dependency: transitive
72104
description:
73105
name: material_color_utilities
74-
url: "https://pub.dartlang.org"
106+
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
107+
url: "https://pub.dev"
75108
source: hosted
76-
version: "0.1.5"
109+
version: "0.8.0"
77110
meta:
78111
dependency: transitive
79112
description:
80113
name: meta
81-
url: "https://pub.dartlang.org"
114+
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
115+
url: "https://pub.dev"
82116
source: hosted
83-
version: "1.8.0"
117+
version: "1.11.0"
84118
path:
85119
dependency: transitive
86120
description:
87121
name: path
88-
url: "https://pub.dartlang.org"
122+
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
123+
url: "https://pub.dev"
89124
source: hosted
90-
version: "1.8.2"
125+
version: "1.9.0"
91126
proximity_sensor:
92127
dependency: "direct main"
93128
description:
94129
path: ".."
95130
relative: true
96131
source: path
97-
version: "1.0.4"
132+
version: "1.2.0"
98133
sky_engine:
99134
dependency: transitive
100135
description: flutter
@@ -104,51 +139,66 @@ packages:
104139
dependency: transitive
105140
description:
106141
name: source_span
107-
url: "https://pub.dartlang.org"
142+
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
143+
url: "https://pub.dev"
108144
source: hosted
109-
version: "1.9.0"
145+
version: "1.10.0"
110146
stack_trace:
111147
dependency: transitive
112148
description:
113149
name: stack_trace
114-
url: "https://pub.dartlang.org"
150+
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
151+
url: "https://pub.dev"
115152
source: hosted
116-
version: "1.10.0"
153+
version: "1.11.1"
117154
stream_channel:
118155
dependency: transitive
119156
description:
120157
name: stream_channel
121-
url: "https://pub.dartlang.org"
158+
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
159+
url: "https://pub.dev"
122160
source: hosted
123-
version: "2.1.0"
161+
version: "2.1.2"
124162
string_scanner:
125163
dependency: transitive
126164
description:
127165
name: string_scanner
128-
url: "https://pub.dartlang.org"
166+
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
167+
url: "https://pub.dev"
129168
source: hosted
130-
version: "1.1.1"
169+
version: "1.2.0"
131170
term_glyph:
132171
dependency: transitive
133172
description:
134173
name: term_glyph
135-
url: "https://pub.dartlang.org"
174+
sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84
175+
url: "https://pub.dev"
136176
source: hosted
137177
version: "1.2.1"
138178
test_api:
139179
dependency: transitive
140180
description:
141181
name: test_api
142-
url: "https://pub.dartlang.org"
182+
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
183+
url: "https://pub.dev"
143184
source: hosted
144-
version: "0.4.12"
185+
version: "0.6.1"
145186
vector_math:
146187
dependency: transitive
147188
description:
148189
name: vector_math
149-
url: "https://pub.dartlang.org"
190+
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
191+
url: "https://pub.dev"
150192
source: hosted
151-
version: "2.1.2"
193+
version: "2.1.4"
194+
vm_service:
195+
dependency: transitive
196+
description:
197+
name: vm_service
198+
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
199+
url: "https://pub.dev"
200+
source: hosted
201+
version: "13.0.0"
152202
sdks:
153-
dart: ">=2.17.0-0 <3.0.0"
203+
dart: ">=3.2.0-0 <4.0.0"
154204
flutter: ">=1.20.0"

0 commit comments

Comments
 (0)