From 373e54a8b1de3487ea795f0362aa841f225a6e1f Mon Sep 17 00:00:00 2001 From: Christina Xu Date: Mon, 9 Dec 2019 00:03:40 -0800 Subject: [PATCH] Rewrite setonclicklistener Reviewed By: dreamolight Differential Revision: D18880429 fbshipit-source-id: 31982cc310ac51691b9c635213b8bd567fe33555 --- .../codeless/internal/ViewHierarchy.java | 38 +++++++++++++++++++ .../suggestedevents/ViewOnClickListener.java | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/facebook-core/src/main/java/com/facebook/appevents/codeless/internal/ViewHierarchy.java b/facebook-core/src/main/java/com/facebook/appevents/codeless/internal/ViewHierarchy.java index 55cbdf3492..f0c4e471f8 100644 --- a/facebook-core/src/main/java/com/facebook/appevents/codeless/internal/ViewHierarchy.java +++ b/facebook-core/src/main/java/com/facebook/appevents/codeless/internal/ViewHierarchy.java @@ -371,6 +371,44 @@ public static View.OnClickListener getExistingOnClickListener(View view) { return null; } + public static void setOnClickListener(View view, View.OnClickListener newListener) { + try { + Field listenerInfoField = null; + Field listenerField = null; + try { + listenerInfoField = Class.forName("android.view.View") + .getDeclaredField("mListenerInfo"); + listenerField = Class.forName("android.view.View$ListenerInfo") + .getDeclaredField("mOnClickListener"); + } catch (ClassNotFoundException e) { /* no op */ + } catch (NoSuchFieldException e) { /* no op */ + } + + if (listenerInfoField == null || listenerField == null) { + view.setOnClickListener(newListener); + return; + } + + listenerInfoField.setAccessible(true); + listenerField.setAccessible(true); + + Object listenerObj = null; + try { + listenerInfoField.setAccessible(true); + listenerObj = listenerInfoField.get(view); + } catch (IllegalAccessException e) { /* no op */ + } + + if (listenerObj == null) { + view.setOnClickListener(newListener); + return; + } + + listenerField.set(listenerObj, newListener); + } catch (Exception e) { /* no op */ + } + } + @Nullable @RestrictTo(RestrictTo.Scope.GROUP_ID) public static View.OnTouchListener getExistingOnTouchListener(View view) { diff --git a/facebook-core/src/main/java/com/facebook/appevents/suggestedevents/ViewOnClickListener.java b/facebook-core/src/main/java/com/facebook/appevents/suggestedevents/ViewOnClickListener.java index b0d505e8a6..f7146524b4 100644 --- a/facebook-core/src/main/java/com/facebook/appevents/suggestedevents/ViewOnClickListener.java +++ b/facebook-core/src/main/java/com/facebook/appevents/suggestedevents/ViewOnClickListener.java @@ -59,7 +59,7 @@ public final class ViewOnClickListener implements View.OnClickListener { static void attachListener(View hostView, View rootView, String activityName) { int key = hostView.hashCode(); if (!viewsAttachedListener.contains(key)) { - hostView.setOnClickListener( + ViewHierarchy.setOnClickListener(hostView, new ViewOnClickListener(hostView, rootView, activityName)); viewsAttachedListener.add(key); }