From 0c2e5ff2dedcaa2ea020aad8883c96048aff54d4 Mon Sep 17 00:00:00 2001 From: Alexey Verein Date: Fri, 14 Aug 2020 19:05:59 +0300 Subject: [PATCH] Safe inspector --- .../java/app/avo/inspector/AvoInspector.java | 127 +++++++++++------- 1 file changed, 78 insertions(+), 49 deletions(-) diff --git a/avoinspector/src/main/java/app/avo/inspector/AvoInspector.java b/avoinspector/src/main/java/app/avo/inspector/AvoInspector.java index f765ca9..e78d078 100644 --- a/avoinspector/src/main/java/app/avo/inspector/AvoInspector.java +++ b/avoinspector/src/main/java/app/avo/inspector/AvoInspector.java @@ -110,7 +110,11 @@ public void onActivityStarted(@NonNull Activity activity) { } catch (Exception e) { Log.e("Avo Inspector", "Something went wrong. Please report to support@avo.app.", e); } - sessionTracker.startOrProlongSession(System.currentTimeMillis()); + try { + sessionTracker.startOrProlongSession(System.currentTimeMillis()); + } catch (Exception e) { + Log.e("Avo Inspector", "Something went wrong. Please report to support@avo.app.", e); + } } } }); @@ -153,58 +157,73 @@ public void hideVisualInspector(Activity rootActivity) { @SuppressWarnings({"unused", "SameParameterValue"}) @NonNull Map avoFunctionTrackSchemaFromEvent(@NonNull String eventName, @Nullable Map eventProperties, @NonNull String eventId, @NonNull String eventHash) { - if (AvoDeduplicator.shouldRegisterEvent(eventName, eventProperties, true)) { - logPreExtract(eventName, eventProperties); - showEventInVisualInspector(eventName, eventProperties, null); + try { + if (AvoDeduplicator.shouldRegisterEvent(eventName, eventProperties, true)) { + logPreExtract(eventName, eventProperties); + showEventInVisualInspector(eventName, eventProperties, null); - Map schema = avoSchemaExtractor.extractSchema(eventProperties, false); + Map schema = avoSchemaExtractor.extractSchema(eventProperties, false); - trackSchemaInternal(eventName, schema, eventId, eventHash); + trackSchemaInternal(eventName, schema, eventId, eventHash); - return schema; - } else { - if (isLogging()) { - Log.d("Avo Inspector", "[avo] Avo Inspector: Deduplicated event " + eventName); - } + return schema; + } else { + if (isLogging()) { + Log.d("Avo Inspector", "[avo] Avo Inspector: Deduplicated event " + eventName); + } + return new HashMap<>(); + } + } catch (Exception e) { + Log.e("Avo Inspector", "Something went wrong. Please report to support@avo.app.", e); return new HashMap<>(); } } @Override public @NonNull Map trackSchemaFromEvent(@NonNull String eventName, @Nullable JSONObject eventProperties) { - if (AvoDeduplicator.shouldRegisterEvent(eventName, Util.jsonToMap(eventProperties), false)) { - logPreExtract(eventName, eventProperties); - showEventInVisualInspector(eventName, null, eventProperties); + try { + if (AvoDeduplicator.shouldRegisterEvent(eventName, Util.jsonToMap(eventProperties), false)) { + logPreExtract(eventName, eventProperties); + showEventInVisualInspector(eventName, null, eventProperties); - Map schema = avoSchemaExtractor.extractSchema(eventProperties, false); + Map schema = avoSchemaExtractor.extractSchema(eventProperties, false); - trackSchemaInternal(eventName, schema, null, null); + trackSchemaInternal(eventName, schema, null, null); - return schema; - } else { - if (isLogging()) { - Log.d("Avo Inspector", "[avo] Avo Inspector: Deduplicated event " + eventName); + return schema; + } else { + if (isLogging()) { + Log.d("Avo Inspector", "[avo] Avo Inspector: Deduplicated event " + eventName); + } + return new HashMap<>(); } + } catch (Exception e) { + Log.e("Avo Inspector", "Something went wrong. Please report to support@avo.app.", e); return new HashMap<>(); } } @Override public @NonNull Map trackSchemaFromEvent(@NonNull String eventName, @Nullable Map eventProperties) { - if (AvoDeduplicator.shouldRegisterEvent(eventName, eventProperties, false)) { - logPreExtract(eventName, eventProperties); - showEventInVisualInspector(eventName, eventProperties, null); + try { + if (AvoDeduplicator.shouldRegisterEvent(eventName, eventProperties, false)) { + logPreExtract(eventName, eventProperties); + showEventInVisualInspector(eventName, eventProperties, null); - Map schema = avoSchemaExtractor.extractSchema(eventProperties, false); + Map schema = avoSchemaExtractor.extractSchema(eventProperties, false); - trackSchemaInternal(eventName, schema, null, null); + trackSchemaInternal(eventName, schema, null, null); - return schema; - } else { - if (isLogging()) { - Log.d("Avo Inspector", "Deduplicated event " + eventName); + return schema; + } else { + if (isLogging()) { + Log.d("Avo Inspector", "Deduplicated event " + eventName); + } + return new HashMap<>(); } + } catch (Exception e) { + Log.e("Avo Inspector", "Something went wrong. Please report to support@avo.app.", e); return new HashMap<>(); } } @@ -276,12 +295,16 @@ private void showSchemaInVisualInspector(String eventName, Map eventSchema) { - if (AvoDeduplicator.shouldRegisterSchemaFromManually(eventName, eventSchema)) { - trackSchemaInternal(eventName, eventSchema, null, null); - } else { - if (isLogging()) { - Log.d("Avo Inspector", "Deduplicated event " + eventName); + try { + if (AvoDeduplicator.shouldRegisterSchemaFromManually(eventName, eventSchema)) { + trackSchemaInternal(eventName, eventSchema, null, null); + } else { + if (isLogging()) { + Log.d("Avo Inspector", "Deduplicated event " + eventName); + } } + } catch (Exception e) { + Log.e("Avo Inspector", "Something went wrong. Please report to support@avo.app.", e); } } @@ -321,24 +344,30 @@ static void logPostExtract(@Nullable String eventName, @NonNull Map extractSchema(@Nullable Object eventProperties) { - sessionTracker.startOrProlongSession(System.currentTimeMillis()); - - Map eventPropsToCheck = new HashMap<>(); - if (eventProperties instanceof Map) { - eventPropsToCheck = (Map)eventProperties; - } else if (eventProperties instanceof JSONObject) { - eventPropsToCheck = Util.jsonToMap((JSONObject) eventProperties); - } try { - //noinspection unchecked - if (AvoDeduplicator.hasSeenEventParams(eventPropsToCheck, true)) { - Log.w("Avo Inspector", "WARNING! You are trying to extract schema shape that was just reported by your Avo functions. " + - "This is an indicator of duplicate inspector reporting. " + - "Please reach out to support@avo.app for advice if you are not sure how to handle this."); + sessionTracker.startOrProlongSession(System.currentTimeMillis()); + + Map eventPropsToCheck = new HashMap<>(); + if (eventProperties instanceof Map) { + eventPropsToCheck = (Map) eventProperties; + } else if (eventProperties instanceof JSONObject) { + eventPropsToCheck = Util.jsonToMap((JSONObject) eventProperties); + } + try { + //noinspection unchecked + if (AvoDeduplicator.hasSeenEventParams(eventPropsToCheck, true)) { + Log.w("Avo Inspector", "WARNING! You are trying to extract schema shape that was just reported by your Avo functions. " + + "This is an indicator of duplicate inspector reporting. " + + "Please reach out to support@avo.app for advice if you are not sure how to handle this."); + } + } catch (Exception ignored) { } - } catch (Exception ignored) {} - return avoSchemaExtractor.extractSchema(eventProperties, true); + return avoSchemaExtractor.extractSchema(eventProperties, true); + } catch (Exception e) { + Log.e("Avo Inspector", "Something went wrong. Please report to support@avo.app.", e); + return new HashMap<>(); + } } @Override