Selaa lähdekoodia

[RN] Use proximity sensor when in audio-only mode

When the audio-only mode is enabled, turn on the proximity sensor to dim the
screen and disable touch controls when there is an object nearby.
j8
Saúl Ibarra Corretgé 8 vuotta sitten
vanhempi
commit
13e3375e8a

+ 1
- 0
android/app/src/main/AndroidManifest.xml Näytä tiedosto

@@ -11,6 +11,7 @@
11 11
     <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
12 12
     <uses-permission android:name="android.permission.RECORD_AUDIO" />
13 13
     <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
14
+    <uses-permission android:name="android.permission.WAKE_LOCK"/>
14 15
 
15 16
     <uses-feature android:name="android.hardware.camera" />
16 17
     <uses-feature android:name="android.hardware.camera.autofocus"/>

+ 2
- 1
android/app/src/main/java/org/jitsi/meet/MainApplication.java Näytä tiedosto

@@ -32,7 +32,8 @@ public class MainApplication extends Application implements ReactApplication {
32 32
                 new com.ocetnik.timer.BackgroundTimerPackage(),
33 33
                 new com.oney.WebRTCModule.WebRTCModulePackage(),
34 34
                 new com.rnimmersive.RNImmersivePackage(),
35
-                new org.jitsi.meet.audiomode.AudioModePackage()
35
+                new org.jitsi.meet.audiomode.AudioModePackage(),
36
+                new org.jitsi.meet.proximity.ProximityPackage()
36 37
             );
37 38
         }
38 39
     };

+ 101
- 0
android/app/src/main/java/org/jitsi/meet/proximity/ProximityModule.java Näytä tiedosto

@@ -0,0 +1,101 @@
1
+package org.jitsi.meet.proximity;
2
+
3
+import android.content.Context;
4
+import android.os.PowerManager;
5
+import android.os.PowerManager.WakeLock;
6
+
7
+import com.facebook.react.bridge.ReactApplicationContext;
8
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
9
+import com.facebook.react.bridge.ReactMethod;
10
+import com.facebook.react.bridge.UiThreadUtil;
11
+
12
+/**
13
+ * Module implementing a simple API to enable a proximity sensor-controlled
14
+ * wake lock. When the lock is held, if the proximity sensor detects a nearby
15
+ * object it will dim the screen and disable touch controls. The functionality
16
+ * is used with the conference audio-only mode.
17
+ */
18
+public class ProximityModule extends ReactContextBaseJavaModule {
19
+    /**
20
+     * React Native module name.
21
+     */
22
+    private static final String MODULE_NAME = "Proximity";
23
+
24
+    /**
25
+     * This type of wake lock (the one activated by the proximity sensor) has
26
+     * been available for a while, but the constant was only exported in API
27
+     * level 21 (Android Marshmallow) so make no assumptions and use its value
28
+     * directly.
29
+     *
30
+     * TODO: Remove when we bump the API level to 21.
31
+     */
32
+    private static final int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 32;
33
+
34
+    /**
35
+     * {@link WakeLock} instance.
36
+     */
37
+    private final WakeLock wakeLock;
38
+
39
+    /**
40
+     * Initializes a new module instance. There shall be a single instance of
41
+     * this module throughout the lifetime of the application.
42
+     *
43
+     * @param reactContext The {@link ReactApplicationContext} where this module
44
+     * is created.
45
+     */
46
+    public ProximityModule(ReactApplicationContext reactContext) {
47
+        super(reactContext);
48
+
49
+        WakeLock wakeLock;
50
+        PowerManager powerManager
51
+            = (PowerManager)
52
+                reactContext.getSystemService(Context.POWER_SERVICE);
53
+
54
+        try {
55
+            wakeLock
56
+                = powerManager.newWakeLock(
57
+                        PROXIMITY_SCREEN_OFF_WAKE_LOCK,
58
+                        MODULE_NAME);
59
+        } catch (Throwable ignored) {
60
+            wakeLock = null;
61
+        }
62
+
63
+        this.wakeLock = wakeLock;
64
+    }
65
+
66
+    /**
67
+     * Gets the name of this module to be used in the React Native bridge.
68
+     *
69
+     * @return The name of this module to be used in the React Native bridge.
70
+     */
71
+    @Override
72
+    public String getName() {
73
+        return MODULE_NAME;
74
+    }
75
+
76
+    /**
77
+     * Acquires / releases the proximity sensor wake lock.
78
+     *
79
+     * @param enabled {@code true} to enable the proximity sensor; otherwise,
80
+     * {@code false}.
81
+     */
82
+    @ReactMethod
83
+    public void setEnabled(final boolean enabled) {
84
+        if (wakeLock == null) {
85
+            return;
86
+        }
87
+
88
+        UiThreadUtil.runOnUiThread(new Runnable() {
89
+            @Override
90
+            public void run() {
91
+                if (enabled) {
92
+                    if (!wakeLock.isHeld()) {
93
+                        wakeLock.acquire();
94
+                    }
95
+                } else if (wakeLock.isHeld()) {
96
+                    wakeLock.release();
97
+                }
98
+            }
99
+        });
100
+    }
101
+}

+ 48
- 0
android/app/src/main/java/org/jitsi/meet/proximity/ProximityPackage.java Näytä tiedosto

@@ -0,0 +1,48 @@
1
+package org.jitsi.meet.proximity;
2
+
3
+import com.facebook.react.ReactPackage;
4
+import com.facebook.react.bridge.JavaScriptModule;
5
+import com.facebook.react.bridge.NativeModule;
6
+import com.facebook.react.bridge.ReactApplicationContext;
7
+import com.facebook.react.uimanager.ViewManager;
8
+
9
+import java.util.ArrayList;
10
+import java.util.Collections;
11
+import java.util.List;
12
+
13
+/**
14
+ * Implements {@link ReactPackage} for {@link ProximityModule}.
15
+ */
16
+public class ProximityPackage implements ReactPackage {
17
+    /**
18
+     * {@inheritDoc}
19
+     */
20
+    @Override
21
+    public List<Class<? extends JavaScriptModule>> createJSModules() {
22
+        return Collections.emptyList();
23
+    }
24
+
25
+    /**
26
+     * {@inheritDoc}
27
+     *
28
+     * @return List of native modules to be exposed by React Native.
29
+     */
30
+    @Override
31
+    public List<NativeModule> createNativeModules(
32
+            ReactApplicationContext reactContext) {
33
+        List<NativeModule> modules = new ArrayList<>();
34
+
35
+        modules.add(new ProximityModule(reactContext));
36
+
37
+        return modules;
38
+    }
39
+
40
+    /**
41
+     * {@inheritDoc}
42
+     */
43
+    @Override
44
+    public List<ViewManager> createViewManagers(
45
+            ReactApplicationContext reactContext) {
46
+        return Collections.emptyList();
47
+    }
48
+}

+ 24
- 0
ios/app/Proximity.m Näytä tiedosto

@@ -0,0 +1,24 @@
1
+#import "RCTBridgeModule.h"
2
+
3
+#import <UIKit/UIKit.h>
4
+
5
+@interface Proximity : NSObject<RCTBridgeModule>
6
+@end
7
+
8
+@implementation Proximity
9
+
10
+RCT_EXPORT_MODULE();
11
+
12
+/**
13
+ * Enables / disables the proximity sensor monitoring. On iOS enabling the
14
+ * proximity sensor automatically dims the screen and disables touch controls,
15
+ * so there is nothing else to do (unlike on Android)!
16
+ *
17
+ * @param enabled {@code YES} to enable proximity (sensor) monitoring;
18
+ * {@code NO}, otherwise.
19
+ */
20
+RCT_EXPORT_METHOD(setEnabled:(BOOL)enabled) {
21
+    [[UIDevice currentDevice] setProximityMonitoringEnabled:enabled];
22
+}
23
+
24
+@end

+ 4
- 0
ios/jitsi-meet-react.xcodeproj/project.pbxproj Näytä tiedosto

@@ -14,6 +14,7 @@
14 14
 		00C302E91ABCBA2D00DB3ED1 /* libRCTNetwork.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302DC1ABCB9D200DB3ED1 /* libRCTNetwork.a */; };
15 15
 		00C302EA1ABCBA2D00DB3ED1 /* libRCTVibration.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 00C302E41ABCB9EE00DB3ED1 /* libRCTVibration.a */; };
16 16
 		0B42DFAE1E2FD90700111B12 /* AudioMode.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B42DFAD1E2FD90700111B12 /* AudioMode.m */; };
17
+		0B96CAF11E8CF0E8005F348C /* Proximity.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B96CAF01E8CF0E8005F348C /* Proximity.m */; };
17 18
 		133E29F31AD74F7200F7D852 /* libRCTLinking.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 78C398B91ACF4ADC00677621 /* libRCTLinking.a */; };
18 19
 		139105C61AF99C1200B5F7CC /* libRCTSettings.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 139105C11AF99BAD00B5F7CC /* libRCTSettings.a */; };
19 20
 		139FDEF61B0652A700C62182 /* libRCTWebSocket.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 139FDEF41B06529B00C62182 /* libRCTWebSocket.a */; };
@@ -273,6 +274,7 @@
273 274
 		00C302DF1ABCB9EE00DB3ED1 /* RCTVibration.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTVibration.xcodeproj; path = "../node_modules/react-native/Libraries/Vibration/RCTVibration.xcodeproj"; sourceTree = "<group>"; };
274 275
 		0965153BB98645B4A8B6AA10 /* RNBackgroundTimer.xcodeproj */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = "wrapper.pb-project"; name = RNBackgroundTimer.xcodeproj; path = "../node_modules/react-native-background-timer/ios/RNBackgroundTimer.xcodeproj"; sourceTree = "<group>"; };
275 276
 		0B42DFAD1E2FD90700111B12 /* AudioMode.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AudioMode.m; path = app/AudioMode.m; sourceTree = "<group>"; };
277
+		0B96CAF01E8CF0E8005F348C /* Proximity.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Proximity.m; path = app/Proximity.m; sourceTree = "<group>"; };
276 278
 		0EA8C046B2BF46279796F07D /* libKCKeepAwake.a */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = 9; includeInIndex = 0; lastKnownFileType = archive.ar; path = libKCKeepAwake.a; sourceTree = "<group>"; };
277 279
 		139105B61AF99BAD00B5F7CC /* RCTSettings.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTSettings.xcodeproj; path = "../node_modules/react-native/Libraries/Settings/RCTSettings.xcodeproj"; sourceTree = "<group>"; };
278 280
 		139FDEE61B06529A00C62182 /* RCTWebSocket.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RCTWebSocket.xcodeproj; path = "../node_modules/react-native/Libraries/WebSocket/RCTWebSocket.xcodeproj"; sourceTree = "<group>"; };
@@ -426,6 +428,7 @@
426 428
 				008F07F21AC5B25A0029DE68 /* main.jsbundle */,
427 429
 				13B07FB71A68108700A75B9A /* main.m */,
428 430
 				B3A9D0241E0481E10009343D /* POSIX.m */,
431
+				0B96CAF01E8CF0E8005F348C /* Proximity.m */,
429 432
 			);
430 433
 			name = app;
431 434
 			sourceTree = "<group>";
@@ -945,6 +948,7 @@
945 948
 			buildActionMask = 2147483647;
946 949
 			files = (
947 950
 				B3A9D0251E0481E10009343D /* POSIX.m in Sources */,
951
+				0B96CAF11E8CF0E8005F348C /* Proximity.m in Sources */,
948 952
 				0B42DFAE1E2FD90700111B12 /* AudioMode.m in Sources */,
949 953
 				13B07FBC1A68108700A75B9A /* AppDelegate.m in Sources */,
950 954
 				13B07FC11A68108700A75B9A /* main.m in Sources */,

+ 1
- 0
react/features/app/components/App.native.js Näytä tiedosto

@@ -6,6 +6,7 @@ import { Platform } from '../../base/react';
6 6
 import '../../mobile/audio-mode';
7 7
 import '../../mobile/background';
8 8
 import '../../mobile/full-screen';
9
+import '../../mobile/proximity';
9 10
 import '../../mobile/wake-lock';
10 11
 
11 12
 import { AbstractApp } from './AbstractApp';

+ 1
- 0
react/features/mobile/proximity/index.js Näytä tiedosto

@@ -0,0 +1 @@
1
+import './middleware';

+ 53
- 0
react/features/mobile/proximity/middleware.js Näytä tiedosto

@@ -0,0 +1,53 @@
1
+import { NativeModules } from 'react-native';
2
+
3
+import {
4
+    CONFERENCE_FAILED,
5
+    CONFERENCE_JOINED,
6
+    CONFERENCE_LEFT,
7
+    SET_AUDIO_ONLY
8
+} from '../../base/conference';
9
+import { MiddlewareRegistry } from '../../base/redux';
10
+
11
+/**
12
+ * Middleware which enables / disables the proximity sensor in accord with
13
+ * conference-related actions. If the proximity sensor is enabled, it will dim
14
+ * the screen and disable touch controls when an object is nearby. The
15
+ * functionality is  enabled when a conference is in audio-only mode.
16
+ *
17
+ * @param {Store} store - Redux store.
18
+ * @returns {Function}
19
+ */
20
+MiddlewareRegistry.register(store => next => action => {
21
+    switch (action.type) {
22
+    case CONFERENCE_JOINED: {
23
+        const { audioOnly } = store.getState()['features/base/conference'];
24
+
25
+        _setProximityEnabled(audioOnly);
26
+        break;
27
+    }
28
+
29
+    case CONFERENCE_FAILED:
30
+    case CONFERENCE_LEFT:
31
+        _setProximityEnabled(false);
32
+        break;
33
+
34
+    case SET_AUDIO_ONLY:
35
+        _setProximityEnabled(action.audioOnly);
36
+        break;
37
+    }
38
+
39
+    return next(action);
40
+});
41
+
42
+/**
43
+ * Enables / disables the proximity sensor. If the proximity sensor is enabled,
44
+ * it will dim the screen and disable touch controls when an object is nearby.
45
+ *
46
+ * @param {boolean} enabled - True to enable the proximity sensor or false to
47
+ * disable it.
48
+ * @private
49
+ * @returns {void}
50
+ */
51
+function _setProximityEnabled(enabled) {
52
+    NativeModules.Proximity.setEnabled(Boolean(enabled));
53
+}

Loading…
Peruuta
Tallenna