InAppMessage implements Parcelable, ScheduleData { @NonNull @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) public static InAppMessage fromJson(@NonNull JsonValue jsonValue, @Nullable @Source String defaultSource) throws JsonException { String type = jsonValue.optMap().opt(DISPLAY_TYPE_KEY).optString(); JsonValue content = jsonValue.optMap().opt(DISPLAY_CONTENT_KEY); String name = jsonValue.optMap().opt(NAME_KEY).getString(); if (name != null && name.length() > MAX_NAME_LENGTH) { throw new JsonException("Invalid message name. Must be less than or equal to " + MAX_NAME_LENGTH + " characters."); } InAppMessage.Builder builder = InAppMessage.newBuilder() .setName(name) .setExtras(jsonValue.optMap().opt(EXTRA_KEY).optMap()) .setDisplayContent(type, content); @Source String source = jsonValue.optMap().opt(SOURCE_KEY).getString(); if (source != null) { builder.setSource(source); } else if (defaultSource != null) { builder.setSource(defaultSource); } if (jsonValue.optMap().containsKey(ACTIONS_KEY)) { JsonMap jsonMap = jsonValue.optMap().opt(ACTIONS_KEY).getMap(); if (jsonMap == null) { throw new JsonException("Actions must be a JSON object: " + jsonValue.optMap().opt(ACTIONS_KEY)); } builder.setActions(jsonMap.getMap()); } if (jsonValue.optMap().containsKey(CAMPAIGNS_KEY)) { builder.setCampaigns(jsonValue.optMap().opt(CAMPAIGNS_KEY)); } if (jsonValue.optMap().containsKey(DISPLAY_BEHAVIOR_KEY)) { switch (jsonValue.optMap().opt(DISPLAY_BEHAVIOR_KEY).optString()) { case DISPLAY_BEHAVIOR_DEFAULT: builder.setDisplayBehavior(DISPLAY_BEHAVIOR_DEFAULT); break; case DISPLAY_BEHAVIOR_IMMEDIATE: builder.setDisplayBehavior(DISPLAY_BEHAVIOR_IMMEDIATE); break; default: throw new JsonException("Unexpected display behavior: " + jsonValue.optMap().get(DISPLAY_BEHAVIOR_IMMEDIATE)); } } if (jsonValue.optMap().containsKey(REPORTING_ENABLED_KEY)) { builder.setReportingEnabled(jsonValue.optMap().opt(REPORTING_ENABLED_KEY).getBoolean(true)); } if (jsonValue.optMap().containsKey(RENDERED_LOCALE_KEY)) { JsonMap jsonMap = jsonValue.optMap().opt(RENDERED_LOCALE_KEY).getMap(); if (jsonMap == null) { throw new JsonException("Rendered locale must be a JSON object: " + jsonValue.optMap().opt(RENDERED_LOCALE_KEY)); } if (!jsonMap.containsKey(RENDERED_LOCALE_LANGUAGE_KEY) && !jsonMap.containsKey(RENDERED_LOCALE_COUNTRY_KEY)) { throw new JsonException("Rendered locale must contain one of " + "\"" + RENDERED_LOCALE_LANGUAGE_KEY + "\" " + "or \"" + RENDERED_LOCALE_COUNTRY_KEY + "\" fields :" + jsonMap); } JsonValue languageValue = jsonMap.opt(RENDERED_LOCALE_LANGUAGE_KEY); if (!languageValue.isNull() && !languageValue.isString()) { throw new JsonException("Language must be a string: " + languageValue); } JsonValue countryValue = jsonMap.opt(RENDERED_LOCALE_COUNTRY_KEY); if (!countryValue.isNull() && !countryValue.isString()) { throw new JsonException("Country must be a string: " + countryValue); } builder.setRenderedLocale(jsonMap.getMap()); } try { return builder.build(); } catch (IllegalArgumentException e) { throw new JsonException("Invalid InAppMessage json.", e); } } private  InAppMessage(@NonNull Builder builder); @DisplayType @NonNull String getType(); @SuppressWarnings("unchecked") @Nullable T getDisplayContent(); @Nullable String getName(); @NonNull JsonMap getExtras(); @NonNull Map<String, JsonValue> getActions(); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @Source @NonNull String getSource(); @DisplayBehavior @NonNull String getDisplayBehavior(); boolean isReportingEnabled(); @NonNull @Override JsonValue toJsonValue(); @NonNull @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) static InAppMessage fromJson(@NonNull JsonValue jsonValue, @Nullable @Source String defaultSource); @NonNull static InAppMessage fromJson(@NonNull JsonValue jsonValue); @NonNull static Builder newBuilder(); @NonNull static Builder newBuilder(@NonNull InAppMessage message); @Override void writeToParcel(@NonNull Parcel dest, int flags); @Override int describeContents(); @NonNull @Override String toString(); @Override boolean equals(Object o); @Override int hashCode(); static final int MAX_NAME_LENGTH; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String SOURCE_LEGACY_PUSH; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String SOURCE_REMOTE_DATA; @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
    @NonNull
    static final String SOURCE_APP_DEFINED; @NonNull
    static final String TYPE_BANNER; @NonNull
    static final String TYPE_CUSTOM; @NonNull
    static final String TYPE_FULLSCREEN; @NonNull
    static final String TYPE_MODAL; @NonNull
    static final String TYPE_HTML; @NonNull
    static final String DISPLAY_BEHAVIOR_DEFAULT; @NonNull
    static final String DISPLAY_BEHAVIOR_IMMEDIATE; @NonNull
    static final Creator<InAppMessage> CREATOR; }
@Test public void testFromJson() throws JsonException { JsonMap actionsMap = JsonMap.newBuilder().put("action_value", "action_name").build(); JsonMap jsonMap = JsonMap.newBuilder() .put("display_type", "custom") .put("display", customDisplayContent.toJsonValue()) .put("message_id", "messageId") .put("name", "message name") .put("actions", actionsMap) .put("reporting_enabled", false) .put("display_behavior", "default") .put("rendered_locale", JsonValue.wrap(renderedLocale)) .build(); InAppMessage message = InAppMessage.fromJson(jsonMap.toJsonValue()); assertEquals("message name", message.getName()); assertEquals(InAppMessage.TYPE_CUSTOM, message.getType()); assertEquals(customDisplayContent, message.getDisplayContent()); assertEquals(actionsMap.getMap(), message.getActions()); assertEquals(InAppMessage.DISPLAY_BEHAVIOR_DEFAULT, message.getDisplayBehavior()); assertFalse(message.isReportingEnabled()); assertEquals(renderedLocale, message.getRenderedLocale()); }