Skip to content

Commit f8ba425

Browse files
committed
fix(demo): fix stale Android click targets and IAM WebView debugging
1 parent c204543 commit f8ba425

2 files changed

Lines changed: 90 additions & 5 deletions

File tree

examples/demo/Assets/Scripts/AppBootstrapper.cs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ private async void Start()
5858
_apiService.SetAppId(appId);
5959

6060
OneSignal.Debug.LogLevel = LogLevel.Verbose;
61+
#if UNITY_ANDROID && !UNITY_EDITOR
62+
SetAndroidWebViewDebugging(false);
63+
#endif
6164
OneSignal.ConsentRequired = _prefs.ConsentRequired;
6265
OneSignal.ConsentGiven = _prefs.PrivacyConsent;
6366
OneSignal.Initialize(appId);
@@ -114,6 +117,42 @@ private static void RequestAndroidPostNotificationsPermission()
114117
1001
115118
);
116119
}
120+
121+
private static void SetAndroidWebViewDebugging(bool enabled)
122+
{
123+
if (!DotEnv.IsE2EMode)
124+
return;
125+
126+
try
127+
{
128+
using var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
129+
using var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
130+
if (activity == null)
131+
return;
132+
133+
activity.Call(
134+
"runOnUiThread",
135+
new AndroidJavaRunnable(() =>
136+
{
137+
try
138+
{
139+
using var webView = new AndroidJavaClass("android.webkit.WebView");
140+
webView.CallStatic("setWebContentsDebuggingEnabled", enabled);
141+
}
142+
catch (AndroidJavaException ex)
143+
{
144+
Debug.LogWarning(
145+
$"[{Tag}] Could not set WebView debugging: {ex.Message}"
146+
);
147+
}
148+
})
149+
);
150+
}
151+
catch (AndroidJavaException ex)
152+
{
153+
Debug.LogWarning($"[{Tag}] Could not set WebView debugging: {ex.Message}");
154+
}
155+
}
117156
#endif
118157

119158
private void RegisterSdkListeners()
@@ -141,17 +180,27 @@ private void OnDestroy()
141180
OneSignal.Notifications.ForegroundWillDisplay -= OnNotificationForegroundWillDisplay;
142181
}
143182

144-
private void OnIamWillDisplay(object sender, InAppMessageWillDisplayEventArgs e) =>
183+
private void OnIamWillDisplay(object sender, InAppMessageWillDisplayEventArgs e)
184+
{
185+
#if UNITY_ANDROID && !UNITY_EDITOR
186+
SetAndroidWebViewDebugging(true);
187+
#endif
145188
Debug.Log($"[{Tag}] IAM will display: {e.Message.MessageId}");
189+
}
146190

147191
private void OnIamDidDisplay(object sender, InAppMessageDidDisplayEventArgs e) =>
148192
Debug.Log($"[{Tag}] IAM did display: {e.Message.MessageId}");
149193

150194
private void OnIamWillDismiss(object sender, InAppMessageWillDismissEventArgs e) =>
151195
Debug.Log($"[{Tag}] IAM will dismiss: {e.Message.MessageId}");
152196

153-
private void OnIamDidDismiss(object sender, InAppMessageDidDismissEventArgs e) =>
197+
private void OnIamDidDismiss(object sender, InAppMessageDidDismissEventArgs e)
198+
{
154199
Debug.Log($"[{Tag}] IAM did dismiss: {e.Message.MessageId}");
200+
#if UNITY_ANDROID && !UNITY_EDITOR
201+
SetAndroidWebViewDebugging(false);
202+
#endif
203+
}
155204

156205
private void OnIamClicked(object sender, InAppMessageClickEventArgs e) =>
157206
Debug.Log($"[{Tag}] IAM clicked: {e.Result.ActionId}");

examples/demo/Assets/Scripts/Services/AccessibilityBridge.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Action action
189189
isEnabled ?? (() => true),
190190
action
191191
);
192+
target.RegisterCallback<DetachFromPanelEvent>(OnAndroidClickTargetDetached);
192193
#elif UNITY_IOS && !UNITY_EDITOR
193194
if (target == null || action == null || string.IsNullOrEmpty(target.name))
194195
return;
@@ -201,6 +202,14 @@ Action action
201202
#endif
202203
}
203204

205+
#if UNITY_ANDROID && !UNITY_EDITOR
206+
private static void OnAndroidClickTargetDetached(DetachFromPanelEvent e)
207+
{
208+
if (e.target is VisualElement target)
209+
AndroidClickTargets.Remove(target);
210+
}
211+
#endif
212+
204213
public static void RequestResync()
205214
{
206215
_instance?.ScheduleResync();
@@ -652,14 +661,41 @@ private void InvokeAndroidNativeAction(string id)
652661
return;
653662
}
654663

664+
Action actionToInvoke = null;
665+
List<VisualElement> detachedTargets = null;
655666
foreach (var kvp in AndroidClickTargets)
656667
{
657668
var el = kvp.Key;
658-
if (el == null || el.name != id || !kvp.Value.IsEnabled())
669+
if (el == null || el.panel == null || !IsDescendantOfRoot(el))
670+
{
671+
if (el != null)
672+
{
673+
detachedTargets ??= new List<VisualElement>();
674+
detachedTargets.Add(el);
675+
}
659676
continue;
660-
kvp.Value.Action();
661-
return;
677+
}
678+
if (el.name != id || !IsVisible(el) || !kvp.Value.IsEnabled())
679+
continue;
680+
actionToInvoke = kvp.Value.Action;
681+
break;
682+
}
683+
if (detachedTargets != null)
684+
{
685+
foreach (var target in detachedTargets)
686+
AndroidClickTargets.Remove(target);
687+
}
688+
actionToInvoke?.Invoke();
689+
}
690+
691+
private bool IsDescendantOfRoot(VisualElement el)
692+
{
693+
for (var current = el; current != null; current = current.hierarchy.parent)
694+
{
695+
if (current == _root)
696+
return true;
662697
}
698+
return false;
663699
}
664700

665701
private void InvokeAndroidNativeScroll(string direction)

0 commit comments

Comments
 (0)