From 9bdb63c23448707be7b529cc21fdb8f267efd281 Mon Sep 17 00:00:00 2001 From: Dave Miller Date: Thu, 9 Jun 2016 09:58:04 -0700 Subject: [PATCH] Add ability to dump a Native View Hierarchy Differential Revision: D3411095 fbshipit-source-id: cc7e26eea63b0146250177c2e1b780ecc03da02f --- .../react/uimanager/ViewHierarchyDumper.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewHierarchyDumper.java diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewHierarchyDumper.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewHierarchyDumper.java new file mode 100644 index 000000000..27b80fa5d --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewHierarchyDumper.java @@ -0,0 +1,52 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.uimanager; + +import javax.annotation.Nullable; + +import android.view.View; +import android.view.ViewGroup; + +import com.facebook.react.bridge.UiThreadUtil; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +public class ViewHierarchyDumper { + + public static @Nullable JSONObject toJSON(@Nullable View view) { + UiThreadUtil.assertOnUiThread(); + if (view == null) { + return null; + } + JSONObject result = new JSONObject(); + try { + result.put("class", view.getClass().getSimpleName()); + Object tag = view.getTag(); + if (tag != null && tag instanceof String) { + result.put("id", tag); + } + if (view instanceof ViewGroup) { + ViewGroup viewGroup = (ViewGroup) view; + if (viewGroup.getChildCount() > 0) { + JSONArray children = new JSONArray(); + for (int i = 0; i < viewGroup.getChildCount(); i++) { + children.put(i, toJSON(viewGroup.getChildAt(i))); + } + result.put("children", children); + } + } + } catch (JSONException ex) { + return null; + } + return result; + } +}