Skip to content

Commit 9ed4f26

Browse files
authored
fix(AccessibilityInfo): Do not subscribe to accessibility events in background (microsoft#1488)
Uses the same pattern as DeviceInfoModule to subscribe/unsubscribe from the high contrast event only when the app is running in the foreground.
1 parent 81c9361 commit 9ed4f26

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

ReactWindows/ReactNative/Modules/AccessibilityInfo/AccessibilityInfoModule.cs

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
using ReactNative.Bridge;
22
using ReactNative.Modules.Core;
33
using System.Collections.Generic;
4+
using Windows.ApplicationModel.Core;
45
using Windows.UI.ViewManagement;
56

67
namespace ReactNative.Modules.Accessibilityinfo
78
{
8-
class AccessibilityInfoModule : ReactContextNativeModuleBase
9+
class AccessibilityInfoModule : ReactContextNativeModuleBase, ILifecycleEventListener, IBackgroundEventListener
910
{
1011
private readonly AccessibilitySettings _accessibility = new AccessibilitySettings();
1112
private readonly UISettings _settings = new UISettings();
1213

14+
private bool _isSubscribed;
15+
1316
private string GetRgbaString(UIElementType type)
1417
{
1518
var color = _settings.UIElementColor(type);
@@ -53,20 +56,72 @@ public override IReadOnlyDictionary<string, object> Constants
5356
}
5457
}
5558

59+
private static bool HasCoreWindow => CoreApplication.MainView.CoreWindow != null;
60+
61+
/// <summary>
62+
/// Called after the creation of a <see cref="IReactInstance"/>,
63+
/// </summary>
5664
public override void Initialize()
5765
{
58-
DispatcherHelpers.RunOnDispatcher(() =>
66+
Context.AddLifecycleEventListener(this);
67+
Context.AddBackgroundEventListener(this);
68+
}
69+
70+
/// <summary>
71+
/// Called when the application is suspended.
72+
/// </summary>
73+
public void OnSuspend()
74+
{
75+
Unsubscribe();
76+
}
77+
78+
/// <summary>
79+
/// Called when the application is resumed.
80+
/// </summary>
81+
public void OnResume()
82+
{
83+
Subscribe();
84+
}
85+
86+
/// <summary>
87+
/// Called when the host entered background mode.
88+
/// </summary>
89+
public void OnEnteredBackground()
90+
{
91+
Unsubscribe();
92+
}
93+
94+
/// <summary>
95+
/// Called when the host is leaving background mode.
96+
/// </summary>
97+
public void OnLeavingBackground()
98+
{
99+
Subscribe();
100+
}
101+
102+
private void Subscribe()
103+
{
104+
if (!_isSubscribed && HasCoreWindow)
59105
{
106+
_isSubscribed = true;
60107
_accessibility.HighContrastChanged += OnHighContrastChanged;
61-
});
108+
}
62109
}
63110

64-
public override void OnReactInstanceDispose()
111+
private void Unsubscribe()
65112
{
66-
DispatcherHelpers.RunOnDispatcher(() =>
113+
if (_isSubscribed && HasCoreWindow)
67114
{
68115
_accessibility.HighContrastChanged -= OnHighContrastChanged;
69-
});
116+
_isSubscribed = false;
117+
}
118+
}
119+
120+
/// <summary>
121+
/// Called when the application is terminated.
122+
/// </summary>
123+
public void OnDestroy()
124+
{
70125
}
71126

72127
private void OnHighContrastChanged(AccessibilitySettings sender, object args)

0 commit comments

Comments
 (0)