<commit_before>package com.mixpanel.android.mpmetrics;

import android.content.Context;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/* package */ class DecideChecker {

    /* package */ static class Result {
        public Result() {
            surveys = new ArrayList<Survey>();
            notifications = new ArrayList<InAppNotification>();
        }
        public final List<Survey> surveys;
        public final List<InAppNotification> notifications;
    }

    public DecideChecker(final Context context, final MPConfig config) {
        mContext = context;
        mConfig = config;
        mChecks = new LinkedList<DecideUpdates>();
    }

    public void addDecideCheck(final DecideUpdates check) {
        mChecks.add(check);
    }

    public void runDecideChecks(final ServerMessage poster) {
        final Iterator<DecideUpdates> itr = mChecks.iterator();
        while (itr.hasNext()) {
            final DecideUpdates updates = itr.next();
            if (updates.isDestroyed()) {
                itr.remove();
            } else {
                final Result result = runDecideCheck(updates.getToken(), updates.getDistinctId(), poster);
                updates.reportResults(result.surveys, result.notifications);
            }
        }
    }

    private Result runDecideCheck(final String token, final String distinctId, final ServerMessage poster) {
        final String responseString = getDecideResponseFromServer(token, distinctId, poster);
        if (MPConfig.DEBUG) Log.d(LOGTAG, "Mixpanel decide server response was\n" + responseString);

        Result parsed = new Result();
        if (null != responseString) {
            parsed = parseDecideResponse(responseString);
        }

       return parsed;
    }// runDecideCheck

    /* package */ static Result parseDecideResponse(String responseString) {
        JSONObject response;
        final Result ret = new Result();

        try {
            response = new JSONObject(responseString);
        } catch (final JSONException e) {
            Log.e(LOGTAG, "Mixpanel endpoint returned unparsable result:\n" + responseString, e);
            return ret;
        }

        JSONArray surveys = null;
        if (response.has("surveys")) {
            try {
                surveys = response.getJSONArray("surveys");
            } catch (final JSONException e) {
                Log.e(LOGTAG, "Mixpanel endpoint returned non-array JSON for surveys: " + response);
            }
        }

        for (int i = 0; null != surveys && i < surveys.length(); i++) {
            try {
                final JSONObject surveyJson = surveys.getJSONObject(i);
                final Survey survey = new Survey(surveyJson);
                ret.surveys.add(survey);
            } catch (final JSONException e) {
                Log.e(LOGTAG, "Received a strange response from surveys service: " + surveys.toString());
            } catch (final BadDecideObjectException e) {
                Log.e(LOGTAG, "Received a strange response from surveys service: " + surveys.toString());
            }
        }

        JSONArray notifications = null;
        if (response.has("notifications")) {
            try {
                notifications = response.getJSONArray("notifications");
            } catch (final JSONException e) {
                Log.e(LOGTAG, "Mixpanel endpoint returned non-array JSON for notifications: " + response);
            }
        }

        for (int i = 0; null != notifications && i < notifications.length(); i++) {
            try {
                final JSONObject notificationJson = notifications.getJSONObject(i);
                final InAppNotification notification = new InAppNotification(notificationJson);
                ret.notifications.add(notification);
            } catch (final JSONException e) {
                Log.e(LOGTAG, "Received a strange response from notifications service: " + notifications.toString(), e);
            } catch (final BadDecideObjectException e) {
                Log.e(LOGTAG, "Received a strange response from notifications service: " + notifications.toString(), e);
            }
        }

        return ret;
    }

    private String getDecideResponseFromServer(String unescapedToken, String unescapedDistinctId, ServerMessage poster) {
        String escapedToken;
        String escapedId;
        try {
            escapedToken = URLEncoder.encode(unescapedToken, "utf-8");
            escapedId = URLEncoder.encode(unescapedDistinctId, "utf-8");
        } catch(final UnsupportedEncodingException e) {
            throw new RuntimeException("Mixpanel library requires utf-8 string encoding to be available", e);
        }
        final String checkQuery = new StringBuilder()
                .append("?version=1&lib=android&token=")
                .append(escapedToken)<commit_msg>Redownload images again! Yay!<commit_after>package com.mixpanel.android.mpmetrics;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

/* package */ class DecideChecker {

    /* package */ static class Result {
        public Result() {
            surveys = new ArrayList<Survey>();
            notifications = new ArrayList<InAppNotification>();
        }
        public final List<Survey> surveys;
        public final List<InAppNotification> notifications;
    }

    public DecideChecker(final Context context, final MPConfig config) {
        mContext = context;
        mConfig = config;
        mChecks = new LinkedList<DecideUpdates>();
    }

    public void addDecideCheck(final DecideUpdates check) {
        mChecks.add(check);
    }

    public void runDecideChecks(final ServerMessage poster) {
        final Iterator<DecideUpdates> itr = mChecks.iterator();
        while (itr.hasNext()) {
            final DecideUpdates updates = itr.next();
            if (updates.isDestroyed()) {
                itr.remove();
            } else {
                final Result result = runDecideCheck(updates.getToken(), updates.getDistinctId(), poster);
                updates.reportResults(result.surveys, result.notifications);
            }
        }
    }

    private Result runDecideCheck(final String token, final String distinctId, final ServerMessage poster) {
        final String responseString = getDecideResponseFromServer(token, distinctId, poster);
        if (MPConfig.DEBUG) Log.d(LOGTAG, "Mixpanel decide server response was\n" + responseString);

        Result parsed = new Result();
        if (null != responseString) {
            parsed = parseDecideResponse(responseString, mContext);
        }

       return parsed;
    }// runDecideCheck

    /* package */ static Result parseDecideResponse(String responseString, Context context) {
        JSONObject response;
        final Result ret = new Result();

        try {
            response = new JSONObject(responseString);
        } catch (final JSONException e) {
            Log.e(LOGTAG, "Mixpanel endpoint returned unparsable result:\n" + responseString, e);
            return ret;
        }

        JSONArray surveys = null;
        if (response.has("surveys")) {
            try {
                surveys = response.getJSONArray("surveys");
            } catch (final JSONException e) {
                Log.e(LOGTAG, "Mixpanel endpoint returned non-array JSON for surveys: " + response);
            }
        }

        for (int i = 0; null != surveys && i < surveys.length(); i++) {
            try {
                final JSONObject surveyJson = surveys.getJSONObject(i);
                final Survey survey = new Survey(surveyJson);
                ret.surveys.add(survey);
            } catch (final JSONException e) {
                Log.e(LOGTAG, "Received a strange response from surveys service: " + surveys.toString());
            } catch (final BadDecideObjectException e) {
                Log.e(LOGTAG, "Received a strange response from surveys service: " + surveys.toString());
            }
        }

        JSONArray notifications = null;
        if (response.has("notifications")) {
            try {
                notifications = response.getJSONArray("notifications");
            } catch (final JSONException e) {
                Log.e(LOGTAG, "Mixpanel endpoint returned non-array JSON for notifications: " + response);
            }
        }

        for (int i = 0; null != notifications && i < notifications.length(); i++) {
            try {
                final JSONObject notificationJson = notifications.getJSONObject(i);
                final InAppNotification notification = new InAppNotification(notificationJson);

                String imageUrl;
                if (notification.getType() == InAppNotification.Type.MINI) {
                    imageUrl = notification.getImageUrl();
                } else {
                    imageUrl = notification.getImage2xUrl();
                }
                if (MPConfig.DEBUG) Log.d(LOGTAG, "Downloading image from URL " + imageUrl);
                final ServerMessage imageMessage = new ServerMessage();
                final ServerMessage.Result result = imageMessage.get(context, imageUrl, null);
                if (result.getStatus() != ServerMessage.Status.SUCCEEDED) {
                    // Shouldn't drop this notification on the floor if this is a connectivity issue!
                    Log.i(LOGTAG, "Could not access image at " + imageUrl);
                } else {
                    final byte[] imageBytes = result.getResponseBytes();
                    final Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
                    if (null == image) {
                        Log.w(LOGTAG, "Notification referred to bad or corrupted image at " + imageUrl);
                    } else {
                        notification.setImage(image);
                    }
                }

                ret.notifications.add(notification);
            } catch (final JSONException e) {
                Log.e(LOGTAG, "Received a strange response from notifications service: " + notifications.toString(), e);
            } catch (final BadDecideObjectException e) {
                Log.e(LOGTAG, "Received a strange response from notifications service: " + notifications.toString(), e);
            }
        }

        return ret;
    }

    private String getDecideResponseFromServer(String unescapedToken, String unescapedDistinctId, ServerMessage poster) {
        String escapedToken;
        String escapedId;
        try {
            escapedToken = URLEncoder.encode(unescapedToken, "utf-8");
            escapedId = URLEncoder.encode(unescapedDistinctId, "utf-8");
        } catch(final UnsupportedEncodingException e) {
            throw new RuntimeException("Mixpanel library requires utf-8 string encoding to be available", e);
        }
        final String checkQuery = new StringBuilder()
                .append("?version=1&lib=android&token=")
                .append(escapedToken)
{'fraction_non_alphanumeric': 0.09387368783341937, 'fraction_numerical': 0.0012046119428669765, 'mean_word_length': 2.2045767852219464, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5106577', 'n_tokens_mistral': 2977, 'n_tokens_neox': 2888, 'n_words': 773}