Skip to content

Commit 3716398

Browse files
a7medevHeshamMegid
authored andcommitted
fix(android): resolve private views through UI manager directly (#1121)
1 parent 15ba483 commit 3716398

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Support user identification using ID ([#1115](https://github.com/Instabug/Instabug-React-Native/pull/1115))
88

9+
### Fixed
10+
11+
- Fix an Android `NullPointerException` crash in private views APIs ([#1121](https://github.com/Instabug/Instabug-React-Native/pull/1121)), closes [#514](https://github.com/Instabug/Instabug-React-Native/issues/514).
12+
913
## [12.6.0](https://github.com/Instabug/Instabug-React-Native/compare/v12.5.0...v12.6.0) (January 14, 2024)
1014

1115
### Changed

android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import android.net.Uri;
88
import android.view.View;
99

10+
import androidx.annotation.UiThread;
11+
1012
import com.facebook.react.bridge.Arguments;
1113
import com.facebook.react.bridge.Callback;
1214
import com.facebook.react.bridge.Promise;
@@ -17,8 +19,6 @@
1719
import com.facebook.react.bridge.WritableMap;
1820
import com.facebook.react.bridge.WritableNativeArray;
1921
import com.facebook.react.bridge.WritableNativeMap;
20-
import com.facebook.react.uimanager.NativeViewHierarchyManager;
21-
import com.facebook.react.uimanager.UIBlock;
2222
import com.facebook.react.uimanager.UIManagerModule;
2323
import com.instabug.library.Feature;
2424
import com.instabug.library.Instabug;
@@ -899,24 +899,32 @@ public void networkLog(String jsonObject) throws JSONException {
899899
networkLog.insert();
900900
}
901901

902+
@UiThread
903+
@Nullable
904+
private View resolveReactView(final int reactTag) {
905+
final ReactApplicationContext reactContext = getReactApplicationContext();
906+
final UIManagerModule uiManagerModule = reactContext.getNativeModule(UIManagerModule.class);
907+
908+
if (uiManagerModule == null) {
909+
return null;
910+
}
911+
912+
return uiManagerModule.resolveView(reactTag);
913+
}
914+
902915

903916
@ReactMethod
904917
public void addPrivateView(final int reactTag) {
905918
MainThreadHandler.runOnMainThread(new Runnable() {
906919
@Override
907920
public void run() {
908-
UIManagerModule uiManagerModule = getReactApplicationContext().getNativeModule(UIManagerModule.class);
909-
uiManagerModule.prependUIBlock(new UIBlock() {
910-
@Override
911-
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
912-
try {
913-
final View view = nativeViewHierarchyManager.resolveView(reactTag);
914-
Instabug.addPrivateViews(view);
915-
} catch(Exception e) {
916-
e.printStackTrace();
917-
}
918-
}
919-
});
921+
try {
922+
final View view = resolveReactView(reactTag);
923+
924+
Instabug.addPrivateViews(view);
925+
} catch (Exception e) {
926+
e.printStackTrace();
927+
}
920928
}
921929
});
922930
}
@@ -926,18 +934,13 @@ public void removePrivateView(final int reactTag) {
926934
MainThreadHandler.runOnMainThread(new Runnable() {
927935
@Override
928936
public void run() {
929-
UIManagerModule uiManagerModule = getReactApplicationContext().getNativeModule(UIManagerModule.class);
930-
uiManagerModule.prependUIBlock(new UIBlock() {
931-
@Override
932-
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
933-
try {
934-
final View view = nativeViewHierarchyManager.resolveView(reactTag);
935-
Instabug.removePrivateViews(view);
936-
} catch(Exception e) {
937-
e.printStackTrace();
938-
}
939-
}
940-
});
937+
try {
938+
final View view = resolveReactView(reactTag);
939+
940+
Instabug.removePrivateViews(view);
941+
} catch (Exception e) {
942+
e.printStackTrace();
943+
}
941944
}
942945
});
943946
}

0 commit comments

Comments
 (0)